Advertisement
Yhugi

Lightspeed Trackday

May 23rd, 2024 (edited)
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.77 KB | None | 0 0
  1. local player = ac.getCarState(1)
  2. local collisionTimeout = 0
  3. local cutTimeout = 0
  4. local minCollisionHeight = 0.27
  5. local toTeleportMaxSpeedThreshold = 500
  6. local isCarLockedForPlayer = false
  7.  
  8. local pitsCornerA = { x = 549.23, z = 1502.58 }
  9. local pitsCornerB = { x = 696.02, z = 1346.43 }
  10. local pitsMinX, pitsMaxX = math.min(pitsCornerA.x, pitsCornerB.x), math.max(pitsCornerA.x, pitsCornerB.x)
  11. local pitsMinZ, pitsMaxZ = math.min(pitsCornerA.z, pitsCornerB.z), math.max(pitsCornerA.z, pitsCornerB.z)
  12.  
  13. local function isWithinPits(currentPosition)
  14.     return (currentPosition.x >= pitsMinX and currentPosition.x <= pitsMaxX) and
  15.         (currentPosition.z >= pitsMinZ and currentPosition.z <= pitsMaxZ)
  16. end
  17.  
  18. local onCarLockedReceive = ac.OnlineEvent({
  19.     ac.StructItem.key("lightspeedPointsCarLockedReceive"),
  20.     isLocked = ac.StructItem.uint16(),
  21. }, function(sender, message)
  22.     if sender ~= nil then return end
  23.  
  24.     ac.debug("IsLocked", message.isLocked)
  25.     if message.isLocked == 1 then
  26.         isCarLockedForPlayer = true
  27.     end
  28. end)
  29.  
  30. local onCollision = ac.OnlineEvent({
  31.     ac.StructItem.key("lightspeedPointsEnvironmentCollision"),
  32.     Speed = ac.StructItem.int32()
  33. })
  34. local onCut = ac.OnlineEvent({
  35.     ac.StructItem.key("lightspeedPointsLapCut"),
  36.     Speed = ac.StructItem.int32()
  37. })
  38. local onPitLeave = ac.OnlineEvent({
  39.     ac.StructItem.key("lightspeedPointsPitLeave"),
  40.     id = ac.StructItem.int32()
  41. })
  42. local onPitReEntry = ac.OnlineEvent({
  43.     ac.StructItem.key("lightspeedPointsPitReEntry"),
  44.     id = ac.StructItem.int32()
  45. })
  46. local onPitTeleport = ac.OnlineEvent({
  47.     ac.StructItem.key("lightspeedPointsPitTeleport"),
  48.     id = ac.StructItem.int32()
  49. })
  50. local onLapStart = ac.OnlineEvent({
  51.     ac.StructItem.key("lightspeedPointsLapStart"),
  52.     id = ac.StructItem.int32()
  53. })
  54. local onConvoyLeave = ac.OnlineEvent({
  55.     ac.StructItem.key("lightspeedPointsLeavingConvoy"),
  56.     id = ac.StructItem.int32()
  57. })
  58. local onConvoyAlmostFinish = ac.OnlineEvent({
  59.     ac.StructItem.key("lightspeedPointsConvoyAlmostFinish"),
  60.     id = ac.StructItem.int32()
  61. })
  62. local onConvoyAtNorthTurn = ac.OnlineEvent({
  63.     ac.StructItem.key("lightspeedPointsConvoyAtNorthTurn"),
  64.     id = ac.StructItem.int32()
  65. })
  66. local onConvoyAtAirfield = ac.OnlineEvent({
  67.     ac.StructItem.key("lightspeedPointsConvoyAtAirfield"),
  68.     id = ac.StructItem.int32()
  69. })
  70. local onConvoyAtFoxhole = ac.OnlineEvent({
  71.     ac.StructItem.key("lightspeedPointsConvoyAtFoxhole"),
  72.     id = ac.StructItem.int32()
  73. })
  74. local onConvoyAtKallenForest = ac.OnlineEvent({
  75.     ac.StructItem.key("lightspeedPointsConvoyAtKallenForest"),
  76.     id = ac.StructItem.int32()
  77. })
  78. local onConvoyAtWaterMill = ac.OnlineEvent({
  79.     ac.StructItem.key("lightspeedPointsConvoyAtWaterMill"),
  80.     id = ac.StructItem.int32()
  81. })
  82. local onConvoyAtLittleValley = ac.OnlineEvent({
  83.     ac.StructItem.key("lightspeedPointsConvoyAtLittleValley"),
  84.     id = ac.StructItem.int32()
  85. })
  86. local onConvoyAtFirstCarousel = ac.OnlineEvent({
  87.     ac.StructItem.key("lightspeedPointsConvoyAtFirstCarousel"),
  88.     id = ac.StructItem.int32()
  89. })
  90. local onConvoyAtBrunnchen = ac.OnlineEvent({
  91.     ac.StructItem.key("lightspeedPointsConvoyAtBrunnchen"),
  92.     id = ac.StructItem.int32()
  93. })
  94. local onConvoyAtSecondCarousel = ac.OnlineEvent({
  95.     ac.StructItem.key("lightspeedPointsConvoyAtSecondCarousel"),
  96.     id = ac.StructItem.int32()
  97. })
  98.  
  99. local Checkpoint = class("Checkpoint")
  100. function Checkpoint:initialize(position, forward, radius)
  101.     self.position = position
  102.     self.normal = forward:normalize()
  103.     self.radius = radius
  104.     self.d = self.normal:dot(position)
  105. end
  106.  
  107. function Checkpoint:side(position)
  108.     return self.normal:dot(position) - self.d > 0
  109. end
  110.  
  111. function Checkpoint:passed(previous, current)
  112.     local previousSide = self:side(previous)
  113.     local currentSide = self:side(current)
  114.     return current:closerToThan(self.position, self.radius) and previousSide ~= currentSide and previousSide == false
  115. end
  116.  
  117. local pitExitCheckpoint = Checkpoint(vec3(585.79, 91.35, 1475.79), vec3(0, 0, 1), 12)
  118. local pitReEntryCheckpoint = Checkpoint(vec3(585.79, 91.35, 1475.79), vec3(0, 0, -1), 12)
  119. local lapStartCheckpoint = Checkpoint(vec3(170.46, 105.64, 1737.48), vec3(0, 0, 1), 20)
  120. local convoyLeavingCheckpoint = Checkpoint(vec3(545.16, 93.93, 1503.26), vec3(0, 0, 1), 12)
  121. local convoyNearFinishCheckpoint = Checkpoint(vec3(1986.75, 74.97, 374.93), vec3(1, 0, 0), 20)
  122.  
  123. local convoyNorthTurnCheckpoint = Checkpoint(vec3(-625.59, 146.59, 2240.01), vec3(-1, 0, 0), 25)      -- 2km - North Turn
  124. local convoyAirfieldCheckpoint = Checkpoint(vec3(-2177.44, 93.59, 1540.91), vec3(0, 0, -1), 20)       -- 4km - Airfield
  125. local convoyFoxholeCheckpoint = Checkpoint(vec3(-2526.74, 37.83, -16.62), vec3(0, 0, -1), 20)         -- 6km - Foxhole
  126. local convoyKallenForestCheckpoint = Checkpoint(vec3(-1589.83, -31.08, -1700.77), vec3(-1, 0, 0), 20) -- 8km - Kallen Forest
  127. local convoyWaterMillCheckpoint = Checkpoint(vec3(-110.40, -124.35, -2249.71), vec3(0, 0, -1), 20)    -- 10km - Water Mill
  128. local convoyLittleValleyCheckpoint = Checkpoint(vec3(1196.56, -44.42, -1644.69), vec3(1, 0, 0), 20)   -- 12km - Little Valley
  129. local convoyFirstCarouselCheckpoint = Checkpoint(vec3(2099.80, 75.73, -1407.64), vec3(0, 0, 1), 20)   -- 14km - First Carousel
  130. local convoyBrunnchenCheckpoint = Checkpoint(vec3(3439.53, 67.56, -1219.93), vec3(1, 0, 0), 20)       -- 16km - Brunnchen a.k.a Youtube Corner
  131. local convoySecondCarouselCheckpoint = Checkpoint(vec3(1721.88, 68.38, 178.33), vec3(0, 0, 1), 20)    -- 19km - Second Carousel
  132.  
  133. local lastCarPositions = {}
  134.  
  135. function DoUpdate(dt)
  136.     local carIndex = 0
  137.     local car = ac.getCar(carIndex)
  138.     local currentPosition = car.position
  139.  
  140.     if isCarLockedForPlayer then
  141.         physics.setCarNoInput(true)
  142.         physics.setCarFuel(0, 0)
  143.         physics.forceUserBrakesFor(60, 1)
  144.         physics.forceUserClutchFor(60, 1)
  145.         physics.engageGear(0, 1)
  146.         return
  147.     end
  148.  
  149.     lastCarPositions[carIndex] = lastCarPositions[carIndex] or currentPosition:clone()
  150.     local lastPosition = lastCarPositions[carIndex]
  151.     local speed = ((currentPosition - lastPosition):length() / dt)
  152.     if speed > toTeleportMaxSpeedThreshold and (isWithinPits(currentPosition)) then
  153.         onPitTeleport { id = carIndex }
  154.         ac.debug("Teleport Det (Pos)", currentPosition)
  155.         ac.debug("Teleport Det (Speed)", ac.getCarState(carIndex + 1).speedKmh)
  156.     end
  157.  
  158.     local checkpoints = {
  159.         { checkpoint = pitExitCheckpoint,              event = onPitLeave },
  160.         { checkpoint = pitReEntryCheckpoint,           event = onPitReEntry },
  161.         { checkpoint = lapStartCheckpoint,             event = onLapStart },
  162.         { checkpoint = convoyLeavingCheckpoint,        event = onConvoyLeave },
  163.         { checkpoint = convoyNearFinishCheckpoint,     event = onConvoyAlmostFinish },
  164.         { checkpoint = convoyNorthTurnCheckpoint,      event = onConvoyAtNorthTurn },
  165.         { checkpoint = convoyAirfieldCheckpoint,       event = onConvoyAtAirfield },
  166.         { checkpoint = convoyFoxholeCheckpoint,        event = onConvoyAtFoxhole },
  167.         { checkpoint = convoyKallenForestCheckpoint,   event = onConvoyAtKallenForest },
  168.         { checkpoint = convoyWaterMillCheckpoint,      event = onConvoyAtWaterMill },
  169.         { checkpoint = convoyLittleValleyCheckpoint,   event = onConvoyAtLittleValley },
  170.         { checkpoint = convoyFirstCarouselCheckpoint,  event = onConvoyAtFirstCarousel },
  171.         { checkpoint = convoyBrunnchenCheckpoint,      event = onConvoyAtBrunnchen },
  172.         { checkpoint = convoySecondCarouselCheckpoint, event = onConvoyAtSecondCarousel }
  173.     }
  174.     for _, item in ipairs(checkpoints) do
  175.         if item.checkpoint:passed(lastPosition, currentPosition) then
  176.             item.event { id = carIndex }
  177.         end
  178.     end
  179.  
  180.     lastCarPositions[carIndex] = currentPosition:clone()
  181. end
  182.  
  183. function script.update(dt)
  184.     if collisionTimeout > 0 then
  185.         collisionTimeout = collisionTimeout - dt
  186.     elseif player.speedKmh > 0 and player.collisionPosition.y > minCollisionHeight then
  187.         if player.collidedWith >= 0 then
  188.             onCollision { Speed = player.speedKmh }
  189.             collisionTimeout = 1
  190.         end
  191.     end
  192.  
  193.     if cutTimeout > 0 then
  194.         cutTimeout = cutTimeout - dt
  195.     elseif player.speedKmh > 1 and player.wheelsOutside > 3 then
  196.         onCut { Speed = player.speedKmh }
  197.         cutTimeout = 1
  198.     end
  199.  
  200.     --ac.debug("Is Camera in Pits", isWithinPits(ac.getCameraPosition()))
  201.     --ac.debug("Camera Position", ac.getCameraPosition())
  202.     --ac.debug("Camera Forward", ac.getCameraForward())
  203.     --ac.debug("Camera Direction", ac.getCameraDirection())
  204.     --ac.debug("Total Distance KM", player.distanceDrivenTotalKm)
  205.     --ac.debug("Session Distance KM", player.distanceDrivenSessionKm)
  206.     DoUpdate(dt)
  207. end
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement