1
0
Fork 0

Codechange: Handle SnowTile state separately from ClearGround.

This removes the need for ClearGround to pretend that CLEAR_SNOW exists.
pull/13660/head
Peter Nelson 2025-02-20 19:21:39 +00:00 committed by Peter Nelson
parent 02a1f59a6c
commit 3cf9b15959
7 changed files with 36 additions and 37 deletions

View File

@ -25,7 +25,7 @@
static CommandCost ClearTile_Clear(TileIndex tile, DoCommandFlags flags) static CommandCost ClearTile_Clear(TileIndex tile, DoCommandFlags flags)
{ {
static const Price clear_price_table[] = { static constexpr Price clear_price_table[] = {
PR_CLEAR_GRASS, PR_CLEAR_GRASS,
PR_CLEAR_ROUGH, PR_CLEAR_ROUGH,
PR_CLEAR_ROCKS, PR_CLEAR_ROCKS,
@ -35,7 +35,9 @@ static CommandCost ClearTile_Clear(TileIndex tile, DoCommandFlags flags)
}; };
CommandCost price(EXPENSES_CONSTRUCTION); CommandCost price(EXPENSES_CONSTRUCTION);
if (!IsClearGround(tile, CLEAR_GRASS) || GetClearDensity(tile) != 0) { if (IsSnowTile(tile)) {
price.AddCost(_price[clear_price_table[CLEAR_SNOW]]);
} else if (!IsClearGround(tile, CLEAR_GRASS) || GetClearDensity(tile) != 0) {
price.AddCost(_price[clear_price_table[GetClearGround(tile)]]); price.AddCost(_price[clear_price_table[GetClearGround(tile)]]);
} }
@ -100,7 +102,9 @@ static void DrawClearLandFence(const TileInfo *ti)
static void DrawTile_Clear(TileInfo *ti) static void DrawTile_Clear(TileInfo *ti)
{ {
switch (GetClearGround(ti->tile)) { ClearGround ground = IsSnowTile(ti->tile) ? CLEAR_SNOW : GetClearGround(ti->tile);
switch (ground) {
case CLEAR_GRASS: case CLEAR_GRASS:
DrawClearLandTile(ti, GetClearDensity(ti->tile)); DrawClearLandTile(ti, GetClearDensity(ti->tile));
break; break;
@ -237,6 +241,8 @@ static void TileLoop_Clear(TileIndex tile)
default: break; default: break;
} }
if (IsSnowTile(tile)) return;
switch (GetClearGround(tile)) { switch (GetClearGround(tile)) {
case CLEAR_GRASS: case CLEAR_GRASS:
if (GetClearDensity(tile) == 3) return; if (GetClearDensity(tile) == 3) return;

View File

@ -21,7 +21,7 @@ enum ClearGround : uint8_t {
CLEAR_ROUGH = 1, ///< 3 CLEAR_ROUGH = 1, ///< 3
CLEAR_ROCKS = 2, ///< 3 CLEAR_ROCKS = 2, ///< 3
CLEAR_FIELDS = 3, ///< 3 CLEAR_FIELDS = 3, ///< 3
CLEAR_SNOW = 4, ///< 0-3 CLEAR_SNOW = 4, ///< 0-3 (Not stored in map.)
CLEAR_DESERT = 5, ///< 1,3 CLEAR_DESERT = 5, ///< 1,3
}; };
@ -38,18 +38,6 @@ inline bool IsSnowTile(Tile t)
return HasBit(t.m3(), 4); return HasBit(t.m3(), 4);
} }
/**
* Get the type of clear tile but never return CLEAR_SNOW.
* @param t the tile to get the clear ground type of
* @pre IsTileType(t, MP_CLEAR)
* @return the ground type
*/
inline ClearGround GetRawClearGround(Tile t)
{
assert(IsTileType(t, MP_CLEAR));
return (ClearGround)GB(t.m5(), 2, 3);
}
/** /**
* Get the type of clear tile. * Get the type of clear tile.
* @param t the tile to get the clear ground type of * @param t the tile to get the clear ground type of
@ -58,8 +46,8 @@ inline ClearGround GetRawClearGround(Tile t)
*/ */
inline ClearGround GetClearGround(Tile t) inline ClearGround GetClearGround(Tile t)
{ {
if (IsSnowTile(t)) return CLEAR_SNOW; assert(IsTileType(t, MP_CLEAR));
return GetRawClearGround(t); return static_cast<ClearGround>(GB(t.m5(), 2, 3));
} }
/** /**
@ -295,13 +283,13 @@ inline void MakeField(Tile t, uint field_type, IndustryID industry)
* Make a snow tile. * Make a snow tile.
* @param t the tile to make snowy * @param t the tile to make snowy
* @param density The density of snowiness. * @param density The density of snowiness.
* @pre GetClearGround(t) != CLEAR_SNOW * @pre !IsSnowTile(t)
*/ */
inline void MakeSnow(Tile t, uint density = 0) inline void MakeSnow(Tile t, uint density = 0)
{ {
assert(GetClearGround(t) != CLEAR_SNOW); assert(!IsSnowTile(t));
SetBit(t.m3(), 4); SetBit(t.m3(), 4);
if (GetRawClearGround(t) == CLEAR_FIELDS) { if (GetClearGround(t) == CLEAR_FIELDS) {
SetClearGroundDensity(t, CLEAR_GRASS, density); SetClearGroundDensity(t, CLEAR_GRASS, density);
} else { } else {
SetClearDensity(t, density); SetClearDensity(t, density);
@ -311,11 +299,11 @@ inline void MakeSnow(Tile t, uint density = 0)
/** /**
* Clear the snow from a tile and return it to its previous type. * Clear the snow from a tile and return it to its previous type.
* @param t the tile to clear of snow * @param t the tile to clear of snow
* @pre GetClearGround(t) == CLEAR_SNOW * @pre IsSnowTile(t)
*/ */
inline void ClearSnow(Tile t) inline void ClearSnow(Tile t)
{ {
assert(GetClearGround(t) == CLEAR_SNOW); assert(IsSnowTile(t));
ClrBit(t.m3(), 4); ClrBit(t.m3(), 4);
SetClearDensity(t, 3); SetClearDensity(t, 3);
} }

View File

@ -1004,7 +1004,7 @@ static const uint8_t _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5,
static bool IsSuitableForFarmField(TileIndex tile, bool allow_fields) static bool IsSuitableForFarmField(TileIndex tile, bool allow_fields)
{ {
switch (GetTileType(tile)) { switch (GetTileType(tile)) {
case MP_CLEAR: return !IsClearGround(tile, CLEAR_SNOW) && !IsClearGround(tile, CLEAR_DESERT) && (allow_fields || !IsClearGround(tile, CLEAR_FIELDS)); case MP_CLEAR: return !IsSnowTile(tile) && !IsClearGround(tile, CLEAR_DESERT) && (allow_fields || !IsClearGround(tile, CLEAR_FIELDS));
case MP_TREES: return GetTreeGround(tile) != TREE_GROUND_SHORE; case MP_TREES: return GetTreeGround(tile) != TREE_GROUND_SHORE;
default: return false; default: return false;
} }

View File

@ -633,7 +633,7 @@ public:
/* Clear farmland. */ /* Clear farmland. */
for (const auto tile : Map::Iterate()) { for (const auto tile : Map::Iterate()) {
if (IsTileType(tile, MP_CLEAR) && GetRawClearGround(tile) == CLEAR_FIELDS) { if (IsTileType(tile, MP_CLEAR) && GetClearGround(tile) == CLEAR_FIELDS) {
MakeClear(tile, CLEAR_GRASS, 3); MakeClear(tile, CLEAR_GRASS, 3);
} }
} }

View File

@ -2387,7 +2387,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_135)) { if (IsSavegameVersionBefore(SLV_135)) {
for (auto t : Map::Iterate()) { for (auto t : Map::Iterate()) {
if (IsTileType(t, MP_CLEAR)) { if (IsTileType(t, MP_CLEAR)) {
if (GetRawClearGround(t) == CLEAR_SNOW) { if (GetClearGround(t) == CLEAR_SNOW) { // CLEAR_SNOW becomes CLEAR_GRASS with IsSnowTile() set.
SetClearGroundDensity(t, CLEAR_GRASS, GetClearDensity(t)); SetClearGroundDensity(t, CLEAR_GRASS, GetClearDensity(t));
SetBit(t.m3(), 4); SetBit(t.m3(), 4);
} else { } else {

View File

@ -130,14 +130,14 @@
{ {
if (!::IsValidTile(tile)) return false; if (!::IsValidTile(tile)) return false;
return (::IsTileType(tile, MP_CLEAR) && ::GetRawClearGround(tile) == ::CLEAR_ROCKS); return (::IsTileType(tile, MP_CLEAR) && ::GetClearGround(tile) == ::CLEAR_ROCKS);
} }
/* static */ bool ScriptTile::IsRoughTile(TileIndex tile) /* static */ bool ScriptTile::IsRoughTile(TileIndex tile)
{ {
if (!::IsValidTile(tile)) return false; if (!::IsValidTile(tile)) return false;
return (::IsTileType(tile, MP_CLEAR) && ::GetRawClearGround(tile) == ::CLEAR_ROUGH); return (::IsTileType(tile, MP_CLEAR) && ::GetClearGround(tile) == ::CLEAR_ROUGH);
} }
/* static */ bool ScriptTile::IsSnowTile(TileIndex tile) /* static */ bool ScriptTile::IsSnowTile(TileIndex tile)

View File

@ -74,7 +74,7 @@ static bool CanPlantTreesOnTile(TileIndex tile, bool allow_desert)
return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile)); return !IsBridgeAbove(tile) && IsCoast(tile) && !IsSlopeWithOneCornerRaised(GetTileSlope(tile));
case MP_CLEAR: case MP_CLEAR:
return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && GetRawClearGround(tile) != CLEAR_ROCKS && return !IsBridgeAbove(tile) && !IsClearGround(tile, CLEAR_FIELDS) && !IsClearGround(tile, CLEAR_ROCKS) &&
(allow_desert || !IsClearGround(tile, CLEAR_DESERT)); (allow_desert || !IsClearGround(tile, CLEAR_DESERT));
default: return false; default: return false;
@ -106,15 +106,20 @@ static void PlantTreesOnTile(TileIndex tile, TreeType treetype, uint count, Tree
ClearNeighbourNonFloodingStates(tile); ClearNeighbourNonFloodingStates(tile);
break; break;
case MP_CLEAR: case MP_CLEAR: {
switch (GetClearGround(tile)) { ClearGround clearground = GetClearGround(tile);
case CLEAR_GRASS: ground = TREE_GROUND_GRASS; break; if (IsSnowTile(tile)) {
case CLEAR_ROUGH: ground = TREE_GROUND_ROUGH; break; ground = clearground == CLEAR_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT;
case CLEAR_SNOW: ground = GetRawClearGround(tile) == CLEAR_ROUGH ? TREE_GROUND_ROUGH_SNOW : TREE_GROUND_SNOW_DESERT; break; } else {
default: ground = TREE_GROUND_SNOW_DESERT; break; switch (clearground) {
case CLEAR_GRASS: ground = TREE_GROUND_GRASS; break;
case CLEAR_ROUGH: ground = TREE_GROUND_ROUGH; break;
default: ground = TREE_GROUND_SNOW_DESERT; break;
}
} }
if (GetClearGround(tile) != CLEAR_ROUGH) density = GetClearDensity(tile); if (clearground != CLEAR_ROUGH) density = GetClearDensity(tile);
break; break;
}
default: NOT_REACHED(); default: NOT_REACHED();
} }
@ -575,7 +580,7 @@ CommandCost CmdPlantTree(DoCommandFlags flags, TileIndex tile, TileIndex start_t
if (IsTileType(current_tile, MP_CLEAR)) { if (IsTileType(current_tile, MP_CLEAR)) {
/* Remove fields or rocks. Note that the ground will get barrened */ /* Remove fields or rocks. Note that the ground will get barrened */
switch (GetRawClearGround(current_tile)) { switch (GetClearGround(current_tile)) {
case CLEAR_FIELDS: case CLEAR_FIELDS:
case CLEAR_ROCKS: { case CLEAR_ROCKS: {
CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, current_tile); CommandCost ret = Command<CMD_LANDSCAPE_CLEAR>::Do(flags, current_tile);