Unbox101

Taxi

Apr 28th, 2022 (edited)
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. term.clear()
  2. rednet.open("top")
  3. --these poses are set up as x,y,radius
  4. local waypoints = {
  5.     base = {-50, -926, 200},
  6.     spawn = {227, 90, 150},
  7.     ethans = {170, -90, 60},
  8.     simi = {1000, -270, 60},
  9.     stronghold = {1071, 1400}
  10. }
  11.  
  12. local args = {...}
  13.  
  14. local gotoPos = nil
  15. local plusOrMinusDistance = 300
  16. if args[1] == "goto" then
  17.     if tonumber(args[2]) then
  18.        
  19.         gotoPos = {args[2], args[3]}
  20.        
  21.     elseif type(args[2]) == "string" then
  22.        
  23.         if not waypoints[args[2]] then
  24.             error("Their is no saved location with the name ", args[2])
  25.         end
  26.         gotoPos = waypoints[args[2]]
  27.         if waypoints[args[2]][3] then
  28.             plusOrMinusDistance = waypoints[args[2]][3]
  29.         end
  30.        
  31.     else
  32.        
  33.         error("invalid second argument!")
  34.        
  35.     end
  36. else
  37.     error("invalid argument 1!")
  38. end
  39.  
  40. print("Busy...")
  41. id, message, password = rednet.receive("u2", 3)
  42. if message == nil then
  43.     error("Error! Didn't recieve player pos table after 3 seconds!")
  44. end
  45. local playerPosTable = textutils.unserialize(message)
  46. print(message)
  47. local distFromDestination = math.sqrt(math.pow(playerPosTable[1] - gotoPos[1], 2) + math.pow(playerPosTable[3] - gotoPos[2], 2))
  48. local shipPos = vector.new(playerPosTable[1],0,playerPosTable[3])
  49. local destinationPos = vector.new(gotoPos[1],0,gotoPos[2])
  50. local gotoDirectionVec = (shipPos - destinationPos):normalize()
  51. local playerDirectionVec = vector.new(math.cos(math.rad(playerPosTable[4])), 0, math.sin(math.rad(playerPosTable[4])))
  52. local dotProduct = gotoDirectionVec:dot(playerDirectionVec)
  53.  
  54. local delayForwardEngineStart = os.clock()
  55. if plusOrMinusDistance < 300 then plusOrMinusDistance = 300 end--clamp
  56. while distFromDestination > plusOrMinusDistance do
  57.    
  58.     parallel.waitForAny(
  59.         function()
  60.             id, message, password = rednet.receive("u2", 0.1)
  61.             if id then
  62.                 playerPosTable = textutils.unserialize(message)
  63.                 distFromDestination = math.sqrt(math.pow(playerPosTable[1] - gotoPos[1], 2) + math.pow(playerPosTable[3] - gotoPos[2], 2))
  64.                 shipPos = vector.new(playerPosTable[1],0,playerPosTable[3])
  65.                 gotoDirectionVec = (shipPos - destinationPos):normalize()
  66.                 playerDirectionVec = vector.new(math.cos(math.rad(playerPosTable[4])), 0, math.sin(math.rad(playerPosTable[4])))
  67.                 dotProduct = gotoDirectionVec:dot(playerDirectionVec)
  68.                 redstone.setOutput("right", false)
  69.                 redstone.setOutput("left", false)
  70.                 if os.clock() - delayForwardEngineStart > 5 then
  71.                     redstone.setOutput("back", true)
  72.                 end
  73.                 if dotProduct < 0.05 then
  74.                    
  75.                     local springEnginePower = math.abs(dotProduct) * 8--this num is max turning power. max 0-16
  76.                     redstone.setOutput("left", false)
  77.                     redstone.setAnalogOutput("right", math.floor(springEnginePower))
  78.  
  79.                 elseif dotProduct > -0.05 then
  80.                    
  81.                     local springEnginePower = math.abs(dotProduct) * 8
  82.                     redstone.setOutput("right", false)
  83.                     redstone.setAnalogOutput("left", math.floor(springEnginePower))
  84.                 else
  85.                     redstone.setOutput("right", false)
  86.                     redstone.setOutput("left", false)
  87.                 end
  88.                
  89.                 print("Distance from destination = ", distFromDestination)
  90.             end
  91.         end,
  92.         function()
  93.             sleep(0.08)
  94.             redstone.setOutput("right", false)
  95.             redstone.setOutput("left", false)
  96.         end
  97.     )
  98.    
  99.    
  100.    
  101.    
  102.    
  103.    
  104. end
  105. redstone.setOutput("back", false)
  106. redstone.setOutput("right", false)
  107. redstone.setOutput("left", false)
  108. print("Destination reached! (plus or minus ",plusOrMinusDistance," blocks xd)")
  109. --[=[]]
  110. print("playerDirectionVec = ", playerDirectionVec)
  111. print("dot product = " ,gotoDirectionVec:dot(playerDirectionVec))
  112.  
  113. ]=]
  114.  
  115.  
Add Comment
Please, Sign In to add comment