Advertisement
CaptainSpaceCat

Outpost

Jun 6th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. local tArgs = {...}
  2. xDest, yDest, zDest = tonumber(tArgs[1]), tonumber(tArgs[2]), tonumber(tArgs[3])
  3.  
  4. direct = {}
  5. direct[0] = "south"
  6. direct[1] = "west"
  7. direct[2] = "north"
  8. direct[3] = "east"
  9.  
  10. function direction()
  11.   xPos, yPos, zPos = gps.locate()
  12.   if not xPos or not yPos or not zPos then
  13.     print("ERROR: GPS API REQUIRED")
  14.     os.exit(goTo)
  15.   end
  16.   turtle.forward()
  17.   xPos2, yPos2, zPos2 = gps.locate()
  18.   turtle.back()
  19.   if xPos ~= xPos2 then
  20.     if xPos > xPos2 then
  21.       fDir = 1
  22.     else
  23.       fDir = 3
  24.     end
  25.   else
  26.     if zPos > zPos2 then
  27.       fDir = 2
  28.     else
  29.       fDir = 0
  30.     end
  31.   end
  32. end
  33.  
  34. function forward(num)
  35.   if not num then
  36.     num = 1
  37.   end
  38.   for i = 1, num do
  39.     turtle.forward()
  40.     if fDir == 0 then zPos = zPos + 1 end
  41.     if fDir == 2 then zPos = zPos - 1 end
  42.     if fDir == 1 then xPos = xPos - 1 end
  43.     if fDir == 3 then xPos = xPos + 1 end
  44.   end
  45. end
  46.  
  47. function left(num)
  48.   if not num then
  49.     num = 1
  50.   end
  51.   for i = 1, num do
  52.     turtle.turnLeft()
  53.     fDir = fDir - 1
  54.     if fDir < 0 then fDir = 3 end
  55.   end
  56. end
  57.  
  58. function right(num)
  59.   if not num then
  60.     num = 1
  61.   end
  62.   for i = 1, num do
  63.     turtle.turnRight()
  64.     fDir = fDir + 1
  65.     if fDir > 3 then fDir = 0 end
  66.   end
  67. end
  68.  
  69. function up(num)
  70.   if not num then
  71.     num = 1
  72.   end
  73.   for i = 1, num do
  74.     turtle.digUp()
  75.     turtle.up()
  76.     yPos = yPos + 1
  77.     print(yPos)
  78.   end
  79. end
  80.  
  81. function down(num)
  82.   if not num then
  83.     num = 1
  84.   end
  85.   for i = 1, num do
  86.     turtle.digDown()
  87.     turtle.down()
  88.     yPos = yPos - 1
  89.   end
  90. end
  91.  
  92. function goToX()
  93.   if xDest < xPos then
  94.     while fDir ~= 1 do
  95.       left()
  96.     end
  97.   elseif xDest > xPos then
  98.     while fDir ~= 3 do
  99.       left()
  100.     end
  101.   end
  102.   while xPos ~= xDest do
  103.     turtle.dig()
  104.     forward()
  105.   end
  106. end
  107.  
  108. function goToZ()
  109.   if zDest < zPos then
  110.     while fDir ~= 2 do
  111.       left()
  112.     end
  113.   elseif zDest > zPos then
  114.     while fDir ~= 0 do
  115.       left()
  116.     end
  117.   end
  118.   while zPos ~= zDest do
  119.     turtle.dig()
  120.     forward()
  121.   end
  122. end
  123.  
  124. function goToY()
  125.   if yDest < yPos then
  126.     while yDest ~= yPos do
  127.       turtle.digDown()
  128.       turtle.down()
  129.       yPos = yPos - 1
  130.       print(yPos)
  131.     end
  132.   elseif yDest > yPos then
  133.     while yDest ~= yPos do
  134.       turtle.digUp()
  135.       turtle.up()
  136.       yPos = yPos + 1
  137.       print(yPos)
  138.     end
  139.   end
  140. end
  141.  
  142. origin = {gps.locate()}
  143. distance = math.sqrt(math.abs(origin[1] - tArgs[1])^2 + math.abs(origin[2] - tArgs[2])^2 + math.abs(origin[3] - tArgs[3])^2)
  144. fuelReq = (math.abs(origin[1] - tArgs[1]) + math.abs(origin[2] - tArgs[2]) + math.abs(origin[3] - tArgs[3]))*2 + 100
  145. term.setBackgroundColor(colors.black)
  146. term.clear()
  147. term.setCursorPos(1, 1)
  148. print("Distance from Origin: " .. tostring(distance))
  149. print("WARNING: Fuel Required is an ESTIMATE and may not be enough, depending on how high the turtle is going. It is recommended to give the turtle extra fuel just in case.")
  150. local termX, termY = term.getCursorPos()
  151. term.setTextColor(colors.yellow)
  152. term.setCursorPos(1, termY)
  153. term.write("Approximate Fuel Required: " .. tostring(fuelReq))
  154. local w, h = term.getSize()
  155. while fuelReq > turtle.getFuelLevel() do
  156.   term.setCursorPos(1, termY + 1)
  157.   term.clearLine()
  158.   printError("Current Fuel Level: " .. tostring(turtle.getFuelLevel()))
  159.   term.setCursorPos(1, termY + 2)
  160.   term.write("Insert fuel into slot 16")
  161.   turtle.select(16)
  162.   turtle.refuel(1)
  163. end
  164. term.setCursorPos(1, termY + 1)
  165. term.clearLine()
  166. term.setTextColor(colors.green)
  167. print("Current Fuel Level: " .. tostring(turtle.getFuelLevel()))
  168. term.setCursorPos(1, termY + 2)
  169. term.clearLine()
  170. term.setCursorPos(w/2 - 2, termY + 3)
  171. term.setBackgroundColor(colors.gray)
  172. term.setTextColor(colors.white)
  173. term.write("Ready")
  174. while true do
  175.   local click = {os.pullEvent()}
  176.   if click[1] == "mouse_click" and click[2] == 1 and click[3] >= w/2 - 2 and click[3] <= w/2 + 2 and click[4] == termY + 3 then
  177.     term.setCursorPos(w/2 - 2, termY + 3)
  178.     term.setBackgroundColor(colors.blue)
  179.     term.write("Ready")
  180.     sleep(.2)
  181.     break
  182.   end
  183. end
  184. term.setBackgroundColor(colors.black)
  185. term.clear()
  186. term.setCursorPos(1, 1)
  187. print("Traveling to destination...")
  188. direction()
  189. goToY()
  190. goToX()
  191. goToZ()
  192. local outpost = {
  193. [1] = {1, 1, 1, 1, 1},
  194. [2] = {1, 1, 1, 1, 1},
  195. [3] = {1, 1, 0, 1, 1},
  196. [4] = {1, 1, 1, 1, 1},
  197. [5] = {1, 1, 1, 1, 1}
  198. }
  199. local outpost2 = {
  200. [1] = {3, 0, 0, 0, 3},
  201. [2] = {0, 0, 2, 0, 0},
  202. [3] = {3, 0, 0, 0, 3}
  203. }
  204.  
  205. print("Pinpointing building location...")
  206. while not turtle.digDown() do
  207.   down()
  208.   print(yPos)
  209. end
  210. up(21)
  211. left()
  212. forward(2)
  213. left()
  214. forward(2)
  215. left(2)
  216. --shell.run("go up 21 left forward 2 left forward 2 left 2")
  217.  
  218. print("Constructing outpost...")
  219. local turn = "right"
  220. for i, v in pairs(outpost) do
  221.   for k, e in pairs(v) do
  222.     if e > 0 then
  223.       turtle.select(e)
  224.       turtle.placeDown()
  225.     end
  226.     if e == 0 then
  227.       outpostLocation = {xPos, yPos, zPos}
  228.     end
  229.     if k < 5 then
  230.       forward()
  231.     end
  232.   end
  233.   if i < 5 then
  234.     if turn == "right" then
  235.       right()
  236.       forward()
  237.       right()
  238.       turn = "left"
  239.     elseif turn == "left" then
  240.       left()
  241.       forward()
  242.       left()
  243.       turn = "right"
  244.     end
  245.   end
  246. end
  247. left(2)
  248. up()
  249. --shell.run("go left 2 up")
  250. local turn = "right"
  251. for i, v in pairs(outpost2) do
  252.   for k, e in pairs(v) do
  253.     if e == 2 then
  254.       turtle.down()
  255.       turtle.select(e)
  256.       turtle.placeDown()
  257.       turtle.up()
  258.     elseif e == 3 then
  259.       turtle.select(e)
  260.       turtle.placeDown()
  261.     end
  262.     if k < 5 then
  263.       forward()
  264.     end
  265.   end
  266.   if i < 3 then
  267.     if turn == "right" then
  268.       right()
  269.       forward(2)
  270.       right()
  271.       turn = "left"
  272.     elseif turn == "left" then
  273.       left()
  274.       forward(2)
  275.       left()
  276.       turn = "right"
  277.     end
  278.   end
  279. end
  280. forward()
  281.  
  282. print("Traveling to origin...")
  283. xDest, yDest, zDest = origin[1], origin[2], origin[3]
  284. goToY()
  285. goToX()
  286. goToZ()
  287. print("Completed. New outpost at x: " .. tostring(outpostLocation[1]) .. " y: " .. tostring(outpostLocation[2]) .. " z: " .. tostring(outpostLocation[3]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement