Implement power storage nodes

This commit is contained in:
mrkubax10 2023-04-18 21:48:08 +02:00
parent 8e322b3d79
commit 432fd72597
2 changed files with 188 additions and 16 deletions

20
api.lua
View File

@ -201,6 +201,21 @@ industrialtest.api.transferPowerToItem=function(srcMeta,itemstack,amount)
srcMeta:set_int("industrialtest.powerAmount",srcMeta:get_int("industrialtest.powerAmount")-actualFlow)
return actualFlow
end
-- \brief Adds power to destination metadata while subtracting it from source itemstack
-- \param srcItemstack ItemStack from which subtract power
-- \param meta MetaDataRef to which add power
-- \param amount How much power should be transferred
-- \returns How much of power was actually transferred
industrialtest.api.transferPowerFromItem=function(srcItemstack,meta,amount)
local srcMeta=srcItemstack:get_meta()
local currentFlow=math.min(srcMeta:get_int("industrialtest.powerAmount"),amount)
if currentFlow==0 then
return 0
end
local actualFlow=industrialtest.api.addPower(meta,currentFlow)
industrialtest.api.addPowerToItem(srcItemstack,-actualFlow)
return actualFlow
end
-- \brief Transfers power from source node to all neighbouring nodes
-- \param pos Vector with position of source node
-- \returns two values: true if any neighbouring node has room for more power, false otherwise
@ -241,7 +256,9 @@ industrialtest.api.powerFlow=function(pos)
local roomAvailable=false
local transferred=false
for key,value in ipairs(neighbours) do
if industrialtest.api.isPowerOutput(meta,key) and value~=0 then
if value~=0 then
local normalizedKey=industrialtest.api.normalizeSide(pos,key)
if industrialtest.api.isPowerOutput(meta,normalizedKey) then
if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then
transferred=true
end
@ -262,6 +279,7 @@ industrialtest.api.powerFlow=function(pos)
end
end
end
end
return roomAvailable,transferred
end
-- \brief Starts node timer of neighbouring nodes with center node

View File

@ -128,6 +128,11 @@ local function registerMachine(config)
config.onMetadataInventoryPut(pos,listname,index,stack,player)
end
end,
on_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
if config.onMetadataInventoryPut then
config.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count)
end
end,
on_metadata_inventory_take=function(pos,listname,index,stack,player)
if config.onMetadataInventoryTake then
config.onMetadataInventoryTake(pos,listname,index,stack,player)
@ -1807,3 +1812,152 @@ minetest.register_craft({
{"","industrialtest:insulated_iron_cable",""}
}
})
-- Power storage nodes
local function registerPowerStorageNode(config)
local function getFormspec(pos)
local meta=minetest.get_meta(pos)
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
local formspec
if industrialtest.mtgAvailable then
formspec={
"list[context;charged;1,2.5;1,1]",
"listring[context;charged]",
"label[0.9,3.9;"..S("Charge").."]",
"list[context;discharged;3,2.5;1,1]",
"listring[context;discharged]",
"label[2.7,3.9;"..S("Discharge").."]",
"box[9,1;0.3,4.8;#202020]",
(charged>0 and "box[9,"..(1+4.8-(charged*4.8))..";0.3,"..(charged*4.8)..";#FF1010]" or "")
}
elseif industrialtest.mclAvailable then
formspec={
"list[context;charged;1,2.5;1,1]",
"listring[context;charged]",
mcl_formspec.get_itemslot_bg(1,2.5,1,1),
"label[0.9,3.9;"..S("Charge").."]",
"list[context;discharged;3,2.5;1,1]",
"listring[context;discharged]",
mcl_formspec.get_itemslot_bg(3,2.5,1,1),
"label[2.7,3.9;"..S("Discharge").."]",
"box[9,1;0.3,4.8;#202020]",
(charged>0 and "box[9,"..(1+4.8-(charged*4.8))..";0.3,"..(charged*4.8)..";#FF1010]" or "")
}
end
return table.concat(formspec,"")
end
registerMachine({
name=config.name,
displayName=config.displayName,
capacity=config.capacity,
flow=config.flow,
ioConfig="iiiioi",
sounds=config.sounds,
powerSlots={"charged","discharged"},
storageSlots={"charged","discharged"},
registerActiveVariant=false,
groups={
_industrialtest_hasPowerOutput=1,
_industrialtest_hasPowerInput=1
},
customKeys={
tiles={
config.machineBlockTexture,
config.machineBlockTexture,
config.machineBlockTexture,
config.machineBlockTexture,
config.machineBlockTexture,
config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png"
},
paramtype2="facedir",
legacy_facedir_simple=true
},
requiresWrench=config.requiresWrench,
getFormspec=getFormspec,
onConstruct=function(pos,meta,inv)
inv:set_size("charged",1)
inv:set_size("discharged",1)
end,
onTimer=function(pos,elapsed,meta,inv)
local chargedSlot=inv:get_stack("charged",1)
local dischargedSlot=inv:get_stack("discharged",1)
local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos)
local shouldUpdateFormspec=flowTransferred
local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0)
if chargedSlot:get_count()>0 and meta:get_int("industrialtest.powerAmount")>0 and industrialtest.api.transferPowerToItem(meta,chargedSlot,config.flow)>0 then
inv:set_stack("charged",1,chargedSlot)
shouldRerunTimer=true
shouldUpdateFormspec=true
end
if dischargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(meta) and industrialtest.api.transferPowerFromItem(dischargedSlot,meta,config.flow)>0 then
inv:set_stack("discharged",1,dischargedSlot)
shouldRerunTimer=true
shouldUpdateFormspec=true
end
if not industrialtest.api.isFullyCharged(meta) then
industrialtest.api.triggerNeighbours(pos)
end
return shouldRerunTimer,shouldUpdateFormspec
end,
onMetadataInventoryPut=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end,
onMetadataInventoryMove=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
})
end
registerPowerStorageNode({
name="batbox",
displayName="BatBox",
capacity=25000,
flow=industrialtest.api.lvPowerFlow,
sounds="wood",
machineBlockTexture="industrialtest_wood_machine_block.png",
requiresWrench=false
})
minetest.register_craft({
type="shaped",
output="industrialtest:batbox",
recipe={
{"group:wood","industrialtest:insulated_tin_cable","group:wood"},
{"industrialtest:re_battery","industrialtest:re_battery","industrialtest:re_battery"},
{"group:wood","group:wood","group:wood"}
}
})
registerPowerStorageNode({
name="cesu",
displayName="CESU",
capacity=400000,
flow=industrialtest.api.mvPowerFlow,
sounds="metal",
machineBlockTexture="industrialtest_bronze_machine_block.png",
requiresWrench=false
})
-- TODO: CESU craft
registerPowerStorageNode({
name="mfe",
displayName="MFE",
capacity=3000000,
flow=industrialtest.api.hvPowerFlow,
sounds="metal",
machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true
})
-- TODO: MFE craft
registerPowerStorageNode({
name="mfsu",
displayName="MFSU",
capacity=30000000,
flow=industrialtest.api.evPowerFlow,
sounds="metal",
machineBlockTexture="industrialtest_advanced_machine_block.png",
requiresWrench=false
})
-- TODO: MFSU craft