1
0
Fork 0

Codechange: strongly type TownID

pull/13579/head
Rubidium 2025-02-07 18:58:57 +01:00 committed by rubidium42
parent d3408a4542
commit 1003967267
10 changed files with 18 additions and 20 deletions

View File

@ -81,7 +81,7 @@ inline CargoMonitorID EncodeCargoTownMonitor(CompanyID company, CargoType ctype,
assert(company < (1 << CCB_COMPANY_LENGTH));
uint32_t ret = 0;
SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town);
SB(ret, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH, town.base());
SB(ret, CCB_CARGO_TYPE_START, CCB_CARGO_TYPE_LENGTH, ctype);
SB(ret, CCB_COMPANY_START, CCB_COMPANY_LENGTH, company.base());
return ret;

View File

@ -170,7 +170,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID, uint8_
case 0x81: return GB(this->tile.base(), 8, 8);
/* Pointer to the town the industry is associated with */
case 0x82: return this->industry->town->index;
case 0x82: return this->industry->town->index.base();
case 0x83:
case 0x84:
case 0x85: Debug(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
@ -357,7 +357,7 @@ static uint32_t GetCountAndDistanceOfClosestInstance(uint8_t param_setID, uint8_
case 0x80: return this->industry->location.tile.base();
case 0x81: return GB(this->industry->location.tile.base(), 8, 8);
/* Pointer to the town the industry is associated with */
case 0x82: return this->industry->town->index;
case 0x82: return this->industry->town->index.base();
case 0x83:
case 0x84:
case 0x85: Debug(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported

View File

@ -31,7 +31,7 @@
return 0;
/* Town index */
case 0x41: return this->t->index;
case 0x41: return this->t->index.base();
/* Get a variable from the persistent storage */
case 0x7C: {

View File

@ -923,7 +923,7 @@ uint32_t SerialiseNewsReference(const NewsReference &reference)
uint32_t operator()(const VehicleID v) { return v; }
uint32_t operator()(const StationID s) { return s; }
uint32_t operator()(const IndustryID i) { return i.base(); }
uint32_t operator()(const TownID t) { return t; }
uint32_t operator()(const TownID t) { return t.base(); }
uint32_t operator()(const EngineID e) { return e.base(); }
};

View File

@ -636,7 +636,7 @@ inline void MakeRoadNormal(Tile t, RoadBits bits, RoadType road_rt, RoadType tra
{
SetTileType(t, MP_ROAD);
SetTileOwner(t, road);
t.m2() = town;
t.m2() = town.base();
t.m3() = (tram_rt != INVALID_ROADTYPE ? bits : 0);
t.m5() = (road_rt != INVALID_ROADTYPE ? bits : 0) | ROAD_TILE_NORMAL << 6;
SB(t.m6(), 2, 4, 0);
@ -661,7 +661,7 @@ inline void MakeRoadCrossing(Tile t, Owner road, Owner tram, Owner rail, Axis ro
{
SetTileType(t, MP_ROAD);
SetTileOwner(t, rail);
t.m2() = town;
t.m2() = town.base();
t.m3() = 0;
t.m4() = INVALID_ROADTYPE;
t.m5() = ROAD_TILE_CROSSING << 6 | roaddir;

View File

@ -1010,7 +1010,7 @@ bool AfterLoadGame()
if ((GB(t.m5(), 4, 2) == ROAD_TILE_CROSSING ? (Owner)t.m3() : GetTileOwner(t)) == OWNER_TOWN) {
SetTownIndex(t, CalcClosestTownFromTile(t)->index);
} else {
SetTownIndex(t, TOWN_BEGIN);
SetTownIndex(t, TownID::Begin());
}
break;
@ -1234,11 +1234,11 @@ bool AfterLoadGame()
GetRailType(t)
);
} else {
TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, UINT_MAX)->index : TOWN_BEGIN;
TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, UINT_MAX)->index : TownID::Begin();
/* MakeRoadNormal */
SetTileType(t, MP_ROAD);
t.m2() = town;
t.m2() = town.base();
t.m3() = 0;
t.m5() = (axis == AXIS_X ? ROAD_Y : ROAD_X) | ROAD_TILE_NORMAL << 6;
SB(t.m6(), 2, 4, 0);
@ -1626,7 +1626,7 @@ bool AfterLoadGame()
if (IsSavegameVersionBefore(SLV_52)) {
for (auto t : Map::Iterate()) {
if (IsTileType(t, MP_OBJECT) && t.m5() == OBJECT_STATUE) {
t.m2() = CalcClosestTownFromTile(t)->index;
t.m2() = CalcClosestTownFromTile(t)->index.base();
}
}
}

View File

@ -53,7 +53,7 @@ struct DEPTChunkHandler : ChunkHandler {
SlObject(depot, slt);
/* Set the town 'pointer' so we can restore it later. */
if (IsSavegameVersionBefore(SLV_141)) depot->town = (Town *)(size_t)_town_index;
if (IsSavegameVersionBefore(SLV_141)) depot->town = reinterpret_cast<Town *>(static_cast<size_t>(_town_index.base()));
}
}

View File

@ -33,7 +33,7 @@ static const uint TOWN_GROWTH_DESERT = 0xFFFFFFFF; ///< The town needs the cargo
static const uint16_t TOWN_GROWTH_RATE_NONE = 0xFFFF; ///< Special value for Town::growth_rate to disable town growth.
static const uint16_t MAX_TOWN_GROWTH_TICKS = 930; ///< Max amount of original town ticks that still fit into uint16_t, about equal to UINT16_MAX / TOWN_GROWTH_TICKS but slightly less to simplify calculations
typedef Pool<Town, TownID, 64, TOWN_END> TownPool;
typedef Pool<Town, TownID, 64, TownID::End().base()> TownPool;
extern TownPool _town_pool;
/** Data structure with cached data of towns. */

View File

@ -35,7 +35,7 @@ inline TownID GetTownIndex(Tile t)
inline void SetTownIndex(Tile t, TownID index)
{
assert(IsTileType(t, MP_HOUSE) || (IsTileType(t, MP_ROAD) && !IsRoadDepot(t)));
t.m2() = index;
t.m2() = index.base();
}
/**
@ -378,7 +378,7 @@ inline void MakeHouseTile(Tile t, TownID tid, uint8_t counter, uint8_t stage, Ho
SetTileType(t, MP_HOUSE);
t.m1() = random_bits;
t.m2() = tid;
t.m2() = tid.base();
t.m3() = 0;
SetHouseType(t, type);
SetHouseCompleted(t, stage == TOWN_HOUSE_COMPLETED);

View File

@ -11,12 +11,10 @@
#define TOWN_TYPE_H
#include "core/enum_type.hpp"
#include "core/pool_type.hpp"
enum TownID : uint16_t {
TOWN_BEGIN = 0,
TOWN_END = 64000,
INVALID_TOWN = 0xFFFF
};
using TownID = PoolID<uint16_t, struct TownIDTag, 64000, 0xFFFF>;
static constexpr TownID INVALID_TOWN = TownID::Invalid();
struct Town;