From a14ad77a36c836fd3f6940eadeb8161e7ad02f92 Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 25 Oct 2008 13:51:47 +0000 Subject: (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) --- src/town_cmd.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/town_cmd.cpp') 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); } -- cgit v1.2.3-54-g00ecf