(svn r18377) -Codechange: add 'cache' of the tile area of truck and bus stops.

This commit is contained in:
rubidium 2009-12-02 16:20:44 +00:00
parent 1c65150d6d
commit 81062163a2
5 changed files with 75 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -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),
@ -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()
{

View File

@ -67,7 +67,10 @@ public:
}
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

View File

@ -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:
@ -1212,7 +1212,7 @@ restart:
}
}
} else {
ta.tile = INVALID_TILE;
ta.Clear();
}
st->train_station = ta;
@ -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;
@ -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);
@ -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]);

View File

@ -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 */