Skip to content

five-on-it / packages/ai/src/traffic-drive

packages/ai/src/traffic-drive ​

Driving an ambient car, one tick at a time, ending in an ordinary input:

  • The way: the lanes ahead, a random way out at each junction (then a random lane of it; a bus or a truck straight on where it can, its turns sweeping the lanes beside), the same lane along a road, a U-turn only at a dead end; replanned as the route runs short (traffic-route.ts turns the lanes into the polyline it follows).
  • The speed: the driver commands an acceleration, never a throttle. Its free speed is the road's limit times its cruising share (a scared driver's 1.4 times the limit), and what it must slow for are targets: the bends ahead (each route point's cap, met by a planned deceleration to the point), the car, wreck or person in front (followed by the Intelligent Driver Model: a gap of 2.5 m plus 0.9 s of speed, closing speed weighed in, so cars queue behind each other and stop 2.5 m short), and the junction ahead (a red light, or an amber it decided to stop for; a junction without one where someone else arrived first: the first to arrive at the box goes, the others wait at its line, two cars going straight across from opposite sides go together, ten seconds of waiting and it goes anyway; a box it could not leave; a bus's stop), each a standing car to queue behind whose rear is just past the line. The free acceleration toward the free speed is 2.5 m/s² at most (a bus's or a truck's less), the comfortable braking 4 m/s², the emergency reserve 8, and the command changes by 8 m/s³ at most (40 in an emergency), so no car ever bangs its throttle or stands on its brakes. The decision at an amber light is made once and kept: stop when braking at 3.5 m/s² does it, else go through; a red that even the emergency braking could not stop for before the line is gone through too. The throttle and the brake are then whatever the class's engine and brakes need for that acceleration (pedal), so a bus and a sedan both drive smoothly within their means. A scared driver ignores the junctions, braking only for what is in front, and pulls away harder.
  • The wheel: pure pursuit of a point half a second ahead on the route.
  • A bus's stops: at the level's bus stops (@foi/world's spots of kind bus_stop) on its kerb side, its front door by the stop's queue, or, on a level without, every couple of hundred metres (never near a junction), a bus pulls in toward the kerb, stops a few seconds (its queue boarding: the simulation's pedestrians) and goes on; the car behind may pass it. A delivery scooter pulls in the same way every couple of hundred metres (a delivery), an ice-cream van too, standing there half a minute (its hatch open: the server's shop at it).
  • Trouble: a car that asks for speed and gets none backs off for a second, wheels turned the other way; three times in twenty seconds, a second on its side or roof, two seconds off its route, or twenty seconds held behind something that is not traffic waiting, or twenty seconds held at a junction's line by the others (a gridlock), and it is stuck: taken off the road once out of sight. Two cars held by each other (nose to nose, or one knocked across the other's way) back off by turns.

The mode the readout shows is what holds the speed most: follow or queue behind a car (moving or nearly stopped), red, giveway, panic, a bus's stop, else drive.

Interfaces ​

Variables ​

Functions ​