1
0
Fork 0

Codechange: Use EnumBitSet for GrfMiscBits. (#13873)

pull/13874/head
Peter Nelson 2025-03-22 21:07:59 +00:00 committed by GitHub
parent af49320637
commit 21d2a94809
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 26 deletions

View File

@ -119,7 +119,7 @@ static void DrawTile_Clear(TileInfo *ti)
break; break;
case CLEAR_ROCKS: case CLEAR_ROCKS:
DrawGroundSprite((HasGrfMiscBit(GMB_SECOND_ROCKY_TILE_SET) && (TileHash(ti->x, ti->y) & 1) ? SPR_FLAT_ROCKY_LAND_2 : SPR_FLAT_ROCKY_LAND_1) + SlopeToSpriteOffset(ti->tileh), PAL_NONE); DrawGroundSprite((HasGrfMiscBit(GrfMiscBit::SecondRockyTileSet) && (TileHash(ti->x, ti->y) & 1) ? SPR_FLAT_ROCKY_LAND_2 : SPR_FLAT_ROCKY_LAND_1) + SlopeToSpriteOffset(ti->tileh), PAL_NONE);
break; break;
case CLEAR_FIELDS: case CLEAR_FIELDS:

View File

@ -78,7 +78,7 @@ const std::vector<GRFFile *> &GetAllGRFFiles()
} }
/** Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E */ /** Miscellaneous GRF features, set by Action 0x0D, parameter 0x9E */
uint8_t _misc_grf_features = 0; GrfMiscBits _misc_grf_features{};
/** 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */ /** 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */
static uint32_t _ttdpatch_flags[8]; static uint32_t _ttdpatch_flags[8];
@ -6953,13 +6953,16 @@ bool GetGlobalVariable(uint8_t param, uint32_t *value, const GRFFile *grffile)
*value = 1; *value = 1;
return true; return true;
case 0x1E: // Miscellaneous GRF features case 0x1E: { // Miscellaneous GRF features
*value = _misc_grf_features; GrfMiscBits bits = _misc_grf_features;
/* Add the local flags */ /* Add the local flags */
assert(!HasBit(*value, GMB_TRAIN_WIDTH_32_PIXELS)); assert(!bits.Test(GrfMiscBit::TrainWidth32Pixels));
if (_cur.grffile->traininfo_vehicle_width == VEHICLEINFO_FULL_VEHICLE_WIDTH) SetBit(*value, GMB_TRAIN_WIDTH_32_PIXELS); if (_cur.grffile->traininfo_vehicle_width == VEHICLEINFO_FULL_VEHICLE_WIDTH) bits.Set(GrfMiscBit::TrainWidth32Pixels);
*value = bits.base();
return true; return true;
}
/* case 0x1F: // locale dependent settings not implemented to avoid desync */ /* case 0x1F: // locale dependent settings not implemented to avoid desync */
@ -8000,22 +8003,25 @@ static void ParamSet(ByteReader &buf)
GrfMsg(7, "ParamSet: Skipping unimplemented target 0x{:02X}", target); GrfMsg(7, "ParamSet: Skipping unimplemented target 0x{:02X}", target);
break; break;
case 0x9E: // Miscellaneous GRF features case 0x9E: { // Miscellaneous GRF features
GrfMiscBits bits(res);
/* Set train list engine width */ /* Set train list engine width */
_cur.grffile->traininfo_vehicle_width = HasBit(res, GMB_TRAIN_WIDTH_32_PIXELS) ? VEHICLEINFO_FULL_VEHICLE_WIDTH : TRAININFO_DEFAULT_VEHICLE_WIDTH; _cur.grffile->traininfo_vehicle_width = bits.Test(GrfMiscBit::TrainWidth32Pixels) ? VEHICLEINFO_FULL_VEHICLE_WIDTH : TRAININFO_DEFAULT_VEHICLE_WIDTH;
/* Remove the local flags from the global flags */ /* Remove the local flags from the global flags */
ClrBit(res, GMB_TRAIN_WIDTH_32_PIXELS); bits.Reset(GrfMiscBit::TrainWidth32Pixels);
/* Only copy safe bits for static grfs */ /* Only copy safe bits for static grfs */
if (_cur.grfconfig->flags.Test(GRFConfigFlag::Static)) { if (_cur.grfconfig->flags.Test(GRFConfigFlag::Static)) {
uint32_t safe_bits = 0; GrfMiscBits safe_bits = GrfMiscBit::SecondRockyTileSet;
SetBit(safe_bits, GMB_SECOND_ROCKY_TILE_SET);
_misc_grf_features = (_misc_grf_features & ~safe_bits) | (res & safe_bits); _misc_grf_features.Reset(safe_bits);
_misc_grf_features.Set(bits & safe_bits);
} else { } else {
_misc_grf_features = res; _misc_grf_features = bits;
} }
break; break;
}
case 0x9F: // locale-dependent settings case 0x9F: // locale-dependent settings
GrfMsg(7, "ParamSet: Skipping unimplemented target 0x{:02X}", target); GrfMsg(7, "ParamSet: Skipping unimplemented target 0x{:02X}", target);
@ -9134,7 +9140,7 @@ void ResetNewGRFData()
SetupCargoForClimate(_settings_game.game_creation.landscape); SetupCargoForClimate(_settings_game.game_creation.landscape);
/* Reset misc GRF features and train list display variables */ /* Reset misc GRF features and train list display variables */
_misc_grf_features = 0; _misc_grf_features = {};
_loaded_newgrf_features.has_2CC = false; _loaded_newgrf_features.has_2CC = false;
_loaded_newgrf_features.used_liveries = 1 << LS_DEFAULT; _loaded_newgrf_features.used_liveries = 1 << LS_DEFAULT;

