1 /* $Id: script_station.cpp 26398 2014-03-11 22:08:58Z zuu $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file script_station.cpp Implementation of ScriptStation. */
12 #include "../../stdafx.h"
13 #include "script_station.hpp"
14 #include "script_map.hpp"
15 #include "script_town.hpp"
16 #include "script_cargo.hpp"
17 #include "../../station_base.h"
18 #include "../../roadstop_base.h"
19 #include "../../town.h"
21 #include "../../safeguards.h"
23 /* static */ bool ScriptStation::IsValidStation(StationID station_id
)
25 const Station
*st
= ::Station::GetIfValid(station_id
);
26 return st
!= NULL
&& (st
->owner
== ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY
|| 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;
53 template<bool Tfrom
, bool Tvia
>
54 /* static */ int32
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
)) {
62 const StationCargoList
&cargo_list
= ::Station::Get(station_id
)->goods
[cargo_id
].cargo
;
63 if (!Tfrom
&& !Tvia
) return cargo_list
.TotalCount();
65 uint16 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
->SourceStation() == from_station_id
) cargo_count
+= cp
->Count();
78 /* static */ int32
ScriptStation::GetCargoWaiting(StationID station_id
, CargoID cargo_id
)
80 return CountCargoWaiting
<false, false>(station_id
, STATION_INVALID
, STATION_INVALID
, cargo_id
);
83 /* static */ int32
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 */ int32
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 */ int32
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 */ int32
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
)) {
110 const FlowStatMap
&flows
= ::Station::Get(station_id
)->goods
[cargo_id
].flows
;
112 return Tvia
? flows
.GetFlowFromVia(from_station_id
, via_station_id
) :
113 flows
.GetFlowFrom(from_station_id
);
115 return Tvia
? flows
.GetFlowVia(via_station_id
) : flows
.GetFlow();
119 /* static */ int32
ScriptStation::GetCargoPlanned(StationID station_id
, CargoID cargo_id
)
121 return CountCargoPlanned
<false, false>(station_id
, STATION_INVALID
, STATION_INVALID
, cargo_id
);
124 /* static */ int32
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 */ int32
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 */ int32
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 */ int32
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 */ int32
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 */ int32
ScriptStation::GetStationCoverageRadius(StationID station_id
)
175 if (!IsValidStation(station_id
)) return -1;
177 return Station::Get(station_id
)->GetCatchmentRadius();
180 /* static */ int32
ScriptStation::GetDistanceManhattanToTile(StationID station_id
, TileIndex tile
)
182 if (!IsValidStation(station_id
)) return -1;
184 return ScriptMap::DistanceManhattan(tile
, GetLocation(station_id
));
187 /* static */ int32
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
& 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 ::RoadTypes r
= RoadTypeToRoadTypes((::RoadType
)road_type
);
216 for (const RoadStop
*rs
= ::Station::Get(station_id
)->GetPrimaryRoadStop(ROADSTOP_BUS
); rs
!= NULL
; rs
= rs
->next
) {
217 if ((::GetRoadTypes(rs
->xy
) & r
) != 0) return true;
219 for (const RoadStop
*rs
= ::Station::Get(station_id
)->GetPrimaryRoadStop(ROADSTOP_TRUCK
); rs
!= NULL
; rs
= rs
->next
) {
220 if ((::GetRoadTypes(rs
->xy
) & r
) != 0) return true;
226 /* static */ TownID
ScriptStation::GetNearestTown(StationID station_id
)
228 if (!IsValidStation(station_id
)) return INVALID_TOWN
;
230 return ::Station::Get(station_id
)->town
->index
;
233 /* static */ bool ScriptStation::IsAirportClosed(StationID station_id
)
235 EnforcePrecondition(false, IsValidStation(station_id
));
236 EnforcePrecondition(false, HasStationType(station_id
, STATION_AIRPORT
));
238 return (::Station::Get(station_id
)->airport
.flags
& AIRPORT_CLOSED_block
) != 0;
241 /* static */ bool ScriptStation::OpenCloseAirport(StationID station_id
)
243 EnforcePrecondition(false, IsValidStation(station_id
));
244 EnforcePrecondition(false, HasStationType(station_id
, STATION_AIRPORT
));
246 return ScriptObject::DoCommand(0, station_id
, 0, CMD_OPEN_CLOSE_AIRPORT
);