Advertisement
coaster3000

Turtle Complete API Beta

Jun 9th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.41 KB | None | 0 0
  1. --[[
  2.     Name: Turtle Complete
  3.     Author: Chris K (Coaster3000)
  4.     Creation Date: 6/4/13
  5.     Updated Date: 6/9/13
  6.     Version: 0.1
  7.     Flavor: API Replacement / wrapper
  8.  
  9.     COPYRIGHT NOTICE
  10.     © Chris K 2013, Some Rights Reserved
  11.     Except where otherwise noted, this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0
  12.  
  13.     You are free:
  14.         * to Share - to copy, distribute and transmit the work in accordance that is stays under non profit distribution
  15.         * to Remix - to adapt the work
  16.  
  17.     Under the following conditions:
  18.         * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
  19.         * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
  20.         * Non-Profit. You must distribute it in a non-profit way.
  21.         * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
  22.         * Any of the above conditions can be waived if you get permission from the copyright holder.
  23.         * Nothing in this license impairs or restricts the author's moral rights.
  24.    
  25.        
  26.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  27.     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  28.     COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29.     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30.    
  31.     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/.
  32. ]]
  33. local nativeassert = assert
  34.  
  35. local function assert(condition, msg, level) level = (level or 1) + 1 if not condition then error(msg, level) end return condition end -- Custom assert function. Based of theOriginalBit's assert function link: http://theoriginalbit.net46.net/index.html
  36.  
  37. local function yieldme()
  38.     os.queueEvent("nothing")
  39.     coroutine.yield()
  40. end
  41.  
  42. if not turtle then
  43.     error( "Cannot load turtle API on computer" )
  44. end
  45.  
  46. native = turtle.native or turtle
  47.  
  48. local function wrap( _sCommand )
  49.     return function( ... )
  50.         local id = native[_sCommand]( ... )
  51.         local event, responseID, success
  52.         while event ~= "turtle_response" or responseID ~= id do
  53.             event, responseID, success = os.pullEvent( "turtle_response")
  54.         end
  55.         return success
  56.     end
  57. end
  58.  
  59. local turtle = {}
  60. turtle["getItemCount"] = native.getItemCount
  61. turtle["getItemSpace"] = native.getItemSpace
  62.  
  63. for k,v in pairs( native ) do
  64.     if type (k) == "string" and type(v) == "function" then
  65.         if turtle[k] == nil then
  66.             turtle[k] = wrap(k)
  67.         end
  68.     end
  69. end
  70.  
  71. local env = getfenv()
  72. for k,v in pairs( turtle ) do
  73.     env[k] = v
  74. end
  75.  
  76. -- End Globalization --
  77.  
  78. ----Data Handling----
  79.  
  80.  
  81. ------------------
  82. ---- Tracking ----
  83.  
  84. -- Position data
  85. local position = {}
  86. position.x = 0
  87. position.y = 0
  88. position.z = 0
  89. position.dir = 0
  90.  
  91. local function cust_fmod(num, div)
  92.     local ret = math.fmod(num, div)
  93.     if ret < 0 then
  94.         ret = (div - 1) + ret
  95.     end
  96.     return ret
  97. end
  98.  
  99. function getX()
  100.     local ret = position.x
  101.     return ret
  102. end
  103.  
  104. function getY()
  105.     local ret = position.y
  106.     return ret
  107. end
  108.  
  109. function getZ()
  110.     local ret = position.z
  111.     return ret
  112. end
  113.  
  114. -- End position data
  115. local slot = 1
  116. turtle.select(1)
  117.  
  118. function select(_slot)
  119.     local good = turtle.select(_slot)
  120.     if good then
  121.         slot = _slot
  122.     end
  123.     return good
  124. end
  125.  
  126. function getSlot()
  127.     local s = slot
  128.     return s
  129. end
  130.  
  131. -- Breakers
  132.  
  133. local maxAttempts = 500
  134.  
  135. function getMaxAttempts() local a = maxAttempts return a end -- It is written this way to make sure variable is not publicized.
  136.  
  137. function setMaxAttempts(num)
  138.     num = num or maxAttempts
  139.     maxAttempts = num
  140. end
  141. -- End Breakers
  142.  
  143.  
  144. ------------------
  145. ---- Fuel --------
  146.  
  147. function hasFuel( ammount )
  148.     return turtle.getFuelLeve() >= (ammount or 1)
  149. end
  150.  
  151. function isFuel(slot)
  152.     local lastSlot = getSlot()
  153.     select(slot)
  154.     local good = refuel(0)
  155.     select(lastSlot)
  156.     return good
  157. end
  158.  
  159. function refuel( ammount , advanced)
  160.     assert(type(ammount) == "number", "Invalid Argument: Expected number. Got "..type(ammount),2)
  161.     if advanced == nil then
  162.         advanced = true
  163.     end
  164.  
  165.     if advanced then
  166.         if hasFuel(ammount) then
  167.             for i=1,16 do
  168.                 t.select(i)
  169.                 while t.getItemCount(i) > 0 do
  170.                     if not t.isFuel(i) then
  171.                         break
  172.                     end
  173.                     if hasFuel(ammount) then
  174.                         return true
  175.                     else
  176.                         t.refuel(1)
  177.                     end
  178.                 end
  179.             end
  180.             return false
  181.         else
  182.             return true
  183.         end
  184.     else
  185.         return turtle.refuel(ammount)
  186.     end
  187. end
  188.  
  189.  
  190.  
  191. ------------------
  192. ---- Movement ----
  193.  
  194. -- Rotation --
  195. function turnRight()
  196.     local good = turtle.turnRight()
  197.     if good then
  198.         position.dir = position.dir + 1
  199.     end
  200.     return good
  201. end
  202.  
  203. function turnLeft()
  204.     local good = turtle.turnLeft()
  205.     if good then
  206.         position.dir = position.dir - 1
  207.     end
  208. end
  209.  
  210. function turnAround( _useLeft)
  211.     if _useLeft then
  212.         turnLeft()
  213.         turnLeft()
  214.     else
  215.         turnRight()
  216.         turnRight()
  217.     end
  218. end
  219.  
  220. function spin( _count, _useLeft )
  221.     local count = _count or 1
  222.     for i=1,count do
  223.         turnAround(_useLeft)
  224.         turnAround(_useLeft)
  225.     end
  226. end
  227.  
  228. function shakeNo(_alt)
  229.     if _alt then
  230.         turnLeft()
  231.         turnAround()
  232.         turnLeft()
  233.     else
  234.         turnRight()
  235.         turnAround(true)
  236.         turnRight()
  237.     end
  238. end
  239.  
  240. function turn(direction)
  241.     assert(direction,"Must specify a direction!")
  242.     direction = string.lower(direction)
  243.     if direction == "left" then
  244.         turnLeft()
  245.     elseif direction == "right" then
  246.         turnRight()
  247.     elseif (direction == "back") or (direction == "turnaround") or (direction == "around") then
  248.         turnAround()
  249.     elseif (direction == "forward") or (direction == "straight") then
  250.         return
  251.     else
  252.         error("'"..direction.."' is not a valid direction. See documentation.")
  253.     end
  254. end
  255.  
  256. -- Motion --
  257. function forward(_count)
  258.     if not _count then
  259.         local good = turtle.forward()
  260.         if good then
  261.             local m = cust_fmod(position.dir, 4)
  262.             if m == 0 then position.x = position.x + 1
  263.             elseif m == 1 then position.z = position.z + 1
  264.             elseif m == 2 then position.x = position.x - 1
  265.             elseif m == 3 then position.z = position.z - 1
  266.             else print("An internal navigation error has occured in the custom turtle api...")
  267.             end
  268.         end
  269.         return good
  270.     end
  271.     local good = 0
  272.     assert(_count > 0,"Must specify a number above 0")
  273.     assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
  274.     for i=1,_count do
  275.         yieldme()
  276.         if forward() then
  277.             good = good + 1
  278.         end
  279.     end
  280.     return good
  281. end
  282.  
  283. function back(_count)
  284.     if not _count then
  285.         local good = turtle.back()
  286.         if good then
  287.             local m = cust_fmod(position.dir, 4)
  288.             if m == 0 then position.x = position.x - 1
  289.             elseif m == 1 then position.z = position.z - 1
  290.             elseif m == 2 then position.x = position.x + 1
  291.             elseif m == 3 then position.z = position.z + 1
  292.             else print("An internal navigation error has occured in the custom turtle api...")
  293.             end
  294.         end
  295.         return good
  296.     end
  297.     local good = 0
  298.     assert(_count > 0,"Must specify a number above 0")
  299.     assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
  300.     for i=1,_count do
  301.         yieldme()
  302.         if back() then
  303.             good = good + 1
  304.         end
  305.     end
  306.     return good
  307. end
  308.  
  309. function up(_count)
  310.     if not _count then
  311.         local good = turtle.up()
  312.         if good then
  313.             position.y = position.y + 1
  314.         end
  315.         return good
  316.     end
  317.     local good = 0
  318.     assert(_count > 0,"Must specify a number above 0")
  319.     assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
  320.     for i=1,_count do
  321.         yieldme()
  322.         if up() then
  323.             good = good + 1
  324.         end
  325.     end
  326.     return good
  327. end
  328.  
  329. function down(_count)
  330.     if not _count then
  331.         local good = turtle.down()
  332.         if good then
  333.             position.y = position.y - 1
  334.         end
  335.         return good
  336.     end
  337.     local good = 0
  338.     assert(_count > 0,"Must specify a number above 0")
  339.     assert(turtle.getFuelLevel() > _count,"Not enough fuel for '".._count.."' moves.")
  340.     for i=1,_count do
  341.         yieldme()
  342.         if down() then
  343.             good = good + 1
  344.         end
  345.     end
  346.     return good
  347. end
  348.  
  349. function tillBlock(direction)
  350.     direction = direction or "forward"
  351.     turn(direction)
  352.     while not turtle.detect() do
  353.         assert(hasFuel(), "Ran out of fuel.")
  354.         forward()
  355.         yieldme()
  356.     end
  357. end
  358.  
  359. ---------------
  360. ----DIGGING----
  361.  
  362. function dig()
  363.     local good = turtle.dig()
  364.     local ret = good
  365.     while good do
  366.         sleep(0.4)
  367.         good = turtle.dig()
  368.     end
  369.     return ret
  370. end
  371.  
  372. function digUp()
  373.     local good = turtle.digUp()
  374.     local ret = good
  375.     while good do
  376.         sleep(0.4)
  377.         good = turtle.digUp()
  378.     end
  379.     return ret
  380. end
  381.  
  382. function tunnelUp(count, _refuelCheck)
  383.     assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
  384.     assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
  385.     local refuelCheck = nil
  386.     if _refuelCheck == nil then
  387.         refuelCheck = true
  388.     else
  389.         refuelCheck = _refuelCheck
  390.     end
  391.     if refuelCheck and (count > 0) then
  392.         if not refuel(count, true) then
  393.             error("Not enough fuel...")
  394.         end
  395.     end
  396.     for i=1,count do
  397.         digUp()
  398.         while not up() and hasFuel() do
  399.             digUp()
  400.             turtle.attackUp()
  401.         end
  402.     end
  403. end
  404.  
  405. function tunnelDown(count, _refuelCheck)
  406.     assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
  407.     assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
  408.     local refuelCheck = nil
  409.     if _refuelCheck == nil then
  410.         refuelCheck = true
  411.     else
  412.         refuelCheck = _refuelCheck
  413.     end
  414.     if refuelCheck and (count > 0) then
  415.         if not refuel(count, true) then
  416.             error("Not enough fuel...")
  417.         end
  418.     end
  419.     for i=1,count do
  420.         digDown()
  421.         while not down() and hasFuel() do
  422.             digDown()
  423.             turtle.attackDown()
  424.         end
  425.     end
  426. end
  427.  
  428. function tunnel(count, _refuelCheck)
  429.     assert(type(count)=="number", "Invalid Argument: Must specify a number.",0)
  430.     assert((count > 0)or (count == -1), "Invalid Argument: Number must be above 0 or -1 for infinite.",0)
  431.     local refuelCheck = nil
  432.     if _refuelCheck == nil then
  433.         refuelCheck = true
  434.     else
  435.         refuelCheck = _refuelCheck
  436.     end
  437.     if refuelCheck and (count > 0) then
  438.         if not refuel(count, true) then
  439.             error("Not enough fuel...")
  440.         end
  441.     end
  442.     for i=1,count do
  443.         dig()
  444.         while not forward() and hasFuel() do
  445.             dig()
  446.             turtle.attack()
  447.         end
  448.     end
  449. end
  450. print("Warning this version of the Turtle Complete api is broken!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement