Skip to content

Commit

Permalink
(svn r18377) -Codechange: add 'cache' of the tile area of truck and b…
Browse files Browse the repository at this point in the history
…us stops.
  • Loading branch information
rubidium42 committed Dec 2, 2009
1 parent 1c65150 commit 8106216
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/saveload/station_sl.cpp
Expand Up @@ -98,6 +98,12 @@ void AfterLoadStations()
st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx, NULL);
}

if (Station::IsExpected(st)) {
Station *sta = Station::From(st);
for (const RoadStop *rs = sta->bus_stops; rs != NULL; rs = rs->next) sta->bus_station.Add(rs->xy);
for (const RoadStop *rs = sta->truck_stops; rs != NULL; rs = rs->next) sta->truck_station.Add(rs->xy);
}

StationUpdateAnimTriggers(st);
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/station.cpp
Expand Up @@ -38,6 +38,8 @@ BaseStation::~BaseStation()

Station::Station(TileIndex tile) :
SpecializedStation<Station, false>(tile),
bus_station(INVALID_TILE, 0, 0),
truck_station(INVALID_TILE, 0, 0),
airport_tile(INVALID_TILE),
dock_tile(INVALID_TILE),
indtype(IT_INVALID),
Expand Down Expand Up @@ -511,6 +513,33 @@ TileArea::TileArea(TileIndex start, TileIndex end)
this->h = ey - sy + 1;
}

void TileArea::Add(TileIndex to_add)
{
if (this->tile == INVALID_TILE) {
this->tile = to_add;
this->w = 1;
this->h = 1;
return;
}

uint sx = TileX(this->tile);
uint sy = TileY(this->tile);
uint ex = sx + this->w - 1;
uint ey = sy + this->h - 1;

uint ax = TileX(to_add);
uint ay = TileY(to_add);

sx = min(ax, sx);
sy = min(ay, sy);
ex = max(ax, ex);
ey = max(ay, ey);

this->tile = TileXY(sx, sy);
this->w = ex - sx + 1;
this->h = ey - sy + 1;
}


void InitializeStations()
{
Expand Down
3 changes: 3 additions & 0 deletions src/station_base.h
Expand Up @@ -67,7 +67,10 @@ struct Station : SpecializedStation<Station, false> {
}

RoadStop *bus_stops; ///< All the road stops
TileArea bus_station; ///< Tile area the bus 'station' part covers
RoadStop *truck_stops; ///< All the truck stops
TileArea truck_station; ///< Tile area the truck 'station' part covers

TileIndex airport_tile; ///< The location of the airport
TileIndex dock_tile; ///< The location of the dock

Expand Down
29 changes: 21 additions & 8 deletions src/station_cmd.cpp
Expand Up @@ -397,12 +397,12 @@ void Station::GetTileArea(TileArea *ta, StationType type) const
return;

case STATION_TRUCK:
ta->tile = this->truck_stops != NULL ? this->truck_stops->xy : INVALID_TILE;
break;
*ta = this->truck_station;
return;

case STATION_BUS:
ta->tile = this->bus_stops != NULL ? this->bus_stops->xy : INVALID_TILE;
break;
*ta = this->bus_station;
return;

case STATION_DOCK:
case STATION_OILRIG:
Expand Down Expand Up @@ -1212,7 +1212,7 @@ static void MakeRailStationAreaSmaller(BaseStation *st)
}
}
} else {
ta.tile = INVALID_TILE;
ta.Clear();
}

st->train_station = ta;
Expand Down Expand Up @@ -1427,9 +1427,7 @@ CommandCost RemoveRailStation(T *st, DoCommandFlag flags)
if (flags & DC_EXEC) {
st->rect.AfterRemoveRect(st, st->train_station.tile, st->train_station.w, st->train_station.h);

st->train_station.tile = INVALID_TILE;
st->train_station.w = 0;
st->train_station.h = 0;
st->train_station.Clear();

st->facilities &= ~FACIL_TRAIN;

Expand Down Expand Up @@ -1626,6 +1624,12 @@ CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
RoadStop **currstop = FindRoadStopSpot(type, st);
*currstop = road_stop;

if (type) {
st->truck_station.Add(tile);
} else {
st->bus_station.Add(tile);
}

/* initialize an empty station */
st->AddFacility((type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP, tile);

Expand Down Expand Up @@ -1735,6 +1739,15 @@ static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags)
st->UpdateVirtCoord();
st->RecomputeIndustriesNear();
DeleteStationIfEmpty(st);

/* Update the tile area of the truck/bus stop */
if (is_truck) {
st->truck_station.Clear();
for (const RoadStop *rs = st->truck_stops; rs != NULL; rs = rs->next) st->truck_station.Add(rs->xy);
} else {
st->bus_station.Clear();
for (const RoadStop *rs = st->bus_stops; rs != NULL; rs = rs->next) st->bus_station.Add(rs->xy);
}
}

return CommandCost(EXPENSES_CONSTRUCTION, _price[is_truck ? PR_CLEAR_STATION_TRUCK : PR_CLEAR_STATION_BUS]);
Expand Down
16 changes: 16 additions & 0 deletions src/station_type.h
Expand Up @@ -114,6 +114,22 @@ struct TileArea {
TileIndex tile; ///< The base tile of the area
uint8 w; ///< The width of the area
uint8 h; ///< The height of the area

/**
* Add a single tile to a tile area; enlarge if needed.
* @param to_add The tile to add
*/
void Add(TileIndex to_add);

/**
* Clears the 'tile area', i.e. make the tile invalid.
*/
void Clear()
{
this->tile = INVALID_TILE;
this->w = 0;
this->h = 0;
}
};

/** List of stations */
Expand Down

0 comments on commit 8106216

Please sign in to comment.