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 const Price clear_price_table[] = {
static constexpr Price clear_price_table[] = {
PR_CLEAR_GRASS,
PR_CLEAR_ROUGH,
PR_CLEAR_ROCKS,
@ -35,7 +35,9 @@ static CommandCost ClearTile_Clear(TileIndex tile, DoCommandFlags flags)
};
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)]]);
}
@ -100,7 +102,9 @@ static void DrawClearLandFence(const 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:
DrawClearLandTile(ti, GetClearDensity(ti->tile));
break;
@ -237,6 +241,8 @@ static void TileLoop_Clear(TileIndex tile)
default: break;
}
if (IsSnowTile(tile)) return;
switch (GetClearGround(tile)) {
case CLEAR_GRASS:
if (GetClearDensity(tile) == 3) return;

View File

@ -21,7 +21,7 @@ enum ClearGround : uint8_t {
CLEAR_ROUGH = 1, ///< 3
CLEAR_ROCKS = 2, ///< 3
CLEAR_FIELDS = 3, ///< 3
CLEAR_SNOW = 4, ///< 0-3
CLEAR_SNOW = 4, ///< 0-3 (Not stored in map.)
CLEAR_DESERT = 5, ///< 1,3
};
@ -38,18 +38,6 @@ inline bool IsSnowTile(Tile t)
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.
* @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)
{
if (IsSnowTile(t)) return CLEAR_SNOW;
return GetRawClearGround(t);
assert(IsTileType(t, MP_CLEAR));
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.
* @param t the tile to make snowy
* @param density The density of snowiness.
* @pre GetClearGround(t) != CLEAR_SNOW
* @pre !IsSnowTile(t)
*/
inline void MakeSnow(Tile t, uint density = 0)
{
assert(GetClearGround(t) != CLEAR_SNOW);
assert(!IsSnowTile(t));
SetBit(t.m3(), 4);
if (GetRawClearGround(t) == CLEAR_FIELDS) {
if (GetClearGround(t) == CLEAR_FIELDS) {
SetClearGroundDensity(t, CLEAR_GRASS, density);
} else {
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.
* @param t the tile to clear of snow
* @pre GetClearGround(t) == CLEAR_SNOW
* @pre IsSnowTile(t)
*/
inline void ClearSnow(Tile t)
{
assert(GetClearGround(t) == CLEAR_SNOW);
assert(IsSnowTile(t));
ClrBit(t.m3(), 4);
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)
{
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;
default: return false;
}

View File

@ -633,7 +633,7 @@ public:
/* Clear farmland. */
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);
}
}

View File

@ -2387,7 +2387,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_135)) {
for (auto t : Map::Iterate()) {
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));
SetBit(t.m3(), 4);
} else {

View File

@ -130,14 +130,14 @@
{
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)
{
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)

View File

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