Refactor upgrades

This commit is contained in:
mrkubax10 2025-04-29 11:21:14 +02:00
parent 43484defad
commit 47b85df93c
3 changed files with 162 additions and 116 deletions

View File

@ -128,27 +128,37 @@ function industrialtest.Machine.onMetadataInventoryMove(self,pos,fromList,fromIn
if toList=="upgrades" then
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local stack=inv:get_stack(fromList,fromIndex)
industrialtest.internal.applyUpgrade(pos,meta,stack)
local itemstack=inv:get_stack(fromList,fromIndex)
local def=itemstack:get_definition()
if def and def._industrialtest_self and def._industrialtest_self.apply then
def._industrialtest_self:apply(pos)
end
elseif fromList=="upgrades" then
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local stack=inv:get_stack(fromList,fromIndex)
industrialtest.internal.removeUpgrade(pos,meta,stack)
local itemstack=inv:get_stack(fromList,fromIndex)
local def=itemstack:get_definition()
if def and def._industrialtest_self and def._industrialtest_self.remove then
def._industrialtest_self:remove(pos)
end
end
end
function industrialtest.Machine.onMetadataInventoryPut(self,pos,listname,index,stack)
if listname=="upgrades" then
local meta=minetest.get_meta(pos)
industrialtest.internal.applyUpgrade(pos,meta,stack)
local def=stack:get_definition()
if def and def._industrialtest_self and def._industrialtest_self.apply then
def._industrialtest_self:apply(pos)
end
end
end
function industrialtest.Machine.onMetadataInventoryTake(self,pos,listname,index,stack)
if listname=="upgrades" then
local meta=minetest.get_meta(pos)
industrialtest.internal.removeUpgrade(pos,meta,stack)
local def=stack:get_definition()
if def and def._industrialtest_self and def._industrialtest_self.remove then
def._industrialtest_self:remove(pos)
end
end
end
@ -289,8 +299,8 @@ function industrialtest.Machine.register(self)
industrialtest.api.addTag(self.name,"usesTimer")
end
function industrialtest.Machine.allowMoveToUpgradeSlot(pos,toIndex,stack)
local def=minetest.registered_items[stack:get_name()]
function industrialtest.Machine.allowMoveToUpgradeSlot(pos,toIndex,itemstack)
local def=itemstack:get_definition()
if not def or not def.groups or not def.groups._industrialtest_machineUpgrade then
return 0
end
@ -300,5 +310,5 @@ function industrialtest.Machine.allowMoveToUpgradeSlot(pos,toIndex,stack)
if not targetSlot:is_empty() then
return 0
end
return stack:get_count()
return itemstack:get_count()
end

View File

@ -107,8 +107,8 @@ function industrialtest.SimpleElectricItemProcessor.onMetadataInventoryMove(self
end
end
function industrialtest.SimpleElectricItemProcessor.onMetadataInventoryPut(self,pos)
industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos)
function industrialtest.SimpleElectricItemProcessor.onMetadataInventoryPut(self,pos,listname,index,stack)
industrialtest.ActivatedElectricMachine.onMetadataInventoryPut(self,pos,listname,index,stack)
self:triggerIfNeeded(pos)
end

View File

@ -16,106 +16,56 @@
local S=minetest.get_translator("industrialtest")
industrialtest.internal.applyUpgrade=function(pos,meta,stack)
local def=minetest.registered_items[stack:get_name()]
if def.groups._industrialtest_upgradeSpeed then
local speed=industrialtest.api.getMachineSpeed(meta)
meta:set_int("industrialtest.speed",math.min(4,speed+def.groups._industrialtest_upgradeSpeed))
elseif def.groups._industrialtest_upgradeTransformer then
local flows={
industrialtest.api.lvPowerFlow,
industrialtest.api.mvPowerFlow,
industrialtest.api.hvPowerFlow,
industrialtest.api.evPowerFlow,
industrialtest.api.ivPowerFlow
}
local machineFlow=meta:get_int("industrialtest.powerFlow")
local upgradedFlow=machineFlow
for _,flow in ipairs(flows) do
if flow>machineFlow then
upgradedFlow=flow
break
end
end
meta:set_int("industrialtest.powerFlow",upgradedFlow)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
industrialtest.api.createNetworkMapForNode(network)
end
end
elseif def.groups._industrialtest_upgradePowerStorage then
meta:set_int("industrialtest.powerCapacity",meta:get_int("industrialtest.powerCapacity")+10000)
local nodeDef=minetest.registered_nodes[minetest.get_node(pos).name]
if nodeDef._industrialtest_updateFormspec then
nodeDef._industrialtest_updateFormspec(pos)
end
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
minetest.get_node_timer(network):start(industrialtest.updateDelay)
end
end
end
end
industrialtest.Upgrade={}
industrialtest.internal.removeUpgrade=function(pos,meta,stack)
local def=minetest.registered_items[stack:get_name()]
if def.groups._industrialtest_upgradeSpeed and meta:contains("industrialtest.speed") then
local speed=meta:get_int("industrialtest.speed")
meta:set_int("industrialtest.speed",math.max(1,speed-def.groups._industrialtest_upgradeSpeed))
elseif def.groups._industrialtest_upgradeTransformer then
local flows={
industrialtest.api.lvPowerFlow,
industrialtest.api.mvPowerFlow,
industrialtest.api.hvPowerFlow,
industrialtest.api.evPowerFlow,
industrialtest.api.ivPowerFlow
}
local machineFlow=meta:get_int("industrialtest.powerFlow")
local downgradedFlow=machineFlow
for i=#flows,1,-1 do
local flow=flows[i]
if flow<machineFlow then
downgradedFlow=flow
break
end
end
meta:set_int("industrialtest.powerFlow",downgradedFlow)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
minetest.get_node_timer(network):start(industrialtest.updateDelay)
end
end
elseif def.groups._industrialtest_upgradePowerStorage then
meta:set_int("industrialtest.powerCapacity",meta:get_int("industrialtest.powerCapacity")-10000)
meta:set_int("industrialtest.powerAmount",math.min(meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity")))
local nodeDef=minetest.registered_nodes[minetest.get_node(pos).name]
if nodeDef._industrialtest_updateFormspec then
nodeDef._industrialtest_updateFormspec(pos)
end
end
end
local function registerMachineUpgrade(config)
minetest.register_craftitem("industrialtest:"..config.name,{
description=config.displayName,
inventory_image="industrialtest_"..config.name..".png",
function industrialtest.Upgrade.createDefinitionTable(self)
local def={
description=self.description,
inventory_image=self.inventoryImage,
groups={
_industrialtest_machineUpgrade=1,
_industrialtest_upgradeSpeed=config.speed or nil,
_industrialtest_upgradeTransformer=config.transformer or nil,
_industrialtest_upgradePowerStorage=config.powerStorage or nil,
}
})
_industrialtest_machineUpgrade=1
},
_industrialtest_self=self
}
return def
end
registerMachineUpgrade({
name="overclocker_upgrade",
displayName=S("Overclocker Upgrade"),
speed=1
function industrialtest.Upgrade.register(self)
local def=self:createDefinitionTable()
minetest.register_craftitem(self.name,def)
end
function industrialtest.Upgrade.apply(self,pos)
-- dummy function
end
function industrialtest.Upgrade.remove(self,pos)
-- dummy function
end
industrialtest.OverclockerUpgrade=table.copy(industrialtest.Upgrade)
industrialtest.internal.unpackTableInto(industrialtest.OverclockerUpgrade,{
name="industrialtest:overclocker_upgrade",
description=S("Overclocker Upgrade"),
inventoryImage="industrialtest_overclocker_upgrade.png",
_speed=1
})
function industrialtest.OverclockerUpgrade.apply(self,pos)
local meta=minetest.get_meta(pos)
local speed=industrialtest.api.getMachineSpeed(meta)
meta:set_int("industrialtest.speed",math.min(4,speed+self._speed))
end
function industrialtest.OverclockerUpgrade.remove(self,pos)
local meta=minetest.get_meta(pos)
local speed=industrialtest.api.getMachineSpeed(meta)
meta:set_int("industrialtest.speed",math.max(1,speed-self._speed))
end
industrialtest.OverclockerUpgrade:register()
minetest.register_craft({
type="shaped",
output="industrialtest:overclocker_upgrade",
@ -125,11 +75,68 @@ minetest.register_craft({
}
})
registerMachineUpgrade({
name="transformer_upgrade",
displayName=S("Transformer Upgrade"),
transformer=1
industrialtest.TransformerUpgrade=table.copy(industrialtest.Upgrade)
industrialtest.internal.unpackTableInto(industrialtest.TransformerUpgrade,{
name="industrialtest:transformer_upgrade",
description=S("Transformer Upgrade"),
inventoryImage="industrialtest_transformer_upgrade.png"
})
function industrialtest.TransformerUpgrade.apply(self,pos)
local meta=minetest.get_meta(pos)
local flows={
industrialtest.api.lvPowerFlow,
industrialtest.api.mvPowerFlow,
industrialtest.api.hvPowerFlow,
industrialtest.api.evPowerFlow,
industrialtest.api.ivPowerFlow
}
local machineFlow=meta:get_int("industrialtest.powerFlow")
local upgradedFlow=machineFlow
for _,flow in ipairs(flows) do
if flow>machineFlow then
upgradedFlow=flow
break
end
end
meta:set_int("industrialtest.powerFlow",upgradedFlow)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
industrialtest.api.createNetworkMapForNode(network)
end
end
end
function industrialtest.TransformerUpgrade.remove(self,pos)
local meta=minetest.get_meta(pos)
local flows={
industrialtest.api.lvPowerFlow,
industrialtest.api.mvPowerFlow,
industrialtest.api.hvPowerFlow,
industrialtest.api.evPowerFlow,
industrialtest.api.ivPowerFlow
}
local machineFlow=meta:get_int("industrialtest.powerFlow")
local downgradedFlow=machineFlow
for i=#flows,1,-1 do
local flow=flows[i]
if flow<machineFlow then
downgradedFlow=flow
break
end
end
meta:set_int("industrialtest.powerFlow",downgradedFlow)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
industrialtest.api.createNetworkMapForNode(network)
end
end
end
industrialtest.TransformerUpgrade:register()
minetest.register_craft({
type="shaped",
output="industrialtest:transformer_upgrade",
@ -140,11 +147,40 @@ minetest.register_craft({
}
})
registerMachineUpgrade({
name="power_storage_upgrade",
displayName=S("Power Storage Upgrade"),
powerStorage=1
industrialtest.PowerStorageUpgrade=table.copy(industrialtest.Upgrade)
industrialtest.internal.unpackTableInto(industrialtest.PowerStorageUpgrade,{
name="industrialtest:power_storage_upgrade",
description=S("Power Storage Upgrade"),
inventoryImage="industrialtest_power_storage_upgrade.png",
_storage=10000
})
function industrialtest.PowerStorageUpgrade.apply(self,pos)
local meta=minetest.get_meta(pos)
meta:set_int("industrialtest.powerCapacity",meta:get_int("industrialtest.powerCapacity")+self._storage)
local node=minetest.get_node(pos)
local def=minetest.registered_nodes[node.name]
if def._industrialtest_self then
def._industrialtest_self:updateFormspec(pos)
if def._industrialtest_self.requestPower then
def._industrialtest_self:requestPower(pos)
end
end
end
function industrialtest.PowerStorageUpgrade.remove(self,pos)
local meta=minetest.get_meta(pos)
meta:set_int("industrialtest.powerCapacity",meta:get_int("industrialtest.powerCapacity")-self._storage)
meta:set_int("industrialtest.powerAmount",math.min(meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity")))
local node=minetest.get_node(pos)
local def=minetest.registered_nodes[node.name]
if def._industrialtest_self then
def._industrialtest_self:updateFormspec(pos)
end
end
industrialtest.PowerStorageUpgrade:register()
minetest.register_craft({
type="shaped",
output="industrialtest:power_storage_upgrade",