Fix transformer issues

This commit is contained in:
mrkubax10 2023-11-20 22:08:32 +01:00
parent 8f4356db2a
commit c56c0a9c5c
3 changed files with 128 additions and 123 deletions

92
api.lua
View File

@ -62,7 +62,7 @@ industrialtest.api.normalizeSide=function(pos,side)
5,6,3,4,2,1 5,6,3,4,2,1
}, },
[2]={ [2]={
1,2,3,4,6,5 2,1,3,4,6,5
}, },
[3]={ [3]={
6,5,3,4,1,2 6,5,3,4,1,2
@ -218,11 +218,12 @@ industrialtest.api.transferPowerFromItem=function(srcItemstack,meta,amount)
return actualFlow return actualFlow
end 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 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 -- \returns two values: true if any neighbouring node has room for more power, false otherwise
-- true if any power was transferred, 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) local meta=minetest.get_meta(pos)
-- if machine doesn't have network map then it's not capable of transferring power -- if machine doesn't have network map then it's not capable of transferring power
local network=industrialtest.api.getNetwork(meta) local network=industrialtest.api.getNetwork(meta)
@ -233,7 +234,7 @@ industrialtest.api.powerFlow=function(pos)
local endpointCount=0 local endpointCount=0
for _,endpoint in ipairs(network) do for _,endpoint in ipairs(network) do
local endpointMeta=minetest.get_meta(endpoint.position) 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 endpointCount=endpointCount+1
end end
end end
@ -244,31 +245,32 @@ industrialtest.api.powerFlow=function(pos)
local transferred=false local transferred=false
local roomAvailable=false local roomAvailable=false
for _,endpoint in ipairs(network) do for _,endpoint in ipairs(network) do
local endpointMeta=minetest.get_meta(endpoint.position) if not side or sides[endpoint.sourceSide] then
local transferredToEndpoint=false local endpointMeta=minetest.get_meta(endpoint.position)
if powerDistribution<=endpoint.flow then if powerDistribution<=endpoint.flow then
if industrialtest.api.transferPower(meta,endpointMeta,powerDistribution)>0 then local transferredPower=industrialtest.api.transferPower(meta,endpointMeta,powerDistribution)
transferred=true if transferredPower>0 then
transferredToEndpoint=true transferred=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)
end end
local onPowerFlow=def._industrialtest_onPowerFlow local def=minetest.registered_nodes[minetest.get_node(endpoint.position).name]
if onPowerFlow and transferredToEndpoint then if def then
onPowerFlow(endpoint.position,industrialtest.api.getOppositeSide(1)) 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 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
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
end end
return roomAvailable,transferred return roomAvailable,transferred
@ -313,18 +315,31 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit)
if #connections==0 then if #connections==0 then
return map return map
end 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 serializedSourcePos=pos.x..","..pos.y..","..pos.z
local visitedNodes={[serializedSourcePos]=true} local visitedNodes={[serializedSourcePos]=true}
for _,conn in ipairs(connections) do 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 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 visitedNodes[conn.x..","..conn.y..","..conn.z]=true
addNodeToNetwork(conn,pos) 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] local def=minetest.registered_nodes[minetest.get_node(conn).name]
if def.groups._industrialtest_cable then if def.groups._industrialtest_cable then
table.insert(workers,{ table.insert(workers,{
position=conn, position=conn,
targetPosition=conn,
distance=1, distance=1,
flow=def._industrialtest_cableFlow flow=def._industrialtest_cableFlow,
targetFlow=0,
sourceSide=industrialtest.api.normalizeSide(pos,sides[serializedSideVector])
}) })
if addCables then if addCables then
table.insert(map,{ table.insert(map,{
@ -337,7 +352,9 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit)
table.insert(map,{ table.insert(map,{
position=conn, position=conn,
distance=0, 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
end end
@ -364,13 +381,16 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit)
if directionAssigned then if directionAssigned then
table.insert(workers,{ table.insert(workers,{
position=conn, position=conn,
targetPosition=conn,
distance=worker.distance+1, distance=worker.distance+1,
flow=clampFlow(conn,worker.flow) flow=clampFlow(conn,worker.flow),
targetFlow=0,
sourceSide=worker.sourceSide
}) })
else else
worker.position=conn worker.targetPosition=conn
worker.distance=worker.distance+1 worker.distance=worker.distance+1
worker.flow=clampFlow(conn,worker.flow) worker.targetFlow=clampFlow(conn,worker.flow)
directionAssigned=true directionAssigned=true
end end
if addCables then if addCables then
@ -380,11 +400,17 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit)
}) })
end end
else else
local sideVector=vector.subtract(conn,worker.position)
table.insert(map,{ table.insert(map,{
position=conn, position=conn,
distance=worker.distance, 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 if #connections==1 then
table.remove(workers,i) table.remove(workers,i)
break break
@ -397,6 +423,8 @@ industrialtest.api.createNetworkMap=function(pos,addCables,omit)
table.remove(workers,i) table.remove(workers,i)
break break
end end
worker.position=worker.targetPosition
worker.flow=worker.targetFlow
end end
end end
end end

View File

@ -63,7 +63,9 @@ machine.onConstruct=function(pos,config)
local inv=meta:get_inventory() local inv=meta:get_inventory()
industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) 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 then
if config.groups._industrialtest_hasPowerInput then if config.groups._industrialtest_hasPowerInput then
@ -193,6 +195,9 @@ machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,confi
end end
machine.updateFormspec=function(pos,config) machine.updateFormspec=function(pos,config)
if config.withoutFormspec then
return
end
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
meta:set_string("formspec",machine.getFormspec(pos,config)) meta:set_string("formspec",machine.getFormspec(pos,config))
end end

View File

@ -17,105 +17,77 @@
local S=minetest.get_translator("industrialtest") local S=minetest.get_translator("industrialtest")
local transformer={} local transformer={}
transformer.onConstruct=function(pos) transformer.onPowerFlow=function(pos,side,amount)
local meta=minetest.get_meta(pos) local normalized=industrialtest.api.normalizeSide(pos,side)
industrialtest.api.addPowerStorage(meta,config.upperFlow,0,"aaaaaa") 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 end
transformer.onTimer=function(pos,elapsed) transformer.onTimer=function(pos,elapsed,meta)
local meta=minetest.get_meta(pos) local powerAmount=meta:get_int("industrialtest.powerAmount")
local node=minetest.get_node(pos) local def=minetest.registered_nodes[minetest.get_node(pos).name]
local neighbourPositions={ local afterFlowLower=false
vector.offset(pos,-1,0,0), local afterFlowUpper=false
vector.offset(pos,1,0,0), if powerAmount>=def._industrialtest_lowerFlow then
vector.offset(pos,0,-1,0), meta:set_int("industrialtest.powerFlow",def._industrialtest_lowerFlow)
vector.offset(pos,0,1,0), afterFlowLower,_=industrialtest.api.powerFlow(pos,{
vector.offset(pos,0,0,-1), [1]=true,
vector.offset(pos,0,0,1) [2]=true,
} [3]=true,
local upperPowerDistribution=0 [4]=true,
local lowerPowerDistribution=0 [6]=true
})
for key,value in ipairs(neighbourPositions) do meta:set_int("industrialtest.powerFlow",industrialtest.api.ivPowerFlow)
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
end end
if lowerPowerDistribution>0 then if powerAmount>=def._industrialtest_upperFlow then
lowerPowerDistribution=math.floor(industrialtest.internal.clamp(math.min(config.upperFlow,meta:get_int("industrialtest.powerAmount"))/lowerPowerDistribution,0,config.lowerFlow)) meta:set_int("industrialtest.powerFlow",def._industrialtest_upperFlow)
afterFlowUpper,_=industrialtest.api.powerFlow(pos,{[5]=true})
meta:set_int("industrialtest.powerFlow",industrialtest.api.ivPowerFlow)
end end
return powerAmount>0 and (afterFlowLower or afterFlowUpper),false
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)
end end
local function registerTransformer(config) local function registerTransformer(config)
local definition={ industrialtest.internal.registerMachine({
description=config.displayName, name=config.name,
tiles={ displayName=config.displayName,
config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", capacity=industrialtest.api.ivPowerFlow,
config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", flow=industrialtest.api.ivPowerFlow,
config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", ioConfig="aaaaaa",
config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", storageSlots={},
config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png", powerSlots={},
config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png" sounds=config.sounds,
groups={
_industrialtest_hasPowerOutput=1,
_industrialtest_hasPowerInput=1,
}, },
paramtype2="facedir", customKeys={
legacy_facedir_simple=true, tiles={
drop=(config.requiresWrench and "industrialtest:machine_block" or "industrialtest:"..config.name), config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png",
on_construct=transformer.onConstruct, config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png",
on_timer=transformer.onTimer, config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png",
_industrialtest_onPowerFlow=transformer.onPowerFlow config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png",
} config.machineBlockTexture.."^industrialtest_"..config.name.."_side.png",
if industrialtest.mtgAvailable then config.machineBlockTexture.."^industrialtest_"..config.name.."_front.png"
definition.groups={cracky=2} },
definition.sounds=(config.sounds=="metal" and default.node_sound_metal_defaults() or default.node_sound_wood_defaults()) paramtype2="facedir",
elseif industrialtest.mclAvailable then legacy_facedir_simple=true,
definition.groups={pickaxey=1} _industrialtest_lowerFlow=config.lowerFlow,
definition.sounds=(config.sounds=="metal" and mcl_sounds.node_sound_metal_defaults() or mcl_sounds.node_sound_wood_defaults()) _industrialtest_upperFlow=config.upperFlow,
definition._mcl_blast_resistance=3 _industrialtest_onPowerFlow=transformer.onPowerFlow
definition._mcl_hardness=3.5 },
end requiresWrench=config.requiresWrench,
definition.groups._industrialtest_hasPowerInput=1 withoutFormspec=true,
definition.groups._industrialtest_hasPowerOutput=1 onTimer=transformer.onTimer
definition.groups._industrialtest_wrenchUnmountable=1 })
minetest.register_node("industrialtest:"..config.name,definition)
end end
registerTransformer({ registerTransformer({
name="lv_transformer", name="lv_transformer",
displayName="LV Transformer", displayName=S("LV Transformer"),
machineBlockTexture="industrialtest_wood_machine_block.png", machineBlockTexture="industrialtest_wood_machine_block.png",
requiresWrench=false, requiresWrench=false,
lowerFlow=industrialtest.api.lvPowerFlow, lowerFlow=industrialtest.api.lvPowerFlow,
@ -134,7 +106,7 @@ minetest.register_craft({
registerTransformer({ registerTransformer({
name="mv_transformer", name="mv_transformer",
displayName="MV Transformer", displayName=S("MV Transformer"),
machineBlockTexture="industrialtest_machine_block.png", machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true, requiresWrench=true,
lowerFlow=industrialtest.api.mvPowerFlow, lowerFlow=industrialtest.api.mvPowerFlow,
@ -153,7 +125,7 @@ minetest.register_craft({
registerTransformer({ registerTransformer({
name="hv_transformer", name="hv_transformer",
displayName="HV Transformer", displayName=S("HV Transformer"),
machineBlockTexture="industrialtest_machine_block.png", machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true, requiresWrench=true,
lowerFlow=industrialtest.api.hvPowerFlow, lowerFlow=industrialtest.api.hvPowerFlow,
@ -172,7 +144,7 @@ minetest.register_craft({
registerTransformer({ registerTransformer({
name="ev_transformer", name="ev_transformer",
displayName="EV Transformer", displayName=S("EV Transformer"),
machineBlockTexture="industrialtest_machine_block.png", machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true, requiresWrench=true,
lowerFlow=industrialtest.api.evPowerFlow, lowerFlow=industrialtest.api.evPowerFlow,