Implement Transformers

This commit is contained in:
mrkubax10 2023-03-28 22:09:56 +02:00
parent 03f0440227
commit a21ec8a929
2 changed files with 176 additions and 2 deletions

31
api.lua
View File

@ -27,7 +27,8 @@ industrialtest.api.hvPowerFlow=10200
industrialtest.api.evPowerFlow=40800
industrialtest.api.ivPowerFlow=163800
local function clamp(num,min,max)
industrialtest.internal={}
industrialtest.internal.clamp=function(num,min,max)
return math.max(math.min(num,max),min)
end
@ -43,6 +44,32 @@ industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig)
meta:set_int("industrialtest.powerAmount",0)
meta:set_string("industrialtest.ioConfig",ioConfig)
end
-- \brief Takes rotated node and side and outputs normalized side that can be used for ioConfig lookups
-- \param pos Vector with node position
-- \param side Node side. See industrialtest.api.addPowerStorage for possible values
-- \returns Normalized side or in case of failure side argument back
industrialtest.api.normalizeSide=function(pos,side)
local node=minetest.get_node(pos)
-- FIXME: improve code quality there
local translation={
[0]={
1,2,3,4,5,6
},
[1]={
5,6,3,4,2,1
},
[2]={
1,2,3,4,6,5
},
[3]={
6,5,3,4,1,2
}
}
if node.param2>3 then
return side
end
return translation[node.param2][side]
end
-- \brief Checks if metadata contains power storage
-- \param meta MetaDataRef which should be checked
-- \returns true if metadata contains power storage, false otherwise
@ -129,7 +156,7 @@ industrialtest.api.addPower=function(meta,amount)
local powerAmount=meta:get_int("industrialtest.powerAmount")
local powerCapacity=meta:get_int("industrialtest.powerCapacity")
local prevPowerAmount=powerAmount
powerAmount=clamp(powerAmount+amount,0,powerCapacity)
powerAmount=industrialtest.internal.clamp(powerAmount+amount,0,powerCapacity)
meta:set_int("industrialtest.powerAmount",powerAmount)
return powerAmount-prevPowerAmount
end

View File

@ -1194,3 +1194,150 @@ minetest.register_craft({
{"industrialtest:refined_iron_ingot",industrialtest.elementKeys.dirt,"industrialtest:refined_iron_ingot"}
}
})
-- Transformers
local function registerTransformer(config)
definition={
description=S(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"
},
paramtype2="facedir",
legacy_facedir_simple=true,
drop=(config.requiresWrench and "industrialtest:machine_block" or "industrialtest:"..config.name),
on_construct=function(pos)
local meta=minetest.get_meta(pos)
industrialtest.api.addPowerStorage(meta,config.upperFlow,0,"aaaaaa")
end,
on_timer=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
if industrialtest.api.hasPowerStorage(minetest.get_meta(value)) then
local normalized=industrialtest.api.normalizeSide(pos,key)
if normalized==5 then
upperPowerDistribution=config.upperFlow
else
lowerPowerDistribution=lowerPowerDistribution+1
end
else
neighbourPositions[key]=0
end
end
if lowerPowerDistribution>0 then
lowerPowerDistribution=math.floor(industrialtest.internal.clamp(math.min(config.upperFlow,meta:get_int("industrialtest.powerAmount"))/lowerPowerDistribution,0,config.lowerFlow))
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
return (meta:get_int("industrialtest.powerAmount")>0 and roomAvailable)
end,
_industrialtest_onPowerFlow=function(pos)
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
}
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)
end
registerTransformer({
name="lv_transformer",
displayName="LV Transformer",
machineBlockTexture="industrialtest_wood_machine_block.png",
requiresWrench=false,
lowerFlow=industrialtest.api.lvPowerFlow,
upperFlow=industrialtest.api.mvPowerFlow,
sounds="wood"
})
minetest.register_craft({
type="shaped",
output="industrialtest:lv_transformer",
recipe={
{"group:wood","industrialtest:insulated_tin_cable","group:wood"},
{industrialtest.elementKeys.copperIngot,industrialtest.elementKeys.copperIngot,industrialtest.elementKeys.copperIngot},
{"group:wood","industrialtest:insulated_copper_cable","group:wood"}
}
})
registerTransformer({
name="mv_transformer",
displayName="MV Transformer",
machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true,
lowerFlow=industrialtest.api.mvPowerFlow,
upperFlow=industrialtest.api.hvPowerFlow,
sounds="metal"
})
minetest.register_craft({
type="shaped",
output="industrialtest:mv_transformer",
recipe={
{"industrialtest:insulated_copper_cable"},
{"industrialtest:machine_block"},
{"industrialtest:insulated_gold_cable"}
}
})
registerTransformer({
name="hv_transformer",
displayName="HV Transformer",
machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true,
lowerFlow=industrialtest.api.hvPowerFlow,
upperFlow=industrialtest.api.evPowerFlow,
sounds="metal"
})
-- TODO: Craft for HV transformer
registerTransformer({
name="ev_transformer",
displayName="EV Transformer",
machineBlockTexture="industrialtest_machine_block.png",
requiresWrench=true,
lowerFlow=industrialtest.api.evPowerFlow,
upperFlow=industrialtest.api.ivPowerFlow,
sounds="metal"
})
-- TODO: Craft for EV Transformer