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/>.
|
||||
|
||||
local S=minetest.get_translator("industrialtest")
|
||||
|
||||
local chargepad={}
|
||||
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
|
||||
if industrialtest.mtgAvailable then
|
||||
_,inv=armor:get_valid_player(player,"")
|
||||
@ -68,31 +128,7 @@ local function chargePlayer(meta,player,flow)
|
||||
return true
|
||||
end
|
||||
|
||||
chargepad.getFormspec=function(pos)
|
||||
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)
|
||||
function industrialtest.Chargepad.action(self,pos,node)
|
||||
local meta=minetest.get_meta(pos)
|
||||
local inv=meta:get_inventory()
|
||||
local chargedSlot=inv:get_stack("charged",1)
|
||||
@ -116,138 +152,149 @@ chargepad.action=function(pos,node)
|
||||
for _,player in ipairs(players) do
|
||||
if vector.in_area(player:get_pos(),p1,p2) then
|
||||
playerFound=true
|
||||
shouldUpdateFormspec=shouldUpdateFormspec or chargePlayer(meta,player,flow)
|
||||
shouldUpdateFormspec=shouldUpdateFormspec or self.chargePlayer(meta,player,flow)
|
||||
break
|
||||
end
|
||||
end
|
||||
local active=meta:get_int("active")==1
|
||||
if playerFound and not active then
|
||||
minetest.swap_node(pos,{
|
||||
name=node.name.."_active",
|
||||
param2=node.param2
|
||||
})
|
||||
self:activate(pos)
|
||||
meta:set_int("active",1)
|
||||
elseif (not playerFound or meta:get_int("industrialtest.powerAmount")==0) and active then
|
||||
local def=minetest.registered_nodes[node.name]
|
||||
minetest.swap_node(pos,{
|
||||
name=def._industrialtest_baseNodeName,
|
||||
param2=node.param2
|
||||
})
|
||||
self:deactivate(pos)
|
||||
meta:set_int("active",0)
|
||||
end
|
||||
|
||||
if shouldUpdateFormspec then
|
||||
local def=minetest.registered_nodes[node.name]
|
||||
def._industrialtest_updateFormspec(pos)
|
||||
self:updateFormspec(pos)
|
||||
end
|
||||
end
|
||||
|
||||
local function registerChargepad(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"},
|
||||
requiresWrench=config.requiresWrench,
|
||||
registerActiveVariant=true,
|
||||
groups={
|
||||
_industrialtest_hasPowerOutput=1,
|
||||
_industrialtest_hasPowerInput=1
|
||||
},
|
||||
customKeys={
|
||||
tiles={
|
||||
config.machineBlockTexture.."^industrialtest_chargepad_top.png",
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture.."^"..config.frontTexture
|
||||
},
|
||||
paramtype2="facedir",
|
||||
legacy_facedir_simple=true
|
||||
},
|
||||
activeCustomKeys={
|
||||
tiles={
|
||||
config.machineBlockTexture.."^industrialtest_chargepad_top_active.png",
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture,
|
||||
config.machineBlockTexture.."^"..config.frontTexture
|
||||
},
|
||||
_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}
|
||||
industrialtest.BatboxChargepad=table.copy(industrialtest.Chargepad)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.BatboxChargepad,{
|
||||
name="industrialtest:batbox_chargepad",
|
||||
description=S("BatBox Chargepad"),
|
||||
tiles={
|
||||
"industrialtest_wood_machine_block.png^industrialtest_chargepad_top.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",
|
||||
active={
|
||||
tiles={
|
||||
"industrialtest_wood_machine_block.png^industrialtest_chargepad_top_active.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"
|
||||
}
|
||||
})
|
||||
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,
|
||||
flow=industrialtest.api.lvPowerFlow,
|
||||
sounds="wood",
|
||||
machineBlockTexture="industrialtest_wood_machine_block.png",
|
||||
frontTexture="industrialtest_batbox_front.png",
|
||||
requiresWrench=false,
|
||||
basePowerStorage="batbox"
|
||||
_basePowerStorage="industrialtest:batbox"
|
||||
})
|
||||
industrialtest.BatboxChargepad:register()
|
||||
|
||||
registerChargepad({
|
||||
name="cesu_chargepad",
|
||||
displayName=S("CESU Chargepad"),
|
||||
industrialtest.CESUChargepad=table.copy(industrialtest.Chargepad)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.CESUChargepad,{
|
||||
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,
|
||||
flow=industrialtest.api.mvPowerFlow,
|
||||
sounds="metal",
|
||||
machineBlockTexture="industrialtest_bronze_machine_block.png",
|
||||
frontTexture="industrialtest_cesu_front.png",
|
||||
requiresWrench=false,
|
||||
basePowerStorage="cesu"
|
||||
_basePowerStorage="industrialtest:cesu"
|
||||
})
|
||||
industrialtest.CESUChargepad:register()
|
||||
|
||||
registerChargepad({
|
||||
name="mfe_chargepad",
|
||||
displayName=S("MFE Chargepad"),
|
||||
industrialtest.MFEChargepad=table.copy(industrialtest.Chargepad)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.MFEChargepad,{
|
||||
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,
|
||||
flow=industrialtest.api.hvPowerFlow,
|
||||
sounds="metal",
|
||||
machineBlockTexture="industrialtest_machine_block.png",
|
||||
frontTexture="industrialtest_mfe_front.png",
|
||||
requiresWrench=true,
|
||||
basePowerStorage="mfe"
|
||||
_basePowerStorage="industrialtest:mfe"
|
||||
})
|
||||
industrialtest.MFEChargepad:register()
|
||||
|
||||
registerChargepad({
|
||||
name="mfsu_chargepad",
|
||||
displayName=S("MFSU Chargepad"),
|
||||
industrialtest.MFSUChargepad=table.copy(industrialtest.Chargepad)
|
||||
industrialtest.internal.unpackTableInto(industrialtest.MFSUChargepad,{
|
||||
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,
|
||||
flow=industrialtest.api.evPowerFlow,
|
||||
sounds="metal",
|
||||
machineBlockTexture="industrialtest_advanced_machine_block.png",
|
||||
frontTexture="industrialtest_mfsu_front.png",
|
||||
requiresWrench=true,
|
||||
basePowerStorage="mfsu"
|
||||
_basePowerStorage="industrialtest:mfsu"
|
||||
})
|
||||
industrialtest.MFSUChargepad:register()
|
||||
|
||||
minetest.register_abm({
|
||||
label="Chargepad updating",
|
||||
nodenames=industrialtest.internal.chargepads,
|
||||
interval=industrialtest.updateDelay,
|
||||
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