Facebook
From test, 1 Month ago, written in Lua.
Embed
Download Paste or View Raw
Hits: 142
  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  then
  117.        addMessage("i", -1)
  118.                
  119.      
  120.      if i == 3
  121.     state.collided = true then
  122.      addMessage("Collision", -1)
  123.               end
  124.                 if totalScore > highestScore then
  125.                     highestScore = math.floor(totalScore)
  126.                     ac.sendChatMessage("scored " .. totalScore .. " points.")
  127.                 end
  128.                 totalScore = 0
  129.                 comboMeter = 1
  130.             end
  131.  
  132.             if not state.overtaken and not state.collided and state.drivingAlong then
  133.                 local posDir = (car.pos - player.pos):normalize()
  134.                 local posDot = math.dot(posDir, car.look)
  135.                 state.maxPosDot = math.max(state.maxPosDot, posDot)
  136.                 if posDot < -0.5 and state.maxPosDot > 0.5 then
  137.                     totalScore = totalScore + math.ceil(10 * comboMeter)
  138.                     comboMeter = comboMeter + 1
  139.                     comboColor = comboColor + 90
  140.                     addMessage("Overtake", comboMeter > 20 and 1 or 0)
  141.                     state.overtaken = true
  142.                 end
  143.             end
  144.         else
  145.             state.maxPosDot = -1
  146.             state.overtaken = false
  147.             state.collided = false
  148.             state.drivingAlong = true
  149.             state.nearMiss = false
  150.         end
  151.     end
  152. end
  153.  
  154. -- For various reasons, this is the most questionable part, some UI. I don’t really like
  155. -- this way though. So, yeah, still thinking about the best way to do it.
  156. local messages = {}
  157. local glitter = {}
  158. local glitterCount = 0
  159.  
  160. function addMessage(text, mood)
  161.     for i = math.min(#messages + 1, 4), 2, -1 do
  162.         messages[i] = messages[i - 1]
  163.         messages[i].targetPos = i
  164.     end
  165.     messages[1] = {text = text, age = 0, targetPos = 1, currentPos = 1, mood = mood}
  166.     if mood == 1 then
  167.         for i = 1, 60 do
  168.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  169.             glitterCount = glitterCount + 1
  170.             glitter[glitterCount] = {
  171.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  172.                 pos = vec2(80, 140) + dir * vec2(40, 20),
  173.                 velocity = dir:normalize():scale(0.2 + math.random()),
  174.                 life = 0.5 + 0.5 * math.random()
  175.             }
  176.         end
  177.     end
  178. end
  179.  
  180. local function updateMessages(dt)
  181.     comboColor = comboColor + dt * 10 * comboMeter
  182.     if comboColor > 360 then
  183.         comboColor = comboColor - 360
  184.     end
  185.     for i = 1, #messages do
  186.         local m = messages[i]
  187.         m.age = m.age + dt
  188.         m.currentPos = math.applyLag(m.currentPos, m.targetPos, 0.8, dt)
  189.     end
  190.     for i = glitterCount, 1, -1 do
  191.         local g = glitter[i]
  192.         g.pos:add(g.velocity)
  193.         g.velocity.y = g.velocity.y + 0.02
  194.         g.life = g.life - dt
  195.         g.color.mult = math.saturate(g.life * 4)
  196.         if g.life < 0 then
  197.             if i < glitterCount then
  198.                 glitter[i] = glitter[glitterCount]
  199.             end
  200.             glitterCount = glitterCount - 1
  201.         end
  202.     end
  203.     if comboMeter > 10 and math.random() > 0.98 then
  204.         for i = 1, math.floor(comboMeter) do
  205.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  206.             glitterCount = glitterCount + 1
  207.             glitter[glitterCount] = {
  208.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  209.                 pos = vec2(195, 75) + dir * vec2(40, 20),
  210.                 velocity = dir:normalize():scale(0.2 + math.random()),
  211.                 life = 0.5 + 0.5 * math.random()
  212.             }
  213.         end
  214.     end
  215. end
  216.  
  217. local speedWarning = 0
  218.     function script.drawUI()
  219.         local uiState = ac.getUiState()
  220.         updateMessages(uiState.dt)
  221.  
  222.         local speedRelative = math.saturate(math.floor(ac.getCarState(1).speedKmh) / requiredSpeed)
  223.         speedWarning = math.applyLag(speedWarning, speedRelative < 1 and 1 or 0, 0.5, uiState.dt)
  224.  
  225.         local colorDark = rgbm(0.4, 0.4, 0.4, 1)
  226.         local colorGrey = rgbm(0.7, 0.7, 0.7, 1)
  227.         local colorAccent = rgbm.new(hsv(speedRelative * 120, 1, 1):rgb(), 1)
  228.         local colorCombo =
  229.             rgbm.new(hsv(comboColor, math.saturate(comboMeter / 10), 1):rgb(), math.saturate(comboMeter / 4))
  230.  
  231.         local function speedMeter(ref)
  232.             ui.drawRectFilled(ref + vec2(0, -4), ref + vec2(180, 5), colorDark, 1)
  233.             ui.drawLine(ref + vec2(0, -4), ref + vec2(0, 4), colorGrey, 1)
  234.             ui.drawLine(ref + vec2(requiredSpeed, -4), ref + vec2(requiredSpeed, 4), colorGrey, 1)
  235.  
  236.             local speed = math.min(ac.getCarState(1).speedKmh, 180)
  237.             if speed > 1 then
  238.                 ui.drawLine(ref + vec2(0, 0), ref + vec2(speed, 0), colorAccent, 4)
  239.             end
  240.         end
  241.  
  242.         ui.beginTransparentWindow("overtakeScore", vec2(100, 100), vec2(400 * 0.5, 400 * 0.5))
  243.         ui.beginOutline()
  244.  
  245.         ui.pushStyleVar(ui.StyleVar.Alpha, 1 - speedWarning)
  246.         ui.pushFont(ui.Font.Main)
  247.         ui.text("Highest Score: " .. highestScore .. " pts")
  248.         ui.popFont()
  249.         ui.popStyleVar()
  250.  
  251.         ui.pushFont(ui.Font.Title)
  252.         ui.text(totalScore .. " pts")
  253.         ui.sameLine(0, 20)
  254.         ui.beginRotation()
  255.         ui.textColored(math.ceil(comboMeter * 10) / 10 .. "x", colorCombo)
  256.         if comboMeter > 20 then
  257.             ui.endRotation(math.sin(comboMeter / 180 * 3141.5) * 3 * math.lerpInvSat(comboMeter, 20, 30) + 90)
  258.         end
  259.         ui.popFont()
  260.         ui.endOutline(rgbm(0, 0, 0, 0.3))
  261.  
  262.         ui.offsetCursorY(20)
  263.         ui.pushFont(ui.Font.Main)
  264.         local startPos = ui.getCursor()
  265.         for i = 1, #messages do
  266.             local m = messages[i]
  267.             local f = math.saturate(4 - m.currentPos) * math.saturate(8 - m.age)
  268.             ui.setCursor(startPos + vec2(20 * 0.5 + math.saturate(1 - m.age * 10) ^ 2 * 50, (m.currentPos - 1) * 15))
  269.             ui.textColored(
  270.                 m.text,
  271.                 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)
  272.             )
  273.         end
  274.         for i = 1, glitterCount do
  275.             local g = glitter[i]
  276.             if g ~= nil then
  277.                 ui.drawLine(g.pos, g.pos + g.velocity * 4, g.color, 2)
  278.             end
  279.         end
  280.         ui.popFont()
  281.         ui.setCursor(startPos + vec2(0, 4 * 30))
  282.  
  283.         ui.pushStyleVar(ui.StyleVar.Alpha, speedWarning)
  284.         ui.setCursorY(0)
  285.         ui.pushFont(ui.Font.Main)
  286.         ui.textColored("Keep speed above " .. requiredSpeed .. " km/h:", colorAccent)
  287.         speedMeter(ui.getCursor() + vec2(-9 * 0.5, 4 * 0.2))
  288.  
  289.         ui.popFont()
  290.         ui.popStyleVar()
  291.  
  292.         ui.endTransparentWindow()
  293.     end