Changeset - r28424:913b64f47f66
[Not reviewed]
master
0 5 0
Peter Nelson - 11 months ago 2024-01-08 21:40:09
peter1138@openttd.org
Codechange: Rename and move SpriteGroup-specific cargo types into a namespace.

These 'cargo types' have special defined uses and must not be used elsewhere. This makes it clearer that they are special.
5 files changed with 30 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/newgrf.cpp
Show inline comments
 
@@ -5505,14 +5505,14 @@ static void NewSpriteGroup(ByteReader *b
 
	_cur.spritegroups[setid] = act_group;
 
}
 

	
 
static CargoID TranslateCargo(uint8_t feature, uint8_t ctype)
 
{
 
	/* Special cargo types for purchase list and stations */
 
	if ((feature == GSF_STATIONS || feature == GSF_ROADSTOPS) && ctype == 0xFE) return CT_DEFAULT_NA;
 
	if (ctype == 0xFF) return CT_PURCHASE;
 
	if ((feature == GSF_STATIONS || feature == GSF_ROADSTOPS) && ctype == 0xFE) return SpriteGroupCargo::SG_DEFAULT_NA;
 
	if (ctype == 0xFF) return SpriteGroupCargo::SG_PURCHASE;
 

	
 
	if (_cur.grffile->cargo_list.empty()) {
 
		/* No cargo table, so use bitnum values */
 
		if (ctype >= 32) {
 
			GrfMsg(1, "TranslateCargo: Cargo bitnum {} out of range (max 31), skipping.", ctype);
 
			return INVALID_CARGO;
 
@@ -5634,15 +5634,15 @@ static void VehicleMapSpriteGroup(ByteRe
 
	GrfMsg(8, "-- Default group id 0x{:04X}", groupid);
 

	
 
	for (uint i = 0; i < idcount; i++) {
 
		EngineID engine = engines[i];
 

	
 
		if (wagover) {
 
			SetWagonOverrideSprites(engine, CT_DEFAULT, _cur.spritegroups[groupid], last_engines, last_engines_count);
 
			SetWagonOverrideSprites(engine, SpriteGroupCargo::SG_DEFAULT, _cur.spritegroups[groupid], last_engines, last_engines_count);
 
		} else {
 
			SetCustomEngineSprites(engine, CT_DEFAULT, _cur.spritegroups[groupid]);
 
			SetCustomEngineSprites(engine, SpriteGroupCargo::SG_DEFAULT, _cur.spritegroups[groupid]);
 
			SetEngineGRF(engine, _cur.grffile);
 
		}
 
	}
 
}
 

	
 

	
 
@@ -5719,13 +5719,13 @@ static void StationMapSpriteGroup(ByteRe
 

	
 
		if (statspec->grf_prop.grffile != nullptr) {
 
			GrfMsg(1, "StationMapSpriteGroup: Station {} mapped multiple times, skipping", station);
 
			continue;
 
		}
 

	
 
		statspec->grf_prop.spritegroup[CT_DEFAULT] = _cur.spritegroups[groupid];
 
		statspec->grf_prop.spritegroup[SpriteGroupCargo::SG_DEFAULT] = _cur.spritegroups[groupid];
 
		statspec->grf_prop.grffile = _cur.grffile;
 
		statspec->grf_prop.local_id = station;
 
		StationClass::Assign(statspec);
 
	}
 
}
 

	
 
@@ -6089,13 +6089,13 @@ static void RoadStopMapSpriteGroup(ByteR
 

	
 
		if (roadstopspec->grf_prop.grffile != nullptr) {
 
			GrfMsg(1, "RoadStopMapSpriteGroup: Road stop {} mapped multiple times, skipping", roadstop);
 
			continue;
 
		}
 

	
 
		roadstopspec->grf_prop.spritegroup[CT_DEFAULT] = _cur.spritegroups[groupid];
 
		roadstopspec->grf_prop.spritegroup[SpriteGroupCargo::SG_DEFAULT] = _cur.spritegroups[groupid];
 
		roadstopspec->grf_prop.grffile = _cur.grffile;
 
		roadstopspec->grf_prop.local_id = roadstop;
 
		RoadStopClass::Assign(roadstopspec);
 
	}
 
}
 

	
src/newgrf_cargo.h
Show inline comments
 
@@ -11,15 +11,22 @@
 
#define NEWGRF_CARGO_H
 

	
 
#include "newgrf_callbacks.h"
 
#include "cargo_type.h"
 
#include "gfx_type.h"
 

	
 
static const CargoID CT_DEFAULT      = NUM_CARGO + 0;
 
static const CargoID CT_PURCHASE     = NUM_CARGO + 1;
 
static const CargoID CT_DEFAULT_NA   = NUM_CARGO + 2;
 
/**
 
 * Sprite Group Cargo types.
 
 * These special cargo types are used when resolving sprite groups when non-cargo-specific sprites or callbacks are needed,
 
 * e.g. in purchase lists, or if no specific cargo type sprite group is supplied.
 
 */
 
namespace SpriteGroupCargo {
 
	static constexpr CargoID SG_DEFAULT    = NUM_CARGO;     ///< Default type used when no more-specific cargo matches.
 
	static constexpr CargoID SG_PURCHASE   = NUM_CARGO + 1; ///< Used in purchase lists before an item exists.
 
	static constexpr CargoID SG_DEFAULT_NA = NUM_CARGO + 2; ///< Used only by stations and roads when no more-specific cargo matches.
 
};
 

	
 
/* Forward declarations of structs used */
 
struct CargoSpec;
 
struct GRFFile;
 

	
 
SpriteID GetCustomCargoSprite(const CargoSpec *cs);
src/newgrf_engine.cpp
Show inline comments
 
@@ -28,26 +28,26 @@
 
#include "safeguards.h"
 

	
 
void SetWagonOverrideSprites(EngineID engine, CargoID cargo, const SpriteGroup *group, EngineID *train_id, uint trains)
 
{
 
	Engine *e = Engine::Get(engine);
 

	
 
	assert(cargo < NUM_CARGO + 2); // Include CT_DEFAULT and CT_PURCHASE pseudo cargoes.
 
	assert(cargo < NUM_CARGO + 2); // Include SpriteGroupCargo::SG_DEFAULT and SpriteGroupCargo::SG_PURCHASE pseudo cargoes.
 

	
 
	WagonOverride *wo = &e->overrides.emplace_back();
 
	wo->group = group;
 
	wo->cargo = cargo;
 
	wo->engines.assign(train_id, train_id + trains);
 
}
 

	
 
const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, EngineID overriding_engine)
 
