GE Lua Documentation

Press F to search!

setupTraffic

Definition


-- @/lua/ge/extensions/gameplay/traffic.lua:655

local function setupTraffic(amount, policeRatio, options) -- prepares a group of vehicles for traffic
  amount = amount or -1
  policeRatio = policeRatio or 0 -- can be between 0 and 1
  options = type(options) == 'table' and options or {}

  local trafficGroup, policeGroup
  local policeAmount = 0
  local activeAmount = options.activeAmount or amount

  if not options.ignoreDelete and state == 'on' then
    deleteVehicles() -- clear current traffic
  end

  if type(options.vehGroup) == 'table' then -- directly sets a vehicle group to be used for traffic; may overwrite other parameters
    trafficGroup = options.vehGroup
    log('I', logTag, 'Applying custom traffic group')
  end

  if amount == -1 then -- auto amount
    local amountFromSettings = getAmountFromSettings()
    amount = getIdealSpawnAmount(amountFromSettings) -- maxAmount automatically accounts for currently spawned non-traffic vehicles
    policeAmount = options.policeAmount or math.ceil(amount * policeRatio)
    activeAmount = amount

    if settings.getValue('trafficExtraVehicles') then -- extra amount of vehicles to spawn (they will be inactive when the vehicle pooling system starts)
      local extraAmount = settings.getValue('trafficExtraAmount')
      if extraAmount == 0 then
        extraAmount = clamp(amountFromSettings, 2, 8)
      end

      amount = max(amountFromSettings, amount + extraAmount)
    end
  else
    if options.autoAdjustAmount then
      amount = getIdealSpawnAmount(amount) -- adjust for amount of existing active vehicles
    end
    policeAmount = options.policeAmount or math.ceil(amount * policeRatio)
  end

  if not trafficGroup then -- if predefined vehicle group does not exist, create it
    if policeAmount >= 1 then
      policeAmount = min(policeAmount, amount)
      local fileData, fileName
      local fileMode = options.autoLoadFromFile or settings.getValue('trafficSmartSelections')
      if fileMode then
        fileData, fileName = gameplay_traffic_trafficUtils.getTrafficGroupFromFile({name = 'police'}) -- level specific police vehicles
        if fileData then
          fileData = core_multiSpawn.fitGroup(fileData, policeAmount)
          log('I', logTag, string.format('Loaded police group from file: %s', fileName or ''))
        end
      end
      policeGroup = fileData or gameplay_traffic_trafficUtils.createPoliceGroup(policeAmount)

      if not policeGroup[1] then
        for i = 1, policeAmount do
          table.insert(policeGroup, defaultData.police)
        end
      end
    end

    if amount >= 1 then
      if not trafficGroup then
        local fileData, fileName
        local fileMode = options.autoLoadFromFile and not (settings.getValue('trafficSimpleVehicles') or options.simpleVehs)
        if fileMode then
          fileData, fileName = gameplay_traffic_trafficUtils.getTrafficGroupFromFile({name = 'traffic'}) -- level specific civilian vehicles
          if fileData then
            fileData = core_multiSpawn.fitGroup(fileData, amount)
            log('I', logTag, string.format('Loaded traffic group from file: %s', fileName or ''))
          end
        end

        trafficGroup = fileData or gameplay_traffic_trafficUtils.createTrafficGroup(amount, options.allMods, options.allConfigs, options.simpleVehs)
      end
      if not trafficGroup[1] then
        for i = 1, amount do
          table.insert(trafficGroup, defaultData.traffic)
        end
      end

      if policeGroup then
        for i = 1, policeAmount do
          if policeGroup[i] then
            table.insert(trafficGroup, 1, policeGroup[i]) -- pushes to array (police vehicles have priority)
            table.remove(trafficGroup, #trafficGroup) -- pops from array
          end
        end
      end
    end
  end

  if amount > 0 and trafficGroup and trafficGroup[1] then
    spawnProcess.group = trafficGroup
    spawnProcess.amount = amount

    local multiSpawnOptions = {}
    multiSpawnOptions.pos = options.pos
    multiSpawnOptions.rot = options.rot
    if next(multiSpawnOptions) then spawnProcess.multiSpawnOptions = multiSpawnOptions end

    state = 'loading'

    setTrafficVars({aiMode = 'traffic', activeAmount = activeAmount})
  else
    if amount <= 0 then
      log('I', logTag, 'Traffic amount to spawn is zero, now ignoring traffic')
    else
      log('I', logTag, 'Traffic vehicle group is empty')
    end
    ui_message('ui.traffic.spawnLimit', 5, 'traffic', 'traffic')
    return false
  end

  return true
end

Callers

@/lua/ge/extensions/scenario/busdriver.lua
    if amount == 0 then amount = getMaxVehicleAmount(12) end
    gameplay_traffic.setupTraffic(amount)
  end
@/lua/ge/extensions/freeroam/freeroam.lua
      end
      gameplay_traffic.setupTraffic(trafficAmount, policeRatio, {activeAmount = trafficAmount})
    elseif loadParkedVehicles then
@/lua/ge/extensions/editor/trafficDebug.lua
    if trafficAmountChange[0] > 0 then
      gameplay_traffic.setupTraffic(trafficAmountChange[0], nil, {ignoreDelete = true})
    elseif trafficAmountChange[0] < 0 then
@/lua/ge/extensions/tech/techCore.lua
  gameplay_parking.setupVehicles(parkedAmount)
  gameplay_traffic.setupTraffic(maxAmount, policeRatio)
  request:sendACK('TrafficSpawned')
@/lua/ge/extensions/career/modules/playerDriving.lua

local function setupTraffic(forceSetup)
  if forceSetup or (gameplay_traffic.getState() == "off" and not gameplay_traffic.getTrafficList(true)[1] and playerData.trafficActive == 0) then
    gameplay_parking.setupVehicles(restrict and testTrafficAmounts.parkedCars or parkedAmount)
    gameplay_traffic.setupTraffic(restrict and testTrafficAmounts.traffic + extraAmount or amount + extraAmount, 0, {policeAmount = policeAmount, simpleVehs = true, autoLoadFromFile = true})
    setTrafficVars()
  if M.ensureTraffic then -- temp solution to reset traffic
    setupTraffic(true)
  end
local function onPlayerCameraReady()
  setupTraffic() -- spawns traffic while the loading screen did not fade out yet
end
  if alreadyInLevel then
    setupTraffic()
  end
local function onClientStartMission()
  setupTraffic()
end
@/lua/ge/extensions/gameplay/traffic.lua
    if state == 'off' then
      setupTraffic()
    elseif state == 'on' then
    end
    spawnProcess.trafficSetup = setupTraffic(spawnProcess.amount, spawnProcess.policeRatio)
@/lua/ge/extensions/gameplay/missions/missionManager.lua
      step.validParking = gameplay_parking.setupVehicles(trafficSetup.parkedAmount)
      step.validTraffic = gameplay_traffic.setupTraffic(trafficSetup.amount, 0, options)
      trafficSetup._hasSpawnedTraffic = true
  if not step.started then
    gameplay_traffic.setupTraffic()
    step.started = true