1
0
Fork 0

Codechange: Turn custom vehicle spritenums into enum, and use them consistently. (#14022)

pull/14029/head
frosch 2025-04-18 15:19:28 +02:00 committed by GitHub
parent 2c59838acb
commit b862d4937f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 39 additions and 28 deletions

View File

@ -175,7 +175,7 @@ void Aircraft::GetImage(Direction direction, EngineImageType image_type, Vehicle
{
uint8_t spritenum = this->spritenum;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleSprite(this, direction, image_type, result);
if (result->IsValid()) return;
@ -191,7 +191,7 @@ void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteS
assert(v->subtype == AIR_HELICOPTER);
const Aircraft *w = v->Next()->Next();
if (is_custom_sprite(v->spritenum)) {
if (IsCustomVehicleSpriteNum(v->spritenum)) {
GetCustomRotorSprite(v, image_type, result);
if (result->IsValid()) return;
}
@ -205,7 +205,7 @@ static void GetAircraftIcon(EngineID engine, EngineImageType image_type, Vehicle
const Engine *e = Engine::Get(engine);
uint8_t spritenum = e->u.air.image_index;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
if (result->IsValid()) return;

View File

@ -228,7 +228,7 @@ void InitDepotWindowBlockSizes()
if (!e->IsEnabled()) continue;
uint w = TRAININFO_DEFAULT_VEHICLE_WIDTH;
if (e->GetGRF() != nullptr && is_custom_sprite(e->u.rail.image_index)) {
if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->u.rail.image_index)) {
w = e->GetGRF()->traininfo_vehicle_width;
if (w != VEHICLEINFO_FULL_VEHICLE_WIDTH) {
/* Hopeless.

View File

@ -43,9 +43,9 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int pro
uint8_t orig_spriteid = spriteid;
/* aircraft have different custom id in the GRF file */
if (spriteid == 0xFF) spriteid = 0xFD;
if (spriteid == 0xFF) spriteid = CUSTOM_VEHICLE_SPRITENUM;
if (spriteid < 0xFD) spriteid >>= 1;
if (spriteid < CUSTOM_VEHICLE_SPRITENUM) spriteid >>= 1;
if (IsValidNewGRFImageIndex<VEH_AIRCRAFT>(spriteid)) {
avi->image_index = spriteid;

View File

@ -62,9 +62,9 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, B
uint8_t orig_spriteid = spriteid;
/* cars have different custom id in the GRF file */
if (spriteid == 0xFF) spriteid = 0xFD;
if (spriteid == 0xFF) spriteid = CUSTOM_VEHICLE_SPRITENUM;
if (spriteid < 0xFD) spriteid >>= 1;
if (spriteid < CUSTOM_VEHICLE_SPRITENUM) spriteid >>= 1;
if (IsValidNewGRFImageIndex<VEH_ROAD>(spriteid)) {
rvi->image_index = spriteid;

View File

@ -45,9 +45,9 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint first, uint last, int prop, B
uint8_t orig_spriteid = spriteid;
/* ships have different custom id in the GRF file */
if (spriteid == 0xFF) spriteid = 0xFD;
if (spriteid == 0xFF) spriteid = CUSTOM_VEHICLE_SPRITENUM;
if (spriteid < 0xFD) spriteid >>= 1;
if (spriteid < CUSTOM_VEHICLE_SPRITENUM) spriteid >>= 1;
if (IsValidNewGRFImageIndex<VEH_SHIP>(spriteid)) {
svi->image_index = spriteid;

View File

@ -98,7 +98,7 @@ ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, ByteRead
/* TTD sprite IDs point to a location in a 16bit array, but we use it
* as an array index, so we need it to be half the original value. */
if (spriteid < 0xFD) spriteid >>= 1;
if (spriteid < CUSTOM_VEHICLE_SPRITENUM) spriteid >>= 1;
if (IsValidNewGRFImageIndex<VEH_TRAIN>(spriteid)) {
rvi->image_index = spriteid;

View File

@ -58,12 +58,12 @@ void ConvertTTDBasePrice(uint32_t base_pointer, const char *error_location, Pric
* Helper to check whether an image index is valid for a particular NewGRF vehicle.
* @tparam T The type of vehicle.
* @param image_index The image index to check.
* @return True iff the image index is valid, or 0xFD (use new graphics).
* @return True iff the image index is valid, or CUSTOM_VEHICLE_SPRITENUM (use new graphics).
*/
template <VehicleType T>
static inline bool IsValidNewGRFImageIndex(uint8_t image_index)
{
return image_index == 0xFD || IsValidImageIndex<T>(image_index);
return image_index == CUSTOM_VEHICLE_SPRITENUM || IsValidImageIndex<T>(image_index);
}
ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, ByteReader &buf);

View File

@ -838,8 +838,8 @@ static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *objec
case 0x46: return v->GetEngine()->grf_prop.local_id;
case 0x47: return GB(v->GetEngine()->grf_prop.local_id, 8, 8);
case 0x48:
if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum;
return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD;
if (v->type != VEH_TRAIN || v->spritenum != CUSTOM_VEHICLE_SPRITENUM) return v->spritenum;
return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? CUSTOM_VEHICLE_SPRITENUM_REVERSED : CUSTOM_VEHICLE_SPRITENUM;
case 0x49: return v->day_counter;
case 0x4A: return v->breakdowns_since_last_service;

View File

@ -107,7 +107,7 @@ static void GetRoadVehIcon(EngineID engine, EngineImageType image_type, VehicleS
const Engine *e = Engine::Get(engine);
uint8_t spritenum = e->u.road.image_index;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
if (result->IsValid()) return;
@ -122,8 +122,9 @@ void RoadVehicle::GetImage(Direction direction, EngineImageType image_type, Vehi
{
uint8_t spritenum = this->spritenum;
if (is_custom_sprite(spritenum)) {
GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)), image_type, result);
if (IsCustomVehicleSpriteNum(spritenum)) {
if (spritenum == CUSTOM_VEHICLE_SPRITENUM_REVERSED) direction = ReverseDir(direction);
GetCustomVehicleSprite(this, direction, image_type, result);
if (result->IsValid()) return;
spritenum = this->GetEngine()->original_image_index;

View File

@ -83,7 +83,7 @@ static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpri
const Engine *e = Engine::Get(engine);
uint8_t spritenum = e->u.ship.image_index;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
if (result->IsValid()) return;
@ -137,7 +137,7 @@ void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpri
if (image_type == EIT_ON_MAP) direction = this->rotation;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleSprite(this, direction, image_type, result);
if (result->IsValid()) return;

View File

@ -441,7 +441,7 @@ int Train::GetCursorImageOffset() const
int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
const Engine *e = this->GetEngine();
if (e->GetGRF() != nullptr && is_custom_sprite(e->u.rail.image_index)) {
if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->u.rail.image_index)) {
reference_width = e->GetGRF()->traininfo_vehicle_width;
}
@ -461,7 +461,7 @@ int Train::GetDisplayImageWidth(Point *offset) const
int vehicle_pitch = 0;
const Engine *e = this->GetEngine();
if (e->GetGRF() != nullptr && is_custom_sprite(e->u.rail.image_index)) {
if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->u.rail.image_index)) {
reference_width = e->GetGRF()->traininfo_vehicle_width;
vehicle_pitch = e->GetGRF()->traininfo_vehicle_pitch;
}
@ -495,8 +495,9 @@ void Train::GetImage(Direction direction, EngineImageType image_type, VehicleSpr
if (HasBit(this->flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
if (is_custom_sprite(spritenum)) {
GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)), image_type, result);
if (IsCustomVehicleSpriteNum(spritenum)) {
if (spritenum == CUSTOM_VEHICLE_SPRITENUM_REVERSED) direction = ReverseDir(direction);
GetCustomVehicleSprite(this, direction, image_type, result);
if (result->IsValid()) return;
spritenum = this->GetEngine()->original_image_index;
@ -516,7 +517,7 @@ static void GetRailIcon(EngineID engine, bool rear_head, int &y, EngineImageType
Direction dir = rear_head ? DIR_E : DIR_W;
uint8_t spritenum = e->u.rail.image_index;
if (is_custom_sprite(spritenum)) {
if (IsCustomVehicleSpriteNum(spritenum)) {
GetCustomVehicleIcon(engine, dir, image_type, result);
if (result->IsValid()) {
if (e->GetGRF() != nullptr) {

View File

@ -21,9 +21,18 @@
#include "track_type.h"
#include "livery.h"
#define is_custom_sprite(x) (x >= 0xFD)
#define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD)
#define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
/**
* Special values for Vehicle::spritenum and (Aircraft|Rail|Road|Ship)VehicleInfo::image_index
*/
enum CustomVehicleSpriteNum {
CUSTOM_VEHICLE_SPRITENUM = 0xFD, ///< Vehicle sprite from NewGRF
CUSTOM_VEHICLE_SPRITENUM_REVERSED = 0xFE, ///< Vehicle sprite from NewGRF with reverse driving direction (from articulation callback)
};
static inline bool IsCustomVehicleSpriteNum(uint8_t spritenum)
{
return spritenum >= CUSTOM_VEHICLE_SPRITENUM;
}
static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE{CalendarTime::DAYS_IN_YEAR * 2}; ///< Only vehicles older than this have a meaningful profit.
static const Money VEHICLE_PROFIT_THRESHOLD = 10000; ///< Threshold for a vehicle to be considered making good profit.