Advertisement
thatparadox

WitherBuilder1.1 WIP

Oct 31st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.35 KB | None | 0 0
  1. --Home Coords
  2. xH = -307
  3. yH = 6
  4. zH = -135
  5.  
  6. -- Coords when facing the center of the Wither
  7. xW = -302
  8. yW = 8
  9. zW = -135
  10.  
  11. homeFace = 1 --direction to face when at home coords
  12. witherFace = 2 --direction to face when starts building the wither
  13. recur = false
  14. isStuck = false
  15. count = 0
  16.  
  17. for k,v in pairs(rs.getSides()) do
  18.   if peripheral.getType(v) == "modem" then
  19.    rednet.open(v)
  20.   end
  21. end
  22.  
  23.  
  24. function compass()
  25.         print("Compass")
  26.     compx,compy,compz = gps.locate()
  27.     while turtle.forward() == false do
  28.         turtle.turnRight()
  29.     end
  30.     x1,y1,z1 = gps.locate()
  31.     direction = {compx = compx - x1, compy = compy - y1, compz = compz - z1}
  32.     for k,v in pairs( direction ) do
  33.         if k == "compx" and v == 1 then
  34.             facing = 1 -- west
  35.                         print("Facing West")
  36.         elseif k == "compx" and v == -1 then
  37.             facing = 3 -- east
  38.             print("Facing East")
  39.         elseif k == "compz" and v == 1 then
  40.             facing = 2 -- north
  41.             print("Facing North")
  42.         elseif k == "compz" and v == -1 then
  43.             facing = 0 -- south
  44.             print("Facing South")
  45.         end
  46.     end
  47.     turtle.back()
  48.     return facing
  49. end
  50.  
  51. function goTo(xP,yP,zP)
  52. print("goto function")
  53. lostCount = 0
  54. while true do
  55.     xL,yL,zL = gps.locate()
  56.     x = xL - xP
  57.     y = yL - yP
  58.     z = zL - zP
  59.         print(x.." "..y.." "..z)
  60.     if y > 0.5 and yStuck ~= true then
  61.                 for i = 1,y do
  62.           if turtle.down() == false then
  63.                     yStuck = true
  64.                   end
  65.         end
  66.     elseif y < -0.5 and yStuck ~= true then
  67.                 for i = y,0 do
  68.           if turtle.up() == false then
  69.                     yStuck = true
  70.                   end
  71.         end
  72.     elseif z < -0.5 and zStuck ~= true then
  73.         currentDir = compass()
  74.         if currentDir ~= 0 then
  75.             turnTo(0)
  76.         end
  77.                 for i = z,0 do
  78.           if turtle.forward() == false then
  79.                     zStuck = true
  80.                   end
  81.         end
  82.         print("z less than 1")
  83.     elseif z > 0.5 and zStuck ~= true then
  84.         currentDir = compass()
  85.         if currentDir ~= 2 then
  86.             turnTo(2)
  87.         end
  88.                 for i = 1,z do
  89.           if turtle.forward() == false then
  90.                     zStuck = true
  91.                   end
  92.         end
  93.     elseif x < -0.5 and xStuck ~= true then
  94.         currentDir = compass()
  95.         if currentDir ~= 3 then
  96.             turnTo(3)
  97.         end
  98.                 for i = x,0 do
  99.           if turtle.forward() == false then
  100.                     xStuck = true
  101.                   end
  102.         end
  103.     elseif x > 0.5 and xStuck ~= true then
  104.         currentDir = compass()
  105.         if currentDir ~= 1 then
  106.             turnTo(1)
  107.         end
  108.                 for i = 1,x do
  109.           if turtle.forward() == false then
  110.                     xStuck = true
  111.                   end
  112.         end
  113.     else
  114.           xL,yL,zL = gps.locate()
  115.             if xL == xP and yL == yP and zL == zP then
  116.               print("At destination")
  117.           break
  118.             else
  119.               lostCount = lostCount + 1
  120.               print("Going to random location for the "..lostCount.." time.")
  121.               goRandom()
  122.             end
  123.     end
  124. end
  125. end
  126.  
  127. function goRandom()
  128.   compass()
  129.   turnTo(homeFace)
  130.   isStuck = false
  131.   xS,yS,zS = gps.locate()
  132.   xS = math.floor(math.random(xS-5,xS+5))
  133.   yS = math.floor(math.random(yS-5,yS+5))
  134.   zS = math.floor(math.random(zS-5,zS+5))
  135.   while true do
  136.         print("random")
  137.     xRL,yRL,zRL = gps.locate()
  138.     xR = math.floor(xRL - xS)
  139.     yR = math.floor(yRL - yS)
  140.     zR = math.floor(zRL - zS)
  141.         print(xR.." "..yR.." "..zR)
  142.     if yR > 0.5 and yIsStuck ~= true then
  143.                 for i = 1,y do
  144.           if turtle.down() == false then
  145.                     yIsStuck = true
  146.                   end
  147.         end
  148.     elseif yR < -0.5 and yIsStuck ~= true then
  149.                 for i = y,0 do
  150.           if turtle.up() == false then
  151.                     yIsStuck = true
  152.                   end
  153.         end
  154.     elseif zR < -0.5 and zIsStuck ~= true then
  155.         currentDir = compass()
  156.         if currentDir ~= 0 then
  157.             turnTo(0)
  158.         end
  159.                 for i = z,0 do
  160.           if turtle.forward() == false then
  161.                     zIsStuck = true
  162.                   end
  163.         end
  164.         print("z less than 1")
  165.     elseif zR > 0.5 and zIsStuck ~= true then
  166.         currentDir = compass()
  167.         if currentDir ~= 2 then
  168.             turnTo(2)
  169.         end
  170.                 for i = 1,z do
  171.           if turtle.forward() == false then
  172.                     zIsStuck = true
  173.                   end
  174.         end
  175.     elseif xR < -0.5 and xIsStuck ~= true then
  176.         currentDir = compass()
  177.         if currentDir ~= 3 then
  178.             turnTo(3)
  179.         end
  180.                 for i = x,0 do
  181.           if turtle.forward() == false then
  182.                     xIsStuck = true
  183.                   end
  184.         end
  185.     elseif xR > 0.5 and xIsStuck ~= true then
  186.         currentDir = compass()
  187.         if currentDir ~= 1 then
  188.             turnTo(1)
  189.         end
  190.                 for i = 1,x do
  191.           if turtle.forward() == false then
  192.                     xIsStuck = true
  193.                   end
  194.         end
  195.     else
  196.           print("End Random")
  197.           yIsStuck = false
  198.           zIsStuck = false
  199.           xIsStuck = false
  200.           isStuck = false
  201.           break
  202.     end
  203.   end
  204. end
  205.        
  206. function buildWither()
  207.     turtle.down()
  208.     turtle.select(1)
  209.     turtle.place()
  210.     turtle.up()
  211.     turtle.turnRight()
  212.     turtle.forward()
  213.     turtle.turnLeft()
  214.     turtle.place()
  215.     for i = 1, 2 do
  216.       turtle.turnLeft()
  217.       turtle.forward()
  218.       turtle.turnRight()
  219.       turtle.place()
  220.     end
  221.     turtle.up()
  222.     turtle.select(2)
  223.     turtle.place()
  224.     for i = 1,2 do
  225.       turtle.turnRight()
  226.       turtle.forward()
  227.       turtle.turnLeft()
  228.       turtle.place()
  229.     end
  230.     turtle.down()
  231.     turtle.turnLeft()
  232.     turtle.forward()
  233.     turtle.turnLeft()
  234. end
  235.    
  236. function turnTo(Int)
  237.     if facing ~= Int then
  238.         local current = facing - Int
  239.         if current < 0 then
  240.             for i = 1, math.abs(current) do
  241.                 turtle.turnRight()
  242.             end
  243.         else
  244.             for i = 1,current do
  245.             turtle.turnLeft()
  246.             end
  247.         end
  248.     end
  249. end
  250.  
  251.  
  252. if fs.exists("abort") == false then
  253.   print("Installing abort")
  254.   shell.run("pastebin get eM0db9xZ abort")
  255. end
  256. shell.openTab("abort")
  257. sleep(1)
  258. goTo(xH,yH,zH)
  259. compass()
  260. turnTo(homeFace)
  261. print("home")
  262. while true do
  263.   event, p1, p2, p3 = os.pullEvent("rednet_message")
  264.   if tonumber(p3) == 33 and p2 == "BuildWither" then
  265.     print("got message")
  266.     goTo(xW,yW,zW)
  267.     turnTo(witherFace)
  268.     buildWither()
  269.     goTo(xH,yH,zH)
  270.     rednet.broadcast("WitherBuilt", 33)
  271.     turtle.select(3)
  272.     turtle.refuel(1)
  273.     sleep(60)
  274.     compass()
  275.     turnTo(homeFace)
  276.   else
  277.     sleep(0.05)
  278.   end
  279. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement