00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "company_base.h"
00015 #include "roadveh.h"
00016 #include "viewport_func.h"
00017 #include "window_func.h"
00018 #include "date_func.h"
00019 #include "command_func.h"
00020 #include "news_func.h"
00021 #include "aircraft.h"
00022 #include "vehiclelist.h"
00023 #include "core/pool_func.hpp"
00024 #include "station_base.h"
00025 #include "roadstop_base.h"
00026 #include "industry.h"
00027 #include "core/random_func.hpp"
00028
00029 #include "table/strings.h"
00030
00032 StationPool _station_pool("Station");
00033 INSTANTIATE_POOL_METHODS(Station)
00034
00035 BaseStation::~BaseStation()
00036 {
00037 free(this->name);
00038 free(this->speclist);
00039
00040 if (CleaningPool()) return;
00041
00042 Owner owner = this->owner;
00043 if (!Company::IsValidID(owner)) owner = _local_company;
00044 if (!Company::IsValidID(owner)) return;
00045 DeleteWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, owner, this->index).Pack());
00046 DeleteWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, owner, this->index).Pack());
00047 DeleteWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, owner, this->index).Pack());
00048 DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, owner, this->index).Pack());
00049
00050 this->sign.MarkDirty();
00051 }
00052
00053 Station::Station(TileIndex tile) :
00054 SpecializedStation<Station, false>(tile),
00055 bus_station(INVALID_TILE, 0, 0),
00056 truck_station(INVALID_TILE, 0, 0),
00057 dock_tile(INVALID_TILE),
00058 indtype(IT_INVALID),
00059 time_since_load(255),
00060 time_since_unload(255),
00061 last_vehicle_type(VEH_INVALID)
00062 {
00063
00064
00065
00066 for (CargoID i = 0; i < NUM_CARGO; ++i) {
00067 this->goods[i].cargo.AssignTo(this, i);
00068 }
00069 }
00070
00078 Station::~Station()
00079 {
00080 if (CleaningPool()) {
00081 for (CargoID c = 0; c < NUM_CARGO; c++) {
00082 this->goods[c].cargo.OnCleanPool();
00083 }
00084 return;
00085 }
00086
00087 while (!this->loading_vehicles.empty()) {
00088 this->loading_vehicles.front()->LeaveStation();
00089 }
00090
00091 Aircraft *a;
00092 FOR_ALL_AIRCRAFT(a) {
00093 if (!a->IsNormalAircraft()) continue;
00094 if (a->targetairport == this->index) a->targetairport = INVALID_STATION;
00095 }
00096
00097 Station *st;
00098 FOR_ALL_STATIONS(st) {
00099 for (CargoID c = 0; c < NUM_CARGO; ++c) {
00100 GoodsEntry &ge = st->goods[c];
00101 ge.link_stats.erase(this->index);
00102 DeleteStaleFlows(st->index, c, this->index);
00103 ge.cargo.RerouteStalePackets(this->index);
00104 }
00105 }
00106
00107 Vehicle *v;
00108 FOR_ALL_VEHICLES(v) {
00109
00110 if (v->last_station_visited == this->index) {
00111 v->last_station_visited = INVALID_STATION;
00112 }
00113 if (v->last_loading_station == this->index) {
00114 v->last_loading_station = INVALID_STATION;
00115 }
00116 }
00117
00118
00119 delete this->airport.psa;
00120
00121 if (this->owner == OWNER_NONE) {
00122
00123 InvalidateWindowClassesData(WC_STATION_LIST, 0);
00124 } else {
00125 InvalidateWindowData(WC_STATION_LIST, this->owner, 0);
00126 }
00127
00128 DeleteWindowById(WC_STATION_VIEW, index);
00129
00130
00131 RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);
00132
00133
00134 DeleteStationNews(this->index);
00135
00136 for (CargoID c = 0; c < NUM_CARGO; c++) {
00137 this->goods[c].cargo.Truncate(0);
00138 }
00139
00140 CargoPacket::InvalidateAllFrom(this->index);
00141 }
00142
00143
00149 void BaseStation::PostDestructor(size_t index)
00150 {
00151 InvalidateWindowData(WC_SELECT_STATION, 0, 0);
00152 }
00153
00159 RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const
00160 {
00161 RoadStop *rs = this->GetPrimaryRoadStop(v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK);
00162
00163 for (; rs != NULL; rs = rs->next) {
00164
00165 if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
00166
00167 if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;
00168
00169
00170 break;
00171 }
00172
00173 return rs;
00174 }
00175
00180 void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy)
00181 {
00182 if (this->facilities == FACIL_NONE) {
00183 this->xy = facil_xy;
00184 this->random_bits = Random();
00185 }
00186 this->facilities |= new_facility_bit;
00187 this->owner = _current_company;
00188 this->build_date = _date;
00189 }
00190
00196 void Station::MarkTilesDirty(bool cargo_change) const
00197 {
00198 TileIndex tile = this->train_station.tile;
00199 int w, h;
00200
00201 if (tile == INVALID_TILE) return;
00202
00203
00204
00205 if (cargo_change) {
00206
00207
00208
00209 if (this->num_specs == 0) return;
00210 }
00211
00212 for (h = 0; h < train_station.h; h++) {
00213 for (w = 0; w < train_station.w; w++) {
00214 if (this->TileBelongsToRailStation(tile)) {
00215 MarkTileDirtyByTile(tile);
00216 }
00217 tile += TileDiffXY(1, 0);
00218 }
00219 tile += TileDiffXY(-w, 1);
00220 }
00221 }
00222
00223 uint Station::GetPlatformLength(TileIndex tile) const
00224 {
00225 assert(this->TileBelongsToRailStation(tile));
00226
00227 TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00228
00229 TileIndex t = tile;
00230 uint len = 0;
00231 do {
00232 t -= delta;
00233 len++;
00234 } while (IsCompatibleTrainStationTile(t, tile));
00235
00236 t = tile;
00237 do {
00238 t += delta;
00239 len++;
00240 } while (IsCompatibleTrainStationTile(t, tile));
00241
00242 return len - 1;
00243 }
00244
00245 uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
00246 {
00247 TileIndex start_tile = tile;
00248 uint length = 0;
00249 assert(IsRailStationTile(tile));
00250 assert(dir < DIAGDIR_END);
00251
00252 do {
00253 length++;
00254 tile += TileOffsByDiagDir(dir);
00255 } while (IsCompatibleTrainStationTile(tile, start_tile));
00256
00257 return length;
00258 }
00259
00264 uint Station::GetCatchmentRadius() const
00265 {
00266 uint ret = CA_NONE;
00267
00268 if (_settings_game.station.modified_catchment) {
00269 if (this->bus_stops != NULL) ret = max<uint>(ret, CA_BUS);
00270 if (this->truck_stops != NULL) ret = max<uint>(ret, CA_TRUCK);
00271 if (this->train_station.tile != INVALID_TILE) ret = max<uint>(ret, CA_TRAIN);
00272 if (this->dock_tile != INVALID_TILE) ret = max<uint>(ret, CA_DOCK);
00273 if (this->airport.tile != INVALID_TILE) ret = max<uint>(ret, this->airport.GetSpec()->catchment);
00274 } else {
00275 if (this->bus_stops != NULL || this->truck_stops != NULL || this->train_station.tile != INVALID_TILE || this->dock_tile != INVALID_TILE || this->airport.tile != INVALID_TILE) {
00276 ret = CA_UNMODIFIED;
00277 }
00278 }
00279
00280 return ret;
00281 }
00282
00287 Rect Station::GetCatchmentRect() const
00288 {
00289 assert(!this->rect.IsEmpty());
00290
00291
00292 int catchment_radius = this->GetCatchmentRadius();
00293
00294 Rect ret = {
00295 max<int>(this->rect.left - catchment_radius, 0),
00296 max<int>(this->rect.top - catchment_radius, 0),
00297 min<int>(this->rect.right + catchment_radius, MapMaxX()),
00298 min<int>(this->rect.bottom + catchment_radius, MapMaxY())
00299 };
00300
00301 return ret;
00302 }
00303
00305 struct RectAndIndustryVector {
00306 Rect rect;
00307 IndustryVector *industries_near;
00308 };
00309
00318 static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
00319 {
00320
00321 if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;
00322
00323 RectAndIndustryVector *riv = (RectAndIndustryVector *)user_data;
00324 Industry *ind = Industry::GetByTile(ind_tile);
00325
00326
00327 if (riv->industries_near->Contains(ind)) return false;
00328
00329
00330 int x = TileX(ind_tile);
00331 int y = TileY(ind_tile);
00332 if (x < riv->rect.left || x > riv->rect.right || y < riv->rect.top || y > riv->rect.bottom) return false;
00333
00334
00335 uint cargo_index;
00336 for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00337 if (ind->accepts_cargo[cargo_index] != CT_INVALID) break;
00338 }
00339 if (cargo_index >= lengthof(ind->accepts_cargo)) return false;
00340
00341 *riv->industries_near->Append() = ind;
00342
00343 return false;
00344 }
00345
00350 void Station::RecomputeIndustriesNear()
00351 {
00352 this->industries_near.Clear();
00353 if (this->rect.IsEmpty()) return;
00354
00355 RectAndIndustryVector riv = {
00356 this->GetCatchmentRect(),
00357 &this->industries_near
00358 };
00359
00360
00361 TileIndex start_tile = this->xy;
00362 uint max_radius = max(
00363 max(DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.bottom))),
00364 max(DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.bottom)))
00365 );
00366
00367 CircularTileSearch(&start_tile, 2 * max_radius + 1, &FindIndustryToDeliver, &riv);
00368 }
00369
00373 void Station::RecomputeIndustriesNearForAll()
00374 {
00375 Station *st;
00376 FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
00377 }
00378
00379
00380
00381
00382
00383 StationRect::StationRect()
00384 {
00385 this->MakeEmpty();
00386 }
00387
00388 void StationRect::MakeEmpty()
00389 {
00390 this->left = this->top = this->right = this->bottom = 0;
00391 }
00392
00402 bool StationRect::PtInExtendedRect(int x, int y, int distance) const
00403 {
00404 return this->left - distance <= x && x <= this->right + distance &&
00405 this->top - distance <= y && y <= this->bottom + distance;
00406 }
00407
00408 bool StationRect::IsEmpty() const
00409 {
00410 return this->left == 0 || this->left > this->right || this->top > this->bottom;
00411 }
00412
00413 CommandCost StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
00414 {
00415 int x = TileX(tile);
00416 int y = TileY(tile);
00417 if (this->IsEmpty()) {
00418
00419 if (mode != ADD_TEST) {
00420 this->left = this->right = x;
00421 this->top = this->bottom = y;
00422 }
00423 } else if (!this->PtInExtendedRect(x, y)) {
00424
00425
00426 Rect new_rect = {min(x, this->left), min(y, this->top), max(x, this->right), max(y, this->bottom)};
00427
00428
00429 int w = new_rect.right - new_rect.left + 1;
00430 int h = new_rect.bottom - new_rect.top + 1;
00431 if (mode != ADD_FORCE && (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread)) {
00432 assert(mode != ADD_TRY);
00433 return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
00434 }
00435
00436
00437 if (mode != ADD_TEST) {
00438
00439 *this = new_rect;
00440 }
00441 } else {
00442 ;
00443 }
00444 return CommandCost();
00445 }
00446
00447 CommandCost StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
00448 {
00449 if (mode == ADD_FORCE || (w <= _settings_game.station.station_spread && h <= _settings_game.station.station_spread)) {
00450
00451 CommandCost ret = this->BeforeAddTile(tile, mode);
00452 if (ret.Succeeded()) ret = this->BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
00453 return ret;
00454 }
00455 return CommandCost();
00456 }
00457
00467 bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
00468 {
00469 TileArea ta(TileXY(left_a, top_a), TileXY(right_a, bottom_a));
00470 TILE_AREA_LOOP(tile, ta) {
00471 if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
00472 }
00473
00474 return false;
00475 }
00476
00477 bool StationRect::AfterRemoveTile(BaseStation *st, TileIndex tile)
00478 {
00479 int x = TileX(tile);
00480 int y = TileY(tile);
00481
00482
00483
00484
00485 for (;;) {
00486
00487 bool left_edge = (x == this->left);
00488 bool right_edge = (x == this->right);
00489 bool top_edge = (y == this->top);
00490 bool bottom_edge = (y == this->bottom);
00491
00492
00493 bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, this->top, x, this->bottom));
00494 bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, this->left, y, this->right, y));
00495 if (!(reduce_x || reduce_y)) break;
00496
00497 if (reduce_x) {
00498
00499 if (left_edge) {
00500
00501 this->left = x = x + 1;
00502 } else {
00503
00504 this->right = x = x - 1;
00505 }
00506 }
00507 if (reduce_y) {
00508
00509 if (top_edge) {
00510
00511 this->top = y = y + 1;
00512 } else {
00513
00514 this->bottom = y = y - 1;
00515 }
00516 }
00517
00518 if (left > right || top > bottom) {
00519
00520 this->MakeEmpty();
00521 return true;
00522 }
00523 }
00524 return false;
00525 }
00526
00527 bool StationRect::AfterRemoveRect(BaseStation *st, TileArea ta)
00528 {
00529 assert(this->PtInExtendedRect(TileX(ta.tile), TileY(ta.tile)));
00530 assert(this->PtInExtendedRect(TileX(ta.tile) + ta.w - 1, TileY(ta.tile) + ta.h - 1));
00531
00532 bool empty = this->AfterRemoveTile(st, ta.tile);
00533 if (ta.w != 1 || ta.h != 1) empty = empty || this->AfterRemoveTile(st, TILE_ADDXY(ta.tile, ta.w - 1, ta.h - 1));
00534 return empty;
00535 }
00536
00537 StationRect& StationRect::operator = (const Rect &src)
00538 {
00539 this->left = src.left;
00540 this->top = src.top;
00541 this->right = src.right;
00542 this->bottom = src.bottom;
00543 return *this;
00544 }
00545
00551 Money AirportMaintenanceCost(Owner owner)
00552 {
00553 Money total_cost = 0;
00554
00555 const Station *st;
00556 FOR_ALL_STATIONS(st) {
00557 if (st->owner == owner && (st->facilities & FACIL_AIRPORT)) {
00558 total_cost += _price[PR_INFRASTRUCTURE_AIRPORT] * st->airport.GetSpec()->maintenance_cost;
00559 }
00560 }
00561
00562 return total_cost >> 3;
00563 }