G2A Many GEOs
SHARE
TWEET

BeamNG Police Chase Code

Gamergull Jul 15th, 2018 31 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local M = {}
  2.  
  3. local scenario = require("scenario/scenarios")
  4. local helper = require("scenario/scenariohelper")
  5. local player = nil
  6. local playerPos = nil
  7. local playerDistance = nil
  8. local nonPlayers = {}
  9. local racers = {}
  10. local racerPos = nil
  11. local racerVel = nil
  12. local sceneTimer = 0
  13. local score = 0
  14. local totalRacers = 0
  15. local Racer = {instance = nil, state = true, arrestTimer = 0}
  16.  
  17. function Racer:new(instance, state, arrestTimer) --Racer object
  18.     local self = setmetatable({}, Racer)
  19.     self.__index = self
  20.     self.instance = instance or nil
  21.     self.state = state or true
  22.     self.arrestTimer = arrestTimer or 0
  23.     return self
  24. end
  25.  
  26. local function arrestRacer(theRacer) --Triggers when conditions are true
  27.     if theRacer.state then
  28.         if theRacer.arrestTimer % 1 == 0 then
  29.             local countdown = 5 - math.modf(theRacer.arrestTimer)
  30.             if countdown > 0 then
  31.                 helper.flashUiMessage((tostring(countdown).."..."), 0.5, true)
  32.             end
  33.         end
  34.         theRacer.arrestTimer = theRacer.arrestTimer + 0.25
  35.     else
  36.         theRacer.arrestTimer = 0
  37.     end    
  38.     if theRacer.arrestTimer > 5 then
  39.         helper.flashUiMessage("Busted!", 2, true)
  40.         helper.setAiMode(theRacer.instance.name, "stopping")
  41.         theRacer.arrestTimer = 0
  42.         score = score + 1;
  43.         if score >= totalRacers then
  44.             scenario.finish({msg = "You got them all!"}) --Scenario win trigger and message
  45.         end
  46.         theRacer.state = false
  47.     end
  48. end
  49.  
  50. local function onScenarioUIReady(state) --Always executes at the scenario introduction
  51.     sceneTimer = 0
  52.     score = 0
  53.     totalRacers = 0
  54.     player = scenetree.findObject("scenario_player0")
  55.     nonPlayers = {scenetree.findObject("scenario_ai0"), scenetree.findObject("scenario_ai1"), scenetree.findObject("scenario_ai2")} --Edit this to match your AI vehicles
  56.     for i, v in ipairs(nonPlayers) do
  57.         racers[i] = Racer:new(nonPlayers[i], true, 0)
  58.         helper.setAiMode(racers[i].instance.name, "flee")
  59.         totalRacers = totalRacers + 1
  60.     end
  61. end
  62.  
  63. local function onRaceTick(raceTickTime) --Always executes while a scenario is running
  64.     for i, v in ipairs(racers) do
  65.         playerPos = player:getPosition()
  66.         racerPos = racers[i].instance:getPosition()
  67.         racerVel = map.objects[map.objectNames[racers[i].instance.name]].vel:length()
  68.         playerDistance = (playerPos - racerPos):len()
  69.         if racers[i].state then
  70.             if sceneTimer >= 5 and playerDistance <= 50 and racerVel <= 3 then --If: more than 5 seconds post start, within distance, within speed; can be edited
  71.                 arrestRacer(racers[i])
  72.             else
  73.                 racers[i].arrestTimer = 0
  74.             end
  75.         end
  76.     sceneTimer = sceneTimer + 0.25
  77.     end
  78. end
  79.  
  80. M.onScenarioUIReady = onScenarioUIReady
  81. M.onRaceTick = onRaceTick
  82.  
  83. return M
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top