Advertisement
thatparadox

melee turtle gps

Jan 18th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. for a,b in pairs(rs.getSides()) do  --find modem and open rednet
  2.   if peripheral.getType(b) == 'modem' then
  3.    rednet.open(b)
  4.    break
  5.   end
  6. end
  7.  
  8. function compass()
  9.         x,y,z = gps.locate()
  10.         while turtle.forward() == false do
  11.                 turtle.turnRight()
  12.         end    
  13.         x1,y1,z1 = gps.locate()
  14.         direction = {x = x - x1, y = y - y1, z = z - z1}
  15.         for k,v in pairs( direction ) do
  16.                 if k == "x" and v == 1 then
  17.                         facing = 1 -- west
  18.                 elseif k == "x" and v == -1 then
  19.                         facing = 3 -- east
  20.                 elseif k == "z" and v == 1 then
  21.                         facing = 2 -- north
  22.                 elseif k == "z" and v == -1 then
  23.                         facing = 0 -- south
  24.                 end
  25.         end
  26.         turtle.back()
  27.         print("compass")
  28.         return facing
  29. end
  30.  
  31. function goTo(mobx, moby, mobz)
  32.     xL,yL,zL = gps.locate()
  33.     --while true do
  34.         xL,yL,zL = gps.locate()
  35.         xD = xL - math.floor(mobx)
  36.         yD = yL - math.floor(moby)
  37.         zD = zL - math.floor(mobz) -1
  38.         if yD > 0 then
  39.             while turtle.down() == false do
  40.                 turtle.forward()
  41.             end
  42.         elseif yD < 0 then
  43.             while turtle.up() == false do
  44.                 turtle.forward()
  45.             end
  46.         elseif zD < 0 then
  47.             currentDir = compass()
  48.             if currentDir ~= 0 then
  49.                 turnTo(0)
  50.             end
  51.             for i = 1,zD*-1 do
  52.                 while turtle.forward() == false do
  53.                     turtle.turnRight()
  54.                     turtle.forward()
  55.                 end
  56.             end
  57.         elseif zD > 0 then
  58.             currentDir = compass()
  59.             if currentDir ~= 2 then
  60.                 turnTo(2)
  61.             end
  62.             for i = 1,zD do
  63.                 while turtle.forward() == false do
  64.                     turtle.turnRight()
  65.                     turtle.forward()
  66.                 end
  67.             end
  68.         elseif xD < 0 then
  69.             currentDir = compass()
  70.             if currentDir ~= 3 then
  71.                 turnTo(3)
  72.             end
  73.             for i = 1,(xD*-1) - 1 do
  74.                 while turtle.forward() == false do
  75.                     turtle.turnRight()
  76.                     turtle.forward()
  77.                 end
  78.             end
  79.             print("stuck")
  80.         elseif xD > 0 then
  81.             currentDir = compass()
  82.             if currentDir ~= 1 then
  83.                 turnTo(1)
  84.             end
  85.             for i = 1,xD - 1 do
  86.                 while turtle.forward() == false do
  87.                     turtle.turnRight()
  88.                     turtle.forward()
  89.                 end
  90.             end
  91.             print("stuck2")
  92.         --else
  93.             --break
  94.         end
  95.     --end
  96. end
  97.                
  98.                
  99. function turnTo(Int)
  100.         if facing ~= Int then
  101.                 local current = facing - Int
  102.                 if current < 0 then
  103.                         for i = 1, math.abs(current) do
  104.                                 turtle.turnRight()
  105.                         end
  106.                 else
  107.                         for i = 1,current do
  108.                         turtle.turnLeft()
  109.                         end
  110.                 end
  111.         end
  112. end
  113.  
  114. function mobCompass()
  115.     x,y,z = gps.locate()
  116.     if x < position.x then
  117.         return 3
  118.     elseif x > position.x then
  119.         return 1
  120.     end
  121. end
  122.  
  123. while true do
  124.     turtle.attack()
  125.     id, position = rednet.receive()
  126.     myx,myy,myz = gps.locate()
  127.     if myx - position.x <= 2 and myx - position.x >= -2 and myz - position.z <= 2 and myz - position.z >= -2 and myy == position.y then
  128.         print("arrived at target")
  129.         compass()
  130.         turnTo(mobCompass())
  131.         turtle.forward()
  132.         turtle.attack()
  133.     else
  134.         goTo(position.x, position.y, position.z)
  135.     end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement