Update: Translations from eints
[openttd-github.git] / src / script / api / script_station.cpp
blobee3ff0d5d652d570b1bb4cdfd302b4649b4fc75b
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file script_station.cpp Implementation of ScriptStation. */
10 #include "../../stdafx.h"
11 #include "script_station.hpp"
12 #include "script_map.hpp"
13 #include "script_town.hpp"
14 #include "script_cargo.hpp"
15 #include "../../station_base.h"
16 #include "../../roadstop_base.h"
17 #include "../../town.h"
18 #include "../../station_cmd.h"
20 #include "../../safeguards.h"
22 /* static */ bool ScriptStation::IsValidStation(StationID station_id)
24 EnforceDeityOrCompanyModeValid(false);
25 const Station *st = ::Station::GetIfValid(station_id);
26 return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE);
29 /* static */ ScriptCompany::CompanyID ScriptStation::GetOwner(StationID station_id)
31 if (!IsValidStation(station_id)) return ScriptCompany::COMPANY_INVALID;
33 return static_cast<ScriptCompany::CompanyID>((int)::Station::Get(station_id)->owner);
36 /* static */ StationID ScriptStation::GetStationID(TileIndex tile)
38 if (!::IsValidTile(tile) || !::IsTileType(tile, MP_STATION)) return INVALID_STATION;
39 return ::GetStationIndex(tile);
42 template<bool Tfrom, bool Tvia>
43 /* static */ bool ScriptStation::IsCargoRequestValid(StationID station_id,
44 StationID from_station_id, StationID via_station_id, CargoID cargo_id)
46 if (!IsValidStation(station_id)) return false;
47 if (Tfrom && !IsValidStation(from_station_id) && from_station_id != STATION_INVALID) return false;
48 if (Tvia && !IsValidStation(via_station_id) && via_station_id != STATION_INVALID) return false;
49 if (!ScriptCargo::IsValidCargo(cargo_id)) return false;
50 return true;
53 template<bool Tfrom, bool Tvia>
54 /* static */ SQInteger ScriptStation::CountCargoWaiting(StationID station_id,
55 StationID from_station_id, StationID via_station_id, CargoID cargo_id)
57 if (!ScriptStation::IsCargoRequestValid<Tfrom, Tvia>(station_id, from_station_id,
58 via_station_id, cargo_id)) {
59 return -1;
62 const StationCargoList &cargo_list = ::Station::Get(station_id)->goods[cargo_id].cargo;
63 if (!Tfrom && !Tvia) return cargo_list.TotalCount();
65 uint16_t cargo_count = 0;
66 std::pair<StationCargoList::ConstIterator, StationCargoList::ConstIterator> range = Tvia ?
67 cargo_list.Packets()->equal_range(via_station_id) :
68 std::make_pair(StationCargoList::ConstIterator(cargo_list.Packets()->begin()),
69 StationCargoList::ConstIterator(cargo_list.Packets()->end()));
70 for (StationCargoList::ConstIterator it = range.first; it != range.second; it++) {
71 const CargoPacket *cp = *it;
72 if (!Tfrom || cp->GetFirstStation() == from_station_id) cargo_count += cp->Count();
75 return cargo_count;
78 /* static */ SQInteger ScriptStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
80 return CountCargoWaiting<false, false>(station_id, STATION_INVALID, STATION_INVALID, cargo_id);
83 /* static */ SQInteger ScriptStation::GetCargoWaitingFrom(StationID station_id,
84 StationID from_station_id, CargoID cargo_id)
86 return CountCargoWaiting<true, false>(station_id, from_station_id, STATION_INVALID, cargo_id);
89 /* static */ SQInteger ScriptStation::GetCargoWaitingVia(StationID station_id,
90 StationID via_station_id, CargoID cargo_id)
92 return CountCargoWaiting<false, true>(station_id, STATION_INVALID, via_station_id, cargo_id);
95 /* static */ SQInteger ScriptStation::GetCargoWaitingFromVia(StationID station_id,
96 StationID from_station_id, StationID via_station_id, CargoID cargo_id)
98 return CountCargoWaiting<true, true>(station_id, from_station_id, via_station_id, cargo_id);
101 template<bool Tfrom, bool Tvia>
102 /* static */ SQInteger ScriptStation::CountCargoPlanned(StationID station_id,
103 StationID from_station_id, StationID via_station_id, CargoID cargo_id)
105 if (!ScriptStation::IsCargoRequestValid<Tfrom, Tvia>(station_id, from_station_id,
106 via_station_id, cargo_id)) {
107 return -1;
110 const FlowStatMap &flows = ::Station::Get(station_id)->goods[cargo_id].flows;
111 if (Tfrom) {
112 return Tvia ? flows.GetFlowFromVia(from_station_id, via_station_id) :
113 flows.GetFlowFrom(from_station_id);
114 } else {
115 return Tvia ? flows.GetFlowVia(via_station_id) : flows.GetFlow();
119 /* static */ SQInteger ScriptStation::GetCargoPlanned(StationID station_id, CargoID cargo_id)
121 return CountCargoPlanned<false, false>(station_id, STATION_INVALID, STATION_INVALID, cargo_id);
124 /* static */ SQInteger ScriptStation::GetCargoPlannedFrom(StationID station_id,
125 StationID from_station_id, CargoID cargo_id)
127 return CountCargoPlanned<true, false>(station_id, from_station_id, STATION_INVALID, cargo_id);
130 /* static */ SQInteger ScriptStation::GetCargoPlannedVia(StationID station_id,
131 StationID via_station_id, CargoID cargo_id)
133 return CountCargoPlanned<false, true>(station_id, STATION_INVALID, via_station_id, cargo_id);
136 /* static */ SQInteger ScriptStation::GetCargoPlannedFromVia(StationID station_id,
137 StationID from_station_id, StationID via_station_id, CargoID cargo_id)
139 return CountCargoPlanned<true, true>(station_id, from_station_id, via_station_id, cargo_id);
142 /* static */ bool ScriptStation::HasCargoRating(StationID station_id, CargoID cargo_id)
144 if (!IsValidStation(station_id)) return false;
145 if (!ScriptCargo::IsValidCargo(cargo_id)) return false;
147 return ::Station::Get(station_id)->goods[cargo_id].HasRating();
150 /* static */ SQInteger ScriptStation::GetCargoRating(StationID station_id, CargoID cargo_id)
152 if (!ScriptStation::HasCargoRating(station_id, cargo_id)) return -1;
154 return ::ToPercent8(::Station::Get(station_id)->goods[cargo_id].rating);
157 /* static */ SQInteger ScriptStation::GetCoverageRadius(ScriptStation::StationType station_type)
159 if (station_type == STATION_AIRPORT) return -1;
160 if (!HasExactlyOneBit(station_type)) return -1;
162 if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
164 switch (station_type) {
165 case STATION_TRAIN: return CA_TRAIN;
166 case STATION_TRUCK_STOP: return CA_TRUCK;
167 case STATION_BUS_STOP: return CA_BUS;
168 case STATION_DOCK: return CA_DOCK;
169 default: return CA_NONE;
173 /* static */ SQInteger ScriptStation::GetStationCoverageRadius(StationID station_id)
175 if (!IsValidStation(station_id)) return -1;
177 return Station::Get(station_id)->GetCatchmentRadius();
180 /* static */ SQInteger ScriptStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
182 if (!IsValidStation(station_id)) return -1;
184 return ScriptMap::DistanceManhattan(tile, GetLocation(station_id));
187 /* static */ SQInteger ScriptStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
189 if (!IsValidStation(station_id)) return -1;
191 return ScriptMap::DistanceSquare(tile, GetLocation(station_id));
194 /* static */ bool ScriptStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
196 if (!IsValidStation(station_id)) return false;
198 return ScriptTown::IsWithinTownInfluence(town_id, GetLocation(station_id));
201 /* static */ bool ScriptStation::HasStationType(StationID station_id, StationType station_type)
203 if (!IsValidStation(station_id)) return false;
204 if (!HasExactlyOneBit(station_type)) return false;
206 return (::Station::Get(station_id)->facilities & static_cast<StationFacility>(station_type)) != 0;
209 /* static */ bool ScriptStation::HasRoadType(StationID station_id, ScriptRoad::RoadType road_type)
211 if (!IsValidStation(station_id)) return false;
212 if (!ScriptRoad::IsRoadTypeAvailable(road_type)) return false;
214 for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != nullptr; rs = rs->next) {
215 if (HasBit(::GetPresentRoadTypes(rs->xy), (::RoadType)road_type)) return true;
217 for (const RoadStop *rs = ::Station::Get(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != nullptr; rs = rs->next) {
218 if (HasBit(::GetPresentRoadTypes(rs->xy), (::RoadType)road_type)) return true;
221 return false;
224 /* static */ TownID ScriptStation::GetNearestTown(StationID station_id)
226 if (!IsValidStation(station_id)) return INVALID_TOWN;
228 return ::Station::Get(station_id)->town->index;
231 /* static */ bool ScriptStation::IsAirportClosed(StationID station_id)
233 EnforcePrecondition(false, IsValidStation(station_id));
234 EnforcePrecondition(false, HasStationType(station_id, STATION_AIRPORT));
236 return (::Station::Get(station_id)->airport.flags & AIRPORT_CLOSED_block) != 0;
239 /* static */ bool ScriptStation::OpenCloseAirport(StationID station_id)
241 EnforceCompanyModeValid(false);
242 EnforcePrecondition(false, IsValidStation(station_id));
243 EnforcePrecondition(false, HasStationType(station_id, STATION_AIRPORT));
245 return ScriptObject::Command<CMD_OPEN_CLOSE_AIRPORT>::Do(station_id);