Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- rednet.open("top")
- --these poses are set up as x,y,radius
- local waypoints = {
- base = {-50, -926, 200},
- spawn = {227, 90, 150},
- ethans = {170, -90, 60},
- simi = {1000, -270, 60},
- stronghold = {1071, 1400}
- }
- local args = {...}
- local gotoPos = nil
- local plusOrMinusDistance = 300
- if args[1] == "goto" then
- if tonumber(args[2]) then
- gotoPos = {args[2], args[3]}
- elseif type(args[2]) == "string" then
- if not waypoints[args[2]] then
- error("Their is no saved location with the name ", args[2])
- end
- gotoPos = waypoints[args[2]]
- if waypoints[args[2]][3] then
- plusOrMinusDistance = waypoints[args[2]][3]
- end
- else
- error("invalid second argument!")
- end
- else
- error("invalid argument 1!")
- end
- print("Busy...")
- id, message, password = rednet.receive("u2", 3)
- if message == nil then
- error("Error! Didn't recieve player pos table after 3 seconds!")
- end
- local playerPosTable = textutils.unserialize(message)
- print(message)
- local distFromDestination = math.sqrt(math.pow(playerPosTable[1] - gotoPos[1], 2) + math.pow(playerPosTable[3] - gotoPos[2], 2))
- local shipPos = vector.new(playerPosTable[1],0,playerPosTable[3])
- local destinationPos = vector.new(gotoPos[1],0,gotoPos[2])
- local gotoDirectionVec = (shipPos - destinationPos):normalize()
- local playerDirectionVec = vector.new(math.cos(math.rad(playerPosTable[4])), 0, math.sin(math.rad(playerPosTable[4])))
- local dotProduct = gotoDirectionVec:dot(playerDirectionVec)
- local delayForwardEngineStart = os.clock()
- if plusOrMinusDistance < 300 then plusOrMinusDistance = 300 end--clamp
- while distFromDestination > plusOrMinusDistance do
- parallel.waitForAny(
- function()
- id, message, password = rednet.receive("u2", 0.1)
- if id then
- playerPosTable = textutils.unserialize(message)
- distFromDestination = math.sqrt(math.pow(playerPosTable[1] - gotoPos[1], 2) + math.pow(playerPosTable[3] - gotoPos[2], 2))
- shipPos = vector.new(playerPosTable[1],0,playerPosTable[3])
- gotoDirectionVec = (shipPos - destinationPos):normalize()
- playerDirectionVec = vector.new(math.cos(math.rad(playerPosTable[4])), 0, math.sin(math.rad(playerPosTable[4])))
- dotProduct = gotoDirectionVec:dot(playerDirectionVec)
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- if os.clock() - delayForwardEngineStart > 5 then
- redstone.setOutput("back", true)
- end
- if dotProduct < 0.05 then
- local springEnginePower = math.abs(dotProduct) * 8--this num is max turning power. max 0-16
- redstone.setOutput("left", false)
- redstone.setAnalogOutput("right", math.floor(springEnginePower))
- elseif dotProduct > -0.05 then
- local springEnginePower = math.abs(dotProduct) * 8
- redstone.setOutput("right", false)
- redstone.setAnalogOutput("left", math.floor(springEnginePower))
- else
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- end
- print("Distance from destination = ", distFromDestination)
- end
- end,
- function()
- sleep(0.08)
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- end
- )
- end
- redstone.setOutput("back", false)
- redstone.setOutput("right", false)
- redstone.setOutput("left", false)
- print("Destination reached! (plus or minus ",plusOrMinusDistance," blocks xd)")
- --[=[]]
- print("playerDirectionVec = ", playerDirectionVec)
- print("dot product = " ,gotoDirectionVec:dot(playerDirectionVec))
- ]=]
Add Comment
Please, Sign In to add comment