Codechange: Use IsValidCargoID/IsValidCargoType.

IsValidCargoType() is used only for unmapped IDs.
pull/564/head
Peter Nelson 1 year ago committed by PeterN
parent c4ca6a0f74
commit 76516d7f70

@ -108,7 +108,7 @@ static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_t
const Engine *e = Engine::Get(engine); const Engine *e = Engine::Get(engine);
CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID); CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
if (cargo_type != nullptr) *cargo_type = cargo; if (cargo_type != nullptr) *cargo_type = cargo;
if (cargo == CT_INVALID) return 0; if (!IsValidCargoID(cargo)) return 0;
return e->GetDisplayDefaultCapacity(); return e->GetDisplayDefaultCapacity();
} }
@ -251,8 +251,8 @@ bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *car
CargoID first_cargo = CT_INVALID; CargoID first_cargo = CT_INVALID;
do { do {
if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) { if (IsValidCargoID(v->cargo_type) && v->GetEngine()->CanCarryCargo()) {
if (first_cargo == CT_INVALID) first_cargo = v->cargo_type; if (!IsValidCargoID(first_cargo)) first_cargo = v->cargo_type;
if (first_cargo != v->cargo_type) { if (first_cargo != v->cargo_type) {
if (cargo_type != nullptr) *cargo_type = CT_INVALID; if (cargo_type != nullptr) *cargo_type = CT_INVALID;
return true; return true;

@ -238,7 +238,7 @@ static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool
CargoID cargo_type; CargoID cargo_type;
if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargoes in an automated way if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargoes in an automated way
if (cargo_type == CT_INVALID) { if (!IsValidCargoID(cargo_type)) {
if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine. if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine.
if (!part_of_chain) return CT_NO_REFIT; if (!part_of_chain) return CT_NO_REFIT;
@ -321,7 +321,7 @@ static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehic
/* Does it need to be refitted */ /* Does it need to be refitted */
CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain); CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
if (refit_cargo == CT_INVALID) { if (!IsValidCargoID(refit_cargo)) {
if (!IsLocalCompany()) return CommandCost(); if (!IsLocalCompany()) return CommandCost();
SetDParam(0, old_veh->index); SetDParam(0, old_veh->index);

@ -1328,7 +1328,7 @@ struct BuildVehicleWindow : Window {
this->te.cost = ret.GetCost() - e->GetCost(); this->te.cost = ret.GetCost() - e->GetCost();
this->te.capacity = refit_capacity; this->te.capacity = refit_capacity;
this->te.mail_capacity = refit_mail; this->te.mail_capacity = refit_mail;
this->te.cargo = (cargo == CT_INVALID) ? e->GetDefaultCargoType() : cargo; this->te.cargo = !IsValidCargoID(cargo) ? e->GetDefaultCargoType() : cargo;
this->te.all_capacities = cargo_capacities; this->te.all_capacities = cargo_capacities;
return; return;
} }

@ -88,7 +88,7 @@ CargoID GetDefaultCargoID(LandscapeID l, CargoType ct)
{ {
assert(l < lengthof(_default_climate_cargo)); assert(l < lengthof(_default_climate_cargo));
if (ct == CT_INVALID) return CT_INVALID; if (!IsValidCargoType(ct)) return CT_INVALID;
assert(ct < lengthof(_default_climate_cargo[0])); assert(ct < lengthof(_default_climate_cargo[0]));
CargoLabel cl = _default_climate_cargo[l][ct]; CargoLabel cl = _default_climate_cargo[l][ct];

@ -184,7 +184,7 @@ bool Engine::CanCarryCargo() const
default: NOT_REACHED(); default: NOT_REACHED();
} }
return this->GetDefaultCargoType() != CT_INVALID; return IsValidCargoID(this->GetDefaultCargoType());
} }
@ -1256,7 +1256,7 @@ bool IsEngineRefittable(EngineID engine)
CargoID default_cargo = e->GetDefaultCargoType(); CargoID default_cargo = e->GetDefaultCargoType();
CargoTypes default_cargo_mask = 0; CargoTypes default_cargo_mask = 0;
SetBit(default_cargo_mask, default_cargo); SetBit(default_cargo_mask, default_cargo);
return default_cargo != CT_INVALID && ei->refit_mask != default_cargo_mask; return IsValidCargoID(default_cargo) && ei->refit_mask != default_cargo_mask;
} }
/** /**

@ -117,7 +117,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline int GetCargoProducedIndex(CargoID cargo) const inline int GetCargoProducedIndex(CargoID cargo) const
{ {
if (cargo == CT_INVALID) return -1; if (!IsValidCargoID(cargo)) return -1;
const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo); const CargoID *pos = std::find(this->produced_cargo, endof(this->produced_cargo), cargo);
if (pos == endof(this->produced_cargo)) return -1; if (pos == endof(this->produced_cargo)) return -1;
return pos - this->produced_cargo; return pos - this->produced_cargo;
@ -125,7 +125,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline int GetCargoAcceptedIndex(CargoID cargo) const inline int GetCargoAcceptedIndex(CargoID cargo) const
{ {
if (cargo == CT_INVALID) return -1; if (!IsValidCargoID(cargo)) return -1;
const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo); const CargoID *pos = std::find(this->accepts_cargo, endof(this->accepts_cargo), cargo);
if (pos == endof(this->accepts_cargo)) return -1; if (pos == endof(this->accepts_cargo)) return -1;
return pos - this->accepts_cargo; return pos - this->accepts_cargo;

@ -452,7 +452,7 @@ static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, Ca
for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) { for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
CargoID a = accepts_cargo[i]; CargoID a = accepts_cargo[i];
if (a == CT_INVALID || cargo_acceptance[i] <= 0) continue; // work only with valid cargoes if (!IsValidCargoID(a) || cargo_acceptance[i] <= 0) continue; // work only with valid cargoes
/* Add accepted cargo */ /* Add accepted cargo */
acceptance[a] += cargo_acceptance[i]; acceptance[a] += cargo_acceptance[i];
@ -534,7 +534,7 @@ static bool TransportIndustryGoods(TileIndex tile)
for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) { for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
uint cw = ClampTo<uint8_t>(i->produced_cargo_waiting[j]); uint cw = ClampTo<uint8_t>(i->produced_cargo_waiting[j]);
if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) { if (cw > indspec->minimal_cargo && IsValidCargoID(i->produced_cargo[j])) {
i->produced_cargo_waiting[j] -= cw; i->produced_cargo_waiting[j] -= cw;
/* fluctuating economy? */ /* fluctuating economy? */
@ -991,7 +991,7 @@ bool IsTileForestIndustry(TileIndex tile)
/* Check for wood production */ /* Check for wood production */
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
/* The industry produces wood. */ /* The industry produces wood. */
if (ind->produced_cargo[i] != CT_INVALID && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true; if (IsValidCargoID(ind->produced_cargo[i]) && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true;
} }
return false; return false;
@ -1867,7 +1867,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
/* Industries without "unlimited" cargo types support depend on the specific order/slots of cargo types. /* Industries without "unlimited" cargo types support depend on the specific order/slots of cargo types.
* They need to be able to blank out specific slots without aborting the callback sequence, * They need to be able to blank out specific slots without aborting the callback sequence,
* and solve this by returning undefined cargo indexes. Skip these. */ * and solve this by returning undefined cargo indexes. Skip these. */
if (cargo == CT_INVALID && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue; if (!IsValidCargoID(cargo) && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue;
/* Verify valid cargo */ /* Verify valid cargo */
if (std::find(indspec->accepts_cargo, endof(indspec->accepts_cargo), cargo) == endof(indspec->accepts_cargo)) { if (std::find(indspec->accepts_cargo, endof(indspec->accepts_cargo), cargo) == endof(indspec->accepts_cargo)) {
/* Cargo not in spec, error in NewGRF */ /* Cargo not in spec, error in NewGRF */
@ -1897,7 +1897,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
} }
CargoID cargo = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile); CargoID cargo = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
/* Allow older GRFs to skip slots. */ /* Allow older GRFs to skip slots. */
if (cargo == CT_INVALID && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue; if (!IsValidCargoID(cargo) && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue;
/* Verify valid cargo */ /* Verify valid cargo */
if (std::find(indspec->produced_cargo, endof(indspec->produced_cargo), cargo) == endof(indspec->produced_cargo)) { if (std::find(indspec->produced_cargo, endof(indspec->produced_cargo), cargo) == endof(indspec->produced_cargo)) {
/* Cargo not in spec, error in NewGRF */ /* Cargo not in spec, error in NewGRF */
@ -2420,7 +2420,7 @@ void GenerateIndustries()
static void UpdateIndustryStatistics(Industry *i) static void UpdateIndustryStatistics(Industry *i)
{ {
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] != CT_INVALID) { if (IsValidCargoID(i->produced_cargo[j])) {
byte pct = 0; byte pct = 0;
if (i->this_month_production[j] != 0) { if (i->this_month_production[j] != 0) {
i->last_prod_year = TimerGameCalendar::year; i->last_prod_year = TimerGameCalendar::year;
@ -2622,7 +2622,7 @@ static bool CheckIndustryCloseDownProtection(IndustryType type)
*/ */
static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces) static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
{ {
if (cargo == CT_INVALID) return; if (!IsValidCargoID(cargo)) return;
/* Check for acceptance of cargo */ /* Check for acceptance of cargo */
for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) { for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
@ -2800,7 +2800,7 @@ static void ChangeIndustryProduction(Industry *i, bool monthly)
} else if (_settings_game.economy.type == ET_SMOOTH) { } else if (_settings_game.economy.type == ET_SMOOTH) {
closeit = !(i->ctlflags & (INDCTL_NO_CLOSURE | INDCTL_NO_PRODUCTION_DECREASE)); closeit = !(i->ctlflags & (INDCTL_NO_CLOSURE | INDCTL_NO_PRODUCTION_DECREASE));
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue; if (!IsValidCargoID(i->produced_cargo[j])) continue;
uint32 r = Random(); uint32 r = Random();
int old_prod, new_prod, percent; int old_prod, new_prod, percent;
/* If over 60% is transported, mult is 1, else mult is -1. */ /* If over 60% is transported, mult is 1, else mult is -1. */

@ -158,7 +158,7 @@ static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixTy
if (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) { if (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) {
/* Reworked behaviour with new many-in-many-out scheme */ /* Reworked behaviour with new many-in-many-out scheme */
for (uint j = 0; j < lengthof(suffixes); j++) { for (uint j = 0; j < lengthof(suffixes); j++) {
if (cargoes[j] != CT_INVALID) { if (IsValidCargoID(cargoes[j])) {
byte local_id = indspec->grf_prop.grffile->cargo_map[cargoes[j]]; // should we check the value for valid? byte local_id = indspec->grf_prop.grffile->cargo_map[cargoes[j]]; // should we check the value for valid?
uint cargotype = local_id << 16 | use_input; uint cargotype = local_id << 16 | use_input;
GetCargoSuffix(cargotype, cst, ind, ind_type, indspec, suffixes[j]); GetCargoSuffix(cargotype, cst, ind, ind_type, indspec, suffixes[j]);
@ -175,13 +175,13 @@ static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixTy
} }
switch (use_input) { switch (use_input) {
case CARGOSUFFIX_OUT: case CARGOSUFFIX_OUT:
if (cargoes[0] != CT_INVALID) GetCargoSuffix(3, cst, ind, ind_type, indspec, suffixes[0]); if (IsValidCargoID(cargoes[0])) GetCargoSuffix(3, cst, ind, ind_type, indspec, suffixes[0]);
if (cargoes[1] != CT_INVALID) GetCargoSuffix(4, cst, ind, ind_type, indspec, suffixes[1]); if (IsValidCargoID(cargoes[1])) GetCargoSuffix(4, cst, ind, ind_type, indspec, suffixes[1]);
break; break;
case CARGOSUFFIX_IN: case CARGOSUFFIX_IN:
if (cargoes[0] != CT_INVALID) GetCargoSuffix(0, cst, ind, ind_type, indspec, suffixes[0]); if (IsValidCargoID(cargoes[0])) GetCargoSuffix(0, cst, ind, ind_type, indspec, suffixes[0]);
if (cargoes[1] != CT_INVALID) GetCargoSuffix(1, cst, ind, ind_type, indspec, suffixes[1]); if (IsValidCargoID(cargoes[1])) GetCargoSuffix(1, cst, ind, ind_type, indspec, suffixes[1]);
if (cargoes[2] != CT_INVALID) GetCargoSuffix(2, cst, ind, ind_type, indspec, suffixes[2]); if (IsValidCargoID(cargoes[2])) GetCargoSuffix(2, cst, ind, ind_type, indspec, suffixes[2]);
break; break;
default: default:
NOT_REACHED(); NOT_REACHED();
@ -346,7 +346,7 @@ class BuildIndustryWindow : public Window {
int firstcargo = -1; int firstcargo = -1;
for (int j = 0; j < cargolistlen; j++) { for (int j = 0; j < cargolistlen; j++) {
if (cargolist[j] == CT_INVALID) continue; if (!IsValidCargoID(cargolist[j])) continue;
numcargo++; numcargo++;
if (firstcargo < 0) { if (firstcargo < 0) {
firstcargo = j; firstcargo = j;
@ -845,7 +845,7 @@ public:
bool stockpiling = HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS); bool stockpiling = HasBit(ind->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(ind->callback_mask, CBM_IND_PRODUCTION_256_TICKS);
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] == CT_INVALID) continue; if (!IsValidCargoID(i->accepts_cargo[j])) continue;
has_accept = true; has_accept = true;
if (first) { if (first) {
DrawString(ir, STR_INDUSTRY_VIEW_REQUIRES); DrawString(ir, STR_INDUSTRY_VIEW_REQUIRES);
@ -885,7 +885,7 @@ public:
int button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2; int button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2;
first = true; first = true;
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue; if (!IsValidCargoID(i->produced_cargo[j])) continue;
if (first) { if (first) {
if (has_accept) ir.top += WidgetDimensions::scaled.vsep_wide; if (has_accept) ir.top += WidgetDimensions::scaled.vsep_wide;
DrawString(ir, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE); DrawString(ir, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE);
@ -981,7 +981,7 @@ public:
if (pt.y >= this->production_offset_y) { if (pt.y >= this->production_offset_y) {
int row = (pt.y - this->production_offset_y) / this->cheat_line_height; int row = (pt.y - this->production_offset_y) / this->cheat_line_height;
for (uint j = 0; j < lengthof(i->produced_cargo); j++) { for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue; if (!IsValidCargoID(i->produced_cargo[j])) continue;
row--; row--;
if (row < 0) { if (row < 0) {
line = (InfoLine)(IL_RATE1 + j); line = (InfoLine)(IL_RATE1 + j);
@ -1140,7 +1140,7 @@ static void UpdateIndustryProduction(Industry *i)
if (indspec->UsesOriginalEconomy()) i->RecomputeProductionMultipliers(); if (indspec->UsesOriginalEconomy()) i->RecomputeProductionMultipliers();
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] != CT_INVALID) { if (IsValidCargoID(i->produced_cargo[j])) {
i->last_month_production[j] = 8 * i->production_rate[j]; i->last_month_production[j] = 8 * i->production_rate[j];
} }
} }
@ -1244,7 +1244,7 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair<
case CF_NONE: case CF_NONE:
accepted_cargo_matches = std::all_of(std::begin((*industry)->accepts_cargo), std::end((*industry)->accepts_cargo), [](CargoID cargo) { accepted_cargo_matches = std::all_of(std::begin((*industry)->accepts_cargo), std::end((*industry)->accepts_cargo), [](CargoID cargo) {
return cargo == CT_INVALID; return !IsValidCargoID(cargo);
}); });
break; break;
@ -1263,7 +1263,7 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair<
case CF_NONE: case CF_NONE:
produced_cargo_matches = std::all_of(std::begin((*industry)->produced_cargo), std::end((*industry)->produced_cargo), [](CargoID cargo) { produced_cargo_matches = std::all_of(std::begin((*industry)->produced_cargo), std::end((*industry)->produced_cargo), [](CargoID cargo) {
return cargo == CT_INVALID; return !IsValidCargoID(cargo);
}); });
break; break;
@ -1425,7 +1425,7 @@ protected:
{ {
assert(id < lengthof(i->produced_cargo)); assert(id < lengthof(i->produced_cargo));
if (i->produced_cargo[id] == CT_INVALID) return -1; if (!IsValidCargoID(i->produced_cargo[id])) return -1;
return ToPercent8(i->last_month_pct_transported[id]); return ToPercent8(i->last_month_pct_transported[id]);
} }
@ -1489,8 +1489,8 @@ protected:
uint prod_a = 0, prod_b = 0; uint prod_a = 0, prod_b = 0;
for (uint i = 0; i < lengthof(a->produced_cargo); i++) { for (uint i = 0; i < lengthof(a->produced_cargo); i++) {
if (filter == CF_ANY) { if (filter == CF_ANY) {
if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i]; if (IsValidCargoID(a->produced_cargo[i])) prod_a += a->last_month_production[i];
if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i]; if (IsValidCargoID(b->produced_cargo[i])) prod_b += b->last_month_production[i];
} else { } else {
if (a->produced_cargo[i] == filter) prod_a += a->last_month_production[i]; if (a->produced_cargo[i] == filter) prod_a += a->last_month_production[i];
if (b->produced_cargo[i] == filter) prod_b += b->last_month_production[i]; if (b->produced_cargo[i] == filter) prod_b += b->last_month_production[i];
@ -1534,7 +1534,7 @@ protected:
std::vector<CargoInfo> cargos; std::vector<CargoInfo> cargos;
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] == CT_INVALID) continue; if (!IsValidCargoID(i->produced_cargo[j])) continue;
cargos.push_back({ i->produced_cargo[j], i->last_month_production[j], cargo_suffix[j].text.c_str(), ToPercent8(i->last_month_pct_transported[j]) }); cargos.push_back({ i->produced_cargo[j], i->last_month_production[j], cargo_suffix[j].text.c_str(), ToPercent8(i->last_month_pct_transported[j]) });
} }
@ -1969,7 +1969,7 @@ struct CargoesField {
int ConnectCargo(CargoID cargo, bool producer) int ConnectCargo(CargoID cargo, bool producer)
{ {
assert(this->type == CFT_CARGO); assert(this->type == CFT_CARGO);
if (cargo == INVALID_CARGO) return -1; if (!IsValidCargoID(cargo)) return -1;
/* Find the vertical cargo column carrying the cargo. */ /* Find the vertical cargo column carrying the cargo. */
int column = -1; int column = -1;
@ -1982,10 +1982,10 @@ struct CargoesField {
if (column < 0) return -1; if (column < 0) return -1;
if (producer) { if (producer) {
assert(this->u.cargo.supp_cargoes[column] == INVALID_CARGO); assert(!IsValidCargoID(this->u.cargo.supp_cargoes[column]));
this->u.cargo.supp_cargoes[column] = column; this->u.cargo.supp_cargoes[column] = column;
} else { } else {
assert(this->u.cargo.cust_cargoes[column] == INVALID_CARGO); assert(!IsValidCargoID(this->u.cargo.cust_cargoes[column]));
this->u.cargo.cust_cargoes[column] = column; this->u.cargo.cust_cargoes[column] = column;
} }
return column; return column;
@ -2000,8 +2000,8 @@ struct CargoesField {
assert(this->type == CFT_CARGO); assert(this->type == CFT_CARGO);
for (uint i = 0; i < MAX_CARGOES; i++) { for (uint i = 0; i < MAX_CARGOES; i++) {
if (this->u.cargo.supp_cargoes[i] != INVALID_CARGO) return true; if (IsValidCargoID(this->u.cargo.supp_cargoes[i])) return true;
if (this->u.cargo.cust_cargoes[i] != INVALID_CARGO) return true; if (IsValidCargoID(this->u.cargo.cust_cargoes[i])) return true;
} }
return false; return false;
} }
@ -2021,7 +2021,7 @@ struct CargoesField {
uint i; uint i;
uint num = 0; uint num = 0;
for (i = 0; i < MAX_CARGOES && i < length; i++) { for (i = 0; i < MAX_CARGOES && i < length; i++) {
if (cargoes[i] != INVALID_CARGO) { if (IsValidCargoID(cargoes[i])) {
this->u.cargo.vertical_cargoes[num] = cargoes[i]; this->u.cargo.vertical_cargoes[num] = cargoes[i];
num++; num++;
} }
@ -2128,13 +2128,13 @@ struct CargoesField {
} }
ypos1 += CargoesField::cargo_border.height + (FONT_HEIGHT_NORMAL - CargoesField::cargo_line.height) / 2; ypos1 += CargoesField::cargo_border.height + (FONT_HEIGHT_NORMAL - CargoesField::cargo_line.height) / 2;
for (uint i = 0; i < CargoesField::max_cargoes; i++) { for (uint i = 0; i < CargoesField::max_cargoes; i++) {
if (other_right[i] != INVALID_CARGO) { if (IsValidCargoID(other_right[i])) {
const CargoSpec *csp = CargoSpec::Get(other_right[i]); const CargoSpec *csp = CargoSpec::Get(other_right[i]);
int xp = xpos + industry_width + CargoesField::cargo_stub.width; int xp = xpos + industry_width + CargoesField::cargo_stub.width;
DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp); DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp);
GfxDrawLine(xp, ypos1, xp, ypos1 + CargoesField::cargo_line.height - 1, CARGO_LINE_COLOUR); GfxDrawLine(xp, ypos1, xp, ypos1 + CargoesField::cargo_line.height - 1, CARGO_LINE_COLOUR);
} }
if (other_left[i] != INVALID_CARGO) { if (IsValidCargoID(other_left[i])) {
const CargoSpec *csp = CargoSpec::Get(other_left[i]); const CargoSpec *csp = CargoSpec::Get(other_left[i]);
int xp = xpos - CargoesField::cargo_stub.width; int xp = xpos - CargoesField::cargo_stub.width;
DrawHorConnection(xp + 1, xpos - 1, ypos1, csp); DrawHorConnection(xp + 1, xpos - 1, ypos1, csp);
@ -2172,7 +2172,7 @@ struct CargoesField {
} }
ypos += CargoesField::cargo_border.height + vert_inter_industry_space / 2 + (FONT_HEIGHT_NORMAL - CargoesField::cargo_line.height) / 2; ypos += CargoesField::cargo_border.height + vert_inter_industry_space / 2 + (FONT_HEIGHT_NORMAL - CargoesField::cargo_line.height) / 2;
for (uint i = 0; i < MAX_CARGOES; i++) { for (uint i = 0; i < MAX_CARGOES; i++) {
if (hor_left[i] != INVALID_CARGO) { if (IsValidCargoID(hor_left[i])) {
int col = hor_left[i]; int col = hor_left[i];
int dx = 0; int dx = 0;
const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]); const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
@ -2183,7 +2183,7 @@ struct CargoesField {
} }
DrawHorConnection(xpos, cargo_base - dx, ypos, csp); DrawHorConnection(xpos, cargo_base - dx, ypos, csp);
} }
if (hor_right[i] != INVALID_CARGO) { if (IsValidCargoID(hor_right[i])) {
int col = hor_right[i]; int col = hor_right[i];
int dx = 0; int dx = 0;
const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]); const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]);
@ -2202,7 +2202,7 @@ struct CargoesField {
case CFT_CARGO_LABEL: case CFT_CARGO_LABEL:
ypos += CargoesField::cargo_border.height + vert_inter_industry_space / 2; ypos += CargoesField::cargo_border.height + vert_inter_industry_space / 2;
for (uint i = 0; i < MAX_CARGOES; i++) { for (uint i = 0; i < MAX_CARGOES; i++) {
if (this->u.cargo_label.cargoes[i] != INVALID_CARGO) { if (IsValidCargoID(this->u.cargo_label.cargoes[i])) {
const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]); const CargoSpec *csp = CargoSpec::Get(this->u.cargo_label.cargoes[i]);
DrawString(xpos + WidgetDimensions::scaled.framerect.left, xpos + industry_width - 1 - WidgetDimensions::scaled.framerect.right, ypos, csp->name, TC_WHITE, DrawString(xpos + WidgetDimensions::scaled.framerect.left, xpos + industry_width - 1 - WidgetDimensions::scaled.framerect.right, ypos, csp->name, TC_WHITE,
(this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT); (this->u.cargo_label.left_align) ? SA_LEFT : SA_RIGHT);
@ -2248,7 +2248,7 @@ struct CargoesField {
/* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */ /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */
if (col == 0) { if (col == 0) {
if (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]]; if (IsValidCargoID(this->u.cargo.supp_cargoes[row])) return this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]];
if (left != nullptr) { if (left != nullptr) {
if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row]; if (left->type == CFT_INDUSTRY) return left->u.industry.other_produced[row];
if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row]; if (left->type == CFT_CARGO_LABEL && !left->u.cargo_label.left_align) return left->u.cargo_label.cargoes[row];
@ -2256,7 +2256,7 @@ struct CargoesField {
return INVALID_CARGO; return INVALID_CARGO;
} }
if (col == this->u.cargo.num_cargoes) { if (col == this->u.cargo.num_cargoes) {
if (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]]; if (IsValidCargoID(this->u.cargo.cust_cargoes[row])) return this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]];
if (right != nullptr) { if (right != nullptr) {
if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row]; if (right->type == CFT_INDUSTRY) return right->u.industry.other_accepted[row];
if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row]; if (right->type == CFT_CARGO_LABEL && right->u.cargo_label.left_align) return right->u.cargo_label.cargoes[row];
@ -2268,10 +2268,10 @@ struct CargoesField {
* Since the horizontal connection is made in the same order as the vertical list, the above condition * Since the horizontal connection is made in the same order as the vertical list, the above condition
* ensures we are left-below the main diagonal, thus at the supplying side. * ensures we are left-below the main diagonal, thus at the supplying side.
*/ */
return (this->u.cargo.supp_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO; return (IsValidCargoID(this->u.cargo.supp_cargoes[row])) ? this->u.cargo.vertical_cargoes[this->u.cargo.supp_cargoes[row]] : INVALID_CARGO;
} else { } else {
/* Clicked at a customer connection. */ /* Clicked at a customer connection. */
return (this->u.cargo.cust_cargoes[row] != INVALID_CARGO) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO; return (IsValidCargoID(this->u.cargo.cust_cargoes[row])) ? this->u.cargo.vertical_cargoes[this->u.cargo.cust_cargoes[row]] : INVALID_CARGO;
} }
} }
@ -2361,7 +2361,7 @@ struct CargoesRow {
/* Allocate other cargoes in the empty holes of the horizontal cargo connections. */ /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
for (uint i = 0; i < CargoesField::max_cargoes && other_count > 0; i++) { for (uint i = 0; i < CargoesField::max_cargoes && other_count > 0; i++) {
if (cargo_fld->u.cargo.supp_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_produced[i] = others[--other_count]; if (!IsValidCargoID(cargo_fld->u.cargo.supp_cargoes[i])) ind_fld->u.industry.other_produced[i] = others[--other_count];
} }
} else { } else {
/* Houses only display what is demanded. */ /* Houses only display what is demanded. */
@ -2419,7 +2419,7 @@ struct CargoesRow {
/* Allocate other cargoes in the empty holes of the horizontal cargo connections. */ /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */
for (uint i = 0; i < CargoesField::max_cargoes && other_count > 0; i++) { for (uint i = 0; i < CargoesField::max_cargoes && other_count > 0; i++) {
if (cargo_fld->u.cargo.cust_cargoes[i] == INVALID_CARGO) ind_fld->u.industry.other_accepted[i] = others[--other_count]; if (!IsValidCargoID(cargo_fld->u.cargo.cust_cargoes[i])) ind_fld->u.industry.other_accepted[i] = others[--other_count];
} }
} else { } else {
/* Houses only display what is demanded. */ /* Houses only display what is demanded. */
@ -2602,7 +2602,7 @@ struct IndustryCargoesWindow : public Window {
static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2) static bool HasCommonValidCargo(const CargoID *cargoes1, uint length1, const CargoID *cargoes2, uint length2)
{ {
while (length1 > 0) { while (length1 > 0) {
if (*cargoes1 != INVALID_CARGO) { if (IsValidCargoID(*cargoes1)) {
for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true; for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true;
} }
cargoes1++; cargoes1++;
@ -2620,7 +2620,7 @@ struct IndustryCargoesWindow : public Window {
static bool HousesCanSupply(const CargoID *cargoes, uint length) static bool HousesCanSupply(const CargoID *cargoes, uint length)
{ {
for (uint i = 0; i < length; i++) { for (uint i = 0; i < length; i++) {
if (cargoes[i] == INVALID_CARGO) continue; if (!IsValidCargoID(cargoes[i])) continue;
if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true; if (cargoes[i] == CT_PASSENGERS || cargoes[i] == CT_MAIL) return true;
} }
return false; return false;
@ -2643,7 +2643,7 @@ struct IndustryCargoesWindow : public Window {
default: NOT_REACHED(); default: NOT_REACHED();
} }
for (uint i = 0; i < length; i++) { for (uint i = 0; i < length; i++) {
if (cargoes[i] == INVALID_CARGO) continue; if (!IsValidCargoID(cargoes[i])) continue;
for (uint h = 0; h < NUM_HOUSES; h++) { for (uint h = 0; h < NUM_HOUSES; h++) {
HouseSpec *hs = HouseSpec::Get(h); HouseSpec *hs = HouseSpec::Get(h);
@ -3012,13 +3012,13 @@ struct IndustryCargoesWindow : public Window {
CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : nullptr; CargoesField *lft = (fieldxy.x > 0) ? this->fields[fieldxy.y].columns + fieldxy.x - 1 : nullptr;
CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : nullptr; CargoesField *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : nullptr;
CargoID cid = fld->CargoClickedAt(lft, rgt, xy); CargoID cid = fld->CargoClickedAt(lft, rgt, xy);
if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid); if (IsValidCargoID(cid)) this->ComputeCargoDisplay(cid);
break; break;
} }
case CFT_CARGO_LABEL: { case CFT_CARGO_LABEL: {
CargoID cid = fld->CargoLabelClickedAt(xy); CargoID cid = fld->CargoLabelClickedAt(xy);
if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid); if (IsValidCargoID(cid)) this->ComputeCargoDisplay(cid);
break; break;
} }
@ -3113,7 +3113,7 @@ struct IndustryCargoesWindow : public Window {
default: default:
break; break;
} }
if (cid != INVALID_CARGO && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) { if (IsValidCargoID(cid) && (this->ind_cargo < NUM_INDUSTRYTYPES || cid != this->ind_cargo - NUM_INDUSTRYTYPES)) {
const CargoSpec *csp = CargoSpec::Get(cid); const CargoSpec *csp = CargoSpec::Get(cid);
uint64 params[5]; uint64 params[5];
params[0] = csp->name; params[0] = csp->name;

@ -944,7 +944,7 @@ static CargoTypes TranslateRefitMask(uint32 refit_mask)
CargoTypes result = 0; CargoTypes result = 0;
for (uint8 bit : SetBitIterator(refit_mask)) { for (uint8 bit : SetBitIterator(refit_mask)) {
CargoID cargo = GetCargoTranslation(bit, _cur.grffile, true); CargoID cargo = GetCargoTranslation(bit, _cur.grffile, true);
if (cargo != CT_INVALID) SetBit(result, cargo); if (IsValidCargoID(cargo)) SetBit(result, cargo);
} }
return result; return result;
} }
@ -1306,8 +1306,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop
ctt = 0; ctt = 0;
while (count--) { while (count--) {
CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
if (ctype == CT_INVALID) continue; if (IsValidCargoID(ctype)) SetBit(ctt, ctype);
SetBit(ctt, ctype);
} }
break; break;
} }
@ -1516,8 +1515,7 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop
ctt = 0; ctt = 0;
while (count--) { while (count--) {
CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
if (ctype == CT_INVALID) continue; if (IsValidCargoID(ctype)) SetBit(ctt, ctype);
SetBit(ctt, ctype);
} }
break; break;
} }
@ -1700,8 +1698,7 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop
ctt = 0; ctt = 0;
while (count--) { while (count--) {
CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
if (ctype == CT_INVALID) continue; if (IsValidCargoID(ctype)) SetBit(ctt, ctype);
SetBit(ctt, ctype);
} }
break; break;
} }
@ -1862,8 +1859,7 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int
ctt = 0; ctt = 0;
while (count--) { while (count--) {
CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
if (ctype == CT_INVALID) continue; if (IsValidCargoID(ctype)) SetBit(ctt, ctype);
SetBit(ctt, ctype);
} }
break; break;
} }
@ -2547,7 +2543,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt
uint8 cargo_part = GB(cargotypes, 8 * j, 8); uint8 cargo_part = GB(cargotypes, 8 * j, 8);
CargoID cargo = GetCargoTranslation(cargo_part, _cur.grffile); CargoID cargo = GetCargoTranslation(cargo_part, _cur.grffile);
if (cargo == CT_INVALID) { if (!IsValidCargoID(cargo)) {
/* Disable acceptance of invalid cargo type */ /* Disable acceptance of invalid cargo type */
housespec->cargo_acceptance[j] = 0; housespec->cargo_acceptance[j] = 0;
} else { } else {
@ -2565,7 +2561,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt
byte count = buf->ReadByte(); byte count = buf->ReadByte();
for (byte j = 0; j < count; j++) { for (byte j = 0; j < count; j++) {
CargoID cargo = GetCargoTranslation(buf->ReadByte(), _cur.grffile); CargoID cargo = GetCargoTranslation(buf->ReadByte(), _cur.grffile);
if (cargo != CT_INVALID) SetBit(housespec->watched_cargoes, cargo); if (IsValidCargoID(cargo)) SetBit(housespec->watched_cargoes, cargo);
} }
break; break;
} }
@ -5457,7 +5453,7 @@ static void NewSpriteGroup(ByteReader *buf)
for (uint i = 0; i < group->num_input; i++) { for (uint i = 0; i < group->num_input; i++) {
byte rawcargo = buf->ReadByte(); byte rawcargo = buf->ReadByte();
CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile); CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile);
if (cargo == CT_INVALID) { if (!IsValidCargoID(cargo)) {
/* The mapped cargo is invalid. This is permitted at this point, /* The mapped cargo is invalid. This is permitted at this point,
* as long as the result is not used. Mark it invalid so this * as long as the result is not used. Mark it invalid so this
* can be tested later. */ * can be tested later. */
@ -5479,7 +5475,7 @@ static void NewSpriteGroup(ByteReader *buf)
for (uint i = 0; i < group->num_output; i++) { for (uint i = 0; i < group->num_output; i++) {
byte rawcargo = buf->ReadByte(); byte rawcargo = buf->ReadByte();
CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile); CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile);
if (cargo == CT_INVALID) { if (!IsValidCargoID(cargo)) {
/* Mark this result as invalid to use */ /* Mark this result as invalid to use */
group->version = 0xFF; group->version = 0xFF;
} else if (std::find(group->cargo_output, group->cargo_output + i, cargo) != group->cargo_output + i) { } else if (std::find(group->cargo_output, group->cargo_output + i, cargo) != group->cargo_output + i) {
@ -5553,7 +5549,7 @@ static CargoID TranslateCargo(uint8 feature, uint8 ctype)
} }
ctype = GetCargoIDByLabel(cl); ctype = GetCargoIDByLabel(cl);
if (ctype == CT_INVALID) { if (!IsValidCargoID(ctype)) {
GrfMsg(5, "TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' unsupported, skipping.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8)); GrfMsg(5, "TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' unsupported, skipping.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8));
return CT_INVALID; return CT_INVALID;
} }
@ -5623,7 +5619,7 @@ static void VehicleMapSpriteGroup(ByteReader *buf, byte feature, uint8 idcount)
GrfMsg(8, "VehicleMapSpriteGroup: * [{}] Cargo type 0x{:X}, group id 0x{:02X}", c, ctype, groupid); GrfMsg(8, "VehicleMapSpriteGroup: * [{}] Cargo type 0x{:X}, group id 0x{:02X}", c, ctype, groupid);
ctype = TranslateCargo(feature, ctype); ctype = TranslateCargo(feature, ctype);
if (ctype == CT_INVALID) continue; if (!IsValidCargoID(ctype)) continue;
for (uint i = 0; i < idcount; i++) { for (uint i = 0; i < idcount; i++) {
EngineID engine = engines[i]; EngineID engine = engines[i];
@ -5702,7 +5698,7 @@ static void StationMapSpriteGroup(ByteReader *buf, uint8 idcount)
if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue; if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue;
ctype = TranslateCargo(GSF_STATIONS, ctype); ctype = TranslateCargo(GSF_STATIONS, ctype);
if (ctype == CT_INVALID) continue; if (!IsValidCargoID(ctype)) continue;
for (auto &station : stations) { for (auto &station : stations) {
StationSpec *statspec = station >= _cur.grffile->stations.size() ? nullptr : _cur.grffile->stations[station].get(); StationSpec *statspec = station >= _cur.grffile->stations.size() ? nullptr : _cur.grffile->stations[station].get();
@ -5883,7 +5879,7 @@ static void ObjectMapSpriteGroup(ByteReader *buf, uint8 idcount)
if (!IsValidGroupID(groupid, "ObjectMapSpriteGroup")) continue; if (!IsValidGroupID(groupid, "ObjectMapSpriteGroup")) continue;
ctype = TranslateCargo(GSF_OBJECTS, ctype); ctype = TranslateCargo(GSF_OBJECTS, ctype);
if (ctype == CT_INVALID) continue; if (!IsValidCargoID(ctype)) continue;
for (auto &object : objects) { for (auto &object : objects) {
ObjectSpec *spec = object >= _cur.grffile->objectspec.size() ? nullptr : _cur.grffile->objectspec[object].get(); ObjectSpec *spec = object >= _cur.grffile->objectspec.size() ? nullptr : _cur.grffile->objectspec[object].get();
@ -6069,7 +6065,7 @@ static void RoadStopMapSpriteGroup(ByteReader *buf, uint8 idcount)
if (!IsValidGroupID(groupid, "RoadStopMapSpriteGroup")) continue; if (!IsValidGroupID(groupid, "RoadStopMapSpriteGroup")) continue;
ctype = TranslateCargo(GSF_ROADSTOPS, ctype); ctype = TranslateCargo(GSF_ROADSTOPS, ctype);
if (ctype == CT_INVALID) continue; if (!IsValidCargoID(ctype)) continue;
for (auto &roadstop : roadstops) { for (auto &roadstop : roadstops) {
RoadStopSpec *roadstopspec = roadstop >= _cur.grffile->roadstops.size() ? nullptr : _cur.grffile->roadstops[roadstop].get(); RoadStopSpec *roadstopspec = roadstop >= _cur.grffile->roadstops.size() ? nullptr : _cur.grffile->roadstops[roadstop].get();
@ -6844,9 +6840,9 @@ static void SkipIf(ByteReader *buf)
if (condtype >= 0x0B) { if (condtype >= 0x0B) {
/* Tests that ignore 'param' */ /* Tests that ignore 'param' */
switch (condtype) { switch (condtype) {
case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID; case 0x0B: result = !IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val)));
break; break;
case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID; case 0x0C: result = IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val)));
break; break;
case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE; case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE;
break; break;
@ -8945,7 +8941,7 @@ static void CalculateRefitMasks()
CargoTypes original_known_cargoes = 0; CargoTypes original_known_cargoes = 0;
for (int ct = 0; ct != NUM_ORIGINAL_CARGO; ++ct) { for (int ct = 0; ct != NUM_ORIGINAL_CARGO; ++ct) {
CargoID cid = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast<CargoType>(ct)); CargoID cid = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast<CargoType>(ct));
if (cid != CT_INVALID) SetBit(original_known_cargoes, cid); if (IsValidCargoID(cid)) SetBit(original_known_cargoes, cid);
} }
for (Engine *e : Engine::Iterate()) { for (Engine *e : Engine::Iterate()) {
@ -9035,7 +9031,7 @@ static void CalculateRefitMasks()
/* Translate cargo_type using the original climate-specific cargo table. */ /* Translate cargo_type using the original climate-specific cargo table. */
ei->cargo_type = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast<CargoType>(ei->cargo_type)); ei->cargo_type = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast<CargoType>(ei->cargo_type));
if (ei->cargo_type != CT_INVALID) ClrBit(_gted[engine].ctt_exclude_mask, ei->cargo_type); if (IsValidCargoID(ei->cargo_type)) ClrBit(_gted[engine].ctt_exclude_mask, ei->cargo_type);
} }
/* Compute refittability */ /* Compute refittability */
@ -9064,17 +9060,17 @@ static void CalculateRefitMasks()
} }
/* Clear invalid cargoslots (from default vehicles or pre-NewCargo GRFs) */ /* Clear invalid cargoslots (from default vehicles or pre-NewCargo GRFs) */
if (ei->cargo_type != CT_INVALID && !HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID; if (IsValidCargoID(ei->cargo_type) && !HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID;
/* Ensure that the vehicle is either not refittable, or that the default cargo is one of the refittable cargoes. /* Ensure that the vehicle is either not refittable, or that the default cargo is one of the refittable cargoes.
* Note: Vehicles refittable to no cargo are handle differently to vehicle refittable to a single cargo. The latter might have subtypes. */ * Note: Vehicles refittable to no cargo are handle differently to vehicle refittable to a single cargo. The latter might have subtypes. */
if (!only_defaultcargo && (e->type != VEH_SHIP || e->u.ship.old_refittable) && ei->cargo_type != CT_INVALID && !HasBit(ei->refit_mask, ei->cargo_type)) { if (!only_defaultcargo && (e->type != VEH_SHIP || e->u.ship.old_refittable) && IsValidCargoID(ei->cargo_type) && !HasBit(ei->refit_mask, ei->cargo_type)) {
ei->cargo_type = CT_INVALID; ei->cargo_type = CT_INVALID;
} }
/* Check if this engine's cargo type is valid. If not, set to the first refittable /* Check if this engine's cargo type is valid. If not, set to the first refittable
* cargo type. Finally disable the vehicle, if there is still no cargo. */ * cargo type. Finally disable the vehicle, if there is still no cargo. */
if (ei->cargo_type == CT_INVALID && ei->refit_mask != 0) { if (!IsValidCargoID(ei->cargo_type) && ei->refit_mask != 0) {
/* Figure out which CTT to use for the default cargo, if it is 'first refittable'. */ /* Figure out which CTT to use for the default cargo, if it is 'first refittable'. */
const uint8 *cargo_map_for_first_refittable = nullptr; const uint8 *cargo_map_for_first_refittable = nullptr;
{ {
@ -9097,12 +9093,12 @@ static void CalculateRefitMasks()
} }
} }
if (ei->cargo_type == CT_INVALID) { if (!IsValidCargoID(ei->cargo_type)) {
/* Use first refittable cargo slot */ /* Use first refittable cargo slot */
ei->cargo_type = (CargoID)FindFirstBit(ei->refit_mask); ei->cargo_type = (CargoID)FindFirstBit(ei->refit_mask);
} }
} }
if (ei->cargo_type == CT_INVALID) ei->climates = 0; if (!IsValidCargoID(ei->cargo_type)) ei->climates = 0;
/* Clear refit_mask for not refittable ships */ /* Clear refit_mask for not refittable ships */
if (e->type == VEH_SHIP && !e->u.ship.old_refittable) { if (e->type == VEH_SHIP && !e->u.ship.old_refittable) {

@ -503,7 +503,7 @@ struct NewGRFInspectWindow : Window {
break; break;
case NIT_CARGO: case NIT_CARGO:
string = value != INVALID_CARGO ? CargoSpec::Get(value)->name : STR_QUANTITY_N_A; string = IsValidCargoID(value) ? CargoSpec::Get(value)->name : STR_QUANTITY_N_A;
break; break;
default: default:

@ -964,7 +964,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
case 0x47: { // Vehicle cargo info case 0x47: { // Vehicle cargo info
const Engine *e = Engine::Get(this->self_type); const Engine *e = Engine::Get(this->self_type);
CargoID cargo_type = e->GetDefaultCargoType(); CargoID cargo_type = e->GetDefaultCargoType();
if (cargo_type != CT_INVALID) { if (IsValidCargoID(cargo_type)) {
const CargoSpec *cs = CargoSpec::Get(cargo_type); const CargoSpec *cs = CargoSpec::Get(cargo_type);
return (cs->classes << 16) | (cs->weight << 8) | this->ro.grffile->cargo_map[cargo_type]; return (cs->classes << 16) | (cs->weight << 8) | this->ro.grffile->cargo_map[cargo_type];
} else { } else {

@ -340,7 +340,7 @@ static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseI
/* Cargo acceptance history of nearby stations */ /* Cargo acceptance history of nearby stations */
case 0x64: { case 0x64: {
CargoID cid = GetCargoTranslation(parameter, this->ro.grffile); CargoID cid = GetCargoTranslation(parameter, this->ro.grffile);
if (cid == CT_INVALID) return 0; if (!IsValidCargoID(cid)) return 0;
/* Extract tile offset. */ /* Extract tile offset. */
int8 x_offs = GB(GetRegister(0x100), 0, 8); int8 x_offs = GB(GetRegister(0x100), 0, 8);

@ -316,7 +316,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
case 0x70: case 0x70:
case 0x71: { case 0x71: {
CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile); CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile);
if (cargo == CT_INVALID) return 0; if (!IsValidCargoID(cargo)) return 0;
int index = this->industry->GetCargoProducedIndex(cargo); int index = this->industry->GetCargoProducedIndex(cargo);
if (index < 0) return 0; // invalid cargo if (index < 0) return 0; // invalid cargo
switch (variable) { switch (variable) {
@ -335,7 +335,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
case 0x6E: case 0x6E:
case 0x6F: { case 0x6F: {
CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile); CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile);
if (cargo == CT_INVALID) return 0; if (!IsValidCargoID(cargo)) return 0;
int index = this->industry->GetCargoAcceptedIndex(cargo); int index = this->industry->GetCargoAcceptedIndex(cargo);
if (index < 0) return 0; // invalid cargo if (index < 0) return 0; // invalid cargo
if (variable == 0x6E) return this->industry->last_cargo_accepted_at[index]; if (variable == 0x6E) return this->industry->last_cargo_accepted_at[index];

@ -346,7 +346,7 @@ void TriggerRoadStopAnimation(BaseStation *st, TileIndex trigger_tile, StationAn
const RoadStopSpec *ss = GetRoadStopSpec(cur_tile); const RoadStopSpec *ss = GetRoadStopSpec(cur_tile);
if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) { if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) {
CargoID cargo; CargoID cargo;
if (cargo_type == CT_INVALID) { if (!IsValidCargoID(cargo_type)) {
cargo = CT_INVALID; cargo = CT_INVALID;
} else { } else {
cargo = ss->grf_prop.grffile->cargo_map[cargo_type]; cargo = ss->grf_prop.grffile->cargo_map[cargo_type];
@ -379,7 +379,7 @@ void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTri
/* Check the cached cargo trigger bitmask to see if we need /* Check the cached cargo trigger bitmask to see if we need
* to bother with any further processing. */ * to bother with any further processing. */
if (st->cached_roadstop_cargo_triggers == 0) return; if (st->cached_roadstop_cargo_triggers == 0) return;
if (cargo_type != CT_INVALID && !HasBit(st->cached_roadstop_cargo_triggers, cargo_type)) return; if (IsValidCargoID(cargo_type) && !HasBit(st->cached_roadstop_cargo_triggers, cargo_type)) return;
SetBit(st->waiting_triggers, trigger); SetBit(st->waiting_triggers, trigger);
@ -406,7 +406,7 @@ void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTri
if ((ss->cargo_triggers & ~empty_mask) != 0) return; if ((ss->cargo_triggers & ~empty_mask) != 0) return;
} }
if (cargo_type == CT_INVALID || HasBit(ss->cargo_triggers, cargo_type)) { if (!IsValidCargoID(cargo_type) || HasBit(ss->cargo_triggers, cargo_type)) {
RoadStopResolverObject object(ss, st, cur_tile, INVALID_ROADTYPE, GetStationType(cur_tile), GetStationGfx(cur_tile)); RoadStopResolverObject object(ss, st, cur_tile, INVALID_ROADTYPE, GetStationType(cur_tile), GetStationGfx(cur_tile));
object.waiting_triggers = st->waiting_triggers; object.waiting_triggers = st->waiting_triggers;

@ -412,7 +412,7 @@ uint32 Station::GetNewGRFVariable(const ResolverObject &object, byte variable, b
if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) { if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) {
CargoID c = GetCargoTranslation(parameter, object.grffile); CargoID c = GetCargoTranslation(parameter, object.grffile);
if (c == CT_INVALID) { if (!IsValidCargoID(c)) {
switch (variable) { switch (variable) {
case 0x62: return 0xFFFFFFFF; case 0x62: return 0xFFFFFFFF;
case 0x64: return 0xFF00; case 0x64: return 0xFF00;
@ -931,7 +931,7 @@ void TriggerStationAnimation(BaseStation *st, TileIndex trigger_tile, StationAni
const StationSpec *ss = GetStationSpec(tile); const StationSpec *ss = GetStationSpec(tile);
if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) { if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) {
CargoID cargo; CargoID cargo;
if (cargo_type == CT_INVALID) { if (!IsValidCargoID(cargo_type)) {
cargo = CT_INVALID; cargo = CT_INVALID;
} else { } else {
cargo = ss->grf_prop.grffile->cargo_map[cargo_type]; cargo = ss->grf_prop.grffile->cargo_map[cargo_type];
@ -962,7 +962,7 @@ void TriggerStationRandomisation(Station *st, TileIndex trigger_tile, StationRan
/* Check the cached cargo trigger bitmask to see if we need /* Check the cached cargo trigger bitmask to see if we need
* to bother with any further processing. */ * to bother with any further processing. */
if (st->cached_cargo_triggers == 0) return; if (st->cached_cargo_triggers == 0) return;
if (cargo_type != CT_INVALID && !HasBit(st->cached_cargo_triggers, cargo_type)) return; if (IsValidCargoID(cargo_type) && !HasBit(st->cached_cargo_triggers, cargo_type)) return;
uint32 whole_reseed = 0; uint32 whole_reseed = 0;
ETileArea area = ETileArea(st, trigger_tile, tas[trigger]); ETileArea area = ETileArea(st, trigger_tile, tas[trigger]);
@ -993,7 +993,7 @@ void TriggerStationRandomisation(Station *st, TileIndex trigger_tile, StationRan
if ((ss->cargo_triggers & ~empty_mask) != 0) continue; if ((ss->cargo_triggers & ~empty_mask) != 0) continue;
} }
if (cargo_type == CT_INVALID || HasBit(ss->cargo_triggers, cargo_type)) { if (!IsValidCargoID(cargo_type) || HasBit(ss->cargo_triggers, cargo_type)) {
StationResolverObject object(ss, st, tile, CBID_RANDOM_TRIGGER, 0); StationResolverObject object(ss, st, tile, CBID_RANDOM_TRIGGER, 0);
object.waiting_triggers = st->waiting_triggers; object.waiting_triggers = st->waiting_triggers;

@ -3023,7 +3023,7 @@ bool AfterLoadGame()
/* Make sure last_cargo_accepted_at is copied to elements for every valid input cargo. /* Make sure last_cargo_accepted_at is copied to elements for every valid input cargo.
* The loading routine should put the original singular value into the first array element. */ * The loading routine should put the original singular value into the first array element. */
for (size_t ci = 0; ci < lengthof(i->accepts_cargo); ci++) { for (size_t ci = 0; ci < lengthof(i->accepts_cargo); ci++) {
if (i->accepts_cargo[ci] != CT_INVALID) { if (IsValidCargoID(i->accepts_cargo[ci])) {
i->last_cargo_accepted_at[ci] = i->last_cargo_accepted_at[0]; i->last_cargo_accepted_at[ci] = i->last_cargo_accepted_at[0];
} else { } else {
i->last_cargo_accepted_at[ci] = 0; i->last_cargo_accepted_at[ci] = 0;

@ -1440,7 +1440,7 @@ static bool LoadOldSubsidy(LoadgameState *ls, int num)
{ {
Subsidy *s = new (num) Subsidy(); Subsidy *s = new (num) Subsidy();
bool ret = LoadChunk(ls, s, subsidy_chunk); bool ret = LoadChunk(ls, s, subsidy_chunk);
if (s->cargo_type == CT_INVALID) delete s; if (!IsValidCargoID(s->cargo_type)) delete s;
return ret; return ret;
} }

@ -31,7 +31,7 @@ ScriptCargoList_IndustryAccepting::ScriptCargoList_IndustryAccepting(IndustryID
Industry *ind = ::Industry::Get(industry_id); Industry *ind = ::Industry::Get(industry_id);
for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) { for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) {
CargoID cargo_id = ind->accepts_cargo[i]; CargoID cargo_id = ind->accepts_cargo[i];
if (cargo_id != CT_INVALID) { if (::IsValidCargoID(cargo_id)) {
this->AddItem(cargo_id); this->AddItem(cargo_id);
} }
} }
@ -44,7 +44,7 @@ ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID
Industry *ind = ::Industry::Get(industry_id); Industry *ind = ::Industry::Get(industry_id);
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
CargoID cargo_id = ind->produced_cargo[i]; CargoID cargo_id = ind->produced_cargo[i];
if (cargo_id != CT_INVALID) { if (::IsValidCargoID(cargo_id)) {
this->AddItem(cargo_id); this->AddItem(cargo_id);
} }
} }

@ -234,7 +234,7 @@
Industry *i = Industry::GetIfValid(industry_id); Industry *i = Industry::GetIfValid(industry_id);
if (i == nullptr) return ScriptDate::DATE_INVALID; if (i == nullptr) return ScriptDate::DATE_INVALID;
if (cargo_type == CT_INVALID) { if (!::IsValidCargoID(cargo_type)) {
return (ScriptDate::Date)std::accumulate(std::begin(i->last_cargo_accepted_at), std::end(i->last_cargo_accepted_at), 0, [](TimerGameCalendar::Date a, TimerGameCalendar::Date b) { return std::max(a, b); }); return (ScriptDate::Date)std::accumulate(std::begin(i->last_cargo_accepted_at), std::end(i->last_cargo_accepted_at), 0, [](TimerGameCalendar::Date a, TimerGameCalendar::Date b) { return std::max(a, b); });
} else { } else {
int index = i->GetCargoAcceptedIndex(cargo_type); int index = i->GetCargoAcceptedIndex(cargo_type);

@ -72,7 +72,7 @@
ScriptList *list = new ScriptList(); ScriptList *list = new ScriptList();
for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) { for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) {
if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i]); if (::IsValidCargoID(ins->produced_cargo[i])) list->AddItem(ins->produced_cargo[i]);
} }
return list; return list;
@ -86,7 +86,7 @@
ScriptList *list = new ScriptList(); ScriptList *list = new ScriptList();
for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) { for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) {
if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i]); if (::IsValidCargoID(ins->accepts_cargo[i])) list->AddItem(ins->accepts_cargo[i]);
} }
return list; return list;

@ -86,7 +86,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in
{ {
bool cargo_accepts = false; bool cargo_accepts = false;
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] != CT_INVALID) cargo_accepts = true; if (::IsValidCargoID(i->accepts_cargo[j])) cargo_accepts = true;
} }
if (!cargo_accepts) return; if (!cargo_accepts) return;
} }
@ -104,7 +104,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in
{ {
bool cargo_accepts = false; bool cargo_accepts = false;
for (byte j = 0; j < lengthof(i->accepts_cargo); j++) { for (byte j = 0; j < lengthof(i->accepts_cargo); j++) {
if (i->accepts_cargo[j] != CT_INVALID && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true; if (::IsValidCargoID(i->accepts_cargo[j]) && acceptance[i->accepts_cargo[j]] != 0) cargo_accepts = true;
} }
if (!cargo_accepts) continue; if (!cargo_accepts) continue;
} }
@ -125,7 +125,7 @@ ScriptTileList_IndustryProducing::ScriptTileList_IndustryProducing(IndustryID in
/* Check if this industry produces anything */ /* Check if this industry produces anything */
bool cargo_produces = false; bool cargo_produces = false;
for (byte j = 0; j < lengthof(i->produced_cargo); j++) { for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
if (i->produced_cargo[j] != CT_INVALID) cargo_produces = true; if (::IsValidCargoID(i->produced_cargo[j])) cargo_produces = true;
} }
if (!cargo_produces) return; if (!cargo_produces) return;

@ -75,7 +75,7 @@
{ {
EnforceCompanyModeValid(VEHICLE_INVALID); EnforceCompanyModeValid(VEHICLE_INVALID);
EnforcePrecondition(VEHICLE_INVALID, ScriptEngine::IsBuildable(engine_id)); EnforcePrecondition(VEHICLE_INVALID, ScriptEngine::IsBuildable(engine_id));
EnforcePrecondition(VEHICLE_INVALID, cargo == CT_INVALID || ScriptCargo::IsValidCargo(cargo)); EnforcePrecondition(VEHICLE_INVALID, !::IsValidCargoID(cargo) || ScriptCargo::IsValidCargo(cargo));
::VehicleType type = ::Engine::Get(engine_id)->type; ::VehicleType type = ::Engine::Get(engine_id)->type;

@ -407,7 +407,7 @@ void Station::AddIndustryToDeliver(Industry *ind, TileIndex tile)
/* Include only industries that can accept cargo */ /* Include only industries that can accept cargo */
uint cargo_index; uint cargo_index;
for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) { for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
if (ind->accepts_cargo[cargo_index] != CT_INVALID) break; if (IsValidCargoID(ind->accepts_cargo[cargo_index])) break;
} }
if (cargo_index >= lengthof(ind->accepts_cargo)) return; if (cargo_index >= lengthof(ind->accepts_cargo)) return;

@ -173,7 +173,7 @@ static bool CMSAMine(TileIndex tile)
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
/* The industry extracts something non-liquid, i.e. no oil or plastic, so it is a mine. /* The industry extracts something non-liquid, i.e. no oil or plastic, so it is a mine.
* Also the production of passengers and mail is ignored. */ * Also the production of passengers and mail is ignored. */
if (ind->produced_cargo[i] != CT_INVALID && if (IsValidCargoID(ind->produced_cargo[i]) &&
(CargoSpec::Get(ind->produced_cargo[i])->classes & (CC_LIQUID | CC_PASSENGERS | CC_MAIL)) == 0) { (CargoSpec::Get(ind->produced_cargo[i])->classes & (CC_LIQUID | CC_PASSENGERS | CC_MAIL)) == 0) {
return true; return true;
} }
@ -534,7 +534,7 @@ CargoArray GetProductionAroundTiles(TileIndex north_tile, int w, int h, int rad)
for (uint j = 0; j < lengthof(i->produced_cargo); j++) { for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
CargoID cargo = i->produced_cargo[j]; CargoID cargo = i->produced_cargo[j];
if (cargo != CT_INVALID) produced[cargo]++; if (IsValidCargoID(cargo)) produced[cargo]++;
} }
} }

@ -1203,9 +1203,9 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
case SCC_CARGO_LONG: { // {CARGO_LONG} case SCC_CARGO_LONG: { // {CARGO_LONG}
/* First parameter is cargo type, second parameter is cargo count */ /* First parameter is cargo type, second parameter is cargo count */
CargoID cargo = args->GetInt32(SCC_CARGO_LONG); CargoID cargo = args->GetInt32(SCC_CARGO_LONG);
if (cargo != CT_INVALID && cargo >= CargoSpec::GetArraySize()) break; if (IsValidCargoID(cargo) && cargo >= CargoSpec::GetArraySize()) break;
StringID cargo_str = (cargo == CT_INVALID) ? STR_QUANTITY_N_A : CargoSpec::Get(cargo)->quantifier; StringID cargo_str = !IsValidCargoID(cargo) ? STR_QUANTITY_N_A : CargoSpec::Get(cargo)->quantifier;
StringParameters tmp_args(*args, 1); StringParameters tmp_args(*args, 1);
buff = GetStringWithArgs(buff, cargo_str, &tmp_args, last); buff = GetStringWithArgs(buff, cargo_str, &tmp_args, last);
break; break;

@ -388,12 +388,12 @@ bool FindSubsidyIndustryCargoRoute()
int num_cargos = 0; int num_cargos = 0;
uint cargo_index; uint cargo_index;
for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) { for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
if (src_ind->produced_cargo[cargo_index] != CT_INVALID) num_cargos++; if (IsValidCargoID(src_ind->produced_cargo[cargo_index])) num_cargos++;
} }
if (num_cargos == 0) return false; // industry produces nothing if (num_cargos == 0) return false; // industry produces nothing
int cargo_num = RandomRange(num_cargos) + 1; int cargo_num = RandomRange(num_cargos) + 1;
for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) { for (cargo_index = 0; cargo_index < lengthof(src_ind->produced_cargo); cargo_index++) {
if (src_ind->produced_cargo[cargo_index] != CT_INVALID) cargo_num--; if (IsValidCargoID(src_ind->produced_cargo[cargo_index])) cargo_num--;
if (cargo_num == 0) break; if (cargo_num == 0) break;
} }
assert(cargo_num == 0); // indicates loop didn't break as intended assert(cargo_num == 0); // indicates loop didn't break as intended
@ -405,7 +405,7 @@ bool FindSubsidyIndustryCargoRoute()
* or if the pct transported is already large enough * or if the pct transported is already large enough
* or if the cargo is automatically distributed */ * or if the cargo is automatically distributed */
if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED || if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED ||
cid == CT_INVALID || !IsValidCargoID(cid) ||
_settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) { _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) {
return false; return false;
} }

@ -561,7 +561,7 @@ static void TileLoop_Town(TileIndex tile)
if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile); CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile);
if (cargo == CT_INVALID) continue; if (!IsValidCargoID(cargo)) continue;
uint amt = GB(callback, 0, 8); uint amt = GB(callback, 0, 8);
if (amt == 0) continue; if (amt == 0) continue;
@ -706,7 +706,7 @@ static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced)
CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile); CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grf_prop.grffile);
if (cargo == CT_INVALID) continue; if (!IsValidCargoID(cargo)) continue;
produced[cargo]++; produced[cargo]++;
} }
} else { } else {
@ -721,7 +721,7 @@ static void AddProducedCargo_Town(TileIndex tile, CargoArray &produced)
static inline void AddAcceptedCargoSetMask(CargoID cargo, uint amount, CargoArray &acceptance, CargoTypes *always_accepted) static inline void AddAcceptedCargoSetMask(CargoID cargo, uint amount, CargoArray &acceptance, CargoTypes *always_accepted)
{ {
if (cargo == CT_INVALID || amount == 0) return; if (!IsValidCargoID(cargo) || amount == 0) return;
acceptance[cargo] += amount; acceptance[cargo] += amount;
SetBit(*always_accepted, cargo); SetBit(*always_accepted, cargo);
} }

@ -209,7 +209,7 @@ static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int rig
SetDParam(3, _settings_game.vehicle.freight_trains); SetDParam(3, _settings_game.vehicle.freight_trains);
str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM; str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
} else { } else {
str = item->cargo == INVALID_CARGO ? STR_QUANTITY_N_A : STR_VEHICLE_DETAILS_CARGO_EMPTY; str = !IsValidCargoID(item->cargo) ? STR_QUANTITY_N_A : STR_VEHICLE_DETAILS_CARGO_EMPTY;
} }
DrawString(left, right, y, str, TC_LIGHT_BLUE); DrawString(left, right, y, str, TC_LIGHT_BLUE);
@ -248,7 +248,7 @@ static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y) static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
{ {
StringID str; StringID str;
if (item->cargo != INVALID_CARGO) { if (IsValidCargoID(item->cargo)) {
SetDParam(0, item->cargo); SetDParam(0, item->cargo);
SetDParam(1, item->capacity); SetDParam(1, item->capacity);
SetDParam(4, item->subtype); SetDParam(4, item->subtype);
@ -276,7 +276,7 @@ static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *su
CargoSummaryItem new_item; CargoSummaryItem new_item;
new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO; new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO;
new_item.subtype = GetCargoSubtypeText(v); new_item.subtype = GetCargoSubtypeText(v);
if (new_item.cargo == INVALID_CARGO && new_item.subtype == STR_EMPTY) continue; if (!IsValidCargoID(new_item.cargo) && new_item.subtype == STR_EMPTY) continue;
auto item = std::find(summary->begin(), summary->end(), new_item); auto item = std::find(summary->begin(), summary->end(), new_item);
if (item == summary->end()) { if (item == summary->end()) {

@ -232,7 +232,7 @@ bool Vehicle::NeedsServicing() const
if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) continue; if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) continue;
/* Did the old vehicle carry anything? */ /* Did the old vehicle carry anything? */
if (cargo_type != CT_INVALID) { if (IsValidCargoID(cargo_type)) {
/* We can't refit the vehicle to carry the cargo we want */ /* We can't refit the vehicle to carry the cargo we want */
if (!HasBit(available_cargo_types, cargo_type)) continue; if (!HasBit(available_cargo_types, cargo_type)) continue;
} }
@ -1884,8 +1884,8 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
/* Note: Luckily cargo_type is not needed for engines */ /* Note: Luckily cargo_type is not needed for engines */
} }
if (cargo_type == CT_INVALID) cargo_type = e->GetDefaultCargoType(); if (!IsValidCargoID(cargo_type)) cargo_type = e->GetDefaultCargoType();
if (cargo_type == CT_INVALID) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo if (!IsValidCargoID(cargo_type)) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo
if (e->u.rail.railveh_type == RAILVEH_WAGON) { if (e->u.rail.railveh_type == RAILVEH_WAGON) {
if (!CargoSpec::Get(cargo_type)->is_freight) { if (!CargoSpec::Get(cargo_type)->is_freight) {
if (parent_engine_type == INVALID_ENGINE) { if (parent_engine_type == INVALID_ENGINE) {
@ -1924,8 +1924,8 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
e = Engine::Get(engine_type); e = Engine::Get(engine_type);
cargo_type = v->First()->cargo_type; cargo_type = v->First()->cargo_type;
} }
if (cargo_type == CT_INVALID) cargo_type = e->GetDefaultCargoType(); if (!IsValidCargoID(cargo_type)) cargo_type = e->GetDefaultCargoType();
if (cargo_type == CT_INVALID) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo if (!IsValidCargoID(cargo_type)) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo
/* Important: Use Tram Flag of front part. Luckily engine_type refers to the front part here. */ /* Important: Use Tram Flag of front part. Luckily engine_type refers to the front part here. */
if (HasBit(e->info.misc_flags, EF_ROAD_TRAM)) { if (HasBit(e->info.misc_flags, EF_ROAD_TRAM)) {
@ -1937,8 +1937,8 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_
} }
case VEH_SHIP: case VEH_SHIP:
if (cargo_type == CT_INVALID) cargo_type = e->GetDefaultCargoType(); if (!IsValidCargoID(cargo_type)) cargo_type = e->GetDefaultCargoType();
if (cargo_type == CT_INVALID) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo if (!IsValidCargoID(cargo_type)) cargo_type = CT_GOODS; // The vehicle does not carry anything, let's pick some freight cargo
return IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP; return IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP;
case VEH_AIRCRAFT: case VEH_AIRCRAFT:

@ -95,16 +95,16 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
if (!IsEngineBuildable(eid, type, _current_company)) return { CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type), INVALID_VEHICLE, 0, 0, {} }; if (!IsEngineBuildable(eid, type, _current_company)) return { CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type), INVALID_VEHICLE, 0, 0, {} };
/* Validate the cargo type. */ /* Validate the cargo type. */
if (cargo >= NUM_CARGO && cargo != CT_INVALID) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} }; if (cargo >= NUM_CARGO && IsValidCargoID(cargo)) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} };
const Engine *e = Engine::Get(eid); const Engine *e = Engine::Get(eid);
CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost()); CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
/* Engines without valid cargo should not be available */ /* Engines without valid cargo should not be available */
CargoID default_cargo = e->GetDefaultCargoType(); CargoID default_cargo = e->GetDefaultCargoType();
if (default_cargo == CT_INVALID) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} }; if (!IsValidCargoID(default_cargo)) return { CMD_ERROR, INVALID_VEHICLE, 0, 0, {} };
bool refitting = cargo != CT_INVALID && cargo != default_cargo; bool refitting = IsValidCargoID(cargo) && cargo != default_cargo;
/* Check whether the number of vehicles we need to build can be built according to pool space. */ /* Check whether the number of vehicles we need to build can be built according to pool space. */
uint num_vehicles; uint num_vehicles;
@ -951,7 +951,7 @@ std::tuple<CommandCost, VehicleID> CmdCloneVehicle(DoCommandFlag flags, TileInde
const Engine *e = v->GetEngine(); const Engine *e = v->GetEngine();
CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID); CargoID initial_cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID);
if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) { if (v->cargo_type != initial_cargo && IsValidCargoID(initial_cargo)) {
bool dummy; bool dummy;
total_cost.AddCost(GetRefitCost(nullptr, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy)); total_cost.AddCost(GetRefitCost(nullptr, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy));
} }

Loading…
Cancel
Save