Facebook
From test, 1 Month ago, written in Lua.
Embed
Download Paste or View Raw
Hits: 153
  1. -- Whole thing is still at very early stage of development, a lot might and possibly
  2. -- will change. Currently whole thing is limited to sort of original drifting mode
  3. -- level. Observe things that happen, draw some extra UI, score user,
  4. -- decide when session ends.
  5.  
  6. -- This mode in particular is meant for Track Day with AI Flood on large tracks. Set
  7. -- AIs to draw some slow cars, get yourself that Red Bull monstrousity and try to
  8. -- score some points.
  9.  
  10. -- Key points for future:
  11. -- • Integration with CM’s Quick Drive section, with settings and everything;
  12. -- • These modes might need to be able to force certain CSP parameters — here, for example,
  13. --   it should be AI flood parameters;
  14. -- • To ensure competitiveness, they might also need to collect some data, verify integrity
  15. --   and possibly record short replays?
  16. -- • Remote future: control scene, AIs, spawn extra geometry and so on.
  17.  
  18. -- Event configuration:
  19. local requiredSpeed = 60
  20.  
  21.  
  22. -- This function is called before event activates. Once it returns true, it’ll run:
  23. function script.prepare(dt)
  24.     ac.debug("speed", ac.getCarState(1).speedKmh)
  25.     return ac.getCarState(1).speedKmh > 60
  26. end
  27.  
  28. -- Event state:
  29. local timePassed = 0
  30. local totalScore = 0
  31. local comboMeter = 1
  32. local comboColor = 0
  33. local highestScore = 0
  34. local dangerouslySlowTimer = 0
  35. local carsState = {}
  36. local wheelsWarningTimeout = 0
  37.  
  38. function script.update(dt)
  39.     if timePassed == 0 then
  40.         addMessage("Let’s go!", 0)
  41.     end
  42.  
  43.     local player = ac.getCarState(1)
  44.     if player.engineLifeLeft < 1 then
  45.         if totalScore > highestScore then
  46.             highestScore = math.floor(totalScore)
  47.             ac.sendChatMessage("scored " .. totalScore .. " points.")
  48.         end
  49.         totalScore = 0
  50.         comboMeter = 1
  51.         return
  52.     end
  53.  
  54.     timePassed = timePassed + dt
  55.  
  56.     local comboFadingRate = 0.5 * math.lerp(1, 0.1, math.lerpInvSat(player.speedKmh, 80, 200)) + player.wheelsOutside
  57.     comboMeter = math.max(1, comboMeter - dt * comboFadingRate)
  58.  
  59.     local sim = ac.getSimState()
  60.     while sim.carsCount > #carsState do
  61.         carsState[#carsState + 1] = {}
  62.     end
  63.  
  64.     if wheelsWarningTimeout > 0 then
  65.         wheelsWarningTimeout = wheelsWarningTimeout - dt
  66.     elseif player.wheelsOutside > 0 then
  67.         if wheelsWarningTimeout == 0 then
  68.         end
  69.         addMessage("Car is outside", -1)
  70.         wheelsWarningTimeout = 60
  71.     end
  72.  
  73.     if player.speedKmh < requiredSpeed then
  74.         if dangerouslySlowTimer > 3 then
  75.             if totalScore > highestScore then
  76.                 highestScore = math.floor(totalScore)
  77.                 ac.sendChatMessage("scored " .. totalScore .. " points.")
  78.             end
  79.             totalScore = 0
  80.             comboMeter = 1
  81.         else
  82.             if dangerouslySlowTimer == 0 then
  83.                 addMessage("Too slow!", -1)
  84.             end
  85.         end
  86.         dangerouslySlowTimer = dangerouslySlowTimer + dt
  87.         comboMeter = 1
  88.         return
  89.     else
  90.         dangerouslySlowTimer = 0
  91.     end
  92.  
  93.     for i = 1, ac.getSimState().carsCount do
  94.         local car = ac.getCarState(i)
  95.         local state = carsState[i]
  96.  
  97.         if car.pos:closerToThan(player.pos, 10) then
  98.             local drivingAlong = math.dot(car.look, player.look) > 0.2
  99.             if not drivingAlong then
  100.                 state.drivingAlong = false
  101.  
  102.                 if not state.nearMiss and car.pos:closerToThan(player.pos, 3) then
  103.                     state.nearMiss = true
  104.  
  105.                     if car.pos:closerToThan(player.pos, 2.5) then
  106.                         comboMeter = comboMeter + 3
  107.                         addMessage("Very close near miss!", 1)
  108.                     else
  109.                         comboMeter = comboMeter + 1
  110.                         addMessage("Near miss: bonus combo", 0)
  111.                     end
  112.                 end
  113.             end
  114.  
  115.             if car.collidedWith == 0 then
  116.                 i = i + 1
  117.         addMessage("i", -1)
  118.      if i == 3 then
  119.     state.collided = true
  120.      addMessage("Collision", -1)
  121.               end
  122.                 if totalScore > highestScore then
  123.                     highestScore = math.floor(totalScore)
  124.                     ac.sendChatMessage("scored " .. totalScore .. " points.")
  125.                 end
  126.                 totalScore = 0
  127.                 comboMeter = 1
  128.             end
  129.  
  130.             if not state.overtaken and not state.collided and state.drivingAlong then
  131.                 local posDir = (car.pos - player.pos):normalize()
  132.                 local posDot = math.dot(posDir, car.look)
  133.                 state.maxPosDot = math.max(state.maxPosDot, posDot)
  134.                 if posDot < -0.5 and state.maxPosDot > 0.5 then
  135.                     totalScore = totalScore + math.ceil(10 * comboMeter)
  136.                     comboMeter = comboMeter + 1
  137.                     comboColor = comboColor + 90
  138.                     addMessage("Overtake", comboMeter > 20 and 1 or 0)
  139.                     state.overtaken = true
  140.                 end
  141.             end
  142.         else
  143.             state.maxPosDot = -1
  144.             state.overtaken = false
  145.             state.collided = false
  146.             state.drivingAlong = true
  147.             state.nearMiss = false
  148.         end
  149.     end
  150. end
  151.  
  152. -- For various reasons, this is the most questionable part, some UI. I don’t really like
  153. -- this way though. So, yeah, still thinking about the best way to do it.
  154. local messages = {}
  155. local glitter = {}
  156. local glitterCount = 0
  157.  
  158. function addMessage(text, mood)
  159.     for i = math.min(#messages + 1, 4), 2, -1 do
  160.         messages[i] = messages[i - 1]
  161.         messages[i].targetPos = i
  162.     end
  163.     messages[1] = {text = text, age = 0, targetPos = 1, currentPos = 1, mood = mood}
  164.     if mood == 1 then
  165.         for i = 1, 60 do
  166.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  167.             glitterCount = glitterCount + 1
  168.             glitter[glitterCount] = {
  169.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  170.                 pos = vec2(80, 140) + dir * vec2(40, 20),
  171.                 velocity = dir:normalize():scale(0.2 + math.random()),
  172.                 life = 0.5 + 0.5 * math.random()
  173.             }
  174.         end
  175.     end
  176. end
  177.  
  178. local function updateMessages(dt)
  179.     comboColor = comboColor + dt * 10 * comboMeter
  180.     if comboColor > 360 then
  181.         comboColor = comboColor - 360
  182.     end
  183.     for i = 1, #messages do
  184.         local m = messages[i]
  185.         m.age = m.age + dt
  186.         m.currentPos = math.applyLag(m.currentPos, m.targetPos, 0.8, dt)
  187.     end
  188.     for i = glitterCount, 1, -1 do
  189.         local g = glitter[i]
  190.         g.pos:add(g.velocity)
  191.         g.velocity.y = g.velocity.y + 0.02
  192.         g.life = g.life - dt
  193.         g.color.mult = math.saturate(g.life * 4)
  194.         if g.life < 0 then
  195.             if i < glitterCount then
  196.                 glitter[i] = glitter[glitterCount]
  197.             end
  198.             glitterCount = glitterCount - 1
  199.         end
  200.     end
  201.     if comboMeter > 10 and math.random() > 0.98 then
  202.         for i = 1, math.floor(comboMeter) do
  203.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  204.             glitterCount = glitterCount + 1
  205.             glitter[glitterCount] = {
  206.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  207.                 pos = vec2(195, 75) + dir * vec2(40, 20),
  208.                 velocity = dir:normalize():scale(0.2 + math.random()),
  209.                 life = 0.5 + 0.5 * math.random()
  210.             }
  211.         end
  212.     end
  213. end
  214.  
  215. local speedWarning = 0
  216.     function script.drawUI()
  217.         local uiState = ac.getUiState()
  218.         updateMessages(uiState.dt)
  219.  
  220.         local speedRelative = math.saturate(math.floor(ac.getCarState(1).speedKmh) / requiredSpeed)
  221.         speedWarning = math.applyLag(speedWarning, speedRelative < 1 and 1 or 0, 0.5, uiState.dt)
  222.  
  223.         local colorDark = rgbm(0.4, 0.4, 0.4, 1)
  224.         local colorGrey = rgbm(0.7, 0.7, 0.7, 1)
  225.         local colorAccent = rgbm.new(hsv(speedRelative * 120, 1, 1):rgb(), 1)
  226.         local colorCombo =
  227.             rgbm.new(hsv(comboColor, math.saturate(comboMeter / 10), 1):rgb(), math.saturate(comboMeter / 4))
  228.  
  229.         local function speedMeter(ref)
  230.             ui.drawRectFilled(ref + vec2(0, -4), ref + vec2(180, 5), colorDark, 1)
  231.             ui.drawLine(ref + vec2(0, -4), ref + vec2(0, 4), colorGrey, 1)
  232.             ui.drawLine(ref + vec2(requiredSpeed, -4), ref + vec2(requiredSpeed, 4), colorGrey, 1)
  233.  
  234.             local speed = math.min(ac.getCarState(1).speedKmh, 180)
  235.             if speed > 1 then
  236.                 ui.drawLine(ref + vec2(0, 0), ref + vec2(speed, 0), colorAccent, 4)
  237.             end
  238.         end
  239.  
  240.         ui.beginTransparentWindow("overtakeScore", vec2(100, 100), vec2(400 * 0.5, 400 * 0.5))
  241.         ui.beginOutline()
  242.  
  243.         ui.pushStyleVar(ui.StyleVar.Alpha, 1 - speedWarning)
  244.         ui.pushFont(ui.Font.Main)
  245.         ui.text("Highest Score: " .. highestScore .. " pts")
  246.         ui.popFont()
  247.         ui.popStyleVar()
  248.  
  249.         ui.pushFont(ui.Font.Title)
  250.         ui.text(totalScore .. " pts")
  251.         ui.sameLine(0, 20)
  252.         ui.beginRotation()
  253.         ui.textColored(math.ceil(comboMeter * 10) / 10 .. "x", colorCombo)
  254.         if comboMeter > 20 then
  255.             ui.endRotation(math.sin(comboMeter / 180 * 3141.5) * 3 * math.lerpInvSat(comboMeter, 20, 30) + 90)
  256.         end
  257.         ui.popFont()
  258.         ui.endOutline(rgbm(0, 0, 0, 0.3))
  259.  
  260.         ui.offsetCursorY(20)
  261.         ui.pushFont(ui.Font.Main)
  262.         local startPos = ui.getCursor()
  263.         for i = 1, #messages do
  264.             local m = messages[i]
  265.             local f = math.saturate(4 - m.currentPos) * math.saturate(8 - m.age)
  266.             ui.setCursor(startPos + vec2(20 * 0.5 + math.saturate(1 - m.age * 10) ^ 2 * 50, (m.currentPos - 1) * 15))
  267.             ui.textColored(
  268.                 m.text,
  269.                 m.mood == 1 and rgbm(0, 1, 0, f) or m.mood == -1 and rgbm(1, 0, 0, f) or rgbm(1, 1, 1, f)
  270.             )
  271.         end
  272.         for i = 1, glitterCount do
  273.             local g = glitter[i]
  274.             if g ~= nil then
  275.                 ui.drawLine(g.pos, g.pos + g.velocity * 4, g.color, 2)
  276.             end
  277.         end
  278.         ui.popFont()
  279.         ui.setCursor(startPos + vec2(0, 4 * 30))
  280.  
  281.         ui.pushStyleVar(ui.StyleVar.Alpha, speedWarning)
  282.         ui.setCursorY(0)
  283.         ui.pushFont(ui.Font.Main)
  284.         ui.textColored("Keep speed above " .. requiredSpeed .. " km/h:", colorAccent)
  285.         speedMeter(ui.getCursor() + vec2(-9 * 0.5, 4 * 0.2))
  286.  
  287.         ui.popFont()
  288.         ui.popStyleVar()
  289.  
  290.         ui.endTransparentWindow()
  291.     end