Refactor Chargepad
This commit is contained in:
parent
8f529ad6c3
commit
371ef36ce3
@ -15,11 +15,71 @@
|
|||||||
-- 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 chargepad={}
|
|
||||||
industrialtest.internal.chargepads={}
|
industrialtest.internal.chargepads={}
|
||||||
|
industrialtest.Chargepad=table.copy(industrialtest.ActivatedElectricMachine)
|
||||||
|
industrialtest.internal.unpackTableInto(industrialtest.Chargepad,{
|
||||||
|
storageLists={
|
||||||
|
"charged",
|
||||||
|
"discharged"
|
||||||
|
},
|
||||||
|
powerLists={
|
||||||
|
{
|
||||||
|
list="charged",
|
||||||
|
direction="o"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
list="discharged",
|
||||||
|
direction="i"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
facedir=true,
|
||||||
|
ioConfig="iiiioi",
|
||||||
|
hasPowerInput=true,
|
||||||
|
hasPowerOutput=true
|
||||||
|
})
|
||||||
|
|
||||||
local function chargePlayer(meta,player,flow)
|
function industrialtest.Chargepad.onConstruct(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
local inv=meta:get_inventory()
|
||||||
|
inv:set_size("charged",1)
|
||||||
|
inv:set_size("discharged",1)
|
||||||
|
meta:set_int("active",0)
|
||||||
|
industrialtest.ActivatedElectricMachine.onConstruct(self,pos)
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Chargepad.getFormspec(self,pos)
|
||||||
|
local meta=minetest.get_meta(pos)
|
||||||
|
local parentFormspec=industrialtest.ActivatedElectricMachine.getFormspec(self,pos)
|
||||||
|
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
|
||||||
|
local formspec={
|
||||||
|
"list[context;charged;1,2.5;1,1]",
|
||||||
|
industrialtest.internal.getItemSlotBg(1,2.5,1,1),
|
||||||
|
"label[0.9,3.9;"..S("Charge").."]",
|
||||||
|
"list[context;discharged;3,2.5;1,1]",
|
||||||
|
industrialtest.internal.getItemSlotBg(3,2.5,1,1),
|
||||||
|
"label[2.7,3.9;"..S("Discharge").."]",
|
||||||
|
self.createPowerIndicatorWidget(charged,9,1),
|
||||||
|
"listring[context;charged]",
|
||||||
|
"listring[context;discharged]"
|
||||||
|
}
|
||||||
|
return parentFormspec..table.concat(formspec,"")
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Chargepad.register(self)
|
||||||
|
industrialtest.ActivatedElectricMachine.register(self)
|
||||||
|
table.insert(industrialtest.internal.chargepads,self.name)
|
||||||
|
table.insert(industrialtest.internal.chargepads,self.name.."_active")
|
||||||
|
minetest.register_craft({
|
||||||
|
type="shaped",
|
||||||
|
output=self.name,
|
||||||
|
recipe={
|
||||||
|
{"industrialtest:electronic_circuit",industrialtest.elementKeys.stoneSlab,"industrialtest:electronic_circuit"},
|
||||||
|
{industrialtest.elementKeys.rubber,self._basePowerStorage,industrialtest.elementKeys.rubber}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
function industrialtest.Chargepad.chargePlayer(meta,player,flow)
|
||||||
local inv
|
local inv
|
||||||
if industrialtest.mtgAvailable then
|
if industrialtest.mtgAvailable then
|
||||||
_,inv=armor:get_valid_player(player,"")
|
_,inv=armor:get_valid_player(player,"")
|
||||||
@ -68,31 +128,7 @@ local function chargePlayer(meta,player,flow)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
chargepad.getFormspec=function(pos)
|
function industrialtest.Chargepad.action(self,pos,node)
|
||||||
local meta=minetest.get_meta(pos)
|
|
||||||
local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")
|
|
||||||
local formspec={
|
|
||||||
"list[context;charged;1,2.5;1,1]",
|
|
||||||
industrialtest.internal.getItemSlotBg(1,2.5,1,1),
|
|
||||||
"label[0.9,3.9;"..S("Charge").."]",
|
|
||||||
"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 ""),
|
|
||||||
"listring[context;charged]",
|
|
||||||
"listring[context;discharged]"
|
|
||||||
}
|
|
||||||
return table.concat(formspec,"")
|
|
||||||
end
|
|
||||||
|
|
||||||
chargepad.onConstruct=function(pos,meta,inv)
|
|
||||||
inv:set_size("charged",1)
|
|
||||||
inv:set_size("discharged",1)
|
|
||||||
meta:set_int("active",0)
|
|
||||||
end
|
|
||||||
|
|
||||||
chargepad.action=function(pos,node)
|
|
||||||
local meta=minetest.get_meta(pos)
|
local meta=minetest.get_meta(pos)
|
||||||
local inv=meta:get_inventory()
|
local inv=meta:get_inventory()
|
||||||
local chargedSlot=inv:get_stack("charged",1)
|
local chargedSlot=inv:get_stack("charged",1)
|
||||||
@ -116,138 +152,149 @@ chargepad.action=function(pos,node)
|
|||||||
for _,player in ipairs(players) do
|
for _,player in ipairs(players) do
|
||||||
if vector.in_area(player:get_pos(),p1,p2) then
|
if vector.in_area(player:get_pos(),p1,p2) then
|
||||||
playerFound=true
|
playerFound=true
|
||||||
shouldUpdateFormspec=shouldUpdateFormspec or chargePlayer(meta,player,flow)
|
shouldUpdateFormspec=shouldUpdateFormspec or self.chargePlayer(meta,player,flow)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local active=meta:get_int("active")==1
|
local active=meta:get_int("active")==1
|
||||||
if playerFound and not active then
|
if playerFound and not active then
|
||||||
minetest.swap_node(pos,{
|
self:activate(pos)
|
||||||
name=node.name.."_active",
|
|
||||||
param2=node.param2
|
|
||||||
})
|
|
||||||
meta:set_int("active",1)
|
meta:set_int("active",1)
|
||||||
elseif (not playerFound or meta:get_int("industrialtest.powerAmount")==0) and active then
|
elseif (not playerFound or meta:get_int("industrialtest.powerAmount")==0) and active then
|
||||||
local def=minetest.registered_nodes[node.name]
|
self:deactivate(pos)
|
||||||
minetest.swap_node(pos,{
|
|
||||||
name=def._industrialtest_baseNodeName,
|
|
||||||
param2=node.param2
|
|
||||||
})
|
|
||||||
meta:set_int("active",0)
|
meta:set_int("active",0)
|
||||||
end
|
end
|
||||||
|
|
||||||
if shouldUpdateFormspec then
|
if shouldUpdateFormspec then
|
||||||
local def=minetest.registered_nodes[node.name]
|
self:updateFormspec(pos)
|
||||||
def._industrialtest_updateFormspec(pos)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerChargepad(config)
|
industrialtest.BatboxChargepad=table.copy(industrialtest.Chargepad)
|
||||||
industrialtest.internal.registerMachine({
|
industrialtest.internal.unpackTableInto(industrialtest.BatboxChargepad,{
|
||||||
name=config.name,
|
name="industrialtest:batbox_chargepad",
|
||||||
displayName=config.displayName,
|
description=S("BatBox Chargepad"),
|
||||||
capacity=config.capacity,
|
|
||||||
flow=config.flow,
|
|
||||||
ioConfig="iiiioi",
|
|
||||||
sounds=config.sounds,
|
|
||||||
powerSlots={"charged","discharged"},
|
|
||||||
storageSlots={"charged","discharged"},
|
|
||||||
requiresWrench=config.requiresWrench,
|
|
||||||
registerActiveVariant=true,
|
|
||||||
groups={
|
|
||||||
_industrialtest_hasPowerOutput=1,
|
|
||||||
_industrialtest_hasPowerInput=1
|
|
||||||
},
|
|
||||||
customKeys={
|
|
||||||
tiles={
|
tiles={
|
||||||
config.machineBlockTexture.."^industrialtest_chargepad_top.png",
|
"industrialtest_wood_machine_block.png^industrialtest_chargepad_top.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture.."^"..config.frontTexture
|
"industrialtest_wood_machine_block.png^industrialtest_batbox_front.png"
|
||||||
},
|
},
|
||||||
paramtype2="facedir",
|
sounds="wood",
|
||||||
legacy_facedir_simple=true
|
active={
|
||||||
},
|
|
||||||
activeCustomKeys={
|
|
||||||
tiles={
|
tiles={
|
||||||
config.machineBlockTexture.."^industrialtest_chargepad_top_active.png",
|
"industrialtest_wood_machine_block.png^industrialtest_chargepad_top_active.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture,
|
"industrialtest_wood_machine_block.png",
|
||||||
config.machineBlockTexture.."^"..config.frontTexture
|
"industrialtest_wood_machine_block.png^industrialtest_batbox_front.png"
|
||||||
},
|
|
||||||
_industrialtest_baseNodeName="industrialtest:"..config.name
|
|
||||||
},
|
|
||||||
getFormspec=chargepad.getFormspec,
|
|
||||||
onConstruct=chargepad.onConstruct
|
|
||||||
})
|
|
||||||
minetest.register_craft({
|
|
||||||
type="shaped",
|
|
||||||
output="industrialtest:"..config.name,
|
|
||||||
recipe={
|
|
||||||
{"industrialtest:electronic_circuit",industrialtest.elementKeys.stoneSlab,"industrialtest:electronic_circuit"},
|
|
||||||
{industrialtest.elementKeys.rubber,"industrialtest:"..config.basePowerStorage,industrialtest.elementKeys.rubber}
|
|
||||||
}
|
}
|
||||||
})
|
},
|
||||||
table.insert(industrialtest.internal.chargepads,"industrialtest:"..config.name)
|
|
||||||
table.insert(industrialtest.internal.chargepads,"industrialtest:"..config.name.."_active")
|
|
||||||
end
|
|
||||||
|
|
||||||
registerChargepad({
|
|
||||||
name="batbox_chargepad",
|
|
||||||
displayName=S("BatBox Chargepad"),
|
|
||||||
capacity=25000,
|
capacity=25000,
|
||||||
flow=industrialtest.api.lvPowerFlow,
|
flow=industrialtest.api.lvPowerFlow,
|
||||||
sounds="wood",
|
_basePowerStorage="industrialtest:batbox"
|
||||||
machineBlockTexture="industrialtest_wood_machine_block.png",
|
|
||||||
frontTexture="industrialtest_batbox_front.png",
|
|
||||||
requiresWrench=false,
|
|
||||||
basePowerStorage="batbox"
|
|
||||||
})
|
})
|
||||||
|
industrialtest.BatboxChargepad:register()
|
||||||
|
|
||||||
registerChargepad({
|
industrialtest.CESUChargepad=table.copy(industrialtest.Chargepad)
|
||||||
name="cesu_chargepad",
|
industrialtest.internal.unpackTableInto(industrialtest.CESUChargepad,{
|
||||||
displayName=S("CESU Chargepad"),
|
name="industrialtest:cesu_chargepad",
|
||||||
|
description=S("CESU Chargepad"),
|
||||||
|
tiles={
|
||||||
|
"industrialtest_bronze_machine_block.png^industrialtest_chargepad_top.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",
|
||||||
|
active={
|
||||||
|
tiles={
|
||||||
|
"industrialtest_bronze_machine_block.png^industrialtest_chargepad_top_active.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"
|
||||||
|
}
|
||||||
|
},
|
||||||
capacity=400000,
|
capacity=400000,
|
||||||
flow=industrialtest.api.mvPowerFlow,
|
flow=industrialtest.api.mvPowerFlow,
|
||||||
sounds="metal",
|
_basePowerStorage="industrialtest:cesu"
|
||||||
machineBlockTexture="industrialtest_bronze_machine_block.png",
|
|
||||||
frontTexture="industrialtest_cesu_front.png",
|
|
||||||
requiresWrench=false,
|
|
||||||
basePowerStorage="cesu"
|
|
||||||
})
|
})
|
||||||
|
industrialtest.CESUChargepad:register()
|
||||||
|
|
||||||
registerChargepad({
|
industrialtest.MFEChargepad=table.copy(industrialtest.Chargepad)
|
||||||
name="mfe_chargepad",
|
industrialtest.internal.unpackTableInto(industrialtest.MFEChargepad,{
|
||||||
displayName=S("MFE Chargepad"),
|
name="industrialtest:mfe_chargepad",
|
||||||
|
description=S("MFE Chargepad"),
|
||||||
|
tiles={
|
||||||
|
"industrialtest_machine_block.png^industrialtest_chargepad_top.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",
|
||||||
|
requiresWrench=true,
|
||||||
|
active={
|
||||||
|
tiles={
|
||||||
|
"industrialtest_machine_block.png^industrialtest_chargepad_top_active.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"
|
||||||
|
}
|
||||||
|
},
|
||||||
capacity=3000000,
|
capacity=3000000,
|
||||||
flow=industrialtest.api.hvPowerFlow,
|
flow=industrialtest.api.hvPowerFlow,
|
||||||
sounds="metal",
|
_basePowerStorage="industrialtest:mfe"
|
||||||
machineBlockTexture="industrialtest_machine_block.png",
|
|
||||||
frontTexture="industrialtest_mfe_front.png",
|
|
||||||
requiresWrench=true,
|
|
||||||
basePowerStorage="mfe"
|
|
||||||
})
|
})
|
||||||
|
industrialtest.MFEChargepad:register()
|
||||||
|
|
||||||
registerChargepad({
|
industrialtest.MFSUChargepad=table.copy(industrialtest.Chargepad)
|
||||||
name="mfsu_chargepad",
|
industrialtest.internal.unpackTableInto(industrialtest.MFSUChargepad,{
|
||||||
displayName=S("MFSU Chargepad"),
|
name="industrialtest:mfsu_chargepad",
|
||||||
|
description=S("MFSU Chargepad"),
|
||||||
|
tiles={
|
||||||
|
"industrialtest_advanced_machine_block.png^industrialtest_chargepad_top.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",
|
||||||
|
requiresWrench=true,
|
||||||
|
active={
|
||||||
|
tiles={
|
||||||
|
"industrialtest_advanced_machine_block.png^industrialtest_chargepad_top_active.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"
|
||||||
|
}
|
||||||
|
},
|
||||||
capacity=30000000,
|
capacity=30000000,
|
||||||
flow=industrialtest.api.evPowerFlow,
|
flow=industrialtest.api.evPowerFlow,
|
||||||
sounds="metal",
|
_basePowerStorage="industrialtest:mfsu"
|
||||||
machineBlockTexture="industrialtest_advanced_machine_block.png",
|
|
||||||
frontTexture="industrialtest_mfsu_front.png",
|
|
||||||
requiresWrench=true,
|
|
||||||
basePowerStorage="mfsu"
|
|
||||||
})
|
})
|
||||||
|
industrialtest.MFSUChargepad:register()
|
||||||
|
|
||||||
minetest.register_abm({
|
minetest.register_abm({
|
||||||
label="Chargepad updating",
|
label="Chargepad updating",
|
||||||
nodenames=industrialtest.internal.chargepads,
|
nodenames=industrialtest.internal.chargepads,
|
||||||
interval=industrialtest.updateDelay,
|
interval=industrialtest.updateDelay,
|
||||||
chance=1,
|
chance=1,
|
||||||
action=chargepad.action
|
action=function(pos,node)
|
||||||
|
local def=minetest.registered_nodes[node.name]
|
||||||
|
def._industrialtest_self:action(pos,node)
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user