Changeset - r27421:e8c2cdc1e8e6
[Not reviewed]
src/articulated_vehicles.cpp
Show inline comments
 
@@ -105,13 +105,13 @@ uint CountArticulatedParts(EngineID engi
 
 */
 
static inline uint16 GetVehicleDefaultCapacity(EngineID engine, CargoID *cargo_type)
 
{
 
	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();
 
}
 

	
 
/**
 
 * Returns all cargoes a vehicle can carry.
 
 * @param engine the EngineID of interest
 
@@ -248,14 +248,14 @@ CargoTypes GetIntersectionOfArticulatedR
 
 */
 
bool IsArticulatedVehicleCarryingDifferentCargoes(const Vehicle *v, CargoID *cargo_type)
 
{
 
	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;
 
			}
 
		}
 

	
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -235,13 +235,13 @@ static CargoID GetNewCargoTypeForReplace
 

	
 
	if (union_mask == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
 

	
 
	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;
 

	
 
		/* the old engine didn't have cargo capacity, but the new one does
 
		 * now we will figure out what cargo the train is carrying and refit to fit this */
 
@@ -318,13 +318,13 @@ static CommandCost BuildReplacementVehic
 
	CommandCost cost = GetNewEngineType(old_veh, c, true, e);
 
	if (cost.Failed()) return cost;
 
	if (e == INVALID_ENGINE) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered
 

	
 
	/* 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);
 

	
 
		int order_id = GetIncompatibleRefitOrderIdForAutoreplace(old_veh, e);
 
		if (order_id != -1) {
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -1325,13 +1325,13 @@ struct BuildVehicleWindow : Window {
 
			/* Query for cost and refitted capacity */
 
			auto [ret, veh_id, refit_capacity, refit_mail, cargo_capacities] = Command<CMD_BUILD_VEHICLE>::Do(DC_QUERY_COST, this->window_number, this->sel_engine, true, cargo, INVALID_CLIENT_ID);
 
			if (ret.Succeeded()) {
 
				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;
 
			}
 
		}
 

	
 
		/* Purchase test was not possible or failed, fill in the defaults instead. */
src/cargotype.cpp
Show inline comments
 
@@ -85,13 +85,13 @@ void SetupCargoForClimate(LandscapeID l)
 
 * @return ID number if the cargo exists, else #CT_INVALID
 
 */
 
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];
 
	/* Bzzt: check if cl is just an index into the cargo table */
 
	if (cl < lengthof(_default_cargo)) {
 
		cl = _default_cargo[cl].label;
src/engine.cpp
Show inline comments
 
@@ -181,13 +181,13 @@ bool Engine::CanCarryCargo() const
 
		case VEH_SHIP:
 
		case VEH_AIRCRAFT:
 
			break;
 

	
 
		default: NOT_REACHED();
 
	}
 
	return this->GetDefaultCargoType() != CT_INVALID;
 
	return IsValidCargoID(this->GetDefaultCargoType());
 
}
 

	
 

	
 
/**
 
 * Determines capacity of a given vehicle from scratch.
 
 * For aircraft the main capacity is determined. Mail might be present as well.
 
@@ -1253,13 +1253,13 @@ bool IsEngineRefittable(EngineID engine)
 
	if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
 

	
 
	/* Is there any cargo except the default cargo? */
 
	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;
 
}
 

	
 
/**
 
 * Check for engines that have an appropriate availability.
 
 */
 
void CheckEngines()
src/industry.h
Show inline comments
 
@@ -114,21 +114,21 @@ struct Industry : IndustryPool::PoolItem
 
	{
 
		return IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == this->index;
 
	}
 

	
 
	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;
 
	}
 

	
 
	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;
 
	}
 

	
 
	/**
src/industry_cmd.cpp
Show inline comments
 
@@ -449,13 +449,13 @@ static void AddAcceptedCargo_Industry(Ti
 
			for (uint i = 0; i < 3; i++) cargo_acceptance[i] = GB(res, i * 4, 4);
 
		}
 
	}
 

	
 
	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];
 

	
 
		/* Maybe set 'always accepted' bit (if it's not set already) */
 
		if (HasBit(*always_accepted, a)) continue;
 
