Translations update
[openttd/fttd.git] / src / station.cpp
blob4dcdd44aeae5f7f227da0587891ac8f1d29a0ec2
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file station.cpp Implementation of the station base class. */
12 #include "stdafx.h"
14 #include <functional>
16 #include "company_func.h"
17 #include "company_base.h"
18 #include "roadveh.h"
19 #include "viewport_func.h"
20 #include "date_func.h"
21 #include "command_func.h"
22 #include "news_func.h"
23 #include "aircraft.h"
24 #include "vehiclelist.h"
25 #include "core/pool_func.hpp"
26 #include "station_base.h"
27 #include "roadstop_base.h"
28 #include "industry.h"
29 #include "core/random_func.hpp"
30 #include "linkgraph/linkgraph.h"
31 #include "linkgraph/linkgraphschedule.h"
32 #include "map/road.h"
34 #include "table/strings.h"
36 /** The pool of stations. */
37 template<> BaseStation::Pool BaseStation::PoolItem::pool ("Station");
38 INSTANTIATE_POOL_METHODS(Station)
40 BaseStation::~BaseStation()
42 free(this->name);
43 free(this->speclist);
45 if (CleaningPool()) return;
47 DeleteWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, this->owner, this->index).Pack());
48 DeleteWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, this->owner, this->index).Pack());
49 DeleteWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, this->owner, this->index).Pack());
50 DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, this->owner, this->index).Pack());
52 this->sign.MarkDirty();
55 Station::Station(TileIndex tile) :
56 SpecializedStation<Station, false>(tile),
57 bus_station(INVALID_TILE, 0, 0),
58 truck_station(INVALID_TILE, 0, 0),
59 dock_area(INVALID_TILE, 0, 0),
60 indtype(IT_INVALID),
61 time_since_load(255),
62 time_since_unload(255),
63 last_vehicle_type(VEH_INVALID)
65 /* this->random_bits is set in Station::AddFacility() */
68 /**
69 * Clean up a station by clearing vehicle orders, invalidating windows and
70 * removing link stats.
71 * Aircraft-Hangar orders need special treatment here, as the hangars are
72 * actually part of a station (tiletype is STATION), but the order type
73 * is OT_GOTO_DEPOT.
75 Station::~Station()
77 if (CleaningPool()) {
78 for (CargoID c = 0; c < NUM_CARGO; c++) {
79 this->goods[c].cargo.OnCleanPool();
81 return;
84 while (!this->loading_vehicles.empty()) {
85 this->loading_vehicles.front()->LeaveStation();
88 Aircraft *a;
89 FOR_ALL_AIRCRAFT(a) {
90 if (!a->IsNormalAircraft()) continue;
91 if (a->targetairport == this->index) a->targetairport = INVALID_STATION;
94 for (CargoID c = 0; c < NUM_CARGO; ++c) {
95 LinkGraph *lg = LinkGraph::GetIfValid(this->goods[c].link_graph);
96 if (lg == NULL) continue;
98 for (NodeID node = 0; node < lg->Size(); ++node) {
99 Station *st = Station::Get((*lg)[node]->Station());
100 st->goods[c].flows.erase(this->index);
101 if ((*lg)[node][this->goods[c].node].LastUpdate() != INVALID_DATE) {
102 st->goods[c].flows.DeleteFlows(this->index);
103 RerouteCargo (st, c, this->index);
106 lg->RemoveNode(this->goods[c].node);
107 if (lg->Size() == 0) {
108 LinkGraphSchedule::instance.Unqueue(lg);
109 delete lg;
113 Vehicle *v;
114 FOR_ALL_VEHICLES(v) {
115 /* Forget about this station if this station is removed */
116 if (v->last_station_visited == this->index) {
117 v->last_station_visited = INVALID_STATION;
119 if (v->last_loading_station == this->index) {
120 v->last_loading_station = INVALID_STATION;
124 /* Clear the persistent storage. */
125 delete this->airport.psa;
127 if (this->owner == OWNER_NONE) {
128 /* Invalidate all in case of oil rigs. */
129 InvalidateWindowClassesData(WC_STATION_LIST, 0);
130 } else {
131 InvalidateWindowData(WC_STATION_LIST, this->owner, 0);
134 DeleteWindowById(WC_STATION_VIEW, index);
136 /* Now delete all orders that go to the station */
137 RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);
139 /* Remove all news items */
140 DeleteStationNews(this->index);
142 for (CargoID c = 0; c < NUM_CARGO; c++) {
143 this->goods[c].cargo.Truncate();
146 CargoPacket::InvalidateAllFrom(this->index);
151 * Invalidating of the JoinStation window has to be done
152 * after removing item from the pool.
153 * @param index index of deleted item
155 void BaseStation::PostDestructor(size_t index)
157 InvalidateWindowData(WC_SELECT_STATION, 0, 0);
161 * Get the primary road stop (the first road stop) that the given vehicle can load/unload.
162 * @param v the vehicle to get the first road stop for
163 * @return the first roadstop that this vehicle can load at
165 RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const
167 RoadStop *rs = this->GetPrimaryRoadStop(v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK);
169 for (; rs != NULL; rs = rs->next) {
170 /* The vehicle cannot go to this roadstop (different roadtype) */
171 if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
172 /* The vehicle is articulated and can therefore not go to a standard road stop. */
173 if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;
175 /* The vehicle can actually go to this road stop. So, return it! */
176 break;
179 return rs;
183 * Called when new facility is built on the station. If it is the first facility
184 * it initializes also 'xy' and 'random_bits' members
186 void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy)
188 if (this->facilities == FACIL_NONE) {
189 this->xy = facil_xy;
190 this->random_bits = Random();
192 this->facilities |= new_facility_bit;
193 this->owner = _current_company;
194 this->build_date = _date;
198 * Marks the tiles of the station as dirty.
200 * @ingroup dirty
202 void Station::MarkTilesDirty(bool cargo_change) const
204 TileIndex tile = this->train_station.tile;
205 int w, h;
207 if (tile == INVALID_TILE) return;
209 /* cargo_change is set if we're refreshing the tiles due to cargo moving
210 * around. */
211 if (cargo_change) {
212 /* Don't waste time updating if there are no custom station graphics
213 * that might change. Even if there are custom graphics, they might
214 * not change. Unfortunately we have no way of telling. */
215 if (this->num_specs == 0) return;
218 for (h = 0; h < train_station.h; h++) {
219 for (w = 0; w < train_station.w; w++) {
220 if (this->TileBelongsToRailStation(tile)) {
221 MarkTileDirtyByTile(tile);
223 tile += TileDiffXY(1, 0);
225 tile += TileDiffXY(-w, 1);
229 /* virtual */ uint Station::GetPlatformLength(TileIndex tile) const
231 assert(this->TileBelongsToRailStation(tile));
233 TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
235 TileIndex t = tile;
236 uint len = 0;
237 do {
238 t -= delta;
239 len++;
240 } while (IsCompatibleTrainStationTile(t, tile));
242 t = tile;
243 do {
244 t += delta;
245 len++;
246 } while (IsCompatibleTrainStationTile(t, tile));
248 return len - 1;
251 /* virtual */ uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
253 TileIndex start_tile = tile;
254 uint length = 0;
255 assert(IsRailStationTile(tile));
256 assert(dir < DIAGDIR_END);
258 do {
259 length++;
260 tile += TileOffsByDiagDir(dir);
261 } while (IsCompatibleTrainStationTile(tile, start_tile));
263 return length;
267 * Determines the catchment radius of the station
268 * @return The radius
270 uint Station::GetCatchmentRadius() const
272 uint ret = CA_NONE;
274 if (_settings_game.station.modified_catchment) {
275 if (this->bus_stops != NULL) ret = max<uint>(ret, CA_BUS);
276 if (this->truck_stops != NULL) ret = max<uint>(ret, CA_TRUCK);
277 if (this->train_station.tile != INVALID_TILE) ret = max<uint>(ret, CA_TRAIN);
278 if (this->docks != NULL) ret = max<uint>(ret, CA_DOCK);
279 if (this->airport.tile != INVALID_TILE) ret = max<uint>(ret, this->airport.GetSpec()->catchment);
280 } else {
281 if (this->bus_stops != NULL || this->truck_stops != NULL || this->train_station.tile != INVALID_TILE || this->docks != NULL || this->airport.tile != INVALID_TILE) {
282 ret = CA_UNMODIFIED;
286 return ret;
290 * Determines the catchment area of this station
291 * @return clamped catchment rectangle
293 TileArea Station::GetCatchmentArea() const
295 assert(!this->rect.empty());
297 TileArea catchment (this->rect);
298 catchment.expand (this->GetCatchmentRadius());
299 return catchment;
303 * Recomputes Station::industries_near, list of industries possibly
304 * accepting cargo in station's catchment radius
306 void Station::RecomputeIndustriesNear()
308 this->industries_near.Clear();
309 if (this->rect.empty()) return;
311 TileArea catchment = this->GetCatchmentArea();
313 /* Compute maximum extent of acceptance rectangle wrt. station sign */
314 CircularTileIterator iter (this->xy, 2 * catchment.get_radius_max (this->xy) + 1);
315 for (TileIndex tile = iter; tile != INVALID_TILE; tile = ++iter) {
316 /* Only process industry tiles within the catchment area */
317 if (!IsIndustryTile(tile)) continue;
318 if (!catchment.Contains(tile)) continue;
320 Industry *ind = Industry::GetByTile (tile);
322 /* Don't check further if this industry is already in the list */
323 if (this->industries_near.Contains(ind)) continue;
325 /* Include only industries that can accept cargo */
326 for (uint cid = 0; cid < lengthof(ind->accepts_cargo); cid++) {
327 if (ind->accepts_cargo[cid] != CT_INVALID) {
328 *this->industries_near.Append() = ind;
329 break;
336 * Recomputes Station::industries_near for all stations
338 /* static */ void Station::RecomputeIndustriesNearForAll()
340 Station *st;
341 FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
344 /** Test if adding an area would exceed the maximum station spread. */
345 bool BaseStation::TestAddRect (const TileArea &ta)
347 assert (ta.w <= _settings_game.station.station_spread);
348 assert (ta.h <= _settings_game.station.station_spread);
350 if (this->rect.empty()) return true;
352 TileArea new_rect (this->rect);
353 new_rect.Add (ta);
355 /* special case when new area is already contained in old area */
356 if (new_rect.tile == this->rect.tile && new_rect.w == this->rect.w && new_rect.h == this->rect.h) {
357 return true;
360 /* check new rect dimensions against preset max */
361 return new_rect.w <= _settings_game.station.station_spread &&
362 new_rect.h <= _settings_game.station.station_spread;
365 /** Update station area after removing a rectangle. */
366 void BaseStation::AfterRemoveRect (const TileArea &ta)
368 this->rect.shrink_span (std::bind1st (std::mem_fun (&BaseStation::TileBelongsToStation), this), ta);
372 /** The pool of docks. */
373 template<> Dock::Pool Dock::PoolItem::pool ("Dock");
374 INSTANTIATE_POOL_METHODS(Dock)
377 * Calculates the maintenance cost of all airports of a company.
378 * @param owner Company.
379 * @return Total cost.
381 Money AirportMaintenanceCost(Owner owner)
383 Money total_cost = 0;
385 const Station *st;
386 FOR_ALL_STATIONS(st) {
387 if (st->owner == owner && (st->facilities & FACIL_AIRPORT)) {
388 total_cost += _price[PR_INFRASTRUCTURE_AIRPORT] * st->airport.GetSpec()->maintenance_cost;
391 /* 3 bits fraction for the maintenance cost factor. */
392 return total_cost >> 3;