Advertisement
npexception

CC BetterTurtle API

Mar 1st, 2013 (edited)
26,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.77 KB | None | 0 0
  1. local version = 1.8
  2.  
  3. -- >TODO<
  4. --[[
  5.   * add turtle.moveTo(x,y,[z]) function
  6. ]]--
  7.  
  8. --  +------------------------+  --
  9. --  |-->  INITIALIZATION  <--|  --
  10. --  +------------------------+  --
  11. if not turtle then
  12.   print("Error: This can only be used by a turtle!")
  13.   return
  14. end
  15.  
  16. -- UPDATE HANDLING --
  17. if _UD and _UD.su(version, "6XL8EYXe", {...}) then return end
  18.  
  19. local turtle = turtle
  20.  
  21. -- add checkvariable to turtle
  22. -- use the variable to check if the turtle can execute the additional functions
  23. turtle.isBetterAPI = true
  24.  
  25.  
  26. ---- LOCATION DATA ----
  27.  
  28. local x = 0
  29. local y = 0
  30. local z = 0
  31. -- facing vector
  32. local fx = 0    -- west = -1, east = 1
  33. local fz = -1   -- north = -1, south = 1
  34.  
  35. local facingVectors = {
  36.   north = { 0,-1},
  37.   south = { 0, 1},
  38.   west  = {-1, 0},
  39.   east  = { 1, 0}
  40. }
  41.  
  42. function turtle.location()
  43.   return x,y,z
  44. end
  45.  
  46. function turtle.setLocation(_x,_y,_z)
  47.   x = _x
  48.   y = _y
  49.   z = _z
  50. end
  51.  
  52. function turtle.facing()
  53.   if fx == 0 then
  54.     return fz == -1 and "north" or "south"
  55.   end
  56.   return fx == -1 and "west" or "east"
  57. end
  58.  
  59. function turtle.setFacing(facing)
  60.   local vector = facingVectors[facing]
  61.   if not vector then
  62.     error("Unknown facing: "..tostring(facing).." !")
  63.   end
  64.   fx,fz = unpack(vector)
  65. end
  66.  
  67.  
  68. ----  UPDATED MOVEMENT ----
  69.  
  70. local forward = turtle.forward
  71. local back = turtle.back
  72. local up = turtle.up
  73. local down = turtle.down
  74. local turnLeft = turtle.turnLeft
  75. local turnRight = turtle.turnRight
  76.  
  77. function turtle.forward()
  78.   if not forward() then
  79.     return false
  80.   end
  81.   x = x + fx
  82.   z = z + fz
  83.   return true
  84. end
  85.  
  86. function turtle.back()
  87.   if not back() then
  88.     return false
  89.   end
  90.   x = x - fx
  91.   z = z - fz
  92.   return true
  93. end
  94.  
  95. function turtle.up()
  96.   if not up() then
  97.     return false
  98.   end
  99.   y = y + 1
  100.   return true
  101. end
  102.  
  103. function turtle.down()
  104.   if not down() then
  105.     return false
  106.   end
  107.   y = y - 1
  108.   return true
  109. end
  110.  
  111. function turtle.turnLeft()
  112.   turnLeft()
  113.   local oldFx = fx
  114.   fx = fz
  115.   fz = -oldFx
  116.   return true
  117. end
  118.  
  119. function turtle.turnRight()
  120.   turnRight()
  121.   local oldFx = fx
  122.   fx = -fz
  123.   fz = oldFx
  124.   return true
  125. end
  126.  
  127. function turtle.turnAround()
  128.   turtle.turnLeft()
  129.   turtle.turnLeft()
  130.   return true
  131. end
  132.  
  133.  
  134. ---- FORCE MOVEMENTS ----
  135.  
  136. function turtle.forceForward()
  137.   while not turtle.forward() do
  138.     turtle.dig()
  139.     turtle.attack()
  140.   end
  141. end
  142.  
  143. function turtle.forceBack()
  144.   if not turtle.back() then
  145.     turtle.turnAround()
  146.     turtle.forceForward()
  147.     turtle.turnAround()
  148.   end
  149. end
  150.  
  151. function turtle.forceUp()
  152.   while not turtle.up() do
  153.     turtle.digUp()
  154.     turtle.attackUp()
  155.   end
  156. end
  157.  
  158. function turtle.forceDown()
  159.   while not turtle.down() do
  160.     turtle.digDown()
  161.     turtle.attackDown()
  162.   end
  163. end
  164.  
  165. function turtle.forceLeft()
  166.   turtle.turnLeft()
  167.   turtle.forceForward()
  168.   turtle.turnRight()
  169. end
  170.  
  171. function turtle.forceRight()
  172.   turtle.turnRight()
  173.   turtle.forceForward()
  174.   turtle.turnLeft()
  175. end
  176.  
  177.  
  178. ---- ADDITIONAL MOVES ----
  179.  
  180. function turtle.left()
  181.   turtle.turnLeft()
  182.   local result = turtle.forward()
  183.   turtle.turnRight()
  184.   return result
  185. end
  186.  
  187. function turtle.right()
  188.   turtle.turnRight()
  189.   local result = turtle.forward()
  190.   turtle.turnLeft()
  191.   return result
  192. end
  193.  
  194.  
  195. ---- ADDITIONAL DIGS ----
  196.  
  197. function turtle.digLeft()
  198.   turtle.turnLeft()
  199.   local result = turtle.dig()
  200.   turtle.turnRight()
  201.   return result
  202. end
  203.  
  204. function turtle.digRight()
  205.   turtle.turnRight()
  206.   local result = turtle.dig()
  207.   turtle.turnLeft()
  208.   return result
  209. end
  210.  
  211. function turtle.digBack()
  212.   turtle.turnAround()
  213.   local result = turtle.dig()
  214.   turtle.turnAround()
  215.   return result
  216. end
  217.  
  218. function turtle.digDirection(face)
  219.   if face == "top" then
  220.     return turtle.digUp()
  221.   elseif face == "bottom" then
  222.     return turtle.digDown()
  223.   elseif face == "left" then
  224.     return turtle.digLeft()
  225.   elseif face == "right" then
  226.     return turtle.digRight()
  227.   elseif face == "back" then
  228.     return turtle.digBack()
  229.   elseif face == "front" then
  230.     return turtle.dig()
  231.   else
  232.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  233.   end
  234. end
  235.  
  236.  
  237. ---- ADDITIONAL DETECTS ----
  238.  
  239. function turtle.detectLeft()
  240.   turtle.turnLeft()
  241.   local result = turtle.detect()
  242.   turtle.turnRight()
  243.   return result
  244. end
  245.  
  246. function turtle.detectRight()
  247.   turtle.turnRight()
  248.   local result = turtle.detect()
  249.   turtle.turnLeft()
  250.   return result
  251. end
  252.  
  253. function turtle.detectBack()
  254.   turtle.turnAround()
  255.   local result = turtle.detect()
  256.   turtle.turnAround()
  257.   return result
  258. end
  259.  
  260. function turtle.detectDirection(face)
  261.   if face == "top" then
  262.     return turtle.detectUp()
  263.   elseif face == "bottom" then
  264.     return turtle.detectDown()
  265.   elseif face == "left" then
  266.     return turtle.detectLeft()
  267.   elseif face == "right" then
  268.     return turtle.detectRight()
  269.   elseif face == "back" then
  270.     return turtle.detectBack()
  271.   elseif face == "front" then
  272.     return turtle.detect()
  273.   else
  274.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  275.   end
  276. end
  277.  
  278.  
  279. ---- ADDITIONAL COMPARES ----
  280.  
  281. function turtle.compareLeft()
  282.   turtle.turnLeft()
  283.   local result = turtle.compare()
  284.   turtle.turnRight()
  285.   return result
  286. end
  287.  
  288. function turtle.compareRight()
  289.   turtle.turnRight()
  290.   local result = turtle.compare()
  291.   turtle.turnLeft()
  292.   return result
  293. end
  294.  
  295. function turtle.compareBack()
  296.   turtle.turnAround()
  297.   local result = turtle.compare()
  298.   turtle.turnAround()
  299.   return result
  300. end
  301.  
  302. function turtle.compareDirection(face)
  303.   if face == "top" then
  304.     return turtle.compareUp()
  305.   elseif face == "bottom" then
  306.     return turtle.compareDown()
  307.   elseif face == "left" then
  308.     return turtle.compareLeft()
  309.   elseif face == "right" then
  310.     return turtle.compareRight()
  311.   elseif face == "back" then
  312.     return turtle.compareBack()
  313.   elseif face == "front" then
  314.     return turtle.compare()
  315.   else
  316.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  317.   end
  318. end
  319.  
  320.  
  321. ---- ADDITIONAL INSPECTS ----
  322.  
  323. if turtle.inspect then
  324.  
  325.   function turtle.inspectLeft()
  326.     turtle.turnLeft()
  327.     local result, data = turtle.inspect()
  328.     turtle.turnRight()
  329.     return result, data
  330.   end
  331.  
  332.   function turtle.inspectRight()
  333.     turtle.turnRight()
  334.     local result, data = turtle.inspect()
  335.     turtle.turnLeft()
  336.     return result, data
  337.   end
  338.  
  339.   function turtle.inspectBack()
  340.     turtle.turnAround()
  341.     local result, data = turtle.inspect()
  342.     turtle.turnAround()
  343.     return result, data
  344.   end
  345.  
  346.   function turtle.inspectDirection(face)
  347.     if face == "top" then
  348.       return turtle.inspectUp()
  349.     elseif face == "bottom" then
  350.       return turtle.inspectDown()
  351.     elseif face == "left" then
  352.       return turtle.inspectLeft()
  353.     elseif face == "right" then
  354.       return turtle.inspectRight()
  355.     elseif face == "back" then
  356.       return turtle.inspectBack()
  357.     elseif face == "front" then
  358.       return turtle.inspect()
  359.     else
  360.       error("UNKNOWN DIRECTION: "..tostring(face).." !")
  361.     end
  362.   end
  363.  
  364. end
  365.  
  366.  
  367. ---- ADDITIONAL PLACE ----
  368.  
  369. function turtle.placeLeft(signtext)
  370.   turtle.turnLeft()
  371.   local result = turtle.place(signtext)
  372.   turtle.turnRight()
  373.   return result
  374. end
  375.  
  376. function turtle.placeRight(signtext)
  377.   turtle.turnRight()
  378.   local result = turtle.place(signtext)
  379.   turtle.turnLeft()
  380.   return result
  381. end
  382.  
  383. function turtle.placeBack(signtext)
  384.   turtle.turnAround()
  385.   local result = turtle.place(signtext)
  386.   turtle.turnAround()
  387.   return result
  388. end
  389.  
  390. function turtle.placeDirection(face, signtext)
  391.   if face == "top" then
  392.     return turtle.placeUp(signtext)
  393.   elseif face == "bottom" then
  394.     return turtle.placeDown(signtext)
  395.   elseif face == "left" then
  396.     return turtle.placeLeft(signtext)
  397.   elseif face == "right" then
  398.     return turtle.placeRight(signtext)
  399.   elseif face == "back" then
  400.     return turtle.placeBack(signtext)
  401.   elseif face == "front" then
  402.     return turtle.place(signtext)
  403.   else
  404.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  405.   end
  406. end
  407.  
  408.  
  409. ---- ADDITIONAL DROPS ----
  410.  
  411. function turtle.dropLeft(amount)
  412.   turtle.turnLeft()
  413.   local result = turtle.drop(amount)
  414.   turtle.turnRight()
  415.   return result
  416. end
  417.  
  418. function turtle.dropRight(amount)
  419.   turtle.turnRight()
  420.   local result = turtle.drop(amount)
  421.   turtle.turnLeft()
  422.   return result
  423. end
  424.  
  425. function turtle.dropBack(amount)
  426.   turtle.turnAround()
  427.   local result = turtle.drop(amount)
  428.   turtle.turnAround()
  429.   return result
  430. end
  431.  
  432. function turtle.dropDirection(face, amount)
  433.   if face == "top" then
  434.     return turtle.dropUp(amount)
  435.   elseif face == "bottom" then
  436.     return turtle.dropDown(amount)
  437.   elseif face == "left" then
  438.     return turtle.dropLeft(amount)
  439.   elseif face == "right" then
  440.     return turtle.dropRight(amount)
  441.   elseif face == "back" then
  442.     return turtle.dropBack(amount)
  443.   elseif face == "front" then
  444.     return turtle.drop(amount)
  445.   else
  446.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  447.   end
  448. end
  449.  
  450.  
  451. ---- ADDITIONAL SUCKS ----
  452.  
  453. function turtle.suckLeft(amount)
  454.   turtle.turnLeft()
  455.   local result = turtle.suck(amount)
  456.   turtle.turnRight()
  457.   return result
  458. end
  459.  
  460. function turtle.suckRight(amount)
  461.   turtle.turnRight()
  462.   local result = turtle.suck(amount)
  463.   turtle.turnLeft()
  464.   return result
  465. end
  466.  
  467. function turtle.suckBack(amount)
  468.   turtle.turnAround()
  469.   local result = turtle.suck(amount)
  470.   turtle.turnAround()
  471.   return result
  472. end
  473.  
  474. function turtle.suckDirection(face, amount)
  475.   if face == "top" then
  476.     return turtle.suckUp(amount)
  477.   elseif face == "bottom" then
  478.     return turtle.suckDown(amount)
  479.   elseif face == "left" then
  480.     return turtle.suckLeft(amount)
  481.   elseif face == "right" then
  482.     return turtle.suckRight(amount)
  483.   elseif face == "back" then
  484.     return turtle.suckBack(amount)
  485.   elseif face == "front" then
  486.     return turtle.suck(amount)
  487.   else
  488.     error("UNKNOWN DIRECTION: "..tostring(face).." !")
  489.   end
  490. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement