Refactor power storage

This commit is contained in:
mrkubax10 2025-03-27 22:54:31 +01:00
parent 6c9c69a308
commit 69aadeae71
2 changed files with 120 additions and 111 deletions

View File

@ -98,7 +98,7 @@ function industrialtest.ElectricMachine.allowMetadataInventoryMove(self,pos,from
local found=false
if self.powerLists then
for _,value in ipairs(self.powerLists) do
if value==toList then
if value.list==toList then
found=true
break
end
@ -115,7 +115,7 @@ function industrialtest.ElectricMachine.allowMetadataInventoryPut(self,pos,listn
local found=false
if self.powerLists then
for _,value in ipairs(self.powerLists) do
if value==listname then
if value.list==listname then
found=true
break
end
@ -129,15 +129,25 @@ function industrialtest.ElectricMachine.allowMetadataInventoryPut(self,pos,listn
end
function industrialtest.ElectricMachine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
if toList=="charged" then
self:trigger(pos)
if self.powerLists then
for _,value in ipairs(self.powerLists) do
if value.list==toList then
self:trigger(pos)
break
end
end
end
industrialtest.Machine.onMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count)
end
function industrialtest.ElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack)
if listname=="charged" then
self:trigger(pos)
if self.powerLists then
for _,value in ipairs(self.powerLists) do
if value.list==listname then
self:trigger(pos)
break
end
end
end
industrialtest.Machine.onMetadataInventoryPut(self,pos,listname,index,stack)
end

View File

@ -15,9 +15,38 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest")
local powerStorage={}
industrialtest.PowerStorage=table.copy(industrialtest.ElectricMachine)
industrialtest.internal.unpackTableInto(industrialtest.PowerStorage,{
facedir=true,
storageLists={
"charged",
"discharged"
},
powerLists={
{
list="charged",
direction="o"
},
{
list="discharged",
direction="i"
}
},
hasPowerInput=true,
hasPowerOutput=true,
ioConfig="iiiioi"
})
powerStorage.getFormspec=function(pos)
function industrialtest.PowerStorage.onConstruct(self,pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
inv:set_size("charged",1)
inv:set_size("discharged",1)
industrialtest.ElectricMachine.onConstruct(self,pos)
end
function industrialtest.PowerStorage.getFormspec(self,pos)
local parentFormspec=industrialtest.ElectricMachine.getFormspec(self,pos)
local meta=minetest.get_meta(pos)
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
local formspec={
@ -27,95 +56,32 @@ powerStorage.getFormspec=function(pos)
"list[context;discharged;3,2.5;1,1]",
industrialtest.internal.getItemSlotBg(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 ""),
self.createPowerIndicatorWidget(charged,9,1),
"listring[context;charged]",
"listring[context;discharged]"
}
return table.concat(formspec,"")
return parentFormspec..table.concat(formspec,"")
end
powerStorage.onConstruct=function(pos,meta,inv)
inv:set_size("charged",1)
inv:set_size("discharged",1)
end
powerStorage.onTimer=function(pos,elapsed,meta,inv,config)
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
return shouldRerunTimer,shouldUpdateFormspec
end
powerStorage.onMetadataInventoryPut=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
powerStorage.onMetadataInventoryMove=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
local function registerPowerStorageNode(config)
industrialtest.internal.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=powerStorage.getFormspec,
onConstruct=powerStorage.onConstruct,
onTimer=function(pos,elapsed,meta,inv)
return powerStorage.onTimer(pos,elapsed,meta,inv,config)
end,
onMetadataInventoryPut=powerStorage.onMetadataInventoryPut,
onMetadataInventoryMove=powerStorage.onMetadataInventoryMove
})
end
registerPowerStorageNode({
name="batbox",
displayName=S("BatBox"),
capacity=25000,
flow=industrialtest.api.lvPowerFlow,
industrialtest.BatBox=table.copy(industrialtest.PowerStorage)
industrialtest.internal.unpackTableInto(industrialtest.BatBox,{
name="industrialtest:batbox",
description=S("BatBox"),
tiles={
"industrialtest_wood_machine_block.png",
"industrialtest_wood_machine_block.png",
"industrialtest_wood_machine_block.png",
"industrialtest_wood_machine_block.png",
"industrialtest_wood_machine_block.png",
"industrialtest_wood_machine_block.png^industrialtest_batbox_front.png"
},
sounds="wood",
machineBlockTexture="industrialtest_wood_machine_block.png",
requiresWrench=false
capacity=25000,
flow=industrialtest.api.lvPowerFlow
})
industrialtest.BatBox:register()
minetest.register_craft({
type="shaped",
output="industrialtest:batbox",
@ -126,15 +92,25 @@ minetest.register_craft({
}
})
registerPowerStorageNode({
name="cesu",
displayName=S("CESU"),
capacity=400000,
flow=industrialtest.api.mvPowerFlow,
industrialtest.CESU=table.copy(industrialtest.PowerStorage)
industrialtest.internal.unpackTableInto(industrialtest.CESU,{
name="industrialtest:cesu",
description=S("CESU"),
tiles={
"industrialtest_bronze_machine_block.png",
"industrialtest_bronze_machine_block.png",
"industrialtest_bronze_machine_block.png",
"industrialtest_bronze_machine_block.png",
"industrialtest_bronze_machine_block.png",
"industrialtest_bronze_machine_block.png^industrialtest_cesu_front.png"
},
sounds="metal",
machineBlockTexture="industrialtest_bronze_machine_block.png",
requiresWrench=false
capacity=400000,
flow=industrialtest.api.mvPowerFlow
})
industrialtest.CESU:register()
minetest.register_craft({
type="shaped",
output="industrialtest:cesu",
@ -145,15 +121,26 @@ minetest.register_craft({
}
})
registerPowerStorageNode({
name="mfe",
displayName=S("MFE"),
capacity=3000000,
flow=industrialtest.api.hvPowerFlow,
industrialtest.MFE=table.copy(industrialtest.PowerStorage)
industrialtest.internal.unpackTableInto(industrialtest.MFE,{
name="industrialtest:mfe",
description=S("MFE"),
tiles={
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png^industrialtest_mfe_front.png"
},
sounds="metal",
machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true
requiresWrench=true,
capacity=3000000,
flow=industrialtest.api.hvPowerFlow
})
industrialtest.MFE:register()
minetest.register_craft({
type="shaped",
output="industrialtest:mfe",
@ -164,15 +151,27 @@ minetest.register_craft({
}
})
registerPowerStorageNode({
name="mfsu",
displayName=S("MFSU"),
capacity=30000000,
flow=industrialtest.api.evPowerFlow,
industrialtest.MFSU=table.copy(industrialtest.PowerStorage)
industrialtest.internal.unpackTableInto(industrialtest.MFSU,{
name="industrialtest:mfsu",
description=S("MFSU"),
tiles={
"industrialtest_advanced_machine_block.png",
"industrialtest_advanced_machine_block.png",
"industrialtest_advanced_machine_block.png",
"industrialtest_advanced_machine_block.png",
"industrialtest_advanced_machine_block.png",
"industrialtest_advanced_machine_block.png^industrialtest_mfsu_front.png"
},
sounds="metal",
machineBlockTexture="industrialtest_advanced_machine_block.png",
requiresWrench=false
requiresWrench=true,
capacity=30000000,
flow=industrialtest.api.evPowerFlow
})
industrialtest.MFSU:register()
minetest.register_craft({
type="shaped",
output="industrialtest:mfsu",