Refactor solar panels

This commit is contained in:
mrkubax10 2025-04-04 19:57:46 +02:00
parent 329dd0e293
commit 0ac1dd0e49

View File

@ -15,9 +15,35 @@
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
local S=minetest.get_translator("industrialtest") local S=minetest.get_translator("industrialtest")
local solarPanel={} industrialtest.SolarPanelBase=table.copy(industrialtest.ElectricMachine)
industrialtest.internal.unpackTableInto(industrialtest.SolarPanelBase,{
sounds="metal",
requiresWrench=true,
storageLists={
"charged"
},
powerLists={
{
list="charged",
direction="o"
}
},
hasPowerOutput=true,
ioConfig="oooooo"
})
solarPanel.getFormspec=function(pos) local solarPanels={}
function industrialtest.SolarPanelBase.onConstruct(self,pos)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
inv:set_size("charged",1)
meta:set_float("prevAmount",0)
industrialtest.ElectricMachine.onConstruct(self,pos)
end
function industrialtest.SolarPanelBase.getFormspec(self,pos)
local parentFormspec=industrialtest.ElectricMachine.getFormspec(self,pos)
local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0 local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0
local charging=amount>0.5 local charging=amount>0.5
local formspec={ local formspec={
@ -27,71 +53,48 @@ solarPanel.getFormspec=function(pos)
or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]"), or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]"),
"listring[context;charged]" "listring[context;charged]"
} }
return table.concat(formspec,"") return parentFormspec..table.concat(formspec,"")
end end
solarPanel.onConstruct=function(pos,meta,inv) function industrialtest.SolarPanelBase.register(self)
inv:set_size("charged",1) industrialtest.ElectricMachine.register(self)
meta:set_float("prevAmount",0) table.insert(solarPanels,self.name)
end end
solarPanel.onTimer=function(pos,elapsed,meta,inv,config) function industrialtest.SolarPanelBase.action(self,pos)
local chargedSlot=inv:get_stack("charged",1) local meta=minetest.get_meta(pos)
local shouldUpdateFormspec=false
local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0 local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0
local charging=amount>0.5 local charging=amount>0.5
if charging then if charging then
industrialtest.api.addPower(meta,math.ceil(amount*config.flow*elapsed)) if industrialtest.api.addPower(meta,math.ceil(amount*self.flow))>0 then
end self:updateFormspec(pos)
if meta:get_int("industrialtest.powerAmount")>0 then
if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) then
industrialtest.api.transferPowerToItem(meta,chargedSlot,math.ceil(config.flow*elapsed))
inv:set_stack("charged",1,chargedSlot)
end end
industrialtest.api.powerFlow(pos) self:trigger(pos)
end end
if amount~=meta:get_float("prevAmount") then if amount~=meta:get_float("prevAmount") then
shouldUpdateFormspec=true self:updateFormspec(pos)
meta:set_float("prevAmount",amount) meta:set_float("prevAmount",amount)
end end
return true,shouldUpdateFormspec
end end
local function registerSolarPanelGenerator(config) industrialtest.SolarPanel=table.copy(industrialtest.SolarPanelBase)
industrialtest.internal.registerMachine({ industrialtest.internal.unpackTableInto(industrialtest.SolarPanel,{
name=config.name, name="industrialtest:solar_panel",
displayName=config.displayName, description=S("Solar Panel"),
getFormspec=solarPanel.getFormspec, tiles={
capacity=config.capacity, "industrialtest_machine_block.png^industrialtest_solar_panel_top.png",
flow=config.flow, "industrialtest_machine_block.png",
ioConfig="oooooo", "industrialtest_machine_block.png",
requiresWrench=true, "industrialtest_machine_block.png",
registerActiveVariant=false, "industrialtest_machine_block.png",
powerSlots={"charged"}, "industrialtest_machine_block.png"
storageSlots={"charged"}, },
sounds="metal",
groups={
_industrialtest_hasPowerOutput=1
},
customKeys={
tiles={
"industrialtest_machine_block.png^industrialtest_"..config.name.."_top.png",
"industrialtest_machine_block.png"
}
},
onConstruct=solarPanel.onConstruct,
onTimer=function(pos,elapsed,meta,inv)
return solarPanel.onTimer(pos,elapsed,meta,inv,config)
end
})
end
registerSolarPanelGenerator({
name="solar_panel",
displayName=S("Solar Panel"),
capacity=industrialtest.api.lvPowerFlow*2, capacity=industrialtest.api.lvPowerFlow*2,
flow=industrialtest.api.lvPowerFlow flow=industrialtest.api.lvPowerFlow
}) })
industrialtest.SolarPanel:register()
minetest.register_craft({ minetest.register_craft({
type="shaped", type="shaped",
output="industrialtest:solar_panel", output="industrialtest:solar_panel",
@ -102,12 +105,24 @@ minetest.register_craft({
} }
}) })
registerSolarPanelGenerator({ industrialtest.LVSolarArray=table.copy(industrialtest.SolarPanelBase)
name="lv_solar_array", industrialtest.internal.unpackTableInto(industrialtest.LVSolarArray,{
displayName=S("LV Solar Array"), name="industrialtest:lv_solar_array",
description=S("LV Solar Array"),
tiles={
"industrialtest_machine_block.png^industrialtest_lv_solar_array_top.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png"
},
capacity=industrialtest.api.lvPowerFlow*4, capacity=industrialtest.api.lvPowerFlow*4,
flow=industrialtest.api.lvPowerFlow*2 flow=industrialtest.api.lvPowerFlow*2
}) })
industrialtest.LVSolarArray:register()
minetest.register_craft({ minetest.register_craft({
type="shaped", type="shaped",
output="industrialtest:lv_solar_array", output="industrialtest:lv_solar_array",
@ -118,12 +133,24 @@ minetest.register_craft({
} }
}) })
registerSolarPanelGenerator({ industrialtest.MVSolarArray=table.copy(industrialtest.SolarPanelBase)
name="mv_solar_array", industrialtest.internal.unpackTableInto(industrialtest.MVSolarArray,{
displayName=S("MV Solar Array"), name="industrialtest:mv_solar_array",
description=S("MV Solar Array"),
tiles={
"industrialtest_machine_block.png^industrialtest_mv_solar_array_top.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png"
},
capacity=industrialtest.api.mvPowerFlow*2, capacity=industrialtest.api.mvPowerFlow*2,
flow=industrialtest.api.mvPowerFlow flow=industrialtest.api.mvPowerFlow
}) })
industrialtest.MVSolarArray:register()
minetest.register_craft({ minetest.register_craft({
type="shaped", type="shaped",
output="industrialtest:mv_solar_array", output="industrialtest:mv_solar_array",
@ -134,12 +161,25 @@ minetest.register_craft({
} }
}) })
registerSolarPanelGenerator({
name="hv_solar_array", industrialtest.HVSolarArray=table.copy(industrialtest.SolarPanelBase)
displayName=S("HV Solar Array"), industrialtest.internal.unpackTableInto(industrialtest.HVSolarArray,{
name="industrialtest:hv_solar_array",
description=S("HV Solar Array"),
tiles={
"industrialtest_machine_block.png^industrialtest_hv_solar_array_top.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png",
"industrialtest_machine_block.png"
},
capacity=industrialtest.api.hvPowerFlow*2, capacity=industrialtest.api.hvPowerFlow*2,
flow=industrialtest.api.hvPowerFlow flow=industrialtest.api.hvPowerFlow
}) })
industrialtest.HVSolarArray:register()
minetest.register_craft({ minetest.register_craft({
type="shaped", type="shaped",
output="industrialtest:hv_solar_array", output="industrialtest:hv_solar_array",
@ -149,3 +189,14 @@ minetest.register_craft({
{"industrialtest:mv_solar_array","industrialtest:mv_solar_array","industrialtest:mv_solar_array"} {"industrialtest:mv_solar_array","industrialtest:mv_solar_array","industrialtest:mv_solar_array"}
} }
}) })
minetest.register_abm({
name="Solar panel updating",
nodenames=solarPanels,
interval=industrialtest.updateDelay,
chance=1,
action=function(pos,node)
local def=minetest.registered_nodes[node.name]
def._industrialtest_self:action(pos)
end
})