Advertisement
Wyvern67

Turtle Extended

Jan 7th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. if type(old) == "table" then
  2.     return false
  3. else
  4.     old = {}
  5.     old.turtle = {}
  6.     for i,v in pairs(turtle) do
  7.         old.turtle[i] = v
  8.     end
  9. end
  10.  
  11. local function refuel()
  12.     shell.run("refuel", "all")
  13.     if turtle.getFuelLevel() == 0 then
  14.         return false
  15.     else
  16.         return true
  17.     end
  18. end
  19.  
  20. local function doSide(f, direction, ...)
  21.     local args = {...}
  22.     if not old.turtle[f] then
  23.         return false, "No such function as turtle." .. tostring(f)
  24.     end
  25.  
  26.     if direction == "top" or direction == "up" then
  27.         return old.turtle[f .. "Up"](unpack(args))
  28.     elseif direction == "bottom" or direction == "down" then
  29.         return old.turtle[f .. "Down"](unpack(args))
  30.     elseif direction == nil or direction == "front" or direction == "forward" then
  31.         return old.turtle[f](unpack(args))
  32.     elseif direction == "right" then
  33.         turtle.turnRight()
  34.         local a = {old.turtle[f](unpack(args))}
  35.         turtle.turnLeft()
  36.         return unpack(a)
  37.     elseif direction == "left" then
  38.         turtle.turnLeft()
  39.         local a = {old.turtle[f](unpack(args))}
  40.         turtle.turnRight()
  41.         return unpack(a)
  42.     elseif direction == "back" then
  43.         turtle.turnLeft()
  44.         turtle.turnLeft()
  45.         local a = {old.turtle[f](unpack(args))}
  46.         turtle.turnLeft()
  47.         turtle.turnLeft()
  48.         return unpack(a)
  49.     else
  50.         return false, "Direction unrecognized"
  51.     end
  52. end
  53.  
  54. local function drop(n, side)
  55.     if type(n) == "string" then
  56.         side = n
  57.         n = 1
  58.     end
  59.     return doSide("drop", side, n)
  60. end
  61. turtle.drop = drop
  62.  
  63. local function suck(n, side)
  64.     if type(n) == "string" then
  65.         side = n
  66.         n = 1
  67.     end
  68.     return doSide("suck", side, n)
  69. end
  70. turtle.suck = suck
  71.  
  72. local function inspect(side)
  73.     return doSide("inspect", side)
  74. end
  75. turtle.inspect = inspect
  76.  
  77. local function dig(side)
  78.     return doSide("dig", side)
  79. end
  80. turtle.dig = dig
  81.  
  82. local function detect(side)
  83.     return doSide("detect", side)
  84. end
  85. turtle.detect = detect
  86.  
  87. local function place(side)
  88.     return doSide("place", side)
  89. end
  90. turtle.place = place
  91.  
  92. local function attack(side)
  93.     return doSide("attack", side)
  94. end
  95. turtle.attack = attack
  96.  
  97. local function forward()
  98.     if turtle.getFuelLevel() > 0 then
  99.         _, block = turtle.inspect("forward")
  100.         if type(block) == "table" then
  101.             if block.name == "minecraft:gravel" or block.name == "minecraft:sand" then
  102.                 while turtle.detect("forward") do
  103.                     turtle.dig("forward")
  104.                     sleep(0.5)
  105.                 end
  106.             else
  107.                 while turtle.detect("forward") do turtle.dig("forward") end
  108.             end
  109.         end
  110.        
  111.         local walked = old.turtle.forward("forward")
  112.         if not walked then
  113.             print("ERROR : Unable to move forward")
  114.             if turtle.detect("forward") then
  115.                 print("Block detected")
  116.                 while not forward() do
  117.                     sleep(0.5)
  118.                 end
  119.             else
  120.                 print("ERROR : There must be a mob.")
  121.                 while true do
  122.                     turtle.attack("forward")
  123.                     walked = old.turtle.forward()
  124.                     if walked == true then break end
  125.                 end
  126.                 print("SUCCESS : Mob gone")
  127.             end
  128.         end
  129.     else
  130.         print("Lacking energy. Attempting to refuel.")
  131.         if refuel() == false then
  132.             print("ERROR : No more fuel.")
  133.             shell.exit()
  134.         end
  135.         return false
  136.     end
  137.     return true
  138. end
  139. turtle.forward = forward
  140.  
  141. local function go(direction)
  142.     if direction == "top" or direction == "up" then
  143.         return turtle.up()
  144.     elseif direction == "bottom" or direction == "down" then
  145.         return turtle.down()
  146.     elseif direction == "front" or direction == "forward" then
  147.         return turtle.forward()
  148.     elseif direction == "right" then
  149.         turtle.turnRight()
  150.         return turtle.forward()
  151.     elseif direction == "left" then
  152.         turtle.turnLeft()
  153.         return turtle.forward()
  154.     elseif direction == "back" then
  155.         return turtle.back()
  156.     else
  157.         return turtle.forward(), "Direction unrecognized"
  158.     end
  159. end
  160. turtle.go = go
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement