From 40d0e578c43d168a5a50450e2f7ad7a5993b7bfe Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Fri, 7 Apr 2023 19:50:39 +0200 Subject: [PATCH] Refactor machines.lua (reduced ~300 lines of code) --- api.lua | 2 +- machines.lua | 1119 +++++++++++++++++++++++--------------------------- utils.lua | 2 +- 3 files changed, 523 insertions(+), 600 deletions(-) diff --git a/api.lua b/api.lua index bab209c..3d61b57 100644 --- a/api.lua +++ b/api.lua @@ -249,7 +249,7 @@ industrialtest.api.powerFlow=function(pos) if def then local updateFormspec=def._industrialtest_updateFormspec if updateFormspec then - updateFormspec(value) + updateFormspec(neighbourPositions[key]) end local onPowerFlow=def._industrialtest_onPowerFlow if onPowerFlow and transferred then diff --git a/machines.lua b/machines.lua index 33bdc9f..1baccd0 100644 --- a/machines.lua +++ b/machines.lua @@ -16,46 +16,6 @@ local S=minetest.get_translator("industrialtest") --- Generators -local function generatorFormspec(fuelPercent,charged) - local formspec - if industrialtest.mtgAvailable then - formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..S("Generator").."]", - "list[context;charged;4.9,1.8;1,1]", - "listring[context;charged]", - (fuelPercent>0 and "image[4.9,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" - or "image[4.9,2.8;1,1;default_furnace_fire_bg.png]"), - "list[context;fuel;4.9,3.9;1,1]", - "listring[context;fuel]", - "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 ""), - "list[current_player;main;0.5,6.25;8,1]list[current_player;main;0.5,7.5;8,3;8]" - } - elseif industrialtest.mclAvailable then - formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..S("Generator").."]", - "list[context;charged;4.7,1.8;1,1]", - mcl_formspec.get_itemslot_bg(4.7,1.8,1,1), - "listring[context;charged]", - (fuelPercent>0 and "image[4.7,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" - or "image[4.7,2.8;1,1;default_furnace_fire_bg.png]"), - "list[context;fuel;4.7,3.9;1,1]", - mcl_formspec.get_itemslot_bg(4.7,3.9,1,1), - "listring[context;fuel]", - "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 ""), - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) - } - end - return table.concat(formspec,"") -end local function mclAfterDigNode(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) @@ -71,40 +31,274 @@ local function mclAfterDigNode(pos,oldmeta,lists) end meta:from_table(meta2:to_table()) end -local definition={ - description=S("Generator"), - 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_iron_furnace_front.png", - "industrialtest_machine_block.png" +local function registerMachine(config) + local function getFormspec(pos) + local formspec + if industrialtest.mtgAvailable then + formspec={ + "formspec_version[4]", + "size[10.8,12]", + "label[0.5,0.5;"..S(config.displayName).."]", + (config.getFormspec and config.getFormspec(pos) or ""), + "list[current_player;main;0.5,6.25;8,1]", + "list[current_player;main;0.5,7.5;8,3;8]" + } + elseif industrialtest.mclAvailable then + formspec={ + "size[10.04,12]", + "label[0.25,0.25;"..S(config.displayName).."]", + (config.getFormspec and config.getFormspec(pos) or ""), + "list[current_player;main;0.5,7;9,3;9]", + mcl_formspec.get_itemslot_bg(0.5,7,9,3), + "list[current_player;main;0.5,10.24;9,1]", + mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + } + end + return table.concat(formspec,"") + end + local definition={ + description=S(config.displayName), + on_construct=function(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + meta:set_string("formspec",getFormspec(pos)) + if config.onConstruct then + config.onConstruct(pos,meta,inv) + end + industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + on_timer=function(pos,elapsed) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local shouldRerunTimer=false + local shouldUpdateFormspec=false + + if config.onTimer then + shouldRerunTimer,shouldUpdateFormspec=config.onTimer(pos,elapsed,meta,inv) + end + + if shouldUpdateFormspec then + meta:set_string("formspec",getFormspec(pos)) + end + + return shouldRerunTimer + end, + allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local movedItemStack=inv:get_stack(fromList,1) + local found=false + if config.powerSlots then + for _,value in ipairs(config.powerSlots) do + if value==toList then + found=true + break + end + end + end + 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, + allow_metadata_inventory_put=function(pos,listname,index,stack,player) + local found=false + if config.powerSlots then + for _,value in ipairs(config.powerSlots) do + if value==listname then + found=true + break + end + end + end + 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, + on_metadata_inventory_put=function(pos,listname,index,stack,player) + if config.onMetadataInventoryPut then + config.onMetadataInventoryPut(pos,listname,index,stack,player) + end + end, + on_metadata_inventory_take=function(pos,listname,index,stack,player) + if config.onMetadataInventoryTake then + config.onMetadataInventoryTake(pos,listname,index,stack,player) + end + end, + _industrialtest_updateFormspec=function(pos) + local meta=minetest.get_meta(pos) + meta:set_string("formspec",getFormspec(pos)) + end + } + if industrialtest.mtgAvailable then + definition.groups={cracky=2} + if config.sounds=="metal" then + definition.sounds=default.node_sound_metal_defaults() + end + definition.can_dig=function(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + for _,value in ipairs(config.storageSlots) do + if inv:get_stack(value,1):get_count()>0 then + return false + end + end + return true + end + elseif industrialtest.mclAvailable then + definition.after_dig_node=function(pos,oldnode,oldmeta) + mclAfterDigNode(pos,oldmeta,config.storageSlots) + end + if config.sounds=="metal" then + definition.sounds=mcl_sounds.node_sound_metal_defaults() + end + definition.groups={pickaxey=1} + definition._mcl_blast_resistance=3.5 + definition._mcl_hardness=3.9 + end + definition.groups._industrialtest_wrenchUnmountable=1 + if config.requiresWrench then + definition.drop="industrialtest:machine_block" + end + if config.customKeys then + for key,value in pairs(config.customKeys) do + definition[key]=value + end + end + if config.groups then + for key,value in pairs(config.groups) do + definition.groups[key]=value + end + end + minetest.register_node("industrialtest:"..config.name,definition) + if config.registerActiveVariant then + definition=table.copy(definition) + definition.description=nil + definition.on_timer=function(pos,elapsed) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local shouldRerunTimer=false + local shouldUpdateFormspec=false + + if config.onTimer then + shouldRerunTimer,shouldUpdateFormspec=config.activeOnTimer(pos,elapsed,meta,inv) + end + + if shouldUpdateFormspec then + meta:set_string("formspec",getFormspec(pos)) + end + + return shouldRerunTimer + end + if not definition.drop then + definition.drop="industrialtest:"..config.name + end + if config.activeCustomKeys then + for key,value in pairs(config.activeCustomKeys) do + definition[key]=value + end + end + if industrialtest.mclAvailable then + definition.groups.not_in_creative_inventory=1 + definition._doc_items_create_entry=false + end + minetest.register_node("industrialtest:"..config.name.."_active",definition) + end +end + +-- Generators +local function generatorFormspec(pos) + local meta=minetest.get_meta(pos) + local fuelPercent=meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100 + local charged=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity") + local formspec + if industrialtest.mtgAvailable then + formspec={ + "list[context;charged;4.9,1.8;1,1]", + "listring[context;charged]", + (fuelPercent>0 and "image[4.9,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" + or "image[4.9,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;fuel;4.9,3.9;1,1]", + "listring[context;fuel]", + "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 "") + } + elseif industrialtest.mclAvailable then + formspec={ + "list[context;charged;4.7,1.8;1,1]", + mcl_formspec.get_itemslot_bg(4.7,1.8,1,1), + "listring[context;charged]", + (fuelPercent>0 and "image[4.7,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" + or "image[4.7,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;fuel;4.7,3.9;1,1]", + mcl_formspec.get_itemslot_bg(4.7,3.9,1,1), + "listring[context;fuel]", + "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 "") + } + end + return table.concat(formspec,"") +end +registerMachine({ + name="generator", + displayName="Generator", + getFormspec=generatorFormspec, + capacity=7000, + flow=industrialtest.api.lvPowerFlow, + ioConfig="oooooo", + registerActiveVariant=true, + powerSlots={"charged"}, + storageSlots={"charged","fuel"}, + sounds="metal", + groups={ + _industrialtest_hasPowerOutput=1 }, - paramtype2="facedir", - legacy_facedir_simple=true, - on_construct=function(pos,placer) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + customKeys={ + 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_iron_furnace_front.png", + "industrialtest_machine_block.png" + }, + paramtype2="facedir", + legacy_facedir_simple=true + }, + 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_iron_furnace_front_active.png", + "industrialtest_machine_block.png" + }, + light_source=8 + }, + onConstruct=function(pos,meta,inv) inv:set_size("charged",1) inv:set_size("fuel",1) - meta:set_string("formspec",generatorFormspec(0,0)) meta:set_int("fuelTime",0) meta:set_int("maxFuelTime",1) - industrialtest.api.addPowerStorage(meta,7000,industrialtest.api.lvPowerFlow,"oooooo") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, - on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) + onTimer=function(pos,elapsed,meta,inv) local powerFlow=meta:get_int("industrialtest.powerFlow") - local inv=meta:get_inventory() local chargedSlot=inv:get_stack("charged",1) local fuelSlot=inv:get_stack("fuel",1) local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) local shouldUpdateFormspec=flowTransferred local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) - if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then industrialtest.api.transferPowerToItem(meta,chargedSlot,powerFlow) inv:set_stack("charged",1,chargedSlot) @@ -128,122 +322,52 @@ local definition={ minetest.get_node_timer(pos):start(industrialtest.updateDelay) end end - - if shouldUpdateFormspec then - meta:set_string("formspec",generatorFormspec(meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"))) + return shouldRerunTimer,shouldUpdateFormspec + end, + activeOnTimer=function(pos,elapsed,meta,inv) + local chargedSlot=inv:get_stack("charged",1) + local fuelSlot=inv:get_stack("fuel",1) + local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) + local shouldUpdateFormspec=flowTransferred + local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) + if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then + industrialtest.api.transferPowerToItem(meta,chargedSlot,powerFlow) + inv:set_stack("charged",1,chargedSlot) + shouldUpdateFormspec=true + shouldRerunTimer=true end - - return shouldRerunTimer - end, - allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count,player) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local movedItemStack=inv:get_list(fromList)[1] - if toList=="charged" and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then - return 0 - end - return count - end, - allow_metadata_inventory_put=function(pos,listname,index,stack,player) - if listname=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then - return 0 - end - return stack:get_count() - end, - on_metadata_inventory_put=function(pos,listname,index,stack,player) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end, - on_metadata_inventory_take=function(pos,listname,index,stack,player) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end, - _industrialtest_updateFormspec=function(meta) - meta:set_string("formspec",generatorFormspec(meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"))) - end -} -if industrialtest.mtgAvailable then - definition.groups={cracky=2} - definition.sounds=default.node_sound_metal_defaults() - definition.can_dig=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - return not (inv:get_list("charged")[1]:get_count()>0 or inv:get_list("fuel")[1]:get_count()>0) - end -elseif industrialtest.mclAvailable then - definition.after_dig_node=function(pos,oldnode,oldmeta) - mclAfterDigNode(pos,oldmeta,{"charged","fuel"}) - end - definition.groups={pickaxey=1} - definition.sounds=mcl_sounds.node_sound_metal_defaults() - definition._mcl_blast_resistance=3.5 - definition._mcl_hardness=3.9 -end -definition.groups._industrialtest_hasPowerOutput=1 -definition.groups._industrialtest_wrenchUnmountable=1 -minetest.register_node("industrialtest:generator",definition) -definition=table.copy(definition) -definition.description=nil -definition.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_iron_furnace_front_active.png", - "industrialtest_machine_block.png" -} -definition.drop="industrialtest:generator" -definition.light_source=8 -definition.on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local powerFlow=meta:get_int("industrialtest.powerFlow") - local inv=meta:get_inventory() - local chargedSlot=inv:get_stack("charged",1) - local fuelSlot=inv:get_stack("fuel",1) - local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) - local shouldUpdateFormspec=flowTransferred - local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) - - if chargedSlot:get_count()>0 and not industrialtest.api.isFullyCharged(chargedSlot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0 then - industrialtest.api.transferPowerToItem(meta,chargedSlot,powerFlow) - inv:set_stack("charged",1,chargedSlot) - shouldUpdateFormspec=true - shouldRerunTimer=true - end - if fuelSlot:get_count()>0 and meta:get_int("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then - local output,after=minetest.get_craft_result({ + if fuelSlot:get_count()>0 and meta:get_int("fuelTime")<=0 and not industrialtest.api.isFullyCharged(meta) then + local output,after=minetest.get_craft_result({ method="fuel", width=1, items={fuelSlot} - }) - if output.time>0 then - meta:set_int("fuelTime",output.time) - meta:set_int("maxFuelTime",output.time) - inv:set_stack("fuel",1,after.items[1]) + }) + if output.time>0 then + meta:set_int("fuelTime",output.time) + meta:set_int("maxFuelTime",output.time) + inv:set_stack("fuel",1,after.items[1]) + end end + if meta:get_int("fuelTime")>0 then + meta:set_int("fuelTime",meta:get_int("fuelTime")-elapsed) + industrialtest.api.addPower(meta,200) + shouldUpdateFormspec=true + shouldRerunTimer=true + else + minetest.swap_node(pos,{ + name="industrialtest:generator", + param2=minetest.get_node(pos).param2 + }) + end + return shouldRerunTimer,shouldUpdateFormspec + end, + onMetadataInventoryPut=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + onMetadataInventoryMove=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) end - if meta:get_int("fuelTime")>0 then - meta:set_int("fuelTime",meta:get_int("fuelTime")-elapsed) - industrialtest.api.addPower(meta,200) - shouldUpdateFormspec=true - shouldRerunTimer=true - else - minetest.swap_node(pos,{ - name="industrialtest:generator", - param2=minetest.get_node(pos).param2 - }) - end - - if shouldUpdateFormspec then - meta:set_string("formspec",generatorFormspec(meta:get_int("fuelTime")/meta:get_int("maxFuelTime")*100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"))) - end - - return shouldRerunTimer -end -if industrialtest.mclAvailable then - definition.groups.not_in_creative_inventory=1 - definition._doc_items_create_entry=false -end -minetest.register_node("industrialtest:generator_active",definition) +}) minetest.register_craft({ type="shaped", output="industrialtest:generator", @@ -264,58 +388,56 @@ minetest.register_craft({ }) local function registerSolarPanelGenerator(config) - local function getFormspec(charging) + local function getFormspec(pos) + local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0 + local charging=amount>0.5 local formspec if industrialtest.mtgAvailable then formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..S(config.displayName).."]", "list[context;charged;4.9,1.8;1,1]", "listring[context;charged]", (charging and "image[4.9,2.8;1,1;industrialtest_gui_sun_fg.png]" - or "image[4.9,2.8;1,1;industrialtest_gui_sun_bg.png]"), - "list[current_player;main;0.5,6.25;8,1]list[current_player;main;0.5,7.5;8,3;8]" + or "image[4.9,2.8;1,1;industrialtest_gui_sun_bg.png]") } elseif industrialtest.mclAvailable then formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..S(config.displayName).."]", "list[context;charged;4.7,1.8;1,1]", mcl_formspec.get_itemslot_bg(4.7,1.8,1,1), "listring[context;charged]", (charging and "image[4.7,2.8;1,1;industrialtest_gui_sun_fg.png]" - or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]"), - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + or "image[4.7,2.8;1,1;industrialtest_gui_sun_bg.png]") } end return table.concat(formspec,"") end - definition={ - description=S(config.displayName), - tiles={ - "industrialtest_machine_block.png^industrialtest_"..config.name.."_top.png", - "industrialtest_machine_block.png" + registerMachine({ + name=config.name, + displayName=config.displayName, + getFormspec=getFormspec, + capacity=config.capacity, + flow=config.flow, + ioConfig="oooooo", + requiresWrench=true, + registerActiveVariant=false, + powerSlots={"charged"}, + storageSlots={"charged"}, + sounds="metal", + groups={ + _industrialtest_hasPowerOutput=1 }, - drop="industrialtest:machine_block", - on_construct=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + customKeys={ + tiles={ + "industrialtest_machine_block.png^industrialtest_"..config.name.."_top.png", + "industrialtest_machine_block.png" + } + }, + onConstruct=function(pos,meta,inv) inv:set_size("charged",1) - meta:set_string("formspec",getFormspec(false)) meta:set_float("prevAmount",0) - industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,"oooooo") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, - on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + onTimer=function(pos,elapsed,meta,inv) local chargedSlot=inv:get_stack("charged",1) local shouldUpdateFormspec=false - local amount=minetest.get_natural_light(vector.offset(pos,0,1,0))/15.0 local charging=amount>0.5 if charging then @@ -332,55 +454,9 @@ local function registerSolarPanelGenerator(config) shouldUpdateFormspec=true meta:set_float("prevAmount",amount) end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(charging)) - end - - return true - end, - allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local movedItemStack=inv:get_list(fromList)[1] - if toList=="charged" and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then - return 0 - end - return count - end, - allow_metadata_inventory_put=function(pos,listname,index,stack) - if listname=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then - return 0 - end - return stack:get_count() - end, - on_metadata_inventory_put=function(pos) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end, - on_metadata_inventory_take=function(pos) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) + return true,shouldUpdateFormspec end - } - if industrialtest.mtgAvailable then - definition.groups={cracky=2} - definition.sounds=default.node_sound_metal_defaults() - definition.can_dig=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - return inv:get_list("charged")[1]:get_count()==0 - end - elseif industrialtest.mclAvailable then - definition.groups={pickaxey=1} - definition.sounds=mcl_sounds.node_sound_metal_defaults() - definition._mcl_blast_resistance=1 - definition._mcl_hardness=3.9 - definition.after_dig_node=function(pos,oldnode,oldmeta) - mclAfterDigNode(pos,oldmeta,{"charged"}) - end - end - definition.groups._industrialtest_hasPowerOutput=1 - definition.groups._industrialtest_wrenchUnmountable=1 - minetest.register_node("industrialtest:"..config.name,definition) + }) end registerSolarPanelGenerator({ @@ -448,15 +524,16 @@ minetest.register_craft({ }) local function registerFluidGenerator(config) - local function getFormspec(fluidPercent,powerPercent,fluid) + local function getFormspec(pos) + local meta=minetest.get_meta(pos) + local fluidPercent=meta:get_float("fluidAmount")/100 + local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity") + local fluid=meta:get_string("fluid") local formspec local fuel=config.getFuel(fluid) local tile=(fuel and fuel.texture or "industrialtest_gui_fluid_bg.png") if industrialtest.mtgAvailable then formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..S(config.displayName).."]", "list[context;fluid;2,1.8;1,1]", "listring[context;fluid]", (fluidPercent>0 and "image[2,3;1,1;industrialtest_gui_fluid_bg.png^[lowpart:"..fluidPercent..":"..tile.."]" or "image[2,3;1,1;industrialtest_gui_fluid_bg.png]"), @@ -465,13 +542,10 @@ local function registerFluidGenerator(config) "list[context;charged;6,3;1,1]", "listring[context;charged]", "box[9,1;0.3,4.8;#202020]", - (powerPercent>0 and "box[9,"..(1+4.8-(powerPercent*4.8))..";0.3,"..(powerPercent*4.8)..";#FF1010]" or ""), - "list[current_player;main;0.5,6.25;8,1]list[current_player;main;0.5,7.5;8,3;8]" + (powerPercent>0 and "box[9,"..(1+4.8-(powerPercent*4.8))..";0.3,"..(powerPercent*4.8)..";#FF1010]" or "") } elseif industrialtest.mclAvailable then formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..S(config.displayName).."]", "list[context;fluid;2,1.8;1,1]", mcl_formspec.get_itemslot_bg(2,1.8,1,1), "listring[context;fluid]", @@ -483,48 +557,51 @@ local function registerFluidGenerator(config) mcl_formspec.get_itemslot_bg(6,3,1,1), "listring[context;charged]", "box[9,1;0.3,4.8;#202020]", - (powerPercent>0 and "box[9,"..(1+4.8-(powerPercent*4.8))..";0.3,"..(powerPercent*4.8)..";#FF1010]" or ""), - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + (powerPercent>0 and "box[9,"..(1+4.8-(powerPercent*4.8))..";0.3,"..(powerPercent*4.8)..";#FF1010]" or "") } end return table.concat(formspec,"") end - definition={ - description=S(config.displayName), - tiles={ - "industrialtest_machine_block.png"..(config.customTopTexture and "^"..config.customTopTexture or ""), - "industrialtest_machine_block.png"..(config.customBottomTexture and "^"..config.customBottomTexture or ""), - "industrialtest_machine_block.png"..(config.customRightTexture and "^"..config.customRightTexture or ""), - "industrialtest_machine_block.png"..(config.customLeftTexture and "^"..config.customLeftTexture or ""), - "industrialtest_machine_block.png"..(config.customBackTexture and "^"..config.customBackTexture or ""), - "industrialtest_machine_block.png"..(config.customFrontTexture and "^"..config.customFrontTexture or "") + local definition={ + name=config.name, + displayName=config.displayName, + getFormspec=getFormspec, + capacity=7000, + flow=industrialtest.api.lvPowerFlow, + ioConfig="oooooo", + requiresWrench=true, + registerActiveVariant=config.registerActiveVariant, + powerSlots={"charged"}, + storageSlots={"fluid","fluidLeftover"}, + sounds="metal", + groups={ + _industrialtest_hasPowerOutput=1 }, - paramtype2="facedir", - legacy_facedir_simple=true, - drop="industrialtest:machine_block", - on_construct=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + customKeys={ + tiles={ + "industrialtest_machine_block.png"..(config.customTopTexture and "^"..config.customTopTexture or ""), + "industrialtest_machine_block.png"..(config.customBottomTexture and "^"..config.customBottomTexture or ""), + "industrialtest_machine_block.png"..(config.customRightTexture and "^"..config.customRightTexture or ""), + "industrialtest_machine_block.png"..(config.customLeftTexture and "^"..config.customLeftTexture or ""), + "industrialtest_machine_block.png"..(config.customBackTexture and "^"..config.customBackTexture or ""), + "industrialtest_machine_block.png"..(config.customFrontTexture and "^"..config.customFrontTexture or "") + }, + paramtype2="facedir", + legacy_facedir_simple=true + }, + onConstruct=function(pos,meta,inv) inv:set_size("charged",1) inv:set_size("fluid",1) inv:set_size("leftover",1) meta:set_float("fluidAmount",0) meta:set_string("fluid","") - meta:set_string("formspec",getFormspec(0,0,"")) - industrialtest.api.addPowerStorage(meta,7000,industrialtest.api.lvPowerFlow,"oooooo") end, - on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + onTimer=function(pos,elapsed,meta,inv) local fluidSlot=inv:get_stack("fluid",1) local chargedSlot=inv:get_stack("charged",1) local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) local shouldUpdateFormspec=false local shouldRerunTimer=(afterFlow and meta:get_int("industrialtest.powerAmount")>0) - if fluidSlot:get_count()>0 and meta:get_float("fluidAmount")<=9000 then local fuel=config.getFuelByItem(fluidSlot:get_name()) if fuel and (fuel.name==meta:get_string("fluid") or meta:get_string("fluid")=="") then @@ -574,75 +651,28 @@ local function registerFluidGenerator(config) if flowTransferred then shouldUpdateFormspec=true end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(meta:get_float("fluidAmount")/100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"),meta:get_string("fluid"))) - end - - return shouldRerunTimer + return shouldRerunTimer,shouldUpdateFormspec end, - allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local itemstack=inv:get_stack(fromList,fromIndex) - if toList=="charged" and not industrialtest.api.hasPowerStorage(itemstack:get_meta()) then - return 0 - end - return count - end, - allow_metadata_inventory_put=function(pos,listname,index,stack) - if toList=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then - return 0 - end - return stack:get_count() - end, - on_metadata_inventory_move=function(pos) + onMetadataInventoryPut=function(pos) minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, - on_metadata_inventory_put=function(pos) + onMetadataInventoryMove=function(pos) minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end, - _industrialtest_updateFormspec=function(meta) - meta:set_string("formspec",getFormspec(meta:get_float("fluidAmount")/100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"),meta:get_string("fluid"))) end } - if industrialtest.mtgAvailable then - definition.groups={ - cracky=1, - level=2 - } - definition.sounds=default.node_sound_metal_defaults() - definition.can_dig=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - return not (inv:get_list("charged")[1]:get_count()>0 or inv:get_list("fluid")[1]:get_count()>0) - end - elseif industrialtest.mclAvailable then - definition.after_dig_node=function(pos,oldnode,oldmeta) - mclAfterDigNode(pos,oldmeta,{"charged","fluid"}) - end - definition.groups={pickaxey=1} - definition.sounds=mcl_sounds.node_sound_metal_defaults() - definition._mcl_blast_resistance=3 - definition._mcl_hardness=3.5 - end - definition.groups._industrialtest_hasPowerOutput=1 - definition.groups._industrialtest_wrenchUnmountable=1 - minetest.register_node("industrialtest:"..config.name,definition) if config.registerActiveVariant then - definition=table.copy(definition) - definition.description=nil - definition.tiles={ - "industrialtest_machine_block.png"..(config.customTopTexture and "^"..config.customTopTextureActive or ""), - "industrialtest_machine_block.png"..(config.customBottomTexture and "^"..config.customBottomTextureActive or ""), - "industrialtest_machine_block.png"..(config.customRightTexture and "^"..config.customRightTextureActive or ""), - "industrialtest_machine_block.png"..(config.customLeftTexture and "^"..config.customLeftTextureActive or ""), - "industrialtest_machine_block.png"..(config.customBackTexture and "^"..config.customBackTextureActive or ""), - "industrialtest_machine_block.png"..(config.customFrontTexture and "^"..config.customFrontTextureActive or "") + definition.activeCustomKeys={ + tiles={ + "industrialtest_machine_block.png"..(config.customTopTexture and "^"..config.customTopTextureActive or ""), + "industrialtest_machine_block.png"..(config.customBottomTexture and "^"..config.customBottomTextureActive or ""), + "industrialtest_machine_block.png"..(config.customRightTexture and "^"..config.customRightTextureActive or ""), + "industrialtest_machine_block.png"..(config.customLeftTexture and "^"..config.customLeftTextureActive or ""), + "industrialtest_machine_block.png"..(config.customBackTexture and "^"..config.customBackTextureActive or ""), + "industrialtest_machine_block.png"..(config.customFrontTexture and "^"..config.customFrontTextureActive or "") + }, + light_source=8 } - definition.on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + definition.activeOnTimer=function(pos,elapsed,meta,inv) local fluidSlot=inv:get_stack("fluid",1) local chargedSlot=inv:get_stack("charged",1) local afterFlow,flowTransferred=industrialtest.api.powerFlow(pos) @@ -697,20 +727,10 @@ local function registerFluidGenerator(config) if flowTransferred then shouldUpdateFormspec=true end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(meta:get_float("fluidAmount")/100,meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity"),meta:get_string("fluid"))) - end - - return shouldRerunTimer + return shouldRerunTimer,shouldUpdateFormspec end - if industrialtest.mclAvailable then - definition.groups.not_in_creative_inventory=1 - definition._doc_items_create_entry=false - end - definition.light_source=8 - minetest.register_node("industrialtest:"..config.name.."_active",definition) end + registerMachine(definition) end registerFluidGenerator({ @@ -788,62 +808,61 @@ minetest.register_craft({ } }) -local function windMillFormspec(charging) +local function windMillFormspec(pos) + local meta=minetest.get_meta(pos) + local charging=meta:get_int("charging") local formspec if industrialtest.mtgAvailable then formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..S("Wind Mill").."]", "list[context;charged;4.9,1.8;1,1]", "listring[context;charged]", (charging>0 and "image[4.9,3;1,1;industrialtest_gui_wind_bg.png^[lowpart:"..charging..":industrialtest_gui_wind_fg.png]" - or "image[4.9,3;1,1;industrialtest_gui_wind_bg.png]"), - "list[current_player;main;0.5,6.25;8,1]list[current_player;main;0.5,7.5;8,3;8]" + or "image[4.9,3;1,1;industrialtest_gui_wind_bg.png]") } elseif industrialtest.mclAvailable then formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..S("Wind Mill").."]", "list[context;charged;4.7,1.8;1,1]", mcl_formspec.get_itemslot_bg(4.7,1.8,1,1), "listring[context;charged]", (charging>0 and "image[4.7,3;1,1;industrialtest_gui_wind_bg.png^[lowpart:"..charging..":industrialtest_gui_wind_fg.png]" - or "image[4.7,3;1,1;industrialtest_gui_wind_bg.png]"), - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + or "image[4.7,3;1,1;industrialtest_gui_wind_bg.png]") } end return table.concat(formspec,"") end -definition={ - description=S("Wind Mill"), - tiles={ - "industrialtest_machine_block.png", - "industrialtest_machine_block.png", - "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", - "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", - "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", - "industrialtest_machine_block.png^industrialtest_wind_mill_side.png" +registerMachine({ + name="wind_mill", + displayName="Wind Mill", + getFormspec=windMillFormspec, + capacity=7000, + flow=industrialtest.api.lvPowerFlow, + ioConfig="oooooo", + requiresWrench=true, + registerActiveVariant=false, + powerSlots={"charged"}, + storageSlots={"charged"}, + sounds="metal", + groups={ + _industrialtest_hasPowerOutput=1 }, - drop="industrialtest:machine_block", - on_construct=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + customKeys={ + tiles={ + "industrialtest_machine_block.png", + "industrialtest_machine_block.png", + "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", + "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", + "industrialtest_machine_block.png^industrialtest_wind_mill_side.png", + "industrialtest_machine_block.png^industrialtest_wind_mill_side.png" + } + }, + onConstruct=function(pos,meta,inv) inv:set_size("charged",1) meta:set_int("prevCharging",0) - meta:set_string("formspec",windMillFormspec(0)) - industrialtest.api.addPowerStorage(meta,7000,industrialtest.api.lvPowerFlow,"oooooo") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) + meta:set_int("charging",0) end, - on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + onTimer=function(pos,elapsed,meta,inv) local chargedSlot=inv:get_stack("charged",1) local shouldUpdateFormspec=false - local charging if industrialtest.mtgAvailable then charging=math.min(math.max(pos.y,0)/150,1.0) @@ -880,55 +899,13 @@ definition={ if industrialtest.api.transferPowerToItem(meta,chargedSlot,industrialtest.api.lvPowerFlow)>0 then inv:set_stack("charged",1,chargedSlot) end - minetest.chat_send_all("charging") end industrialtest.api.powerFlow(pos) + meta:set_int("charging",charging*100) + return true,shouldUpdateFormspec - if shouldUpdateFormspec then - meta:set_string("formspec",windMillFormspec(charging*100)) - end - - return true - end, - allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local itemstack=inv:get_stack(fromList,fromIndex) - if toList=="charged" and not industrialtest.api.hasPowerStorage(itemstack:get_meta()) then - return 0 - end - return count - end, - allow_metadata_inventory_put=function(pos,listname,index,stack) - if toList=="charged" and not industrialtest.api.hasPowerStorage(stack:get_meta()) then - return 0 - end - return stack:get_count() end -} -if industrialtest.mtgAvailable then - definition.groups={ - cracky=1, - level=2 - } - definition.sounds=default.node_sound_metal_defaults() - definition.can_dig=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - return inv:get_list("charged")[1]:get_count()==0 - end -elseif industrialtest.mclAvailable then - definition.after_dig_node=function(pos,oldnode,oldmeta) - mclAfterDigNode(pos,oldmeta,{"charged"}) - end - definition.groups={pickaxey=1} - definition.sounds=mcl_sounds.node_sound_metal_defaults() - definition._mcl_blast_resistance=3 - definition._mcl_hardness=3.5 -end -definition.groups._industrialtest_hasPowerOutput=1 -definition.groups._industrialtest_wrenchUnmountable=1 -minetest.register_node("industrialtest:wind_mill",definition) +}) minetest.register_craft({ type="shaped", output="industrialtest:wind_mill", @@ -941,13 +918,13 @@ minetest.register_craft({ -- Item processing machines local function registerSimpleElectricItemProcessor(config) - local function getFormspec(powerPercent,srcPercent) + local function getFormspec(pos) + local meta=minetest.get_meta(pos) + local powerPercent=meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100 + local srcPercent=meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100 local formspec if industrialtest.mtgAvailable then formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..S(config.displayName).."]", "list[context;src;3.4,1.8;1,1]", "listring[context;src]", (powerPercent>0 and "image[3.4,2.8;1,1;industrialtest_gui_electricity_bg.png^[lowpart:"..powerPercent..":industrialtest_gui_electricity_fg.png]" @@ -959,14 +936,10 @@ local function registerSimpleElectricItemProcessor(config) "list[context;dst;6.4,2.8;1,1]", "listring[context;dst]", "list[context;upgrades;9,0.9;1,4]", - "listring[context;upgrades]", - "list[current_player;main;0.5,6.25;8,1]", - "list[current_player;main;0.5,7.5;8,3;8]" + "listring[context;upgrades]" } elseif industrialtest.mclAvailable then formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..S(config.displayName).."]", "list[context;src;3.4,1.8;1,1]", mcl_formspec.get_itemslot_bg(3.4,1.8,1,1), "listring[context;src]", @@ -982,11 +955,7 @@ local function registerSimpleElectricItemProcessor(config) "listring[context;dst]", "list[context;upgrades;9,0.9;1,4]", mcl_formspec.get_itemslot_bg(9,0.9,1,4), - "listring[context;upgrades]", - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + "listring[context;upgrades]" } end return table.concat(formspec,"") @@ -1062,41 +1031,62 @@ local function registerSimpleElectricItemProcessor(config) end error("Unknown craft method passed to craftResultProxy") end - definition={ - description=S(config.displayName), - tiles={ - "industrialtest_machine_block.png"..(config.customTopTexture and "^industrialtest_"..config.name.."_top.png" or ""), - "industrialtest_machine_block.png"..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom.png" or ""), - "industrialtest_machine_block.png"..(config.customRightTexture and "^industrialtest_"..config.name.."_right.png" or ""), - "industrialtest_machine_block.png"..(config.customLeftTexture and "^industrialtest_"..config.name.."_left.png" or ""), - "industrialtest_machine_block.png"..(config.customBackTexture and "^industrialtest_"..config.name.."_back.png" or ""), - "industrialtest_machine_block.png"..(config.customFrontTexture and "^industrialtest_"..config.name.."_front.png" or "") + registerMachine({ + name=config.name, + displayName=config.displayName, + capacity=config.capacity, + getFormspec=getFormspec, + flow=config.flow, + ioConfig="iiiiii", + requiresWrench=config.requiresWrench, + registerActiveVariant=true, + sounds="metal", + powerSlots={"powerStorage"}, + storageSlots={"src","dst","powerStorage","upgrades"}, + groups={ + _industrialtest_hasPowerOutput=1 }, - paramtype2="facedir", - legacy_facedir_simple=true, - drop=(config.requiresWrench and "industrialtest:machine_block" or "industrialtest:"..config.name), - on_construct=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + customKeys={ + tiles={ + "industrialtest_machine_block.png"..(config.customTopTexture and "^industrialtest_"..config.name.."_top.png" or ""), + "industrialtest_machine_block.png"..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom.png" or ""), + "industrialtest_machine_block.png"..(config.customRightTexture and "^industrialtest_"..config.name.."_right.png" or ""), + "industrialtest_machine_block.png"..(config.customLeftTexture and "^industrialtest_"..config.name.."_left.png" or ""), + "industrialtest_machine_block.png"..(config.customBackTexture and "^industrialtest_"..config.name.."_back.png" or ""), + "industrialtest_machine_block.png"..(config.customFrontTexture and "^industrialtest_"..config.name.."_front.png" or "") + }, + paramtype2="facedir", + legacy_facedir_simple=true, + _industrialtest_onPowerFlow=function(pos) + -- FIXME: this probably will require refactor so node timer won't be started + -- just to test if machine can process item + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end + }, + activeCustomKeys={ + tiles={ + "industrialtest_machine_block.png"..(config.customTopTexture and "^industrialtest_"..config.name.."_top_active.png" or ""), + "industrialtest_machine_block.png"..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom_active.png" or ""), + "industrialtest_machine_block.png"..(config.customRightTexture and "^industrialtest_"..config.name.."_right_active.png" or ""), + "industrialtest_machine_block.png"..(config.customLeftTexture and "^industrialtest_"..config.name.."_left_active.png" or ""), + "industrialtest_machine_block.png"..(config.customBackTexture and "^industrialtest_"..config.name.."_back_active.png" or ""), + "industrialtest_machine_block.png"..(config.customFrontTexture and "^industrialtest_"..config.name.."_front_active.png" or "") + } + }, + onConstruct=function(pos,meta,inv) inv:set_size("src",1) inv:set_size("dst",1) inv:set_size("powerStorage",1) inv:set_size("upgrades",4) - meta:set_string("formspec",getFormspec(0,0)) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) - industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,"iiiiii") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, - on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() + onTimer=function(pos,elapsed,meta,inv) local srcSlot=inv:get_stack("src",1) local powerStorageSlot=inv:get_stack("powerStorage",1) local shouldUpdateFormspec=false local shouldRerunTimer=false local requiredPower=elapsed*config.opPower - if powerStorageSlot:get_count()>0 then local stackMeta=powerStorageSlot:get_meta() if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then @@ -1123,28 +1113,18 @@ local function registerSimpleElectricItemProcessor(config) if not industrialtest.api.isFullyCharged(meta) then industrialtest.api.triggerNeighbours(pos) end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) - end - - return shouldRerunTimer + return shouldRerunTimer,shouldUpdateFormspec end, - allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,count) + allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,count) if toList=="dst" then return 0 - elseif toList=="powerStorage" then - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local stack=inv:get_stack(fromList,fromIndex) - return (industrialtest.api.hasPowerStorage(stack:get_meta()) and count or 0) elseif toList=="upgrades" then -- TODO: Add support for upgrades when they will be added return 0 end return count end, - allow_metadata_inventory_put=function(pos,listname,index,stack) + allowMetadataInventoryPut=function(pos,listname,index,stack) if listname=="dst" then return 0 elseif listname=="src" then @@ -1155,15 +1135,16 @@ local function registerSimpleElectricItemProcessor(config) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) end - elseif listname=="powerStorage" then - return (industrialtest.api.hasPowerStorage(stack:get_meta()) and stack:get_count() or 0) elseif listname=="upgrades" then --TODO: See allow_metadata_inventory_move return 0 end return stack:get_count() end, - on_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) + onMetadataInventoryPut=function(pos) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) + end, + onMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count) local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local srcSlot=inv:get_stack("src",1) @@ -1172,18 +1153,13 @@ local function registerSimpleElectricItemProcessor(config) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",getFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + meta:set_string("formspec",getFormspec(pos)) end elseif fromList=="dst" and dstSlot:get_free_space()==0 then minetest.get_node_timer(pos):start(industrialtest.updateDelay) end end, - on_metadata_inventory_put=function(pos,listname) - if listname=="src" or listname=="powerStorage" then - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end - end, - on_metadata_inventory_take=function(pos,listname,index,stack) + onMetadataInventoryTake=function(pos,listname,index,stack) local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local srcSlot=inv:get_stack("src",1) @@ -1192,123 +1168,70 @@ local function registerSimpleElectricItemProcessor(config) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",getFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + meta:set_string("formspec",getFormspec(pos)) end elseif listname=="dst" and dstSlot:get_free_space()==0 then minetest.get_node_timer(pos):start(industrialtest.updateDelay) end end, - _industrialtest_updateFormspec=function(meta) - meta:set_string("formspec",getFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) - end, - _industrialtest_onPowerFlow=function(pos) - -- FIXME: this probably will require refactor so node timer won't be started - -- just to test if machine can process item - minetest.get_node_timer(pos):start(industrialtest.updateDelay) - end - } - if industrialtest.mtgAvailable then - definition.groups={ - cracky=1, - level=2 - } - definition.sounds=default.node_sound_metal_defaults() - definition.can_dig=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - return not (inv:get_list("src")[1]:get_count()>0 or inv:get_list("powerStorage")[1]:get_count()>0 or inv:get_list("dst")[1]:get_count()>0) - end - elseif industrialtest.mclAvailable then - definition.after_dig_node=function(pos,oldnode,oldmeta) - mclAfterDigNode(pos,oldmeta,{"src","powerStorage","dst","upgrades"}) - end - definition.groups={pickaxey=1} - definition.sounds=mcl_sounds.node_sound_metal_defaults() - definition._mcl_blast_resistance=3 - definition._mcl_hardness=3.5 - end - definition.groups._industrialtest_hasPowerInput=1 - definition.groups._industrialtest_wrenchUnmountable=1 - minetest.register_node("industrialtest:"..config.name,definition) - definition=table.copy(definition) - definition.description=nil - definition.tiles={ - "industrialtest_machine_block.png"..(config.customTopTexture and "^industrialtest_"..config.name.."_top_active.png" or ""), - "industrialtest_machine_block.png"..(config.customBottomTexture and "^industrialtest_"..config.name.."_bottom_active.png" or ""), - "industrialtest_machine_block.png"..(config.customRightTexture and "^industrialtest_"..config.name.."_right_active.png" or ""), - "industrialtest_machine_block.png"..(config.customLeftTexture and "^industrialtest_"..config.name.."_left_active.png" or ""), - "industrialtest_machine_block.png"..(config.customBackTexture and "^industrialtest_"..config.name.."_back_active.png" or ""), - "industrialtest_machine_block.png"..(config.customFrontTexture and "^industrialtest_"..config.name.."_front_active.png" or "") - } - definition.on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local srcSlot=inv:get_stack("src",1) - local powerStorageSlot=inv:get_stack("powerStorage",1) - local shouldUpdateFormspec=false - local shouldRerunTimer=false - local requiredPower=elapsed*config.opPower + activeOnTimer=function(pos,elapsed,meta,inv) + local srcSlot=inv:get_stack("src",1) + local powerStorageSlot=inv:get_stack("powerStorage",1) + local shouldUpdateFormspec=false + local shouldRerunTimer=false + local requiredPower=elapsed*config.opPower - if powerStorageSlot:get_count()>0 then - local stackMeta=powerStorageSlot:get_meta() - if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then - shouldUpdateFormspec=true - shouldRerunTimer=true - industrialtest.api.updateItemPowerText(powerStorageSlot) - inv:set_stack("powerStorage",1,powerStorageSlot) + if powerStorageSlot:get_count()>0 then + local stackMeta=powerStorageSlot:get_meta() + if industrialtest.api.transferPower(stackMeta,meta,stackMeta:get_int("industrialtest.powerFlow"))>0 then + shouldUpdateFormspec=true + shouldRerunTimer=true + industrialtest.api.updateItemPowerText(powerStorageSlot) + inv:set_stack("powerStorage",1,powerStorageSlot) + end end - end - if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then - local output=craftResultProxy(config.method,srcSlot) - if output.time>0 and inv:room_for_item("dst",output.item) then - meta:set_float("srcTime",0) - meta:set_float("maxSrcTime",output.time*config.efficiency) + if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_int("industrialtest.powerAmount")>=requiredPower then + local output=craftResultProxy(config.method,srcSlot) + if output.time>0 and inv:room_for_item("dst",output.item) then + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",output.time*config.efficiency) + end end - end - if srcSlot:get_count()==0 and meta:get_float("maxSrcTime")>0 then - meta:set_float("srcTime",-1) - meta:set_float("maxSrcTime",0) - shouldUpdateFormspec=true - end - if meta:get_float("maxSrcTime")>0 then - if meta:get_int("industrialtest.powerAmount")>=requiredPower then - meta:set_int("industrialtest.powerAmount",meta:get_int("industrialtest.powerAmount")-requiredPower) - meta:set_float("srcTime",meta:get_float("srcTime")+elapsed) - shouldRerunTimer=true - end - shouldUpdateFormspec=true - end - if meta:get_float("maxSrcTime")<=0 or meta:get_int("industrialtest.powerAmount")=meta:get_float("maxSrcTime") then - local output=craftResultProxy(config.method,srcSlot) - if output.item:get_count()>0 then - inv:add_item("dst",output.item) + if srcSlot:get_count()==0 and meta:get_float("maxSrcTime")>0 then meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) + shouldUpdateFormspec=true end - inv:set_stack("src",1,output.src) + if meta:get_float("maxSrcTime")>0 then + if meta:get_int("industrialtest.powerAmount")>=requiredPower then + meta:set_int("industrialtest.powerAmount",meta:get_int("industrialtest.powerAmount")-requiredPower) + meta:set_float("srcTime",meta:get_float("srcTime")+elapsed) + shouldRerunTimer=true + end + shouldUpdateFormspec=true + end + if meta:get_float("maxSrcTime")<=0 or meta:get_int("industrialtest.powerAmount")=meta:get_float("maxSrcTime") then + local output=craftResultProxy(config.method,srcSlot) + if output.item:get_count()>0 then + inv:add_item("dst",output.item) + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + end + inv:set_stack("src",1,output.src) + end + if not industrialtest.api.isFullyCharged(meta) then + industrialtest.api.triggerNeighbours(pos) + end + return shouldRerunTimer,shouldUpdateFormspec end - if not industrialtest.api.isFullyCharged(meta) then - industrialtest.api.triggerNeighbours(pos) - end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) - end - - return shouldRerunTimer - end - if industrialtest.mclAvailable then - definition.groups.not_in_creative_inventory=1 - definition._doc_items_create_entry=false - end - minetest.register_node("industrialtest:"..config.name.."_active",definition) + }) end local function ironFurnaceFormspec(fuelPercent,srcPercent) diff --git a/utils.lua b/utils.lua index 0ea7cf6..d17a044 100644 --- a/utils.lua +++ b/utils.lua @@ -75,7 +75,7 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) meta:set_string("industrialtest.powerIOConfig",fields.powerIOConfig) local def=minetest.registered_nodes[minetest.get_node(context).name] if def and def._industrialtest_updateFormspec then - def._industrialtest_updateFormspec(meta) + def._industrialtest_updateFormspec(context) end minetest.close_formspec(player:get_player_name(),formname) elseif fields.triggerNeighbours then