Implement copper cable
This commit is contained in:
77
api.lua
77
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
|
||||
|
||||
Reference in New Issue
Block a user