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