jacob614

turtle

Mar 21st, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --new changes entirely untested
  2. local x,y,z = -164,64,193
  3. local inventory = {}
  4. local facing = 1
  5. local selected = 1
  6.  
  7. --comment this out to not print anything
  8. local function debugprint(msg)
  9.   print(msg)
  10. end
  11.  
  12. --[[
  13.  
  14. USEFUL FUNCTIONS SECTION
  15.  
  16. --]]
  17.  
  18. local function turnTo(dir)
  19.   if dir ~= facing then
  20.     debugprint("turning from "..facing.." to "..dir)
  21.     if math.abs(facing-dir) == 2 then
  22.       turtle.turnRight()
  23.       turtle.turnRight()
  24.     elseif facing == dir-1 or (facing == 3 and dir == 0) then
  25.       turtle.turnRight()
  26.     else
  27.       turtle.turnLeft()
  28.     end
  29.   end
  30.   facing = dir  
  31. end
  32.  
  33. local function decend()
  34.   local dug = false
  35.   if turtle.detectDown() then
  36.     if not turtle.digDown() then
  37.       debugprint("was not able to dig down")
  38.       return false, false, false
  39.     end
  40.     dug = true
  41.   end
  42.   if not turtle.down() then
  43.     debugprint("error moving down")
  44.     return false, dug, turtle.attackDown()
  45.   end
  46.   y = y - 1
  47.   return true, dug, false
  48. end
  49.  
  50. local function accend()
  51.   local dug = false
  52.   if turtle.detectUp() then
  53.     if not turtle.digUp() then
  54.       debugprint("was not able to dig up")
  55.       return false, false, false
  56.     end
  57.     dug = true
  58.   end
  59.   if not turtle.up() then
  60.     debugprint("was not able to move up")
  61.     return false, dug, turtle.attackUp()
  62.   end
  63.   y = y + 1
  64.   return true, dug, false
  65. end
  66.  
  67. local function move(dir)
  68.   turnTo(dir)
  69.   local dug = false
  70.   if turtle.detect() then
  71.     if not turtle.dig() then
  72.       debugprint("was not able to dig")
  73.       return false, false, false
  74.     end
  75.     dug = true
  76.   end
  77.   if not turtle.forward() then
  78.     debugprint("error moving forward")
  79.     return false, dug, turtle.attack()
  80.   end
  81.   if dir == 0 then z = z + 1
  82.   elseif dir == 1 then x = x - 1
  83.   elseif dir == 2 then z = z - 1
  84.   elseif dir == 3 then x = x + 1
  85.   end
  86.   return true, dug, false
  87. end
  88.  
  89. local function detectInventoryChange()
  90.   for i=1,16 do
  91.     turtle.select(i)
  92.     local amount = turtle.getItemCount(i)
  93.     if amount ~= 0 and inventory[i].amount == 0 then
  94.       debugprint("found previously unknown item in slot "..i)
  95.       for j=1,16 do
  96.         if i~=j then
  97.           if turtle.compareTo(j) then
  98.             debugprint("unknown item is "..inventory[j].name.." from slot "..j)
  99.             inventory[i] = {["name"] = inventory[j].name, ["amount"] = amount}
  100.             return inventory[i].name, i, amount
  101.           end
  102.         end
  103.       end
  104.       inventory[i] = {["name"] = "unknown", ["amount"] = amount}
  105.       return inventory[i].name, i, amount
  106.     elseif amount == 0 and inventory[i].amount == 0 then
  107.       --both slots empty, nothing changed
  108.     elseif amount > inventory[i].amount then
  109.       debugprint("found "..(amount - inventory[i].amount).." "..inventory[i].name.." in slot "..i)
  110.       inventory[i].amount = amount
  111.       return inventory[i].name, i, (amount - inventory[i].amount)
  112.     elseif amounts < inventory[i].amount then
  113.       debugprint("WARNING: "..inventory[i].name.." in slot "..i.." decreased by "..(inventory[i].amount - inventoryBackup[i].amount))
  114.       inventory[i].amount = amount
  115.     end
  116.   end
  117. end
  118.  
  119. local function tryMove(moveFunction, ...)
  120.   local continue = true
  121.   while continue do
  122.     local moved, dug, attacked = moveFunction(...)
  123.     if moved then return true, dug, attacked end
  124.     if dug then detectInventoryChange() end
  125.     elseif attacked then detectInventoryChange() end
  126.     continue = dug or attacked
  127.   end
  128.   print("FATAL ERROR: cannot move")
  129.   error("cannot move, aborting")
  130.   return false, false, false
  131. end
  132.  
  133. local function checkFuel()
  134.   local fuel = turtle.getFuelLevel()
  135.   if fuel == "unlimited" then return true end
  136.   if fuel < 10 then
  137.     debugprint("Low on fuel, refueling")
  138.     turtle.select(1)
  139.     if not turtle.refuel(10) then
  140.       debugprint("ERROR: coult not refuel")
  141.       if fuel == 0 then
  142.         print("FATAL ERROR: cannot refuel")
  143.         error("FATAL ERROR: cannot refuel")
  144.       else
  145.         debugprint("ERROR: cannot refuel")
  146.       end
  147.       return false
  148.     end
  149.     return true
  150.   end
  151.   return false
  152. end
  153.  
  154. --[[
  155.  
  156. PROGRAMS START HERE
  157.  
  158. --]]
  159.  
  160. local function stairs()
  161.   local goingUp = false
  162.   local yAdj = 0
  163.   while true do
  164.     if not checkFuel() then return end
  165.     tryMove(move, facing)
  166.     if goingUp then
  167.       if yAdj == 0 and not turtle.detectDown() then
  168.         turtle.select(2)
  169.         turtle.placeDown()
  170.       end
  171.       tryMove(accend)
  172.       if y == 64+yAdj then
  173.         if yAdj == 3 then
  174.           break
  175.        else
  176.           yAdj = yAdj + 1
  177.           tryMove(accend)
  178.           tryMove(move, (facing+3)%4)
  179.           turnTo((facing+3)%4)
  180.           goingUp = false
  181.         end
  182.       end
  183.     else
  184.       tryMove(decend)
  185.       if yAdj == 0 and not turtle.detectDown() then
  186.         turtle.select(2)
  187.         turtle.placeDown()
  188.       end
  189.       if y == 10+yAdj then
  190.         goingUp = true
  191.         tryMove(move, (facing+3)%4)
  192.         turnTo((facing+3)%4)
  193.         tryMove(accend)
  194.       end
  195.     end
  196.   end
  197. end
  198.  
  199. function test()
  200.   for i=1,10 do
  201.     tryMove(move, facing)
  202.   end
  203. end
Advertisement
Add Comment
Please, Sign In to add comment