Advertisement
HandieAndy

Turt API

Jan 28th, 2018
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. --Computercraft API For turtle control.
  2. --Made by Andrew Lalis; andrewlalisofficial@gmail.com
  3.  
  4. local orientation = "none"
  5.  
  6. --Local function to validate cardinal directions.
  7. local function isCardinal(dir)
  8.     return (dir == "n" or dir == "s" or dir == "e" or dir == "w")
  9. end
  10.  
  11. --Local function to turn around.
  12. local function turnAround()
  13.     turtle.turnRight()
  14.     turtle.turnRight()
  15. end
  16.  
  17. --Sets the starting orientation of the turtle.
  18. function setOrientation(cardinal)
  19.     if (isCardinal(cardinal)) then
  20.         orientation = cardinal
  21.     else
  22.         error("Unknown cardinal direction. Must be n, s, e, or w.",2)
  23.     end
  24. end
  25.  
  26. --Changes the turtle's orientation.
  27. function changeOrientation(cardinal)
  28.     if (not isCardinal(cardinal)) then
  29.         error("Unknown cardinal direction. Must be n, s, e, or w.", 2)
  30.         return false
  31.     else
  32.         if (orientation == "n") then
  33.             if (cardinal == "w") then
  34.                 turtle.turnLeft()
  35.             elseif (cardinal == "e") then
  36.                 turtle.turnRight()
  37.             elseif (cardinal == "s") then
  38.                 turnAround()
  39.             end
  40.         elseif (orientation == "s") then
  41.             if (cardinal == "w") then
  42.                 turtle.turnRight()
  43.             elseif (cardinal == "e") then
  44.                 turtle.turnLeft()
  45.             elseif (cardinal == "n") then
  46.                 turnAround()
  47.             end
  48.         elseif (orientation == "e") then
  49.             if (cardinal == "n") then
  50.                 turtle.turnLeft()
  51.             elseif (cardinal == "s") then
  52.                 turtle.turnRight()
  53.             elseif (cardinal == "w") then
  54.                 turnAround()
  55.             end
  56.         else
  57.             if (cardinal == "n") then
  58.                 turtle.turnRight()
  59.             elseif (cardinal == "s") then
  60.                 turtle.turnLeft()
  61.             elseif (cardinal == "e") then
  62.                 turnAround()
  63.             end
  64.         end
  65.         orientation = cardinal
  66.         return true
  67.     end
  68. end
  69.  
  70. --Determines if the turtle has an item, and if so, returns the following table.
  71. --{count=X, slots={1,2,3,..}}, or nil, if not found.
  72. function getItemCount(name, damage)
  73.     local dmg = damage or 0
  74.     local result = {count=0, slots={}}
  75.     for i=1,16 do
  76.         local data = turtle.getItemDetail(i)
  77.         if (data ~= nil and data.name == name and data.damage == dmg) then
  78.             result.count = result.count + data.count
  79.             table.insert(result.slots, i)
  80.         end
  81.     end
  82.     if (result.count > 0) then
  83.         return result
  84.     else
  85.         return nil
  86.     end
  87. end
  88.  
  89. --Selects an item by name. Returns success value.
  90. function select(name, damage)
  91.     local item = getItemCount(name, damage)
  92.     if (item ~= nil) then
  93.         turtle.select(item.slots[1])
  94.         return true
  95.     else
  96.         return false
  97.     end
  98. end
  99.  
  100. --Places an item by name. Returns success value.
  101. function place(name, damage, direction, orient)
  102.     if (not select(name, damage)) then
  103.         return false
  104.     end
  105.     local dir = direction or nil
  106.     local newOrient = orient or nil
  107.     local previousOrient = orientation
  108.     local success
  109.     if (dir == "up") then
  110.         success = changeOrientation(newOrient) and turtle.placeUp()
  111.     elseif (dir == "down") then
  112.         success = changeOrientation(newOrient) and turtle.placeDown()
  113.     elseif (dir == "right") then
  114.         turtle.turnRight()
  115.         success = turtle.place()
  116.         turtle.turnLeft()
  117.     elseif (dir == "left") then
  118.         turtle.turnLeft()
  119.         success = turtle.place()
  120.         turtle.turnRight()
  121.     elseif (dir == "back") then
  122.         turnAround()
  123.         success = turtle.place()
  124.         turnAround()
  125.     else
  126.         success = turtle.place()
  127.     end
  128.     if (orientation ~= previousOrient) then
  129.         changeOrientation(previousOrient)
  130.     end
  131.     return success
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement