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 InvalidateWindowData(WC_STATION_LIST, this->owner, 0);
00119
00120 DeleteWindowById(WC_STATION_VIEW, index);
00121
00122
00123 RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);
00124
00125
00126 DeleteStationNews(this->index);
00127
00128 for (CargoID c = 0; c < NUM_CARGO; c++) {
00129 this->goods[c].cargo.Truncate(0);
00130 }
00131
00132 CargoPacket::InvalidateAllFrom(this->index);
00133 }
00134
00135
00141 void BaseStation::PostDestructor(size_t index)
00142 {
00143 InvalidateWindowData(WC_SELECT_STATION, 0, 0);
00144 }
00145
00151 RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const
00152 {
00153 RoadStop *rs = this->GetPrimaryRoadStop(v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK);
00154
00155 for (; rs != NULL; rs = rs->next) {
00156
00157 if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
00158
00159 if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;
00160
00161
00162 break;
00163 }
00164
00165 return rs;
00166 }
00167
00172 void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy)
00173 {
00174 if (this->facilities == FACIL_NONE) {
00175 this->xy = facil_xy;
00176 this->random_bits = Random();
00177 }
00178 this->facilities |= new_facility_bit;
00179 this->owner = _current_company;
00180 this->build_date = _date;
00181 }
00182
00188 void Station::MarkTilesDirty(bool cargo_change) const
00189 {
00190 TileIndex tile = this->train_station.tile;
00191 int w, h;
00192
00193 if (tile == INVALID_TILE) return;
00194
00195
00196
00197 if (cargo_change) {
00198
00199
00200
00201 if (this->num_specs == 0) return;
00202 }
00203
00204 for (h = 0; h < train_station.h; h++) {
00205 for (w = 0; w < train_station.w; w++) {
00206 if (this->TileBelongsToRailStation(tile)) {
00207 MarkTileDirtyByTile(tile);
00208 }
00209 tile += TileDiffXY(1, 0);
00210 }
00211 tile += TileDiffXY(-w, 1);
00212 }
00213 }
00214
00215 uint Station::GetPlatformLength(TileIndex tile) const
00216 {
00217 assert(this->TileBelongsToRailStation(tile));
00218
00219 TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00220
00221 TileIndex t = tile;
00222 uint len = 0;
00223 do {
00224 t -= delta;
00225 len++;
00226 } while (IsCompatibleTrainStationTile(t, tile));
00227
00228 t = tile;
00229 do {
00230 t += delta;
00231 len++;
00232 } while (IsCompatibleTrainStationTile(t, tile));
00233
00234 return len - 1;
00235 }
00236
00237 uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
00238 {
00239 TileIndex start_tile = tile;
00240 uint length = 0;
00241 assert(IsRailStationTile(tile));
00242 assert(dir < DIAGDIR_END);
00243
00244 do {
00245 length++;
00246 tile += TileOffsByDiagDir(dir);
00247 } while (IsCompatibleTrainStationTile(tile, start_tile));
00248
00249 return length;
00250 }
00251
00256 uint Station::GetCatchmentRadius() const
00257 {
00258 uint ret = CA_NONE;
00259
00260 if (_settings_game.station.modified_catchment) {
00261 if (this->bus_stops != NULL) ret = max<uint>(ret, CA_BUS);
00262 if (this->truck_stops != NULL) ret = max<uint>(ret, CA_TRUCK);
00263 if (this->train_station.tile != INVALID_TILE) ret = max<uint>(ret, CA_TRAIN);
00264 if (this->dock_tile != INVALID_TILE) ret = max<uint>(ret, CA_DOCK);
00265 if (this->airport.tile != INVALID_TILE) ret = max<uint>(ret, this->airport.GetSpec()->catchment);
00266 } else {
00267 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) {
00268 ret = CA_UNMODIFIED;
00269 }
00270 }
00271
00272 return ret;
00273 }
00274
00279 Rect Station::GetCatchmentRect() const
00280 {
00281 assert(!this->rect.IsEmpty());
00282
00283
00284 int catchment_radius = this->GetCatchmentRadius();
00285
00286 Rect ret = {
00287 max<int>(this->rect.left - catchment_radius, 0),
00288 max<int>(this->rect.top - catchment_radius, 0),
00289 min<int>(this->rect.right + catchment_radius, MapMaxX()),
00290 min<int>(this->rect.bottom + catchment_radius, MapMaxY())
00291 };
00292
00293 return ret;
00294 }
00295
00297 struct RectAndIndustryVector {
00298 Rect rect;
00299 IndustryVector *industries_near;
00300 };
00301
00310 static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
00311 {
00312
00313 if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;
00314
00315 RectAndIndustryVector *riv = (RectAndIndustryVector *)user_data;
00316 Industry *ind = Industry::GetByTile(ind_tile);
00317
00318
00319 if (riv->industries_near->Contains(ind)) return false;
00320
00321
00322 int x = TileX(ind_tile);
00323 int y = TileY(ind_tile);
00324 if (x < riv->rect.left || x > riv->rect.right || y < riv->rect.top || y > riv->rect.bottom) return false;
00325
00326
00327 uint cargo_index;
00328 for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00329 if (ind->accepts_cargo[cargo_index] != CT_INVALID) break;
00330 }
00331 if (cargo_index >= lengthof(ind->accepts_cargo)) return false;
00332
00333 *riv->industries_near->Append() = ind;
00334
00335 return false;
00336 }
00337
00342 void Station::RecomputeIndustriesNear()
00343 {
00344 this->industries_near.Clear();
00345 if (this->rect.IsEmpty()) return;
00346
00347 RectAndIndustryVector riv = {
00348 this->GetCatchmentRect(),
00349 &this->industries_near
00350 };
00351
00352
00353 TileIndex start_tile = this->xy;
00354 uint max_radius = max(
00355 max(DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.bottom))),
00356 max(DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.bottom)))
00357 );
00358
00359 CircularTileSearch(&start_tile, 2 * max_radius + 1, &FindIndustryToDeliver, &riv);
00360 }
00361
00365 void Station::RecomputeIndustriesNearForAll()
00366 {
00367 Station *st;
00368 FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
00369 }
00370
00371
00372
00373
00374
00375 StationRect::StationRect()
00376 {
00377 this->MakeEmpty();
00378 }
00379
00380 void StationRect::MakeEmpty()
00381 {
00382 this->left = this->top = this->right = this->bottom = 0;
00383 }
00384
00394 bool StationRect::PtInExtendedRect(int x, int y, int distance) const
00395 {
00396 return this->left - distance <= x && x <= this->right + distance &&
00397 this->top - distance <= y && y <= this->bottom + distance;
00398 }
00399
00400 bool StationRect::IsEmpty() const
00401 {
00402 return this->left == 0 || this->left > this->right || this->top > this->bottom;
00403 }
00404
00405 CommandCost StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
00406 {
00407 int x = TileX(tile);
00408 int y = TileY(tile);
00409 if (this->IsEmpty()) {
00410
00411 if (mode != ADD_TEST) {
00412 this->left = this->right = x;
00413 this->top = this->bottom = y;
00414 }
00415 } else if (!this->PtInExtendedRect(x, y)) {
00416
00417
00418 Rect new_rect = {min(x, this->left), min(y, this->top), max(x, this->right), max(y, this->bottom)};
00419
00420
00421 int w = new_rect.right - new_rect.left + 1;
00422 int h = new_rect.bottom - new_rect.top + 1;
00423 if (mode != ADD_FORCE && (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread)) {
00424 assert(mode != ADD_TRY);
00425 return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
00426 }
00427
00428
00429 if (mode != ADD_TEST) {
00430
00431 *this = new_rect;
00432 }
00433 } else {
00434 ;
00435 }
00436 return CommandCost();
00437 }
00438
00439 CommandCost StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
00440 {
00441 if (mode == ADD_FORCE || (w <= _settings_game.station.station_spread && h <= _settings_game.station.station_spread)) {
00442
00443 CommandCost ret = this->BeforeAddTile(tile, mode);
00444 if (ret.Succeeded()) ret = this->BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
00445 return ret;
00446 }
00447 return CommandCost();
00448 }
00449
00459 bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
00460 {
00461 TileArea ta(TileXY(left_a, top_a), TileXY(right_a, bottom_a));
00462 TILE_AREA_LOOP(tile, ta) {
00463 if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
00464 }
00465
00466 return false;
00467 }
00468
00469 bool StationRect::AfterRemoveTile(BaseStation *st, TileIndex tile)
00470 {
00471 int x = TileX(tile);
00472 int y = TileY(tile);
00473
00474
00475
00476
00477 for (;;) {
00478
00479 bool left_edge = (x == this->left);
00480 bool right_edge = (x == this->right);
00481 bool top_edge = (y == this->top);
00482 bool bottom_edge = (y == this->bottom);
00483
00484
00485 bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, this->top, x, this->bottom));
00486 bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, this->left, y, this->right, y));
00487 if (!(reduce_x || reduce_y)) break;
00488
00489 if (reduce_x) {
00490
00491 if (left_edge) {
00492
00493 this->left = x = x + 1;
00494 } else {
00495
00496 this->right = x = x - 1;
00497 }
00498 }
00499 if (reduce_y) {
00500
00501 if (top_edge) {
00502
00503 this->top = y = y + 1;
00504 } else {
00505
00506 this->bottom = y = y - 1;
00507 }
00508 }
00509
00510 if (left > right || top > bottom) {
00511
00512 this->MakeEmpty();
00513 return true;
00514 }
00515 }
00516 return false;
00517 }
00518
00519 bool StationRect::AfterRemoveRect(BaseStation *st, TileArea ta)
00520 {
00521 assert(this->PtInExtendedRect(TileX(ta.tile), TileY(ta.tile)));
00522 assert(this->PtInExtendedRect(TileX(ta.tile) + ta.w - 1, TileY(ta.tile) + ta.h - 1));
00523
00524 bool empty = this->AfterRemoveTile(st, ta.tile);
00525 if (ta.w != 1 || ta.h != 1) empty = empty || this->AfterRemoveTile(st, TILE_ADDXY(ta.tile, ta.w - 1, ta.h - 1));
00526 return empty;
00527 }
00528
00529 StationRect& StationRect::operator = (const Rect &src)
00530 {
00531 this->left = src.left;
00532 this->top = src.top;
00533 this->right = src.right;
00534 this->bottom = src.bottom;
00535 return *this;
00536 }