Traffic Utilities
Reference for `gameplay_traffic_trafficUtils`, a collection of utility functions used by the traffic system for spawn point finding, vehicle group creation, road validation, and role loading.
Reference for gameplay_traffic_trafficUtils, a collection of utility functions used by the traffic system for spawn point finding, vehicle group creation, road validation, and role loading.
Module Exports (M)
Spawn Point Finding
| Function | Signature | Description |
|---|---|---|
findSafeSpawnPoint | (startPos?, startDir?, minDist?, maxDist?, targetDist?, options?) → spawnData, valid | Primary spawn finder - tries route, then radial, then fallback |
findSpawnPointOnRoute | (startPos?, startDir?, minDist?, maxDist?, targetDist?, options?) → spawnData, valid | Finds spawn along a pathfinder route ahead |
findSpawnPointOnLine | (startPos?, startDir?, minDist?, maxDist?, targetDist?, options?) → spawnData, valid | Finds spawn along a straight line |
findSpawnPointRadial | (startPos?, startDir?, minDist?, maxDist?, targetDist?, options?) → spawnData, valid | Finds spawn via random radial search |
checkSpawnPoint | (pos, origPos?, minDist?, minVehDist?) → bool | Validates a position for vehicle conflicts |
finalizeSpawnPoint | (pos, dir?, n1?, n2?, options?) → newPos, newDir | Snaps position to a road lane |
Vehicle Group Creation
| Function | Signature | Description |
|---|---|---|
createTrafficGroup | (amount, allMods?, allConfigs?, simpleVehs?) → table | Creates civilian vehicle group from settings |
createPoliceGroup | (amount, allMods?) → table | Creates police vehicle group |
getTrafficGroupFromFile | (options?) → group, fileName | Loads vehicle group from .vehGroup.json file |
Utilities
| Function | Signature | Description |
|---|---|---|
getRoleConstructor | (roleName) → function | Returns cached role constructor module |
placeTrafficVehicles | (pos?, dir?, options?) | Teleports all AI traffic vehicles, lined up |
getNearestTrafficVehicle | (pos?, filters?) → id, dist | Finds nearest traffic vehicle with optional property filters |
Module Fields
| Field | Default | Description |
|---|---|---|
defaults.drivability | 0.3 | Minimum road drivability for spawning |
defaults.radius | 1.2 | Minimum road radius for spawning |
debugMode | false | Debug logging and visualization |
M.checkSpawnPoint | (pos, origPos, minDist, minVehDist) | - |
M.createPoliceGroup | (amount, allMods) | - |
M.createTrafficGroup | (amount, allMods, allConfigs, simpleVehs) | - |
M.debugMode | value | - |
M.defaults | value | - |
M.finalizeSpawnPoint | (pos, dir, mapNode1, mapNode2, options) | - |
M.findSafeSpawnPoint | (startPos, startDir, minDist, maxDist, targetDist, options) | - |
M.findSpawnPoint | (startPos, startDir, minDist, maxDist, targetDist, options) | - |
M.findSpawnPointOnLine | (startPos, startDir, minDist, maxDist, targetDist, options) | - |
M.findSpawnPointOnRoute | (startPos, startDir, minDist, maxDist, targetDist, options) | - |
M.findSpawnPointRadial | (startPos, startDir, minDist, maxDist, targetDist, options) | - |
M.getNearestTrafficVehicle | (pos, filters) | - |
M.getRoleConstructor | (roleName) | - |
M.getTrafficGroupFromFile | (options) | - |
M.placeTrafficVehicles | (pos, dir, options) | - |
Internals
Spawn Search Strategy (findSafeSpawnPoint)
- Route search - Generates a pathfinder route ahead of camera/player, tests points at intervals
- Small radial search (200–1000m) - Random directions if route fails
- Large radial search (200–2000m) - More permissive drivability
- Emergency fallback (10000m) - Last resort, places at ground level
Spawn Point Validation
checkSpawnPoint(pos) verifies:
- Position is at least
minDist(80m) from camera - Position is at least
minVehDist(15m) from all active vehicles - Accounts for vehicle velocity (moving vehicles need larger buffer using braking distance calculation)
Lane Finalization (finalizeSpawnPoint)
Snaps a road-center position to a specific lane:
- Estimates lane count from road width (≥6.1m → 3.05m lanes, otherwise 2.2m)
- Respects one-way roads and right/left-hand drive rules
roadDirparameter controls facing direction relative to playerlegalDirectionenforces correct driving direction
Vehicle Group Params
Groups are created via core_multiSpawn.createGroup() with filters:
- Type: car (1.0), truck (0.6), optionally automation (1.0)
- Config Type: Police excluded for civilian traffic
- Country/Region: Based on level info for smart selections
- Population: Min 50 for variety, 0 for police/mods
Role Constructor Cache
getRoleConstructor() caches loaded role modules by name. Falls back to "standard" if the role file doesn't exist.
Usage Example
-- Find a safe spawn point 200m ahead
local spawnData, valid = gameplay_traffic_trafficUtils.findSafeSpawnPoint(
core_camera.getPosition(),
core_camera.getForward(),
200, 800, 400
)
if valid then
local pos, dir = gameplay_traffic_trafficUtils.finalizeSpawnPoint(
spawnData.pos, spawnData.dir, spawnData.n1, spawnData.n2,
{legalDirection = true, roadDir = 1}
)
end
-- Create a group of 5 police vehicles
local policeGroup = gameplay_traffic_trafficUtils.createPoliceGroup(5)See Also
- gameplay/traffic/baseRole - Traffic Role Base Class - Related reference
- gameplay/traffic/vehicle - Traffic Vehicle Object - Related reference
- Gameplay Systems Guide - Guide
Base Traffic Role
Reference for `gameplay_traffic_baseRole`, the base class for all traffic vehicle driver roles. Provides personality generation, action management, flowgraph support, and common utility methods inheri
Traffic Vehicle
Reference for the traffic vehicle class (`gameplay/traffic/vehicle`), instantiated for each vehicle in the traffic system. Manages per-vehicle state including position tracking, damage detection, coll