thatparadox

WitherTurtleGPS

Oct 28th, 2015
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Home Coords
  2. xH = 1754
  3. yH = 5
  4. zH = -1040
  5.  
  6. -- Coords when facing the center of the Wither
  7. xW = 1754
  8. yW = 5
  9. zW = -1030
  10.  
  11. homeFace = 1 --direction to face when at home coords
  12. witherFace = 2 --direction to face when starts building the wither
  13.  
  14. for k,v in pairs(rs.getSides()) do
  15.   if peripheral.getType(v) == "modem" then
  16.    rednet.open(v)
  17.   end
  18. end
  19.  
  20.  
  21. function compass()
  22.         print("Compass")
  23.     compx,compy,compz = gps.locate()
  24.     while turtle.forward() == false do
  25.         turtle.turnRight()
  26.     end
  27.     x1,y1,z1 = gps.locate()
  28.     direction = {compx = compx - x1, compy = compy - y1, compz = compz - z1}
  29.     for k,v in pairs( direction ) do
  30.         if k == "compx" and v == 1 then
  31.             facing = 1 -- west
  32.                         print("Facing West")
  33.         elseif k == "compx" and v == -1 then
  34.             facing = 3 -- east
  35.             print("Facing East")
  36.         elseif k == "compz" and v == 1 then
  37.             facing = 2 -- north
  38.             print("Facing North")
  39.         elseif k == "compz" and v == -1 then
  40.             facing = 0 -- south
  41.             print("Facing South")
  42.         end
  43.     end
  44.     turtle.back()
  45.     return facing
  46. end
  47.  
  48. function goTo(xP,yP,zP)
  49. print("goto function")
  50. while true do
  51.     xL,yL,zL = gps.locate()
  52.     x = xL - xP
  53.     y = yL - yP
  54.     z = zL - zP
  55.         print(x.." "..y.." "..z)
  56.     if y > 0.5 then
  57.                 for i = 1,y do
  58.           turtle.down()
  59.         end
  60.     elseif y < -0.5 then
  61.                 for i = y,0 do
  62.           turtle.up()
  63.         end
  64.     elseif z < -0.5 then
  65.         currentDir = compass()
  66.         if currentDir ~= 0 then
  67.             turnTo(0)
  68.         end
  69.                 for i = z,0 do
  70.           turtle.forward()
  71.         end
  72.         print("z less than 1")
  73.     elseif z > 0.5 then
  74.         currentDir = compass()
  75.         if currentDir ~= 2 then
  76.             turnTo(2)
  77.         end
  78.                 for i = 1,z do
  79.           turtle.forward()
  80.         end
  81.     elseif x < -0.5 then
  82.         currentDir = compass()
  83.         if currentDir ~= 3 then
  84.             turnTo(3)
  85.         end
  86.                 for i = x,0 do
  87.           turtle.forward()
  88.         end
  89.     elseif x > 0.5 then
  90.         currentDir = compass()
  91.         if currentDir ~= 1 then
  92.             turnTo(1)
  93.         end
  94.                 for i = 1,x do
  95.           turtle.forward()
  96.         end
  97.     else
  98.         break
  99.     end
  100. end
  101. end
  102.        
  103. function buildWither()
  104.     turtle.down()
  105.     turtle.select(1)
  106.     turtle.place()
  107.     turtle.up()
  108.     turtle.turnRight()
  109.     turtle.forward()
  110.     turtle.turnLeft()
  111.     turtle.place()
  112.     for i = 1, 2 do
  113.       turtle.turnLeft()
  114.       turtle.forward()
  115.       turtle.turnRight()
  116.       turtle.place()
  117.     end
  118.     turtle.up()
  119.     turtle.select(2)
  120.     turtle.place()
  121.     for i = 1,2 do
  122.       turtle.turnRight()
  123.       turtle.forward()
  124.       turtle.turnLeft()
  125.       turtle.place()
  126.     end
  127.     turtle.down()
  128.     turtle.turnLeft()
  129.     turtle.forward()
  130.     turtle.turnLeft()
  131. end
  132.    
  133. function turnTo(Int)
  134.     if facing ~= Int then
  135.         local current = facing - Int
  136.         if current < 0 then
  137.             for i = 1, math.abs(current) do
  138.                 turtle.turnRight()
  139.             end
  140.         else
  141.             for i = 1,current do
  142.             turtle.turnLeft()
  143.             end
  144.         end
  145.     end
  146. end
  147.  
  148.  
  149. if fs.exists("abort") == false then
  150.   print("Installing abort")
  151.   shell.run("pastebin get eM0db9xZ abort")
  152. end
  153. shell.openTab("abort")
  154. sleep(1)
  155. goTo(xH,yH,zH)
  156. compass()
  157. turnTo(homeFace)
  158. print("home")
  159. while true do
  160.   event, p1, p2, p3 = os.pullEvent("rednet_message")
  161.   if tonumber(p3) == 33 and p2 == "BuildWither" then
  162.     print("got message")
  163.     goTo(xW,yW,zW)
  164.     turnTo(witherFace)
  165.     buildWither()
  166.     goTo(xH,yH,zH)
  167.     rednet.broadcast("WitherBuilt", 33)
  168.     turtle.select(3)
  169.     turtle.refuel(1)
  170.     sleep(60)
  171.     compass()
  172.     turnTo(homeFace)
  173.   else
  174.     sleep(0.05)
  175.   end
  176. end
RAW Paste Data