@@ -531,13 +531,13 @@ static bool TransportIndustryGoods(TileI
 
	Industry *i = Industry::GetByTile(tile);
 
	const IndustrySpec *indspec = GetIndustrySpec(i->type);
 
	bool moved_cargo = false;
 

	
 
	for (uint j = 0; j < lengthof(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;
 

	
 
			/* fluctuating economy? */
 
			if (EconomyIsInRecession()) cw = (cw + 1) / 2;
 

	
 
			i->this_month_production[j] += cw;
 
@@ -988,13 +988,13 @@ bool IsTileForestIndustry(TileIndex tile
 
	/* Check for organic industry (i.e. not processing or extractive) */
 
	if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false;
 

	
 
	/* 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;
 
}
 

	
 
static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
 
@@ -1864,13 +1864,13 @@ static void DoCreateNewIndustry(Industry
 
				break;
 
			}
 
			CargoID cargo = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
 
			/* 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 */
 
				ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
 
				break;
 
			}
 
@@ -1894,13 +1894,13 @@ static void DoCreateNewIndustry(Industry
 
			if (indspec->grf_prop.grffile->grf_version >= 8 && res >= 0x100) {
 
				ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
 
				break;
 
			}
 
			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 */
 
				ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
 
				break;
 
			}
 
@@ -2417,13 +2417,13 @@ void GenerateIndustries()
 
 * Monthly update of industry statistics.
 
 * @param i Industry to update.
 
 */
 
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;
 
				pct = ClampTo<byte>(i->this_month_transported[j] * 256 / i->this_month_production[j]);
 
			}
 
			i->last_month_pct_transported[j] = pct;
 
@@ -2619,13 +2619,13 @@ static bool CheckIndustryCloseDownProtec
 
 * @param *c_produces: Pointer to boolean for production of cargo
 
 * @return: \c *c_accepts is set when industry accepts the cargo type,
 
 *          \c *c_produces is set when the industry produces the cargo 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++) {
 
		if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) {
 
			*c_accepts = true;
 
			break;
 
@@ -2797,13 +2797,13 @@ static void ChangeIndustryProduction(Ind
 
					div = 1; // Decrease production
 
				}
 
			}
 
		} 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. */
 
				int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
 

	
 
				new_prod = old_prod = i->production_rate[j];
src/industry_gui.cpp
Show inline comments
 
@@ -155,13 +155,13 @@ static inline void GetAllCargoSuffixes(C
 
{
 
	static_assert(lengthof(cargoes) <= lengthof(suffixes));
 

	
 
	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]);
 
			} else {
 
				suffixes[j].text[0] = '\0';
 
				suffixes[j].display = CSD_CARGO;
 
@@ -172,19 +172,19 @@ static inline void GetAllCargoSuffixes(C
 
		for (uint j = 0; j < lengthof(suffixes); j++) {
 
			suffixes[j].text[0] = '\0';
 
			suffixes[j].display = CSD_CARGO;
 
		}
 
		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();
 
		}
 
	}
 
}
 
@@ -343,13 +343,13 @@ class BuildIndustryWindow : public Windo
 
	{
 
		std::string cargostring;
 
		int numcargo = 0;
 
		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;
 
				continue;
 
			}
 
			SetDParam(0, CargoSpec::Get(cargolist[j])->name);
 
@@ -842,13 +842,13 @@ public:
 

	
 
		CargoSuffix cargo_suffix[lengthof(i->accepts_cargo)];
 
		GetAllCargoSuffixes(CARGOSUFFIX_IN, CST_VIEW, i, i->type, ind, i->accepts_cargo, cargo_suffix);
 
		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);
 
				ir.top += FONT_HEIGHT_NORMAL;
 
				first = false;
 
			}
 
@@ -882,13 +882,13 @@ public:
 
		GetAllCargoSuffixes(CARGOSUFFIX_OUT, CST_VIEW, i, i->type, ind, i->produced_cargo, cargo_suffix);
 
		int line_height = this->editable == EA_RATE ? this->cheat_line_height : FONT_HEIGHT_NORMAL;
 
		int text_y_offset = (line_height - FONT_HEIGHT_NORMAL) / 2;
 
		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);
 
				ir.top += FONT_HEIGHT_NORMAL;
 
				if (this->editable == EA_RATE) this->production_offset_y = ir.top;
 
				first = false;
 
@@ -978,13 +978,13 @@ public:
 
						break;
 

	
 
					case EA_RATE:
 
						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);
 
									break;
 
								}
 
							}
 
@@ -1137,13 +1137,13 @@ public:
 
static void UpdateIndustryProduction(Industry *i)
 
{
 
	const IndustrySpec *indspec = GetIndustrySpec(i->type);
 
	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];
 
		}
 
	}
 
}
 

	
 
/** Widget definition of the view industry gui */
 
@@ -1241,13 +1241,13 @@ static bool CDECL CargoFilter(const Indu
 
		case CF_ANY:
 
			accepted_cargo_matches = true;
 
			break;
 

	
 
		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;
 

	
 
		default:
 
			const auto &ac = (*industry)->accepts_cargo;
 
			accepted_cargo_matches = std::find(std::begin(ac), std::end(ac), accepted_cargo) != std::end(ac);
 
@@ -1260,13 +1260,13 @@ static bool CDECL CargoFilter(const Indu
 
		case CF_ANY:
 
			produced_cargo_matches = true;
 
			break;
 

	
 
		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;
 

	
 
		default:
 
			const auto &pc = (*industry)->produced_cargo;
 
			produced_cargo_matches = std::find(std::begin(pc), std::end(pc), produced_cargo) != std::end(pc);
 
@@ -1422,13 +1422,13 @@ protected:
 
	 * @return percents of cargo transported, or -1 if industry doesn't use this cargo slot
 
	 */
 
	static inline int GetCargoTransportedPercentsIfValid(const Industry *i, uint id)
 
	{
 
		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]);
 
	}
 

	
 
	/**
 
	 * Returns value representing industry's transported cargo
 
	 *  percentage for industry sorting
 
@@ -1486,14 +1486,14 @@ protected:
 
		CargoID filter = IndustryDirectoryWindow::produced_cargo_filter;
 
		if (filter == CF_NONE) return IndustryTypeSorter(a, b);
 

	
 
		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];
 
			}
 
		}
 
		int r = prod_a - prod_b;
 
@@ -1531,13 +1531,13 @@ protected:
 
			const char *suffix;
 
			uint transported;
 
		};
 
		std::vector<CargoInfo> 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]) });
 
		}
 

	
 
		switch (static_cast<IndustryDirectoryWindow::SorterType>(this->industries.SortType())) {
 
			case IndustryDirectoryWindow::SorterType::ByName:
 
			case IndustryDirectoryWindow::SorterType::ByType:
 
@@ -1966,29 +1966,29 @@ struct CargoesField {
 
	 * @param producer Cargo is produced (if \c false, cargo is assumed to be accepted).
 
	 * @return Horizontal connection index, or \c -1 if not accepted at all.
 
	 */
 
	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;
 
		for (int i = 0; i < this->u.cargo.num_cargoes; i++) {
 
			if (cargo == this->u.cargo.vertical_cargoes[i]) {
 
				column = i;
 
				break;
 
			}
 
		}
 
		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;
 
	}
 

	
 
	/**
 
@@ -1997,14 +1997,14 @@ struct CargoesField {
 
	 */
 
	bool HasConnection()
 
	{
 
		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;
 
	}
 

	
 
	/**
 
	 * Make a piece of cargo column.
 
@@ -2018,13 +2018,13 @@ struct CargoesField {
 
	void MakeCargo(const CargoID *cargoes, uint length, int count = -1, bool top_end = false, bool bottom_end = false)
 
	{
 
		this->type = CFT_CARGO;
 
		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++;
 
			}
 
		}
 
		this->u.cargo.num_cargoes = (count < 0) ? num : count;
 
		for (; num < MAX_CARGOES; num++) this->u.cargo.vertical_cargoes[num] = INVALID_CARGO;
 
@@ -2125,19 +2125,19 @@ struct CargoesField {
 
				} else {
 
					other_right = this->u.industry.other_produced;
 
					other_left  = this->u.industry.other_accepted;
 
				}
 
				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);
 
						GfxDrawLine(xp, ypos1, xp, ypos1 + CargoesField::cargo_line.height - 1, CARGO_LINE_COLOUR);
 
					}
 
					ypos1 += FONT_HEIGHT_NORMAL + CargoesField::cargo_space.height;
 
@@ -2169,24 +2169,24 @@ struct CargoesField {
 
				} else {
 
					hor_left  = this->u.cargo.supp_cargoes;
 
					hor_right = this->u.cargo.cust_cargoes;
 
				}
 
				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]);
 
						for (; col > 0; col--) {
 
							int lf = cargo_base + col * CargoesField::cargo_line.width + (col - 1) * CargoesField::cargo_space.width;
 
							DrawHorConnection(lf, lf + CargoesField::cargo_space.width - dx, ypos, csp);
 
							dx = 1;
 
						}
 
						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]);
 
						for (; col < this->u.cargo.num_cargoes - 1; col++) {
 
							int lf = cargo_base + (col + 1) * CargoesField::cargo_line.width + col * CargoesField::cargo_space.width;
 
							DrawHorConnection(lf + dx - 1, lf + CargoesField::cargo_space.width - 1, ypos, csp);
 
@@ -2199,13 +2199,13 @@ struct CargoesField {
 
				break;
 
			}
 

	
 
			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);
 
					}
 
					ypos += FONT_HEIGHT_NORMAL + CargoesField::cargo_space.height;
 
				}
 
@@ -2245,36 +2245,36 @@ struct CargoesField {
 
			vpos += FONT_HEIGHT_NORMAL + CargoesField::cargo_space.width;
 
		}
 
		if (row == MAX_CARGOES) return INVALID_CARGO;
 

	
 
		/* 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];
 
			}
 
			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];
 
			}
 
			return INVALID_CARGO;
 
		}
 
		if (row >= col) {
 
			/* Clicked somewhere in-between vertical cargo connection.
 
			 * 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;
 
		}
 
	}
 

	
 
	/**
 
	 * Decide what cargo the user clicked in the cargo label field.
 
	 * @param pt Click position in the cargo label field.
 
@@ -2358,13 +2358,13 @@ struct CargoesRow {
 
				int col = cargo_fld->ConnectCargo(indsp->produced_cargo[i], true);
 
				if (col < 0) others[other_count++] = indsp->produced_cargo[i];
 
			}
 

	
 
			/* 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. */
 
			for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
 
				CargoID cid = cargo_fld->u.cargo.vertical_cargoes[i];
 
				if (cid == CT_PASSENGERS || cid == CT_MAIL) cargo_fld->ConnectCargo(cid, true);
 
