summaryrefslogtreecommitdiff
path: root/src/town_cmd.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-10-25 13:51:47 +0000
committerrubidium <rubidium@openttd.org>2008-10-25 13:51:47 +0000
commita14ad77a36c836fd3f6940eadeb8161e7ad02f92 (patch)
treeba7c12966aad60d82c8e03bb0593bce949ec2b86 /src/town_cmd.cpp
parent9075a2bbfc7c33a089fe100c42b96b831521d6d8 (diff)
downloadopenttd-a14ad77a36c836fd3f6940eadeb8161e7ad02f92.tar.xz
(svn r14528) -Codechange: cache the closest town for all road tiles instead of only roads owned by tiles. This replaces a O(n) search over all towns from the road's tileloop with a O(1) lookup (PhilSophus)
Diffstat (limited to 'src/town_cmd.cpp')
-rw-r--r--src/town_cmd.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp
index e55781b99..daa8a298b 100644
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -8,6 +8,7 @@
#include "debug.h"
#include "road_map.h"
#include "road_internal.h" /* Cleaning up road bits */
+#include "road_cmd.h"
#include "landscape.h"
#include "town_map.h"
#include "tunnel_map.h"
@@ -93,6 +94,12 @@ Town::~Town()
break;
case MP_ROAD:
+ if (!IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) && GetTownIndex(tile) == this->index) {
+ /* Town-owned roads get cleared soon, anyway */
+ SetTownIndex(tile, (TownID)INVALID_TOWN);
+ break;
+ }
+ /* Fall-through */
case MP_TUNNELBRIDGE:
if (IsTileOwner(tile, OWNER_TOWN) &&
ClosestTownFromTile(tile, UINT_MAX) == this)
@@ -1558,6 +1565,7 @@ CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Town *t = new Town(tile);
_generating_world = true;
DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1);
+ InvalidateTownForRoadTile();
_generating_world = false;
}
return CommandCost();
@@ -2468,6 +2476,19 @@ Town *ClosestTownFromTile(TileIndex tile, uint threshold)
IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)
)) {
return GetTownByTile(tile);
+ } else if (IsTileType(tile, MP_ROAD)) {
+ TownID town_id = GetTownIndex(tile);
+ Town *town;
+
+ if (town_id == INVALID_TOWN) {
+ town = CalcClosestTownFromTile(tile, UINT_MAX);
+ if (town != NULL) SetTownIndex(tile, town->index);
+ } else {
+ town = GetTown(town_id);
+ }
+
+ if (town != NULL && town->IsValid() && DistanceManhattan(tile, town->xy) < threshold) return town;
+ return NULL;
} else {
return CalcClosestTownFromTile(tile, threshold);
}