coaster3000

Turtle Complete API

Jun 5th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.84 KB | None | 0 0
  1. --[[
  2.     Name: Turtle Complete
  3.     Author: Chris K (Coaster3000)
  4.     Creation Date: 6/4/13
  5.     Updated Date: 6/6/13
  6.     Version: 1.0 Pre-Release
  7.  
  8.     COPYRIGHT NOTICE
  9.     © Chris K 2013, Some Rights Reserved
  10.     Except where otherwise noted, this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0
  11.  
  12.     You are free:
  13.         * to Share - to copy, distribute and transmit the work in accordance that is stays under non profit distribution
  14.         * to Remix - to adapt the work
  15.  
  16.     Under the following conditions:
  17.         * 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).
  18.         * 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.
  19.         * Non-Profit. You must distribute it in a non-profit way.
  20.         * 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.
  21.         * Any of the above conditions can be waived if you get permission from the copyright holder.
  22.         * Nothing in this license impairs or restricts the author's moral rights.
  23.    
  24.        
  25.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  26.     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  27.     COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  28.     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29.    
  30.     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/.
  31. ]]
  32. function _G.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
  33.  
  34. local function cust_fmod(num, div)
  35.     local b = math.fmod(num, div)
  36.     if b < 0 then
  37.         b = (div-1) + b
  38.     end
  39.     return b
  40. end
  41.  
  42. function createAPI()
  43.     local t = {}
  44.     t.posx = 0
  45.     t.posy = 0
  46.     t.posz = 0
  47.     t.dir = 0
  48.     t.slot = 1
  49.     turtle.select(1)
  50.     t.attemptLimit = 500
  51.  
  52.     function t.setLimit(num)
  53.         t.attemptLimit = num or t.attemptLimit
  54.     end
  55.  
  56.     function t.getLimit()
  57.         return t.attemptLimit
  58.     end
  59.  
  60.     function t.getPosition()
  61.         local b = {}
  62.         b.x = t.posx
  63.         b.y = t.posy
  64.         b.z = t.posz
  65.         b.dir = t.dir
  66.         return b
  67.     end
  68.  
  69.     function t.dump()
  70.         printError("Memory Dump of data...")
  71.         printError("posx: "..t.posx.."    posy: "..t.posy.."    posz: "..t.posz)
  72.         printError("dir: "..t.dir.."    slot: "..t.slot)
  73.         printError("fuel: "..t.getFuelLevel().."    Max Attempt Limit: "..t.attemptLimit)
  74.         printError("End of dump...")
  75.     end
  76.  
  77.  
  78.     function t.back(moveCount)
  79.         if not moveCount then
  80.             local good = turtle.back()
  81.             if good then
  82.                 local b = cust_fmod(t.dir, 4)
  83.                 if b == 0 then t.posx = t.posx - 1
  84.                 elseif b == 1 then t.posz = t.posz - 1
  85.                 elseif b == 2 then t.posx = t.posx + 1
  86.                 elseif b == 3 then t.posz = t.posz + 1
  87.                 else
  88.                     printError("Turtle Complete system had a severe error.")
  89.                     t.dump()
  90.                     error("",0)
  91.                 end
  92.             end
  93.             return good
  94.         else
  95.             local count = 0
  96.             for i=1,moveCount do
  97.                 if t.forward() then
  98.                     count = count + 1
  99.                 end
  100.             end
  101.             return count
  102.         end
  103.     end
  104.  
  105.     function t.forward(moveCount)
  106.         if not moveCount then
  107.             local good = turtle.forward()
  108.             if good then
  109.                 local b = cust_fmod(t.dir, 4)
  110.                 if b == 0 then t.posx = t.posx + 1
  111.                 elseif b == 1 then t.posz = t.posz + 1
  112.                 elseif b == 2 then t.posx = t.posx - 1
  113.                 elseif b == 3 then t.posz = t.posz - 1
  114.                 else
  115.                     printError("Turtle Complete system had a severe error.")
  116.                     t.dump()
  117.                     error("",0)
  118.                 end
  119.             end
  120.             return good
  121.         else
  122.             local count = 0
  123.             for i=1,moveCount do
  124.                 if t.forward() then
  125.                     count = count + 1
  126.                 end
  127.             end
  128.             return count
  129.         end
  130.     end
  131.  
  132.     function t.turnRight(turnCount)
  133.         if not turnCount then
  134.             local good = turtle.turnRight()
  135.             if good then
  136.                 t.dir = t.dir + 1
  137.             end
  138.             return good
  139.         else
  140.             local count = 0
  141.             for i=1,turnCount do
  142.                 if t.turnRight() then
  143.                     count = count + 1
  144.                 end
  145.             end
  146.             return count
  147.         end
  148.     end
  149.  
  150.     function t.turnLeft(turnCount)
  151.         if not turnCount then
  152.             local good = turtle.turnLeft()
  153.             if good then
  154.                 t.dir = t.dir - 1
  155.             end
  156.             return good
  157.         else
  158.             local count = 0
  159.             for i=1,turnCount do
  160.                 if t.turnLeft() then
  161.                     count = count + 1
  162.                 end
  163.             end
  164.             return count
  165.         end
  166.     end
  167.  
  168.     function t.dig()
  169.         local count = 0
  170.         while turtle.dig() do
  171.             count = count + 1
  172.             sleep(0.5)
  173.             if count > 500 then
  174.                 return count
  175.             end
  176.         end
  177.         if count == 0 then
  178.             return false
  179.         else
  180.             return count
  181.         end
  182.     end
  183.  
  184.     function t.digDown()
  185.         local count = 0
  186.         while turtle.digDown() do
  187.             count = count + 1
  188.             if count > 500 then
  189.                 return count
  190.             end
  191.             sleep(0.5)
  192.         end
  193.         if count == 0 then
  194.             return false
  195.         else
  196.             return count
  197.         end
  198.     end
  199.  
  200.     function t.digUp()
  201.         local count = 0
  202.         while turtle.digUp() do
  203.             count = count + 1
  204.             if count > 500 then
  205.                 return count
  206.             end
  207.             sleep(0.5)
  208.         end
  209.         if count == 0 then
  210.             return false
  211.         else
  212.             return count
  213.         end
  214.     end
  215.  
  216.     function t.up(moveCount)
  217.         if not moveCount then  
  218.             local good = turtle.up()
  219.             if good then
  220.                 t.posy = t.posy + 1
  221.             end
  222.             return good
  223.         else
  224.             local count = 0
  225.             for i=1,moveCount do
  226.                 if t.up() then
  227.                     count = count + 1
  228.                 end
  229.             end
  230.             return count
  231.         end
  232.     end
  233.  
  234.     function t.down(moveCount)
  235.         if not moveCount then  
  236.             local good = turtle.down()
  237.             if good then
  238.                 t.posy = t.posy - 1
  239.             end
  240.             return good
  241.         else
  242.             local count = 0
  243.             for i=1,moveCount do
  244.                 if t.down() then
  245.                     count = count + 1
  246.                 end
  247.             end
  248.             return count
  249.         end
  250.     end
  251.  
  252.     function t.tryDown(_break, _attack)
  253.         local attempts = 0
  254.         if _break == nil then _break = false end
  255.         if _attack == nil then _attack = false end
  256.  
  257.         while attempts <= t.attemptLimit do
  258.             local good = t.down()
  259.             if good then
  260.                 return true
  261.             end
  262.             if _break then t.digDown() end
  263.             if _attack then turtle.attackDown() end
  264.  
  265.             attempts = attempts + 1
  266.  
  267.             local isleep = 0.2
  268.             if _break then isleep = isleep + 0.3 end
  269.             sleep(isleep)
  270.         end
  271.         return false
  272.     end
  273.  
  274.     function t.tryForward(_break, _attack)
  275.         local attempts = 0
  276.         if _break == nil then _break = false end
  277.         if _attack == nil then _attack = false end
  278.  
  279.         while attempts <= t.attemptLimit do
  280.             local good = t.forward()
  281.             if good then
  282.                 return true
  283.             end
  284.             if _break then t.dig() end
  285.             if _attack then turtle.attack() end
  286.  
  287.             attempts = attempts + 1
  288.  
  289.             local isleep = 0.2
  290.             if _break then isleep = isleep + 0.3 end
  291.             sleep(isleep)
  292.         end
  293.         return false
  294.     end
  295.  
  296.     function t.tryUp(_break, _attack)
  297.         local attempts = 0
  298.         if _break == nil then _break = false end
  299.         if _attack == nil then _attack = false end
  300.  
  301.         while attempts <= t.attemptLimit do
  302.             local good = t.up()
  303.             if good then
  304.                 return true
  305.             end
  306.             if _break then t.digUp() end
  307.             if _attack then turtle.attackUp()   end
  308.  
  309.             attempts = attempts + 1
  310.  
  311.             local isleep = 0.2
  312.             if _break then isleep = isleep + 0.3 end
  313.             sleep(isleep)
  314.         end
  315.         return false
  316.     end
  317.  
  318.     function t.getFuelLevel()
  319.         return turtle.getFuelLevel()
  320.     end
  321.  
  322.     function t.select(i)
  323.         local good = turtle.select(i)
  324.         if good then
  325.             t.slot = i
  326.         end
  327.         return good
  328.     end
  329.  
  330.     function t.craft(quantity)
  331.         return turtle.craft(quantity)
  332.     end
  333.  
  334.     function t.place(signText)
  335.         return turtle.place(signText)
  336.     end
  337.  
  338.     function t.placeDown()
  339.         return turtle.placeDown()
  340.     end
  341.  
  342.     function t.placeUp()
  343.         return turtle.placeUp()
  344.     end
  345.  
  346.     function t.detect()
  347.         return turtle.detect()
  348.     end
  349.  
  350.     function t.compare()
  351.         return turtle.compare()
  352.     end
  353.  
  354.     function t.compareUp()
  355.         return turtle.compareUp()
  356.     end
  357.  
  358.     function t.compareDown()
  359.         return turtle.compareDown()
  360.     end
  361.  
  362.     function t.compareTo(slot)
  363.         return turtle.compareTo(slot)
  364.     end
  365.  
  366.     function t.drop(count)
  367.         return turtle.drop(count)
  368.     end
  369.  
  370.     function t.dropUp(count)
  371.         return turtle.dropUp(count)
  372.     end
  373.  
  374.     function t.dropDown(count)
  375.         return turtle.dropDown(count)
  376.     end
  377.  
  378.     function t.suck(all)
  379.         if all then
  380.             while turtle.suck() do
  381.                 sleep(0.1)
  382.             end
  383.             return true
  384.         end
  385.         return turtle.suck()
  386.     end
  387.  
  388.     function t.suckUp(all)
  389.         if all then
  390.             while turtle.suckUp() do
  391.                 sleep(0.1)
  392.             end
  393.             return true
  394.         end
  395.         return turtle.suckUp()
  396.     end
  397.  
  398.     function t.suckDown(all)
  399.         if all then
  400.             while turtle.suckDown() do
  401.                 sleep(0.1)
  402.             end
  403.             return true
  404.         end
  405.         return turtle.suckDown()
  406.     end
  407.  
  408.     function t.attack()
  409.         return turtle.attack()
  410.     end
  411.  
  412.     function t.attackUp()
  413.         return turtle.attackUp()
  414.     end
  415.  
  416.     function t.attackDown()
  417.         return turtle.attackDown()
  418.     end
  419.  
  420.     function t.getSlot()
  421.         return t.slot
  422.     end
  423.  
  424.     function t.isFuel(_slot)
  425.         local retslot = t.getSlot()
  426.         _slot = _slot or retslot
  427.  
  428.         t.select(_slot)
  429.         local bFuel = t.refuel(0)
  430.         t.select(retslot)
  431.         return bFuel
  432.     end
  433.  
  434.     function t.getItemCount(slotNum)
  435.         slotNum = slotNum or t.getSlot()
  436.         return turtle.getItemCount(slotNum)
  437.     end
  438.  
  439.     function t.getItemSpace(slotNum)
  440.         slotNum = slotNum or t.getSlot()
  441.         return turtle.getItemSpace(slotNum)
  442.     end
  443.  
  444.     function t.transferTo(slot, quantity)
  445.         return turtle.transferTo(slot, quantity)
  446.     end
  447.  
  448.     function t.refuel(ammount, advanced)
  449.         local advanced = advanced or false
  450.         if advanced then
  451.             if ammount > t.getFuelLevel() then
  452.                 for i=1,16 do
  453.                     t.select(i)
  454.                     while t.getItemCount(i) > 0 do
  455.                         if not t.isFuel(i) then
  456.                             break
  457.                         end
  458.                         if t.getFuelLevel() > ammount then
  459.                             return true
  460.                         else
  461.                             t.refuel(1)
  462.                         end
  463.                     end
  464.                 end
  465.             end
  466.         else
  467.             return turtle.refuel(ammount)
  468.         end
  469.     end
  470.  
  471.     --[[ Start: Attribution of code
  472.         This section of code is based off of the default "go" program.
  473.         It has been modified to include additional actions for this api to run.
  474.         It also has been modified to run through the api to keep the internal position tracking intact.
  475.     ]]
  476.     function t.go(...) --Code must go on bottome due to shortcuts to other functions
  477.         local args = {...}
  478.         assert(#args > 0, "Arguments must be specified.",2)
  479.         local tHandlers = {
  480.         ["forward"] = t.tryForward,
  481.         ["fwd"] = t.tryForward,
  482.         ["back"] = t.back,
  483.         ["bck"] = t.back,
  484.         ["left"] = t.turnLeft,
  485.         ["lft"] = t.turnLeft,
  486.         ["right"] = t.turnRight,
  487.         ["rt"] = t.turnRight,
  488.         ["down"] = t.tryDown,
  489.         ["up"] = t.tryUp,
  490.         ["dig"] = t.dig,
  491.         ["dg"] = t.dig,
  492.         ["digdown"] = t.digDown,
  493.         ["digup"] = t.digUp,
  494.         ["place"] = t.place,
  495.         ["placedown"] = t.placeDown,
  496.         ["placeup"] = t.placeUp,
  497.         ["strafeleft"] = t.strafeLeft,
  498.         ["straferight"] = t.strafeRight
  499.         }
  500.  
  501.         local nArg = 1
  502.         local calls = {}
  503.         local fuelNeeded = 0
  504.         while nArg <= #args do
  505.             local sDirection = args[nArg]
  506.             local count = 1
  507.             if nArg < #tArgs then
  508.                 local num = tonumber( tArgs[nArg + 1] )
  509.                 if num then
  510.                     count = num
  511.                     nArg = nArg + 1
  512.                 end
  513.             end
  514.  
  515.             local fnHandler = assert(tHandlers[string.lower(sDirection)], sDirection.." is not a valid direction.", 2)
  516.             if (fnHandler == t.tryForward) or (fnHandler == t.back) or (fnHandler == t.tryUp) or (fnHandler == t.tryDown) then
  517.                 fuelNeeded = fuelNeeded + count
  518.             end
  519.             for i=1,count do
  520.                 calls[#calls+1] = fnHandler
  521.             end
  522.         end
  523.         local good = nil
  524.         if fuelNeeded > 0 then
  525.             good = t.refuel(fuelNeeded, true)
  526.         end
  527.         assert(good, "Do not have enough fuel to make it all the way through calls...\n Fuel Level: "..t.getFuelLevel().."\nFuel Needed: "..fuelNeeded,2)
  528.  
  529.         for i=1,#calls do
  530.             local good, ret = pcall(calls[i])
  531.             if not good then
  532.                 error(ret,3)
  533.             end
  534.         end
  535.     end
  536.     -- End of attributed code
  537.     return t
  538. end
Advertisement
Add Comment
Please, Sign In to add comment