diff --git a/src/articulated_vehicles.cpp b/src/articulated_vehicles.cpp index b3aa98fe0b..b5faa2b9de 100644 --- a/src/articulated_vehicles.cpp +++ b/src/articulated_vehicles.cpp @@ -108,7 +108,7 @@ static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_t const Engine *e = Engine::Get(engine); CargoID cargo = (e->CanCarryCargo() ? e->GetDefaultCargoType() : (CargoID)CT_INVALID); if (cargo_type != nullptr) *cargo_type = cargo; - if (cargo == CT_INVALID) return 0; + if (!IsValidCargoID(cargo)) return 0; return e->GetDisplayDefaultCapacity(); } @@ -251,8 +251,8 @@ bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *car CargoID first_cargo = CT_INVALID; do { - if (v->cargo_type != CT_INVALID && v->GetEngine()->CanCarryCargo()) { - if (first_cargo == CT_INVALID) first_cargo = v->cargo_type; + if (IsValidCargoID(v->cargo_type) && v->GetEngine()->CanCarryCargo()) { + if (!IsValidCargoID(first_cargo)) first_cargo = v->cargo_type; if (first_cargo != v->cargo_type) { if (cargo_type != nullptr) *cargo_type = CT_INVALID; return true; diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index c9ba7c36b8..50e6f94907 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -238,7 +238,7 @@ static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool CargoID cargo_type; 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 (!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 */ CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain); - if (refit_cargo == CT_INVALID) { + if (!IsValidCargoID(refit_cargo)) { if (!IsLocalCompany()) return CommandCost(); SetDParam(0, old_veh->index); diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index ded2102e78..ecda60450b 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1328,7 +1328,7 @@ struct BuildVehicleWindow : Window { this->te.cost = ret.GetCost() - e->GetCost(); this->te.capacity = refit_capacity; 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; return; } diff --git a/src/cargotype.cpp b/src/cargotype.cpp index e646490ddc..8fb40ee629 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -88,7 +88,7 @@ CargoID GetDefaultCargoID(LandscapeID l, CargoType ct) { 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])); CargoLabel cl = _default_climate_cargo[l][ct]; diff --git a/src/engine.cpp b/src/engine.cpp index 591710905e..a7847763a4 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -184,7 +184,7 @@ bool Engine::CanCarryCargo() const 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(); CargoTypes default_cargo_mask = 0; 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; } /** diff --git a/src/industry.h b/src/industry.h index 9574fc5f38..b3879b1534 100644 --- a/src/industry.h +++ b/src/industry.h @@ -117,7 +117,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { 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); if (pos == endof(this->produced_cargo)) return -1; return pos - this->produced_cargo; @@ -125,7 +125,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { 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); if (pos == endof(this->accepts_cargo)) return -1; return pos - this->accepts_cargo; diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 62f93cc295..ed8873acab 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -452,7 +452,7 @@ static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, Ca for (byte i = 0; i < lengthof(itspec->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 */ 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++) { uint cw = ClampTo(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; /* fluctuating economy? */ @@ -991,7 +991,7 @@ bool IsTileForestIndustry(TileIndex tile) /* Check for wood production */ for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { /* 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; @@ -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. * 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. */ - if (cargo == CT_INVALID && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue; + if (!IsValidCargoID(cargo) && !(indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED)) continue; /* Verify valid cargo */ if (std::find(indspec->accepts_cargo, endof(indspec->accepts_cargo), cargo) == endof(indspec->accepts_cargo)) { /* 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); /* 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 */ if (std::find(indspec->produced_cargo, endof(indspec->produced_cargo), cargo) == endof(indspec->produced_cargo)) { /* Cargo not in spec, error in NewGRF */ @@ -2420,7 +2420,7 @@ void GenerateIndustries() static void UpdateIndustryStatistics(Industry *i) { 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; if (i->this_month_production[j] != 0) { 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) { - if (cargo == CT_INVALID) return; + if (!IsValidCargoID(cargo)) return; /* Check for acceptance of cargo */ 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) { closeit = !(i->ctlflags & (INDCTL_NO_CLOSURE | INDCTL_NO_PRODUCTION_DECREASE)); 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(); int old_prod, new_prod, percent; /* If over 60% is transported, mult is 1, else mult is -1. */ diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 32c040845a..98ce724ad5 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -158,7 +158,7 @@ static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixTy if (indspec->behaviour & INDUSTRYBEH_CARGOTYPES_UNLIMITED) { /* Reworked behaviour with new many-in-many-out scheme */ 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? uint cargotype = local_id << 16 | use_input; GetCargoSuffix(cargotype, cst, ind, ind_type, indspec, suffixes[j]); @@ -175,13 +175,13 @@ static inline void GetAllCargoSuffixes(CargoSuffixInOut use_input, CargoSuffixTy } switch (use_input) { case CARGOSUFFIX_OUT: - if (cargoes[0] != CT_INVALID) 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[0])) GetCargoSuffix(3, cst, ind, ind_type, indspec, suffixes[0]); + if (IsValidCargoID(cargoes[1])) GetCargoSuffix(4, cst, ind, ind_type, indspec, suffixes[1]); break; case CARGOSUFFIX_IN: - if (cargoes[0] != CT_INVALID) GetCargoSuffix(0, cst, ind, ind_type, indspec, suffixes[0]); - if (cargoes[1] != CT_INVALID) 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[0])) GetCargoSuffix(0, cst, ind, ind_type, indspec, suffixes[0]); + if (IsValidCargoID(cargoes[1])) GetCargoSuffix(1, cst, ind, ind_type, indspec, suffixes[1]); + if (IsValidCargoID(cargoes[2])) GetCargoSuffix(2, cst, ind, ind_type, indspec, suffixes[2]); break; default: NOT_REACHED(); @@ -346,7 +346,7 @@ class BuildIndustryWindow : public Window { int firstcargo = -1; for (int j = 0; j < cargolistlen; j++) { - if (cargolist[j] == CT_INVALID) continue; + if (!IsValidCargoID(cargolist[j])) continue; numcargo++; if (firstcargo < 0) { 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); 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; if (first) { DrawString(ir, STR_INDUSTRY_VIEW_REQUIRES); @@ -885,7 +885,7 @@ public: int button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2; first = true; 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 (has_accept) ir.top += WidgetDimensions::scaled.vsep_wide; DrawString(ir, STR_INDUSTRY_VIEW_PRODUCTION_LAST_MONTH_TITLE); @@ -981,7 +981,7 @@ public: if (pt.y >= this->production_offset_y) { int row = (pt.y - this->production_offset_y) / this->cheat_line_height; 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--; if (row < 0) { line = (InfoLine)(IL_RATE1 + j); @@ -1140,7 +1140,7 @@ static void UpdateIndustryProduction(Industry *i) if (indspec->UsesOriginalEconomy()) i->RecomputeProductionMultipliers(); 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]; } } @@ -1244,7 +1244,7 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair< case CF_NONE: 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; @@ -1263,7 +1263,7 @@ static bool CDECL CargoFilter(const Industry * const *industry, const std::pair< case CF_NONE: 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; @@ -1425,7 +1425,7 @@ protected: { 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]); } @@ -1489,8 +1489,8 @@ protected: uint prod_a = 0, prod_b = 0; for (uint i = 0; i < lengthof(a->produced_cargo); i++) { if (filter == CF_ANY) { - if (a->produced_cargo[i] != CT_INVALID) prod_a += a->last_month_production[i]; - if (b->produced_cargo[i] != CT_INVALID) prod_b += b->last_month_production[i]; + if (IsValidCargoID(a->produced_cargo[i])) prod_a += a->last_month_production[i]; + if (IsValidCargoID(b->produced_cargo[i])) prod_b += b->last_month_production[i]; } else { 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]; @@ -1534,7 +1534,7 @@ protected: std::vector cargos; 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]) }); } @@ -1969,7 +1969,7 @@ struct CargoesField { int ConnectCargo(CargoID cargo, bool producer) { assert(this->type == CFT_CARGO); - if (cargo == INVALID_CARGO) return -1; + if (!IsValidCargoID(cargo)) return -1; /* Find the vertical cargo column carrying the cargo. */ int column = -1; @@ -1982,10 +1982,10 @@ struct CargoesField { if (column < 0) return -1; 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; } 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; } return column; @@ -2000,8 +2000,8 @@ struct CargoesField { assert(this->type == CFT_CARGO); for (uint i = 0; i < MAX_CARGOES; i++) { - if (this->u.cargo.supp_cargoes[i] != INVALID_CARGO) return true; - if (this->u.cargo.cust_cargoes[i] != INVALID_CARGO) return true; + if (IsValidCargoID(this->u.cargo.supp_cargoes[i])) return true; + if (IsValidCargoID(this->u.cargo.cust_cargoes[i])) return true; } return false; } @@ -2021,7 +2021,7 @@ struct CargoesField { uint i; uint num = 0; 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]; num++; } @@ -2128,13 +2128,13 @@ struct CargoesField { } ypos1 += CargoesField::cargo_border.height + (FONT_HEIGHT_NORMAL - CargoesField::cargo_line.height) / 2; 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]); int xp = xpos + industry_width + CargoesField::cargo_stub.width; DrawHorConnection(xpos + industry_width, xp - 1, ypos1, csp); 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]); int xp = xpos - CargoesField::cargo_stub.width; 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; 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 dx = 0; const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]); @@ -2183,7 +2183,7 @@ struct CargoesField { } DrawHorConnection(xpos, cargo_base - dx, ypos, csp); } - if (hor_right[i] != INVALID_CARGO) { + if (IsValidCargoID(hor_right[i])) { int col = hor_right[i]; int dx = 0; const CargoSpec *csp = CargoSpec::Get(this->u.cargo.vertical_cargoes[col]); @@ -2202,7 +2202,7 @@ struct CargoesField { case CFT_CARGO_LABEL: ypos += CargoesField::cargo_border.height + vert_inter_industry_space / 2; 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]); 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); @@ -2248,7 +2248,7 @@ struct CargoesField { /* row = 0 -> at first horizontal row, row = 1 -> second horizontal row, 2 = 3rd horizontal row. */ 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->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]; @@ -2256,7 +2256,7 @@ struct CargoesField { return INVALID_CARGO; } 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->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]; @@ -2268,10 +2268,10 @@ struct CargoesField { * 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. */ - 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 { /* 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. */ 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 { /* Houses only display what is demanded. */ @@ -2419,7 +2419,7 @@ struct CargoesRow { /* Allocate other cargoes in the empty holes of the horizontal cargo connections. */ 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 { /* 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) { while (length1 > 0) { - if (*cargoes1 != INVALID_CARGO) { + if (IsValidCargoID(*cargoes1)) { for (uint i = 0; i < length2; i++) if (*cargoes1 == cargoes2[i]) return true; } cargoes1++; @@ -2620,7 +2620,7 @@ struct IndustryCargoesWindow : public Window { static bool HousesCanSupply(const CargoID *cargoes, uint length) { 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; } return false; @@ -2643,7 +2643,7 @@ struct IndustryCargoesWindow : public Window { default: NOT_REACHED(); } 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++) { 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 *rgt = (fieldxy.x < 4) ? this->fields[fieldxy.y].columns + fieldxy.x + 1 : nullptr; CargoID cid = fld->CargoClickedAt(lft, rgt, xy); - if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid); + if (IsValidCargoID(cid)) this->ComputeCargoDisplay(cid); break; } case CFT_CARGO_LABEL: { CargoID cid = fld->CargoLabelClickedAt(xy); - if (cid != INVALID_CARGO) this->ComputeCargoDisplay(cid); + if (IsValidCargoID(cid)) this->ComputeCargoDisplay(cid); break; } @@ -3113,7 +3113,7 @@ struct IndustryCargoesWindow : public Window { default: 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); uint64 params[5]; params[0] = csp->name; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index ec1257223f..9d4d14e072 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -944,7 +944,7 @@ static CargoTypes TranslateRefitMask(uint32 refit_mask) CargoTypes result = 0; for (uint8 bit : SetBitIterator(refit_mask)) { CargoID cargo = GetCargoTranslation(bit, _cur.grffile, true); - if (cargo != CT_INVALID) SetBit(result, cargo); + if (IsValidCargoID(cargo)) SetBit(result, cargo); } return result; } @@ -1306,8 +1306,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop ctt = 0; while (count--) { CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); - if (ctype == CT_INVALID) continue; - SetBit(ctt, ctype); + if (IsValidCargoID(ctype)) SetBit(ctt, ctype); } break; } @@ -1516,8 +1515,7 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop ctt = 0; while (count--) { CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); - if (ctype == CT_INVALID) continue; - SetBit(ctt, ctype); + if (IsValidCargoID(ctype)) SetBit(ctt, ctype); } break; } @@ -1700,8 +1698,7 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop ctt = 0; while (count--) { CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); - if (ctype == CT_INVALID) continue; - SetBit(ctt, ctype); + if (IsValidCargoID(ctype)) SetBit(ctt, ctype); } break; } @@ -1862,8 +1859,7 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int ctt = 0; while (count--) { CargoID ctype = GetCargoTranslation(buf->ReadByte(), _cur.grffile); - if (ctype == CT_INVALID) continue; - SetBit(ctt, ctype); + if (IsValidCargoID(ctype)) SetBit(ctt, ctype); } break; } @@ -2547,7 +2543,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt uint8 cargo_part = GB(cargotypes, 8 * j, 8); CargoID cargo = GetCargoTranslation(cargo_part, _cur.grffile); - if (cargo == CT_INVALID) { + if (!IsValidCargoID(cargo)) { /* Disable acceptance of invalid cargo type */ housespec->cargo_acceptance[j] = 0; } else { @@ -2565,7 +2561,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt byte count = buf->ReadByte(); for (byte j = 0; j < count; j++) { 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; } @@ -5457,7 +5453,7 @@ static void NewSpriteGroup(ByteReader *buf) for (uint i = 0; i < group->num_input; i++) { byte rawcargo = buf->ReadByte(); CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile); - if (cargo == CT_INVALID) { + if (!IsValidCargoID(cargo)) { /* The mapped cargo is invalid. This is permitted at this point, * as long as the result is not used. Mark it invalid so this * can be tested later. */ @@ -5479,7 +5475,7 @@ static void NewSpriteGroup(ByteReader *buf) for (uint i = 0; i < group->num_output; i++) { byte rawcargo = buf->ReadByte(); CargoID cargo = GetCargoTranslation(rawcargo, _cur.grffile); - if (cargo == CT_INVALID) { + if (!IsValidCargoID(cargo)) { /* Mark this result as invalid to use */ group->version = 0xFF; } 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); - 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)); 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); ctype = TranslateCargo(feature, ctype); - if (ctype == CT_INVALID) continue; + if (!IsValidCargoID(ctype)) continue; for (uint i = 0; i < idcount; i++) { EngineID engine = engines[i]; @@ -5702,7 +5698,7 @@ static void StationMapSpriteGroup(ByteReader *buf, uint8 idcount) if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue; ctype = TranslateCargo(GSF_STATIONS, ctype); - if (ctype == CT_INVALID) continue; + if (!IsValidCargoID(ctype)) continue; for (auto &station : stations) { 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; ctype = TranslateCargo(GSF_OBJECTS, ctype); - if (ctype == CT_INVALID) continue; + if (!IsValidCargoID(ctype)) continue; for (auto &object : objects) { 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; ctype = TranslateCargo(GSF_ROADSTOPS, ctype); - if (ctype == CT_INVALID) continue; + if (!IsValidCargoID(ctype)) continue; for (auto &roadstop : roadstops) { 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) { /* Tests that ignore 'param' */ switch (condtype) { - case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID; + case 0x0B: result = !IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val))); break; - case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID; + case 0x0C: result = IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val))); break; case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE; break; @@ -8945,7 +8941,7 @@ static void CalculateRefitMasks() CargoTypes original_known_cargoes = 0; for (int ct = 0; ct != NUM_ORIGINAL_CARGO; ++ct) { CargoID cid = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast(ct)); - if (cid != CT_INVALID) SetBit(original_known_cargoes, cid); + if (IsValidCargoID(cid)) SetBit(original_known_cargoes, cid); } for (Engine *e : Engine::Iterate()) { @@ -9035,7 +9031,7 @@ static void CalculateRefitMasks() /* Translate cargo_type using the original climate-specific cargo table. */ ei->cargo_type = GetDefaultCargoID(_settings_game.game_creation.landscape, static_cast(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 */ @@ -9064,17 +9060,17 @@ static void CalculateRefitMasks() } /* 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. * 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; } /* 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. */ - 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'. */ 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 */ 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 */ if (e->type == VEH_SHIP && !e->u.ship.old_refittable) { diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index 8dd2dab408..bccae4f1bd 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -503,7 +503,7 @@ struct NewGRFInspectWindow : Window { break; 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; default: diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 1719057539..44651eee8b 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -964,7 +964,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, case 0x47: { // Vehicle cargo info const Engine *e = Engine::Get(this->self_type); CargoID cargo_type = e->GetDefaultCargoType(); - if (cargo_type != CT_INVALID) { + if (IsValidCargoID(cargo_type)) { const CargoSpec *cs = CargoSpec::Get(cargo_type); return (cs->classes << 16) | (cs->weight << 8) | this->ro.grffile->cargo_map[cargo_type]; } else { diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index b6fcdccc50..eb53449baa 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -340,7 +340,7 @@ static uint32 GetDistanceFromNearbyHouse(uint8 parameter, TileIndex tile, HouseI /* Cargo acceptance history of nearby stations */ case 0x64: { CargoID cid = GetCargoTranslation(parameter, this->ro.grffile); - if (cid == CT_INVALID) return 0; + if (!IsValidCargoID(cid)) return 0; /* Extract tile offset. */ int8 x_offs = GB(GetRegister(0x100), 0, 8); diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index 0ace93b0e2..fe0afcdf66 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -316,7 +316,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout case 0x70: case 0x71: { CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile); - if (cargo == CT_INVALID) return 0; + if (!IsValidCargoID(cargo)) return 0; int index = this->industry->GetCargoProducedIndex(cargo); if (index < 0) return 0; // invalid cargo switch (variable) { @@ -335,7 +335,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout case 0x6E: case 0x6F: { CargoID cargo = GetCargoTranslation(parameter, this->ro.grffile); - if (cargo == CT_INVALID) return 0; + if (!IsValidCargoID(cargo)) return 0; int index = this->industry->GetCargoAcceptedIndex(cargo); if (index < 0) return 0; // invalid cargo if (variable == 0x6E) return this->industry->last_cargo_accepted_at[index]; diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index a03ff256ee..097747a948 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -346,7 +346,7 @@ void TriggerRoadStopAnimation(BaseStation *st, TileIndex trigger_tile, StationAn const RoadStopSpec *ss = GetRoadStopSpec(cur_tile); if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) { CargoID cargo; - if (cargo_type == CT_INVALID) { + if (!IsValidCargoID(cargo_type)) { cargo = CT_INVALID; } else { 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 * to bother with any further processing. */ 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); @@ -406,7 +406,7 @@ void TriggerRoadStopRandomisation(Station *st, TileIndex tile, RoadStopRandomTri 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)); object.waiting_triggers = st->waiting_triggers; diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 9d43bd857c..7c1a5de1b2 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -412,7 +412,7 @@ uint32 Station::GetNewGRFVariable(const ResolverObject &object, byte variable, b if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) { CargoID c = GetCargoTranslation(parameter, object.grffile); - if (c == CT_INVALID) { + if (!IsValidCargoID(c)) { switch (variable) { case 0x62: return 0xFFFFFFFF; case 0x64: return 0xFF00; @@ -931,7 +931,7 @@ void TriggerStationAnimation(BaseStation *st, TileIndex trigger_tile, StationAni const StationSpec *ss = GetStationSpec(tile); if (ss != nullptr && HasBit(ss->animation.triggers, trigger)) { CargoID cargo; - if (cargo_type == CT_INVALID) { + if (!IsValidCargoID(cargo_type)) { cargo = CT_INVALID; } else { 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 * to bother with any further processing. */ 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; 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 (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); object.waiting_triggers = st->waiting_triggers; diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 7cdd8dbb0b..7c0c3de451 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -3023,7 +3023,7 @@ bool AfterLoadGame() /* 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. */ 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]; } else { i->last_cargo_accepted_at[ci] = 0; diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 56705daf87..c9f1f22e75 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1440,7 +1440,7 @@ static bool LoadOldSubsidy(LoadgameState *ls, int num) { Subsidy *s = new (num) Subsidy(); bool ret = LoadChunk(ls, s, subsidy_chunk); - if (s->cargo_type == CT_INVALID) delete s; + if (!IsValidCargoID(s->cargo_type)) delete s; return ret; } diff --git a/src/script/api/script_cargolist.cpp b/src/script/api/script_cargolist.cpp index 0019dc5e91..2fd6a10753 100644 --- a/src/script/api/script_cargolist.cpp +++ b/src/script/api/script_cargolist.cpp @@ -31,7 +31,7 @@ ScriptCargoList_IndustryAccepting::ScriptCargoList_IndustryAccepting(IndustryID Industry *ind = ::Industry::Get(industry_id); for (uint i = 0; i < lengthof(ind->accepts_cargo); i++) { CargoID cargo_id = ind->accepts_cargo[i]; - if (cargo_id != CT_INVALID) { + if (::IsValidCargoID(cargo_id)) { this->AddItem(cargo_id); } } @@ -44,7 +44,7 @@ ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID Industry *ind = ::Industry::Get(industry_id); for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { CargoID cargo_id = ind->produced_cargo[i]; - if (cargo_id != CT_INVALID) { + if (::IsValidCargoID(cargo_id)) { this->AddItem(cargo_id); } } diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp index 5bc98251c1..9bb9fdff0c 100644 --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -234,7 +234,7 @@ Industry *i = Industry::GetIfValid(industry_id); 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); }); } else { int index = i->GetCargoAcceptedIndex(cargo_type); diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp index 8d47e338cb..653f696cf1 100644 --- a/src/script/api/script_industrytype.cpp +++ b/src/script/api/script_industrytype.cpp @@ -72,7 +72,7 @@ ScriptList *list = new ScriptList(); 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; @@ -86,7 +86,7 @@ ScriptList *list = new ScriptList(); 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; diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp index ac6afdde1b..ac0b306739 100644 --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -86,7 +86,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in { bool cargo_accepts = false; 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; } @@ -104,7 +104,7 @@ ScriptTileList_IndustryAccepting::ScriptTileList_IndustryAccepting(IndustryID in { bool cargo_accepts = false; 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; } @@ -125,7 +125,7 @@ ScriptTileList_IndustryProducing::ScriptTileList_IndustryProducing(IndustryID in /* Check if this industry produces anything */ bool cargo_produces = false; 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; diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp index 1e06e4f5fb..e4d6abff2c 100644 --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -75,7 +75,7 @@ { EnforceCompanyModeValid(VEHICLE_INVALID); 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; diff --git a/src/station.cpp b/src/station.cpp index 02388b6384..6fc7b51b69 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -407,7 +407,7 @@ void Station::AddIndustryToDeliver(Industry *ind, TileIndex tile) /* Include only industries that can accept cargo */ uint 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; diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index d09710dcbc..e0cb2369ec 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -173,7 +173,7 @@ static bool CMSAMine(TileIndex tile) 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. * 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) { 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++) { CargoID cargo = i->produced_cargo[j]; - if (cargo != CT_INVALID) produced[cargo]++; + if (IsValidCargoID(cargo)) produced[cargo]++; } } diff --git a/src/strings.cpp b/src/strings.cpp index ed8d1bfc94..9d004f7c83 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -1203,9 +1203,9 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg case SCC_CARGO_LONG: { // {CARGO_LONG} /* First parameter is cargo type, second parameter is cargo count */ 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); buff = GetStringWithArgs(buff, cargo_str, &tmp_args, last); break; diff --git a/src/subsidy.cpp b/src/subsidy.cpp index 88879592eb..5c9e561d39 100644 --- a/src/subsidy.cpp +++ b/src/subsidy.cpp @@ -388,12 +388,12 @@ bool FindSubsidyIndustryCargoRoute() int num_cargos = 0; uint 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 int cargo_num = RandomRange(num_cargos) + 1; 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; } 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 cargo is automatically distributed */ if (total == 0 || trans > SUBSIDY_MAX_PCT_TRANSPORTED || - cid == CT_INVALID || + !IsValidCargoID(cid) || _settings_game.linkgraph.GetDistributionType(cid) != DT_MANUAL) { return false; } diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index ad110ae006..6d4b479591 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -561,7 +561,7 @@ static void TileLoop_Town(TileIndex tile) if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; 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); 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); - if (cargo == CT_INVALID) continue; + if (!IsValidCargoID(cargo)) continue; produced[cargo]++; } } 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) { - if (cargo == CT_INVALID || amount == 0) return; + if (!IsValidCargoID(cargo) || amount == 0) return; acceptance[cargo] += amount; SetBit(*always_accepted, cargo); } diff --git a/src/train_gui.cpp b/src/train_gui.cpp index 5a44282d44..a283515c49 100644 --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -209,7 +209,7 @@ static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int rig SetDParam(3, _settings_game.vehicle.freight_trains); str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM; } 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); @@ -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) { StringID str; - if (item->cargo != INVALID_CARGO) { + if (IsValidCargoID(item->cargo)) { SetDParam(0, item->cargo); SetDParam(1, item->capacity); SetDParam(4, item->subtype); @@ -276,7 +276,7 @@ static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *su CargoSummaryItem new_item; new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO; 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); if (item == summary->end()) { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index fb2f663569..29f03bd060 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -232,7 +232,7 @@ bool Vehicle::NeedsServicing() const if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) continue; /* 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 */ 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 */ } - if (cargo_type == CT_INVALID) 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 = e->GetDefaultCargoType(); + 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 (!CargoSpec::Get(cargo_type)->is_freight) { if (parent_engine_type == INVALID_ENGINE) { @@ -1924,8 +1924,8 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_ e = Engine::Get(engine_type); cargo_type = v->First()->cargo_type; } - if (cargo_type == CT_INVALID) 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 = e->GetDefaultCargoType(); + 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. */ if (HasBit(e->info.misc_flags, EF_ROAD_TRAM)) { @@ -1937,8 +1937,8 @@ LiveryScheme GetEngineLiveryScheme(EngineID engine_type, EngineID parent_engine_ } case VEH_SHIP: - if (cargo_type == CT_INVALID) 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 = e->GetDefaultCargoType(); + 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; case VEH_AIRCRAFT: diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 4c432d9eef..ed27394d24 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -95,16 +95,16 @@ std::tuple CmdBuildVehicle(DoC if (!IsEngineBuildable(eid, type, _current_company)) return { CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + type), INVALID_VEHICLE, 0, 0, {} }; /* 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); CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost()); /* Engines without valid cargo should not be available */ 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. */ uint num_vehicles; @@ -951,7 +951,7 @@ std::tuple CmdCloneVehicle(DoCommandFlag flags, TileInde const Engine *e = v->GetEngine(); 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; total_cost.AddCost(GetRefitCost(nullptr, v->engine_type, v->cargo_type, v->cargo_subtype, &dummy)); }