1
0
Fork 0

(svn r16985) -Codechange: simplify the station removal code a bit by giving proper names and using some helper functions

release/1.0
rubidium 2009-07-29 21:41:45 +00:00
parent d51c4b6f6e
commit bfcf32c64e
1 changed files with 24 additions and 40 deletions

View File

@ -1123,51 +1123,35 @@ restart:
/** Remove a single tile from a railroad station. /** Remove a single tile from a railroad station.
* This allows for custom-built station with holes and weird layouts * This allows for custom-built station with holes and weird layouts
* @param tile tile of station piece to remove * @param start tile of station piece to remove
* @param flags operation to perform * @param flags operation to perform
* @param p1 start_tile * @param p1 start_tile
* @param p2 unused * @param p2 unused
* @param text unused
* @return cost of operation or error
*/ */
CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) CommandCost CmdRemoveFromRailroadStation(TileIndex start, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{ {
TileIndex start = p1 == 0 ? tile : p1; TileIndex end = p1 == 0 ? start : p1;
if (start >= MapSize() || end >= MapSize()) return CMD_ERROR;
/* Count of the number of tiles removed */ /* Count of the number of tiles removed */
int quantity = 0; int quantity = 0;
if (tile >= MapSize() || start >= MapSize()) return CMD_ERROR; TileArea ta(start, end);
/* make sure sx,sy are smaller than ex,ey */
int ex = TileX(tile);
int ey = TileY(tile);
int sx = TileX(start);
int sy = TileY(start);
if (ex < sx) Swap(ex, sx);
if (ey < sy) Swap(ey, sy);
tile = TileXY(sx, sy);
int size_x = ex - sx + 1;
int size_y = ey - sy + 1;
SmallVector<Station *, 4> affected_stations; SmallVector<Station *, 4> affected_stations;
/* Do the action for every tile into the area */ /* Do the action for every tile into the area */
TILE_LOOP(tile2, size_x, size_y, tile) { TILE_LOOP(tile, ta.w, ta.h, ta.tile) {
/* Make sure the specified tile is a railroad station */ /* Make sure the specified tile is a railroad station */
if (!IsRailStationTile(tile2)) { if (!IsRailStationTile(tile)) continue;
continue;
}
/* If there is a vehicle on ground, do not allow to remove (flood) the tile */ /* If there is a vehicle on ground, do not allow to remove (flood) the tile */
if (!EnsureNoVehicleOnGround(tile2)) { if (!EnsureNoVehicleOnGround(tile)) continue;
continue;
}
/* Check ownership of station */ /* Check ownership of station */
Station *st = Station::GetByTile(tile2); Station *st = Station::GetByTile(tile);
if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) { if (_current_company != OWNER_WATER && !CheckOwnership(st->owner)) continue;
continue;
}
/* Do not allow removing from stations if non-uniform stations are not enabled /* Do not allow removing from stations if non-uniform stations are not enabled
* The check must be here to give correct error message * The check must be here to give correct error message
@ -1179,13 +1163,13 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
if (flags & DC_EXEC) { if (flags & DC_EXEC) {
/* read variables before the station tile is removed */ /* read variables before the station tile is removed */
uint specindex = GetCustomStationSpecIndex(tile2); uint specindex = GetCustomStationSpecIndex(tile);
Track track = GetRailStationTrack(tile2); Track track = GetRailStationTrack(tile);
Owner owner = GetTileOwner(tile2); Owner owner = GetTileOwner(tile);
Train *v = NULL; Train *v = NULL;
if (HasStationReservation(tile2)) { if (HasStationReservation(tile)) {
v = GetTrainForReservation(tile2, track); v = GetTrainForReservation(tile, track);
if (v != NULL) { if (v != NULL) {
/* Free train reservation. */ /* Free train reservation. */
FreeTrainTrackReservation(v); FreeTrainTrackReservation(v);
@ -1196,10 +1180,10 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
} }
} }
DoClearSquare(tile2); DoClearSquare(tile);
st->rect.AfterRemoveTile(st, tile2); st->rect.AfterRemoveTile(st, tile);
AddTrackToSignalBuffer(tile2, track, owner); AddTrackToSignalBuffer(tile, track, owner);
YapfNotifyTrackLayoutChange(tile2, track); YapfNotifyTrackLayoutChange(tile, track);
DeallocateSpecFromStation(st, specindex); DeallocateSpecFromStation(st, specindex);
@ -1215,6 +1199,9 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
} }
} }
/* If we've not removed any tiles, give an error */
if (quantity == 0) return CMD_ERROR;
for (Station **stp = affected_stations.Begin(); stp != affected_stations.End(); stp++) { for (Station **stp = affected_stations.Begin(); stp != affected_stations.End(); stp++) {
Station *st = *stp; Station *st = *stp;
@ -1236,9 +1223,6 @@ CommandCost CmdRemoveFromRailroadStation(TileIndex tile, DoCommandFlag flags, ui
st->RecomputeIndustriesNear(); st->RecomputeIndustriesNear();
} }
/* If we've not removed any tiles, give an error */
if (quantity == 0) return CMD_ERROR;
return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_rail_station * quantity); return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_rail_station * quantity);
} }