00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "roadveh.h"
00015 #include "functions.h"
00016 #include "window_func.h"
00017 #include "date_func.h"
00018 #include "command_func.h"
00019 #include "news_func.h"
00020 #include "aircraft.h"
00021 #include "vehicle_gui.h"
00022 #include "core/pool_func.hpp"
00023 #include "station_base.h"
00024 #include "roadstop_base.h"
00025 #include "industry.h"
00026 #include "core/random_func.hpp"
00027
00028 #include "table/strings.h"
00029
00030 StationPool _station_pool("Station");
00031 INSTANTIATE_POOL_METHODS(Station)
00032
00033 BaseStation::~BaseStation()
00034 {
00035 free(this->name);
00036 free(this->speclist);
00037 }
00038
00039 Station::Station(TileIndex tile) :
00040 SpecializedStation<Station, false>(tile),
00041 bus_station(INVALID_TILE, 0, 0),
00042 truck_station(INVALID_TILE, 0, 0),
00043 airport_tile(INVALID_TILE),
00044 dock_tile(INVALID_TILE),
00045 indtype(IT_INVALID),
00046 time_since_load(255),
00047 time_since_unload(255),
00048 last_vehicle_type(VEH_INVALID)
00049 {
00050
00051 }
00052
00059 Station::~Station()
00060 {
00061 if (CleaningPool()) return;
00062
00063 while (!this->loading_vehicles.empty()) {
00064 this->loading_vehicles.front()->LeaveStation();
00065 }
00066
00067 Aircraft *a;
00068 FOR_ALL_AIRCRAFT(a) {
00069 if (!a->IsNormalAircraft()) continue;
00070 if (a->targetairport == this->index) a->targetairport = INVALID_STATION;
00071 }
00072
00073 Vehicle *v;
00074 FOR_ALL_VEHICLES(v) {
00075
00076 if (v->last_station_visited == this->index) {
00077 v->last_station_visited = INVALID_STATION;
00078 }
00079 }
00080
00081 this->sign.MarkDirty();
00082 InvalidateWindowData(WC_STATION_LIST, this->owner, 0);
00083
00084 DeleteWindowById(WC_STATION_VIEW, index);
00085 WindowNumber wno = (this->index << 16) | VLW_STATION_LIST | this->owner;
00086 DeleteWindowById(WC_TRAINS_LIST, wno | (VEH_TRAIN << 11));
00087 DeleteWindowById(WC_ROADVEH_LIST, wno | (VEH_ROAD << 11));
00088 DeleteWindowById(WC_SHIPS_LIST, wno | (VEH_SHIP << 11));
00089 DeleteWindowById(WC_AIRCRAFT_LIST, wno | (VEH_AIRCRAFT << 11));
00090
00091
00092 RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);
00093
00094
00095 DeleteStationNews(this->index);
00096
00097 for (CargoID c = 0; c < NUM_CARGO; c++) {
00098 this->goods[c].cargo.Truncate(0);
00099 }
00100
00101 CargoPacket::InvalidateAllFrom(this->index);
00102 }
00103
00104
00110 void BaseStation::PostDestructor(size_t index)
00111 {
00112 InvalidateWindowData(WC_SELECT_STATION, 0, 0);
00113 }
00114
00120 RoadStop *Station::GetPrimaryRoadStop(const RoadVehicle *v) const
00121 {
00122 RoadStop *rs = this->GetPrimaryRoadStop(v->IsBus() ? ROADSTOP_BUS : ROADSTOP_TRUCK);
00123
00124 for (; rs != NULL; rs = rs->next) {
00125
00126 if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
00127
00128 if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;
00129
00130
00131 break;
00132 }
00133
00134 return rs;
00135 }
00136
00139 void Station::AddFacility(StationFacility new_facility_bit, TileIndex facil_xy)
00140 {
00141 if (this->facilities == FACIL_NONE) {
00142 this->xy = facil_xy;
00143 this->random_bits = Random();
00144 }
00145 this->facilities |= new_facility_bit;
00146 this->owner = _current_company;
00147 this->build_date = _date;
00148 }
00149
00150 void Station::MarkTilesDirty(bool cargo_change) const
00151 {
00152 TileIndex tile = this->train_station.tile;
00153 int w, h;
00154
00155 if (tile == INVALID_TILE) return;
00156
00157
00158
00159 if (cargo_change) {
00160
00161
00162
00163 if (this->num_specs == 0) return;
00164 }
00165
00166 for (h = 0; h < train_station.h; h++) {
00167 for (w = 0; w < train_station.w; w++) {
00168 if (this->TileBelongsToRailStation(tile)) {
00169 MarkTileDirtyByTile(tile);
00170 }
00171 tile += TileDiffXY(1, 0);
00172 }
00173 tile += TileDiffXY(-w, 1);
00174 }
00175 }
00176
00177 uint Station::GetPlatformLength(TileIndex tile) const
00178 {
00179 assert(this->TileBelongsToRailStation(tile));
00180
00181 TileIndexDiff delta = (GetRailStationAxis(tile) == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00182
00183 TileIndex t = tile;
00184 uint len = 0;
00185 do {
00186 t -= delta;
00187 len++;
00188 } while (IsCompatibleTrainStationTile(t, tile));
00189
00190 t = tile;
00191 do {
00192 t += delta;
00193 len++;
00194 } while (IsCompatibleTrainStationTile(t, tile));
00195
00196 return len - 1;
00197 }
00198
00199 uint Station::GetPlatformLength(TileIndex tile, DiagDirection dir) const
00200 {
00201 TileIndex start_tile = tile;
00202 uint length = 0;
00203 assert(IsRailStationTile(tile));
00204 assert(dir < DIAGDIR_END);
00205
00206 do {
00207 length++;
00208 tile += TileOffsByDiagDir(dir);
00209 } while (IsCompatibleTrainStationTile(tile, start_tile));
00210
00211 return length;
00212 }
00213
00217 uint Station::GetCatchmentRadius() const
00218 {
00219 uint ret = CA_NONE;
00220
00221 if (_settings_game.station.modified_catchment) {
00222 if (this->bus_stops != NULL) ret = max<uint>(ret, CA_BUS);
00223 if (this->truck_stops != NULL) ret = max<uint>(ret, CA_TRUCK);
00224 if (this->train_station.tile != INVALID_TILE) ret = max<uint>(ret, CA_TRAIN);
00225 if (this->dock_tile != INVALID_TILE) ret = max<uint>(ret, CA_DOCK);
00226 if (this->airport_tile != INVALID_TILE) ret = max<uint>(ret, this->GetAirportSpec()->catchment);
00227 } else {
00228 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) {
00229 ret = CA_UNMODIFIED;
00230 }
00231 }
00232
00233 return ret;
00234 }
00235
00240 Rect Station::GetCatchmentRect() const
00241 {
00242 assert(!this->rect.IsEmpty());
00243
00244
00245 int catchment_radius = this->GetCatchmentRadius();
00246
00247 Rect ret = {
00248 max<int>(this->rect.left - catchment_radius, 0),
00249 max<int>(this->rect.top - catchment_radius, 0),
00250 min<int>(this->rect.right + catchment_radius, MapMaxX()),
00251 min<int>(this->rect.bottom + catchment_radius, MapMaxY())
00252 };
00253
00254 return ret;
00255 }
00256
00258 struct RectAndIndustryVector {
00259 Rect rect;
00260 IndustryVector *industries_near;
00261 };
00262
00271 static bool FindIndustryToDeliver(TileIndex ind_tile, void *user_data)
00272 {
00273
00274 if (!IsTileType(ind_tile, MP_INDUSTRY)) return false;
00275
00276 RectAndIndustryVector *riv = (RectAndIndustryVector *)user_data;
00277 Industry *ind = Industry::GetByTile(ind_tile);
00278
00279
00280 if (riv->industries_near->Contains(ind)) return false;
00281
00282
00283 int x = TileX(ind_tile);
00284 int y = TileY(ind_tile);
00285 if (x < riv->rect.left || x > riv->rect.right || y < riv->rect.top || y > riv->rect.bottom) return false;
00286
00287
00288 uint cargo_index;
00289 for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00290 if (ind->accepts_cargo[cargo_index] != CT_INVALID) break;
00291 }
00292 if (cargo_index >= lengthof(ind->accepts_cargo)) return false;
00293
00294 *riv->industries_near->Append() = ind;
00295
00296 return false;
00297 }
00298
00303 void Station::RecomputeIndustriesNear()
00304 {
00305 this->industries_near.Clear();
00306 if (this->rect.IsEmpty()) return;
00307
00308 RectAndIndustryVector riv = {
00309 this->GetCatchmentRect(),
00310 &this->industries_near
00311 };
00312
00313
00314 TileIndex start_tile = this->xy;
00315 uint max_radius = max(
00316 max(DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.left, riv.rect.bottom))),
00317 max(DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.top)), DistanceManhattan(start_tile, TileXY(riv.rect.right, riv.rect.bottom)))
00318 );
00319
00320 CircularTileSearch(&start_tile, 2 * max_radius + 1, &FindIndustryToDeliver, &riv);
00321 }
00322
00326 void Station::RecomputeIndustriesNearForAll()
00327 {
00328 Station *st;
00329 FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
00330 }
00331
00332
00333
00334
00335
00336 StationRect::StationRect()
00337 {
00338 this->MakeEmpty();
00339 }
00340
00341 void StationRect::MakeEmpty()
00342 {
00343 this->left = this->top = this->right = this->bottom = 0;
00344 }
00345
00355 bool StationRect::PtInExtendedRect(int x, int y, int distance) const
00356 {
00357 return
00358 this->left - distance <= x &&
00359 x <= this->right + distance &&
00360 this->top - distance <= y &&
00361 y <= this->bottom + distance;
00362 }
00363
00364 bool StationRect::IsEmpty() const
00365 {
00366 return this->left == 0 || this->left > this->right || this->top > this->bottom;
00367 }
00368
00369 bool StationRect::BeforeAddTile(TileIndex tile, StationRectMode mode)
00370 {
00371 int x = TileX(tile);
00372 int y = TileY(tile);
00373 if (IsEmpty()) {
00374
00375 if (mode != ADD_TEST) {
00376 this->left = this->right = x;
00377 this->top = this->bottom = y;
00378 }
00379 } else if (!PtInExtendedRect(x, y)) {
00380
00381
00382 Rect new_rect = {min(x, this->left), min(y, this->top), max(x, this->right), max(y, this->bottom)};
00383
00384
00385 int w = new_rect.right - new_rect.left + 1;
00386 int h = new_rect.bottom - new_rect.top + 1;
00387 if (mode != ADD_FORCE && (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread)) {
00388 assert(mode != ADD_TRY);
00389 _error_message = STR_ERROR_STATION_TOO_SPREAD_OUT;
00390 return false;
00391 }
00392
00393
00394 if (mode != ADD_TEST) {
00395
00396 *this = new_rect;
00397 }
00398 } else {
00399 ;
00400 }
00401 return true;
00402 }
00403
00404 bool StationRect::BeforeAddRect(TileIndex tile, int w, int h, StationRectMode mode)
00405 {
00406 return (mode == ADD_FORCE || (w <= _settings_game.station.station_spread && h <= _settings_game.station.station_spread)) &&
00407 this->BeforeAddTile(tile, mode) && this->BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
00408 }
00409
00419 bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a)
00420 {
00421 TileIndex top_left = TileXY(left_a, top_a);
00422 int width = right_a - left_a + 1;
00423 int height = bottom_a - top_a + 1;
00424
00425 TILE_LOOP(tile, width, height, top_left) {
00426 if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true;
00427 }
00428
00429 return false;
00430 }
00431
00432 bool StationRect::AfterRemoveTile(BaseStation *st, TileIndex tile)
00433 {
00434 int x = TileX(tile);
00435 int y = TileY(tile);
00436
00437
00438
00439
00440 for (;;) {
00441
00442 bool left_edge = (x == this->left);
00443 bool right_edge = (x == this->right);
00444 bool top_edge = (y == this->top);
00445 bool bottom_edge = (y == this->bottom);
00446
00447
00448 bool reduce_x = ((left_edge || right_edge) && !ScanForStationTiles(st->index, x, this->top, x, this->bottom));
00449 bool reduce_y = ((top_edge || bottom_edge) && !ScanForStationTiles(st->index, this->left, y, this->right, y));
00450 if (!(reduce_x || reduce_y)) break;
00451
00452 if (reduce_x) {
00453
00454 if (left_edge) {
00455
00456 this->left = x = x + 1;
00457 } else {
00458
00459 this->right = x = x - 1;
00460 }
00461 }
00462 if (reduce_y) {
00463
00464 if (top_edge) {
00465
00466 this->top = y = y + 1;
00467 } else {
00468
00469 this->bottom = y = y - 1;
00470 }
00471 }
00472
00473 if (left > right || top > bottom) {
00474
00475 this->MakeEmpty();
00476 return true;
00477 }
00478 }
00479 return false;
00480 }
00481
00482 bool StationRect::AfterRemoveRect(BaseStation *st, TileIndex tile, int w, int h)
00483 {
00484 assert(PtInExtendedRect(TileX(tile), TileY(tile)));
00485 assert(PtInExtendedRect(TileX(tile) + w - 1, TileY(tile) + h - 1));
00486
00487 bool empty = this->AfterRemoveTile(st, tile);
00488 if (w != 1 || h != 1) empty = empty || AfterRemoveTile(st, TILE_ADDXY(tile, w - 1, h - 1));
00489 return empty;
00490 }
00491
00492 StationRect& StationRect::operator = (Rect src)
00493 {
00494 this->left = src.left;
00495 this->top = src.top;
00496 this->right = src.right;
00497 this->bottom = src.bottom;
00498 return *this;
00499 }
00500
00501 TileArea::TileArea(TileIndex start, TileIndex end)
00502 {
00503 uint sx = TileX(start);
00504 uint sy = TileY(start);
00505 uint ex = TileX(end);
00506 uint ey = TileY(end);
00507
00508 if (sx > ex) Swap(sx, ex);
00509 if (sy > ey) Swap(sy, ey);
00510
00511 this->tile = TileXY(sx, sy);
00512 this->w = ex - sx + 1;
00513 this->h = ey - sy + 1;
00514 }
00515
00516 void TileArea::Add(TileIndex to_add)
00517 {
00518 if (this->tile == INVALID_TILE) {
00519 this->tile = to_add;
00520 this->w = 1;
00521 this->h = 1;
00522 return;
00523 }
00524
00525 uint sx = TileX(this->tile);
00526 uint sy = TileY(this->tile);
00527 uint ex = sx + this->w - 1;
00528 uint ey = sy + this->h - 1;
00529
00530 uint ax = TileX(to_add);
00531 uint ay = TileY(to_add);
00532
00533 sx = min(ax, sx);
00534 sy = min(ay, sy);
00535 ex = max(ax, ex);
00536 ey = max(ay, ey);
00537
00538 this->tile = TileXY(sx, sy);
00539 this->w = ex - sx + 1;
00540 this->h = ey - sy + 1;
00541 }
00542
00543 bool TileArea::Intersects(const TileArea &ta) const
00544 {
00545 if (ta.w == 0 || this->w == 0) return false;
00546
00547 assert(ta.w != 0 && ta.h != 0 && this->w != 0 && this->h != 0);
00548
00549 uint left1 = TileX(this->tile);
00550 uint top1 = TileY(this->tile);
00551 uint right1 = left1 + this->w - 1;
00552 uint bottom1 = top1 + this->h - 1;
00553
00554 uint left2 = TileX(ta.tile);
00555 uint top2 = TileY(ta.tile);
00556 uint right2 = left2 + ta.w - 1;
00557 uint bottom2 = top2 + ta.h - 1;
00558
00559 return !(
00560 left2 > right1 ||
00561 right2 < left1 ||
00562 top2 > bottom1 ||
00563 bottom2 < top1
00564 );
00565 }
00566
00567
00568 void InitializeStations()
00569 {
00570 _station_pool.CleanPool();
00571 }