Compare commits

...

2 Commits

Author SHA1 Message Date
371ef36ce3 Refactor Chargepad 2025-03-03 11:45:36 +01:00
8f529ad6c3 Fix few oversights and bring back some missing features 2025-03-03 11:44:18 +01:00
4 changed files with 218 additions and 154 deletions

@ -16,28 +16,26 @@
industrialtest.ActivatedElectricMachine=table.copy(industrialtest.ElectricMachine)
-- Forward methods from ActivatedMachine
industrialtest.internal.unpackTableInto(industrialtest.ActivatedElectricMachine,{
canUpdate=industrialtest.ActivatedMachine.canUpdate,
register=industrialtest.ActivatedMachine.register,
createDefinitionTable=industrialtest.ActivatedMachine.createDefinitionTable,
createActiveDefinitionTable=industrialtest.ActivatedMachine.createActiveDefinitionTable,
activate=industrialtest.ActivatedMachine.activate,
deactivate=industrialtest.ActivatedMachine.deactivate,
shouldActivate=industrialtest.ActivatedMachine.shouldActivate,
shouldDeactivate=industrialtest.ActivatedMachine.shouldDeactivate,
afterActivation=industrialtest.ActivatedMachine.afterActivation,
afterDeactivation=industrialtest.ActivatedMachine.afterDeactivation
})
function industrialtest.ActivatedElectricMachine.onTimer(self,pos,elapsed)
local result=self:powerExchange(pos)
local result2=industrialtest.ActivatedMachine.onTimer(self,pos,elapsed)
return result or result2
end
industrialtest.ActivatedElectricMachine.canUpdate=industrialtest.ActivatedMachine.canUpdate
industrialtest.ActivatedElectricMachine.register=industrialtest.ActivatedMachine.register
industrialtest.ActivatedElectricMachine.createDefinitionTable=industrialtest.ActivatedMachine.createDefinitionTable
industrialtest.ActivatedElectricMachine.createActiveDefinitionTable=industrialtest.ActivatedMachine.createActiveDefinitionTable
industrialtest.ActivatedElectricMachine.shouldActivate=industrialtest.ActivatedMachine.shouldActivate
industrialtest.ActivatedElectricMachine.shouldDeactivate=industrialtest.ActivatedMachine.shouldDeactivate
industrialtest.ActivatedElectricMachine.afterActivation=industrialtest.ActivatedMachine.afterActivation
industrialtest.ActivatedElectricMachine.afterDeactivation=industrialtest.ActivatedMachine.afterDeactivation
function industrialtest.ActivatedElectricMachine.activeOnTimer(self,pos,elapsed)
local result=self:powerExchange(pos)
local result2=industrialtest.ActivatedMachine.activeOnTimer(self,pos,elapsed)

@ -24,12 +24,7 @@ function industrialtest.ActivatedMachine.onTimer(self,pos,elapsed)
local result=industrialtest.Machine.onTimer(self,pos,elapsed)
if self:shouldActivate(pos) then
minetest.swap_node(pos,{
name=self.name.."_active",
param2=minetest.get_node(pos).param2
})
self:afterActivation(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
self:activate(pos)
return false
end
@ -65,6 +60,24 @@ function industrialtest.ActivatedMachine.createActiveDefinitionTable(self)
return def
end
function industrialtest.ActivatedMachine.activate(self,pos)
minetest.swap_node(pos,{
name=self.name.."_active",
param2=minetest.get_node(pos).param2
})
self:afterActivation(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
function industrialtest.ActivatedMachine.deactivate(self,pos)
minetest.swap_node(pos,{
name=self.name,
param2=minetest.get_node(pos).param2
})
self:afterDeactivation(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
function industrialtest.ActivatedMachine.shouldActivate(self,pos)
return false
end
@ -86,12 +99,7 @@ function industrialtest.ActivatedMachine.activeOnTimer(self,pos,elapsed)
local shouldUpdateFormspec=false
if self:shouldDeactivate(pos) then
minetest.swap_node(pos,{
name=self.name,
param2=minetest.get_node(pos).param2
})
self:afterDeactivation(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
self:deactivate(pos)
return false
end

@ -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
})

@ -179,6 +179,8 @@ function industrialtest.Machine.createDefinitionTable(self)
def.groups={cracky=2}
if self.sounds=="metal" then
def.sounds=default.node_sound_metal_defaults()
elseif self.sounds=="wood" then
def.sounds=default.node_sound_wood_defaults()
end
def.can_dig=function(pos)
local meta=minetest.get_meta(pos)
@ -196,6 +198,8 @@ function industrialtest.Machine.createDefinitionTable(self)
end
if self.sounds=="metal" then
def.sounds=mcl_sounds.node_sound_metal_defaults()
elseif sounds=="wood" then
def.sounds=mcl_sounds.node_sound_wood_defaults()
end
def.groups={
pickaxey=1,
@ -232,6 +236,13 @@ function industrialtest.Machine.createDefinitionTable(self)
def.legacy_facedir_simple=true
end
if self.hasPowerInput then
def.groups._industrialtest_hasPowerInput=1
end
if self.hasPowerOutput then
def.groups._industrialtest_hasPowerOutput=1
end
return def
end