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

Function TriggerRoadStopRandomisation

src/newgrf_roadstop.cpp:418–486  ·  view source on GitHub ↗

* Trigger road stop randomisation * * @param st the station being triggered * @param tile the exact tile of the station that should be triggered * @param trigger trigger type * @param cargo_type cargo type causing the trigger */

Source from the content-addressed store, hash-verified

416 * @param cargo_type cargo type causing the trigger
417 */
418void TriggerRoadStopRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type)
419{
420 assert(st != nullptr);
421
422 /* Check the cached cargo trigger bitmask to see if we need
423 * to bother with any further processing.
424 * Note: cached_roadstop_cargo_triggers must be non-zero even for cargo-independent triggers. */
425 if (st->cached_roadstop_cargo_triggers == 0) return;
426 if (IsValidCargoType(cargo_type) && !HasBit(st->cached_roadstop_cargo_triggers, cargo_type)) return;
427
428 st->waiting_random_triggers.Set(trigger);
429
430 uint32_t whole_reseed = 0;
431
432 /* Bitmask of completely empty cargo types to be matched. */
433 CargoTypes empty_mask{};
434 if (trigger == StationRandomTrigger::CargoTaken) {
435 empty_mask = GetEmptyMask(Station::From(st));
436 }
437
438 StationRandomTriggers used_random_triggers;
439 auto process_tile = [&](TileIndex cur_tile) {
440 const RoadStopSpec *ss = GetRoadStopSpec(cur_tile);
441 if (ss == nullptr) return;
442
443 /* Cargo taken "will only be triggered if all of those
444 * cargo types have no more cargo waiting." */
445 if (trigger == StationRandomTrigger::CargoTaken) {
446 if ((ss->cargo_triggers & ~empty_mask) != 0) return;
447 }
448
449 if (!IsValidCargoType(cargo_type) || HasBit(ss->cargo_triggers, cargo_type)) {
450 RoadStopResolverObject object(ss, st, cur_tile, INVALID_ROADTYPE, GetStationType(cur_tile), GetStationGfx(cur_tile));
451 object.SetWaitingRandomTriggers(st->waiting_random_triggers);
452
453 object.ResolveRerandomisation();
454
455 used_random_triggers.Set(object.GetUsedRandomTriggers());
456
457 uint32_t reseed = object.GetReseedSum();
458 if (reseed != 0) {
459 whole_reseed |= reseed;
460 reseed >>= 16;
461
462 /* Set individual tile random bits */
463 uint8_t random_bits = st->GetRoadStopRandomBits(cur_tile);
464 random_bits &= ~reseed;
465 random_bits |= Random() & reseed;
466 st->SetRoadStopRandomBits(cur_tile, random_bits);
467
468 MarkTileDirtyByTile(cur_tile);
469 }
470 }
471 };
472 if (trigger == StationRandomTrigger::NewCargo || trigger == StationRandomTrigger::CargoTaken) {
473 for (const RoadStopTileData &tile_data : st->custom_roadstop_tile_data) {
474 process_tile(tile_data.tile);
475 }

Callers 4

LeaveStationMethod · 0.85
UpdateStationWaitingFunction · 0.85
LoadUnloadVehicleFunction · 0.85

Calls 15

IsValidCargoTypeFunction · 0.85
HasBitFunction · 0.85
GetEmptyMaskFunction · 0.85
GetRoadStopSpecFunction · 0.85
GetStationTypeFunction · 0.85
GetStationGfxFunction · 0.85
GetUsedRandomTriggersMethod · 0.80
GetReseedSumMethod · 0.80
GetRoadStopRandomBitsMethod · 0.80
SetRoadStopRandomBitsMethod · 0.80

Tested by

no test coverage detected