station.cpp

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

Generated on Wed Mar 31 22:43:28 2010 for OpenTTD by  doxygen 1.6.1