fluid things
This commit is contained in:
parent
7694798ad0
commit
ad6af0fa35
21 changed files with 608 additions and 183 deletions
0
computer/13/current_fluids
Normal file
0
computer/13/current_fluids
Normal file
40
computer/13/fluid_state.lua
Normal file
40
computer/13/fluid_state.lua
Normal file
|
@ -0,0 +1,40 @@
|
|||
return {
|
||||
{
|
||||
connected = false,
|
||||
name = "water",
|
||||
amount = 13100,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
name = "blood",
|
||||
amount = 100,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
{
|
||||
connected = false,
|
||||
amount = 0,
|
||||
},
|
||||
}
|
172
computer/13/fluids.lua
Normal file
172
computer/13/fluids.lua
Normal file
|
@ -0,0 +1,172 @@
|
|||
require("pathfinding")
|
||||
pretty = require("cc.pretty")
|
||||
pp = pretty.pretty_print()
|
||||
|
||||
isFluid = {
|
||||
water = true,
|
||||
lava = true,
|
||||
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(2, 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
|
||||
for x = -4, 4 do
|
||||
goTo(vector.new(x, 0, -3))
|
||||
setRedRouter(false)
|
||||
end
|
||||
_G.activeFluidDevice = nil
|
||||
for i = 1, 9 do
|
||||
_G.fluidTanks[i].connected = false
|
||||
end
|
||||
end
|
||||
|
||||
function selectFluidDevice(name)
|
||||
if fluidDevicePos[name] == nil then
|
||||
error("selectFluidDevice(" .. name .. "); not a known fluid device", 2)
|
||||
end
|
||||
|
||||
if _G.activeFluidDevice ~= nil then
|
||||
goTo(fluidDevicePos[_G.activeFluidDevice])
|
||||
_G.activeFluidDevice = nil
|
||||
setRedRouter(false)
|
||||
end
|
||||
goTo(fluidDevicePos[name])
|
||||
setRedRouter(true)
|
||||
_G.activeFluidDevice = name
|
||||
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,
|
||||
connected = false
|
||||
}
|
||||
print("assigned tank", index, "to", fluid)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
connectTanks({ fluid })
|
||||
end
|
||||
|
||||
function connectTanks(fluid_list)
|
||||
print("connecting tanks")
|
||||
pp(fluid_list)
|
||||
for index, tank in pairs(_G.fluidTanks) do
|
||||
should_be_connected = false
|
||||
for _, fluid in pairs(fluid_list) do
|
||||
if tank.name == fluid then
|
||||
should_be_connected = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if should_be_connected ~= tank.connected then
|
||||
print("setting", tank.name, index, "to", should_be_connected)
|
||||
goTo(tankPos[index])
|
||||
setRedRouter(should_be_connected)
|
||||
_G.fluidTanks[index].connected = should_be_connected
|
||||
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
|
||||
-- TODO limit capacity
|
||||
end
|
||||
end
|
||||
saveFluids()
|
||||
end
|
||||
|
||||
_G.fluidTanks = _G.fluidTanks or loadFluidInventory()
|
|
@ -1,4 +1,5 @@
|
|||
pp = require("cc.pretty")
|
||||
pp = require("cc.pretty").pretty_print
|
||||
require("fluids")
|
||||
|
||||
function pFront(fn, ...)
|
||||
return peripheral.call("front", fn, unpack(arg))
|
||||
|
@ -31,22 +32,10 @@ function takeItems(type, count)
|
|||
end
|
||||
|
||||
if slot ~= 1 then
|
||||
local size = pFront("size")
|
||||
local empty_slot = nil
|
||||
for i = 1, size do
|
||||
if pFront("getItemDetail", i) == nil then
|
||||
empty_slot = i
|
||||
break
|
||||
end
|
||||
end
|
||||
if empty_slot == nil then
|
||||
printError("no empty slot in chest, pls fix")
|
||||
exit()
|
||||
end
|
||||
if empty_slot ~= 1 then
|
||||
pFront("pullItems", "front", 1, 64, empty_slot) -- empty first slot
|
||||
end
|
||||
local empty_slot = pFront("size")
|
||||
pFront("pullItems", "front", 1, 64, empty_slot) -- empty first slot
|
||||
pFront("pullItems", "front", slot, 64, 1) -- get target item to first slot
|
||||
pFront("pullItems", "front", empty_slot, 64, slot) -- empty last slot for next time
|
||||
end
|
||||
return turtle.suck(count)
|
||||
end
|
||||
|
@ -93,7 +82,15 @@ function getMissing(needed_items)
|
|||
end
|
||||
end
|
||||
end
|
||||
pp.pretty_print(missing)
|
||||
|
||||
for name, amount in pairs(missing) do
|
||||
if isFluid[name] then
|
||||
missing [name] = amount - getFluidAmount(name)
|
||||
if missing[name] < 1 then
|
||||
missing[name] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
return missing
|
||||
end
|
||||
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
require("inventory")
|
||||
require("pathfinding")
|
||||
require("fluids")
|
||||
sfx = require("sfx")
|
||||
|
||||
press_pos = vector.new(1,0,0)
|
||||
deployer_pos = vector.new(2,0,0)
|
||||
furnace_pos = vector.new(3,0,0)
|
||||
furnace_pos = vector.new(4,2,0)
|
||||
saw_pos = vector.new(3,1,0)
|
||||
saw_out_pos = vector.new(2,0,0)
|
||||
mill_pos = vector.new(4,0,0)
|
||||
mixer_pos = vector.new(-1,0,0)
|
||||
compactor_pos = vector.new(-2,0,0)
|
||||
|
@ -11,11 +15,34 @@ spout_pos = vector.new(-3,0,0)
|
|||
melter_pos = vector.new(-4,1,-3)
|
||||
casting_table_pos = vector.new(-3,0,-3)
|
||||
|
||||
-- fluid_tanks = {
|
||||
-- water = vector.new(0,0,-1),
|
||||
-- lava = vector.new(0,0,-1)
|
||||
-- }
|
||||
|
||||
function spout(extra_items)
|
||||
local fluid = extra_items[1]
|
||||
if not fluid then
|
||||
error("no fluid specified")
|
||||
end
|
||||
goTo(spout_pos, "south")
|
||||
insertDepot(1)
|
||||
selectFluidDevice("spout")
|
||||
connectTanks({fluid.name})
|
||||
pumpToDevices(true)
|
||||
sleep(10)
|
||||
pumpToDevices(false)
|
||||
goTo(spout_pos, "south")
|
||||
takeDepot()
|
||||
fluidInvAdd(fluid.name, -fluid.count)
|
||||
pumpToTanks(true)
|
||||
goTo(spout_pos, "south")
|
||||
while #pFront("tanks")[1].amount > 0 do
|
||||
sleep(0.5)
|
||||
end
|
||||
pumpToTanks(false)
|
||||
end
|
||||
function saw(_)
|
||||
goTo(saw_pos, "south")
|
||||
turtle.drop()
|
||||
goTo(saw_out_pos, "south")
|
||||
takeDepot()
|
||||
end
|
||||
function mill(extra_items)
|
||||
goTo(mill_pos,"south")
|
||||
item_count = 0
|
||||
|
@ -36,13 +63,13 @@ function mill(extra_items)
|
|||
takeForward(i)
|
||||
end
|
||||
end
|
||||
function deploy(extra_items, nbt)
|
||||
function deploy(extra_items)
|
||||
-- extra_items should only be one item
|
||||
goTo(deployer_pos, "south")
|
||||
insertDepot(1)
|
||||
goUp()
|
||||
goUp()
|
||||
selectItem(extra_items[1].name, nbt)
|
||||
selectItem(extra_items[1].name)
|
||||
turtle.dropUp(1)
|
||||
pFront("pullItem", "top")
|
||||
|
||||
|
@ -51,7 +78,18 @@ function deploy(extra_items, nbt)
|
|||
takeDepot()
|
||||
end
|
||||
function deploy_tool(extra_items)
|
||||
deploy(extra_items, true)
|
||||
-- extra_items should only be one item
|
||||
goTo(deployer_pos, "south")
|
||||
insertDepot(1)
|
||||
goUp()
|
||||
goUp()
|
||||
selectItem(extra_items[1].name)
|
||||
turtle.dropUp(1)
|
||||
pFront("pullItem", "top")
|
||||
|
||||
goDown()
|
||||
goDown()
|
||||
takeDepot()
|
||||
goUp()
|
||||
goUp()
|
||||
pFront("pushItem", "top")
|
||||
|
@ -82,9 +120,63 @@ function press(_)
|
|||
end
|
||||
function compact(extra_items)
|
||||
printError("unimplemented :3")
|
||||
fail()
|
||||
sfx.fail()
|
||||
end
|
||||
function mix(extra_items)
|
||||
function alloy(parts, product)
|
||||
for _, item in pairs(parts) do
|
||||
if not isFluid[item.name] then
|
||||
error(item.name .. " is not a known fluid, but was used in alloy")
|
||||
end
|
||||
end
|
||||
selectFluidDevice("mixer")
|
||||
connectTanks(parts)
|
||||
pumpToDevices(true)
|
||||
goTo(mixer_pos, "south")
|
||||
-- TODO
|
||||
--[[
|
||||
wait until at least one input fluid is used up
|
||||
if two remain, it needs to keep track of which tank gets what when draining
|
||||
]]
|
||||
|
||||
-- while true do
|
||||
|
||||
-- end
|
||||
pumpToDevices(false)
|
||||
end
|
||||
function melt(_, product, yield)
|
||||
goTo(melter_pos, "north")
|
||||
goUp()
|
||||
turtle.drop()
|
||||
selectFluidDevice("melter")
|
||||
connectTankOrAssign(product)
|
||||
pumpToTanks(true)
|
||||
goTo(melter_pos, "north")
|
||||
while #pFront("items") > 0 do
|
||||
sleep(1)
|
||||
end
|
||||
pumpToTanks(false)
|
||||
fluidInvAdd(product, yield)
|
||||
end
|
||||
function mix(extra_items, product, yield)
|
||||
-- prepare fluids
|
||||
local fluids = {}
|
||||
for index, item in pairs(extra_items) do
|
||||
if isFluid[item.name] then
|
||||
table.insert(fluids, item)
|
||||
extra_items[index] = nil
|
||||
end
|
||||
end
|
||||
if #fluids > 0 then
|
||||
print("mixing with fluids", fluids[1])
|
||||
selectFluidDevice("mixer")
|
||||
connectTanks({fluids[1].name})
|
||||
pumpToDevices(true)
|
||||
sleep(10)
|
||||
pumpToDevices(false)
|
||||
end
|
||||
-- print("aaa")
|
||||
-- read()
|
||||
-- mix
|
||||
goTo(mixer_pos, "south")
|
||||
insertForward(1, 1)
|
||||
for _, item in pairs(extra_items) do
|
||||
|
@ -98,6 +190,7 @@ function mix(extra_items)
|
|||
sleep(10)
|
||||
-- todo wait until ingredients are gone
|
||||
takeForward(10)
|
||||
-- todo empty fluids
|
||||
end
|
||||
function craft(extra_items)
|
||||
local slot = 0
|
||||
|
@ -124,5 +217,9 @@ return {
|
|||
press = press,
|
||||
compact = compact,
|
||||
mix = mix,
|
||||
craft = craft
|
||||
craft = craft,
|
||||
alloy = alloy,
|
||||
melt = melt,
|
||||
saw = saw,
|
||||
spout = spout,
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
pp = require("cc.pretty").pretty_print
|
||||
|
||||
_G.facing = "south"
|
||||
_G.pos = vector.new(0,0,0)
|
||||
_G.facing = _G.facing or "south"
|
||||
_G.pos = _G.pos or vector.new(0,0,0)
|
||||
|
||||
local up = vector.new(0,1,0)
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ function read_recipe(file)
|
|||
base = nil
|
||||
intermediate = nil
|
||||
repeats = 1
|
||||
yield = 1
|
||||
while 1 do
|
||||
line = file.readLine()
|
||||
if string.sub(line, 1, 5) == "base " then
|
||||
|
@ -16,6 +17,8 @@ function read_recipe(file)
|
|||
intermediate = string.sub(line, 14)
|
||||
elseif string.sub(line, 1, 7) == "repeat " then
|
||||
repeats = tonumber(string.sub(line, 8))
|
||||
elseif string.sub(line, 1, 6) == "yield " then
|
||||
yield = tonumber(string.sub(line, 7))
|
||||
elseif line == "steps:" then
|
||||
break
|
||||
end
|
||||
|
@ -44,6 +47,7 @@ function read_recipe(file)
|
|||
return {
|
||||
product = product,
|
||||
base = base,
|
||||
yield = yield,
|
||||
intermediate = intermediate or base,
|
||||
repeats = repeats,
|
||||
steps = steps
|
||||
|
|
|
@ -177,6 +177,78 @@ steps:
|
|||
deploy rubber
|
||||
deploy copper_sheet
|
||||
|
||||
redrouter_block
|
||||
steps:
|
||||
craft andesite_alloy redstone_torch andesite_alloy redstone_torch monitor_normal redstone_torch andesite_alloy redstone_torch andesite_alloy
|
||||
|
||||
monitor_normal
|
||||
steps:
|
||||
craft andesite_alloy andesite_alloy andesite_alloy andesite_alloy glass_pane andesite_alloy andesite_alloy andesite_alloy andesite_alloy
|
||||
|
||||
torch
|
||||
yield 4
|
||||
steps:
|
||||
craft charcoal nil nil stick
|
||||
|
||||
kelp
|
||||
steps:
|
||||
mix water kelp bone_meal:2
|
||||
|
||||
electron_tube
|
||||
base polished_rose_quartz
|
||||
steps:
|
||||
spout molten_rose_gold:111
|
||||
|
||||
molten_rose_gold
|
||||
steps:
|
||||
alloy molten_copper molten_gold
|
||||
|
||||
molten_copper
|
||||
yield 111
|
||||
base copper_ingot
|
||||
steps:
|
||||
melt
|
||||
|
||||
molten_gold
|
||||
yield 111
|
||||
base gold_ingot
|
||||
steps:
|
||||
melt
|
||||
|
||||
cow_jar
|
||||
base milk_jar
|
||||
steps:
|
||||
deploy beef
|
||||
deploy leather
|
||||
spout blood:100
|
||||
|
||||
beef
|
||||
base seitan
|
||||
intermediate protobeef
|
||||
steps:
|
||||
deploy moss_carpet
|
||||
spout blood:100
|
||||
press
|
||||
saw
|
||||
|
||||
seitan
|
||||
base dough
|
||||
repeat 2
|
||||
steps:
|
||||
spout water:100
|
||||
press
|
||||
saw
|
||||
|
||||
dough
|
||||
steps:
|
||||
mix wheat_flour water:1000
|
||||
|
||||
wheat_flour
|
||||
steps:
|
||||
mill wheat
|
||||
|
||||
blood
|
||||
yield 50
|
||||
base rotten_flesh
|
||||
steps:
|
||||
melt
|
||||
|
|
12
computer/13/test.lua
Normal file
12
computer/13/test.lua
Normal file
|
@ -0,0 +1,12 @@
|
|||
function bar()
|
||||
error("mmm", 4)
|
||||
end
|
||||
|
||||
function foo()
|
||||
bar()
|
||||
end
|
||||
|
||||
while true do
|
||||
foo()
|
||||
-- error("aaaaa")
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
pp = require("cc.pretty")
|
||||
pp = require("cc.pretty").pretty_print
|
||||
require("recipes")
|
||||
sfx = require("sfx")
|
||||
require("pathfinding")
|
||||
|
@ -10,23 +10,25 @@ keep_stocked = {
|
|||
flint = 10
|
||||
}
|
||||
|
||||
resetAllFluidDevices()
|
||||
goHome()
|
||||
|
||||
print("known recipes:\n")
|
||||
for k,_ in pairs(recipes) do
|
||||
for k, _ in pairs(recipes) do
|
||||
write(k)
|
||||
write(", ")
|
||||
end
|
||||
write("\nchoose one: ")
|
||||
function completion(partial)
|
||||
list = {}
|
||||
for k,_ in pairs(recipes) do
|
||||
for k, _ in pairs(recipes) do
|
||||
if string.sub(k, 1, #partial) == partial then
|
||||
table.insert(list, string.sub(k, #partial + 1))
|
||||
end
|
||||
end
|
||||
return list
|
||||
end
|
||||
|
||||
input = splitString(read(nil, nil, completion))
|
||||
|
||||
target_product = input[1]
|
||||
|
@ -52,7 +54,7 @@ function doRecipe(recipe)
|
|||
inited = true
|
||||
selectItem(recipe.base, false)
|
||||
end
|
||||
use_machine[step.machine](step.extra_items)
|
||||
use_machine[step.machine](step.extra_items, recipe.product, recipe.yield)
|
||||
-- read()
|
||||
end
|
||||
end
|
||||
|
@ -66,10 +68,9 @@ function doRecipe(recipe)
|
|||
turtle.select(1)
|
||||
end
|
||||
|
||||
|
||||
todo = {}
|
||||
for i = 1,copies do
|
||||
table.insert(todo, recipes[target_product])
|
||||
work_queue = {}
|
||||
for i = 1, copies do
|
||||
table.insert(work_queue, recipes[target_product])
|
||||
end
|
||||
|
||||
function listUncraftable(ingredients)
|
||||
|
@ -90,10 +91,12 @@ function tryCreating(recipe)
|
|||
ingredients = ingredientsOf(recipe)
|
||||
missing_ingredients = getMissing(ingredients)
|
||||
if len(missing_ingredients) == 0 then
|
||||
todo[#todo] = nil
|
||||
work_queue[#work_queue] = nil
|
||||
turtle.select(1)
|
||||
-- todo exclude deploy_tool too and make it get its own tool
|
||||
if recipe.steps[1].machine ~= "craft" then
|
||||
if recipe.steps[1].machine ~= "craft" and
|
||||
recipe.steps[1].machine ~= "alloy"
|
||||
then
|
||||
for item, count in pairs(ingredients) do
|
||||
takeItems(item, count)
|
||||
end
|
||||
|
@ -104,19 +107,19 @@ function tryCreating(recipe)
|
|||
return missing_ingredients
|
||||
end
|
||||
|
||||
while #todo > 0 do
|
||||
while #work_queue > 0 do
|
||||
-- for item, _count in pairs(getMissing(keep_stocked)) do
|
||||
-- print("creating", item, "to keep stock up")
|
||||
-- tryCreating(recipes[item])
|
||||
-- end
|
||||
current_recipe = todo[#todo]
|
||||
current_recipe = work_queue[#work_queue]
|
||||
-- ingredients = ingredientsOf(current_recipe)
|
||||
pp.pretty_print(ingredients)
|
||||
pp(ingredients)
|
||||
missing_ingredients = tryCreating(current_recipe)
|
||||
if missing_ingredients then
|
||||
wait_for = listUncraftable(missing_ingredients)
|
||||
while len(wait_for) > 0 do
|
||||
for name,count in pairs(wait_for) do
|
||||
for name, count in pairs(wait_for) do
|
||||
print("please supply", count, name, "manually")
|
||||
end
|
||||
print("press return to continue, q to quit")
|
||||
|
@ -130,11 +133,10 @@ while #todo > 0 do
|
|||
for name, count in pairs(missing_ingredients) do
|
||||
if recipes[name] then
|
||||
print("first making", count, name)
|
||||
table.insert(todo, recipes[name])
|
||||
table.insert(work_queue, recipes[name])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
print("done!")
|
||||
sfx.success()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue