trainworld_computercraft/computer/13/fluids.lua
2025-05-28 02:37:55 +02:00

164 lines
3.5 KiB
Lua

require("pathfinding")
pretty = require("cc.pretty")
pp = pretty.pretty_print
isFluid = {
water = true,
lava = true,
still_milk = true,
blood = true,
molten_rose_gold = true,
molten_copper = true,
molten_gold = true,
molten_bronze = true,
molten_tin = true,
molten_zinc = true,
compound_mixture = true,
}
fluidDevicePos = {
trash = vector.new(1, 0, -1),
water_source = vector.new(0, 0, -1),
mixer = vector.new(-1, 0, -1),
compactor = vector.new(-2, 0, -1),
spout = vector.new(-3, 0, -1),
melter = vector.new(-4, 0, -1),
}
tankPos = {
vector.new(4, 0, -3),
vector.new(3, 0, -3),
vector.new(2, 0, -3),
vector.new(1, 0, -3),
vector.new(0, 0, -3),
vector.new(-1, 0, -3),
vector.new(-2, 0, -3),
vector.new(-3, 0, -3),
vector.new(-4, 0, -3),
}
function setRedRouter(state)
peripheral.call("bottom", "setOutput", "bottom", state)
peripheral.call("bottom", "setOutput", "front", state)
end
function loadFluidInventory()
return require("fluid_state")
end
function saveFluids()
fs.delete("fluid_state.lua")
repeat
file = fs.open("fluid_state.lua", "w")
until file
file.write("return ")
file.write(textutils.serialise(_G.fluidTanks))
file.close()
end
function getFluidAmount(type)
for index, tank in pairs(_G.fluidTanks) do
if tank.name == type then
return tank.amount
end
end
return 0
end
function pumpToDevices(enable)
goTo(vector.new(4, 0, -1))
-- clutches invert the signal
setRedRouter(not enable)
end
function pumpToTanks(enable)
goTo(vector.new(3, 0, -1))
-- clutches invert the signal
setRedRouter(not enable)
end
function resetAllFluidDevices()
pumpToDevices(false)
pumpToTanks(false)
-- for name, pos in pairs(fluidDevicePos) do
-- goTo(pos)
-- setRedRouter(false)
-- end
-- for x = 2, -4, -1 do
-- goTo(vector.new(x, 0, -1))
-- setRedRouter(false)
-- end
end
function selectFluidDevice(name)
if fluidDevicePos[name] == nil then
error("selectFluidDevice(" .. name .. "); not a known fluid device", 2)
end
if _G.activeFluidDevice ~= name then
print("set active fluid device", name)
_G.activeFluidDevice = name
goTo(fluidDevicePos[name])
redstone.setOutput("bottom", true)
sleep(0.1)
redstone.setOutput("bottom", false)
end
end
function connectTankOrAssign(fluid)
local has_fluid = false
for index, tank in pairs(_G.fluidTanks) do
if tank.name == fluid then
has_fluid = true
print(fluid, "already assigned to", index)
end
end
if not has_fluid then
print(fluid, "not in tanks, assigning new")
for index = 1, 9 do
if _G.fluidTanks[index].name == nil or _G.fluidTanks[index].amount == 0 then
_G.fluidTanks[index] = {
name = fluid,
amount = 0,
}
print("assigned tank", index, "to", fluid)
break
end
end
end
connectTank(fluid)
end
function connectTank(name)
for index, tank in pairs(_G.fluidTanks) do
if tank.name == name then
if index == _G.connectTank then
print("tank", name, index, "already connected")
break
end
goTo(tankPos[index])
redstone.setOutput("bottom", true)
sleep(0.1)
redstone.setOutput("bottom", false)
_G.connectedTank = index
print("tank", name, index, "already connected")
break
end
end
end
function fluidInvAdd(name, amount)
print("added", amount, "to", name)
for index, tank in pairs(_G.fluidTanks) do
if tank.name == name then
tank.amount = tank.amount + amount
if tank.amount < 1 then
tank.amount = 0
end
-- TODO limit capacity
end
end
saveFluids()
end
_G.fluidTanks = _G.fluidTanks or loadFluidInventory()