Advertisement
joebodo

tracker.api

Jun 12th, 2014
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. Tracker = { }
  2.  
  3. function Tracker.getDistance(id)
  4.   for i = 1, 10 do
  5.     Message.send(59999, 'getInfo')
  6.     local _, _, msg, distance = Message.waitForMessage('info', 1)
  7.     local fromId = msg.contents.id
  8.  
  9.     if id == fromId and distance then
  10.       Logger.log('turtle', 'distance: ' .. distance)
  11.       return distance
  12.     end
  13.   end
  14. end
  15.  
  16. local function locate(id, d1, boundingBox)
  17.  
  18.   local function checkBB(boundingBox)
  19.     if boundingBox then
  20.       local heading = turtle.getHeadingInfo(turtle.point.heading)
  21.       local x = turtle.point.x + heading.xd
  22.       local z = turtle.point.z + heading.zd
  23.       if x < boundingBox.ax or x > boundingBox.bx or
  24.          z < boundingBox.az or z > boundingBox.bz then
  25.         return true
  26.       end
  27.     end
  28.     return false
  29.   end
  30.  
  31.   if checkBB(boundingBox) then
  32.     turtle.turnAround()
  33.   end
  34.  
  35.   if d1 == 1 then
  36.     return d1
  37.   end
  38.  
  39.   turtle.forward()
  40.  
  41.   local d2 = Tracker.getDistance(id)
  42.   if not d2 then return end
  43.   if d2 == 1 then return d2 end
  44.  
  45.   if d2 > d1 then
  46.     turtle.turnAround()
  47.   end
  48.  
  49.   d1 = d2
  50.  
  51.   while true do
  52.     if checkBB(boundingBox) then
  53.       break
  54.     end
  55.     turtle.forward()
  56.  
  57.     d2 = Tracker.getDistance(id)
  58.     if not d2 then return end
  59.     if d2 == 1 then return d2 end
  60.  
  61.     if d2 > d1 then
  62.       turtle.back()
  63.       return d1
  64.     end
  65.     d1 = d2
  66.   end
  67.   return d2
  68. end
  69.  
  70. function Tracker.track(id, d1, nozcheck, boundingBox)
  71.  
  72.   d1 = locate(id, d1, boundingBox)
  73.   if not d1 then return end
  74.  
  75.   turtle.turnRight()
  76.  
  77.   d1 = locate(id, d1, boundingBox)
  78.   if not d1 then return end
  79.  
  80.   --if math.floor(d1) == d1 then
  81. d1 = math.floor(d1)
  82.     local y = d1
  83.  
  84.     if not nozcheck then
  85.       turtle.up()
  86.  
  87.       d2 = Tracker.getDistance(id)
  88.       if not d2 then return end
  89.  
  90.       --TL2.down()
  91.  
  92.       y = -math.floor(d2)
  93.       if d1 < d2 then
  94.         y = math.floor(d2)
  95.       end
  96.     end
  97.     return { x = turtle.point.x, z = turtle.point.y, y = y }
  98.   --end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement