script_station.cpp

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