1 /* $Id: script_industry.cpp 23777 2012-01-08 21:48:05Z rubidium $ */
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_industry.cpp Implementation of ScriptIndustry. */
12 #include "../../stdafx.h"
13 #include "script_industry.hpp"
14 #include "script_cargo.hpp"
15 #include "script_map.hpp"
16 #include "../../industry.h"
17 #include "../../strings_func.h"
18 #include "../../station_base.h"
19 #include "../../newgrf_industries.h"
20 #include "table/strings.h"
22 #include "../../safeguards.h"
24 /* static */ int32
ScriptIndustry::GetIndustryCount()
26 return (int32
)::Industry::GetNumItems();
29 /* static */ bool ScriptIndustry::IsValidIndustry(IndustryID industry_id
)
31 return ::Industry::IsValidID(industry_id
);
34 /* static */ IndustryID
ScriptIndustry::GetIndustryID(TileIndex tile
)
36 if (!::IsValidTile(tile
) || !::IsTileType(tile
, MP_INDUSTRY
)) return INVALID_INDUSTRY
;
37 return ::GetIndustryIndex(tile
);
40 /* static */ char *ScriptIndustry::GetName(IndustryID industry_id
)
42 if (!IsValidIndustry(industry_id
)) return NULL
;
44 ::SetDParam(0, industry_id
);
45 return GetString(STR_INDUSTRY_NAME
);
48 /* static */ ScriptIndustry::CargoAcceptState
ScriptIndustry::IsCargoAccepted(IndustryID industry_id
, CargoID cargo_id
)
50 if (!IsValidIndustry(industry_id
)) return CAS_NOT_ACCEPTED
;
51 if (!ScriptCargo::IsValidCargo(cargo_id
)) return CAS_NOT_ACCEPTED
;
53 Industry
*i
= ::Industry::Get(industry_id
);
55 for (byte j
= 0; j
< lengthof(i
->accepts_cargo
); j
++) {
56 if (i
->accepts_cargo
[j
] == cargo_id
) {
57 if (IndustryTemporarilyRefusesCargo(i
, cargo_id
)) return CAS_TEMP_REFUSED
;
62 return CAS_NOT_ACCEPTED
;
65 /* static */ int32
ScriptIndustry::GetStockpiledCargo(IndustryID industry_id
, CargoID cargo_id
)
67 if (!IsValidIndustry(industry_id
)) return -1;
68 if (!ScriptCargo::IsValidCargo(cargo_id
)) return -1;
70 Industry
*ind
= ::Industry::Get(industry_id
);
71 for (uint i
= 0; i
< lengthof(ind
->accepts_cargo
); i
++) {
72 CargoID cid
= ind
->accepts_cargo
[i
];
73 if (cid
== cargo_id
) {
74 return ind
->incoming_cargo_waiting
[i
];
81 /* static */ int32
ScriptIndustry::GetLastMonthProduction(IndustryID industry_id
, CargoID cargo_id
)
83 if (!IsValidIndustry(industry_id
)) return -1;
84 if (!ScriptCargo::IsValidCargo(cargo_id
)) return -1;
86 const Industry
*i
= ::Industry::Get(industry_id
);
88 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
89 if (i
->produced_cargo
[j
] == cargo_id
) return i
->last_month_production
[j
];
95 /* static */ int32
ScriptIndustry::GetLastMonthTransported(IndustryID industry_id
, CargoID cargo_id
)
97 if (!IsValidIndustry(industry_id
)) return -1;
98 if (!ScriptCargo::IsValidCargo(cargo_id
)) return -1;
100 const Industry
*i
= ::Industry::Get(industry_id
);
102 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
103 if (i
->produced_cargo
[j
] == cargo_id
) return i
->last_month_transported
[j
];
109 /* static */ int32
ScriptIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id
, CargoID cargo_id
)
111 if (!IsValidIndustry(industry_id
)) return -1;
112 if (!ScriptCargo::IsValidCargo(cargo_id
)) return -1;
114 const Industry
*i
= ::Industry::Get(industry_id
);
116 for (byte j
= 0; j
< lengthof(i
->produced_cargo
); j
++) {
117 if (i
->produced_cargo
[j
] == cargo_id
) return ::ToPercent8(i
->last_month_pct_transported
[j
]);
123 /* static */ TileIndex
ScriptIndustry::GetLocation(IndustryID industry_id
)
125 if (!IsValidIndustry(industry_id
)) return INVALID_TILE
;
127 return ::Industry::Get(industry_id
)->location
.tile
;
130 /* static */ int32
ScriptIndustry::GetAmountOfStationsAround(IndustryID industry_id
)
132 if (!IsValidIndustry(industry_id
)) return -1;
134 Industry
*ind
= ::Industry::Get(industry_id
);
135 StationList stations
;
136 ::FindStationsAroundTiles(ind
->location
, &stations
);
137 return (int32
)stations
.Length();
140 /* static */ int32
ScriptIndustry::GetDistanceManhattanToTile(IndustryID industry_id
, TileIndex tile
)
142 if (!IsValidIndustry(industry_id
)) return -1;
144 return ScriptMap::DistanceManhattan(tile
, GetLocation(industry_id
));
147 /* static */ int32
ScriptIndustry::GetDistanceSquareToTile(IndustryID industry_id
, TileIndex tile
)
149 if (!IsValidIndustry(industry_id
)) return -1;
151 return ScriptMap::DistanceSquare(tile
, GetLocation(industry_id
));
154 /* static */ bool ScriptIndustry::IsBuiltOnWater(IndustryID industry_id
)
156 if (!IsValidIndustry(industry_id
)) return false;
158 return (::GetIndustrySpec(::Industry::Get(industry_id
)->type
)->behaviour
& INDUSTRYBEH_BUILT_ONWATER
) != 0;
161 /* static */ bool ScriptIndustry::HasHeliport(IndustryID industry_id
)
163 if (!IsValidIndustry(industry_id
)) return false;
165 return (::GetIndustrySpec(::Industry::Get(industry_id
)->type
)->behaviour
& INDUSTRYBEH_AI_AIRSHIP_ROUTES
) != 0;
168 /* static */ TileIndex
ScriptIndustry::GetHeliportLocation(IndustryID industry_id
)
170 if (!IsValidIndustry(industry_id
)) return INVALID_TILE
;
171 if (!HasHeliport(industry_id
)) return INVALID_TILE
;
173 const Industry
*ind
= ::Industry::Get(industry_id
);
174 TILE_AREA_LOOP(tile_cur
, ind
->location
) {
175 if (IsTileType(tile_cur
, MP_STATION
) && IsOilRig(tile_cur
)) {
183 /* static */ bool ScriptIndustry::HasDock(IndustryID industry_id
)
185 if (!IsValidIndustry(industry_id
)) return false;
187 return (::GetIndustrySpec(::Industry::Get(industry_id
)->type
)->behaviour
& INDUSTRYBEH_AI_AIRSHIP_ROUTES
) != 0;
190 /* static */ TileIndex
ScriptIndustry::GetDockLocation(IndustryID industry_id
)
192 if (!IsValidIndustry(industry_id
)) return INVALID_TILE
;
193 if (!HasDock(industry_id
)) return INVALID_TILE
;
195 const Industry
*ind
= ::Industry::Get(industry_id
);
196 TILE_AREA_LOOP(tile_cur
, ind
->location
) {
197 if (IsTileType(tile_cur
, MP_STATION
) && IsOilRig(tile_cur
)) {
205 /* static */ IndustryType
ScriptIndustry::GetIndustryType(IndustryID industry_id
)
207 if (!IsValidIndustry(industry_id
)) return INVALID_INDUSTRYTYPE
;
209 return ::Industry::Get(industry_id
)->type
;