Facebook
From test, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 143
  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. function sleep(s)
  28.   local ntime = os.time() + s
  29.   repeat until os.time() > ntime
  30.   addMessage("test", -1)
  31. end
  32. -- Event state:
  33. Carp = 0
  34. local timePassed = 0
  35. local totalScore = 0
  36. local comboMeter = 1
  37. local comboColor = 0
  38. local highestScore = 0
  39. local dangerouslySlowTimer = 0
  40. local carsState = {}
  41. local wheelsWarningTimeout = 0
  42.  
  43. function script.update(dt)
  44.     if timePassed == 0 then
  45.         addMessage("Let’s go!", 0)
  46.     end
  47.  
  48.     local player = ac.getCarState(1)
  49.     if player.engineLifeLeft < 1 then
  50.         if totalScore > highestScore then
  51.             highestScore = math.floor(totalScore)
  52.             ac.sendChatMessage("scored " .. totalScore .. " points.")
  53.         end
  54.         totalScore = 0
  55.         comboMeter = 1
  56.         return
  57.     end
  58.  
  59.     timePassed = timePassed + dt
  60.  
  61.     local comboFadingRate = 0.5 * math.lerp(1, 0.1, math.lerpInvSat(player.speedKmh, 80, 200)) + player.wheelsOutside
  62.     comboMeter = math.max(1, comboMeter - dt * comboFadingRate)
  63.  
  64.     local sim = ac.getSimState()
  65.     while sim.carsCount > #carsState do
  66.         carsState[#carsState + 1] = {}
  67.     end
  68.  
  69.     if wheelsWarningTimeout > 0 then
  70.         wheelsWarningTimeout = wheelsWarningTimeout - dt
  71.     elseif player.wheelsOutside > 0 then
  72.         if wheelsWarningTimeout == 0 then
  73.         end
  74.         addMessage("Car is outside", -1)
  75.         wheelsWarningTimeout = 60
  76.     end
  77.  
  78.     if player.speedKmh < requiredSpeed then
  79.         if dangerouslySlowTimer > 3 then
  80.             if totalScore > highestScore then
  81.                 highestScore = math.floor(totalScore)
  82.                 ac.sendChatMessage("scored " .. totalScore .. " points.")
  83.             end
  84.             totalScore = 0
  85.             comboMeter = 1
  86.         else
  87.             if dangerouslySlowTimer == 0 then
  88.                 addMessage("Too slow!", -1)
  89.             end
  90.         end
  91.         dangerouslySlowTimer = dangerouslySlowTimer + dt
  92.         comboMeter = 1
  93.         return
  94.     else
  95.         dangerouslySlowTimer = 0
  96.     end
  97.  
  98.     for i = 1, ac.getSimState().carsCount do
  99.         local car = ac.getCarState(i)
  100.         local state = carsState[i]
  101.  
  102.         if car.pos:closerToThan(player.pos, 10) then
  103.             local drivingAlong = math.dot(car.look, player.look) > 0.2
  104.             if not drivingAlong then
  105.                 state.drivingAlong = false
  106.  
  107.                 if not state.nearMiss and car.pos:closerToThan(player.pos, 3) then
  108.                     state.nearMiss = true
  109.  
  110.                     if car.pos:closerToThan(player.pos, 2.5) then
  111.                         comboMeter = comboMeter + 3
  112.                         addMessage("Very close near miss!", 1)
  113.                     else
  114.                         comboMeter = comboMeter + 1
  115.                         addMessage("Near miss: bonus combo", 0)
  116.                     end
  117.                 end
  118.             end
  119.  
  120.             if car.collidedWith == 0 then
  121.        Carp = Carp + 1
  122.      state.collided = false
  123.      if Carp == 30 then
  124.     state.collided = true
  125.      addMessage("Collision", -1)
  126.               end
  127.                 if totalScore > highestScore then
  128.                     highestScore = math.floor(totalScore)
  129.                     ac.sendChatMessage("scored " .. totalScore .. " points.")
  130.                 end
  131.                 totalScore = 0
  132.                 comboMeter = 1
  133.             end
  134.  
  135.             if not state.overtaken and not state.collided and state.drivingAlong then
  136.                 local posDir = (car.pos - player.pos):normalize()
  137.                 local posDot = math.dot(posDir, car.look)
  138.                 state.maxPosDot = math.max(state.maxPosDot, posDot)
  139.                 if posDot < -0.5 and state.maxPosDot > 0.5 then
  140.                     totalScore = totalScore + math.ceil(10 * comboMeter)
  141.                     comboMeter = comboMeter + 1
  142.                     comboColor = comboColor + 90
  143.                     addMessage("Overtake", comboMeter > 20 and 1 or 0)
  144.                     state.overtaken = true
  145.                 end
  146.             end
  147.         else
  148.             state.maxPosDot = -1
  149.             state.overtaken = false
  150.             state.collided = false
  151.             state.drivingAlong = true
  152.             state.nearMiss = false
  153.         end
  154.     end
  155. end
  156.  
  157. -- For various reasons, this is the most questionable part, some UI. I don’t really like
  158. -- this way though. So, yeah, still thinking about the best way to do it.
  159. local messages = {}
  160. local glitter = {}
  161. local glitterCount = 0
  162.  
  163. function addMessage(text, mood)
  164.     for i = math.min(#messages + 1, 4), 2, -1 do
  165.         messages[i] = messages[i - 1]
  166.         messages[i].targetPos = i
  167.     end
  168.     messages[1] = {text = text, age = 0, targetPos = 1, currentPos = 1, mood = mood}
  169.     if mood == 1 then
  170.         for i = 1, 60 do
  171.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  172.             glitterCount = glitterCount + 1
  173.             glitter[glitterCount] = {
  174.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  175.                 pos = vec2(80, 140) + dir * vec2(40, 20),
  176.                 velocity = dir:normalize():scale(0.2 + math.random()),
  177.                 life = 0.5 + 0.5 * math.random()
  178.             }
  179.         end
  180.     end
  181. end
  182.  
  183. local function updateMessages(dt)
  184.     comboColor = comboColor + dt * 10 * comboMeter
  185.     if comboColor > 360 then
  186.         comboColor = comboColor - 360
  187.     end
  188.     for i = 1, #messages do
  189.         local m = messages[i]
  190.         m.age = m.age + dt
  191.         m.currentPos = math.applyLag(m.currentPos, m.targetPos, 0.8, dt)
  192.     end
  193.     for i = glitterCount, 1, -1 do
  194.         local g = glitter[i]
  195.         g.pos:add(g.velocity)
  196.         g.velocity.y = g.velocity.y + 0.02
  197.         g.life = g.life - dt
  198.         g.color.mult = math.saturate(g.life * 4)
  199.         if g.life < 0 then
  200.             if i < glitterCount then
  201.                 glitter[i] = glitter[glitterCount]
  202.             end
  203.             glitterCount = glitterCount - 1
  204.         end
  205.     end
  206.     if comboMeter > 10 and math.random() > 0.98 then
  207.         for i = 1, math.floor(comboMeter) do
  208.             local dir = vec2(math.random() - 0.5, math.random() - 0.5)
  209.             glitterCount = glitterCount + 1
  210.             glitter[glitterCount] = {
  211.                 color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1),
  212.                 pos = vec2(195, 75) + dir * vec2(40, 20),
  213.                 velocity = dir:normalize():scale(0.2 + math.random()),
  214.                 life = 0.5 + 0.5 * math.random()
  215.             }
  216.         end
  217.     end
  218. end
  219.  
  220. local speedWarning = 0
  221.     function script.drawUI()
  222.         local uiState = ac.getUiState()
  223.         updateMessages(uiState.dt)
  224.  
  225.         local speedRelative = math.saturate(math.floor(ac.getCarState(1).speedKmh) / requiredSpeed)
  226.         speedWarning = math.applyLag(speedWarning, speedRelative < 1 and 1 or 0, 0.5, uiState.dt)
  227.  
  228.         local colorDark = rgbm(0.4, 0.4, 0.4, 1)
  229.         local colorGrey = rgbm(0.7, 0.7, 0.7, 1)
  230.         local colorAccent = rgbm.new(hsv(speedRelative * 120, 1, 1):rgb(), 1)
  231.         local colorCombo =
  232.             rgbm.new(hsv(comboColor, math.saturate(comboMeter / 10), 1):rgb(), math.saturate(comboMeter / 4))
  233.  
  234.         local function speedMeter(ref)
  235.             ui.drawRectFilled(ref + vec2(0, -4), ref + vec2(180, 5), colorDark, 1)
  236.             ui.drawLine(ref + vec2(0, -4), ref + vec2(0, 4), colorGrey, 1)
  237.             ui.drawLine(ref + vec2(requiredSpeed, -4), ref + vec2(requiredSpeed, 4), colorGrey, 1)
  238.  
  239.             local speed = math.min(ac.getCarState(1).speedKmh, 180)
  240.             if speed > 1 then
  241.                 ui.drawLine(ref + vec2(0, 0), ref + vec2(speed, 0), colorAccent, 4)
  242.             end
  243.         end
  244.  
  245.         ui.beginTransparentWindow("overtakeScore", vec2(100, 100), vec2(400 * 0.5, 400 * 0.5))
  246.         ui.beginOutline()
  247.  
  248.         ui.pushStyleVar(ui.StyleVar.Alpha, 1 - speedWarning)
  249.         ui.pushFont(ui.Font.Main)
  250.         ui.text("Highest Score: " .. highestScore .. " pts")
  251.         ui.popFont()
  252.         ui.popStyleVar()
  253.  
  254.         ui.pushFont(ui.Font.Title)
  255.         ui.text(totalScore .. " pts")
  256.         ui.sameLine(0, 20)
  257.         ui.beginRotation()
  258.         ui.textColored(math.ceil(comboMeter * 10) / 10 .. "x", colorCombo)
  259.         if comboMeter > 20 then
  260.             ui.endRotation(math.sin(comboMeter / 180 * 3141.5) * 3 * math.lerpInvSat(comboMeter, 20, 30) + 90)
  261.         end
  262.         ui.popFont()
  263.         ui.endOutline(rgbm(0, 0, 0, 0.3))
  264.  
  265.         ui.offsetCursorY(20)
  266.         ui.pushFont(ui.Font.Main)
  267.         local startPos = ui.getCursor()
  268.         for i = 1, #messages do
  269.             local m = messages[i]
  270.             local f = math.saturate(4 - m.currentPos) * math.saturate(8 - m.age)
  271.             ui.setCursor(startPos + vec2(20 * 0.5 + math.saturate(1 - m.age * 10) ^ 2 * 50, (m.currentPos - 1) * 15))
  272.             ui.textColored(
  273.                 m.text,
  274.                 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)
  275.             )
  276.         end
  277.         for i = 1, glitterCount do
  278.             local g = glitter[i]
  279.             if g ~= nil then
  280.                 ui.drawLine(g.pos, g.pos + g.velocity * 4, g.color, 2)
  281.             end
  282.         end
  283.         ui.popFont()
  284.         ui.setCursor(startPos + vec2(0, 4 * 30))
  285.  
  286.         ui.pushStyleVar(ui.StyleVar.Alpha, speedWarning)
  287.         ui.setCursorY(0)
  288.         ui.pushFont(ui.Font.Main)
  289.         ui.textColored("Keep speed above " .. requiredSpeed .. " km/h:", colorAccent)
  290.         speedMeter(ui.getCursor() + vec2(-9 * 0.5, 4 * 0.2))
  291.  
  292.         ui.popFont()
  293.         ui.popStyleVar()
  294.  
  295.         ui.endTransparentWindow()
  296.     end
  297.  
  298.  function sleep(n)
  299.   if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
  300.   end