Advertisement
Bane_MC

Smarter Turtles v0.1a

May 20th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.78 KB | None | 0 0
  1. -- THIS SCRIPT IS THE PROPERTY OF Bane83
  2. -- You are hereby granted permission to use this script as you see fit, however you are not granted permission to
  3. -- redistribute, modify, or claim this software as your own.
  4.  
  5. function table.copy(t)
  6.   local t2 = {}
  7.   for k,v in pairs(t) do
  8.     t2[k] = v
  9.   end
  10.   return t2
  11. end
  12.  
  13. turtle.original = table.copy(turtle)
  14.  
  15. turtle.maxRetries = 10
  16. turtle.directions = { up = "move_up", down = "move_down", back = "move_back", forward = "move_forward", left = "move_left", right = "move_right" }
  17.  
  18. turtle.sturtle = {
  19.     _moveSide = function (dir, n, reorientAfterEachMove, reorientWhenDone, afterEachMove, retryOnFail, ...)
  20.         dir = dir or turtle.directions.left
  21.         n = n or 1
  22.         retryOnFail = retryOnFail or (retryOnFail == nil)
  23.         reorientAfterEachMove = reorientAfterEachMove or false
  24.         reorientWhenDone = reorientWhenDone or (reorientWhenDone == nil)
  25.  
  26.         if reorientWhenDone and reorientAfterEachMove then
  27.             reorientWhenDone = false
  28.         end
  29.  
  30.         if (dir == turtle.directions.left) then
  31.             turtle.turnLeft()
  32.         elseif (dir == turtle.directions.right) then
  33.             turtle.turnRight()
  34.         end
  35.  
  36.         local result = {}
  37.         local _result = false
  38.         for i = 1, n do
  39.             _result = turtle.sturtle._retryMoveDelegate(turtle.original.forward, retryOnFail)
  40.  
  41.             if (_result) then
  42.                 if (reorientAfterEachMove) then
  43.                     if (dir == turtle.directions.left) then
  44.                         turtle.turnRight()
  45.                     elseif (dir == turtle.directions.right) then
  46.                         turtle.turnLeft()
  47.                     end
  48.                 end
  49.  
  50.                 if (afterEachMove ~= nil) then
  51.                     afterEachMove(i)
  52.                 end
  53.  
  54.                 if (reorientAfterEachMove) then
  55.                     if (dir == turtle.directions.left) then
  56.                         turtle.turnLeft()
  57.                     elseif (dir == turtle.directions.right) then
  58.                         turtle.turnRight()
  59.                     end
  60.                 end
  61.             end
  62.  
  63.             table.insert(result, _result)
  64.         end
  65.  
  66.         if (reorientWhenDone) then
  67.             if (dir == turtle.directions.left) then
  68.                 turtle.turnRight()
  69.             elseif (dir == turtle.directions.right) then
  70.                 turtle.turnLeft()
  71.             end
  72.         end
  73.  
  74.         return result
  75.     end,
  76.     _dig = function (dir, n, afterEachDig, whenDone, retryOnFail, ...)
  77.         dir = dir or turtle.directions.forward
  78.         n = n or 1
  79.         retryOnFail = retryOnFail or (retryOnFail == nil)
  80.  
  81.         local func = turtle.sturtle._getDigDelegates(dir)
  82.  
  83.         local result = {}
  84.         local _result = false
  85.         for i = 1, n do
  86.             while (func.detect()) do
  87.                 _result = turtle.sturtle._retryDigDelegate(func.dig, retryOnFail)
  88.  
  89.                 sleep(0.50) -- Wait for falling entities
  90.             end
  91.            
  92.             if (_result and afterEachDig ~= nil) then
  93.                 afterEachDig(i)
  94.             end
  95.  
  96.             if (n > 1) then
  97.                 turtle.sturtle._move(dir, 1)
  98.             end
  99.  
  100.             table.insert(result, _result)
  101.         end
  102.  
  103.         if (whenDone ~= nil) then
  104.             whenDone(n)
  105.         end
  106.  
  107.         return result
  108.     end,
  109.     _move = function (dir, n, afterEachMove, retryOnFail, ...)
  110.         dir = dir or turtle.directions.forward
  111.         n = n or 1
  112.         retryOnFail = retryOnFail or (retryOnFail == nil)
  113.  
  114.         local func = turtle.sturtle._getDelegate(dir)
  115.  
  116.         local result = {}
  117.         local _result = false
  118.         for i = 1, n do
  119.             _result = turtle.sturtle._retryMoveDelegate(func, retryOnFail)
  120.  
  121.             if (_result and afterEachMove ~= nil) then
  122.                 afterEachMove(i)
  123.             end
  124.  
  125.             table.insert(result, _result)
  126.         end
  127.  
  128.         return result
  129.     end,
  130.     _retryMoveDelegate = function (func, retryOnFail)
  131.         local attempts = 0
  132.         local message = ""
  133.  
  134.         local _result = func()
  135.         while (_result ~= true and retryOnFail) do
  136.             attempts = attempts + 1
  137.  
  138.             if (turtle.getFuelLevel() == 0) then
  139.                 message = os.getComputerLabel() .. " is out of fuel!" -- TODO: Attempt auto-fueling
  140.                 break
  141.             end
  142.  
  143.             if (attempts == math.floor(turtle.maxRetries / 3)) then
  144.                 sleep(0.5)
  145.             elseif (attempts == turtle.maxRetries) then
  146.                 if (message == "") then
  147.                     message = os.getComputerLabel() .. " is stuck and in need of assistance!"
  148.                 end
  149.  
  150.                 turtle.sturtle._needsAssistance(message)
  151.  
  152.                 attempts = 0 -- Starting a new attempt loop
  153.             end
  154.  
  155.             _result = func()
  156.         end
  157.  
  158.         return _result
  159.     end,
  160.     _retryDigDelegate = function (func, retryOnFail)
  161.         local attempts = 0
  162.         local message = ""
  163.  
  164.         local _result = func()
  165.         while (_result ~= true and retryOnFail) do
  166.             attempts = attempts + 1
  167.  
  168.             if (turtle.getFuelLevel() == 0) then
  169.                 message = os.getComputerLabel() .. " is out of fuel!" -- TODO: Attempt auto-fueling
  170.                 break
  171.             end
  172.  
  173.             if (attempts == math.floor(turtle.maxRetries / 3)) then
  174.                 sleep(0.5)
  175.             elseif (attempts == turtle.maxRetries) then
  176.                 if (message == "") then
  177.                     message = os.getComputerLabel() .. " can't dig and is in need of assistance!"
  178.                 end
  179.  
  180.                 turtle.sturtle._needsAssistance(message)
  181.  
  182.                 attempts = 0 -- Starting a new attempt loop
  183.             end
  184.  
  185.             _result = func()
  186.         end
  187.  
  188.         return _result
  189.     end,
  190.     _needsAssistance = function (message)
  191.         print(message)
  192.  
  193.         local event, char
  194.         char = "n"
  195.  
  196.         while string.lower(char) ~= "y" do
  197.             print("Am I ready to go? (Y/N)")
  198.             event, char = os.pullEvent("char")
  199.         end
  200.     end,
  201.  
  202.     _getDelegate = function (dir)
  203.         local delegates = {
  204.             move_up = turtle.original.up,
  205.             move_down = turtle.original.down,
  206.             move_back = turtle.original.back,
  207.             move_forward = turtle.original.forward
  208.         }
  209.  
  210.         return delegates[dir]
  211.     end,
  212.     _getDigDelegates = function (dir)
  213.         local delegates = {
  214.             move_up = { dig = turtle.original.digUp, detect = turtle.original.detectUp },
  215.             move_down = { dig = turtle.original.digDown, detect = turtle.original.detectDown },
  216.             move_forward = { dig = turtle.original.dig, detect = turtle.original.detect }
  217.         }
  218.  
  219.         return delegates[dir]
  220.     end
  221. }
  222.  
  223. turtle.up = setmetatable({up = turtle.up}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.up, ...) end})
  224. turtle.down = setmetatable({down = turtle.down}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.down, ...) end})
  225. turtle.back = setmetatable({back = turtle.back}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.back, ...) end})
  226. turtle.forward = setmetatable({forward = turtle.forward}, {__call = function(self, ...) return turtle.sturtle._move(turtle.directions.forward, ...) end})
  227.  
  228. turtle.left = setmetatable({left = turtle.left}, {__call = function(self, ...) return turtle.sturtle._moveSide(turtle.directions.left, ...) end})
  229. turtle.right = setmetatable({right = turtle.right}, {__call = function(self, ...) return turtle.sturtle._moveSide(turtle.directions.right, ...) end})
  230.  
  231. turtle.dig = setmetatable({dig = turtle.dig}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.forward, ...) end})
  232. turtle.digUp = setmetatable({digUp = turtle.digUp}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.up, ...) end})
  233. turtle.digDown = setmetatable({digDown = turtle.digDown}, {__call = function(self, ...) return turtle.sturtle._dig(turtle.directions.down, ...) end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement