diff --git a/machines.lua b/machines.lua index e798943..d86e676 100644 --- a/machines.lua +++ b/machines.lua @@ -56,13 +56,13 @@ local function generatorFormspec(fuelPercent,charged) end return table.concat(formspec,"") end -local function generatorMclAfterDigNode(pos,oldnode,oldmeta,digger) +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) local meta2=meta meta:from_table(oldmeta) local inv=meta:get_inventory() - for _,listname in ipairs({"src", "dst", "fuel"}) do + for _,listname in ipairs(lists) do local stack=inv:get_stack(listname,1) if not stack:is_empty() then local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5} @@ -165,7 +165,9 @@ if industrialtest.mtgAvailable then 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=generatorMclAfterDigNode + definition.after_dig_node=function(pos,oldnode,oldmeta) + mclAfterDigNode(pos,oldmeta,{"charged","fuel"}) + end definition._mcl_blast_resistance=3.5 definition._mcl_hardness=3.9 end @@ -197,3 +199,224 @@ minetest.register_craft({ {"","",industrialtest.elementKeys.furnace} } }) + +-- Item processing machines +local function ironFurnaceFormspec(fuelPercent,srcPercent) + local formspec + if industrialtest.mtgAvailable then + formspec={ + "formspec_version[4]", + "size[10.8,12]", + "label[0.5,0.5;"..S("Iron Furnace").."]", + "list[context;src;3.4,1.8;1,1]", + "listring[context;src]", + (fuelPercent>0 and "image[3.4,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" + or "image[3.4,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;fuel;3.4,3.9;1,1]", + "listring[context;fuel]", + (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.4,2.8;1,1]", + "listring[context;dst]", + "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("Iron Furnace").."]", + "list[context;src;3.4,1.8;1,1]", + mcl_formspec.get_itemslot_bg(3.4,1.8,1,1), + "listring[context;src]", + (fuelPercent>0 and "image[3.4,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..fuelPercent..":default_furnace_fire_fg.png]" + or "image[3.4,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;fuel;3.4,3.9;1,1]", + mcl_formspec.get_itemslot_bg(3.4,3.9,1,1), + "listring[context;fuel]", + (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.4,2.8;1,1]", + mcl_formspec.get_itemslot_bg(6.4,2.8,1,1), + "listring[context;dst]", + "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 +definition={ + description=S("Iron Furnace"), + tiles={ + "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, + on_construct=function(pos,placer) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + inv:set_size("src",1) + inv:set_size("dst",1) + inv:set_size("fuel",1) + meta:set_string("formspec",ironFurnaceFormspec(0,0)) + meta:set_float("fuelTime",0) + meta:set_float("maxFuelTime",1) + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + end, + on_timer=function(pos,elapsed) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + local fuelSlot=inv:get_stack("fuel",1) + local dstSlot=inv:get_stack("dst",1) + local shouldUpdateFormspec=false + local shouldRerunTimer=false + + if fuelSlot:get_count()>0 and meta:get_float("fuelTime")<=0 then + local output,after=minetest.get_craft_result({ + method="cooking", + width=1, + items={srcSlot} + }) + if output.time>0 and inv:room_for_item("dst",output.item) then + output,after=minetest.get_craft_result({ + method="fuel", + width=1, + items={fuelSlot} + }) + if output.time>0 then + meta:set_float("fuelTime",output.time) + meta:set_float("maxFuelTime",output.time) + inv:set_stack("fuel",1,after.items[1]) + end + end + end + if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_float("fuelTime")>0 then + local output,after=minetest.get_craft_result({ + method="cooking", + width=1, + items={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*0.7) + end + end + if meta:get_float("fuelTime")>0 then + meta:set_float("fuelTime",meta:get_float("fuelTime")-elapsed) + shouldUpdateFormspec=true + shouldRerunTimer=true + end + if meta:get_float("maxSrcTime")>0 then + if meta:get_float("fuelTime")>0 then + meta:set_float("srcTime",meta:get_float("srcTime")+elapsed) + else + meta:set_float("srcTime",0) + end + shouldUpdateFormspec=true + shouldRerunTimer=true + end + if meta:get_float("srcTime")>=meta:get_float("maxSrcTime") then + local output,after=minetest.get_craft_result({ + method="cooking", + width=1, + items={srcSlot} + }) + if output.item:get_count()>0 then + inv:set_stack("src",1,after.items[1]) + inv:add_item("dst",output.item) + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + end + end + + if shouldUpdateFormspec then + meta:set_string("formspec",ironFurnaceFormspec(meta:get_float("fuelTime")/meta:get_float("maxFuelTime")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + end + + return shouldRerunTimer + end, + allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count,player) + if toList=="dst" then + return 0 + end + return count + end, + allow_metadata_inventory_put=function(pos,listname,index,stack) + if listname=="dst" then + return 0 + end + if listname=="src" then + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + if srcSlot:get_name()~=stack:get_name() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + end + end + return stack:get_count() + end, + on_metadata_inventory_move=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) + if fromList=="src" and count==srcSlot:get_count() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + if meta:get_float("maxFuelTime")>0 then + meta:set_string("formspec",ironFurnaceFormspec(meta:get_float("fuelTime")/meta:get_float("maxFuelTime")*100,0)) + end + end + end, + on_metadata_inventory_put=function(pos,listname,index,stack) + minetest.get_node_timer(pos):start(1.0) + end, + on_metadata_inventory_take=function(pos,listname,index,stack) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local srcSlot=inv:get_stack("src",1) + if listname=="src" and stack:get_count()==srcSlot:get_count() then + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + if meta:get_float("maxFuelTime")>0 then + meta:set_string("formspec",ironFurnaceFormspec(meta:get_float("fuelTime")/meta:get_float("maxFuelTime")*100,0)) + end + end + end +} +if industrialtest.mtgAvailable then + definition.groups={ + cracky=1, + level=2 + } + 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("fuel")[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","fuel","dst"}) + end + definition._mcl_blast_resistance=3 + definition._mcl_hardness=3.5 +end +minetest.register_node("industrialtest:iron_furnace",definition) +minetest.register_craft({ + type="shaped", + output="industrialtest:iron_furnace", + recipe={ + {industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot}, + {industrialtest.elementKeys.ironIngot,"",industrialtest.elementKeys.ironIngot}, + {industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot} + } +})