Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --new changes entirely untested
- local x,y,z = -164,64,193
- local inventory = {}
- local facing = 1
- local selected = 1
- --comment this out to not print anything
- local function debugprint(msg)
- print(msg)
- end
- --[[
- USEFUL FUNCTIONS SECTION
- --]]
- local function turnTo(dir)
- if dir ~= facing then
- debugprint("turning from "..facing.." to "..dir)
- if math.abs(facing-dir) == 2 then
- turtle.turnRight()
- turtle.turnRight()
- elseif facing == dir-1 or (facing == 3 and dir == 0) then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- end
- facing = dir
- end
- local function decend()
- local dug = false
- if turtle.detectDown() then
- if not turtle.digDown() then
- debugprint("was not able to dig down")
- return false, false, false
- end
- dug = true
- end
- if not turtle.down() then
- debugprint("error moving down")
- return false, dug, turtle.attackDown()
- end
- y = y - 1
- return true, dug, false
- end
- local function accend()
- local dug = false
- if turtle.detectUp() then
- if not turtle.digUp() then
- debugprint("was not able to dig up")
- return false, false, false
- end
- dug = true
- end
- if not turtle.up() then
- debugprint("was not able to move up")
- return false, dug, turtle.attackUp()
- end
- y = y + 1
- return true, dug, false
- end
- local function move(dir)
- turnTo(dir)
- local dug = false
- if turtle.detect() then
- if not turtle.dig() then
- debugprint("was not able to dig")
- return false, false, false
- end
- dug = true
- end
- if not turtle.forward() then
- debugprint("error moving forward")
- return false, dug, turtle.attack()
- end
- if dir == 0 then z = z + 1
- elseif dir == 1 then x = x - 1
- elseif dir == 2 then z = z - 1
- elseif dir == 3 then x = x + 1
- end
- return true, dug, false
- end
- local function detectInventoryChange()
- for i=1,16 do
- turtle.select(i)
- local amount = turtle.getItemCount(i)
- if amount ~= 0 and inventory[i].amount == 0 then
- debugprint("found previously unknown item in slot "..i)
- for j=1,16 do
- if i~=j then
- if turtle.compareTo(j) then
- debugprint("unknown item is "..inventory[j].name.." from slot "..j)
- inventory[i] = {["name"] = inventory[j].name, ["amount"] = amount}
- return inventory[i].name, i, amount
- end
- end
- end
- inventory[i] = {["name"] = "unknown", ["amount"] = amount}
- return inventory[i].name, i, amount
- elseif amount == 0 and inventory[i].amount == 0 then
- --both slots empty, nothing changed
- elseif amount > inventory[i].amount then
- debugprint("found "..(amount - inventory[i].amount).." "..inventory[i].name.." in slot "..i)
- inventory[i].amount = amount
- return inventory[i].name, i, (amount - inventory[i].amount)
- elseif amounts < inventory[i].amount then
- debugprint("WARNING: "..inventory[i].name.." in slot "..i.." decreased by "..(inventory[i].amount - inventoryBackup[i].amount))
- inventory[i].amount = amount
- end
- end
- end
- local function tryMove(moveFunction, ...)
- local continue = true
- while continue do
- local moved, dug, attacked = moveFunction(...)
- if moved then return true, dug, attacked end
- if dug then detectInventoryChange() end
- elseif attacked then detectInventoryChange() end
- continue = dug or attacked
- end
- print("FATAL ERROR: cannot move")
- error("cannot move, aborting")
- return false, false, false
- end
- local function checkFuel()
- local fuel = turtle.getFuelLevel()
- if fuel == "unlimited" then return true end
- if fuel < 10 then
- debugprint("Low on fuel, refueling")
- turtle.select(1)
- if not turtle.refuel(10) then
- debugprint("ERROR: coult not refuel")
- if fuel == 0 then
- print("FATAL ERROR: cannot refuel")
- error("FATAL ERROR: cannot refuel")
- else
- debugprint("ERROR: cannot refuel")
- end
- return false
- end
- return true
- end
- return false
- end
- --[[
- PROGRAMS START HERE
- --]]
- local function stairs()
- local goingUp = false
- local yAdj = 0
- while true do
- if not checkFuel() then return end
- tryMove(move, facing)
- if goingUp then
- if yAdj == 0 and not turtle.detectDown() then
- turtle.select(2)
- turtle.placeDown()
- end
- tryMove(accend)
- if y == 64+yAdj then
- if yAdj == 3 then
- break
- else
- yAdj = yAdj + 1
- tryMove(accend)
- tryMove(move, (facing+3)%4)
- turnTo((facing+3)%4)
- goingUp = false
- end
- end
- else
- tryMove(decend)
- if yAdj == 0 and not turtle.detectDown() then
- turtle.select(2)
- turtle.placeDown()
- end
- if y == 10+yAdj then
- goingUp = true
- tryMove(move, (facing+3)%4)
- turnTo((facing+3)%4)
- tryMove(accend)
- end
- end
- end
- end
- function test()
- for i=1,10 do
- tryMove(move, facing)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment