Advertisement
Machuga14

ComputerCraft FTB Infinity Build Platform Utilities

Apr 30th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.99 KB | None | 0 0
  1. -- Utilities for my FTB Infinity
  2. -- This is filled with utilities that I wrote for my robot turtles to
  3. -- do things and stuff
  4. -- (BuildPlatformUtilities - bpu)
  5.  
  6. -- This API is defined: http://pastebin.com/2Wk9RVTK
  7. os.loadAPI("MatthewC/tst") -- Replace with new name of the turtle self tracking
  8.  
  9. local transitY = 110 -- The height the turtle will travel at going between its work, and the waystation
  10.  
  11. local refuelX = 999 -- X-location of refueling
  12. local refuelY = 101 -- Y-location of refueling
  13. local refuelZ = 944 -- Z-location of refueling
  14. local refuelOrientation = "E" -- Orientation of refueling
  15.  
  16. local restockX = 1006
  17. local restockY = 101
  18. local restockZ = 944
  19. local restockOrientation = "E" -- Orientation of restocking on building supplies
  20.  
  21. -- The vector to start probing for the next row to build
  22. local findRowStartX = 1003
  23. local findRowStartY = 61
  24. local findRowStartZ = 914
  25. local findRowStartOrientation = "N"
  26. local buildStartOrientation = "E" -- Stores the location the robot starts building in.
  27.                                   --The robot will circle around to the right always. (clockwise)
  28.  
  29. local platformHeight = 8
  30.  
  31. local modem = peripheral.wrap("left") -- Put in the location of the modem here. you BETTER have one.
  32. local modemPort = 25565
  33. local errorPort = 9001 -- The port for reporting errors.
  34. local turtleName = "Turtle1" -- Put the name of the turtle here.
  35.  
  36.  
  37. -- Stores the status of the robot's current state.
  38. -- 1 : Refueling
  39. -- 2 : Restocking
  40. -- 3 : Searching for next location to place blocks (once a location is found, cache the start location, and
  41. -- 4 : Building platform
  42. -- 5 : Return to build location
  43. status = 3
  44.  
  45. -- Caches where we started building
  46. local startBuildX = nil
  47. local startBuildY = nil
  48. local startBuildZ = nil
  49.  
  50. -- Caches where we are currently building
  51. local currentBuildX = nil
  52. local currentBuildY = nil
  53. local currentBuildZ = nil
  54. local currentBuildOrientation = nil
  55.  
  56. fileSaveLoc = "MatthewC/appdata/turtleStatus" -- The directory to save / load the cached values out of
  57.  
  58. -- Build a string that indicates the fuel reserves of the Turtle
  59. function buildStringOfFuel()
  60.   return "Fuel: "..(100 * (turtle.getFuelLevel() / turtle.getFuelLimit())).."%"
  61. end
  62.  
  63. function getFuelPercent() -- Calculate the percentage level of the turte
  64.   return 100 * (turtle.getFuelLevel() / turtle.getFuelLimit())
  65. end
  66.  
  67. function updateStatus()
  68.   --write("Sent a message to modem at "..modemPort.."\r\n")
  69.   modem.transmit(modemPort, 100, tst.getLocationAsString().." "..buildStringOfFuel())
  70. end
  71.  
  72. function sendMessage(message, port)
  73.   modem.transmit(modemPort, port, turtleName.." "..message)
  74. end
  75.  
  76. function sendError(message)
  77.   modem.transmit(modemPort, errorPort, turtleName.." ***ERROR*** "..message)
  78. end
  79.  
  80. function loadCurrentState()
  81.   write("Loading current state\r\n")
  82.   h = fs.open(fileSaveLoc, "r")
  83.  
  84.   if not h then
  85.     write("It appears that we have no file at: "..fileSaveLoc.."\r\n")
  86.     status = 3
  87.     startBuildX = nil
  88.     startBuildY = nil
  89.     startBuildZ = nil
  90.     currentBuildX = nil
  91.     currentBuildY = nil
  92.     currentBuildZ = nil
  93.     currentBuildOrientation = nil
  94.     return
  95.   end
  96.  
  97.   local testStatus = h.readLine()
  98.   if testStatus and testStatus ~= "" then
  99.     status = tonumber(testStatus)
  100.     startBuildX = tonumber(h.readLine())
  101.     startBuildY = tonumber(h.readLine())
  102.     startBuildZ = tonumber(h.readLine())
  103.     currentBuildX = tonumber(h.readLine())
  104.     currentBuildY = tonumber(h.readLine())
  105.     currentBuildZ = tonumber(h.readLine())
  106.     currentBuildOrientation = h.readLine()
  107.  
  108.     if not currentBuildOrientation then
  109.       write("Failed to properly load the orientation")
  110.       sendMessage("Failed to properly load the orientation", 101)
  111.     else
  112.       write("Loaded the file... Orientation = "..currentBuildOrientation)
  113.     end
  114.   end
  115.  
  116.   h.close()
  117. end
  118.  
  119. function purgeInventory() -- Function to purge the entire inventory of a turtle
  120.   for i = 1, 16, 1 do
  121.     turtle.select(i)
  122.     turtle.drop()
  123.   end
  124.  
  125.   turtle.select(1)
  126. end
  127.  
  128. function safeWriteNilVal(valToWrite) -- Safely builds a string for values that may be nil
  129.   if not valToWrite then
  130.     return ""
  131.   end
  132.  
  133.   return valToWrite
  134. end
  135.  
  136. function saveCurrentState()
  137.   -- Build a string with the data to save.
  138.   saveData =
  139.     safeWriteNilVal(status).."\r\n"..
  140.     safeWriteNilVal(startBuildX).."\r\n"..
  141.     safeWriteNilVal(startBuildY).."\r\n"..
  142.     safeWriteNilVal(startBuildZ).."\r\n"..
  143.     safeWriteNilVal(currentBuildX).."\r\n"..
  144.     safeWriteNilVal(currentBuildY).."\r\n"..
  145.     safeWriteNilVal(currentBuildZ).."\r\n"..
  146.     safeWriteNilVal(currentBuildOrientation).."\r\n"
  147.  
  148.   h = fs.open(fileSaveLoc, "w")
  149.   h.write(saveData)
  150.   h.flush() -- I think this is unnecessary, but meh...
  151.   h.close()
  152.  
  153.   if not currentBuildOrientation then
  154.     sendError("Current Orientation is nil. (Machuga14, remove this message in the future)")
  155.   end
  156.  
  157. end
  158.  
  159. -- Helper function; calculates the distance between two vectors.
  160. -- Distance is technically calculated with a "Manhatten Heuristic"
  161. function getDistanceBetweenVectors(x1, y1, z1, x2, y2, z2)
  162.   local xDist = math.abs(x2-x1)
  163.   local yDist = math.abs(y2-y2)
  164.   local zDist = math.abs(z2-z1)
  165.  
  166.   return xDist + yDist + zDist
  167. end
  168.  
  169. function goDirectToVector(x, y, z)
  170.   --sendMessage("Traveling to (x,y,z) of ("..x..", "..y..", "..z..")", 101)
  171.   local xPos, yPos, zPos, orientation = tst.getLocation()
  172.  
  173.   -- This is really lazy, but I don't care... I just need to
  174.   -- Mostly work under stringent conditions.
  175.  
  176.   -- Keeps track if we ever fail to rotate.
  177.   local hasFailed = false
  178.  
  179.   -- Travel to the corresponding Y-Location
  180.   if y > yPos then
  181.     for i = 0, y - yPos - 1, 1 do
  182.       hasFailed = not tst.up()
  183.  
  184.       if hasFailed then
  185.         sendMessage("Failed to travel to Y-Location", 101)
  186.         return false
  187.       end
  188.     end
  189.   elseif y < yPos - 1 then
  190.     for i = 0, yPos - y - 1, 1 do
  191.       hasFailed = not tst.down()
  192.  
  193.       if hasFailed then
  194.         sendMessage("Failed to travel to Y-Location", 101)
  195.         return false
  196.       end
  197.     end
  198.   end
  199.  
  200.   updateStatus()
  201.  
  202.   -- Travel to the corresponding X-Location
  203.   -- Note: X-Location is your East/Westness
  204.   -- Orient to the correct direction
  205.   if x > xPos then
  206.     tst.turnToOrientation("E") -- We need to travel East
  207.   elseif x < xPos then
  208.     tst.turnToOrientation("W") -- We need to travel West
  209.   end
  210.  
  211.   -- Move to the correct X-pos
  212.   for i = 0, math.abs(x-xPos) - 1, 1 do
  213.     hasFailed = not tst.forward()
  214.  
  215.     if hasFailed then
  216.       sendMessage("Failed to travel to Y-Location", 101)
  217.       return false
  218.     end
  219.   end
  220.  
  221.   updateStatus()
  222.  
  223.   -- Travel to the corresponding Z-Location
  224.   -- Note: z-Location is your North/Southness
  225.   -- Orient to the correct direction
  226.   if z < zPos then
  227.     tst.turnToOrientation("N") -- We need to travel North
  228.   elseif z > zPos then
  229.     tst.turnToOrientation("S") -- We need to travel South
  230.   end
  231.  
  232.   -- Move to the correct Z-pos
  233.   for i = 0, math.abs(z-zPos) - 1, 1 do
  234.     hasFailed = not tst.forward()
  235.  
  236.     if hasFailed then
  237.       sendMessage("Failed to travel to Y-Location", 101)
  238.       return false
  239.     end
  240.   end
  241.  
  242.   updateStatus()
  243.   return true
  244. end
  245.  
  246. -- Designed to safely go to a specified vector, by traveling to the y location, then the x location, then the z location.
  247. function travelToVector(x, y, z, direction)
  248.   local hasFailed = false
  249.   local xPos, yPos, zPos, orientation = tst.getLocation()
  250.  
  251.   -- Check to see if we are already at the specified location. If so, let's just turn to the appropriate orientation.
  252.   if x == xPos and y == yPos and z == zPos then
  253.     if direction then
  254.       return tst.turnToOrientation(direction)
  255.     end
  256.    
  257.     return true
  258.   end
  259.  
  260.   -- Travel to y location
  261.   hasFailed = not goDirectToVector(xPos, transitY, zPos)
  262.   if hasFailed then
  263.     sendMessage("Safe Travel To Vector Failed to travel to Y-Location", 101)
  264.     return false
  265.   end
  266.  
  267.   -- Travel to x location
  268.   hasFailed = not goDirectToVector(x, transitY, zPos)
  269.   if hasFailed then
  270.     sendMessage("Safe Travel to Vector failed to travel to X-Location", 101)
  271.     return false
  272.   end
  273.  
  274.   -- Travel to z location
  275.   hasFailed = not goDirectToVector(x, transitY, z)
  276.   if hasFailed then
  277.     return false
  278.   end
  279.  
  280.   -- Travel to y location
  281.   hasFailed = not goDirectToVector(x, y, z)
  282.   if hasFailed then
  283.     sendMessage("Safe Travel to Vector failed to travel to Z-Location", 101)
  284.     return false
  285.   end
  286.  
  287.   if direction then
  288.     hasFailed = not tst.turnToOrientation(direction)
  289.   end
  290.   if hasFailed then
  291.     sendMessage("Safe Travel to Vector failed to Orient to "..direction, 101)
  292.     return false
  293.   end
  294.  
  295.   updateStatus()
  296.   return true
  297. end
  298.  
  299. function headToRefuel() -- Function for returning to refuel
  300.   if getFuelPercent() > 98 then
  301.     return true
  302.   end
  303.  
  304.  
  305.   local xPos, yPos, zPos, orientation = tst.getLocation()
  306.  
  307.   local hasFailed = false
  308.  
  309.   sendMessage("Beginning to refuel", 101)
  310.   turtle.select(1) -- Drop everything in slot 1, since that is what we'll use to refuel.
  311.   turtle.drop()
  312.   hasFailed = not travelToVector(refuelX, refuelY, refuelZ, refuelOrientation)
  313.   if (hasFailed) then
  314.     sendError("Failed to travel to refuel station")
  315.     updateStatus()
  316.     return false
  317.   end
  318.  
  319.   -- Code here to pick up blocks of fuel, and refuel self
  320.   sendMessage("Arrived at refuel depo. Beginning to refuel", 101)
  321.   turtle.select(1)
  322.  
  323.   while getFuelPercent() < 98 do
  324.     while not turtle.suck() do -- keep sucking until suck is successful
  325.     end
  326.  
  327.     if not turtle.refuel() then
  328.       tst.up()
  329.       tst.turnRight()
  330.       purgeInventory()
  331.       tst.turnLeft()
  332.       tst.down()
  333.     end
  334.    
  335.     updateStatus()
  336.   end
  337.  
  338.   sendMessage("Refueled. Returning to original location", 101)
  339.  
  340.   hasFailed = not travelToVector(xPos, yPos, zPos, orientation)
  341.  
  342.   if (hasFailed) then
  343.     sendError("Failed to travel from refuel entry to original location")
  344.     updateStatus()
  345.     return false
  346.   end
  347.  
  348.   sendMessage("Successfully Refueled and Returned.", 101)
  349.   return true
  350. end
  351.  
  352. function checkIfEmpty() -- Returns true if nothing is in the inventory, and false if something is in the inventory
  353.   for i = 1, 16, 1 do
  354.     turtle.select(i)
  355.     if turtle.getItemCount() > 0 then
  356.       return false
  357.     end
  358.   end
  359.  
  360.   return true
  361. end
  362.  
  363. function headToPlatformBuildingChest() -- Function for restocking on supplies for building the platform
  364.   if not checkIfEmpty() then
  365.     return true
  366.   end
  367.  
  368.   local xPos, yPos, zPos, orientation = tst.getLocation()
  369.  
  370.   local hasFailed = false
  371.  
  372.   sendMessage("Beginning to restock on building supplies. Direction ="..orientation, 101)
  373.   local hasFailed = not travelToVector(restockX, restockY, restockZ, restockOrientation)
  374.   if (hasFailed) then
  375.     sendError("Failed to travel to restock station")
  376.     updateStatus()
  377.     return false
  378.   end
  379.  
  380.   -- Code here to pick up blocks from the building supply chest
  381.   for i = 1, 16, 1 do
  382.     turtle.select(i)
  383.    
  384.     while not turtle.suck(64) do -- Keep sucking until a stack is successfully sucked.
  385.     end
  386.   end
  387.  
  388.   sendMessage("Restocked. returning to station.", 101)
  389.  
  390.   hasFailed = not travelToVector(xPos, yPos, zPos, orientation)
  391.  
  392.   if (hasFailed) then
  393.     sendError("Failed to travel from refuel entry to original location")
  394.     updateStatus()
  395.     return false
  396.   end
  397.  
  398.   sendMessage("Successfully Restocked and Returned. Orientation="..orientation, 101)
  399.   return true
  400. end
  401.  
  402. function travelToYLocation(yLocation)
  403.   local xPos, yPos, zPos, orientation = tst.getLocation()
  404.  
  405.   -- Try to move up indefinitely until we hit the appropriate height for the platform.
  406.   while math.abs(yPos - yLocation) ~= 0 do
  407.     if yPos < yLocation then
  408.       local success = tst.up()
  409.       updateStatus()
  410.  
  411.       if not success then
  412.         return false
  413.       end
  414.     else
  415.       local success = tst.down()
  416.       updateStatus()
  417.  
  418.       if not success then
  419.         return false
  420.       end
  421.     end
  422.     xPos, yPos, zPos, orientation = tst.getLocation()
  423.   end
  424.  
  425.   return true -- We have traveled to the desired y-location
  426. end
  427.  
  428. function stubbornTravelToYLocation(yLocation) -- Infinitely try to travel to a y location.
  429.   while not travelToYLocation(yLocation) do end
  430. end
  431.  
  432. function travelToYLocationCacheCurrentLocation(yLocation)
  433.   currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  434.  
  435.   -- Try to move up indefinitely until we hit the appropriate height for the platform.
  436.   while math.abs(currentBuildY - yLocation) ~= 0 do
  437.     if currentBuildY < yLocation then
  438.       local success = tst.up()
  439.       updateStatus()
  440.      
  441.       if not success then
  442.         return false
  443.       end
  444.     else
  445.       local success = tst.down()
  446.       updateStatus()
  447.  
  448.       if not success then
  449.         return false
  450.       end
  451.     end
  452.  
  453.     currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  454.     saveCurrentState()
  455.   end
  456.  
  457.   return true -- We have traveled to the desired y-location
  458. end
  459.  
  460. function findNextBuildRow() -- Find the next row for building
  461.   -- First, travel to the start location
  462.   if not travelToVector(findRowStartX, findRowStartY, findRowStartZ, findRowStartOrientation) then
  463.     sendError("Failed to travel to the start location for finding the next row to build")
  464.     updateStatus()
  465.     return false
  466.   end
  467.  
  468.   -- At this point, we have found our first build location.
  469.   local xPos, yPos, zPos, orientation = tst.getLocation()
  470.   stubbornTravelToYLocation(findRowStartY + platformHeight)  
  471.  
  472.   -- Start "Probing" For the next actual build location.
  473.   local foundX, foundY, foundZ = nil
  474.  
  475.   while not foundX do
  476.     -- 1. Keep trying to move forward indefinately
  477.     while not tst.forward() do end
  478.  
  479.     -- Keep trying to move down until we can't, or until we find the "bottom".
  480.     -- Once we have found the "bottom", we have found our build location.
  481.     if not travelToYLocation(findRowStartY) then
  482.       stubbornTravelToYLocation(findRowStartY + platformHeight)
  483.     else
  484.       if not turtle.detectDown() then -- Return we have found a valid location if and only if there is no block below us.
  485.         foundX, foundY, foundZ = tst.getLocation()
  486.       else
  487.         stubbornTravelToYLocation(findRowStartY + platformHeight)
  488.       end
  489.     end
  490.   end
  491.  
  492.   tst.turnToOrientation(buildStartOrientation)
  493.   return foundX, foundY, foundZ
  494. end
  495.  
  496. function equipNextStack() -- returns true if a stack was equipped. returns false if it was empty.
  497.   write("Equipping next stack\r\n")
  498.   for i = 1, 16, 1 do
  499.     if turtle.getItemCount(i) > 0 then
  500.       turtle.select(i)
  501.       write("Found a stack at inventory["..i.."]")
  502.       return true
  503.     end
  504.   end
  505.  
  506.   write("Detected that the inventory is empty...:(\r\n")
  507.   return false -- This means we will need to restock
  508. end
  509.  
  510. function buildColumn(top) --Builds a column, moving up until the bot gets to top in the ylocation
  511.   currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  512.  
  513.   write("building a column\r\n")
  514.  
  515.   if not equipNextStack() then
  516.     return false
  517.   end
  518.  
  519.   -- Build the column.
  520.   while currentBuildY < top do
  521.     while not turtle.detectDown() and not turtle.placeDown() do -- Keep trying to place a block below the turtle
  522.       if not equipNextStack() then
  523.         return false
  524.       end
  525.     end
  526.     local success = tst.up()
  527.     if not success then
  528.       updateStatus()
  529.       sendError("Seemingly stuck while trying to build column at current location. Waiting 5 seconds")
  530.       os.sleep(5)
  531.     end
  532.  
  533.     currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  534.     saveCurrentState()
  535.   end
  536.  
  537.   return true -- We have built a column to the desired y-location
  538. end
  539.  
  540. function buildRow(finishXLoc, finishYLoc, finishZLoc) -- build the current row the robot is located at.
  541.   currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  542.   saveCurrentState()
  543.   travelToYLocationCacheCurrentLocation(findRowStartY) -- Make sure we are at the bottom of the current row
  544.  
  545.   local failToTurnRightCount = 0 -- Keep track of how many times we have tried to turn right, and failed.
  546.           --If the count exceeds 2 in a row, we have an error, and should return 0.
  547.  
  548.   -- While-Loop to build the current column, move forward, drop, and repeat.
  549.   while true do
  550.     if failToTurnRightCount == 1 then -- We have failed to turn right, so lets try reorienting ourselves left.
  551.       tst.back()
  552.       tst.turnLeft()
  553.       tst.forward()
  554.       tst.down()
  555.       if turtle.detectDown() then -- Return we are finished if we cannot turn left.
  556.         return 0
  557.       end
  558.  
  559.       travelToYLocationCacheCurrentLocation(findRowStartY) -- Make sure we are at the bottom of the current row (since we just moved here...)
  560.     end
  561.  
  562.     if not buildColumn(findRowStartY + platformHeight) then
  563.       return 2 -- We are out of building supplies. Time to restock!
  564.     end
  565.    
  566.     if not tst.forward() then
  567.       return 0 -- We are done; we ran into an obstacle. We must have finished.
  568.     end
  569.  
  570.     currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  571.     saveCurrentState()
  572.    
  573.     -- We have successfully moved forward. If we have successfully moved forward, and our X and Z locations match the finish, we're done.
  574.     if currentBuildX == finishXLoc and currentBuildZ == finishZLoc then
  575.       return 0
  576.     end
  577.  
  578.     -- At this point, we just need to try to travel back down to the findRowStartY, and build another platform
  579.     travelToYLocationCacheCurrentLocation(findRowStartY)
  580.     currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  581.     saveCurrentState()
  582.    
  583.     -- Only try to turn right if we are at the bottom of the column. If we couldn't get there, keep moving forwards.
  584.     if findRowStartY == currentBuildY then
  585.       failToTurnRightCount = 0 -- Indicate that we have successfully turned right, so reset the counter to 0.
  586.       -- Check to the right of the turtle. If it's empty, we need to stay right
  587.       tst.turnRight()
  588.       currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  589.       saveCurrentState()
  590.       tst.down()
  591.       currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  592.       saveCurrentState()
  593.       if turtle.detect() then
  594.         tst.turnLeft()
  595.         currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  596.         saveCurrentState()
  597.       end
  598.       while not tst.up() do
  599.         currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  600.         saveCurrentState()
  601.         os.sleep(3)
  602.       end -- Keep forcing itself to go up
  603.     else
  604.       failToTurnRightCount = failToTurnRightCount + 1
  605.     end -- End If findRowStartY == currentBuildY
  606.   end -- End while loop
  607. end -- End Function
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616. ---------------------------------------------------
  617. --
  618. -- Code to run BuildPlatform here
  619. ---------------------------------------------------
  620. function buildPlatform()
  621. loadCurrentState()
  622. updateStatus()
  623. sendMessage("Booted up; Starting to Build Platform", 101)
  624.  
  625. local xpos, ypos, zpos, orientation = tst.getLocation()
  626. if status == 4 and (xpos ~= currentBuildX or zpos ~= currentBuildZ) then
  627.   write("Anomaly detected: turtlebot not where expected. Traveling to expected location.\r\n")
  628.   write("Current "..tst.getLocationAsString().."\r\n")
  629.   write("Expected Location: x="..safeWriteNilVal(currentBuildX).." y="..safeWriteNilVal(currentBuildY).." z="..safeWriteNilVal(currentBuildZ).." Orientation="..safeWriteNilVal(currentBuildOrientation).."\r\n")
  630.   sendError("Anomaly detected: turtlebot not where expected. Traveling to expected location.")
  631.   sendError("current "..tst.getLocationAsString())
  632.   sendError("Expected Location: x="..currentBuildX.." y="..currentBuildY.." z="..currentBuildZ.." Orientation="..currentBuildOrientation)  
  633.   while not travelToVector(currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation) do -- Keep trying to travel to the appropriate vector.
  634.     updateStatus()
  635.     os.sleep(1)
  636.   end
  637. end
  638.  
  639. while true do
  640.   if status == 1 then
  641.     while not headToRefuel() do end -- Continuously try to refuel
  642.     status = 5
  643.     saveCurrentState()
  644.   elseif status == 2 then
  645.     write("Need to head to platform")
  646.     while not headToPlatformBuildingChest() do end -- Continuously try to restock
  647.     write("Done heading to platform. Setting status to 5.")
  648.     status = 5
  649.     saveCurrentState()
  650.   elseif status == 3 then
  651.     sendMessage("Searching for next build row", 101)
  652.     startBuildX = nil
  653.     startBuildY = nil
  654.     startBuildZ = nil
  655.     currentBuildX = nil
  656.     currentBuildY = nil
  657.     currentBuildZ = nil
  658.     currentBuildOrientation = nil
  659.  
  660.     if getFuelPercent() < 30 then -- If we are at less than 30% fuel, we should refuel.
  661.       status = 1
  662.     else
  663.       while not startBuildX do
  664.         startBuildX, startBuildY, startBuildZ = findNextBuildRow()
  665.         if not startBuildX then
  666.           write("Failed to find next row. trying again.")
  667.           write("Looked for row at: (x,y,z) ("..startBuildX..", "..startBuildY..", "..startBuildZ..")")
  668.           sendMessage("Failed to find next row. trying again.",101)
  669.           sendMessage("Looked for row at: (x,y,z) ("..startBuildX..", "..startBuildY..", "..startBuildZ..")",101)
  670.           os.sleep(5)
  671.         end
  672.       end
  673.      
  674.       write("Found a location to build.")
  675.       sendMessage("Found a location to build.", 101)
  676.       status = 4
  677.     end
  678.     saveCurrentState()
  679.  
  680.   elseif status == 4 then
  681.    
  682.     buildAnswer = buildRow(startBuildX, startBuildY, startBuildZ)
  683.        
  684.     currentBuildX, currentBuildY, currentBuildZ, currentBuildOrientation = tst.getLocation()
  685.  
  686.     if buildAnswer == 0 then
  687.       status = 3 -- We need to restart and work on the next row.
  688.            
  689.       -- mark that we no longer have a current build location.
  690.       currentBuildX = nil
  691.       currentBuildY = nil
  692.       currentBuildZ = nil
  693.       currentBuildOrientation = nil
  694.     elseif buildAnswer == 1 then
  695.       status = 1
  696.     elseif buildAnswer == 2 then
  697.       status = 2
  698.     elseif buildAnswer then
  699.       sendError("encountered an unexpected return from buildRow(): "..buildAnswer, 101)
  700.     else
  701.       sendError("Encountered a nil return from buildRow()")
  702.     end
  703.  
  704.   saveCurrentState()
  705.   elseif status == 5 then
  706.     write("Status is 5\r\n")
  707.     if not currentBuildX then
  708.       status = 3 -- If we do not currently have a valid build location, lets start finding our next build location.
  709.       write("Detected we probably should start a new row\r\n")
  710.     else
  711.       write("Detected we need to travel to our current build location. Doing so\r\n")
  712.       sendMessage("Returning to Stage 4, with a direction of"..currentBuildOrientation, 101)
  713.       while not travelToVector(currentBuildX,currentBuildY,currentBuildZ,currentBuildOrientation) do end
  714.       write("Traveled to curret build location. Continuing operation of building base.")
  715.       status = 4
  716.     end
  717.   saveCurrentState()
  718.   else
  719.     status = 1
  720.     saveCurrentState()
  721.   end -- End if
  722. end -- While true
  723. end -- Function BuildPlatform
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement