Advertisement
ascobol

Asco Turtle Api

Feb 28th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- Turtle Helper Api "ascoTurtleApi"
  2.  
  3. --Load other apis
  4. --APIs are loaded in the downloader
  5.  
  6. -- Turtle Detection
  7.  
  8. function detectRight()
  9.     turtle.turnRight()
  10.     local blocked = turtle.detect()
  11.     turtle.turnLeft()
  12.     return blocked
  13. end
  14.  
  15. function detectLeft()
  16.     turtle.turnLeft()
  17.     local blocked = turtle.detect()
  18.     turtle.turnRight()
  19.     return blocked
  20. end
  21.  
  22. -- Turtle Movements
  23.  
  24. function moveForward()
  25.     checkFuel()
  26.     return turtle.forward()
  27. end
  28.  
  29. function moveBackward()
  30.     checkFuel()
  31.     turnAround()
  32.     local moved = turtle.forward()
  33.     turnAround()
  34.     return moved   
  35. end
  36.  
  37. function moveRight()
  38.     turtle.turnRight()
  39.     local moved = moveForward()
  40.     turtle.turnLeft()
  41.     return moved
  42. end
  43.  
  44. function moveLeft()
  45.     turtle.turnLeft()
  46.     local moved = moveForward()
  47.     turtle.turnRight()
  48.     return moved
  49. end
  50.  
  51. function turnAround()
  52.     turtle.turnLeft()
  53.     turtle.turnLeft()
  54.     return true
  55. end
  56.  
  57. -- Fuel Helper
  58.  
  59. fuelSlot = 16
  60.  
  61. function setFuelSlot(slot)
  62.     fuelSlot = slot
  63. end
  64.  
  65. function getFuelSlot()
  66.     return fuelSlot
  67. end
  68.  
  69. function checkFuel()
  70.     turtle.select(getFuelSlot())
  71.     while (turtle.getFuelLevel() < 1) do
  72.         if ((turtle.getItemCount(getFuelSlot()) == 0) or (not turtle.refuel(0))) then                      
  73.             print("Please put fuel in slot " .. getFuelSlot() .. ". Thank you.")
  74.             waitForInput()             
  75.         end            
  76.         turtle.refuel(1)       
  77.     end
  78. end
  79.  
  80. function printFuelStatus()
  81.     print("Fuel left: " .. turtle.getFuelLevel())
  82. end
  83.  
  84. -- Inventory Helper
  85.  
  86. function makeMaxStack(slot)
  87.     turtle.select(slot)
  88.     for s = 1, 16 do
  89.         if(turtle.getItemSpace(slot) > 0) then
  90.             if (slot ~= s) then
  91.                 if (turtle.getItemCount(slot) == 0) then
  92.                     turtle.select(s)
  93.                     turtle.transferTo(slot)
  94.                     turtle.select(slot)
  95.                 elseif (turtle.compareTo(s)) then
  96.                     turtle.select(s)
  97.                     turtle.transferTo(slot)
  98.                 end
  99.             end
  100.         end
  101.     end
  102.     return (turtle.getItemSpace(slot) == 0)
  103. end
  104.  
  105. function emptyInventory(side)
  106.            
  107.        
  108.         if(side == "back") then
  109.             turtle.turnRight()
  110.             turtle.turnRight()
  111.             dropSlots(side)
  112.             turtle.turnLeft()
  113.             turtle.turnLeft()
  114.         elseif(side == "left") then
  115.             turtle.turnLeft()
  116.             dropSlots(side)
  117.             turtle.turnRight()             
  118.         elseif(side == "right") then
  119.             turtle.turnRight()
  120.             dropSlots(side)
  121.             turtle.turnLeft()
  122.         else
  123.             dropSlots(side)
  124.         end    
  125.    
  126. end
  127.  
  128. function dropSlots(side)
  129.     for s = 1, 16 do
  130.         turtle.select(s)
  131.         local itemsToDrop = turtle.getItemCount(s)
  132.         if (s == getFuelSlot()) then
  133.             -- Fuel will be ignored
  134.             if(turtle.refuel(0)) then
  135.                 itemsToDrop = 0
  136.             end
  137.         end
  138.         if(s == ascoFarmingApi.getSeedSlot()) then
  139.             -- Keep atleast one seed, as "template"
  140.             itemsToDrop = itemsToDrop - 1
  141.         end
  142.         if(itemsToDrop > 0) then
  143.             if (side == "top") then
  144.                 turtle.dropUp(itemsToDrop)
  145.             elseif (side == "bottom") then
  146.                 turtle.dropDown(itemsToDrop)
  147.             else
  148.                 turtle.drop(itemsToDrop)
  149.             end
  150.         end
  151.     end
  152.  
  153. end
  154.  
  155. --Wait for Key-Input, Exit program when F1 is pressed
  156. function waitForInput()
  157.     local event, scancode = os.pullEvent("key")
  158.     input = false
  159.     -- F1 == 59
  160.     if (scancode == 59) then
  161.         error()
  162.     end
  163. end
  164.  
  165. -- Debug function
  166.  
  167. local function ListRedstoneSides()
  168.         for k,v in pairs(redstone.getSides()) do
  169.                 print(v)
  170.         end
  171. end
  172.  
  173. local function CursorPosition()
  174.         local x1, y1 = term.getCursorPos()
  175.         print ("New text line")
  176.         local x2, y2 = term.getCursorPos()
  177.         print ("Before: x=" .. x1 .. " y=" .. y1)
  178.         local x3, y3 = term.getCursorPos()
  179.         print ("After: x=" .. x2 .. " y=" .. y2)
  180.         local x4, y4 = term.getCursorPos()
  181.         print ("After: x=" .. x3 .. " y=" .. y3)
  182.         print ("After: x=" .. x4 .. " y=" .. y4)
  183.  
  184.        
  185.         local s = "Waiting "
  186.         print(s)
  187.         for i = 1, 10 do
  188.             local x,y = term.getCursorPos()
  189.             term.setCursorPos(x, y-1)
  190.             term.clearLine()
  191.             s = s .. "."
  192.             print(s)
  193.             os.sleep(2)
  194.         end
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement