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/>.
8 /** @file script_industrytype.cpp Implementation of ScriptIndustryType. */
10 #include "../../stdafx.h"
11 #include "script_industrytype.hpp"
12 #include "script_base.hpp"
13 #include "script_map.hpp"
14 #include "script_error.hpp"
15 #include "../../strings_func.h"
16 #include "../../industry.h"
17 #include "../../newgrf_industries.h"
18 #include "../../core/random_func.hpp"
19 #include "../../industry_cmd.h"
21 #include "../../safeguards.h"
23 /* static */ bool ScriptIndustryType::IsValidIndustryType(IndustryType industry_type
)
25 if (industry_type
>= NUM_INDUSTRYTYPES
) return false;
27 return ::GetIndustrySpec(industry_type
)->enabled
;
30 /* static */ bool ScriptIndustryType::IsRawIndustry(IndustryType industry_type
)
32 if (!IsValidIndustryType(industry_type
)) return false;
34 return ::GetIndustrySpec(industry_type
)->IsRawIndustry();
37 /* static */ bool ScriptIndustryType::IsProcessingIndustry(IndustryType industry_type
)
39 if (!IsValidIndustryType(industry_type
)) return false;
41 return ::GetIndustrySpec(industry_type
)->IsProcessingIndustry();
44 /* static */ bool ScriptIndustryType::ProductionCanIncrease(IndustryType industry_type
)
46 if (!IsValidIndustryType(industry_type
)) return false;
48 if (_settings_game
.game_creation
.landscape
!= LT_TEMPERATE
) return true;
49 return (::GetIndustrySpec(industry_type
)->behaviour
& INDUSTRYBEH_DONT_INCR_PROD
) == 0;
52 /* static */ Money
ScriptIndustryType::GetConstructionCost(IndustryType industry_type
)
54 if (!IsValidIndustryType(industry_type
)) return -1;
55 if (::GetIndustrySpec(industry_type
)->IsRawIndustry() && _settings_game
.construction
.raw_industry_construction
== 0) return -1;
57 return ::GetIndustrySpec(industry_type
)->GetConstructionCost();
60 /* static */ std::optional
<std::string
> ScriptIndustryType::GetName(IndustryType industry_type
)
62 if (!IsValidIndustryType(industry_type
)) return std::nullopt
;
64 return GetString(::GetIndustrySpec(industry_type
)->name
);
67 /* static */ ScriptList
*ScriptIndustryType::GetProducedCargo(IndustryType industry_type
)
69 if (!IsValidIndustryType(industry_type
)) return nullptr;
71 const IndustrySpec
*ins
= ::GetIndustrySpec(industry_type
);
73 ScriptList
*list
= new ScriptList();
74 for (const CargoID
&c
: ins
->produced_cargo
) {
75 if (::IsValidCargoID(c
)) list
->AddItem(c
);
81 /* static */ ScriptList
*ScriptIndustryType::GetAcceptedCargo(IndustryType industry_type
)
83 if (!IsValidIndustryType(industry_type
)) return nullptr;
85 const IndustrySpec
*ins
= ::GetIndustrySpec(industry_type
);
87 ScriptList
*list
= new ScriptList();
88 for (const CargoID
&c
: ins
->accepts_cargo
) {
89 if (::IsValidCargoID(c
)) list
->AddItem(c
);
95 /* static */ bool ScriptIndustryType::CanBuildIndustry(IndustryType industry_type
)
97 if (!IsValidIndustryType(industry_type
)) return false;
99 const bool deity
= ScriptCompanyMode::IsDeity();
100 if (::GetIndustryProbabilityCallback(industry_type
, deity
? IACT_RANDOMCREATION
: IACT_USERCREATION
, 1) == 0) return false;
101 if (deity
) return true;
102 if (!::GetIndustrySpec(industry_type
)->IsRawIndustry()) return true;
104 /* raw_industry_construction == 1 means "Build as other industries" */
105 return _settings_game
.construction
.raw_industry_construction
== 1;
108 /* static */ bool ScriptIndustryType::CanProspectIndustry(IndustryType industry_type
)
110 if (!IsValidIndustryType(industry_type
)) return false;
112 const bool deity
= ScriptCompanyMode::IsDeity();
113 if (!deity
&& !::GetIndustrySpec(industry_type
)->IsRawIndustry()) return false;
114 if (::GetIndustryProbabilityCallback(industry_type
, deity
? IACT_RANDOMCREATION
: IACT_USERCREATION
, 1) == 0) return false;
116 /* raw_industry_construction == 2 means "prospect" */
117 return deity
|| _settings_game
.construction
.raw_industry_construction
== 2;
120 /* static */ bool ScriptIndustryType::BuildIndustry(IndustryType industry_type
, TileIndex tile
)
122 EnforceDeityOrCompanyModeValid(false);
123 EnforcePrecondition(false, CanBuildIndustry(industry_type
));
124 EnforcePrecondition(false, ScriptMap::IsValidTile(tile
));
126 uint32_t seed
= ScriptBase::Rand();
127 uint32_t layout_index
= ScriptBase::RandRange((uint32_t)::GetIndustrySpec(industry_type
)->layouts
.size());
128 return ScriptObject::Command
<CMD_BUILD_INDUSTRY
>::Do(tile
, industry_type
, layout_index
, true, seed
);
131 /* static */ bool ScriptIndustryType::ProspectIndustry(IndustryType industry_type
)
133 EnforceDeityOrCompanyModeValid(false);
134 EnforcePrecondition(false, CanProspectIndustry(industry_type
));
136 uint32_t seed
= ScriptBase::Rand();
137 return ScriptObject::Command
<CMD_BUILD_INDUSTRY
>::Do(TileIndex
{}, industry_type
, 0, false, seed
);
140 /* static */ bool ScriptIndustryType::IsBuiltOnWater(IndustryType industry_type
)
142 if (!IsValidIndustryType(industry_type
)) return false;
144 return (::GetIndustrySpec(industry_type
)->behaviour
& INDUSTRYBEH_BUILT_ONWATER
) != 0;
147 /* static */ bool ScriptIndustryType::HasHeliport(IndustryType industry_type
)
149 if (!IsValidIndustryType(industry_type
)) return false;
151 return (::GetIndustrySpec(industry_type
)->behaviour
& INDUSTRYBEH_AI_AIRSHIP_ROUTES
) != 0;
154 /* static */ bool ScriptIndustryType::HasDock(IndustryType industry_type
)
156 if (!IsValidIndustryType(industry_type
)) return false;
158 return (::GetIndustrySpec(industry_type
)->behaviour
& INDUSTRYBEH_AI_AIRSHIP_ROUTES
) != 0;
161 /* static */ IndustryType
ScriptIndustryType::ResolveNewGRFID(SQInteger grfid
, SQInteger grf_local_id
)
163 EnforcePrecondition(IT_INVALID
, IsInsideBS(grf_local_id
, 0x00, NUM_INDUSTRYTYPES_PER_GRF
));
165 grfid
= BSWAP32(GB(grfid
, 0, 32)); // Match people's expectations.
166 return _industry_mngr
.GetID(grf_local_id
, grfid
);