summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/misc_gui.cpp2
-rw-r--r--src/strings.cpp2
-rw-r--r--src/town.h44
-rw-r--r--src/town_cmd.cpp89
-rw-r--r--src/town_gui.cpp2
5 files changed, 47 insertions, 92 deletions
diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp
index e5d728025..21cd4dd90 100644
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -134,7 +134,7 @@ static void Place_LandInfo(TileIndex tile)
GetString(_landinfo_data[3], STR_LANDINFO_COORDS, lastof(_landinfo_data[3]));
SetDParam(0, STR_01A9_NONE);
- if (t != NULL && IsValidTown(t)) {
+ if (t != NULL && t->IsValid()) {
SetDParam(0, STR_TOWN);
SetDParam(1, t->index);
}
diff --git a/src/strings.cpp b/src/strings.cpp
index 6edf9e22c..bcd44db09 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -824,7 +824,7 @@ static char* FormatString(char* buff, const char* str, const int64* argv, uint c
const Town* t = GetTown(GetInt32(&argv));
int64 temp[1];
- assert(IsValidTown(t));
+ assert(t->IsValid());
temp[0] = t->townnameparts;
uint32 grfid = t->townnamegrfid;
diff --git a/src/town.h b/src/town.h
index 347271557..bda85ff78 100644
--- a/src/town.h
+++ b/src/town.h
@@ -75,7 +75,10 @@ struct BuildingCounts {
uint8 class_count[HOUSE_CLASS_MAX];
};
-struct Town {
+struct Town;
+DECLARE_OLD_POOL(Town, Town, 3, 8000)
+
+struct Town : PoolItem<Town, TownID, &_Town_pool> {
TileIndex xy;
/* Current population of people and amount of houses. */
@@ -139,9 +142,6 @@ struct Town {
/* Fund road reconstruction in action? */
byte road_build_months;
- /* Index in town array */
- TownID index;
-
/* If this is a larger town, and should grow more quickly. */
bool larger_town;
@@ -150,6 +150,18 @@ struct Town {
/* NOSAVE: The number of each type of building in the town. */
BuildingCounts building_counts;
+
+ /**
+ * Creates a new town
+ */
+ Town(TileIndex tile = 0);
+
+ /** Destroy the town */
+ ~Town();
+
+ bool IsValid() const { return this->xy != 0; }
+
+ void QuickFree();
};
struct HouseSpec {
@@ -270,8 +282,6 @@ bool CheckforTownRating(uint32 flags, Town *t, byte type);
VARDEF const Town** _town_sort;
-DECLARE_OLD_POOL(Town, Town, 3, 8000)
-
static inline HouseSpec *GetHouseSpecs(HouseID house_id)
{
assert(house_id < HOUSE_MAX);
@@ -279,23 +289,13 @@ static inline HouseSpec *GetHouseSpecs(HouseID house_id)
}
/**
- * Check if a Town really exists.
- * @param town to inquiry
- * @return true if it exists
- */
-static inline bool IsValidTown(const Town* town)
-{
- return town->xy != 0;
-}
-
-/**
* Check if a TownID is valid.
* @param index to inquiry in the pool of town
* @return true if it exists
*/
static inline bool IsValidTownID(TownID index)
{
- return index < GetTownPoolSize() && IsValidTown(GetTown(index));
+ return index < GetTownPoolSize() && GetTown(index)->IsValid();
}
VARDEF uint _total_towns;
@@ -337,17 +337,9 @@ static inline Town *GetRandomTown()
return GetTown(index);
}
-void DestroyTown(Town *t);
-
-static inline void DeleteTown(Town *t)
-{
- DestroyTown(t);
- t->xy = 0;
-}
-
Town* CalcClosestTownFromTile(TileIndex tile, uint threshold);
-#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (IsValidTown(t))
+#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) if (t->IsValid())
#define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0)
VARDEF bool _town_sort_dirty;
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index 956e8bfca..0e38750f0 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -40,52 +40,40 @@
#include "newgrf_house.h"
#include "newgrf_commons.h"
#include "newgrf_townname.h"
+#include "misc/autoptr.hpp"
-/**
- * Called if a new block is added to the town-pool
- */
-static void TownPoolNewBlock(uint start_item)
-{
- Town *t;
+/* Initialize the town-pool */
+DEFINE_OLD_POOL_GENERIC(Town, Town)
- /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
- * TODO - This is just a temporary stage, this will be removed. */
- for (t = GetTown(start_item); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) t->index = start_item++;
+Town::Town(TileIndex tile)
+{
+ this->xy = tile;
}
-/* Initialize the town-pool */
-DEFINE_OLD_POOL(Town, Town, TownPoolNewBlock, NULL)
-
-/**
- * Removes a specific town as well as all industries
- * under its "juridiction"
- * @param t Town to remove
- */
-void DestroyTown(Town *t)
+Town::~Town()
{
Industry *i;
- TileIndex tile;
/* Delete town authority window
* and remove from list of sorted towns */
- DeleteWindowById(WC_TOWN_VIEW, t->index);
+ DeleteWindowById(WC_TOWN_VIEW, this->index);
_town_sort_dirty = true;
_total_towns--;
/* Delete all industries belonging to the town */
- FOR_ALL_INDUSTRIES(i) if (i->town == t) DeleteIndustry(i);
+ FOR_ALL_INDUSTRIES(i) if (i->town == this) DeleteIndustry(i);
/* Go through all tiles and delete those belonging to the town */
- for (tile = 0; tile < MapSize(); ++tile) {
+ for (TileIndex tile = 0; tile < MapSize(); ++tile) {
switch (GetTileType(tile)) {
case MP_HOUSE:
- if (GetTownByTile(tile) == t) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
+ if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
break;
case MP_ROAD:
case MP_TUNNELBRIDGE:
if (IsTileOwner(tile, OWNER_TOWN) &&
- ClosestTownFromTile(tile, (uint)-1) == t)
+ ClosestTownFromTile(tile, (uint)-1) == this)
DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
break;
@@ -94,10 +82,17 @@ void DestroyTown(Town *t)
}
}
- DeleteName(t->townnametype);
- DeleteSubsidyWithTown(t->index);
+ DeleteSubsidyWithTown(this->index);
MarkWholeScreenDirty();
+
+ this->QuickFree();
+ this->xy = 0;
+}
+
+void Town::QuickFree()
+{
+ DeleteName(this->townnametype);
}
// Local
@@ -1359,10 +1354,6 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize
extern int _nb_orig_names;
int x, i;
- /* clear the town struct */
- i = t->index;
- memset(t, 0, sizeof(Town));
- t->index = i;
_total_towns++;
t->xy = tile;
@@ -1446,30 +1437,6 @@ static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize
UpdateTownMaxPass(t);
}
-static Town *AllocateTown()
-{
- Town *t;
-
- /* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
- * TODO - This is just a temporary stage, this will be removed. */
- for (t = GetTown(0); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) {
- if (!IsValidTown(t)) {
- TownID index = t->index;
-
- memset(t, 0, sizeof(Town));
- t->index = index;
-
- return t;
- }
- }
-
- /* Check if we can add a block to the pool */
- if (AddBlockToPool(&_Town_pool))
- return AllocateTown();
-
- return NULL;
-}
-
/** Create a new town.
* This obviously only works in the scenario editor. Function not removed
* as it might be possible in the future to fund your own town :)
@@ -1480,7 +1447,6 @@ static Town *AllocateTown()
*/
CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
- Town *t;
uint32 townnameparts;
/* Only in the scenario editor */
@@ -1507,14 +1473,16 @@ CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
return_cmd_error(STR_023A_TOO_MANY_TOWNS);
/* Allocate town struct */
- t = AllocateTown();
+ Town *t = new Town(tile);
if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
+ AutoPtrT<Town> t_auto_delete = t;
/* Create the town */
if (flags & DC_EXEC) {
_generating_world = true;
DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
_generating_world = false;
+ t_auto_delete.Detach();
}
return CommandCost();
}
@@ -1540,7 +1508,7 @@ Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size)
if (!CreateTownName(&townnameparts)) break;
/* Allocate a town struct */
- t = AllocateTown();
+ t = new Town(tile);
if (t == NULL) break;
DoCreateTown(t, tile, townnameparts, mode, size);
@@ -2476,12 +2444,7 @@ static void Load_TOWN()
_total_towns = 0;
while ((index = SlIterateArray()) != -1) {
- Town *t;
-
- if (!AddBlockIfNeeded(&_Town_pool, index))
- error("Towns: failed loading savegame: too many towns");
-
- t = GetTown(index);
+ Town *t = new (index) Town();
SlObject(t, _town_desc);
_total_towns++;
diff --git a/src/town_gui.cpp b/src/town_gui.cpp
index eb9410c20..02eabda1a 100644
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -278,7 +278,7 @@ static void TownViewWndProc(Window *w, WindowEvent *e)
break;
case 10: /* delete town */
- DeleteTown(t);
+ delete t;
break;
}
break;