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