MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / TownGenerateCargoBinomial

Function TownGenerateCargoBinomial

src/town_cmd.cpp:579–594  ·  view source on GitHub ↗

* Generate cargo for a house using the binomial algorithm. * @param t The current town. * @param tpe The town production effect. * @param rate The town's product rate for this production. * @param stations Available stations for this house. */

Source from the content-addressed store, hash-verified

577 * @param stations Available stations for this house.
578 */
579static void TownGenerateCargoBinomial(Town *t, TownProductionEffect tpe, uint8_t rate, StationFinder &stations)
580{
581 for (const CargoSpec *cs : CargoSpec::town_production_cargoes[tpe]) {
582 CargoType cargo_type = cs->Index();
583 uint32_t r = Random();
584
585 /* Make a bitmask with up to 32 bits set, one for each potential pax. */
586 int genmax = (rate + 7) / 8;
587 uint32_t genmask = (genmax >= 32) ? 0xFFFFFFFF : ((1 << genmax) - 1);
588
589 /* Mask random value by potential pax and count number of actual pax. */
590 uint amt = CountBits(r & genmask) * cs->town_production_multiplier / TOWN_PRODUCTION_DIVISOR;
591
592 TownGenerateCargo(t, cargo_type, amt, stations, true);
593 }
594}
595
596/**
597 * Tile callback function.

Callers 1

TileLoop_TownFunction · 0.85

Calls 4

CountBitsFunction · 0.85
TownGenerateCargoFunction · 0.85
RandomFunction · 0.50
IndexMethod · 0.45

Tested by

no test coverage detected