From c56c0a9c5c1d7b0a9433cc403f060d06aa775d24 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Mon, 20 Nov 2023 22:08:32 +0100 Subject: [PATCH] Fix transformer issues --- api.lua | 92 +++++++++++++++--------- machines/common.lua | 7 +- machines/transformer.lua | 152 ++++++++++++++++----------------------- 3 files changed, 128 insertions(+), 123 deletions(-) diff --git a/api.lua b/api.lua index d4f17e9..e33a396 100644 --- a/api.lua +++ b/api.lua @@ -62,7 +62,7 @@ industrialtest.api.normalizeSide=function(pos,side) 5,6,3,4,2,1 }, [2]={ - 1,2,3,4,6,5 + 2,1,3,4,6,5 }, [3]={ 6,5,3,4,1,2 @@ -218,11 +218,12 @@ industrialtest.api.transferPowerFromItem=function(srcItemstack,meta,amount) return actualFlow end --- \brief Transfers power from source node to all neighbouring nodes +-- \brief Transfers power from source node to it's network, if sides is set then power will be only transfered to network connected to that sides -- \param pos Vector with position of source node +-- \param (optional) sides table with Vectors -- \returns two values: true if any neighbouring node has room for more power, false otherwise -- true if any power was transferred, false otherwise -industrialtest.api.powerFlow=function(pos) +industrialtest.api.powerFlow=function(pos,sides) local meta=minetest.get_meta(pos) -- if machine doesn't have network map then it's not capable of transferring power local network=industrialtest.api.getNetwork(meta) @@ -233,7 +234,7 @@ industrialtest.api.powerFlow=function(pos) local endpointCount=0 for _,endpoint in ipairs(network) do local endpointMeta=minetest.get_meta(endpoint.position) - if not industrialtest.api.isFullyCharged(endpointMeta) then + if not industrialtest.api.isFullyCharged(endpointMeta) and (not sides or sides[endpoint.sourceSide]) then endpointCount=endpointCount+1 end end @@ -244,31 +245,32 @@ industrialtest.api.powerFlow=function(pos) local transferred=false local roomAvailable=false for _,endpoint in ipairs(network) do - local endpointMeta=minetest.get_meta(endpoint.position) - local transferredToEndpoint=false - if powerDistribution<=endpoint.flow then - if industrialtest.api.transferPower(meta,endpointMeta,powerDistribution)>0 then - transferred=true - transferredToEndpoint=true - end - local def=minetest.registered_nodes[minetest.get_node(endpoint.position).name] - if def then - local updateFormspec=def._industrialtest_updateFormspec - if updateFormspec then - updateFormspec(endpoint.position) + if not side or sides[endpoint.sourceSide] then + local endpointMeta=minetest.get_meta(endpoint.position) + if powerDistribution<=endpoint.flow then + local transferredPower=industrialtest.api.transferPower(meta,endpointMeta,powerDistribution) + if transferredPower>0 then + transferred=true end - local onPowerFlow=def._industrialtest_onPowerFlow - if onPowerFlow and transferredToEndpoint then - onPowerFlow(endpoint.position,industrialtest.api.getOppositeSide(1)) + local def=minetest.registered_nodes[minetest.get_node(endpoint.position).name] + if def then + local updateFormspec=def._industrialtest_updateFormspec + if updateFormspec then + updateFormspec(endpoint.position) + end + local onPowerFlow=def._industrialtest_onPowerFlow + if onPowerFlow and transferredPower>0 then + onPowerFlow(endpoint.position,industrialtest.api.getOppositeSide(endpoint.side),transferredPower) + end end + minetest.get_node_timer(endpoint.position):start(industrialtest.updateDelay) + if not industrialtest.api.isFullyCharged(endpointMeta) then + roomAvailable=true + end + else + minetest.remove_node(endpoint.position) + industrialtest.internal.explode(endpoint.position,2) end - minetest.get_node_timer(endpoint.position):start(industrialtest.updateDelay) - if not industrialtest.api.isFullyCharged(endpointMeta) then - roomAvailable=true - end - else - minetest.remove_node(endpoint.position) - industrialtest.internal.explode(endpoint.position,2) end end return roomAvailable,transferred @@ -313,18 +315,31 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit) if #connections==0 then return map end + local sides={ + ["-1,0,0"]=1, + ["1,0,0"]=2, + ["0,-1,0"]=3, + ["0,1,0"]=4, + ["0,0,-1"]=5, + ["0,0,1"]=6 + } local serializedSourcePos=pos.x..","..pos.y..","..pos.z local visitedNodes={[serializedSourcePos]=true} for _,conn in ipairs(connections) do if not omit or conn.x~=omit.x or conn.y~=omit.y or conn.z~=omit.z then visitedNodes[conn.x..","..conn.y..","..conn.z]=true addNodeToNetwork(conn,pos) + local sideVector=vector.subtract(conn,pos) + local serializedSideVector=sideVector.x..","..sideVector.y..","..sideVector.z local def=minetest.registered_nodes[minetest.get_node(conn).name] if def.groups._industrialtest_cable then table.insert(workers,{ position=conn, + targetPosition=conn, distance=1, - flow=def._industrialtest_cableFlow + flow=def._industrialtest_cableFlow, + targetFlow=0, + sourceSide=industrialtest.api.normalizeSide(pos,sides[serializedSideVector]) }) if addCables then table.insert(map,{ @@ -337,7 +352,9 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit) table.insert(map,{ position=conn, distance=0, - flow=meta:get_int("industrialtest.powerFlow") + flow=meta:get_int("industrialtest.powerFlow"), + side=sides[serializedSideVector], + sourceSide=industrialtest.api.normalizeSide(pos,sides[serializedSideVector]) }) end end @@ -364,13 +381,16 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit) if directionAssigned then table.insert(workers,{ position=conn, + targetPosition=conn, distance=worker.distance+1, - flow=clampFlow(conn,worker.flow) + flow=clampFlow(conn,worker.flow), + targetFlow=0, + sourceSide=worker.sourceSide }) else - worker.position=conn + worker.targetPosition=conn worker.distance=worker.distance+1 - worker.flow=clampFlow(conn,worker.flow) + worker.targetFlow=clampFlow(conn,worker.flow) directionAssigned=true end if addCables then @@ -380,11 +400,17 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit) }) end else + local sideVector=vector.subtract(conn,worker.position) table.insert(map,{ position=conn, distance=worker.distance, - flow=clampFlow(conn,worker.flow) + flow=clampFlow(conn,worker.flow), + side=sides[sideVector.x..","..sideVector.y..","..sideVector.z], + sourceSide=worker.sourceSide }) + if not sides[sideVector.x..","..sideVector.y..","..sideVector.z] then + minetest.debug(minetest.serialize(worker.position)) + end if #connections==1 then table.remove(workers,i) break @@ -397,6 +423,8 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit) table.remove(workers,i) break end + worker.position=worker.targetPosition + worker.flow=worker.targetFlow end end end diff --git a/machines/common.lua b/machines/common.lua index 99f345b..c502ac0 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -63,7 +63,9 @@ machine.onConstruct=function(pos,config) local inv=meta:get_inventory() industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) - meta:set_string("formspec",machine.getFormspec(pos,config)) + if not config.withoutFormspec then + meta:set_string("formspec",machine.getFormspec(pos,config)) + end if config.groups then if config.groups._industrialtest_hasPowerInput then @@ -193,6 +195,9 @@ machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,confi end machine.updateFormspec=function(pos,config) + if config.withoutFormspec then + return + end local meta=minetest.get_meta(pos) meta:set_string("formspec",machine.getFormspec(pos,config)) end diff --git a/machines/transformer.lua b/machines/transformer.lua index 9034ded..9100e58 100644 --- a/machines/transformer.lua +++ b/machines/transformer.lua @@ -17,105 +17,77 @@ local S=minetest.get_translator("industrialtest") local transformer={} -transformer.onConstruct=function(pos) - local meta=minetest.get_meta(pos) - industrialtest.api.addPowerStorage(meta,config.upperFlow,0,"aaaaaa") +transformer.onPowerFlow=function(pos,side,amount) + local normalized=industrialtest.api.normalizeSide(pos,side) + local def=minetest.registered_nodes[minetest.get_node(pos).name] + if normalized~=5 and amount>=def._industrialtest_lowerFlow then + minetest.remove_node(pos) + industrialtest.internal.explode(pos,2) + end end -transformer.onTimer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local node=minetest.get_node(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) - } - local upperPowerDistribution=0 - local lowerPowerDistribution=0 - - for key,value in ipairs(neighbourPositions) do - local normalized=industrialtest.api.normalizeSide(pos,key) - if industrialtest.api.hasPowerStorage(minetest.get_meta(value)) and industrialtest.api.isPowerOutput(meta,normalized) then - if normalized==5 then - upperPowerDistribution=config.upperFlow - else - lowerPowerDistribution=lowerPowerDistribution+1 - end - else - neighbourPositions[key]=0 - end +transformer.onTimer=function(pos,elapsed,meta) + local powerAmount=meta:get_int("industrialtest.powerAmount") + local def=minetest.registered_nodes[minetest.get_node(pos).name] + local afterFlowLower=false + local afterFlowUpper=false + if powerAmount>=def._industrialtest_lowerFlow then + meta:set_int("industrialtest.powerFlow",def._industrialtest_lowerFlow) + afterFlowLower,_=industrialtest.api.powerFlow(pos,{ + [1]=true, + [2]=true, + [3]=true, + [4]=true, + [6]=true + }) + meta:set_int("industrialtest.powerFlow",industrialtest.api.ivPowerFlow) end - if lowerPowerDistribution>0 then - lowerPowerDistribution=math.floor(industrialtest.internal.clamp(math.min(config.upperFlow,meta:get_int("industrialtest.powerAmount"))/lowerPowerDistribution,0,config.lowerFlow)) + if powerAmount>=def._industrialtest_upperFlow then + meta:set_int("industrialtest.powerFlow",def._industrialtest_upperFlow) + afterFlowUpper,_=industrialtest.api.powerFlow(pos,{[5]=true}) + meta:set_int("industrialtest.powerFlow",industrialtest.api.ivPowerFlow) end - - local roomAvailable=false - for key,value in ipairs(neighbourPositions) do - if value~=0 then - local neighbourMeta=minetest.get_meta(value) - local normalized=industrialtest.api.normalizeSide(pos,key) - if normalized==5 then - if meta:get_int("industrialtest.powerAmount")==config.upperFlow then - industrialtest.api.transferPower(meta,neighbourMeta,config.upperFlow) - end - else - industrialtest.api.transferPower(meta,neighbourMeta,lowerPowerDistribution) - end - if not industrialtest.api.isFullyCharged(neighbourMeta) then - roomAvailable=true - end - end - end - - meta:set_string("industrialtest.ioConfig","aaaaaa") - - return (meta:get_int("industrialtest.powerAmount")>0 and roomAvailable) -end - -transformer.onPowerFlow=function(pos,side) - industrialtest.api.changeIoConfig(minetest.get_meta(pos),industrialtest.api.normalizeSide(pos,side),"i") - minetest.get_node_timer(pos):start(industrialtest.updateDelay) + return powerAmount>0 and (afterFlowLower or afterFlowUpper),false end local function registerTransformer(config) - local definition={ - description=config.displayName, - tiles={ - config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", - config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", - config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", - config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", - config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", - config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png" + industrialtest.internal.registerMachine({ + name=config.name, + displayName=config.displayName, + capacity=industrialtest.api.ivPowerFlow, + flow=industrialtest.api.ivPowerFlow, + ioConfig="aaaaaa", + storageSlots={}, + powerSlots={}, + sounds=config.sounds, + groups={ + _industrialtest_hasPowerOutput=1, + _industrialtest_hasPowerInput=1, }, - paramtype2="facedir", - legacy_facedir_simple=true, - drop=(config.requiresWrench and "industrialtest:machine_block" or "industrialtest:"..config.name), - on_construct=transformer.onConstruct, - on_timer=transformer.onTimer, - _industrialtest_onPowerFlow=transformer.onPowerFlow - } - if industrialtest.mtgAvailable then - definition.groups={cracky=2} - definition.sounds=(config.sounds=="metal" and default.node_sound_metal_defaults() or default.node_sound_wood_defaults()) - elseif industrialtest.mclAvailable then - definition.groups={pickaxey=1} - definition.sounds=(config.sounds=="metal" and mcl_sounds.node_sound_metal_defaults() or mcl_sounds.node_sound_wood_defaults()) - definition._mcl_blast_resistance=3 - definition._mcl_hardness=3.5 - end - definition.groups._industrialtest_hasPowerInput=1 - definition.groups._industrialtest_hasPowerOutput=1 - definition.groups._industrialtest_wrenchUnmountable=1 - minetest.register_node("industrialtest:"..config.name,definition) + customKeys={ + tiles={ + config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", + config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", + config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", + config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", + config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", + config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png" + }, + paramtype2="facedir", + legacy_facedir_simple=true, + _industrialtest_lowerFlow=config.lowerFlow, + _industrialtest_upperFlow=config.upperFlow, + _industrialtest_onPowerFlow=transformer.onPowerFlow + }, + requiresWrench=config.requiresWrench, + withoutFormspec=true, + onTimer=transformer.onTimer + }) end registerTransformer({ name="lv_transformer", - displayName="LV Transformer", + displayName=S("LV Transformer"), machineBlockTexture="industrialtest_wood_machine_block.png", requiresWrench=false, lowerFlow=industrialtest.api.lvPowerFlow, @@ -134,7 +106,7 @@ minetest.register_craft({ registerTransformer({ name="mv_transformer", - displayName="MV Transformer", + displayName=S("MV Transformer"), machineBlockTexture="industrialtest_machine_block.png", requiresWrench=true, lowerFlow=industrialtest.api.mvPowerFlow, @@ -153,7 +125,7 @@ minetest.register_craft({ registerTransformer({ name="hv_transformer", - displayName="HV Transformer", + displayName=S("HV Transformer"), machineBlockTexture="industrialtest_machine_block.png", requiresWrench=true, lowerFlow=industrialtest.api.hvPowerFlow, @@ -172,7 +144,7 @@ minetest.register_craft({ registerTransformer({ name="ev_transformer", - displayName="EV Transformer", + displayName=S("EV Transformer"), machineBlockTexture="industrialtest_machine_block.png", requiresWrench=true, lowerFlow=industrialtest.api.evPowerFlow,