Refactor Generator
This commit is contained in:
parent
83ee6fb4c8
commit
78637a4759
@ -15,136 +15,26 @@
|
|||||||
-- 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 generator={}
|
industrialtest.Generator=table.copy(industrialtest.ActivatedElectricMachine)
|
||||||
|
industrialtest.internal.unpackTableInto(industrialtest.Generator,{
|
||||||
generator.getFormspec=function(pos)
|
name="industrialtest:generator",
|
||||||
local meta=minetest.get_meta(pos)
|
description=S("Generator"),
|
||||||
local fuelPercent=meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100
|
|
||||||
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
|
|
||||||
local formspec={
|
|
||||||
"list[context;charged;4.7,1.8;1,1]",
|
|
||||||
industrialtest.internal.getItemSlotBg(4.7,1.8,1,1),
|
|
||||||
(fuelPercent>0 and "image[4.7,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]"
|
|
||||||
or "image[4.7,2.8;1,1;default_furnace_fire_bg.png]"),
|
|
||||||
"list[context;src;4.7,3.9;1,1]",
|
|
||||||
industrialtest.internal.getItemSlotBg(4.7,3.9,1,1),
|
|
||||||
"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 ""),
|
|
||||||
"listring[context;src]"
|
|
||||||
}
|
|
||||||
return table.concat(formspec,"")
|
|
||||||
end
|
|
||||||
|
|
||||||
generator.onConstruct=function(pos,meta,inv)
|
|
||||||
inv:set_size("charged",1)
|
|
||||||
inv:set_size("src",1)
|
|
||||||
meta:set_int("fuelTime",0)
|
|
||||||
meta:set_int("maxFuelTime",1)
|
|
||||||
end
|
|
||||||
|
|
||||||
generator.onTimer=function(pos,elapsed,meta,inv)
|
|
||||||
local powerFlow=meta:get_int("industrialtest.powerFlow")
|
|
||||||
local chargedSlot=inv:get_stack("charged",1)
|
|
||||||
local fuelSlot=inv:get_stack("src",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 not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then
|
|
||||||
industrialtest.api.transferPowerToItem(meta,chargedSlot,powerFlow)
|
|
||||||
inv:set_stack("charged",1,chargedSlot)
|
|
||||||
shouldUpdateFormspec=true
|
|
||||||
shouldRerunTimer=true
|
|
||||||
end
|
|
||||||
if fuelSlot:get_count()>0 and meta:get_int("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then
|
|
||||||
local output,after=minetest.get_craft_result({
|
|
||||||
method="fuel",
|
|
||||||
width=1,
|
|
||||||
items={fuelSlot}
|
|
||||||
})
|
|
||||||
if output.time>0 then
|
|
||||||
meta:set_int("fuelTime",output.time)
|
|
||||||
meta:set_int("maxFuelTime",output.time)
|
|
||||||
inv:set_stack("src",1,after.items[1])
|
|
||||||
minetest.swap_node(pos,{
|
|
||||||
name="industrialtest:generator_active",
|
|
||||||
param2=minetest.get_node(pos).param2
|
|
||||||
})
|
|
||||||
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return shouldRerunTimer,shouldUpdateFormspec
|
|
||||||
end
|
|
||||||
|
|
||||||
generator.activeOnTimer=function(pos,elapsed,meta,inv)
|
|
||||||
local chargedSlot=inv:get_stack("charged",1)
|
|
||||||
local fuelSlot=inv:get_stack("src",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 not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then
|
|
||||||
industrialtest.api.transferPowerToItem(meta,chargedSlot,chargedSlot:get_meta():get_int("industrialtest.powerFlow"))
|
|
||||||
inv:set_stack("charged",1,chargedSlot)
|
|
||||||
shouldUpdateFormspec=true
|
|
||||||
shouldRerunTimer=true
|
|
||||||
end
|
|
||||||
if fuelSlot:get_count()>0 and meta:get_int("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then
|
|
||||||
local output,after=minetest.get_craft_result({
|
|
||||||
method="fuel",
|
|
||||||
width=1,
|
|
||||||
items={fuelSlot}
|
|
||||||
})
|
|
||||||
if output.time>0 then
|
|
||||||
meta:set_int("fuelTime",output.time)
|
|
||||||
meta:set_int("maxFuelTime",output.time)
|
|
||||||
inv:set_stack("src",1,after.items[1])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if meta:get_int("fuelTime")>0 then
|
|
||||||
meta:set_int("fuelTime",meta:get_int("fuelTime")-elapsed)
|
|
||||||
industrialtest.api.addPower(meta,200)
|
|
||||||
shouldUpdateFormspec=true
|
|
||||||
shouldRerunTimer=true
|
|
||||||
else
|
|
||||||
minetest.swap_node(pos,{
|
|
||||||
name="industrialtest:generator",
|
|
||||||
param2=minetest.get_node(pos).param2
|
|
||||||
})
|
|
||||||
end
|
|
||||||
return shouldRerunTimer,shouldUpdateFormspec
|
|
||||||
end
|
|
||||||
|
|
||||||
generator.metadataChange=function(pos)
|
|
||||||
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
|
|
||||||
end
|
|
||||||
|
|
||||||
industrialtest.internal.registerMachine({
|
|
||||||
name="generator",
|
|
||||||
displayName=S("Generator"),
|
|
||||||
getFormspec=generator.getFormspec,
|
|
||||||
capacity=7000,
|
|
||||||
flow=industrialtest.api.lvPowerFlow,
|
|
||||||
ioConfig="oooooo",
|
|
||||||
registerActiveVariant=true,
|
|
||||||
powerSlots={"charged"},
|
|
||||||
storageSlots={"charged","src"},
|
|
||||||
sounds="metal",
|
|
||||||
groups={
|
|
||||||
_industrialtest_hasPowerOutput=1
|
|
||||||
},
|
|
||||||
customKeys={
|
|
||||||
tiles={
|
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_machine_block.png",
|
"industrialtest_machine_block.png",
|
||||||
"industrialtest_machine_block.png",
|
"industrialtest_machine_block.png",
|
||||||
"industrialtest_machine_block.png^industrialtest_iron_furnace_front.png",
|
"industrialtest_machine_block.png^industrialtest_iron_furnace_front.png",
|
||||||
"industrialtest_machine_block.png"
|
"industrialtest_machine_block.png"
|
||||||
},
|
|
||||||
paramtype2="facedir",
|
|
||||||
legacy_facedir_simple=true
|
|
||||||
},
|
},
|
||||||
activeCustomKeys={
|
sounds="metal",
|
||||||
|
facedir=true,
|
||||||
|
storageLists={
|
||||||
|
"src",
|
||||||
|
"charged"
|
||||||
|
},
|
||||||
|
active={
|
||||||
tiles={
|
tiles={
|
||||||
"industrialtest_machine_block.png",
|
"industrialtest_machine_block.png",
|
||||||
"industrialtest_machine_block.png",
|
"industrialtest_machine_block.png",
|
||||||
@ -154,15 +44,124 @@ industrialtest.internal.registerMachine({
|
|||||||
"industrialtest_machine_block.png^industrialtest_iron_furnace_front_active.png",
|
"industrialtest_machine_block.png^industrialtest_iron_furnace_front_active.png",
|
||||||
"industrialtest_machine_block.png"
|
"industrialtest_machine_block.png"
|
||||||
},
|
},
|
||||||
light_source=8
|
lightSource=8
|
||||||
},
|
},
|
||||||
onConstruct=generator.onConstruct,
|
capacity=7000,
|
||||||
onTimer=generator.onTimer,
|
flow=industrialtest.api.lvPowerFlow,
|
||||||
activeOnTimer=generator.activeOnTimer,
|
ioConfig="oooooo",
|
||||||
onMetadataInventoryPut=generator.metadataChange,
|
hasPowerOutput=true,
|
||||||
onMetadataInventoryMove=generator.metadataChange
|
powerLists={"charged"}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function industrialtest.Generator.onConstruct(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
local inv=meta:get_inventory()
|
||||||
|
inv:set_size("charged",1)
|
||||||
|
inv:set_size("src",1)
|
||||||
|
meta:set_float("fuelTime",0)
|
||||||
|
meta:set_float("maxFuelTime",1)
|
||||||
|
industrialtest.ActivatedElectricMachine.onConstruct(self,pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Generator.getFormspec(self,pos)
|
||||||
|
local parentFormspec=industrialtest.ActivatedElectricMachine.getFormspec(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
local fuelPercent=meta:get_float("fuelTime")/meta:get_float("maxFuelTime")*100
|
||||||
|
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
|
||||||
|
local formspec={
|
||||||
|
"list[context;charged;4.7,1.8;1,1]",
|
||||||
|
industrialtest.internal.getItemSlotBg(4.7,1.8,1,1),
|
||||||
|
(fuelPercent>0 and "image[4.7,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]"
|
||||||
|
or "image[4.7,2.8;1,1;default_furnace_fire_bg.png]"),
|
||||||
|
"list[context;src;4.7,3.9;1,1]",
|
||||||
|
industrialtest.internal.getItemSlotBg(4.7,3.9,1,1),
|
||||||
|
self.createPowerIndicatorWidget(charged,9,1),
|
||||||
|
"listring[context;src]"
|
||||||
|
}
|
||||||
|
return parentFormspec..table.concat(formspec,"")
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Generator.onMetadataInventoryPut(self,pos,listname,index,stack)
|
||||||
|
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
|
||||||
|
industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack)
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Generator.activeUpdate(self,pos,elapsed,meta,inv)
|
||||||
|
local chargedSlot=inv:get_stack("charged",1)
|
||||||
|
local fuelSlot=inv:get_stack("src",1)
|
||||||
|
local shouldUpdateFormspec=false
|
||||||
|
local shouldRerunTimer=false
|
||||||
|
|
||||||
|
if fuelSlot:get_count()>0 and meta:get_float("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then
|
||||||
|
local output,after=minetest.get_craft_result({
|
||||||
|
method="fuel",
|
||||||
|
width=1,
|
||||||
|
items={fuelSlot}
|
||||||
|
})
|
||||||
|
if output.time>0 then
|
||||||
|
meta:set_float("fuelTime",output.time)
|
||||||
|
meta:set_float("maxFuelTime",output.time)
|
||||||
|
inv:set_stack("src",1,after.items[1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if meta:get_float("fuelTime")>0 then
|
||||||
|
meta:set_float("fuelTime",meta:get_float("fuelTime")-elapsed)
|
||||||
|
industrialtest.api.addPower(meta,200)
|
||||||
|
shouldUpdateFormspec=true
|
||||||
|
shouldRerunTimer=true
|
||||||
|
end
|
||||||
|
|
||||||
|
return shouldRerunTimer,shouldUpdateFormspec
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Generator.shouldActivate(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
local inv=meta:get_inventory()
|
||||||
|
local fuelSlot=inv:get_stack("src",1)
|
||||||
|
|
||||||
|
if fuelSlot:get_count()>0 and not industrialtest.api.isFullyCharged(meta) then
|
||||||
|
local output,after=minetest.get_craft_result({
|
||||||
|
method="fuel",
|
||||||
|
width=1,
|
||||||
|
items={fuelSlot}
|
||||||
|
})
|
||||||
|
if output.time>0 then
|
||||||
|
meta:set_float("fuelTime",output.time)
|
||||||
|
meta:set_float("maxFuelTime",output.time)
|
||||||
|
inv:set_stack("src",1,after.items[1])
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Generator.shouldDeactivate(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
|
||||||
|
if meta:get_float("fuelTime")>0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local inv=meta:get_inventory()
|
||||||
|
local fuelSlot=inv:get_stack("src",1)
|
||||||
|
if fuelSlot:get_count()>0 and not industrialtest.api.isFullyCharged(meta) then
|
||||||
|
local output,after=minetest.get_craft_result({
|
||||||
|
method="fuel",
|
||||||
|
width=1,
|
||||||
|
items={fuelSlot}
|
||||||
|
})
|
||||||
|
if output.time>0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
industrialtest.Generator:register()
|
||||||
|
|
||||||
minetest.register_craft({
|
minetest.register_craft({
|
||||||
type="shaped",
|
type="shaped",
|
||||||
output="industrialtest:generator",
|
output="industrialtest:generator",
|
||||||
|
Loading…
Reference in New Issue
Block a user