View File

@ -55,16 +55,18 @@ enum GrfLoadingStage : uint8_t {
DECLARE_INCREMENT_DECREMENT_OPERATORS(GrfLoadingStage) DECLARE_INCREMENT_DECREMENT_OPERATORS(GrfLoadingStage)
enum GrfMiscBit : uint8_t { enum class GrfMiscBit : uint8_t {
GMB_DESERT_TREES_FIELDS = 0, // Unsupported. DesertTreesFields = 0, // Unsupported.
GMB_DESERT_PAVED_ROADS = 1, DesertPavedRoads = 1,
GMB_FIELD_BOUNDING_BOX = 2, // Unsupported. FieldBoundingBox = 2, // Unsupported.
GMB_TRAIN_WIDTH_32_PIXELS = 3, ///< Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable; @see GRFFile::traininfo_vehicle_width TrainWidth32Pixels = 3, ///< Use 32 pixels per train vehicle in depot gui and vehicle details. Never set in the global variable; @see GRFFile::traininfo_vehicle_width
GMB_AMBIENT_SOUND_CALLBACK = 4, AmbientSoundCallback = 4,
GMB_CATENARY_ON_3RD_TRACK = 5, // Unsupported. CatenaryOn3rdTrack = 5, // Unsupported.
GMB_SECOND_ROCKY_TILE_SET = 6, SecondRockyTileSet = 6,
}; };
using GrfMiscBits = EnumBitSet<GrfMiscBit, uint8_t>;
enum GrfSpecFeature : uint8_t { enum GrfSpecFeature : uint8_t {
GSF_TRAINS, GSF_TRAINS,
GSF_ROADVEHICLES, GSF_ROADVEHICLES,
@ -192,8 +194,8 @@ struct GRFLoadedFeatures {
*/ */
inline bool HasGrfMiscBit(GrfMiscBit bit) inline bool HasGrfMiscBit(GrfMiscBit bit)
{ {
extern uint8_t _misc_grf_features; extern GrfMiscBits _misc_grf_features;
return HasBit(_misc_grf_features, bit); return _misc_grf_features.Test(bit);
} }
/* Indicates which are the newgrf features currently loaded ingame */ /* Indicates which are the newgrf features currently loaded ingame */

View File

@ -54,7 +54,7 @@ void AmbientSoundEffectCallback(TileIndex tile);
inline void AmbientSoundEffect(TileIndex tile) inline void AmbientSoundEffect(TileIndex tile)
{ {
/* Only run callback if enabled. */ /* Only run callback if enabled. */
if (!HasGrfMiscBit(GMB_AMBIENT_SOUND_CALLBACK)) return; if (!HasGrfMiscBit(GrfMiscBit::AmbientSoundCallback)) return;
AmbientSoundEffectCallback(tile); AmbientSoundEffectCallback(tile);
} }

View File

@ -1362,7 +1362,7 @@ static uint GetRoadSpriteOffset(Slope slope, RoadBits bits)
static bool DrawRoadAsSnowDesert(bool snow_or_desert, Roadside roadside) static bool DrawRoadAsSnowDesert(bool snow_or_desert, Roadside roadside)
{ {
return (snow_or_desert && return (snow_or_desert &&
!(_settings_game.game_creation.landscape == LandscapeType::Tropic && HasGrfMiscBit(GMB_DESERT_PAVED_ROADS) && !(_settings_game.game_creation.landscape == LandscapeType::Tropic && HasGrfMiscBit(GrfMiscBit::DesertPavedRoads) &&
roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS)); roadside != ROADSIDE_BARREN && roadside != ROADSIDE_GRASS && roadside != ROADSIDE_GRASS_ROAD_WORKS));
} }