(svn r16746) -Codechange: use Town::PostDestructor() instead of not very clean construct for invalidating nearest town for road tiles

pull/155/head
smatz 15 years ago
parent aa4ccab6a8
commit 8786ebbad7

@ -1248,7 +1248,7 @@ void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
* @param ignore town that should be ignored (because we are deleting it now)
* @pre invalidate == true implies _generating_world == true
*/
void UpdateNearestTownForRoadTiles(bool invalidate, const Town *ignore)
void UpdateNearestTownForRoadTiles(bool invalidate)
{
assert(!invalidate || _generating_world);
@ -1256,7 +1256,7 @@ void UpdateNearestTownForRoadTiles(bool invalidate, const Town *ignore)
if (IsTileType(t, MP_ROAD) && !HasTownOwnedRoad(t)) {
TownID tid = (TownID)INVALID_TOWN;
if (!invalidate) {
const Town *town = CalcClosestTownFromTile(t, UINT_MAX, ignore);
const Town *town = CalcClosestTownFromTile(t);
if (town != NULL) tid = town->index;
}
SetTownIndex(t, tid);

@ -8,6 +8,6 @@
#include "direction_type.h"
void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt);
void UpdateNearestTownForRoadTiles(bool invalidate, const struct Town *ignore = NULL);
void UpdateNearestTownForRoadTiles(bool invalidate);
#endif /* ROAD_CMD_H */

@ -142,6 +142,7 @@ struct Town : TownPool::PoolItem<&_town_pool> {
}
static Town *GetRandom();
static void PostDestructor(size_t index);
};
uint32 GetWorldPopulation();
@ -181,7 +182,7 @@ bool CheckforTownRating(DoCommandFlag flags, Town *t, TownRatingCheckType type);
TileIndexDiff GetHouseNorthPart(HouseID &house);
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX, const Town *ignore = NULL);
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold = UINT_MAX);
#define FOR_ALL_TOWNS_FROM(var, start) FOR_ALL_ITEMS_FROM(Town, town_index, var, start)
#define FOR_ALL_TOWNS(var) FOR_ALL_TOWNS_FROM(var, 0)

@ -102,8 +102,17 @@ Town::~Town()
DeleteSubsidyWithTown(this->index);
MarkWholeScreenDirty();
}
UpdateNearestTownForRoadTiles(false, this);
/**
* Invalidating of the "nearest town cache" has to be done
* after removing item from the pool.
* @param index index of deleted item
*/
void Town::PostDestructor(size_t index)
{
UpdateNearestTownForRoadTiles(false);
}
/**
@ -2694,14 +2703,13 @@ bool CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags)
}
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold, const Town *ignore)
Town *CalcClosestTownFromTile(TileIndex tile, uint threshold)
{
Town *t;
uint best = threshold;
Town *best_town = NULL;
FOR_ALL_TOWNS(t) {
if (t == ignore) continue;
uint dist = DistanceManhattan(tile, t->xy);
if (dist < best) {
best = dist;

Loading…
Cancel
Save