Add cargo mask of town production cargoes

Use for all non-GUI cases (where display sort order is not required)
pull/661/head
Jonathan G Rennison 3 months ago
parent 8581808952
commit 19e4895ffd

@ -25,6 +25,7 @@
CargoSpec CargoSpec::array[NUM_CARGO];
std::array<std::vector<const CargoSpec *>, NUM_TPE> CargoSpec::town_production_cargoes{};
std::array<CargoTypes, NUM_TPE> CargoSpec::town_production_cargo_mask{};
/**
* Bitmask of cargo types available. This includes phony cargoes like regearing cargoes.
@ -216,6 +217,7 @@ static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * c
void InitializeSortedCargoSpecs()
{
for (auto &tpc : CargoSpec::town_production_cargoes) tpc.clear();
for (auto &tpc : CargoSpec::town_production_cargo_mask) tpc = 0;
_sorted_cargo_specs.clear();
/* Add each cargo spec to the list, and determine the largest cargo icon size. */
for (const CargoSpec *cargo : CargoSpec::Iterate()) {
@ -236,6 +238,7 @@ void InitializeSortedCargoSpecs()
for (const auto &cargo : _sorted_cargo_specs) {
assert(cargo->town_production_effect != INVALID_TPE);
CargoSpec::town_production_cargoes[cargo->town_production_effect].push_back(cargo);
SetBit(CargoSpec::town_production_cargo_mask[cargo->town_production_effect], cargo->Index());
if (cargo->classes & CC_SPECIAL) break;
nb_standard_cargo++;
SetBit(_standard_cargo_mask, cargo->Index());

@ -199,6 +199,9 @@ struct CargoSpec {
/** List of cargo specs for each Town Product Effect. */
static std::array<std::vector<const CargoSpec *>, NUM_TPE> town_production_cargoes;
/** Mask of cargo IDs for each Town Product Effect. */
static std::array<CargoTypes, NUM_TPE> town_production_cargo_mask;
private:
static CargoSpec array[NUM_CARGO]; ///< Array holding all CargoSpecs

@ -293,9 +293,18 @@ bool FindSubsidyPassengerRoute()
{
if (!Subsidy::CanAllocateItem()) return false;
if (CargoSpec::town_production_cargo_mask[TPE_PASSENGERS] == 0) return false;
/* Pick a random TPE_PASSENGER type */
uint32_t r = RandomRange(static_cast<uint>(CargoSpec::town_production_cargoes[TPE_PASSENGERS].size()));
CargoID cid = CargoSpec::town_production_cargoes[TPE_PASSENGERS][r]->Index();
uint32_t r = RandomRange(CountBits(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS]));
CargoID cid{};
for (CargoID cargo_id : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS])) {
if (r == 0) {
cid = cargo_id;
break;
}
r--;
}
const Town *src = Town::GetRandom();
if (src->cache.population < SUBSIDY_PAX_MIN_POPULATION ||
@ -343,8 +352,8 @@ bool FindSubsidyTownCargoRoute()
}
/* Passenger subsidies are not handled here. */
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_PASSENGERS]) {
town_cargo_produced[cs->Index()] = 0;
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS])) {
town_cargo_produced[cid] = 0;
}
uint8_t cargo_count = town_cargo_produced.GetCount();

@ -688,7 +688,8 @@ static void TownGenerateCargo(Town *t, CargoID ct, uint amount, StationFinder &s
*/
static void TownGenerateCargoOriginal(Town *t, TownProductionEffect tpe, uint8_t rate, StationFinder &stations)
{
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[tpe]) {
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[tpe])) {
const CargoSpec *cs = CargoSpec::Get(cid);
uint32_t r = Random();
if (GB(r, 0, 8) < rate) {
CargoID cid = cs->Index();
@ -708,8 +709,8 @@ static void TownGenerateCargoOriginal(Town *t, TownProductionEffect tpe, uint8_t
*/
static void TownGenerateCargoBinominal(Town *t, TownProductionEffect tpe, uint8_t rate, StationFinder &stations)
{
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[tpe]) {
CargoID cid = cs->Index();
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[tpe])) {
const CargoSpec *cs = CargoSpec::Get(cid);
uint32_t r = Random();
/* Make a bitmask with up to 32 bits set, one for each potential pax. */
@ -891,13 +892,13 @@ void AddProducedHouseCargo(HouseID house_id, TileIndex tile, CargoArray &produce
}
} else {
if (hs->population > 0) {
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_PASSENGERS]) {
produced[cs->Index()]++;
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS])) {
produced[cid]++;
}
}
if (hs->mail_generation > 0) {
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_MAIL]) {
produced[cs->Index()]++;
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_MAIL])) {
produced[cid]++;
}
}
}
@ -2184,11 +2185,11 @@ void UpdateTownRadii()
void UpdateTownMaxPass(Town *t)
{
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_PASSENGERS]) {
t->supplied[cs->Index()].old_max = _town_cargo_scaler.Scale(t->cache.population >> 3);
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS])) {
t->supplied[cid].old_max = _town_cargo_scaler.Scale(t->cache.population >> 3);
}
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[TPE_MAIL]) {
t->supplied[cs->Index()].old_max = _town_cargo_scaler.Scale(t->cache.population >> 4);
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_MAIL])) {
t->supplied[cid].old_max = _town_cargo_scaler.Scale(t->cache.population >> 4);
}
}
@ -4180,8 +4181,8 @@ static uint GetNormalGrowthRate(Town *t)
for (auto tpe : {TPE_PASSENGERS, TPE_MAIL}) {
uint32_t old_max = 0;
uint32_t old_act = 0;
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[tpe]) {
const TransportedCargoStat<uint32_t> &stat = t->supplied[cs->Index()];
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[tpe])) {
const TransportedCargoStat<uint32_t> &stat = t->supplied[cid];
old_max += stat.old_max;
old_act += stat.old_act;
}

@ -725,7 +725,7 @@ public:
*/
uint GetDesiredInfoHeight(int width) const
{
uint aimed_height = static_cast<uint>(1 + CargoSpec::town_production_cargoes[TPE_PASSENGERS].size() + CargoSpec::town_production_cargoes[TPE_MAIL].size()) * GetCharacterHeight(FS_NORMAL);
uint aimed_height = static_cast<uint>(1 + CountBits(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS] | CargoSpec::town_production_cargo_mask[TPE_MAIL])) * GetCharacterHeight(FS_NORMAL);
bool first = true;
for (int i = TAE_BEGIN; i < TAE_END; i++) {

@ -194,10 +194,8 @@ SpriteID TileZoneCheckUnservedBuildingsEvaluation(TileIndex tile, Owner owner)
}
auto has_town_cargo = [&](const CargoArray &dat) {
for (auto tpe : {TPE_PASSENGERS, TPE_MAIL}) {
for (const CargoSpec *cs : CargoSpec::town_production_cargoes[tpe]) {
if (dat[cs->Index()] > 0) return true;
}
for (CargoID cid : SetCargoBitIterator(CargoSpec::town_production_cargo_mask[TPE_PASSENGERS] | CargoSpec::town_production_cargo_mask[TPE_MAIL])) {
if (dat[cid] > 0) return true;
}
return false;
};

Loading…
Cancel
Save