trainworld_computercraft/computer/4/build.lua
2025-05-25 03:02:48 +02:00

83 lines
2.1 KiB
Lua

sf = require("structure")
local pf = require("pathfinding")
local im = require("inventorymanager")
local origin = vector.new(-15,235,120)
function build(chunk)
local x,y,z = stringtovec(chunk)
store = {height = 0}
grocerylist = {}
for h = 1,8 do
store[h] = {width = 0}
for w = 1,8 do
store[h][w] = {depth = 0}
for d = 1,8 do
bool, data = sf.blockat(x+w,z+d,y+h)
if bool then
store[h][w][d] = data
store[h][w].depth = d
grocerylist[store[h][w][d]] =
(grocerylist[store[h][w][d]] or 0)+1
end
end
if store[h][w].depth>0 then
store[h].width = w
end
end
if store[h].width>0 then
store.height = h
end
end
if store.height==0 then
--print("cell at",x,y,z,"is done")
return true
end
flag = false
count = im.countinventory()
for k,v in pairs(grocerylist) do
if (count[k] or -1) < v then
print("not enough "..k.." need "..v.." of it")
flag = true
end
end
if turtle.getFuelLevel() < 2000 then
print("too lazy for that")
flag = true
end
if flag then
pf.to(vector.new(pf.home.x,position.y+8,pf.home.z))
pf.returnHome()
return false
end
pf.to(origin + vector.new(x+1,y+8,z+1))
for w = 1,8 do
for h = 1,8 do
for d = 1,8 do
if store[h][w][d] then
pf.to(origin+vector.new(x+w,y+h,z+d))
im.select(store[h][w][d])
turtle.placeDown()
end
end
end
end
return true
end
function stringtovec(str)
parts = {}
for part in string.gmatch(str, "([^,]+)") do
table.insert(parts, tonumber(part))
end
return unpack(parts)
end
repeat
rednet.send(1,nil,"getnexttobuild")
_,chunk = rednet.receive("nexttobuild")
until not build(chunk)
rednet.send(1,chunk,"failedtobuild")
pf.returnHome()