From be8809e008bf7203e8e5cad75d8a07980b04f8df Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sat, 25 Nov 2023 17:58:42 +0100 Subject: [PATCH] Implement Overclocker Upgrade --- api.lua | 7 ++++ init.lua | 1 + machines/common.lua | 74 +++++++++++++++++++++++++++++++++++--- machines/tool_workshop.lua | 35 +++++++++++------- upgrades.lua | 50 ++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 17 deletions(-) create mode 100644 upgrades.lua diff --git a/api.lua b/api.lua index 5bc38b8..317e25d 100644 --- a/api.lua +++ b/api.lua @@ -780,3 +780,10 @@ industrialtest.api.getWaterMillFuelByItem=function(name) end return nil end + +-- \brief Returns machine speed in items per operation +-- \param meta MetaDataRef +-- \returns number +industrialtest.api.getMachineSpeed=function(meta) + return meta:contains("industrialtest.speed") and meta:get_int("industrialtest.speed") or 1 +end diff --git a/init.lua b/init.lua index 9333809..0c9c577 100644 --- a/init.lua +++ b/init.lua @@ -50,6 +50,7 @@ dofile(modpath.."/machines/transformer.lua") dofile(modpath.."/machines/solar_panel_generator.lua") dofile(modpath.."/machines/wind_mill.lua") +dofile(modpath.."/upgrades.lua") dofile(modpath.."/craftitems.lua") dofile(modpath.."/nodes.lua") if industrialtest.developerMode then diff --git a/machines/common.lua b/machines/common.lua index a4af5aa..93d804f 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -17,7 +17,7 @@ local machine={} local simpleElectricItemProcessor={} -function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists) +industrialtest.internal.mclAfterDigNode=function(pos,oldmeta,lists) -- Taken from https://git.minetest.land/MineClone2/MineClone2/src/branch/master/mods/ITEMS/mcl_furnaces/init.lua#L538 local meta=minetest.get_meta(pos) local meta2=meta @@ -33,6 +33,20 @@ function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists) meta:from_table(meta2:to_table()) end +industrialtest.internal.allowMoveToUpgradeSlot=function(pos,toIndex,stack) + local def=minetest.registered_items[stack:get_name()] + if not def or not def.groups or not def.groups._industrialtest_machineUpgrade then + return 0 + end + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local targetSlot=inv:get_stack("upgrades",toIndex) + if not targetSlot:is_empty() then + return 0 + end + return stack:get_count() +end + machine.getFormspec=function(pos,config) local formspec if industrialtest.mtgAvailable then @@ -157,6 +171,11 @@ machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toInde local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local movedItemStack=inv:get_stack(fromList,1) + + if toList=="upgrades" then + return industrialtest.internal.allowMoveToUpgradeSlot(pos,toIndex,movedItemStack) + end + local found=false if config.powerSlots then for _,value in ipairs(config.powerSlots) do @@ -169,13 +188,19 @@ machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toInde if found and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then return 0 end + if config.allowMetadataInventoryMove then return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) end + return count end machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,config) + if listname=="upgrades" then + return industrialtest.internal.allowMoveToUpgradeSlot(pos,index,stack) + end + local found=false if config.powerSlots then for _,value in ipairs(config.powerSlots) do @@ -188,12 +213,42 @@ machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,confi if found and not industrialtest.api.hasPowerStorage(stack:get_meta()) then return 0 end + if config.allowMetadataInventoryPut then return config.allowMetadataInventoryPut(pos,listname,index,stack,player) end + return stack:get_count() end +machine.onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count) + 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(meta,stack) + 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(meta,stack) + end +end + +machine.onMetadataInventoryPut=function(pos,listname,index,stack) + if listname=="upgrades" then + local meta=minetest.get_meta(pos) + industrialtest.internal.applyUpgrade(meta,stack) + end +end + +machine.onMetadataInventoryTake=function(pos,listname,index,stack) + if listname=="upgrades" then + local meta=minetest.get_meta(pos) + industrialtest.internal.removeUpgrade(meta,stack) + end +end + machine.updateFormspec=function(pos,config) if config.withoutFormspec then return @@ -220,16 +275,19 @@ function industrialtest.internal.registerMachine(config) return machine.allowMetadataInventoryPut(pos,listname,index,stack,player,config) end, on_metadata_inventory_put=function(pos,listname,index,stack,player) + machine.onMetadataInventoryPut(pos,listname,index,stack) if config.onMetadataInventoryPut then config.onMetadataInventoryPut(pos,listname,index,stack,player) end end, on_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) + machine.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex) if config.onMetadataInventoryPut then config.onMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) end end, on_metadata_inventory_take=function(pos,listname,index,stack,player) + machine.onMetadataInventoryTake(pos,listname,index,stack) if config.onMetadataInventoryTake then config.onMetadataInventoryTake(pos,listname,index,stack,player) end @@ -490,7 +548,7 @@ simpleElectricItemProcessor.onTimer=function(pos,elapsed,meta,inv,config) local powerStorageSlot=inv:get_stack("powerStorage",1) local shouldUpdateFormspec=false local shouldRerunTimer=false - local requiredPower=elapsed*config.opPower + local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) if powerStorageSlot:get_count()>0 then local stackMeta=powerStorageSlot:get_meta() if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then @@ -586,7 +644,7 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config) local powerStorageSlot=inv:get_stack("powerStorage",1) local shouldUpdateFormspec=false local shouldRerunTimer=false - local requiredPower=elapsed*config.opPower + local requiredPower=elapsed*config.opPower*industrialtest.api.getMachineSpeed(meta) if powerStorageSlot:get_count()>0 then local stackMeta=powerStorageSlot:get_meta() @@ -626,12 +684,20 @@ simpleElectricItemProcessor.activeOnTimer=function(pos,elapsed,meta,inv,config) end if meta:get_float("srcTime")>=meta:get_float("maxSrcTime") then local output=craftResultProxy(config.method,srcSlot) + local speed=industrialtest.api.getMachineSpeed(meta) + local usedItems=srcSlot:get_count()-output.src:get_count() + local multiplier=1 + if srcSlot:get_count()>=speed*usedItems then + multiplier=speed + end if output.item:get_count()>0 then + output.item:set_count(output.item:get_count()*multiplier) inv:add_item("dst",output.item) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) end - inv:set_stack("src",1,output.src) + srcSlot:set_count(srcSlot:get_count()-multiplier*usedItems) + inv:set_stack("src",1,srcSlot) end return shouldRerunTimer,shouldUpdateFormspec end diff --git a/machines/tool_workshop.lua b/machines/tool_workshop.lua index 844fb6d..8882155 100644 --- a/machines/tool_workshop.lua +++ b/machines/tool_workshop.lua @@ -28,7 +28,9 @@ toolWorkshop.getFormspec=function(pos) (powerPercent>0 and "image[3.7,2.5;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" or "image[3.7,2.5;1,1;industrialtest_gui_electricity_bg.png]"), "list[context;tool;5.9,3.2;1,1;0]", - "listring[context;tool]" + "listring[context;tool]", + "list[context;upgrades;9,0.9;1,4]", + "listring[context;upgrades]" } elseif industrialtest.mclAvailable then formspec={ @@ -39,7 +41,10 @@ toolWorkshop.getFormspec=function(pos) or "image[3.7,2.5;1,1;industrialtest_gui_electricity_bg.png]"), "list[context;tool;5.9,3.2;1,1;0]", mcl_formspec.get_itemslot_bg(5.9,3.2,1,1), - "listring[context;tool]" + "listring[context;tool]", + "list[context;upgrades;9,0.9;1,4]", + mcl_formspec.get_itemslot_bg(9,0.9,1,4), + "listring[context;upgrades]" } end return table.concat(formspec,"") @@ -48,11 +53,13 @@ end toolWorkshop.onConstruct=function(pos,meta,inv) inv:set_size("powerStorage",1) inv:set_size("tool",1) + inv:set_size("upgrades",4) end toolWorkshop.onTimer=function(pos,elapsed,meta,inv) local powerStorageSlot=inv:get_stack("powerStorage",1) local toolSlot=inv:get_stack("tool",1) + local requiredPower=industrialtest.api.getMachineSpeed(meta)*10000 local shouldRerunTimer=false local shouldUpdateFormspec=false @@ -66,7 +73,7 @@ toolWorkshop.onTimer=function(pos,elapsed,meta,inv) end end - if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=10000 then + if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then minetest.swap_node(pos,{ name="industrialtest:tool_workshop_active", param2=minetest.get_node(pos).param2 @@ -80,6 +87,8 @@ end toolWorkshop.activeOnTimer=function(pos,elapsed,meta,inv) local powerStorageSlot=inv:get_stack("powerStorage",1) local toolSlot=inv:get_stack("tool",1) + local speed=industrialtest.api.getMachineSpeed(meta) + local requiredPower=speed*10000 local shouldRerunTimer=false local shouldUpdateFormspec=false @@ -93,11 +102,11 @@ toolWorkshop.activeOnTimer=function(pos,elapsed,meta,inv) end end - if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=10000 then - local removed=math.min(toolSlot:get_wear(),200) + if toolSlot:get_count()>0 and toolSlot:get_wear()>0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then + local removed=math.min(toolSlot:get_wear(),speed*200) toolSlot:set_wear(toolSlot:get_wear()-removed) inv:set_stack("tool",1,toolSlot) - industrialtest.api.addPower(meta,-10000) + industrialtest.api.addPower(meta,-requiredPower) shouldRerunTimer=true shouldUpdateFormspec=true else @@ -171,13 +180,13 @@ industrialtest.internal.registerMachine({ }, activeCustomKeys={ 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_tool_workshop_front_active.png", - "industrialtest_machine_block.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_advanced_machine_block.png^industrialtest_tool_workshop_front_active.png", + "industrialtest_advanced_machine_block.png" }, }, onConstruct=toolWorkshop.onConstruct, diff --git a/upgrades.lua b/upgrades.lua new file mode 100644 index 0000000..09776c1 --- /dev/null +++ b/upgrades.lua @@ -0,0 +1,50 @@ +-- IndustrialTest +-- Copyright (C) 2023 mrkubax10 + +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. + +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. + +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +local S=minetest.get_translator("industrialtest") + +industrialtest.internal.applyUpgrade=function(meta,stack) + local def=minetest.registered_items[stack:get_name()] + if def.groups._industrialtest_upgradeSpeed then + local speed=meta:contains("industrialtest.speed") and meta:get_int("industrialtest.speed") or 1 + meta:set_int("industrialtest.speed",math.min(4,speed+def.groups._industrialtest_upgradeSpeed)) + end +end + +industrialtest.internal.removeUpgrade=function(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)) + end +end + +local function registerMachineUpgrade(config) + minetest.register_craftitem("industrialtest:"..config.name,{ + description=config.displayName, + inventory_image="industrialtest_"..config.name..".png", + groups={ + _industrialtest_machineUpgrade=1, + _industrialtest_upgradeSpeed=config.speed or 0 + } + }) +end + +registerMachineUpgrade({ + name="overclocker_upgrade", + displayName=S("Overclocker Upgrade"), + speed=1 +})