{
 
	const Engine *e = Engine::Get(engine);
 

	
 
	for (const WagonOverride &wo : e->overrides) {
 
		if (wo.cargo != cargo && wo.cargo != CT_DEFAULT) continue;
 
		if (wo.cargo != cargo && wo.cargo != SpriteGroupCargo::SG_DEFAULT) continue;
 
		if (std::find(wo.engines.begin(), wo.engines.end(), overriding_engine) != wo.engines.end()) return wo.group;
 
	}
 
	return nullptr;
 
}
 

	
 
void SetCustomEngineSprites(EngineID engine, byte cargo, const SpriteGroup *group)
 
@@ -1041,13 +1041,13 @@ VehicleResolverObject::VehicleResolverOb
 
	self_scope(*this, engine_type, v, rotor_in_gui),
 
	parent_scope(*this, engine_type, ((v != nullptr) ? v->First() : v), rotor_in_gui),
 
	relative_scope(*this, engine_type, v, rotor_in_gui),
 
	cached_relative_count(0)
 
{
 
	if (wagon_override == WO_SELF) {
 
		this->root_spritegroup = GetWagonOverrideSpriteSet(engine_type, CT_DEFAULT, engine_type);
 
		this->root_spritegroup = GetWagonOverrideSpriteSet(engine_type, SpriteGroupCargo::SG_DEFAULT, engine_type);
 
	} else {
 
		if (wagon_override != WO_NONE && v != nullptr && v->IsGroundVehicle()) {
 
			assert(v->engine_type == engine_type); // overrides make little sense with fake scopes
 

	
 
			/* For trains we always use cached value, except for callbacks because the override spriteset
 
			 * to use may be different than the one cached. It happens for callback 0x15 (refit engine),
 
@@ -1058,15 +1058,15 @@ VehicleResolverObject::VehicleResolverOb
 
				this->root_spritegroup = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->GetGroundVehicleCache()->first_engine);
 
			}
 
		}
 

	
 
		if (this->root_spritegroup == nullptr) {
 
			const Engine *e = Engine::Get(engine_type);
 
			CargoID cargo = v != nullptr ? v->cargo_type : CT_PURCHASE;
 
			CargoID cargo = v != nullptr ? v->cargo_type : SpriteGroupCargo::SG_PURCHASE;
 
			assert(cargo < lengthof(e->grf_prop.spritegroup));
 
			this->root_spritegroup = e->grf_prop.spritegroup[cargo] != nullptr ? e->grf_prop.spritegroup[cargo] : e->grf_prop.spritegroup[CT_DEFAULT];
 
			this->root_spritegroup = e->grf_prop.spritegroup[cargo] != nullptr ? e->grf_prop.spritegroup[cargo] : e->grf_prop.spritegroup[SpriteGroupCargo::SG_DEFAULT];
 
		}
 
	}
 
}
 

	
 

	
 

	
src/newgrf_roadstop.cpp
Show inline comments
 
@@ -193,17 +193,17 @@ RoadStopResolverObject::RoadStopResolver
 
		CallbackID callback, uint32_t param1, uint32_t param2)
 
	: ResolverObject(roadstopspec->grf_prop.grffile, callback, param1, param2), roadstop_scope(*this, st, roadstopspec, tile, roadtype, type, view)
 
	{
 

	
 
	this->town_scope = nullptr;
 

	
 
	CargoID ctype = CT_DEFAULT_NA;
 
	CargoID ctype = SpriteGroupCargo::SG_DEFAULT_NA;
 

	
 
	if (st == nullptr) {
 
		/* No station, so we are in a purchase list */
 
		ctype = CT_PURCHASE;
 
		ctype = SpriteGroupCargo::SG_PURCHASE;
 
	} else if (Station::IsExpected(st)) {
 
		const Station *station = Station::From(st);
 
		/* Pick the first cargo that we have waiting */
 
		for (const CargoSpec *cs : CargoSpec::Iterate()) {
 
			if (roadstopspec->grf_prop.spritegroup[cs->Index()] != nullptr &&
 
					station->goods[cs->Index()].cargo.TotalCount() > 0) {
 
@@ -211,13 +211,13 @@ RoadStopResolverObject::RoadStopResolver
 
				break;
 
			}
 
		}
 
	}
 

	
 
	if (roadstopspec->grf_prop.spritegroup[ctype] == nullptr) {
 
		ctype = CT_DEFAULT;
 
		ctype = SpriteGroupCargo::SG_DEFAULT;
 
	}
 

	
 
	/* Remember the cargo type we've picked */
 
	this->roadstop_scope.cargo_type = ctype;
 
	this->root_spritegroup = roadstopspec->grf_prop.spritegroup[ctype];
 
}
src/newgrf_station.cpp
Show inline comments
 
@@ -493,18 +493,18 @@ uint32_t Waypoint::GetNewGRFVariable(con
 

	
 
	uint cargo = 0;
 
	const Station *st = Station::From(this->station_scope.st);
 

	
 
	switch (this->station_scope.cargo_type) {
 
		case INVALID_CARGO:
 
		case CT_DEFAULT_NA:
 
		case CT_PURCHASE:
 
		case SpriteGroupCargo::SG_DEFAULT_NA:
 
		case SpriteGroupCargo::SG_PURCHASE:
 
			cargo = 0;
 
			break;
 

	
 
		case CT_DEFAULT:
 
		case SpriteGroupCargo::SG_DEFAULT:
 
			for (const GoodsEntry &ge : st->goods) {
 
				cargo += ge.cargo.TotalCount();
 
			}
 
			break;
 

	
 
		default:
 
@@ -554,17 +554,17 @@ StationResolverObject::StationResolverOb
 
	: ResolverObject(statspec->grf_prop.grffile, callback, callback_param1, callback_param2),
 
	station_scope(*this, statspec, base_station, tile), town_scope(nullptr)
 
{
 
	/* Invalidate all cached vars */
 
	_svc.valid = 0;
 

	
 
	CargoID ctype = CT_DEFAULT_NA;
 
	CargoID ctype = SpriteGroupCargo::SG_DEFAULT_NA;
 

	
 
	if (this->station_scope.st == nullptr) {
 
		/* No station, so we are in a purchase list */
 
		ctype = CT_PURCHASE;
 
		ctype = SpriteGroupCargo::SG_PURCHASE;
 
	} else if (Station::IsExpected(this->station_scope.st)) {
 
		const Station *st = Station::From(this->station_scope.st);
 
		/* Pick the first cargo that we have waiting */
 
		for (const CargoSpec *cs : CargoSpec::Iterate()) {
 
			if (this->station_scope.statspec->grf_prop.spritegroup[cs->Index()] != nullptr &&
 
					st->goods[cs->Index()].cargo.TotalCount() > 0) {
 
@@ -572,13 +572,13 @@ StationResolverObject::StationResolverOb
 
				break;
 
			}
 
		}
 
	}
 

	
 
	if (this->station_scope.statspec->grf_prop.spritegroup[ctype] == nullptr) {
 
		ctype = CT_DEFAULT;
 
		ctype = SpriteGroupCargo::SG_DEFAULT;
 
	}
 

	
 
	/* Remember the cargo type we've picked */
 
	this->station_scope.cargo_type = ctype;
 
	this->root_spritegroup = this->station_scope.statspec->grf_prop.spritegroup[this->station_scope.cargo_type];
 
}
0 comments (0 inline, 0 general)