@@ -2416,13 +2416,13 @@ struct CargoesRow {
 
				int col = cargo_fld->ConnectCargo(indsp->accepts_cargo[i], false);
 
				if (col < 0) others[other_count++] = indsp->accepts_cargo[i];
 
			}
 

	
 
			/* 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. */
 
			for (uint i = 0; i < cargo_fld->u.cargo.num_cargoes; i++) {
 
				for (uint h = 0; h < NUM_HOUSES; h++) {
 
					HouseSpec *hs = HouseSpec::Get(h);
 
@@ -2599,13 +2599,13 @@ struct IndustryCargoesWindow : public Wi
 
	 * @param length2  Number of cargoes in the second cargo array.
 
	 * @return Arrays have at least one valid cargo in common.
 
	 */
 
	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++;
 
			length1--;
 
		}
 
		return false;
 
@@ -2617,13 +2617,13 @@ struct IndustryCargoesWindow : public Wi
 
	 * @param length  Number of cargoes in the array.
 
	 * @return Houses can supply at least one of the cargoes.
 
	 */
 
	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;
 
	}
 

	
 
	/**
 
@@ -2640,13 +2640,13 @@ struct IndustryCargoesWindow : public Wi
 
			case LT_ARCTIC:    climate_mask = HZ_SUBARTC_ABOVE | HZ_SUBARTC_BELOW; break;
 
			case LT_TROPIC:    climate_mask = HZ_SUBTROPIC; break;
 
			case LT_TOYLAND:   climate_mask = HZ_TOYLND; break;
 
			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);
 
				if (!hs->enabled || !(hs->building_availability & climate_mask)) continue;
 

	
 
				for (uint j = 0; j < lengthof(hs->accepts_cargo); j++) {
 
@@ -3009,19 +3009,19 @@ struct IndustryCargoesWindow : public Wi
 
						break;
 

	
 
					case CFT_CARGO: {
 
						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;
 
					}
 

	
 
					default:
 
						break;
 
				}
 
@@ -3110,13 +3110,13 @@ struct IndustryCargoesWindow : public Wi
 
				}
 
				return true;
 

	
 
			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;
 
			GuiShowTooltips(this, STR_INDUSTRY_CARGOES_CARGO_TOOLTIP, 1, params, close_cond);
 
			return true;
 
		}
src/newgrf.cpp
Show inline comments
 
@@ -941,13 +941,13 @@ static bool ReadSpriteLayout(ByteReader 
 
 */
 
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;
 
}
 

	
 
/**
 
 * Converts TTD(P) Base Price pointers into the enum used by OTTD
 
@@ -1303,14 +1303,13 @@ static ChangeInfoResult RailVehicleChang
 
				_gted[e->index].UpdateRefittability(prop == 0x2C && count != 0);
 
				if (prop == 0x2C) _gted[e->index].defaultcargo_grf = _cur.grffile;
 
				CargoTypes &ctt = prop == 0x2C ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
 
				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;
 
			}
 

	
 
			case PROP_TRAIN_CURVE_SPEED_MOD: // 0x2E Curve speed modifier
 
				rvi->curve_speed_mod = buf->ReadWord();
 
@@ -1513,14 +1512,13 @@ static ChangeInfoResult RoadVehicleChang
 
				_gted[e->index].UpdateRefittability(prop == 0x24 && count != 0);
 
				if (prop == 0x24) _gted[e->index].defaultcargo_grf = _cur.grffile;
 
				CargoTypes &ctt = prop == 0x24 ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
 
				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;
 
			}
 

	
 
			case 0x26: // Engine variant
 
				ei->variant_id = buf->ReadWord();
 
@@ -1697,14 +1695,13 @@ static ChangeInfoResult ShipVehicleChang
 
				_gted[e->index].UpdateRefittability(prop == 0x1E && count != 0);
 
				if (prop == 0x1E) _gted[e->index].defaultcargo_grf = _cur.grffile;
 
				CargoTypes &ctt = prop == 0x1E ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
 
				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;
 
			}
 

	
 
			case 0x20: // Engine variant
 
				ei->variant_id = buf->ReadWord();
 
@@ -1859,14 +1856,13 @@ static ChangeInfoResult AircraftVehicleC
 
				_gted[e->index].UpdateRefittability(prop == 0x1D && count != 0);
 
				if (prop == 0x1D) _gted[e->index].defaultcargo_grf = _cur.grffile;
 
				CargoTypes &ctt = prop == 0x1D ? _gted[e->index].ctt_include_mask : _gted[e->index].ctt_exclude_mask;
 
				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;
 
			}
 

	
 
			case PROP_AIRCRAFT_RANGE: // 0x1F Max aircraft range
 
				avi->max_range = buf->ReadWord();
 
@@ -2544,13 +2540,13 @@ static ChangeInfoResult TownHouseChangeI
 

	
 
				for (uint j = 0; j < 3; j++) {
 
					/* Get the cargo number from the 'list' */
 
					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 {
 
						housespec->accepts_cargo[j] = cargo;
 
					}
 
				}
 
@@ -2562,13 +2558,13 @@ static ChangeInfoResult TownHouseChangeI
 
				break;
 

	
 
			case 0x20: { // Cargo acceptance watch list
 
				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;
 
			}
 

	
 
			case 0x21: // long introduction year
 
				housespec->min_year = buf->ReadWord();
 
