diff --git a/api.lua b/api.lua index 74a9e3b..9726355 100644 --- a/api.lua +++ b/api.lua @@ -33,15 +33,20 @@ industrialtest.api.hasPowerStorage=function(meta) end return true end +industrialtest.api.updateItemPowerText=function(itemstack) + local meta=itemstack:get_meta() + local def=minetest.registered_tools[itemstack:get_name()] + meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity"))) + itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534) +end industrialtest.api.preparePowerStorageItem=function(itemstack) local meta=itemstack:get_meta() local def=minetest.registered_tools[itemstack:get_name()] if industrialtest.api.hasPowerStorage(meta) or not def or not def._industrialtest_powerStorage or not def._industrialtest_powerCapacity or not def._industrialtest_powerFlow then return false end - itemstack:set_wear(65535) industrialtest.api.addPowerStorage(meta,def._industrialtest_powerCapacity,def._industrialtest_powerFlow,"n/a") - meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity"))) + industrialtest.api.updateItemPowerText(itemstack) return true end industrialtest.api.isFullyCharged=function(meta) @@ -60,13 +65,11 @@ industrialtest.api.addPower=function(meta,amount) end industrialtest.api.addPowerToItem=function(itemstack,amount) local meta=itemstack:get_meta() - local def=minetest.registered_tools[itemstack:get_name()] if not industrialtest.api.hasPowerStorage(meta) then return 0 end local added=industrialtest.api.addPower(meta,amount) - itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534) - meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity"))) + industrialtest.api.updateItemPowerText(itemstack) return added end industrialtest.api.transferPower=function(srcMeta,destMeta,amount) @@ -90,7 +93,7 @@ end industrialtest.api.powerFlow=function(pos) local meta=minetest.get_meta(pos) if not industrialtest.api.hasPowerStorage(meta) then - return + return false end local neighbourPositions={ vector.offset(pos,-1,0,0), @@ -106,20 +109,51 @@ industrialtest.api.powerFlow=function(pos) end local neighboursContainingPower=0 for key,value in ipairs(neighbours) do - if industrialtest.api.hasPowerStorage(value) and value:get_string("industrialtest.ioConfig")[key%2==0 and key+1 or key-1]=="i" then + local strIndex=(key%2==0 and key-1 or key+1) + if industrialtest.api.hasPowerStorage(value) and string.sub(value:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="i" then neighboursContainingPower=neighboursContainingPower+1 else - neighbourPositions[key]=nil - neighbours[key]=nil + table.remove(neighbourPositions,key) + table.remove(neighbours,key) end end if neighboursContainingPower==0 then - return + return false end local powerFlow=meta:get_int("industrialtest.powerFlow") local powerDistribution=math.floor(powerFlow/neighboursContainingPower) -- TODO: if supplying machine power flow is too large for receiving machine to handle then that machine should explode - for _,value in ipairs(neighbours) do - industrialtest.api.transferPower(meta,value,powerDistribution) + local roomAvailable=false + local transferred=false + for key,value in ipairs(neighbours) do + if industrialtest.api.transferPower(meta,value,powerDistribution)>0 then + transferred=true + end + local updateFormspec=minetest.registered_nodes[minetest.get_node(neighbourPositions[key]).name]._industrialtest_updateFormspec + if updateFormspec then + updateFormspec(value) + end + minetest.get_node_timer(neighbourPositions[key]):start(industrialtest.updateDelay) + if not industrialtest.api.isFullyCharged(value) then + roomAvailable=true + end + end + return roomAvailable,transferred +end +industrialtest.api.triggerNeighbours=function(pos) + local neighbourPositions={ + vector.offset(pos,-1,0,0), + vector.offset(pos,1,0,0), + vector.offset(pos,0,-1,0), + vector.offset(pos,0,1,0), + vector.offset(pos,0,0,-1), + vector.offset(pos,0,0,1) + } + for key,value in ipairs(neighbourPositions) do + local meta=minetest.get_meta(value) + local strIndex=(key%2==0 and key-1 or key+1) + if industrialtest.api.hasPowerStorage(meta) and string.sub(meta:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="o" then + minetest.get_node_timer(value):start(industrialtest.updateDelay) + end end end diff --git a/init.lua b/init.lua index d4f8cba..3ae942c 100644 --- a/init.lua +++ b/init.lua @@ -20,6 +20,9 @@ local modpath=minetest.get_modpath(MODNAME) -- table with global functions, variables etc industrialtest={} +-- Initial constants +industrialtest.updateDelay=1 -- Note: Make this value smaller to make machines update more frequently (it may make server more laggy) + -- load other lua files dofile(modpath.."/compatibility.lua") dofile(modpath.."/api.lua") diff --git a/machines.lua b/machines.lua index d86e676..0a4865e 100644 --- a/machines.lua +++ b/machines.lua @@ -93,6 +93,7 @@ local definition={ meta:set_int("fuelTime",0) meta:set_int("maxFuelTime",1) industrialtest.api.addPowerStorage(meta,7000,100,"oooooo") + minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, on_timer=function(pos,elapsed) local meta=minetest.get_meta(pos) @@ -100,8 +101,9 @@ local definition={ local inv=meta:get_inventory() local chargedSlot=inv:get_stack("charged",1) local fuelSlot=inv:get_stack("fuel",1) - local shouldUpdateFormspec=false - local shouldRerunTimer=false + 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) @@ -150,10 +152,13 @@ local definition={ return stack:get_count() end, on_metadata_inventory_put=function(pos,listname,index,stack,player) - minetest.get_node_timer(pos):start(1.0) + 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(1.0) + 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 @@ -270,13 +275,13 @@ definition={ meta:set_float("maxFuelTime",1) meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) + 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 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 @@ -353,8 +358,7 @@ definition={ allow_metadata_inventory_put=function(pos,listname,index,stack) if listname=="dst" then return 0 - end - if listname=="src" then + elseif listname=="src" then local meta=minetest.get_meta(pos) local inv=meta:get_inventory() local srcSlot=inv:get_stack("src",1) @@ -378,7 +382,7 @@ definition={ end end, on_metadata_inventory_put=function(pos,listname,index,stack) - minetest.get_node_timer(pos):start(1.0) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) end, on_metadata_inventory_take=function(pos,listname,index,stack) local meta=minetest.get_meta(pos) @@ -420,3 +424,246 @@ minetest.register_craft({ {industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot,industrialtest.elementKeys.ironIngot} } }) + +local function electricFurnaceFormspec(powerPercent,srcPercent) + local formspec + if industrialtest.mtgAvailable then + formspec={ + "formspec_version[4]", + "size[10.8,12]", + "label[0.5,0.5;"..S("Electric Furnace").."]", + "list[context;src;3.4,1.8;1,1]", + "listring[context;src]", + (powerPercent>0 and "image[3.4,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..powerPercent..":default_furnace_fire_fg.png]" + or "image[3.4,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;powerStorage;3.4,3.9;1,1]", + "listring[context;powerStorage]", + (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[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]" + } + elseif industrialtest.mclAvailable then + formspec={ + "size[10.04,12]", + "label[0.25,0.25;"..S("Electric Furnace").."]", + "list[context;src;3.4,1.8;1,1]", + mcl_formspec.get_itemslot_bg(3.4,1.8,1,1), + "listring[context;src]", + (powerPercent>0 and "image[3.4,2.8;1,1;default_furnace_fire_bg.png^[lowpart:"..powerPercent..":default_furnace_fire_fg.png]" + or "image[3.4,2.8;1,1;default_furnace_fire_bg.png]"), + "list[context;powerStorage;3.4,3.9;1,1]", + mcl_formspec.get_itemslot_bg(3.4,3.9,1,1), + "listring[context;powerStorage]", + (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[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) + } + end + return table.concat(formspec,"") +end +definition={ + description=S("Electric Furnace"), + tiles={ + "industrialtest_machine_block.png", + "industrialtest_machine_block.png", + "industrialtest_machine_block.png", + "industrialtest_machine_block.png", + "industrialtest_machine_block.png", + "industrialtest_electric_furnace_front.png", + "industrialtest_machine_block.png" + }, + paramtype2="facedir", + legacy_facedir_simple=true, + on_construct=function(pos) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + inv:set_size("src",1) + inv:set_size("dst",1) + inv:set_size("powerStorage",1) + inv:set_size("upgrades",4) + meta:set_string("formspec",electricFurnaceFormspec(0,0)) + meta:set_float("srcTime",-1) + meta:set_float("maxSrcTime",0) + industrialtest.api.addPowerStorage(meta,416,390,"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() + local srcSlot=inv:get_stack("src",1) + local powerStorageSlot=inv:get_stack("powerStorage",1) + local shouldUpdateFormspec=false + local shouldRerunTimer=false + local requiredPower=elapsed*60 + + 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 + if srcSlot:get_count()>0 and meta:get_float("maxSrcTime")<=0 and meta:get_int("industrialtest.powerAmount")>=requiredPower 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.5) + end + 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 + else + meta:set_float("srcTime",0) + meta:set_float("maxSrcTime",-1) + end + shouldUpdateFormspec=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 not industrialtest.api.isFullyCharged(meta) then + industrialtest.api.triggerNeighbours(pos) + end + + if shouldUpdateFormspec then + meta:set_string("formspec",electricFurnaceFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + end + + return shouldRerunTimer + end, + allow_metadata_inventory_move=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) + if listname=="dst" then + return 0 + elseif 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 + 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) + 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_int("industrialtest.powerAmount")>0 then + meta:set_string("formspec",electricFurnaceFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + end + 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) + 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_int("industrialtest.powerAmount")>0 then + meta:set_string("formspec",electricFurnaceFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + end + end + end, + _industrialtest_hasPowerInput=true, + _industrialtest_updateFormspec=function(meta) + meta:set_string("formspec",electricFurnaceFormspec(meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*100,meta:get_float("srcTime")/meta:get_float("maxSrcTime")*100)) + 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("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._mcl_blast_resistance=3 + definition._mcl_hardness=3.5 +end +minetest.register_node("industrialtest:electric_furnace",definition) +minetest.register_craft({ + type="shaped", + output="industrialtest:electric_furnace", + recipe={ + {"","industrialtest:electronic_circuit",""}, + {industrialtest.elementKeys.powerCarrier,"industrialtest:iron_furnace",industrialtest.elementKeys.powerCarrier}, + {"","",""} + } +}) +minetest.register_craft({ + type="shaped", + output="industrialtest:electric_furnace", + recipe={ + {"","",""}, + {"","industrialtest:electronic_circuit",""}, + {industrialtest.elementKeys.powerCarrier,"industrialtest:iron_furnace",industrialtest.elementKeys.powerCarrier} + } +}) diff --git a/nodes.lua b/nodes.lua index 8da0b9a..05296a2 100644 --- a/nodes.lua +++ b/nodes.lua @@ -38,3 +38,25 @@ minetest.register_craft({ {"industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot"} } }) + +-- Node callbacks +minetest.register_on_placenode(function(pos,newNode) + local def=minetest.registered_nodes[newNode.name] + if def and def._industrialtest_hasPowerInput then + local neighbourPositions={ + vector.offset(pos,-1,0,0), + vector.offset(pos,1,0,0), + vector.offset(pos,0,-1,0), + vector.offset(pos,0,1,0), + vector.offset(pos,0,0,-1), + vector.offset(pos,0,0,1) + } + for key,value in ipairs(neighbourPositions) do + local meta=minetest.get_meta(value) + local strIndex=(key%2==0 and key-1 or key+1) + if industrialtest.api.hasPowerStorage(meta) and string.sub(meta:get_string("industrialtest.ioConfig"),strIndex,strIndex)=="o" then + minetest.get_node_timer(value):start(1.0) + end + end + end +end)