station.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
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   /* this->random_bits is set in Station::AddFacility() */
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     /* Forget about this station if this station is removed */
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   /* Now delete all orders that go to the station */
00091   RemoveOrderFromAllVehicles(OT_GOTO_STATION, this->index);
00092 
00093   /* Remove all news items */
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     /* The vehicle cannot go to this roadstop (different roadtype) */
00125     if ((GetRoadTypes(rs->xy) & v->compatible_roadtypes) == ROADTYPES_NONE) continue;
00126     /* The vehicle is articulated and can therefor not go the a standard road stop */
00127     if (IsStandardRoadStopTile(rs->xy) && v->HasArticulatedPart()) continue;
00128 
00129     /* The vehicle can actually go to this road stop. So, return it! */
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   /* cargo_change is set if we're refreshing the tiles due to cargo moving
00157    * around. */
00158   if (cargo_change) {
00159     /* Don't waste time updating if there are no custom station graphics
00160      * that might change. Even if there are custom graphics, they might
00161      * not change. Unfortunately we have no way of telling. */
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 /* virtual */ 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 /* virtual */ 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   /* Compute acceptance rectangle */
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   /* Only process industry tiles */
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   /* Don't check further if this industry is already in the list */
00279   if (riv->industries_near->Contains(ind)) return false;
00280 
00281   /* Only process tiles in the station acceptance rectangle */
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   /* Include only industries that can accept cargo */
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   /* Compute maximum extent of acceptance rectangle wrt. station sign */
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 /* static */ void Station::RecomputeIndustriesNearForAll()
00326 {
00327   Station *st;
00328   FOR_ALL_STATIONS(st) st->RecomputeIndustriesNear();
00329 }
00330 
00331 /************************************************************************/
00332 /*                     StationRect implementation                       */
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     /* we are adding the first station tile */
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     /* current rect is not empty and new point is outside this rect
00380      * make new spread-out rectangle */
00381     Rect new_rect = {min(x, this->left), min(y, this->top), max(x, this->right), max(y, this->bottom)};
00382 
00383     /* check new rect dimensions against preset max */
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     /* spread-out ok, return true */
00393     if (mode != ADD_TEST) {
00394       /* we should update the station rect */
00395       *this = new_rect;
00396     }
00397   } else {
00398     ; // new point is inside the rect, we don't need to do anything
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)) && // important when the old rect is completely inside the new rect, resp. the old one was empty
00406       this->BeforeAddTile(tile, mode) && this->BeforeAddTile(TILE_ADDXY(tile, w - 1, h - 1), mode);
00407 }
00408 
00418 /* static */ 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   /* look if removed tile was on the bounding rect edge
00437    * and try to reduce the rect by this edge
00438    * do it until we have empty rect or nothing to do */
00439   for (;;) {
00440     /* check if removed tile is on rect edge */
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     /* can we reduce the rect in either direction? */
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; // nothing to do (can't reduce)
00450 
00451     if (reduce_x) {
00452       /* reduce horizontally */
00453       if (left_edge) {
00454         /* move left edge right */
00455         this->left = x = x + 1;
00456       } else {
00457         /* move right edge left */
00458         this->right = x = x - 1;
00459       }
00460     }
00461     if (reduce_y) {
00462       /* reduce vertically */
00463       if (top_edge) {
00464         /* move top edge down */
00465         this->top = y = y + 1;
00466       } else {
00467         /* move bottom edge up */
00468         this->bottom = y = y - 1;
00469       }
00470     }
00471 
00472     if (left > right || top > bottom) {
00473       /* can't continue, if the remaining rectangle is empty */
00474       this->MakeEmpty();
00475       return true; // empty remaining rect
00476     }
00477   }
00478   return false; // non-empty remaining rect
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 
00543 void InitializeStations()
00544 {
00545   _station_pool.CleanPool();
00546 }

Generated on Sat Dec 26 20:06:05 2009 for OpenTTD by  doxygen 1.5.6