Scripting/Server/Traffic

Synchronised ambient traffic makes the streets alive for every player. Turn it on in server.conf:

traffic_sync on
traffic_ped_density 0.25     # 0..3
traffic_car_density 0.25     # 0..3
traffic_radius 100           # players this close to a lower-id player share its traffic
server_side_traffic off      # on = the server picks the simulating player by ping (see below)

server_side_traffic on. GTA III's ped and car AI only exists inside a game client, so the server cannot simulate it itself. With this option the server takes over the decision of WHO runs it: the players of a group are ordered by ping and the lowest one generates and owns the group's traffic (a generator keeps the role against a 40 ms difference); an owner whose ping is high counts as farther away from every entity (0.5 m per ms above 80 ms), so a closer low-ping player takes the cars over. With off (the default) the nearest / lowest-id player does. In both modes a player at the edge of traffic_radius no longer flips between generator and viewer every second.

Traffic peds and cars are script handles named TrafficPed and TrafficVehicle. They come and go constantly. A handle whose entity is gone has Valid == false, reads as null/dead and ignores writes. Ids are reused after onTraffic*Removed, so never keep a handle past that event.

How it works: the server only relays and decides ownership; it never simulates. One player near the area runs the traffic (the "generator"); everyone else sees synchronised copies. Ownership moves to whoever is closest. Writes you make from a script are applied by the owning player's game and synced to everybody. When traffic is on, the wanted-level system is also live (police, helicopters and roadblocks at higher levels).

Global functions

CallNotes
SetTrafficEnabled(bool) / GetTrafficEnabled()Main world (virtual world 0) switch. Turning it off removes the traffic of world 0 only.
ToggleTrafficEnabled(vw, bool)Peds and cars on/off in one virtual world (0.4.3). Every world other than 0 is OFF until switched on; each world has its own population, generators and owners. Turning it off removes that world's traffic.
ToggleTrafficPedsVW(vw, bool) / ToggleTrafficVehiclesVW(vw, bool)Only the pedestrians / only the cars (and the peds riding in them) of that world.
GetTrafficEnabledVW(vw) / GetTrafficPedsVW(vw) / GetTrafficVehiclesVW(vw)Current state. Traffic handles also have a World property.
SetTrafficDensity(ped [, car])0-3. One argument sets both.
GetTrafficPedDensity() / GetTrafficCarDensity()
SetTrafficRadius(metres) / GetTrafficRadius()20-500.
GetTrafficPedCount() / GetTrafficVehicleCount()
GetTrafficPeds() / GetTrafficVehicles()Array of handles.
FindTrafficPed(id) / FindTrafficVehicle(id)Handle or null.
RemoveTrafficPed(id) / RemoveTrafficVehicle(id)By id. true if it existed.
ClearTraffic()Removes everything (generators keep spawning new traffic).
GetTrafficPedInfo(id) / GetTrafficVehicleInfo(id)A plain table of the same data, or null.

TrafficPed

PropertyTypeAccessDescription
IDintegerr0-255.
ValidbooleanrFalse once the ped is gone.
IsTrafficbooleanrAlways true (tells it apart from a Player).
Model / SkinintegerrwPed model 0-300.
PedTypeintegerrEngine ped type (4/5 civilians, 6 cop, 7-15 gangs...).
PosVectorrwWriting also takes the ped out of its car.
AnglefloatrwDegrees.
VelocityVectorrGame units per frame.
Health / ArmourfloatrwSetting health to 0 kills the ped.
WeaponintegerrwAssigning gives the weapon with 9999 ammo.
StateintegerrTRAFFIC_FOOT, TRAFFIC_ATTACK, TRAFFIC_DEAD, TRAFFIC_VEHICLE, TRAFFIC_FALL.
Deadbooleanr
OwnerPlayer|nullrPlayer whose game runs this ped.
WorldintegerrVirtual world the ped lives in.
TargetPlayer|nullrPlayer it is attacking or following.
VehicleTrafficVehicle|Vehicle|nullrCar it is in.
Seatintegerr-1 on foot, 0 driver, 1-8 passenger.
Objective, PedStateintegerrRaw engine values.

TrafficVehicle

PropertyTypeAccessDescription
ID / Valid / IsTrafficinteger / boolr
Modelintegerr
Colour1 / Colour2integerrw
PosVectorrwTeleports.
AnglefloatrwDegrees.
VelocityVectorrw
Speedfloatrkm/h.
Healthfloatrw1000 = full.
Wrecked / Siren / Lightsbooleanr
OwnerPlayer|nullr
DriverTrafficPed|nullr
WorldintegerrVirtual world the vehicle lives in.
NoteWhen a player gets into a traffic car it is converted into a normal Vehicle (event onPlayerTakeOverTrafficVehicle), occupants go with it, and carjacking works. The converted car is deleted after 45 s with nobody in it or within 120 m.
function onTrafficPedDeath(ped, killer)
{
    if (killer) MessagePlayer("You killed a pedestrian", killer);
    CreatePickup(1334, ped.Pos);          // your own drop (traffic peds drop nothing)
}

function onPlayerCommand(player, cmd, text)
{
    if (cmd == "clearpeds") foreach (p in GetTrafficPeds()) p.Kill();
}

Spawn rules (0.5.4)

Spawn rules change what the traffic is made of: more of one car or skin in an area, less of another, or none at all. The players whose games run the traffic apply them the moment the game creates a car or a ped, so everybody sees the result. Rules stay until you remove them (a script reload clears them). Up to 48 rules. The area is a rectangle x1, y1, x2, y2 (any corner order); leave it out for the whole map. Each call returns the rule id (-1 when the 48 are used up).

CallNotes
AddTrafficVehicleSpawn(model, percent [, x1, y1, x2, y2])percent % of the traffic cars made in the area become model. Only ordinary traffic is replaced (police and gang cars keep their model). Custom vehicles (4000+) work too.
AddTrafficVehicleLimit(model, percent [, x1, y1, x2, y2])Cars of model picked in the area are kept only percent % of the time; the game picks another car instead. 0 = this car never appears there.
AddTrafficPedSpawn(skin, percent [, kind] [, x1, y1, x2, y2])percent % of the new peds of kind in the area get skin. They keep what they are: a cop with a new skin is still a cop, a gang member still fights for his gang. kind: TRAFFIC_PED_ANY (default), TRAFFIC_PED_CIVILIAN, TRAFFIC_PED_COP, TRAFFIC_PED_GANG, or one gang TRAFFIC_PED_GANG1 ... TRAFFIC_PED_GANG9.
AddTrafficPedLimit(skin, percent [, x1, y1, x2, y2])Peds with skin that appear on foot in the area are kept only percent % of the time. 0 = never there.
RemoveTrafficSpawnRule(id)true if the rule existed.
ClearTrafficSpawnRules() / GetTrafficSpawnRuleCount()
NoteWhen several spawn rules match, the first one added is tried first. Cars are picked for the spot the traffic is generated around (the generating player), peds by where they appear. A new model has to stream in first: the first few cars or peds after a rule is added (or after you arrive in its area) can still be the old ones. Peds already sitting in a car are not re-skinned.
function onScriptLoad()
{
    // Portland Harwood: lots of Stallions, no Mr Whoopee
    AddTrafficVehicleSpawn(129, 30, 800.0, -1100.0, 1200.0, -700.0);
    AddTrafficVehicleLimit(113, 0, 800.0, -1100.0, 1200.0, -700.0);
    // cops everywhere wear the SWAT skin half of the time
    AddTrafficPedSpawn(2, 50, TRAFFIC_PED_COP);
}
Categories: Traffic