diff --git a/api.lua b/api.lua index 9726355..f4405bc 100644 --- a/api.lua +++ b/api.lua @@ -109,12 +109,12 @@ industrialtest.api.powerFlow=function(pos) end local neighboursContainingPower=0 for key,value in ipairs(neighbours) do - local strIndex=(key%2==0 and key-1 or key+1) - if industrialtest.api.hasPowerStorage(value) and string.sub(value:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="i" then + local side=industrialtest.api.getOppositeSide(key) + if industrialtest.api.hasPowerStorage(value) and industrialtest.api.isPowerInput(value,side) then neighboursContainingPower=neighboursContainingPower+1 else - table.remove(neighbourPositions,key) - table.remove(neighbours,key) + neighbourPositions[key]=0 + neighbours[key]=0 end end if neighboursContainingPower==0 then @@ -126,16 +126,23 @@ industrialtest.api.powerFlow=function(pos) local roomAvailable=false local transferred=false for key,value in ipairs(neighbours) do - if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then - transferred=true - end - local updateFormspec=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name]._industrialtest_updateFormspec - if updateFormspec then - updateFormspec(value) - end - minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay) - if not industrialtest.api.isFullyCharged(value) then - roomAvailable=true + if industrialtest.api.isPowerOutput(meta,key) and value~=0 then + if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then + transferred=true + end + local def=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name] + local updateFormspec=def._industrialtest_updateFormspec + if updateFormspec then + updateFormspec(value) + end + local onPowerFlow=def._industrialtest_onPowerFlow + if onPowerFlow and transferred then + onPowerFlow(neighbourPositions[key],industrialtest.api.getOppositeSide(key)) + end + minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay) + if not industrialtest.api.isFullyCharged(value) then + roomAvailable=true + end end end return roomAvailable,transferred @@ -151,9 +158,47 @@ industrialtest.api.triggerNeighbours=function(pos) } for key,value in ipairs(neighbourPositions) do local meta=minetest.get_meta(value) - local strIndex=(key%2==0 and key-1 or key+1) - if industrialtest.api.hasPowerStorage(meta) and string.sub(meta:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="o" then + local side=industrialtest.api.getOppositeSide(key) + if industrialtest.api.hasPowerStorage(meta) and industrialtest.api.isPowerOutput(meta,side) then minetest.get_node_timer(value):start(industrialtest.updateDelay) end end end +industrialtest.api.getOppositeSide=function(side) + return (side%2==0 and side-1 or side+1) +end +industrialtest.api.getConnections=function(pos) + local result={} + local neighbourPositions={ + vector.offset(pos,-1,0,0), + vector.offset(pos,1,0,0), + vector.offset(pos,0,-1,0), + vector.offset(pos,0,1,0), + vector.offset(pos,0,0,-1), + vector.offset(pos,0,0,1) + } + local index=1 + for key,value in ipairs(neighbourPositions) do + local meta=minetest.get_meta(value) + if industrialtest.api.hasPowerStorage(meta) then + result[index]=key + index=index+1 + end + end + return result +end +industrialtest.api.changeIoConfig=function(meta,side,mode) + local ioConfig=meta:get_string("industrialtest.ioConfig") + ioConfig=string.sub(ioConfig,1,side-1)..mode..string.sub(ioConfig,side+1) + meta:set_string("industrialtest.ioConfig",ioConfig) +end +industrialtest.api.isPowerInput=function(meta,side) + local ioConfig=meta:get_string("industrialtest.ioConfig") + local mode=string.sub(ioConfig,side,side) + return (mode=="i" or mode=="a") +end +industrialtest.api.isPowerOutput=function(meta,side) + local ioConfig=meta:get_string("industrialtest.ioConfig") + local mode=string.sub(ioConfig,side,side) + return (mode=="o" or mode=="a") +end diff --git a/cables.lua b/cables.lua new file mode 100644 index 0000000..6cd8b56 --- /dev/null +++ b/cables.lua @@ -0,0 +1,54 @@ +-- IndustrialTest +-- Copyright (C) 2023 mrkubax10 + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +local S=minetest.get_translator("industrialtest") + +local definition={ + description=S("Copper Cable"), + tiles={"industrialtest_copper_cable.png"}, + on_construct=function(pos) + local meta=minetest.get_meta(pos) + industrialtest.api.addPowerStorage(meta,390,390,"aaaaaa") + end, + on_timer=function(pos) + local meta=minetest.get_meta(pos) + local afterFlow,_=industrialtest.api.powerFlow(pos) + meta:set_string("industrialtest.ioConfig","aaaaaa") + if not industrialtest.api.isFullyCharged(meta) then + industrialtest.api.triggerNeighbours(pos) + end + return (afterFlow and meta:get_int("industrialtest.api.powerAmount")>0) + end, + _industrialtest_hasPowerInput=true, + _industrialtest_onPowerFlow=function(pos,side) + local meta=minetest.get_meta(pos) + industrialtest.api.changeIoConfig(meta,side,"i") + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end +} +if industrialtest.mtgAvailable then + definition.groups={ + cracky=1, + level=1, + oddly_breakable_by_hand=1 + } + definition.sound=default.node_sound_metal_defaults() +elseif industrialtest.mclAvailable then + definition.groups={pickaxey=1} + definition._mcl_blast_resistance=1 + definition._mcl_hardness=0.5 +end +minetest.register_node("industrialtest:copper_cable",definition) diff --git a/init.lua b/init.lua index ef5dff9..72e038c 100644 --- a/init.lua +++ b/init.lua @@ -34,3 +34,4 @@ dofile(modpath.."/nodes.lua") if industrialtest.developerMode then dofile(modpath.."/utils.lua") end +dofile(modpath.."/cables.lua") diff --git a/nodes.lua b/nodes.lua index 05296a2..ead65fe 100644 --- a/nodes.lua +++ b/nodes.lua @@ -43,20 +43,6 @@ minetest.register_craft({ minetest.register_on_placenode(function(pos,newNode) local def=minetest.registered_nodes[newNode.name] if def and def._industrialtest_hasPowerInput then - local neighbourPositions={ - vector.offset(pos,-1,0,0), - vector.offset(pos,1,0,0), - vector.offset(pos,0,-1,0), - vector.offset(pos,0,1,0), - vector.offset(pos,0,0,-1), - vector.offset(pos,0,0,1) - } - for key,value in ipairs(neighbourPositions) do - local meta=minetest.get_meta(value) - local strIndex=(key%2==0 and key-1 or key+1) - if industrialtest.api.hasPowerStorage(meta) and string.sub(meta:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="o" then - minetest.get_node_timer(value):start(1.0) - end - end + industrialtest.api.triggerNeighbours(pos) end end) diff --git a/utils.lua b/utils.lua index 986c540..b05a47c 100644 --- a/utils.lua +++ b/utils.lua @@ -36,8 +36,16 @@ local function inspectNode(pos,playerName) "field[0.5,5.4;2,0.5;powerAmount;"..S("Power Amount")..";"..powerAmount.."]", "field[0.5,6.2;2,0.5;powerIOConfig;"..S("Power IO Config")..";"..powerIOConfig.."]", "button[0.5,6.8;2,0.5;update;"..S("Update").."]", - "button[4.2,1.25;2.8,0.5;triggerNeighbours;"..S("Trigger Neighbours").."]" + "button[4.2,1.25;2.8,0.5;triggerNeighbours;"..S("Trigger Neighbours").."]", + "label[4.2,2.25;"..S("Connections:").."]" } + local connections=industrialtest.api.getConnections(pos) + local sides={"X-","X+","Y-","Y+","Z-","Z+"} + local sideString="" + for _,value in ipairs(connections) do + sideString=sideString..sides[value].." " + end + table.insert(formspec,"label[4.2,2.65;"..sideString.."]") powerStorageInspectorContext[playerName]=pos minetest.show_formspec(playerName,"industrialtest:power_storage_inspector_formspec",table.concat(formspec,"")) end