@@ -5454,13 +5450,13 @@ static void NewSpriteGroup(ByteReader *b
 
							error->data = "too many inputs (max 16)";
 
							return;
 
						}
 
						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. */
 
								group->version = 0xFF;
 
							} else if (std::find(group->cargo_input, group->cargo_input + i, cargo) != group->cargo_input + i) {
 
								GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
@@ -5476,13 +5472,13 @@ static void NewSpriteGroup(ByteReader *b
 
							error->data = "too many outputs (max 16)";
 
							return;
 
						}
 
						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) {
 
								GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
								error->data = "duplicate output cargo";
 
								return;
 
@@ -5550,13 +5546,13 @@ static CargoID TranslateCargo(uint8 feat
 
	if (cl == 0) {
 
		GrfMsg(5, "TranslateCargo: Cargo type {} not available in this climate, skipping.", ctype);
 
		return CT_INVALID;
 
	}
 

	
 
	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;
 
	}
 

	
 
	GrfMsg(6, "TranslateCargo: Cargo '{:c}{:c}{:c}{:c}' mapped to cargo type {}.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8), ctype);
 
	return ctype;
 
@@ -5620,13 +5616,13 @@ static void VehicleMapSpriteGroup(ByteRe
 
		uint16 groupid = buf->ReadWord();
 
		if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) continue;
 

	
 
		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];
 

	
 
			GrfMsg(7, "VehicleMapSpriteGroup: [{}] Engine {}...", i, engine);
 

	
 
@@ -5699,13 +5695,13 @@ static void StationMapSpriteGroup(ByteRe
 
	for (uint c = 0; c < cidcount; c++) {
 
		uint8 ctype = buf->ReadByte();
 
		uint16 groupid = buf->ReadWord();
 
		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();
 

	
 
			if (statspec == nullptr) {
 
				GrfMsg(1, "StationMapSpriteGroup: Station {} undefined, skipping", station);
 
@@ -5880,13 +5876,13 @@ static void ObjectMapSpriteGroup(ByteRea
 
	for (uint c = 0; c < cidcount; c++) {
 
		uint8 ctype = buf->ReadByte();
 
		uint16 groupid = buf->ReadWord();
 
		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();
 

	
 
			if (spec == nullptr) {
 
				GrfMsg(1, "ObjectMapSpriteGroup: Object {} undefined, skipping", object);
 
@@ -6066,13 +6062,13 @@ static void RoadStopMapSpriteGroup(ByteR
 
	for (uint c = 0; c < cidcount; c++) {
 
		uint8 ctype = buf->ReadByte();
 
		uint16 groupid = buf->ReadWord();
 
		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();
 

	
 
			if (roadstopspec == nullptr) {
 
				GrfMsg(1, "RoadStopMapSpriteGroup: Road stop {} undefined, skipping", roadstop);
 
@@ -6841,15 +6837,15 @@ static void SkipIf(ByteReader *buf)
 
	/* condtypes that do not use 'param' are always valid.
 
	 * condtypes that use 'param' are either not valid for param 0x88, or they are only valid for param 0x88.
 
	 */
 
	if (condtype >= 0x0B) {
 
		/* Tests that ignore 'param' */
 
		switch (condtype) {
 
			case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID;
 
				break;
 
			case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID;
 
			case 0x0B: result = !IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val)));
 
				break;
 
			case 0x0C: result = IsValidCargoID(GetCargoIDByLabel(BSWAP32(cond_val)));
 
				break;
 
			case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE;
 
				break;
 
			case 0x0E: result = GetRailTypeByLabel(BSWAP32(cond_val)) != INVALID_RAILTYPE;
 
				break;
 
			case 0x0F: {
 
@@ -8942,13 +8938,13 @@ GRFFile::~GRFFile()
 
 */
 
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<CargoType>(ct));
 
		if (cid != CT_INVALID) SetBit(original_known_cargoes, cid);
 
		if (IsValidCargoID(cid)) SetBit(original_known_cargoes, cid);
 
	}
 

	
 
	for (Engine *e : Engine::Iterate()) {
 
		EngineID engine = e->index;
 
		EngineInfo *ei = &e->info;
 
		bool only_defaultcargo; ///< Set if the vehicle shall carry only the default cargo
 
@@ -9032,13 +9028,13 @@ static void CalculateRefitMasks()
 
				}
 
			}
 
			_gted[engine].UpdateRefittability(_gted[engine].cargo_allowed != 0);
 

	
 
			/* 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));
 
			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 */
 
		{
 
			CargoTypes mask = 0;
 
			CargoTypes not_mask = 0;
 
@@ -9061,23 +9057,23 @@ static void CalculateRefitMasks()
 
			/* Apply explicit refit includes/excludes. */
 
			ei->refit_mask |= _gted[engine].ctt_include_mask;
 
			ei->refit_mask &= ~_gted[engine].ctt_exclude_mask;
 
		}
 

	
 
		/* 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;
 
			{
 
				const GRFFile *file = _gted[engine].defaultcargo_grf;
 
				if (file == nullptr) file = e->GetGRF();
 
				if (file != nullptr && file->grf_version >= 8 && file->cargo_list.size() != 0) {
 
@@ -9094,18 +9090,18 @@ static void CalculateRefitMasks()
 
						best_local_slot = local_slot;
 
						ei->cargo_type = cargo_type;
 
					}
 
				}
 
			}
 

	
 
			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) {
 
			ei->refit_mask = 0;
 
		}
 
	}
src/newgrf_debug_gui.cpp
Show inline comments
 
@@ -500,13 +500,13 @@ struct NewGRFInspectWindow : Window {
 
				switch (nip->type) {
 
					case NIT_INT:
 
						string = STR_JUST_INT;
 
						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:
 
						NOT_REACHED();
 
				}
 

	
src/newgrf_engine.cpp
Show inline comments
 
@@ -961,13 +961,13 @@ static uint32 VehicleGetVariable(Vehicle
 
		switch (variable) {
 
			case 0x43: return GetCompanyInfo(_current_company, LiveryHelper(this->self_type, nullptr)); // Owner information
 
			case 0x46: return 0;               // Motion counter
 
			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 {
 
					return 0x000000FF;
 
				}
 
			}
src/newgrf_house.cpp
Show inline comments
 
@@ -337,13 +337,13 @@ static uint32 GetDistanceFromNearbyHouse
 
			return IsTileType(testtile, MP_HOUSE) ? GetAnimationFrame(testtile) : 0;
 
		}
 

	
 
		/* 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);
 
			int8 y_offs = GB(GetRegister(0x100), 8, 8);
 
			TileIndex testtile = Map::WrapToMap(this->tile + TileDiffXY(x_offs, y_offs));
 

	
src/newgrf_industries.cpp
Show inline comments
 
@@ -313,13 +313,13 @@ static uint32 GetCountAndDistanceOfClose
 
		case 0x6B:
 
		case 0x6C:
 
		case 0x6D:
 
		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) {
 
				case 0x69: return this->industry->produced_cargo_waiting[index];
 
				case 0x6A: return this->industry->this_month_production[index];
 
				case 0x6B: return this->industry->this_month_transported[index];
 
@@ -332,13 +332,13 @@ static uint32 GetCountAndDistanceOfClose
 
		}
 

	
 

	
 
		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];
 
			if (variable == 0x6F) return this->industry->incoming_cargo_waiting[index];
 
			NOT_REACHED();
 
		}
src/newgrf_roadstop.cpp
Show inline comments
 
@@ -343,13 +343,13 @@ void TriggerRoadStopAnimation(BaseStatio
 

	
 
	uint16 random_bits = Random();
 
	auto process_tile = [&](TileIndex cur_tile) {
 
		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];
 
			}
 
			RoadStopAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, cur_tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
 
		}
 
@@ -376,13 +376,13 @@ void TriggerRoadStopRandomisation(Statio
 
{
 
	if (st == nullptr) st = Station::GetByTile(tile);
 

	
 
	/* 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);
 

	
 
	uint32 whole_reseed = 0;
 

	
 
	CargoTypes empty_mask = 0;
 
@@ -403,13 +403,13 @@ void TriggerRoadStopRandomisation(Statio
 
		/* Cargo taken "will only be triggered if all of those
 
		 * cargo types have no more cargo waiting." */
 
		if (trigger == RSRT_CARGO_TAKEN) {
 
			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;
 

	
 
			const SpriteGroup *group = object.Resolve();
 
			if (group == nullptr) return;
 

	
src/newgrf_station.cpp
Show inline comments
 
@@ -409,13 +409,13 @@ uint32 Station::GetNewGRFVariable(const 
 
	}
 

	
 
	/* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
 
	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;
 
				default:   return 0;
 
			}
 
		}
 
@@ -928,13 +928,13 @@ void TriggerStationAnimation(BaseStation
 
	/* Check all tiles over the station to check if the specindex is still in use */
 
	for (TileIndex tile : area) {
 
		if (st->TileBelongsToRailStation(tile)) {
 
			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];
 
				}
 
				StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | GB(Random(), 0, 16), (uint8)trigger | (cargo << 8));
 
			}
 
@@ -959,13 +959,13 @@ void TriggerStationRandomisation(Station
 
	/* Get Station if it wasn't supplied */
 
	if (st == nullptr) st = Station::GetByTile(trigger_tile);
 

	
 
	/* 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]);
 

	
 
	CargoTypes empty_mask = 0;
 
	if (trigger == SRT_CARGO_TAKEN) {
 
@@ -990,13 +990,13 @@ void TriggerStationRandomisation(Station
 
			/* Cargo taken "will only be triggered if all of those
 
			 * cargo types have no more cargo waiting." */
 
			if (trigger == SRT_CARGO_TAKEN) {
 
				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;
 

	
 
				const SpriteGroup *group = object.Resolve();
 
				if (group == nullptr) continue;
 

	
src/saveload/afterload.cpp
Show inline comments
 
@@ -3020,13 +3020,13 @@ bool AfterLoadGame()
 
				i->accepts_cargo[ci] = CT_INVALID;
 
				i->incoming_cargo_waiting[ci] = 0;
 
			}
 
			/* 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;
 
				}
 
			}
 
		}
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -1437,13 +1437,13 @@ static const OldChunks subsidy_chunk[] =
 
};
 

	
 
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;
 
}
 

	
 
static const OldChunks game_difficulty_chunk[] = {
 
	OCL_SVAR( OC_FILE_U16 |  OC_VAR_U8, DifficultySettings, max_no_competitors ),
 
	OCL_NULL( 2), // competitor_start_time
src/script/api/script_cargolist.cpp
Show inline comments
 
@@ -28,26 +28,26 @@ ScriptCargoList_IndustryAccepting::Scrip
 
{
 
	if (!ScriptIndustry::IsValidIndustry(industry_id)) return;
 

	
 
	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);
 
		}
 
	}
 
}
 

	
 
ScriptCargoList_IndustryProducing::ScriptCargoList_IndustryProducing(IndustryID industry_id)
 
{
 
	if (!ScriptIndustry::IsValidIndustry(industry_id)) return;
 

	
 
	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);
 
		}
 
	}
 
}
 

	
 
ScriptCargoList_StationAccepting::ScriptCargoList_StationAccepting(StationID station_id)
src/script/api/script_industry.cpp
Show inline comments
 
@@ -231,13 +231,13 @@
 

	
 
/* static */ ScriptDate::Date ScriptIndustry::GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type)
 
{
 
	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);
 
		if (index < 0) return ScriptDate::DATE_INVALID;
 
		return (ScriptDate::Date)i->last_cargo_accepted_at[index];
 
	}
src/script/api/script_industrytype.cpp
Show inline comments
 
@@ -69,13 +69,13 @@
 
	if (!IsValidIndustryType(industry_type)) return nullptr;
 

	
 
	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
 

	
 
	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;
 
}
 

	
 
/* static */ ScriptList *ScriptIndustryType::GetAcceptedCargo(IndustryType industry_type)
 
@@ -83,13 +83,13 @@
 
	if (!IsValidIndustryType(industry_type)) return nullptr;
 

	
 
	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
 

	
 
	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;
 
}
 

	
 
/* static */ bool ScriptIndustryType::CanBuildIndustry(IndustryType industry_type)
src/script/api/script_tilelist.cpp
Show inline comments
 
@@ -83,13 +83,13 @@ ScriptTileList_IndustryAccepting::Script
 
	if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return;
 

	
 
	/* Check if this industry accepts anything */
 
	{
 
		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;
 
	}
 

	
 
	if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
 

	
 
@@ -101,13 +101,13 @@ ScriptTileList_IndustryAccepting::Script
 
		/* Only add the tile if it accepts the cargo (sometimes just 1 tile of an
 
		 *  industry triggers the acceptance). */
 
		CargoArray acceptance = ::GetAcceptanceAroundTiles(cur_tile, 1, 1, radius);
 
		{
 
			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;
 
		}
 

	
 
		this->AddTile(cur_tile);
 
	}
 
@@ -122,13 +122,13 @@ ScriptTileList_IndustryProducing::Script
 
	/* Check if this industry is only served by its neutral station */
 
	if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) return;
 

	
 
	/* 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;
 

	
 
	if (!_settings_game.station.modified_catchment) radius = CA_UNMODIFIED;
 

	
 
	BitmapTileArea bta(TileArea(i->location).Expand(radius));
src/script/api/script_vehicle.cpp
Show inline comments
 
@@ -72,13 +72,13 @@
 
}
 

	
 
/* static */ VehicleID ScriptVehicle::_BuildVehicleInternal(TileIndex depot, EngineID engine_id, CargoID cargo)
 
{
 
	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;
 

	
 
	EnforcePreconditionCustomError(VEHICLE_INVALID, !ScriptGameSettings::IsDisabledVehicleType((ScriptVehicle::VehicleType)type), ScriptVehicle::ERR_VEHICLE_BUILD_DISABLED);
 

	
 
	if (!ScriptObject::Command<CMD_BUILD_VEHICLE>::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, engine_id, true, cargo, INVALID_CLIENT_ID)) return VEHICLE_INVALID;
src/station.cpp
Show inline comments
 
@@ -404,13 +404,13 @@ void Station::AddIndustryToDeliver(Indus
 
		return;
 
	}
 

	
 
	/* 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;
 

	
 
	this->industries_near.insert(IndustryListEntry{distance, ind});
 
}
 

	
src/station_cmd.cpp
Show inline comments
 
@@ -170,13 +170,13 @@ static bool CMSAMine(TileIndex tile)
 
	/* No extractive industry */
 
	if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_EXTRACTIVE) == 0) return false;
 

	
 
	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;
 
		}
 
	}
 

	
 
	return false;
 
