Fix transformer issues

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

View File

@@ -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

View File

@@ -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,