five-on-it / packages/ai/src/traffic
packages/ai/src/traffic
Ambient traffic (GAME_DESIGN §5.6): cars of the ordinary classes driven by NPC drivers along the road graph's lanes, a number the server sets (setTraffic) kept around the players (or the AI's stand-ins while nobody is in the room: viewersOf). Each driver is a character nobody owns at the wheel, and like every NPC it only writes its input (traffic-drive.ts decides it): the car's physics, the carjack and the shots are the simulation's.
- Spawned out of sight: on a lane between TrafficOptions.spawnMin and
spawnMaxmetres of the nearest player, clear of other cars and of the junctions' boxes, at most one car a tick; a class drawn from CLASS_MIX (mostly cars, some taxis and buses, an occasional garbage truck), a cruising share of the road's speed per driver, both from the traffic's own random stream. A bus pulls in to the kerb now and then for a few seconds (traffic-drive.ts); bus lines are for later. - Despawned far away: a car farther than
despawnfrom every player goes, and with nobody on the server they all do. - Recovered: a car that is stuck (flipped, stalled for good, lost off its route, held behind something that never moves) is taken off the road once out of every player's sight, and the count brings a new one elsewhere.
- Carjacked: the driver pulled out (
carjackevent) gets up and flees the carjacker on foot, then walks off; the car is the carjacker's, and goes once left empty and far. - Panic: a shot or a blast near a car, or a hit on it, makes its driver speed away, ignoring lights and junctions for a few seconds, or bail out (likelier when the car is slow or boxed in) and run from the noise, leaving the car where it stands. A driver leaving its car (bailing, out of a wreck, or out of a car on fire, running from it) brakes hard and gets out as the car's speed allows (the simulation's
exitMode): it stays in while the car is too fast, bails out rolling below the bail-out limit, steps out at a crawl. A driver killed at the wheel stays in its seat (the simulation'sboarding.ts): the car is left with the body in it, and both go once out of sight.
A level's spawn points of the traffic (or a game's spawn areas) change where cars appear: at those points (a car park's exit, a tunnel's mouth; within the spawn ring's far edge of a player, near or not, clear of cars), on the lane nearest each and heading its way, else on the lanes inside those areas.
A mission may switch a signalled junction by hand (switchSignal: green for one of its two ways, red, flashing amber, or back to its plan, for a while): the drivers obey it as they obey the plan, and treat a flashing one as a junction without a light. The server sends the switches to the clients (signalOverrides).
The drivers on foot (walkers) run, then walk, and go once out of sight. Everything runs in a fixed order (cars and walkers by entity id) on the traffic's own random stream, so two worlds fed the same inputs run the same traffic, which hashTraffic feeds into hashAiState.