Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getDiffX()
- return diffX
- end
- function getDiffY()
- return diffY
- end
- function getDiffZ()
- return diffZ
- end
- function getNegX()
- return negX
- end
- function getNegY()
- return negY
- end
- function getNegZ()
- return negZ
- end
- function getFuelCost()
- return fuelcost
- end
- function resetFuelCost()
- fuelcost = 0
- end
- function getPath()
- return path
- end
- fuelcost = 0
- -- Function to move the Turtle to a specific GPS coordinate and predict the path
- function setVariables(targetX, targetY, targetZ, newPath)
- -- Calculate the distance to the target coordinate
- turtle.equipLeft()
- deltaX, deltaY, deltaZ = gps.locate()
- turtle.equipLeft()
- diffX = targetX - deltaX
- diffY = 250 - targetY
- diffZ = targetZ - deltaZ
- negX = diffX / math.abs(diffX)
- negY = diffY / math.abs(diffY)
- negZ = diffZ / math.abs(diffZ)
- end
- -- Function to move the Turtle to a specific GPS coordinate and predict the path
- function setMineVariables(targetX, targetY, targetZ, newPath)
- -- Calculate the distance to the target coordinate
- turtle.equipLeft()
- deltaX, deltaY, deltaZ = gps.locate()
- turtle.equipLeft()
- diffX = targetX - deltaX
- diffY = deltaY - targetY
- diffZ = targetZ - deltaZ
- negX = diffX / math.abs(diffX)
- negY = diffY / math.abs(diffY)
- negZ = diffZ / math.abs(diffZ)
- end
- -- Since a prediction doesn't move a turtle, gps.locate does not give a new destination cord after simulating moving.
- -- hence the creation of this function
- function setVariablesTwo(targetX, targetY, targetZ, newDeltaX, newDeltaY, newDeltaZ, newPath)
- -- Calculate the distance to the target coordinate
- deltaX = newDeltaX
- deltaY = newDeltaY
- deltaZ = newDeltaZ
- diffX = targetX - deltaX
- diffY = 250 - targetY
- diffZ = targetZ - deltaZ
- negX = diffX / math.abs(diffX)
- negY = diffY / math.abs(diffY)
- negZ = diffZ / math.abs(diffZ)
- end
- -- Predict and store the path coordinates in the 'path' table
- function predictToTravelHeight(deltaY, diffY, negY, path)
- --turtle predicts ascending to safe distance
- if deltaY < 250 then
- travelheight = 250 - deltaY
- for increment = 1, travelheight do
- theight = deltaY + (increment * negY)
- table.insert(path, {deltaX, theight, deltaZ})
- fuelcost = fuelcost + 1
- end
- elseif deltaY > 250 then
- travelheight = deltaY - 250
- for increment = 1, travelheight do
- theight = deltaY - (increment * negY)
- table.insert(path, {deltaX, theight, deltaZ})
- fuelcost = fuelcost + 1
- end
- end
- end
- function predictX(deltaX, diffX, negX, path)
- --moves to x cord
- for increment = 1, math.abs(diffX) do
- local x = deltaX + (increment * negX)
- table.insert(path, {x, theight, deltaZ})
- fuelcost = fuelcost + 1
- end
- end
- function predictZ(deltaZ, diffZ, negZ, path)
- --moves to z cord
- for increment = 1, math.abs(diffZ) do
- local z = deltaZ + (increment * negZ)
- table.insert(path, {x, theight, z})
- fuelcost = fuelcost + 1
- end
- end
- function predictY(deltaY, diffY, negY, path)
- --descends to y position
- for increment = 1, math.abs(diffY) do
- local y = 250 - (increment * negY)
- table.insert(path, {x, y, z})
- fuelcost = fuelcost + 1
- end
- end
- -- Function to write a table to a file on the Turtle
- function writeTableToFile(tableData, filename)
- local file = fs.open(filename, "w")
- if file then
- file.write(textutils.serialize(tableData))
- file.close() -- Close the file when done
- return true
- else
- return false
- end
- end
- function finalWrite(path)
- -- Define the filename
- local filename = "path.txt"
- --function to write the 'path' table to a file
- if writeTableToFile(path, filename) then
- rednet.send(11, path)
- else
- print("Failed to write predicted path to the file.")
- end
- end
- function predict(targetX, targetY, targetZ, newPath)
- -- Initialize a table to store the predicted path
- if newPath[1] == 1 then
- print("NIL")
- path = {}
- else
- path = newPath
- end
- targetY = targetY - 1
- -- Call the function to predict the path
- setVariables(targetX, targetY, targetZ, newPath)
- predictToTravelHeight(deltaY, diffY, negY, path)
- predictX(deltaX, diffX, negX, path)
- predictZ(deltaZ, diffZ, negZ, path)
- predictY(deltaY, diffY, negY, path)
- end
- function predictMine(targetX, targetY, targetZ, newPath)
- -- Initialize a table to store the predicted path
- if newPath[1] == 1 then
- print("NIL")
- path = {}
- else
- path = newPath
- end
- targetY = targetY - 1
- -- Call the function to predict the path
- setMineVariables(targetX, targetY, targetZ, newPath)
- predictX(deltaX, diffX, negX, path)
- predictZ(deltaZ, diffZ, negZ, path)
- predictY(deltaY, diffY, negY, path)
- end
- -- takes 2 cords, the first being the first target, and the second being the next target
- --for a round trip put in the target location for target variables, and the current location for the delta variables
- function predictTwo(targetX, targetY, targetZ, newDeltaX, newDeltaY, newDeltaZ, newPath)
- -- Initialize a table to store the predicted path
- if newPath[1] == 1 then
- print("NIL")
- path = {}
- else
- path = newPath
- end
- targetY = targetY - 1
- -- Call the function to predict the path
- setVariables(targetX, targetY, targetZ, newPath)
- predictToTravelHeight(deltaY, diffY, negY, path)
- predictX(deltaX, diffX, negX, path)
- predictZ(deltaZ, diffZ, negZ, path)
- predictY(deltaY, diffY, negY, path)
- setVariablesTwo(newDeltaX, newDeltaY, newDeltaZ, targetX, targetY, targetZ, path)
- predictToTravelHeight(deltaY, diffY, negY, path)
- predictX(deltaX, diffX, negX, path)
- predictZ(deltaZ, diffZ, negZ, path)
- predictY(deltaY, diffY, negY, path)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement