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
commit52ac59242245ab3914a752f2d58a4d00f7d0657e (patch)
treeba7c12966aad60d82c8e03bb0593bce949ec2b86 /src/town_cmd.cpp
parent65e81d5e9bc69d1a58854d87ccb3da06eb108a57 (diff)
downloadopenttd-52ac59242245ab3914a752f2d58a4d00f7d0657e.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);
}