Advertisement
jhnphm

Turtle

Jan 20th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- load the API
  2. os.loadAPI("ocs/apis/sensor")
  3. -- wrap the sensor
  4. prox = sensor.wrap("right")
  5.  
  6. local NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
  7. facing = -1
  8.  
  9. function initFacing()
  10.     if turtle.detect() then
  11.         turtle.dig()
  12.     end
  13.     local me = prox.getTargetDetails("jhnphm")
  14.     local pos = me.Position
  15.     local x = pos.X
  16.     local z = pos.Z
  17.     turtle.forward()
  18.     me = prox.getTargetDetails("jhnphm")
  19.     pos = me.Position
  20.     x = pos.X-x
  21.     z = pos.Z-z
  22.  
  23.     if math.abs(x) > math.abs(z) then
  24.         if x > 0 then
  25.             facing = EAST
  26.         else
  27.             facing = WEST
  28.         end
  29.     else
  30.         if z > 0 then
  31.             facing = SOUTH
  32.         else
  33.             facing = NORTH
  34.         end
  35.     end
  36.     turtle.turnLeft()
  37.     turtle.turnLeft()
  38.     turtle.forward()
  39.     turtle.turnLeft()
  40.     turtle.turnLeft()
  41. end
  42. function turnLeft()
  43.     facing = (facing - 1) % 4
  44.     turtle.turnLeft()
  45. end
  46. function turnRight()
  47.     facing = (facing + 1) % 4
  48.     turtle.turnRight()
  49. end
  50. function turnTo(heading)
  51.     if heading == NORTH then
  52.         if facing == EAST then
  53.             turnLeft()
  54.         elseif facing == SOUTH then
  55.             turnLeft()
  56.             turnLeft()
  57.         elseif facing == WEST then
  58.             turnRight()
  59.         end
  60.     elseif heading == EAST then
  61.         if facing == SOUTH then
  62.             turnLeft()
  63.         elseif facing == WEST then
  64.             turnLeft()
  65.             turnLeft()
  66.         elseif facing == NORTH then
  67.             turnRight()
  68.         end
  69.     elseif heading == SOUTH then
  70.         if facing == WEST then
  71.             turnLeft()
  72.         elseif facing == NORTH then
  73.             turnLeft()
  74.             turnLeft()
  75.         elseif facing == EAST then
  76.             turnRight()
  77.         end
  78.     elseif heading == WEST then
  79.         if facing == NORTH then
  80.             turnLeft()
  81.         elseif facing == EAST then
  82.             turnLeft()
  83.             turnLeft()
  84.         elseif facing == SOUTH then
  85.             turnRight()
  86.         end
  87.     end
  88. end
  89.  
  90.  
  91. function go(x,y,z)
  92.     print("Going to: "..x..","..y..","..z)
  93.     local i
  94.     if math.abs(x) > math.abs(z) then
  95.         if math.floor(x) > 0 then
  96.             turnTo(EAST)
  97.             print("Turning East")
  98.         elseif math.floor(x) < 0 then
  99.             print("Turning West")
  100.             turnTo(WEST)
  101.         end
  102.         print("Moving...")
  103.         turtle.forward()
  104.     else
  105.         if math.floor(z) > 0 then
  106.             print("Turning South")
  107.             turnTo(SOUTH)
  108.         elseif math.floor(z) < 0 then
  109.             print("Turning North")
  110.             turnTo(NORTH)
  111.         end
  112.         if turtle.detect() then
  113.             return
  114.         end
  115.         print("Moving...")
  116.         turtle.forward()
  117.     end
  118.     if math.floor(y) > 0 then
  119.         if turtle.detectUp() then
  120.            return
  121.         end
  122.         print("Moving up...")
  123.         turtle.up()
  124.     elseif math.floor(y) < 0 then
  125.         if turtle.detectDown() then
  126.             return
  127.         end
  128.         print("Moving down...")
  129.         turtle.down()
  130.     end
  131. end
  132.  
  133.  
  134. function distance(pos)
  135.   local xd = pos.X
  136.   local yd = pos.Y
  137.   local zd = pos.Z
  138.   return math.sqrt(xd*xd + yd*yd + zd*zd)
  139. end
  140.  
  141. initFacing()
  142.  
  143. while true do
  144.     local me = prox.getTargetDetails("jhnphm")
  145.     local pos = me.Position
  146.     local dist = distance(pos)
  147.      print("Position:" ..me.Position.X..','..me.Position.Y..","..me.Position.Z)
  148.     if dist > 4 then
  149.         print("Dist: "..dist)
  150.         go(pos.X, pos.Y, pos.Z)
  151.         dist = distance(pos)
  152.     end
  153.     if dist > 20 then
  154.         break
  155.     end
  156. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement