script_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 "script_station.hpp"
00014 #include "script_cargo.hpp"
00015 #include "script_map.hpp"
00016 #include "script_town.hpp"
00017 #include "../../debug.h"
00018 #include "../../station_base.h"
00019 #include "../../roadstop_base.h"
00020 #include "../../company_func.h"
00021 #include "../../town.h"
00022 
00023 /* static */ bool ScriptStation::IsValidStation(StationID station_id)
00024 {
00025   const Station *st = ::Station::GetIfValid(station_id);
00026   return st != NULL && (st->owner == _current_company || st->owner == OWNER_NONE);
00027 }
00028 
00029 /* static */ StationID ScriptStation::GetStationID(TileIndex tile)
00030 {
00031   if (!::IsValidTile(tile) || !::IsTileType(tile, MP_STATION)) return INVALID_STATION;
00032   return ::GetStationIndex(tile);
00033 }
00034 
00035 /* static */ int32 ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
00036 {
00037   if (!IsValidStation(station_id)) return -1;
00038   if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
00039 
00040   return ::Station::Get(station_id)->goods[cargo_id].cargo.Count();
00041 }
00042 
00043 /* static */ int32 ScriptStation::GetCargoRating(StationID station_id, CargoID cargo_id)
00044 {
00045   if (!IsValidStation(station_id)) return -1;
00046   if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
00047 
00048   return ::ToPercent8(::Station::Get(station_id)->goods[cargo_id].rating);
00049 }
00050 
00051 /* static */ int32 ScriptStation::GetCoverageRadius(ScriptStation::StationType station_type)
00052 {
00053   if (station_type == STATION_AIRPORT) return -1;
00054   if (!HasExactlyOneBit(station_type)) return -1;
00055 
00056   if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
00057 
00058   switch (station_type) {
00059     case STATION_TRAIN:      return CA_TRAIN;
00060     case STATION_TRUCK_STOP: return CA_TRUCK;
00061     case STATION_BUS_STOP:   return CA_BUS;
00062     case STATION_DOCK:       return CA_DOCK;
00063     default:                 return CA_NONE;
00064   }
00065 }
00066 
00067 /* static */ int32 ScriptStation::GetStationCoverageRadius(StationID station_id)
00068 {
00069   if (!IsValidStation(station_id)) return -1;
00070 
00071   return Station::Get(station_id)->GetCatchmentRadius();
00072 }
00073 
00074 /* static */ int32 ScriptStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
00075 {
00076   if (!IsValidStation(station_id)) return -1;
00077 
00078   return ScriptMap::DistanceManhattan(tile, GetLocation(station_id));
00079 }
00080 
00081 /* static */ int32 ScriptStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
00082 {
00083   if (!IsValidStation(station_id)) return -1;
00084 
00085   return ScriptMap::DistanceSquare(tile, GetLocation(station_id));
00086 }
00087 
00088 /* static */ bool ScriptStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
00089 {
00090   if (!IsValidStation(station_id)) return false;
00091 
00092   return ScriptTown::IsWithinTownInfluence(town_id, GetLocation(station_id));
00093 }
00094 
00095 /* static */ bool ScriptStation::HasStationType(StationID station_id, StationType station_type)
00096 {
00097   if (!IsValidStation(station_id)) return false;
00098   if (!HasExactlyOneBit(station_type)) return false;
00099 
00100   return (::Station::Get(station_id)->facilities & station_type) != 0;
00101 }
00102 
00103 /* static */ bool ScriptStation::HasRoadType(StationID station_id, ScriptRoad::RoadType road_type)
00104 {
00105   if (!IsValidStation(station_id)) return false;
00106   if (!ScriptRoad::IsRoadTypeAvailable(road_type)) return false;
00107 
00108 	::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
00109 
00110   for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
00111     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
00112   }
00113   for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
00114     if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
00115   }
00116 
00117   return false;
00118 }
00119 
00120 /* static */ TownID ScriptStation::GetNearestTown(StationID station_id)
00121 {
00122   if (!IsValidStation(station_id)) return INVALID_TOWN;
00123 
00124   return ::Station::Get(station_id)->town->index;
00125 }