Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local facingDirs = {{x=0, z=1}, {x=-1, z=0}, {x=0, z=-1}, {x=1, z=0}}
- local facingIdx = 0
- pos = {x=0, y=0, z=0}
- local getFacing = function()
- return facingDirs[facingIdx + 1]
- end
- local repeatN = function(ct, fn)
- for i=1, ct do
- if not fn() then
- return false
- end
- end
- return true
- end
- local splitToInt = function(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, tonumber(str))
- end
- return t
- end
- getFuelLevel = function()
- return (turtle.getItemCount(1)-1) * 80 + turtle.getFuelLevel()
- end
- forward = function()
- while needFuel() do
- if turtle.getItemCount(1) >= 1 then
- turtle.select(1)
- turtle.refuel(1)
- else
- os.sleep(3)
- print("i need fuel! ", getFuelLevel())
- end
- end
- local b, reason = turtle.forward()
- if not b then
- print("failed,", reason)
- return false
- end
- local facing = getFacing()
- pos.x = pos.x + facing.x
- pos.z = pos.z + facing.z
- print("coords", pos.x, pos.y, pos.z)
- print("fuel", getFuelLevel())
- return true
- end
- up = function()
- if not turtle.up() then
- return false
- end
- pos.y = pos.y + 1
- return true
- end
- down = function()
- if not turtle.down() then
- return false
- end
- pos.y = pos.y - 1
- return true
- end
- forwardDig = function()
- turtle.dig()
- return forward()
- end
- forwardFullDig = function()
- if not forwardDig() then
- return false
- end
- turtle.digUp()
- turtle.digDown()
- return true
- end
- turnLeft = function()
- facingIdx = (facingIdx - 1) % 4
- turtle.turnLeft()
- end
- turnRight = function()
- facingIdx = (facingIdx + 1) % 4
- turtle.turnRight()
- end
- local findDirIdx = function(idx)
- -- created from truth table
- if facingIdx-1 == idx or (facingIdx == 0 and idx == 3) then
- while facingIdx ~= idx do
- turnLeft()
- end
- else
- while facingIdx ~= idx do
- turnRight()
- end
- end
- end
- getFreeInventorySlotsCount = function()
- local total = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- total = total + 1
- end
- end
- return total
- end
- depositInventoryForward = function()
- for i = 2, 16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- goToRelativeCoords = function(x, y, z, o_fn)
- local fn = o_fn == nil and forward or o_fn
- local desiredXDir = pos.x > x and 1 or 3
- local desiredZDir = pos.z > z and 2 or 0
- while pos.x ~= x or pos.y ~= y or pos.z ~= z do
- if pos.x ~= x then
- findDirIdx(desiredXDir)
- while pos.x ~= x and fn() do end
- end
- if pos.z ~= z then
- findDirIdx(desiredZDir)
- while pos.z ~= z and fn() do end
- end
- while pos.y < y and up() do end
- while pos.y > y and down() do end
- end
- end
- needFuel = function() return getFuelLevel() <= (math.abs(pos.x) + math.abs(pos.y) + math.abs(pos.z) + 40) or turtle.getFuelLevel() <= 0 end
- goHomeAndDeposit = function()
- goToRelativeCoords(0, 0, 0)
- findDirIdx(2)
- depositInventoryForward()
- end
- forwardFullDigWithAttention = function()
- if not forwardFullDig() then
- return false
- end
- if getFreeInventorySlotsCount() <= 1 then
- local oldFacingIdx = facingIdx
- local oldPos = {x=pos.x, y=pos.y, z=pos.z}
- goHomeAndDeposit()
- goToRelativeCoords(oldPos.x, oldPos.y, oldPos.z)
- findDirIdx(oldFacingIdx)
- end
- return true
- end
- digRectangleOld = function(x, y, z, w, h)
- goToRelativeCoords(x, y, z, forwardFullDigWithAttention)
- for i=0, math.floor(w/2) do
- local j = i*2
- goToRelativeCoords(x + j, y, z + h, forwardFullDigWithAttention)
- if (j+1 >= w) then
- return
- end
- goToRelativeCoords(x + j+1, y, z + h, forwardFullDigWithAttention)
- goToRelativeCoords(x + j+1, y, z, forwardFullDigWithAttention)
- if (j+2 >= w) then
- return
- end
- goToRelativeCoords(x + j+2, y, z, forwardFullDigWithAttention)
- end
- end
- digRectangle = function(x1, y1, z1, x2, y2, z2)
- goToRelativeCoords(x1, y1, z1)
- for gx = 0, x2-x1 do
- for pz = 0, z2-z1 do
- local gz = pz
- if gx % 2 == 1 then
- gz = z2-z1 - pz
- end
- goToRelativeCoords(x1+gx, 0, z1+gz, forwardFullDigWithAttention)
- end
- end
- end
- buildFunction = function(w, h, f)
- for x = 0, w do
- for py = 0, h do
- local y = py
- if x % 2 == 1 then
- y = h - py
- end
- print(math.floor(f(x, y)))
- goToRelativeCoords(x, math.floor(f(x, y)), y)
- for i = 1, 16 do
- turtle.select(i)
- if turtle.placeUp() then
- break
- end
- end
- end
- end
- end
- findDirIdx(0)
- while true do
- local func = loadstring(io.read())
- setfenv(func, getfenv())
- turtle.refuel(1)
- func()
- goHomeAndDeposit()
- end
Add Comment
Please, Sign In to add comment