Project Alice
Loading...
Searching...
No Matches
econ_parsing.cpp
Go to the documentation of this file.
2#include "text.hpp"
3
4namespace parsers {
5
6void make_good(std::string_view name, token_generator& gen, error_handler& err, good_group_context& context) {
7 dcon::commodity_id new_id = context.outer_context.state.world.create_commodity();
8 auto name_id = text::find_or_add_key(context.outer_context.state, name, false);
9 context.outer_context.state.world.commodity_set_name(new_id, name_id);
10 context.outer_context.state.world.commodity_set_commodity_group(new_id, uint8_t(context.group));
11 context.outer_context.state.world.commodity_set_is_available_from_start(new_id, true);
12
13 context.outer_context.map_of_commodity_names.insert_or_assign(std::string(name), new_id);
14 good_context new_context{new_id, context.outer_context};
15 parse_good(gen, err, new_context);
16}
17void make_goods_group(std::string_view name, token_generator& gen, error_handler& err, scenario_building_context& context) {
18 if(name == "military_goods") {
20 parse_goods_group(gen, err, new_context);
21 } else if(name == "raw_material_goods") {
23 parse_goods_group(gen, err, new_context);
24 } else if(name == "industrial_goods") {
26 parse_goods_group(gen, err, new_context);
27 } else if(name == "consumer_goods") {
29 parse_goods_group(gen, err, new_context);
30 // Non-vanilla
31 } else if(name == "industrial_and_consumer_goods") {
33 parse_goods_group(gen, err, new_context);
34 } else {
35 err.accumulated_errors += "Unknown goods category " + std::string(name) + " found in " + err.file_name + "\n";
37 parse_goods_group(gen, err, new_context);
38 }
39}
40
41void building_definition::type(association_type, std::string_view value, error_handler& err, int32_t line, scenario_building_context& context) {
42 if(is_fixed_token_ci(value.data(), value.data() + value.length(), "factory")) {
44 } else if(is_fixed_token_ci(value.data(), value.data() + value.length(), "province_selector")) {
45 // Not a building, all data for it is discarded!
47 } else if(is_fixed_token_ci(value.data(), value.data() + value.length(), "immigrator")
48 || is_fixed_token_ci(value.data(), value.data() + value.length(), "immigrator_selector")
49 || is_fixed_token_ci(value.data(), value.data() + value.length(), "province_immigrator")) {
50 // Not a building, all data for it is discarded!
52 } else if(is_fixed_token_ci(value.data(), value.data() + value.length(), "infrastructure")) {
54 } else {
56 if(std::string(value) == economy::province_building_type_get_name(t)) {
57 stored_type = t;
58 return;
59 }
60 }
62 "Unknown building type " + std::string(value) + " in file " + err.file_name + " line " + std::to_string(line) + "\n";
63 }
64}
65
66void building_file::result(std::string_view name, building_definition&& res, error_handler& err, int32_t line,
68 // no, this messes things up
69 // res.goods_cost.data.safe_get(dcon::commodity_id(0)) = float(res.cost);
70 if(res.stored_type == economy::province_building_type::factory) {
71 auto factory_id = context.state.world.create_factory_type();
72 context.map_of_factory_names.insert_or_assign(std::string(name), factory_id);
73
74 auto desc_id = text::find_or_add_key(context.state, std::string(name) + "_desc", false);
75
76 context.state.world.factory_type_set_name(factory_id, text::find_or_add_key(context.state, name, false));
77 context.state.world.factory_type_set_description(factory_id, desc_id);
78 context.state.world.factory_type_set_construction_time(factory_id, int16_t(res.time));
79 context.state.world.factory_type_set_is_available_from_start(factory_id, res.default_enabled);
80 /*for(uint32_t i = context.state.world.commodity_size(); i-- > 0; ) {
81 dcon::commodity_id cid = dcon::commodity_id(dcon::commodity_id::value_base_t(i));
82 context.state.world.factory_type_set_construction_costs(factory_id, cid, res.goods_cost.data[cid]);
83 }*/
84 uint32_t added = 0;
85 auto& cc = context.state.world.factory_type_get_construction_costs(factory_id);
86 context.state.world.for_each_commodity([&](dcon::commodity_id id) {
87 auto amount = res.goods_cost.data.safe_get(id);
88 if(amount > 0) {
90 err.accumulated_errors += "Too many factory cost goods in " + std::string(name) + " (" + err.file_name + ")\n";
91 } else {
92 cc.commodity_type[added] = id;
93 cc.commodity_amounts[added] = amount;
94 ++added;
95 }
96 }
97 });
98 if(res.production_type.length() > 0) {
99 context.map_of_production_types.insert_or_assign(std::string(res.production_type), factory_id);
100 }
101 } else if(res.stored_type == economy::province_building_type::province_selector) {
102 // Not a building per se, rather we will do what the modders intended this to be!
103 context.state.economy_definitions.selector_modifier = context.state.world.create_modifier();
104 } else if(res.stored_type == economy::province_building_type::province_immigrator) {
105 // Not a building per se, rather we will do what the modders intended this to be!
106 context.state.economy_definitions.immigrator_modifier = context.state.world.create_modifier();
107 } else {
108 auto t = res.stored_type;
109
110 context.state.economy_definitions.building_definitions[int32_t(t)].defined = true; // Is defined now!
111
112 for(uint32_t i = 0; i < 8 && i < res.colonial_points.data.size(); ++i)
113 context.state.economy_definitions.building_definitions[int32_t(t)].colonial_points[i] = res.colonial_points.data[i];
114 context.state.economy_definitions.building_definitions[int32_t(t)].colonial_range = res.colonial_range;
115
116 uint32_t added = 0;
117 context.state.world.for_each_commodity([&](dcon::commodity_id id) {
118 auto amount = res.goods_cost.data.safe_get(id);
119 if(amount > 0) {
121 err.accumulated_warnings += "Too many special building cost goods in " + std::string(name) + " (" + err.file_name + ")\n";
122 } else {
123 context.state.economy_definitions.building_definitions[int32_t(t)].cost.commodity_type[added] = id;
124 context.state.economy_definitions.building_definitions[int32_t(t)].cost.commodity_amounts[added] = amount;
125 ++added;
126 }
127 }
128 });
129 context.state.economy_definitions.building_definitions[int32_t(t)].infrastructure = res.infrastructure;
130 context.state.economy_definitions.building_definitions[int32_t(t)].max_level = res.max_level;
131 context.state.economy_definitions.building_definitions[int32_t(t)].time = res.time;
132 context.state.economy_definitions.building_definitions[int32_t(t)].name = text::find_or_add_key(context.state, name, false);
133 if(res.next_to_add_p != 0) {
134 context.state.economy_definitions.building_definitions[int32_t(t)].province_modifier = context.state.world.create_modifier();
135 context.state.world.modifier_set_province_values(context.state.economy_definitions.building_definitions[int32_t(t)].province_modifier,
136 res.peek_province_mod());
137 context.state.world.modifier_set_national_values(context.state.economy_definitions.building_definitions[int32_t(t)].province_modifier,
138 res.peek_national_mod());
139 context.state.world.modifier_set_icon(context.state.economy_definitions.building_definitions[int32_t(t)].province_modifier,
140 uint8_t(res.icon_index));
141 context.state.world.modifier_set_name(context.state.economy_definitions.building_definitions[int32_t(t)].province_modifier,
142 context.state.economy_definitions.building_definitions[int32_t(t)].name);
143 }
144 }
145}
146
150 return make_trigger(gen, err, t_context);
151}
152
153void make_production_type(std::string_view name, token_generator& gen, error_handler& err, production_context& context) {
154 auto pt = parse_production_type(gen, err, context);
155
156 if(!bool(pt.output_goods_)) { // must be a template
157 if(pt.type_ == production_type_enum::factory && !context.found_worker_types && pt.employees.employees.size() >= 2) {
158 context.outer_context.state.culture_definitions.primary_factory_worker = pt.employees.employees[0].type;
159 context.outer_context.state.culture_definitions.secondary_factory_worker = pt.employees.employees[1].type;
160 context.outer_context.state.economy_definitions.craftsmen_fraction = pt.employees.employees[0].amount;
161 context.found_worker_types = true;
162 }
163 if(pt.type_ == production_type_enum::rgo && bool(pt.owner.type)) {
164 context.outer_context.state.culture_definitions.aristocrat = pt.owner.type;
165 for(auto& wt : pt.employees.employees) {
166 if(wt.type != context.outer_context.state.culture_definitions.slaves) {
167 context.outer_context.state.world.pop_type_set_is_paid_rgo_worker(wt.type, true);
168 }
169 }
170 }
171 context.templates.insert_or_assign(std::string(name), std::move(pt));
172 } else if(pt.type_ == production_type_enum::rgo) {
173 context.outer_context.state.world.commodity_set_is_mine(pt.output_goods_, pt.mine);
174 context.outer_context.state.world.commodity_set_rgo_amount(pt.output_goods_, pt.value);
175 context.outer_context.state.world.commodity_set_rgo_workforce(pt.output_goods_, pt.workforce);
176 } else if(pt.type_ == production_type_enum::artisan) {
178 uint32_t added = 0;
179 context.outer_context.state.world.for_each_commodity([&](dcon::commodity_id id) {
180 auto amount = pt.input_goods.data.safe_get(id);
181 if(amount > 0) {
183 err.accumulated_errors += "Too many artisan input goods in" + std::string(name) + " (" + err.file_name + ")\n";
184 } else {
185 cset.commodity_type[added] = id;
186 cset.commodity_amounts[added] = amount;
187 ++added;
188 }
189 }
190 });
191 context.outer_context.state.world.commodity_set_artisan_inputs(pt.output_goods_, cset);
192 context.outer_context.state.world.commodity_set_artisan_output_amount(pt.output_goods_, pt.value);
193 } else if(pt.type_ == production_type_enum::factory) {
194 if(auto it = context.outer_context.map_of_production_types.find(std::string(name));
195 it != context.outer_context.map_of_production_types.end()) {
196 auto factory_handle = fatten(context.outer_context.state.world, it->second);
197
199 uint32_t added = 0;
200 context.outer_context.state.world.for_each_commodity([&](dcon::commodity_id id) {
201 auto amount = pt.input_goods.data.safe_get(id);
202 if(amount > 0) {
204 err.accumulated_errors += "Too many factory input goods in " + std::string(name) + " (" + err.file_name + ")\n";
205 } else {
206 cset.commodity_type[added] = id;
207 cset.commodity_amounts[added] = amount;
208 ++added;
209 }
210 }
211 });
212
214 uint32_t sm_added = 0;
215 context.outer_context.state.world.for_each_commodity([&](dcon::commodity_id id) {
216 auto amount = pt.efficiency.data.safe_get(id);
217 if(amount > 0) {
219 err.accumulated_errors += "Too many factory efficiency goods in " + std::string(name) + " (" + err.file_name + ")\n";
220 } else {
221 sm_cset.commodity_type[sm_added] = id;
222 sm_cset.commodity_amounts[sm_added] = amount;
223 ++sm_added;
224 }
225 }
226 });
227
228 factory_handle.set_inputs(cset);
229 factory_handle.set_efficiency_inputs(sm_cset);
230 factory_handle.set_output(pt.output_goods_);
231 factory_handle.set_output_amount(pt.value);
232 factory_handle.set_is_coastal(pt.is_coastal);
233 factory_handle.set_base_workforce(pt.workforce);
234
235 if(pt.bonuses.size() >= 1) {
236 factory_handle.set_bonus_1_amount(pt.bonuses[0].value);
237 factory_handle.set_bonus_1_trigger(pt.bonuses[0].trigger);
238 }
239 if(pt.bonuses.size() >= 2) {
240 factory_handle.set_bonus_2_amount(pt.bonuses[1].value);
241 factory_handle.set_bonus_2_trigger(pt.bonuses[1].trigger);
242 }
243 if(pt.bonuses.size() >= 3) {
244 factory_handle.set_bonus_3_amount(pt.bonuses[2].value);
245 factory_handle.set_bonus_3_trigger(pt.bonuses[2].trigger);
246 }
247 if(pt.bonuses.size() >= 4) {
248 err.accumulated_errors += "Too many factory bonuses (" + std::to_string(pt.bonuses.size()) + ") for " + std::string(name) + " (" + err.file_name + ")\n";
249 }
250 } else {
251 err.accumulated_warnings += "Unused factory production type: " + std::string(name) + "\n";
252 }
253 }
254}
255
257 return parse_commodity_array(gen, err, context.outer_context);
258}
259
260} // namespace parsers
std::string accumulated_errors
Definition: parsers.hpp:62
std::string accumulated_warnings
Definition: parsers.hpp:63
std::string file_name
Definition: parsers.hpp:61
std::string_view province_building_type_get_name(economy::province_building_type v)
Definition: economy.hpp:56
province_building_type
Definition: constants.hpp:578
dcon::trigger_key make_trigger(token_generator &gen, error_handler &err, trigger_building_context &context)
association_type
Definition: parsers.hpp:28
void make_goods_group(std::string_view name, token_generator &gen, error_handler &err, scenario_building_context &context)
dcon::trigger_key make_production_bonus_trigger(token_generator &gen, error_handler &err, production_context &context)
void make_good(std::string_view name, token_generator &gen, error_handler &err, good_group_context &context)
Definition: econ_parsing.cpp:6
bool is_fixed_token_ci(char const *start, char const *end, char const (&t)[N])
Definition: parsers.hpp:275
void make_production_type(std::string_view name, token_generator &gen, error_handler &err, production_context &context)
commodity_array make_prod_commodity_array(token_generator &gen, error_handler &err, production_context &context)
dcon::text_key find_or_add_key(sys::state &state, std::string_view key, bool as_unicode)
Definition: text.cpp:695
uint uint32_t
uchar uint8_t
dcon::pop_type_id secondary_factory_worker
Definition: culture.hpp:149
dcon::pop_type_id primary_factory_worker
Definition: culture.hpp:148
dcon::pop_type_id aristocrat
Definition: culture.hpp:146
dcon::pop_type_id slaves
Definition: culture.hpp:144
dcon::modifier_id province_modifier
Definition: economy.hpp:23
economy::commodity_set cost
Definition: economy.hpp:15
float commodity_amounts[set_size]
dcon::commodity_id commodity_type[set_size]
static constexpr uint32_t set_size
dcon::modifier_id selector_modifier
Definition: economy.hpp:101
building_information building_definitions[max_building_types]
Definition: economy.hpp:99
dcon::modifier_id immigrator_modifier
Definition: economy.hpp:102
static constexpr uint32_t set_size
dcon::commodity_id commodity_type[set_size]
void type(association_type, std::string_view value, error_handler &err, int32_t line, scenario_building_context &context)
economy::province_building_type stored_type
void result(std::string_view name, building_definition &&res, error_handler &err, int32_t line, scenario_building_context &context)
scenario_building_context & outer_context
scenario_building_context & outer_context
ankerl::unordered_dense::map< std::string, production_type > templates
ankerl::unordered_dense::map< std::string, dcon::factory_type_id > map_of_production_types
ankerl::unordered_dense::map< std::string, dcon::commodity_id > map_of_commodity_names
ankerl::unordered_dense::map< std::string, dcon::factory_type_id > map_of_factory_names
culture::global_cultural_state culture_definitions
dcon::data_container world
economy::global_economy_state economy_definitions