Implement Electric Furnace :O

This commit is contained in:
2023-03-07 20:41:35 +01:00
parent be74c6c2ee
commit e253564a91
4 changed files with 326 additions and 20 deletions

58
api.lua
View File

@@ -33,15 +33,20 @@ industrialtest.api.hasPowerStorage=function(meta)
end
return true
end
industrialtest.api.updateItemPowerText=function(itemstack)
local meta=itemstack:get_meta()
local def=minetest.registered_tools[itemstack:get_name()]
meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity")))
itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534)
end
industrialtest.api.preparePowerStorageItem=function(itemstack)
local meta=itemstack:get_meta()
local def=minetest.registered_tools[itemstack:get_name()]
if industrialtest.api.hasPowerStorage(meta) or not def or not def._industrialtest_powerStorage or not def._industrialtest_powerCapacity or not def._industrialtest_powerFlow then
return false
end
itemstack:set_wear(65535)
industrialtest.api.addPowerStorage(meta,def._industrialtest_powerCapacity,def._industrialtest_powerFlow,"n/a")
meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity")))
industrialtest.api.updateItemPowerText(itemstack)
return true
end
industrialtest.api.isFullyCharged=function(meta)
@@ -60,13 +65,11 @@ industrialtest.api.addPower=function(meta,amount)
end
industrialtest.api.addPowerToItem=function(itemstack,amount)
local meta=itemstack:get_meta()
local def=minetest.registered_tools[itemstack:get_name()]
if not industrialtest.api.hasPowerStorage(meta) then
return 0
end
local added=industrialtest.api.addPower(meta,amount)
itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534)
meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity")))
industrialtest.api.updateItemPowerText(itemstack)
return added
end
industrialtest.api.transferPower=function(srcMeta,destMeta,amount)
@@ -90,7 +93,7 @@ end
industrialtest.api.powerFlow=function(pos)
local meta=minetest.get_meta(pos)
if not industrialtest.api.hasPowerStorage(meta) then
return
return false
end
local neighbourPositions={
vector.offset(pos,-1,0,0),
@@ -106,20 +109,51 @@ industrialtest.api.powerFlow=function(pos)
end
local neighboursContainingPower=0
for key,value in ipairs(neighbours) do
if industrialtest.api.hasPowerStorage(value) and value:get_string("industrialtest.ioConfig")[key%2==0 and key+1 or key-1]=="i" then
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
neighboursContainingPower=neighboursContainingPower+1
else
neighbourPositions[key]=nil
neighbours[key]=nil
table.remove(neighbourPositions,key)
table.remove(neighbours,key)
end
end
if neighboursContainingPower==0 then
return
return false
end
local powerFlow=meta:get_int("industrialtest.powerFlow")
local powerDistribution=math.floor(powerFlow/neighboursContainingPower)
-- TODO: if supplying machine power flow is too large for receiving machine to handle then that machine should explode
for _,value in ipairs(neighbours) do
industrialtest.api.transferPower(meta,value,powerDistribution)
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
end
end
return roomAvailable,transferred
end
industrialtest.api.triggerNeighbours=function(pos)
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(industrialtest.updateDelay)
end
end
end