Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / script / api / script_industry.cpp
blob86f5a85e40879e617fb42d60b781f6822df58986
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_industry.cpp Implementation of ScriptIndustry. */
10 #include "../../stdafx.h"
11 #include "script_industry.hpp"
12 #include "script_cargo.hpp"
13 #include "script_company.hpp"
14 #include "script_error.hpp"
15 #include "script_map.hpp"
16 #include "../../company_base.h"
17 #include "../../industry.h"
18 #include "../../string_func.h"
19 #include "../../strings_func.h"
20 #include "../../station_base.h"
21 #include "../../newgrf_industries.h"
22 #include "../../industry_cmd.h"
23 #include "../../timer/timer_game_calendar.h"
24 #include "table/strings.h"
26 #include "../../safeguards.h"
28 /* static */ SQInteger ScriptIndustry::GetIndustryCount()
30 return ::Industry::GetNumItems();
33 /* static */ bool ScriptIndustry::IsValidIndustry(IndustryID industry_id)
35 return ::Industry::IsValidID(industry_id);
38 /* static */ IndustryID ScriptIndustry::GetIndustryID(TileIndex tile)
40 if (!::IsValidTile(tile) || !::IsTileType(tile, MP_INDUSTRY)) return INVALID_INDUSTRY;
41 return ::GetIndustryIndex(tile);
44 /* static */ std::optional<std::string> ScriptIndustry::GetName(IndustryID industry_id)
46 if (!IsValidIndustry(industry_id)) return std::nullopt;
48 ::SetDParam(0, industry_id);
49 return GetString(STR_INDUSTRY_NAME);
52 /* static */ ScriptDate::Date ScriptIndustry::GetConstructionDate(IndustryID industry_id)
54 Industry *i = Industry::GetIfValid(industry_id);
55 if (i == nullptr) return ScriptDate::DATE_INVALID;
56 return (ScriptDate::Date)i->construction_date.base();
59 /* static */ bool ScriptIndustry::SetText(IndustryID industry_id, Text *text)
61 CCountedPtr<Text> counter(text);
63 EnforceDeityMode(false);
64 EnforcePrecondition(false, IsValidIndustry(industry_id));
66 return ScriptObject::Command<CMD_INDUSTRY_SET_TEXT>::Do(industry_id, text != nullptr ? text->GetEncodedText() : std::string{});
69 /* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
71 if (!IsValidIndustry(industry_id)) return CAS_NOT_ACCEPTED;
72 if (!ScriptCargo::IsValidCargo(cargo_id)) return CAS_NOT_ACCEPTED;
74 Industry *i = ::Industry::Get(industry_id);
76 if (!i->IsCargoAccepted(cargo_id)) return CAS_NOT_ACCEPTED;
77 if (IndustryTemporarilyRefusesCargo(i, cargo_id)) return CAS_TEMP_REFUSED;
79 return CAS_ACCEPTED;
82 /* static */ SQInteger ScriptIndustry::GetStockpiledCargo(IndustryID industry_id, CargoID cargo_id)
84 if (!IsValidIndustry(industry_id)) return -1;
85 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
87 Industry *i = ::Industry::Get(industry_id);
89 auto it = i->GetCargoAccepted(cargo_id);
90 if (it == std::end(i->accepted)) return -1;
92 return it->waiting;
95 /* static */ SQInteger ScriptIndustry::GetLastMonthProduction(IndustryID industry_id, CargoID cargo_id)
97 if (!IsValidIndustry(industry_id)) return -1;
98 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
100 Industry *i = ::Industry::Get(industry_id);
102 auto it = i->GetCargoProduced(cargo_id);
103 if (it == std::end(i->produced)) return -1;
105 return it->history[LAST_MONTH].production;
108 /* static */ SQInteger ScriptIndustry::GetLastMonthTransported(IndustryID industry_id, CargoID cargo_id)
110 if (!IsValidIndustry(industry_id)) return -1;
111 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
113 Industry *i = ::Industry::Get(industry_id);
115 auto it = i->GetCargoProduced(cargo_id);
116 if (it == std::end(i->produced)) return -1;
118 return it->history[LAST_MONTH].transported;
121 /* static */ SQInteger ScriptIndustry::GetLastMonthTransportedPercentage(IndustryID industry_id, CargoID cargo_id)
123 if (!IsValidIndustry(industry_id)) return -1;
124 if (!ScriptCargo::IsValidCargo(cargo_id)) return -1;
126 Industry *i = ::Industry::Get(industry_id);
128 auto it = i->GetCargoProduced(cargo_id);
129 if (it == std::end(i->produced)) return -1;
131 return ::ToPercent8(it->history[LAST_MONTH].PctTransported());
134 /* static */ TileIndex ScriptIndustry::GetLocation(IndustryID industry_id)
136 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
138 return ::Industry::Get(industry_id)->location.tile;
141 /* static */ SQInteger ScriptIndustry::GetAmountOfStationsAround(IndustryID industry_id)
143 if (!IsValidIndustry(industry_id)) return -1;
145 Industry *ind = ::Industry::Get(industry_id);
146 return ind->stations_near.size();
149 /* static */ SQInteger ScriptIndustry::GetDistanceManhattanToTile(IndustryID industry_id, TileIndex tile)
151 if (!IsValidIndustry(industry_id)) return -1;
153 return ScriptMap::DistanceManhattan(tile, GetLocation(industry_id));
156 /* static */ SQInteger ScriptIndustry::GetDistanceSquareToTile(IndustryID industry_id, TileIndex tile)
158 if (!IsValidIndustry(industry_id)) return -1;
160 return ScriptMap::DistanceSquare(tile, GetLocation(industry_id));
163 /* static */ bool ScriptIndustry::IsBuiltOnWater(IndustryID industry_id)
165 if (!IsValidIndustry(industry_id)) return false;
167 return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
170 /* static */ bool ScriptIndustry::HasHeliport(IndustryID industry_id)
172 if (!IsValidIndustry(industry_id)) return false;
174 return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
177 /* static */ TileIndex ScriptIndustry::GetHeliportLocation(IndustryID industry_id)
179 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
180 if (!HasHeliport(industry_id)) return INVALID_TILE;
182 const Industry *ind = ::Industry::Get(industry_id);
183 for (TileIndex tile_cur : ind->location) {
184 if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
185 return tile_cur;
189 return INVALID_TILE;
192 /* static */ bool ScriptIndustry::HasDock(IndustryID industry_id)
194 if (!IsValidIndustry(industry_id)) return false;
196 return (::GetIndustrySpec(::Industry::Get(industry_id)->type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
199 /* static */ TileIndex ScriptIndustry::GetDockLocation(IndustryID industry_id)
201 if (!IsValidIndustry(industry_id)) return INVALID_TILE;
202 if (!HasDock(industry_id)) return INVALID_TILE;
204 const Industry *ind = ::Industry::Get(industry_id);
205 for (TileIndex tile_cur : ind->location) {
206 if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
207 return tile_cur;
211 return INVALID_TILE;
214 /* static */ IndustryType ScriptIndustry::GetIndustryType(IndustryID industry_id)
216 if (!IsValidIndustry(industry_id)) return INVALID_INDUSTRYTYPE;
218 return ::Industry::Get(industry_id)->type;
221 /* static */ SQInteger ScriptIndustry::GetLastProductionYear(IndustryID industry_id)
223 Industry *i = Industry::GetIfValid(industry_id);
224 if (i == nullptr) return 0;
225 return i->last_prod_year.base();
228 /* static */ ScriptDate::Date ScriptIndustry::GetCargoLastAcceptedDate(IndustryID industry_id, CargoID cargo_type)
230 Industry *i = Industry::GetIfValid(industry_id);
231 if (i == nullptr) return ScriptDate::DATE_INVALID;
233 if (!::IsValidCargoID(cargo_type)) {
234 auto it = std::max_element(std::begin(i->accepted), std::end(i->accepted), [](const auto &a, const auto &b) { return a.last_accepted < b.last_accepted; });
235 return (ScriptDate::Date)it->last_accepted.base();
236 } else {
237 auto it = i->GetCargoAccepted(cargo_type);
238 if (it == std::end(i->accepted)) return ScriptDate::DATE_INVALID;
239 return (ScriptDate::Date)it->last_accepted.base();
243 /* static */ SQInteger ScriptIndustry::GetControlFlags(IndustryID industry_id)
245 Industry *i = Industry::GetIfValid(industry_id);
246 if (i == nullptr) return 0;
247 return i->ctlflags;
250 /* static */ bool ScriptIndustry::SetControlFlags(IndustryID industry_id, SQInteger control_flags)
252 EnforceDeityMode(false);
253 if (!IsValidIndustry(industry_id)) return false;
255 return ScriptObject::Command<CMD_INDUSTRY_SET_FLAGS>::Do(industry_id, (::IndustryControlFlags)control_flags & ::INDCTL_MASK);
258 /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveSupplier(IndustryID industry_id)
260 if (!IsValidIndustry(industry_id)) return ScriptCompany::COMPANY_INVALID;
262 auto company_id = ::Industry::Get(industry_id)->exclusive_supplier;
263 if (!::Company::IsValidID(company_id)) return ScriptCompany::COMPANY_INVALID;
265 return (ScriptCompany::CompanyID)((byte)company_id);
268 /* static */ bool ScriptIndustry::SetExclusiveSupplier(IndustryID industry_id, ScriptCompany::CompanyID company_id)
270 EnforceDeityMode(false);
271 EnforcePrecondition(false, IsValidIndustry(industry_id));
273 auto company = ScriptCompany::ResolveCompanyID(company_id);
274 ::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company);
275 return ScriptObject::Command<CMD_INDUSTRY_SET_EXCLUSIVITY>::Do(industry_id, owner, false);
278 /* static */ ScriptCompany::CompanyID ScriptIndustry::GetExclusiveConsumer(IndustryID industry_id)
280 if (!IsValidIndustry(industry_id)) return ScriptCompany::COMPANY_INVALID;
282 auto company_id = ::Industry::Get(industry_id)->exclusive_consumer;
283 if (!::Company::IsValidID(company_id)) return ScriptCompany::COMPANY_INVALID;
285 return (ScriptCompany::CompanyID)((byte)company_id);
288 /* static */ bool ScriptIndustry::SetExclusiveConsumer(IndustryID industry_id, ScriptCompany::CompanyID company_id)
290 EnforceDeityMode(false);
291 EnforcePrecondition(false, IsValidIndustry(industry_id));
293 auto company = ScriptCompany::ResolveCompanyID(company_id);
294 ::Owner owner = (company == ScriptCompany::COMPANY_INVALID ? ::INVALID_OWNER : (::Owner)company);
295 return ScriptObject::Command<CMD_INDUSTRY_SET_EXCLUSIVITY>::Do(industry_id, owner, true);
298 /* static */ SQInteger ScriptIndustry::GetProductionLevel(IndustryID industry_id)
300 Industry *i = Industry::GetIfValid(industry_id);
301 if (i == nullptr) return 0;
302 return i->prod_level;
305 /* static */ bool ScriptIndustry::SetProductionLevel(IndustryID industry_id, SQInteger prod_level, bool show_news, Text *custom_news)
307 CCountedPtr<Text> counter(custom_news);
309 EnforceDeityMode(false);
310 EnforcePrecondition(false, IsValidIndustry(industry_id));
311 EnforcePrecondition(false, prod_level >= PRODLEVEL_MINIMUM && prod_level <= PRODLEVEL_MAXIMUM);
313 return ScriptObject::Command<CMD_INDUSTRY_SET_PRODUCTION>::Do(industry_id, prod_level, show_news, custom_news != nullptr ? custom_news->GetEncodedText() : std::string{});