fleet.module
README.md
fleet
City traffic from one seed. Fleet.make(seed, n, mix) draws N Car Forge descriptions from a mix of body styles, era, regional colours, wear and dirt, and roles, the same on every machine for the same seed and mix (seedKit rng streams; one stream per car, so car 7 is car 7 however long the fleet is). Then three ways to put a fleet in a street, each costing what it has to:
| Call | What you get | Cost |
|---|---|---|
Fleet.parkRow(descs, opts) | Rows of parked cars: static models on carForge's shared meshes (body, four wheels, a cabin drawn up close), one box collider each so nobody walks through. No physics, no Vehicle, no scripts. Built locally on every peer. | entities only |
Fleet.wake(car) | Turns one parked car into a real drivable car (a synced vehiclesKit car via Cars.spawn) in its bay; every peer swaps its own parked copy out. | one vehicle |
Fleet.drive(descs, path, opts) | Cars that really drive: synced Car Forge vehicles with an aiDrivers AiDriver each, spaced along a roadPieces road or point list. Host only; the host owns and drives them. | one vehicle + AI each |
Fleet.farTraffic(descs, path, opts) | Far-off traffic: low-poly cars sliding along a road, both directions, optional spinning wheels, all moved by one FarTraffic updater through batched typed-buffer transform writes. Built locally on every peer. | well under 1 ms a frame for 100+ |
local Fleet = require("~carForge.fleet")
local Roads = require("~roadPieces.roads")
local street = Fleet.make(2024, 24, {
styles = { hatchback = 3, saloon = 2, panelVan = 1 }, -- shares of body styles
era = { modern = 9, ["80s"] = 1 }, -- or "modern", "classic", { modern = 8, classic = 1 }
region = "harbourCity", -- or regions = { harbourCity = 3, metro = 1 }
roles = { civilian = 20, taxi = 2, delivery = 1 }, -- or "police"
wear = { 0.2, 0.6 }, dirt = { 0, 0.3 }, -- ranges (or a number)
})
local row = Fleet.parkRow(street, { start = { 0, 0, 0 }, dir = { 1, 0, 0 }, angle = "parallel" })
Fleet.wake(row.cars[5]) -- the fifth one can now be driven
local loop = Roads.spline(points, { style = "country", loop = true })
Fleet.drive(Fleet.make(31, 3), loop, { spacing = 25, personality = "commuter" }) -- host only
Fleet.farTraffic(Fleet.make(99, 120), loop, { speed = 14, spinWheels = true })
The mix
Every part is optional; what is left out is a believable city street: mostly modern everyday cars (hatchbacks, saloons, crossovers, estates, SUVs), a few vans, pickups and minivans, the odd sports car or convertible, 8 % classics (more 80s and 90s than 60s), 90 % civilian with taxis, couriers and the occasional police car, works truck or ambulance, one home region's palette for most cars, light wear and dirt.
styles(aliasstyle):{ style = weight }, a style name, or a list of names. Unknown names raise.era:"modern","classic",{ modern = w, classic = w }, or decades{ modern = w, ["70s"] = w, classic90s = w }(keys60s/70s/80s/90sor the classic style names).region/regions: one ofCars.regions()or weights.roles(aliasrole): weights overcivilian taxi delivery police ambulance service. A service car takes a body that service drives (Fleet.ROLE_STYLES), limited to the styles in the mix when they overlap.wear,dirt:{ lo, hi }or a number.look(every car),preset(defaultanyCar),lock(any other settings locked on every car).
Fleet.census(descs) counts styles, roles and regions; Fleet.withLook(d, look) gives the same car in another look.
Options
parkRow(descs, { start, dir, spacing, gap = 1.0, angle = "parallel" | "perpendicular" | "angled" | degrees, side = "right" | "left", reverse, jitter = true, collider = true, cabin = true, parent, key, name })→{ root, cars, slots, key }. Withoutspacing, cars stand bumper to bumpergapmetres apart by their own lengths (widths when perpendicular).wake(car, opts)→ the vehicle entity.Fleet.nearestParked(pos, maxDist)→(entity, desc, distance)finds the car a player stands next to;Fleet.parkedInfo(entity)→ its description.drive(descs, path, { loop, personality = "commuter", personalities = { ... }, spacing = 14, startAt, lane = 1.8, roadWidth = 8, laneOffset, speedLimit, pace, name })→{ entity }(empty on a non-host peer).farTraffic(descs, path, { loop, speed = 12, spacing, lane = 1.8, bothWays = true, look = "lowpoly", spinWheels = false, wheels = true, startAt, parent, name })→{ root, cars, component };component:stats()reports its cost.clear(handle)removes whatparkRow/farTrafficreturned, or the listdrivereturned.
A path is a roadPieces road entity (its centreline via pathOf), a list of points, or { points, loop }.
Multiplayer
Parked rows and far traffic are local, temporary entities built from the seed: run the same calls on every peer (a scene's entrypoint does this) and every player sees the same cars without anything replicating. AI cars are synced vehicles that only the host spawns; their AI runs on the host. A woken parked car is a synced vehicle named after its bay (FleetCar@<row>.<n>); each peer's watcher removes its own parked copy of that bay when it appears.
Known limits
- Building a parked row costs the shared meshes it needs: about 30 ms per new body on desktop the first time a body shape is seen (a fleet of all-different cars builds one body each), nothing for repeats. Build rows at load, not mid-play.
- Far traffic slides on the path: it does not stop for other cars or junctions, and it does not collide. Keep it where players cannot reach it, or use
drive.