From a90b4f8cfed619e015c304a23f21af76cd430cb3 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Fri, 8 Mar 2024 22:41:02 +0100 Subject: [PATCH] Implement Rotary Macerator It's a more advanced version of regular Macerator. It introduces RPM which makes machine faster the longer it works. --- api.lua | 24 +++ compatibility.lua | 6 + craftitems.lua | 37 ++++- init.lua | 1 + machines/rotary_macerator.lua | 285 ++++++++++++++++++++++++++++++++++ 5 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 machines/rotary_macerator.lua diff --git a/api.lua b/api.lua index 197c840..f93f6a8 100644 --- a/api.lua +++ b/api.lua @@ -23,6 +23,7 @@ industrialtest.api.extractorRecipes={} industrialtest.api.cableFormerRecipes={} industrialtest.api.geothermalGeneratorFuels={} industrialtest.api.waterMillFuels={} +industrialtest.api.rotaryMaceratorModifiers={} industrialtest.api.storageCells={} industrialtest.api.lvPowerFlow=600 @@ -864,6 +865,29 @@ industrialtest.api.getWaterMillFuelByItem=function(name) return nil end +-- \brief Registers Rotary Macerator recipe modifier +-- \param config table +-- \returns nil +industrialtest.api.registerRotaryMaceratorModifier=function(config) + local definition={ + name=config.name or "", + modifier=config.modifier or "", + output=config.output or "", + time=config.time or 2, + uses=config.uses or 1, + modifierLeftover=config.modifierLeftover + } + industrialtest.api.rotaryMaceratorModifiers[definition.name.." "..config.modifier]=definition +end + +-- \brief Returns modified Rotary Macerator recipe by item and modifier +-- \param name string +-- \param modifier string +-- \returns table +industrialtest.api.getRotaryMaceratorModifier=function(name,modifier) + return industrialtest.api.rotaryMaceratorModifiers[name.." "..modifier] +end + -- \brief Returns machine speed in items per operation -- \param meta MetaDataRef -- \returns number diff --git a/compatibility.lua b/compatibility.lua index 9a82653..41459b3 100644 --- a/compatibility.lua +++ b/compatibility.lua @@ -33,6 +33,12 @@ for _,mod in ipairs(requiredMclModules) do end end +if industrialtest.mtgAvailable then + industrialtest.stackMax=99 +elseif industrialtest.mclAvailable then + industrialtest.stackMax=64 +end + industrialtest.mods={} if industrialtest.mtgAvailable then industrialtest.mods._3dArmor=minetest.get_modpath("3d_armor") diff --git a/craftitems.lua b/craftitems.lua index 638e7e8..6acaaae 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -95,6 +95,11 @@ minetest.register_craft({ output="industrialtest:refined_iron_ingot", recipe=industrialtest.elementKeys.ironIngot }) +minetest.register_craft({ + type="cooking", + output="industrialtest:refined_iron_ingot", + recipe="industrialtest:refined_iron_dust" +}) minetest.register_craft({ type="shapeless", output="industrialtest:refined_iron_ingot 8", @@ -414,9 +419,33 @@ minetest.register_craft({ output=industrialtest.elementKeys.bronzeIngot, recipe="industrialtest:bronze_dust" }) +industrialtest.api.registerRotaryMaceratorModifier({ + name=industrialtest.elementKeys.copperLump, + modifier=industrialtest.elementKeys.tinLump, + output="industrialtest:bronze_dust 2", + uses=4 +}) +industrialtest.api.registerRotaryMaceratorModifier({ + name=industrialtest.elementKeys.copperIngot, + modifier=industrialtest.elementKeys.tinIngot, + output="industrialtest:bronze_dust", + uses=4 +}) industrialtest.api.registerResourceDust("sulfur","Sulfur",{},"#e3ff33ff",false) -industrialtest.api.registerResourceDust("lead","Lead",{},"#eafef8ff",false) -- TODO: Add lead ore +industrialtest.api.registerResourceDust("lead","Lead",{},"#eafef8ff",false) +industrialtest.api.registerResourceDust("refined_iron","Refined Iron",{ + { + resource="industrialtest:refined_iron_ingot", + count=1 + } +},"#7c8588ff",true) +industrialtest.api.registerRotaryMaceratorModifier({ + name=industrialtest.elementKeys.ironLump, + modifier=industrialtest.elementKeys.coal, + output="industrialtest:refined_iron_dust 2", + uses=industrialtest.stackMax +}) minetest.register_craftitem("industrialtest:hydrated_coal_dust",{ description=S("Hydrated Coal Dust"), @@ -451,6 +480,12 @@ minetest.register_craft({ } } }) +industrialtest.api.registerRotaryMaceratorModifier({ + name=industrialtest.elementKeys.coal, + modifier="industrialtest:water_cell", + output="industrialtest:hydrated_coal_dust", + uses=8 +}) minetest.register_craftitem("industrialtest:hydrated_coal",{ description=S("Hydrated Coal"), diff --git a/init.lua b/init.lua index 1f20582..a845202 100644 --- a/init.lua +++ b/init.lua @@ -46,6 +46,7 @@ dofile(modpath.."/machines/mass_fabricator.lua") dofile(modpath.."/machines/nuclear_reactor.lua") dofile(modpath.."/machines/power_storage.lua") dofile(modpath.."/machines/recycler.lua") +dofile(modpath.."/machines/rotary_macerator.lua") dofile(modpath.."/machines/tool_workshop.lua") dofile(modpath.."/machines/transformer.lua") dofile(modpath.."/machines/solar_panel_generator.lua") diff --git a/machines/rotary_macerator.lua b/machines/rotary_macerator.lua new file mode 100644 index 0000000..feb86d0 --- /dev/null +++ b/machines/rotary_macerator.lua @@ -0,0 +1,285 @@ +-- IndustrialTest +-- Copyright (C) 2024 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") + +local rotaryMacerator={} +rotaryMacerator.opPower=60 + +rotaryMacerator.getFormspec=function(pos) + local meta=minetest.get_meta(pos) + local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100 + local maxSrcTime=meta:get_float("maxSrcTime") + local srcPercent=maxSrcTime>0 and meta:get_float("srcTime")/maxSrcTime*100 or 0 + local rpm=meta:get_int("rpm") + local formspec + if industrialtest.mtgAvailable then + formspec={ + "list[context;src;3.8,1.8;1,1]", + "list[context;modifier;4.9,1.8;1,1]", + (powerPercent>0 and "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" + or "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png]"), + "list[context;powerStorage;3.8,3.9;1,1]", + (srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]" + or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"), + "list[context;dst;6,2.8;1,1;]", + "list[context;upgrades;9,0.9;1,4]", + "label[0.5,2.8;Speed: "..rpm.."]", + "listring[context;src]", + "listring[context;modifier]", + "listring[context;powerStorage]", + "listring[context;dst]", + "listring[context;upgrades]" + } + elseif industrialtest.mclAvailable then + formspec={ + "list[context;src;3.8,1.8;1,1]", + mcl_formspec.get_itemslot_bg(3.8,1.8,1,1), + "list[context;modifier;4.9,1.8;1,1]", + mcl_formspec.get_itemslot_bg(4.9,1.8,1,1), + (powerPercent>0 and "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" + or "image[3.8,2.8;1,1;industrialtest_gui_electricity_bg.png]"), + "list[context;powerStorage;3.8,3.9;1,1]", + mcl_formspec.get_itemslot_bg(3.8,3.9,1,1), + (srcPercent>0 and "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[lowpart:"..srcPercent..":gui_furnace_arrow_fg.png^[transformR270]" + or "image[4.9,2.8;1,1;gui_furnace_arrow_bg.png^[transformR270]"), + "list[context;dst;6,2.8;1,1;]", + mcl_formspec.get_itemslot_bg(6,2.8,1,1), + "list[context;upgrades;9,0.9;1,4]", + mcl_formspec.get_itemslot_bg(9,0.9,1,4), + "label[0.5,2.8;Speed: "..rpm.."]", + "listring[context;src]", + "listring[context;modifier]", + "listring[context;powerStorage]", + "listring[context;dst]", + "listring[context;upgrades]" + } + end + return table.concat(formspec,"") +end + +rotaryMacerator.onConstruct=function(pos,meta,inv) + inv:set_size("src",1) + inv:set_size("modifier",1) + inv:set_size("powerStorage",1) + inv:set_size("dst",1) + inv:set_size("upgrades",4) + meta:set_int("rpm",0) + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",0) +end + +rotaryMacerator.onTimer=function(pos,elapsed,meta,inv) + local shouldRerunTimer=false + local shouldUpdateFormspec=false + local srcSlot=inv:get_stack("src",1) + local modifierSlot=inv:get_stack("modifier",1) + local dstSlot=inv:get_stack("dst",1) + local powerAmount=meta:get_int("industrialtest.powerAmount") + local rpm=meta:get_int("rpm") + + shouldRerunTimer,shouldUpdateFormspec=industrialtest.internal.chargeFromPowerStorageItem(meta,inv) + + if rpm>0 then + meta:set_int("rpm",math.max(rpm-1000,0)) + shouldRerunTimer=shouldRerunTimer or rpm>0 + shouldUpdateFormspec=true + end + + if powerAmount>=rotaryMacerator.opPower and not srcSlot:is_empty() then + local result=industrialtest.api.getMaceratorRecipeResult(srcSlot:get_name()) + if result then + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",result.time) + minetest.swap_node(pos,{ + name="industrialtest:rotary_macerator_active", + param2=minetest.get_node(pos).param2 + }) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + return false,shouldUpdateFormspec + end + end + + return shouldRerunTimer,shouldUpdateFormspec +end + +rotaryMacerator.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count) + if toList=="dst" then + return 0 + end + return count +end + +rotaryMacerator.allowMetadataInventoryPut=function(pos,listname,index,stack) + if listname=="dst" then + return 0 + end + return stack:get_count() +end + +rotaryMacerator.onMetadataInventoryMove=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) +end + +rotaryMacerator.onMetadataInventoryPut=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) +end + +rotaryMacerator.onMetadataInventoryTake=function(pos,listname) + if listname=="dst" then + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end +end + +rotaryMacerator.activeOnTimer=function(pos,elapsed,meta,inv) + local srcSlot=inv:get_stack("src",1) + local modifierSlot=inv:get_stack("modifier",1) + local dstSlot=inv:get_stack("dst",1) + local powerAmount=meta:get_int("industrialtest.powerAmount") + local rpm=meta:get_int("rpm") + + industrialtest.internal.chargeFromPowerStorageItem(meta,inv) + + if srcSlot:is_empty() or powerAmount=meta:get_float("maxSrcTime") then + local speed=industrialtest.api.getMachineSpeed(meta) + local multiplier=1 + if srcSlot:get_count()>=speed then + multiplier=speed + end + resultStack:set_count(resultStack:get_count()*multiplier) + inv:add_item("dst",resultStack) + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",0) + srcSlot:set_count(srcSlot:get_count()-multiplier) + inv:set_stack("src",1,srcSlot) + meta:set_int("rpm",math.min(rpm+750,7500)) + if modified then + local modifierMeta=modifierSlot:get_meta() + local uses=result.uses + if modifierMeta:contains("uses") then + uses=modifierMeta:get_int("uses") + end + uses=math.max(uses-1,0) + if uses==0 then + if result.modifierLeftover then + modifierSlot:set_name(result.modifierLeftover) + else + modifierSlot:take_item(1) + end + uses=result.uses + end + if not modifierSlot:is_empty() and not result.modifierLeftover then + modifierMeta:set_int("uses",uses) + end + inv:set_stack("modifier",1,modifierSlot) + end + else + meta:set_float("srcTime",srcTime) + end + + return true,true +end + +industrialtest.internal.registerMachine({ + name="rotary_macerator", + displayName=S("Rotary Macerator"), + capacity=industrialtest.api.lvPowerFlow*2, + getFormspec=rotaryMacerator.getFormspec, + flow=industrialtest.api.lvPowerFlow, + ioConfig="iiiiii", + requiresWrench=true, + registerActiveVariant=true, + sounds="metal", + powerSlots={"powerStorage"}, + storageSlots={"src","modifier","powerStorage","dst","upgrades"}, + groups={ + _industrialtest_hasPowerInput=1 + }, + customKeys={ + tiles={ + "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_macerator_front.png" + }, + paramtype2="facedir", + legacy_facedir_simple=true + }, + activeCustomKeys={ + tiles={ + "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_macerator_front_active.png" + } + }, + onConstruct=rotaryMacerator.onConstruct, + onTimer=rotaryMacerator.onTimer, + allowMetadataInventoryMove=rotaryMacerator.allowMetadataInventoryMove, + allowMetadataInventoryPut=rotaryMacerator.allowMetadataInventoryPut, + onMetadataInventoryPut=rotaryMacerator.onMetadataInventoryPut, + onMetadataInventoryMove=rotaryMacerator.onMetadataInventoryMove, + onMetadataInventoryTake=rotaryMacerator.onMetadataInventoryTake, + activeOnTimer=rotaryMacerator.activeOnTimer +}) +minetest.register_craft({ + type="shaped", + output="industrialtest:rotary_macerator", + recipe={ + {"industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot"}, + {"industrialtest:refined_iron_ingot","industrialtest:macerator","industrialtest:refined_iron_ingot"}, + {"industrialtest:refined_iron_ingot","industrialtest:advanced_machine_block","industrialtest:refined_iron_ingot"} + } +})