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  */
7 
8 /** @file script_industrytype.cpp Implementation of ScriptIndustryType. */
9 
10 #include "../../stdafx.h"
11 #include "script_industrytype.hpp"
12 #include "script_map.hpp"
13 #include "script_error.hpp"
14 #include "../../strings_func.h"
15 #include "../../industry.h"
16 #include "../../newgrf_industries.h"
17 #include "../../core/random_func.hpp"
18 
19 #include "../../safeguards.h"
20 
IsValidIndustryType(IndustryType industry_type)21 /* static */ bool ScriptIndustryType::IsValidIndustryType(IndustryType industry_type)
22 {
23 	if (industry_type >= NUM_INDUSTRYTYPES) return false;
24 
25 	return ::GetIndustrySpec(industry_type)->enabled;
26 }
27 
IsRawIndustry(IndustryType industry_type)28 /* static */ bool ScriptIndustryType::IsRawIndustry(IndustryType industry_type)
29 {
30 	if (!IsValidIndustryType(industry_type)) return false;
31 
32 	return ::GetIndustrySpec(industry_type)->IsRawIndustry();
33 }
34 
IsProcessingIndustry(IndustryType industry_type)35 /* static */ bool ScriptIndustryType::IsProcessingIndustry(IndustryType industry_type)
36 {
37 	if (!IsValidIndustryType(industry_type)) return false;
38 
39 	return ::GetIndustrySpec(industry_type)->IsProcessingIndustry();
40 }
41 
ProductionCanIncrease(IndustryType industry_type)42 /* static */ bool ScriptIndustryType::ProductionCanIncrease(IndustryType industry_type)
43 {
44 	if (!IsValidIndustryType(industry_type)) return false;
45 
46 	if (_settings_game.game_creation.landscape != LT_TEMPERATE) return true;
47 	return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_DONT_INCR_PROD) == 0;
48 }
49 
GetConstructionCost(IndustryType industry_type)50 /* static */ Money ScriptIndustryType::GetConstructionCost(IndustryType industry_type)
51 {
52 	if (!IsValidIndustryType(industry_type)) return -1;
53 	if (::GetIndustrySpec(industry_type)->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) return -1;
54 
55 	return ::GetIndustrySpec(industry_type)->GetConstructionCost();
56 }
57 
GetName(IndustryType industry_type)58 /* static */ char *ScriptIndustryType::GetName(IndustryType industry_type)
59 {
60 	if (!IsValidIndustryType(industry_type)) return nullptr;
61 
62 	return GetString(::GetIndustrySpec(industry_type)->name);
63 }
64 
GetProducedCargo(IndustryType industry_type)65 /* static */ ScriptList *ScriptIndustryType::GetProducedCargo(IndustryType industry_type)
66 {
67 	if (!IsValidIndustryType(industry_type)) return nullptr;
68 
69 	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
70 
71 	ScriptList *list = new ScriptList();
72 	for (size_t i = 0; i < lengthof(ins->produced_cargo); i++) {
73 		if (ins->produced_cargo[i] != CT_INVALID) list->AddItem(ins->produced_cargo[i]);
74 	}
75 
76 	return list;
77 }
78 
GetAcceptedCargo(IndustryType industry_type)79 /* static */ ScriptList *ScriptIndustryType::GetAcceptedCargo(IndustryType industry_type)
80 {
81 	if (!IsValidIndustryType(industry_type)) return nullptr;
82 
83 	const IndustrySpec *ins = ::GetIndustrySpec(industry_type);
84 
85 	ScriptList *list = new ScriptList();
86 	for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) {
87 		if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i]);
88 	}
89 
90 	return list;
91 }
92 
CanBuildIndustry(IndustryType industry_type)93 /* static */ bool ScriptIndustryType::CanBuildIndustry(IndustryType industry_type)
94 {
95 	if (!IsValidIndustryType(industry_type)) return false;
96 
97 	const bool deity = ScriptObject::GetCompany() == OWNER_DEITY;
98 	if (::GetIndustryProbabilityCallback(industry_type, deity ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) return false;
99 	if (deity) return true;
100 	if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return true;
101 
102 	/* raw_industry_construction == 1 means "Build as other industries" */
103 	return _settings_game.construction.raw_industry_construction == 1;
104 }
105 
CanProspectIndustry(IndustryType industry_type)106 /* static */ bool ScriptIndustryType::CanProspectIndustry(IndustryType industry_type)
107 {
108 	if (!IsValidIndustryType(industry_type)) return false;
109 
110 	const bool deity = ScriptObject::GetCompany() == OWNER_DEITY;
111 	if (!deity && !::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
112 	if (::GetIndustryProbabilityCallback(industry_type, deity ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) return false;
113 
114 	/* raw_industry_construction == 2 means "prospect" */
115 	return deity || _settings_game.construction.raw_industry_construction == 2;
116 }
117 
BuildIndustry(IndustryType industry_type,TileIndex tile)118 /* static */ bool ScriptIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
119 {
120 	EnforcePrecondition(false, CanBuildIndustry(industry_type));
121 	EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
122 
123 	uint32 seed = ::InteractiveRandom();
124 	uint32 layout_index = ::InteractiveRandomRange((uint32)::GetIndustrySpec(industry_type)->layouts.size());
125 	return ScriptObject::DoCommand(tile, (1 << 16) | (layout_index << 8) | industry_type, seed, CMD_BUILD_INDUSTRY);
126 }
127 
ProspectIndustry(IndustryType industry_type)128 /* static */ bool ScriptIndustryType::ProspectIndustry(IndustryType industry_type)
129 {
130 	EnforcePrecondition(false, CanProspectIndustry(industry_type));
131 
132 	uint32 seed = ::InteractiveRandom();
133 	return ScriptObject::DoCommand(0, industry_type, seed, CMD_BUILD_INDUSTRY);
134 }
135 
IsBuiltOnWater(IndustryType industry_type)136 /* static */ bool ScriptIndustryType::IsBuiltOnWater(IndustryType industry_type)
137 {
138 	if (!IsValidIndustryType(industry_type)) return false;
139 
140 	return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0;
141 }
142 
HasHeliport(IndustryType industry_type)143 /* static */ bool ScriptIndustryType::HasHeliport(IndustryType industry_type)
144 {
145 	if (!IsValidIndustryType(industry_type)) return false;
146 
147 	return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
148 }
149 
HasDock(IndustryType industry_type)150 /* static */ bool ScriptIndustryType::HasDock(IndustryType industry_type)
151 {
152 	if (!IsValidIndustryType(industry_type)) return false;
153 
154 	return (::GetIndustrySpec(industry_type)->behaviour & INDUSTRYBEH_AI_AIRSHIP_ROUTES) != 0;
155 }
156