Refactor solar panels
This commit is contained in:
parent
329dd0e293
commit
0ac1dd0e49
@ -15,9 +15,35 @@
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
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 charging=amount>0.5
|
||||
local formspec={
|
||||
@ -27,71 +53,48 @@ solarPanel.getFormspec=function(pos)
|
||||
or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]"),
|
||||
"listring[context;charged]"
|
||||
}
|
||||
return table.concat(formspec,"")
|
||||
return parentFormspec..table.concat(formspec,"")
|
||||
end
|
||||
|
||||
solarPanel.onConstruct=function(pos,meta,inv)
|
||||
inv:set_size("charged",1)
|
||||
meta:set_float("prevAmount",0)
|
||||
function industrialtest.SolarPanelBase.register(self)
|
||||
industrialtest.ElectricMachine.register(self)
|
||||
table.insert(solarPanels,self.name)
|
||||
end
|
||||
|
||||
solarPanel.onTimer=function(pos,elapsed,meta,inv,config)
|
||||
local chargedSlot=inv:get_stack("charged",1)
|
||||
local shouldUpdateFormspec=false
|
||||
function industrialtest.SolarPanelBase.action(self,pos)
|
||||
local meta=minetest.get_meta(pos)
|
||||
local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0
|
||||
local charging=amount>0.5
|
||||
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
|
||||
self:updateFormspec(pos)
|
||||
end
|
||||
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
|
||||
industrialtest.api.powerFlow(pos)
|
||||
self:trigger(pos)
|
||||
end
|
||||
if amount~=meta:get_float("prevAmount") then
|
||||
shouldUpdateFormspec=true
|
||||
self:updateFormspec(pos)
|
||||
meta:set_float("prevAmount",amount)
|
||||
end
|
||||
return true,shouldUpdateFormspec
|
||||
end
|
||||
|
||||
local function registerSolarPanelGenerator(config)
|
||||
industrialtest.internal.registerMachine({
|
||||
name=config.name,
|
||||
displayName=config.displayName,
|
||||
getFormspec=solarPanel.getFormspec,
|
||||
capacity=config.capacity,
|
||||
flow=config.flow,
|
||||
ioConfig="oooooo",
|
||||
requiresWrench=true,
|
||||
registerActiveVariant=false,
|
||||
powerSlots={"charged"},
|
||||
storageSlots={"charged"},
|
||||
sounds="metal",
|
||||
groups={
|
||||
_industrialtest_hasPowerOutput=1
|
||||
},
|
||||
customKeys={
|
||||
industrialtest.SolarPanel=table.copy(industrialtest.SolarPanelBase)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.SolarPanel,{
|
||||
name="industrialtest:solar_panel",
|
||||
description=S("Solar Panel"),
|
||||
tiles={
|
||||
"industrialtest_machine_block.png^industrialtest_"..config.name.."_top.png",
|
||||
"industrialtest_machine_block.png^industrialtest_solar_panel_top.png",
|
||||
"industrialtest_machine_block.png",
|
||||
"industrialtest_machine_block.png",
|
||||
"industrialtest_machine_block.png",
|
||||
"industrialtest_machine_block.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,
|
||||
flow=industrialtest.api.lvPowerFlow
|
||||
})
|
||||
|
||||
industrialtest.SolarPanel:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:solar_panel",
|
||||
@ -102,12 +105,24 @@ minetest.register_craft({
|
||||
}
|
||||
})
|
||||
|
||||
registerSolarPanelGenerator({
|
||||
name="lv_solar_array",
|
||||
displayName=S("LV Solar Array"),
|
||||
industrialtest.LVSolarArray=table.copy(industrialtest.SolarPanelBase)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.LVSolarArray,{
|
||||
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,
|
||||
flow=industrialtest.api.lvPowerFlow*2
|
||||
})
|
||||
|
||||
industrialtest.LVSolarArray:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:lv_solar_array",
|
||||
@ -118,12 +133,24 @@ minetest.register_craft({
|
||||
}
|
||||
})
|
||||
|
||||
registerSolarPanelGenerator({
|
||||
name="mv_solar_array",
|
||||
displayName=S("MV Solar Array"),
|
||||
industrialtest.MVSolarArray=table.copy(industrialtest.SolarPanelBase)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.MVSolarArray,{
|
||||
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,
|
||||
flow=industrialtest.api.mvPowerFlow
|
||||
})
|
||||
|
||||
industrialtest.MVSolarArray:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:mv_solar_array",
|
||||
@ -134,12 +161,25 @@ minetest.register_craft({
|
||||
}
|
||||
})
|
||||
|
||||
registerSolarPanelGenerator({
|
||||
name="hv_solar_array",
|
||||
displayName=S("HV Solar Array"),
|
||||
|
||||
industrialtest.HVSolarArray=table.copy(industrialtest.SolarPanelBase)
|
||||
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,
|
||||
flow=industrialtest.api.hvPowerFlow
|
||||
})
|
||||
|
||||
industrialtest.HVSolarArray:register()
|
||||
|
||||
minetest.register_craft({
|
||||
type="shaped",
|
||||
output="industrialtest:hv_solar_array",
|
||||
@ -149,3 +189,14 @@ minetest.register_craft({
|
||||
{"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
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user