Advertisement
CelticCoder

predictPath

Oct 3rd, 2023 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.74 KB | None | 0 0
  1. function getDiffX()
  2.   return diffX
  3. end
  4.  
  5. function getDiffY()
  6.   return diffY
  7. end
  8.  
  9. function getDiffZ()
  10.   return diffZ
  11. end
  12.  
  13. function getNegX()
  14.   return negX
  15. end
  16.  
  17. function getNegY()
  18.   return negY
  19. end
  20.  
  21. function getNegZ()
  22.   return negZ
  23. end
  24.  
  25. function getFuelCost()
  26.     return fuelcost
  27. end
  28.  
  29. function resetFuelCost()
  30.     fuelcost = 0
  31. end
  32.  
  33. function getPath()
  34.     return path
  35. end
  36.  
  37. fuelcost = 0
  38. -- Function to move the Turtle to a specific GPS coordinate and predict the path
  39. function setVariables(targetX, targetY, targetZ, newPath)
  40.     -- Calculate the distance to the target coordinate
  41.     turtle.equipLeft()
  42.     deltaX, deltaY, deltaZ = gps.locate()
  43.     turtle.equipLeft()
  44.     diffX = targetX - deltaX
  45.     diffY = 250 - targetY
  46.     diffZ = targetZ - deltaZ
  47.     negX = diffX / math.abs(diffX)
  48.     negY = diffY / math.abs(diffY)
  49.     negZ = diffZ / math.abs(diffZ)
  50. end
  51.  
  52. -- Function to move the Turtle to a specific GPS coordinate and predict the path
  53. function setMineVariables(targetX, targetY, targetZ, newPath)
  54.     -- Calculate the distance to the target coordinate
  55.     turtle.equipLeft()
  56.     deltaX, deltaY, deltaZ = gps.locate()
  57.     turtle.equipLeft()
  58.     diffX = targetX - deltaX
  59.     diffY = deltaY - targetY
  60.     diffZ = targetZ - deltaZ
  61.     negX = diffX / math.abs(diffX)
  62.     negY = diffY / math.abs(diffY)
  63.     negZ = diffZ / math.abs(diffZ)
  64. end
  65.  
  66. -- Since a prediction doesn't move a turtle, gps.locate does not give a new destination cord after simulating moving.
  67. -- hence the creation of this function
  68. function setVariablesTwo(targetX, targetY, targetZ, newDeltaX, newDeltaY, newDeltaZ, newPath)
  69.     -- Calculate the distance to the target coordinate
  70.     deltaX = newDeltaX
  71.     deltaY = newDeltaY
  72.     deltaZ = newDeltaZ
  73.     diffX = targetX - deltaX
  74.     diffY = 250 - targetY
  75.     diffZ = targetZ - deltaZ
  76.     negX = diffX / math.abs(diffX)
  77.     negY = diffY / math.abs(diffY)
  78.     negZ = diffZ / math.abs(diffZ)
  79. end
  80.  
  81.     -- Predict and store the path coordinates in the 'path' table
  82. function predictToTravelHeight(deltaY, diffY, negY, path)
  83.         --turtle predicts ascending to safe distance
  84.     if deltaY < 250 then
  85.         travelheight = 250 - deltaY
  86.         for increment = 1, travelheight do
  87.             theight = deltaY + (increment * negY)
  88.             table.insert(path, {deltaX, theight, deltaZ})
  89.             fuelcost = fuelcost + 1
  90.         end
  91.     elseif deltaY > 250 then
  92.         travelheight = deltaY - 250
  93.         for increment = 1, travelheight do
  94.             theight = deltaY - (increment * negY)
  95.             table.insert(path, {deltaX, theight, deltaZ})
  96.             fuelcost = fuelcost + 1
  97.         end
  98.     end
  99. end
  100.  
  101. function predictX(deltaX, diffX, negX, path)
  102.         --moves to x cord
  103.     for increment = 1, math.abs(diffX) do
  104.         local x = deltaX + (increment * negX)
  105.         table.insert(path, {x, theight, deltaZ})
  106.         fuelcost = fuelcost + 1
  107.     end
  108. end
  109.  
  110. function predictZ(deltaZ, diffZ, negZ, path)
  111.         --moves to z cord
  112.     for increment = 1, math.abs(diffZ) do
  113.         local z = deltaZ + (increment * negZ)
  114.         table.insert(path, {x, theight, z})
  115.         fuelcost = fuelcost + 1
  116.     end
  117. end
  118.  
  119. function predictY(deltaY, diffY, negY, path)
  120.         --descends to y position
  121.     for increment = 1, math.abs(diffY) do
  122.         local y = 250 - (increment * negY)
  123.         table.insert(path, {x, y, z})
  124.         fuelcost = fuelcost + 1
  125.     end
  126. end
  127.  
  128. -- Function to write a table to a file on the Turtle
  129. function writeTableToFile(tableData, filename)
  130.     local file = fs.open(filename, "w")
  131.     if file then
  132.         file.write(textutils.serialize(tableData))
  133.         file.close() -- Close the file when done
  134.         return true
  135.     else
  136.         return false
  137.     end
  138. end
  139. function finalWrite(path)
  140.     -- Define the filename
  141.     local filename = "path.txt"
  142.    
  143.     --function to write the 'path' table to a file
  144.     if writeTableToFile(path, filename) then
  145.         rednet.send(11, path)
  146.     else
  147.         print("Failed to write predicted path to the file.")
  148.     end
  149. end
  150.  
  151. function predict(targetX, targetY, targetZ, newPath)
  152.     -- Initialize a table to store the predicted path
  153.     if newPath[1] == 1 then
  154.         print("NIL")
  155.         path = {}
  156.     else
  157.         path = newPath
  158.     end
  159.     targetY = targetY - 1
  160.     -- Call the function to predict the path
  161.     setVariables(targetX, targetY, targetZ, newPath)
  162.     predictToTravelHeight(deltaY, diffY, negY, path)
  163.     predictX(deltaX, diffX, negX, path)
  164.     predictZ(deltaZ, diffZ, negZ, path)
  165.     predictY(deltaY, diffY, negY, path)
  166. end
  167.  
  168. function predictMine(targetX, targetY, targetZ, newPath)
  169.     -- Initialize a table to store the predicted path
  170.     if newPath[1] == 1 then
  171.         print("NIL")
  172.         path = {}
  173.     else
  174.         path = newPath
  175.     end
  176.     targetY = targetY - 1
  177.     -- Call the function to predict the path
  178.     setMineVariables(targetX, targetY, targetZ, newPath)
  179.     predictX(deltaX, diffX, negX, path)
  180.     predictZ(deltaZ, diffZ, negZ, path)
  181.     predictY(deltaY, diffY, negY, path)
  182. end
  183.  
  184. -- takes 2 cords, the first being the first target, and the second being the next target
  185. --for a round trip put in the target location for target variables, and the current location for the delta variables
  186. function predictTwo(targetX, targetY, targetZ, newDeltaX, newDeltaY, newDeltaZ, newPath)
  187.     -- Initialize a table to store the predicted path
  188.     if newPath[1] == 1 then
  189.         print("NIL")
  190.         path = {}
  191.     else
  192.         path = newPath
  193.     end
  194.     targetY = targetY - 1
  195.     -- Call the function to predict the path
  196.     setVariables(targetX, targetY, targetZ, newPath)
  197.     predictToTravelHeight(deltaY, diffY, negY, path)
  198.     predictX(deltaX, diffX, negX, path)
  199.     predictZ(deltaZ, diffZ, negZ, path)
  200.     predictY(deltaY, diffY, negY, path)
  201.     setVariablesTwo(newDeltaX, newDeltaY, newDeltaZ, targetX, targetY, targetZ, path)
  202.     predictToTravelHeight(deltaY, diffY, negY, path)
  203.     predictX(deltaX, diffX, negX, path)
  204.     predictZ(deltaZ, diffZ, negZ, path)
  205.     predictY(deltaY, diffY, negY, path)
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement