LayZee

ComputerCraft lzTurtle API

Dec 26th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | None | 0 0
  1. --[[
  2. /**
  3.  * lzTurtle API
  4.  *
  5.  * Guaranteed movement functions for Turtles
  6.  *
  7.  * @copyright (C) LayZee 2014
  8.  */
  9. --]]
  10.  
  11. --[[ Local variables ]]--
  12. local VERSION = 0.2
  13.  
  14. --[[ Local functions ]]--
  15.  
  16. --[[
  17. /**
  18.  * Verify that direction is supported in movement module
  19.  *
  20.  * @param string direction Direction to verify
  21.  */
  22. --]]
  23. local verify = function(direction)
  24.   local valid = {
  25.     up = true,
  26.     down = true,
  27.     forward = true,
  28.     back = true
  29.   }
  30.  
  31.   assert(
  32.     valid[direction],
  33.     'Invalid direction \''
  34.       .. direction
  35.       .. '\' passed to function move'
  36.   )
  37. end
  38.  
  39. --[[
  40. /**
  41.  * Get move function
  42.  *
  43.  * @param string direction Direction to move
  44.  */
  45. --]]
  46. local getMoveCallback = function(direction)
  47.   local callback = {
  48.     up = turtle.up,
  49.     down = turtle.down,
  50.     forward = turtle.forward,
  51.     back = turtle.back
  52.   }
  53.  
  54.   return callback[direction] or false
  55. end
  56.  
  57. --[[
  58. /**
  59.  * Get detect function
  60.  *
  61.  * @param string direction Direction to detect
  62.  */
  63. --]]
  64. local getDetectCallback = function(direction)
  65.   local callback = {
  66.     up = turtle.detectUp,
  67.     down = turtle.detectDown,
  68.     forward = turtle.detect
  69.   }
  70.  
  71.   return callback[direction] or false
  72. end
  73.  
  74. --[[
  75. /**
  76.  * Get dig function
  77.  *
  78.  * @param string direction Direction to dig
  79.  */
  80. --]]
  81. local getDigCallback = function(direction)
  82.   local callback = {
  83.     up = turtle.digUp,
  84.     down = turtle.digDown,
  85.     forward = turtle.dig
  86.   }
  87.  
  88.   return callback[direction] or false
  89. end
  90.  
  91. --Define variable first to allow recursion
  92. local move
  93. --[[
  94. /**
  95.  * Move turtle one step
  96.  *
  97.  * @param string direction Direction to move
  98.  */
  99. --]]
  100. move = function(direction)
  101.   verify(direction)
  102.  
  103.   local moving = getMoveCallback(direction)
  104.   local detect = getDetectCallback(direction)
  105.   local dig = getDigCallback(direction)
  106.   local isSeeded = false
  107.   local turtleId = os.getComputerID()
  108.  
  109.   --Determine which side to check for adjacent CC peripherals
  110.   local sideToCheckForPeripherals = 'front' --Default
  111.  
  112.   if direction == 'up' then
  113.     sideToCheckForPeripherals = 'top'
  114.   elseif direction == 'down' then
  115.     sideToCheckForPeripherals = 'bottom'
  116.   end
  117.  
  118.   --Try to move
  119.   while not moving() do
  120.     local peripheralType = peripheral.getType(sideToCheckForPeripherals)
  121.  
  122.     --Handle CC peripherals (turtle, cable, monitors, etc.)
  123.     while peripheralType do
  124.       if peripheralType == 'turtle' then
  125.         --Seed randomizer
  126.         isSeeded = isSeeded or math.randomseed(turtleId) and true
  127.  
  128.         local waitingPeriod = math.random(1, 10)
  129.  
  130.         --Move up, wait for 1 to 10 seconds, then move back down
  131.         move('up')
  132.         sleep(waitingPeriod)
  133.         move('down')
  134.       else
  135.         --TODO: Handle other peripherals appropriately
  136.         break
  137.       end
  138.  
  139.       peripheralType = peripheral.getType(sideToCheckForPeripherals)
  140.     end
  141.  
  142.     --Check if a block is in the way
  143.     if detect() then
  144.       if not dig() then
  145.         error('Unbreakable block detected')
  146.       elseif detect() then
  147.         --Sand/gravel falling
  148.         sleep(0.8)
  149.       else
  150.         --Block broken
  151.       end
  152.     elseif turtle.getFuelLevel()
  153.       ~= 'unlimited'
  154.       and turtle.getFuelLevel() == 0
  155.     then
  156.       error('No more fuel')
  157.     elseif turtle.attack() then
  158.       --Attacking NPC/player
  159.     end
  160.   end
  161. end
  162.  
  163. --[[ Module definition ]]--
  164. local module = {}
  165.  
  166. module.VERSION = VERSION
  167.  
  168. --[[
  169. /**
  170.  * Move turtle up n times
  171.  *
  172.  * @param int n Number of moves
  173.  */
  174. --]]
  175. module.up = function(n)
  176.   for i = 0, n - 1 do
  177.     move('up')
  178.   end
  179. end
  180.  
  181. --[[
  182. /**
  183.  * Move turtle down n times
  184.  *
  185.  * @param int n Number of moves
  186.  */
  187. --]]
  188. module.down = function(n)
  189.   for i = 0, n - 1 do
  190.     move('down')
  191.   end
  192. end
  193.  
  194. --[[
  195. /**
  196.  * Move turtle forward n times
  197.  *
  198.  * @param int n Number of moves
  199.  */
  200. --]]
  201. module.forward = function(n)
  202.   for i = 0, n - 1 do
  203.     move('forward')
  204.   end
  205. end
  206.  
  207. --[[ Aliases ]]
  208. module.u = function(n)
  209.   module.up(n)
  210. end
  211.  
  212. module.d = function(n)
  213.   module.down(n)
  214. end
  215.  
  216. module.f = function(n)
  217.   module.forward(n)
  218. end
  219.  
  220. return module
Advertisement
Add Comment
Please, Sign In to add comment