@@ -531,13 +531,13 @@ CargoArray GetProductionAroundTiles(Tile
 
		const Industry *i = Industry::Get(industry);
 
		/* Skip industry with neutral station */
 
		if (i->neutral_station != nullptr && !_settings_game.station.serve_neutral_industries) continue;
 

	
 
		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]++;
 
		}
 
	}
 

	
 
	return produced;
 
}
 

	
src/strings.cpp
Show inline comments
 
@@ -1200,15 +1200,15 @@ static char *FormatString(char *buff, co
 
				break;
 
			}
 

	
 
			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;
 
			}
 

	
 
			case SCC_CARGO_LIST: { // {CARGO_LIST}
src/subsidy.cpp
Show inline comments
 
@@ -385,30 +385,30 @@ bool FindSubsidyIndustryCargoRoute()
 
	CargoID cid;
 

	
 
	/* Randomize cargo type */
 
	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
 
	cid = src_ind->produced_cargo[cargo_index];
 
	trans = src_ind->last_month_pct_transported[cargo_index];
 
	total = src_ind->last_month_production[cargo_index];
 

	
 
	/* Quit if no production in this industry
 
	 * 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;
 
	}
 

	
 
	SourceID src = src_ind->index;
 

	
src/town_cmd.cpp
Show inline comments
 
@@ -558,13 +558,13 @@ static void TileLoop_Town(TileIndex tile
 
		for (uint i = 0; i < 256; i++) {
 
			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, 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;
 

	
 
			uint moved = MoveGoodsToStation(cargo, amt, SourceType::Town, t->index, stations.GetStations());
 

	
 
@@ -703,13 +703,13 @@ static void AddProducedCargo_Town(TileIn
 
			uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, 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;
 
			produced[cargo]++;
 
		}
 
	} else {
 
		if (hs->population > 0) {
 
			produced[CT_PASSENGERS]++;
 
		}
 
@@ -718,13 +718,13 @@ static void AddProducedCargo_Town(TileIn
 
		}
 
	}
 
}
 

	
 
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);
 
}
 

	
 
static void AddAcceptedCargo_Town(TileIndex tile, CargoArray &acceptance, CargoTypes *always_accepted)
 
{
src/train_gui.cpp
Show inline comments
 
@@ -206,13 +206,13 @@ static void TrainDetailsCargoTab(const C
 
		SetDParam(0, item->cargo);
 
		SetDParam(1, item->amount);
 
		SetDParam(2, item->source);
 
		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);
 
}
 

	
 
/**
 
@@ -245,13 +245,13 @@ static void TrainDetailsInfoTab(const Ve
 
 * @param right The right most coordinate to draw
 
 * @param y     The y coordinate
 
 */
 
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);
 
		SetDParam(5, _settings_game.vehicle.freight_trains);
 
		str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY;
 
	} else {
 
@@ -273,13 +273,13 @@ static void GetCargoSummaryOfArticulated
 
	do {
 
		if (!v->GetEngine()->CanCarryCargo()) continue;
 

	
 
		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()) {
 
			summary->emplace_back();
 
			item = summary->end() - 1;
 
			item->cargo = new_item.cargo;
src/vehicle.cpp
Show inline comments
 
@@ -229,13 +229,13 @@ bool Vehicle::NeedsServicing() const
 
		if (union_mask != 0) {
 
			CargoID cargo_type;
 
			/* We cannot refit to mixed cargoes in an automated way */
 
			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;
 
			}
 
		}
 

	
 
		/* Check money.
 
@@ -1881,14 +1881,14 @@ LiveryScheme GetEngineLiveryScheme(Engin
 
				 * Articulated parts use the colour scheme of the first part. (Not supported for articulated wagons) */
 
				engine_type = parent_engine_type;
 
				e = Engine::Get(engine_type);
 
				/* 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) {
 
						return LS_PASSENGER_WAGON_STEAM;
 
					} else {
 
						bool is_mu = HasBit(EngInfo(parent_engine_type)->misc_flags, EF_RAIL_IS_MU);
 
@@ -1921,27 +1921,27 @@ LiveryScheme GetEngineLiveryScheme(Engin
 
			/* Always use the livery of the front */
 
			if (v != nullptr && parent_engine_type != INVALID_ENGINE) {
 
				engine_type = parent_engine_type;
 
				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)) {
 
				/* Tram */
 
				return IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM;
 
			} else {
 
				/* Bus or truck */
 
				return IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_BUS : LS_TRUCK;
 
			}
 

	
 
		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:
 
			switch (e->u.air.subtype) {
 
				case AIR_HELI: return LS_HELICOPTER;
 
				case AIR_CTOL: return LS_SMALL_PLANE;
src/vehicle_cmd.cpp
Show inline comments
 
@@ -92,22 +92,22 @@ std::tuple<CommandCost, VehicleID, uint,
 
	VehicleType type = GetDepotVehicleType(tile);
 

	
 
	/* Validate the engine type. */
 
	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;
 
	switch (type) {
 
		case VEH_TRAIN:    num_vehicles = (e->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + CountArticulatedParts(eid, false); break;
 
		case VEH_ROAD:     num_vehicles = 1 + CountArticulatedParts(eid, false); break;
 
@@ -948,13 +948,13 @@ std::tuple<CommandCost, VehicleID> CmdCl
 
					break;
 
				}
 
			} else {
 
				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));
 
				}
 
			}
 

	
 
			if (v->IsGroundVehicle() && v->HasArticulatedPart()) {
0 comments (0 inline, 0 general)