summaryrefslogtreecommitdiff
path: root/src/road_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/road_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/road_cmd.cpp')
-rw-r--r--src/road_cmd.cpp27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/road_cmd.cpp b/src/road_cmd.cpp
index 02c1ed6b2..bfa5075d1 100644
--- a/src/road_cmd.cpp
+++ b/src/road_cmd.cpp
@@ -4,6 +4,7 @@
#include "stdafx.h"
#include "openttd.h"
+#include "map_func.h"
#include "bridge_map.h"
#include "bridge.h"
#include "cmd_helper.h"
@@ -317,6 +318,11 @@ static CommandCost RemoveRoad(TileIndex tile, uint32 flags, RoadBits pieces, Roa
/* Includes MarkTileDirtyByTile() */
DoClearSquare(tile);
} else {
+ if (rt == ROADTYPE_ROAD && IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN)) {
+ /* Promote ownership from tram or highway and invalidate town index */
+ SetRoadOwner(tile, ROADTYPE_ROAD, GetRoadOwner(tile, (HasBit(rts, ROADTYPE_TRAM) ? ROADTYPE_TRAM : ROADTYPE_HWAY)));
+ SetTownIndex(tile, (TownID)INVALID_TOWN);
+ }
SetRoadBits(tile, ROAD_NONE, rt);
SetRoadTypes(tile, rts);
MarkTileDirtyByTile(tile);
@@ -354,6 +360,7 @@ static CommandCost RemoveRoad(TileIndex tile, uint32 flags, RoadBits pieces, Roa
if (reserved) SetTrackReservation(tile, tracks);
} else {
SetRoadTypes(tile, rts);
+ /* If we ever get HWAY and it is possible without road then we will need to promote ownership and invalidate town index here, too */
}
MarkTileDirtyByTile(tile);
YapfNotifyTrackLayoutChange(tile, FindFirstTrack(GetTrackBits(tile)));
@@ -479,6 +486,10 @@ CommandCost CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
* if a non-company is building the road */
if ((IsValidCompanyID(_current_company) && p2 != 0) || (_current_company == OWNER_TOWN && !IsValidTownID(p2))) return CMD_ERROR;
+ if (_current_company != OWNER_TOWN) {
+ const Town *town = CalcClosestTownFromTile(tile, UINT_MAX);
+ p2 = (town != NULL) ? town->index : (TownID)INVALID_TOWN;
+ }
RoadBits pieces = Extract<RoadBits, 0>(p1);
@@ -654,7 +665,7 @@ do_clear:;
if (existing == ROAD_NONE || rtt == ROAD_TILE_CROSSING) {
SetRoadTypes(tile, GetRoadTypes(tile) | RoadTypeToRoadTypes(rt));
SetRoadOwner(tile, rt, _current_company);
- if (_current_company == OWNER_TOWN && rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
+ if (rt == ROADTYPE_ROAD) SetTownIndex(tile, p2);
}
if (rtt != ROAD_TILE_CROSSING) SetRoadBits(tile, existing | pieces, rt);
} break;
@@ -886,7 +897,7 @@ CommandCost CmdBuildRoadDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2
Depot *dep = new Depot(tile);
dep->town_index = ClosestTownFromTile(tile, UINT_MAX)->index;
- MakeRoadDepot(tile, _current_company, dir, rt);
+ MakeRoadDepot(tile, _current_company, dir, rt, dep->town_index);
MarkTileDirtyByTile(tile);
}
return cost.AddCost(_price.build_road_depot);
@@ -1263,6 +1274,18 @@ void DrawRoadDepotSprite(int x, int y, DiagDirection dir, RoadType rt)
}
}
+void InvalidateTownForRoadTile()
+{
+ TileIndex map_size = MapSize();
+
+ for (TileIndex t = 0; t < map_size; t++) {
+ if (IsTileType(t, MP_ROAD) && GetRoadOwner(t, ROADTYPE_ROAD) != OWNER_TOWN) {
+ /* GetRoadOwner(t, ROADTYPE_ROAD) is valid for road tiles even when there is no road */
+ SetTownIndex(t, (TownID)INVALID_TOWN);
+ }
+ }
+}
+
static uint GetSlopeZ_Road(TileIndex tile, uint x, uint y)
{
uint z;