seiterarch

CC sTurtle API

Jan 10th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.80 KB | None | 0 0
  1. if not turtle then
  2.     error( "Cannot load turtle API on computer" )
  3. end
  4.  
  5. --enhanced turn functions with co-ordinate tracking
  6. function turn(face, pos)
  7.     face = sUtil.faceForm(face)
  8.     pos = sUtil.posForm(pos)
  9.    
  10.     if face == 'right' then
  11.         turtle.turnRight()
  12.         pos[4] = (pos[4] + 1) % 4
  13.     elseif face == 'back' then
  14.         turtle.turnRight()
  15.         turtle.turnRight()
  16.         pos[4] = (pos[4] + 2) % 4
  17.     elseif face == 'left' then
  18.         turtle.turnLeft()
  19.         pos[4] = (pos[4] + 3) % 4
  20.     end
  21.    
  22.     return pos
  23. end
  24. function turnTo(f, pos)
  25.     f = sUtil.nForm(f)
  26.     f = f % 4
  27.     pos = sUtil.posForm(pos)
  28.     local d = (f - pos[4]) % 4
  29.     if d == 1 then pos = turn('right', pos)
  30.     elseif d == 2 then pos = turn('back', pos)
  31.     elseif d == 3 then pos = turn('left', pos)
  32.     end
  33.     return pos
  34. end
  35.  
  36. --basic movement functions with co-ordinate tracking
  37. function f(n, pos)
  38.     n = sUtil.nForm(n, 1)
  39.     pos = sUtil.posForm(pos)
  40.    
  41.     if turtle.getFuelLevel() == 0 then
  42.         error('Turtle is out of fuel')
  43.     end
  44.    
  45.     for i=1,n do
  46.         if turtle.forward() then
  47.             if pos[4]==0 then pos[3] = pos[3]+1
  48.             elseif pos[4]==1 then pos[1] = pos[1]-1
  49.             elseif pos[4]==2 then pos[3] = pos[3]-1
  50.             elseif pos[4]==3 then pos[1] = pos[1]+1
  51.             end
  52.         end
  53.     end
  54.    
  55.     return pos
  56. end
  57. function b(n, pos)
  58.     n = sUtil.nForm(n, 1)
  59.     pos = sUtil.posForm(pos)
  60.    
  61.     if turtle.getFuelLevel() == 0 then
  62.         error('Turtle is out of fuel')
  63.     end
  64.    
  65.     for i=1,n do
  66.         if turtle.back() then
  67.             if pos[4]==0 then pos[3] = pos[3]-1
  68.             elseif pos[4]==1 then pos[1] = pos[1]+1
  69.             elseif pos[4]==2 then pos[3] = pos[3]+1
  70.             elseif pos[4]==3 then pos[1] = pos[1]-1
  71.             end
  72.         end
  73.     end
  74.    
  75.     return pos
  76. end
  77. function u(n, pos)
  78.     n = sUtil.nForm(n, 1)
  79.     pos = sUtil.posForm(pos)
  80.    
  81.     if turtle.getFuelLevel() == 0 then
  82.         error('Turtle is out of fuel')
  83.     end
  84.    
  85.     for i=1,n do
  86.         if turtle.up() then
  87.             pos[2] = pos[2]+1
  88.         end
  89.     end
  90.    
  91.     return pos
  92. end
  93. function d(n, pos)
  94.     n = sUtil.nForm(n, 1)
  95.     pos = sUtil.posForm(pos)
  96.    
  97.     if turtle.getFuelLevel() == 0 then
  98.         error('Turtle is out of fuel')
  99.     end
  100.    
  101.     for i=1,n do
  102.         if turtle.down() then
  103.             pos[2] = pos[2]-1
  104.         end
  105.     end
  106.    
  107.     return pos
  108. end
  109. function l(n, pos)
  110.     n = sUtil.nForm(n, 1)
  111.     pos = sUtil.posForm(pos)
  112.    
  113.     pos = turn('left', pos)
  114.     pos = f(n, pos)
  115.     pos = turn('right', pos)
  116.    
  117.     return pos
  118. end
  119. function r(n, pos)
  120.     n = sUtil.nForm(n, 1)
  121.     pos = sUtil.posForm(pos)
  122.  
  123.     pos = turn('right', pos)
  124.     pos = f(n, pos)
  125.     pos = turn('left', pos)
  126.    
  127.     return pos
  128. end
  129.  
  130. --destructive movement functions with co-ordinate tracking
  131. function mine(n, pos)
  132.     n = sUtil.nForm(n, 1)
  133.     pos = sUtil.posForm(pos)
  134.    
  135.     if turtle.getFuelLevel() == 0 then
  136.         error('Turtle is out of fuel')
  137.     end
  138.    
  139.     for i=1,n do
  140.         while not turtle.forward() do
  141.             turtle.dig()
  142.             turtle.attack()
  143.         end
  144.         if pos[4]==0 then pos[3] = pos[3]+1
  145.             elseif pos[4]==1 then pos[1] = pos[1]-1
  146.             elseif pos[4]==2 then pos[3] = pos[3]-1
  147.             elseif pos[4]==3 then pos[1] = pos[1]+1
  148.         end
  149.     end
  150.    
  151.     return(pos)
  152. end
  153. function mineUp(n, pos)
  154.     n = sUtil.nForm(n, 1)
  155.     pos = sUtil.posForm(pos)
  156.    
  157.     if turtle.getFuelLevel() == 0 then
  158.         error('Turtle is out of fuel')
  159.     end
  160.    
  161.     for i=1,n do
  162.         while not turtle.up() do
  163.             turtle.digUp()
  164.             turtle.attackUp()
  165.         end
  166.         pos[2] = pos[2] + 1
  167.     end
  168.    
  169.     return(pos)
  170. end
  171. function mineDown(n, pos)
  172.     n = sUtil.nForm(n, 1)
  173.     pos = sUtil.posForm(pos)
  174.    
  175.     if turtle.getFuelLevel() == 0 then
  176.         error('Turtle is out of fuel')
  177.     end
  178.    
  179.     for i=1,n do
  180.         turtle.digDown()
  181.         while not turtle.down() do
  182.             turtle.attackDown()
  183.         end
  184.         pos[2] = pos[2] - 1
  185.     end
  186.    
  187.     return(pos)
  188. end
  189.  
  190. --Composite movement functions
  191. function moveTo(target, pos)
  192.     target = sUtil.posForm(target)
  193.     pos = sUtil.posForm(pos)
  194.    
  195.     --x-axis movement
  196.     if pos[1] > target[1] then
  197.         pos = turnTo(1, pos)
  198.         local d = pos[1] - target[1]
  199.         pos = f(d, pos)
  200.     elseif pos[1] < target[1] then
  201.         pos = turnTo(3, pos)
  202.         local d = target[1] - pos[1]
  203.         pos = f(d, pos)
  204.     end
  205.    
  206.     --z-axis movement
  207.     if pos[3] > target[3] then
  208.         pos = turnTo(2, pos)
  209.         local d = pos[3] - target[3]
  210.         pos = f(d, pos)
  211.     elseif pos[3] < target[3] then
  212.         pos = turnTo(0, pos)
  213.         local d = target[3] - pos[3]
  214.         pos = f(d, pos)
  215.     end
  216.    
  217.     --vertical movement
  218.     if pos[2] ~= target[2] then
  219.         local d = target[2] - pos[2]
  220.         if d > 0 then
  221.             pos = u(d, pos)
  222.         else
  223.             pos = d(-d, pos)
  224.         end
  225.     end
  226.    
  227.     pos = turnTo(target[4], pos)
  228.     return pos
  229. end
  230. function pathTo(target, pos)
  231.     print('This function is just a placeholder, sorry.')
  232.     return pos
  233. end
  234. function mineTo(target, pos)
  235.     target = sUtil.posForm(target)
  236.     pos = sUtil.posForm(pos)
  237.    
  238.     --x-axis movement
  239.     if pos[1] > target[1] then
  240.         pos = turnTo(1, pos)
  241.         local d = pos[1] - target[1]
  242.         pos = mine(d, pos)
  243.     elseif pos[1] < target[1] then
  244.         pos = turnTo(3, pos)
  245.         local d = target[1] - pos[1]
  246.         pos = mine(d, pos)
  247.     end
  248.    
  249.     --z-axis movement
  250.     if pos[3] > target[3] then
  251.         pos = turnTo(2, pos)
  252.         local d = pos[3] - target[3]
  253.         pos = mine(d, pos)
  254.     elseif pos[3] < target[3] then
  255.         pos = turnTo(0, pos)
  256.         local d = target[3] - pos[3]
  257.         pos = mine(d, pos)
  258.     end
  259.    
  260.     --vertical movement
  261.     if pos[2] ~= target[2] then
  262.         local d = target[2] - pos[2]
  263.         if d > 0 then
  264.             pos = mineUp(d, pos)
  265.         else
  266.             pos = mineDown(-d, pos)
  267.         end
  268.     end
  269.    
  270.     pos = turnTo(target[4], pos)
  271.     return pos
  272. end
  273.  
  274. --Non-motion reliable dig function
  275. function dig(face)
  276.     face = sUtil.faceForm(face, 'front')
  277.    
  278.     turn(face)
  279.    
  280.     if face == 'up' then
  281.         while turtle.detectUp() do
  282.             turtle.digUp()
  283.             turtle.attackUp()
  284.             sleep(0.1)
  285.         end
  286.     elseif face == 'down' and turtle.detectDown() then
  287.         turtle.digDown()
  288.     else
  289.         while turtle.detect() do
  290.             turtle.dig()
  291.             turtle.attack()
  292.             sleep(0.1)
  293.         end
  294.     end
  295.    
  296.     face = sUtil.faceInv(face)
  297.     turn(face)
  298. end
  299. --Reliable place functions
  300. function place(face)
  301.     face = sUtil.faceForm(face)
  302.    
  303.     turn(face)
  304.     if face == 'down' then
  305.         if not turtle.compareDown() then
  306.             while not turtle.placeDown() do
  307.                 turtle.digDown()
  308.                 turtle.attackDown()
  309.             end
  310.         end
  311.     elseif face == 'up' then
  312.         if not turtle.compareUp() then
  313.             while not turtle.placeUp() do
  314.                 turtle.digUp()
  315.                 turtle.attackUp()
  316.             end
  317.         end
  318.     else
  319.         if not turtle.compare() then
  320.             while not turtle.place() do
  321.                 turtle.dig()
  322.                 turtle.attack()
  323.             end
  324.         end
  325.     end
  326.     face = sUtil.faceInv(face)
  327.     turn(face, {0, 0, 0, 0})
  328. end
  329. --Extended detect function, works as compare if slot specified
  330. function detect(face, s)
  331.     face = sUtil.faceForm(face, 'front')
  332.     s = sUtil.slotForm(s, true)
  333.     if s ~= nil then
  334.         if select(s) == 0 then
  335.             error('Detect cannot compare to an empty slot')
  336.         end
  337.     end
  338.     q = false
  339.    
  340.     turn(face)
  341.     if s == nil then
  342.         if face == 'up' then
  343.             q = turtle.detectUp()
  344.         elseif face == 'down' then
  345.             q = turtle.detectDown()
  346.         else q = turtle.detect()
  347.         end
  348.     else
  349.         if face == 'up' then
  350.             q = turtle.compareUp()
  351.         elseif face == 'down' then
  352.             q = turtle.compareDown()
  353.         else q = turtle.compare()
  354.         end
  355.     end
  356.     turn(sUtil.faceInv(face))
  357.    
  358.     return q
  359. end
  360.  
  361. --Enhanced refuel function
  362. function refuel(s, level)
  363.     s = sUtil.slotForm(s, true)
  364.     level = sUtil.nForm(level, 1)
  365.     local current = turtle.getFuelLevel()
  366.     if current >= level then
  367.         print('Turtle has enough fuel')
  368.         return true
  369.     else print('Refuelling')
  370.     end
  371.    
  372.     if s ~= nil then turtle.select(s) end
  373.    
  374.     while current < level do
  375.         if not(turtle.refuel(1)) then
  376.             print('Run out of fuel.')
  377.             return false
  378.         end
  379.         current = turtle.getFuelLevel()
  380.     end
  381.     return true
  382. end
  383.  
  384. --Extended select function
  385. function select(s)
  386.     s = sUtil.slotForm(s, false)
  387.     turtle.select(s)
  388.     return turtle.getItemCount(s)
  389. end
  390.  
  391. --Find all slots containing the current item in slot s
  392. function findSlots(s)
  393.     s = sUtil.slotForm(s, true)
  394.     if s ~= nil then turtle.select(s) end
  395.     local slots = {}
  396.     local j=1
  397.    
  398.     for i = 1,16 do
  399.         if turtle.compareTo(i) then
  400.             slots[j] = i
  401.             j = j+1
  402.         end
  403.     end
  404.     return slots
  405. end
  406.  
  407. --Count the contents of multiple slots
  408. function sumCount(slots)
  409.     local n = 0
  410.     for i, v in ipairs(slots) do
  411.         n = n + turtle.getItemCount(sUtil.slotForm(v))
  412.     end
  413.     return n
  414. end
  415.  
  416. --Deposit all but one of an item (option for all). Returns number deposited.
  417. function smartDeposit(s, face, keep)
  418.     s = sUtil.slotForm(s)
  419.     face = sUtil.faceForm(face)
  420.     if keep ~= false then keep = true end
  421.     local a = findSlots(s)
  422.     local n = 0
  423.    
  424.     turn(face)
  425.    
  426.     for i, v in ipairs(a) do
  427.         turtle.select(v)
  428.         local c = turtle.getItemCount(v)
  429.         if v == s and keep then c = c-1 end
  430.        
  431.         if face == 'up' then turtle.dropUp(c)
  432.         elseif face == 'down' then turtle.dropDown(c)
  433.         else turtle.drop(c) end
  434.        
  435.         n = n + c
  436.     end
  437.    
  438.     turn(face, true)   
  439.     return n
  440. end
  441.  
  442. --Extended transpose function to allow turtle to prioritise a slot
  443. function ioTranspose(n, face, s)
  444.     n = n or 1
  445.     face = face or 'front'
  446.     if s~=nil then
  447.         s = s % 16
  448.         if s==0 then
  449.             s = 16
  450.         end
  451.         turtle.select(s)
  452.     end
  453.     sUtil.transpose(n, face)
  454. end
Advertisement
Add Comment
Please, Sign In to add comment