Project Alice
Loading...
Searching...
No Matches
gui_build_factory_window.hpp
Go to the documentation of this file.
1#pragma once
2
5#include "ai.hpp"
6#include "triggers.hpp"
7
8namespace ui {
9
11public:
14 }
15
16 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
17 text::add_line(state, contents, "production_close_tooltip");
18 }
19};
20
22public:
23 void on_update(sys::state& state) noexcept override {
24 auto sid = retrieve<dcon::state_instance_id>(state, parent);
25 auto content = retrieve<dcon::factory_type_id>(state, parent);
26 disabled = !command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
27 }
28 void button_action(sys::state& state) noexcept override {
29 auto sid = retrieve<dcon::state_instance_id>(state, parent);
30 auto content = retrieve<dcon::factory_type_id>(state, parent);
31 command::begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
32 if(parent) parent->set_visible(state, false);
33 }
36 }
37 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
38 text::add_line(state, contents, "shift_to_hold_open");
39 }
40 void button_shift_action(sys::state& state) noexcept override {
41 auto sid = retrieve<dcon::state_instance_id>(state, parent);
42 auto content = retrieve<dcon::factory_type_id>(state, parent);
43 command::begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
44 }
45 void button_shift_right_action(sys::state& state) noexcept override {
46 auto content = retrieve<dcon::factory_type_id>(state, parent);
47 auto n = retrieve<dcon::nation_id>(state, parent); //n may be another nation, i.e foreign investment
48 for(const auto s : state.world.nation_get_state_ownership_as_nation(n)) {
49 auto sid = s.get_state();
50 if(command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false)) {
51 command::begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
52 }
53 }
54 }
55};
56
58public:
59 std::string get_text(sys::state& state, dcon::factory_type_id ftid) noexcept {
60 auto fat = dcon::fatten(state.world, ftid);
61 auto name = fat.get_name();
63 }
64
65 void on_update(sys::state& state) noexcept override {
66 auto content = retrieve<dcon::factory_type_id>(state, parent);
67 set_text(state, get_text(state, content));
68 }
69};
70
72public:
73 std::string get_text(sys::state& state, dcon::factory_type_id ftid) noexcept {
74 auto fat = dcon::fatten(state.world, ftid);
75 auto& name = fat.get_construction_costs();
76
77 float factory_mod = state.world.nation_get_modifier_values(state.local_player_nation, sys::national_mod_offsets::factory_cost) + 1.0f;
78 float admin_eff = state.world.nation_get_administrative_efficiency(state.local_player_nation);
79 float admin_cost_factor = (2.0f - admin_eff) * factory_mod;
80
81 auto s = retrieve<dcon::state_instance_id>(state, parent);
82
83 auto total = 0.0f;
84 for(uint32_t i = 0; i < economy::commodity_set::set_size; i++) {
85 auto cid = name.commodity_type[i];
86 if(bool(cid)) {
87 total += economy::price(state, s, cid) * name.commodity_amounts[i] * admin_cost_factor;
88 }
89 } // Credit to leaf for this code :3
90 return text::format_money(total);
91 }
92
93 void on_update(sys::state& state) noexcept override {
94 auto content = retrieve<dcon::factory_type_id>(state, parent);
95 set_text(state, get_text(state, content));
96 }
97};
98
100public:
101 std::string get_text(sys::state& state, dcon::factory_type_id ftid) noexcept {
102 auto fat = dcon::fatten(state.world, ftid);
103 auto name = fat.get_construction_time();
104 return (text::prettify(name) + "d");
105 }
106
107 void on_update(sys::state& state) noexcept override {
108 auto content = retrieve<dcon::factory_type_id>(state, parent);
109 set_text(state, get_text(state, content));
110 }
111};
112
114public:
115 void on_update(sys::state& state) noexcept override {
116 auto sid = retrieve<dcon::state_instance_id>(state, parent);
117 auto content = retrieve<dcon::factory_type_id>(state, parent);
118 disabled = !command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
119 if(retrieve<bool>(state, parent)) {
120 color = sys::pack_color(196, 255, 196);
121 } else {
122 color = sys::pack_color(255, 255, 255);
123 }
124 }
125
126 void button_action(sys::state& state) noexcept override {
127 auto content = retrieve<dcon::factory_type_id>(state, parent);
129 }
130
133 }
134 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
135 if(retrieve<bool>(state, parent)) {
136 text::add_line(state, contents, "alice_recommended_build");
137 }
138 //
139 auto content = retrieve<dcon::factory_type_id>(state, parent);
140 auto sid = retrieve<dcon::state_instance_id>(state, parent);
141 auto n = state.world.state_ownership_get_nation(state.world.state_instance_get_state_ownership(sid));
142 //
143 text::add_line(state, contents, "alice_factory_base_workforce", text::variable_type::x, state.world.factory_type_get_base_workforce(content));
144 //
145 text::add_line(state, contents, "alice_factory_inputs");
146
147 auto s = retrieve<dcon::state_instance_id>(state, parent);
148
149 auto const& cset = state.world.factory_type_get_inputs(content);
150 for(uint32_t i = 0; i < economy::commodity_set::set_size; i++) {
151 if(cset.commodity_type[i] && cset.commodity_amounts[i] > 0.0f) {
152 auto amount = cset.commodity_amounts[i];
153 auto cid = cset.commodity_type[i];
154 auto price = economy::price(state, s, cid);
155
157 text::add_to_substitution_map(m, text::variable_type::name, state.world.commodity_get_name(cid));
161 auto box = text::open_layout_box(contents, 0);
162 text::localised_format_box(state, contents, box, "alice_factory_input_item", m);
163 text::close_layout_box(contents, box);
164 }
165 }
166 //
167 float sum = 0.f;
168 if(auto b1 = state.world.factory_type_get_bonus_1_trigger(content); b1) {
169 text::add_line(state, contents, "alice_factory_bonus", text::variable_type::x, text::fp_four_places{ state.world.factory_type_get_bonus_1_amount(content) });
171 sum -= state.world.factory_type_get_bonus_1_amount(content);
172 }
174 }
175 if(auto b2 = state.world.factory_type_get_bonus_2_trigger(content); b2) {
176 text::add_line(state, contents, "alice_factory_bonus", text::variable_type::x, text::fp_four_places{ state.world.factory_type_get_bonus_2_amount(content) });
178 sum -= state.world.factory_type_get_bonus_2_amount(content);
179 }
181 }
182 if(auto b3 = state.world.factory_type_get_bonus_3_trigger(content); b3) {
183 text::add_line(state, contents, "alice_factory_bonus", text::variable_type::x, text::fp_four_places{ state.world.factory_type_get_bonus_3_amount(content) });
185 sum -= state.world.factory_type_get_bonus_3_amount(content);
186 }
188 }
189 text::add_line(state, contents, "alice_factory_total_bonus", text::variable_type::x, text::fp_four_places{ sum });
190 }
191};
192
193class factory_build_item : public listbox_row_element_base<dcon::factory_type_id> {
194 std::vector<dcon::factory_type_id> desired_types;
195public:
196 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
197 if(name == "bg") {
198 auto ptr = make_element_by_type<factory_build_item_button>(state, id);
199 ptr->base_data.size.x *= 2; // Nudge
200 ptr->base_data.size.x += 42; // Nudge
201 ptr->base_data.size.y += 5; // Nudge
202 return ptr;
203
204 } else if(name == "output") {
205 return make_element_by_type<commodity_image>(state, id);
206
207 } else if(name == "name") {
208 return make_element_by_type<factory_build_output_name_text>(state, id);
209
210 } else if(name == "total_build_cost") {
211 return make_element_by_type<factory_build_cost_text>(state, id);
212
213 } else if(name == "buildtime") {
214 return make_element_by_type<factory_build_time_text>(state, id);
215
216 } else {
217 return nullptr;
218 }
219 }
220
221 void on_update(sys::state& state) noexcept override {
222 desired_types.clear();
223 auto sid = retrieve<dcon::state_instance_id>(state, parent);
224 auto m = state.world.state_instance_get_market_from_local_market(sid);
225 ai::get_desired_factory_types(state, state.local_player_nation, m, desired_types);
226 }
227
228 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
229 if(payload.holds_type<dcon::commodity_id>()) {
230 payload.emplace<dcon::commodity_id>(dcon::fatten(state.world, content).get_output().id);
232 } else if(payload.holds_type<bool>()) {
233 auto sid = retrieve<dcon::state_instance_id>(state, parent);
234 bool is_hl = std::find(desired_types.begin(), desired_types.end(), content) != desired_types.end();
235 is_hl = is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
236 payload.emplace<bool>(is_hl);
238 }
240 }
241};
242
243class factory_build_list : public listbox_element_base<factory_build_item, dcon::factory_type_id> {
244 std::vector<dcon::factory_type_id> desired_types;
245 bool is_highlighted(sys::state& state, dcon::state_instance_id sid, dcon::factory_type_id ftid) {
246 bool is_hl = std::find(desired_types.begin(), desired_types.end(), ftid) != desired_types.end();
247 return is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false);
248 }
249protected:
250 std::string_view get_row_element_name() override {
251 return "new_factory_option";
252 }
253
254public:
255 void on_update(sys::state& state) noexcept override {
256 auto sid = retrieve<dcon::state_instance_id>(state, parent);
257 row_contents.clear();
258 desired_types.clear();
259 auto m = state.world.state_instance_get_market_from_local_market(sid);
260 ai::get_desired_factory_types(state, state.local_player_nation, m, desired_types);
261
262 // First the desired factory types
263 for(const auto ftid : desired_types)
264 if(is_highlighted(state, sid, ftid))
265 row_contents.push_back(ftid);
266 // Then the buildable factories
267 for(const auto ftid : state.world.in_factory_type) {
268 if(command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false) && std::find(desired_types.begin(), desired_types.end(), ftid) == desired_types.end()) {
269 row_contents.push_back(ftid);
270 }
271 }
272 // Then the ones that can't be built
273 for(const auto ftid : state.world.in_factory_type) {
274 if(!command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false)) {
275 row_contents.push_back(ftid);
276 }
277 }
278 update(state);
279 }
280};
281
283public:
284 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
285 auto fat = dcon::fatten(state.world, fid);
286 return text::produce_simple_string(state, fat.get_name());
287 }
288
289 void on_update(sys::state& state) noexcept override {
290 if(parent) {
291 Cyto::Any payload = dcon::factory_type_id{};
292 parent->impl_get(state, payload);
293 auto content = any_cast<dcon::factory_type_id>(payload);
294 set_text(state, get_text(state, content));
295 }
296 }
297};
298
300public:
301 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
302 auto fat = dcon::fatten(state.world, fid);
303 return text::prettify(fat.get_base_workforce());
304 }
305
306 void on_update(sys::state& state) noexcept override {
307 if(parent) {
308 Cyto::Any payload = dcon::factory_type_id{};
309 parent->impl_get(state, payload);
310 auto content = any_cast<dcon::factory_type_id>(payload);
311 set_text(state, get_text(state, content));
312 }
313 }
314};
315
317public:
318 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
319 auto fat = dcon::fatten(state.world, fid);
320 return text::prettify(int64_t(fat.get_output_amount()));
321 }
322
323 void on_update(sys::state& state) noexcept override {
324 if(parent) {
325 Cyto::Any payload = dcon::factory_type_id{};
326 parent->impl_get(state, payload);
327 auto content = any_cast<dcon::factory_type_id>(payload);
328 set_text(state, get_text(state, content));
329 }
330 }
331};
332
334public:
335 std::string get_text(sys::state& state) noexcept {
336 return text::format_money(nations::get_treasury(state, state.local_player_nation));
337 }
338
339 void on_update(sys::state& state) noexcept override {
341 }
342};
343
345public:
346 void on_create(sys::state& state) noexcept override {
348 }
349
350 void on_update(sys::state& state) noexcept override {
351 auto content = retrieve<dcon::factory_type_id>(state, parent);
352 auto fat = dcon::fatten(state.world, content);
355 auto box = text::open_layout_box(layout, 0);
356 text::add_to_layout_box(state, layout, box, fat.get_description());
357 text::close_layout_box(layout, box);
358 }
359};
360
362public:
363 int32_t n = 0;
364
365 void on_update(sys::state& state) noexcept override {
366 auto type = retrieve<dcon::factory_type_id>(state, parent);
367 if(!type) {
368 set_text(state, "");
369 return;
370 }
371 auto& inputs = state.world.factory_type_get_inputs(type);
372 if(n < int32_t(economy::commodity_set::set_size)) {
373 auto amount = inputs.commodity_amounts[n];
374 if(amount > 0) {
375 set_text(state, text::format_float(amount, 2));
376 } else {
377 set_text(state, "");
378 }
379 }
380 }
381};
382
384public:
385 int32_t n = 0;
386 dcon::commodity_id c;
387
388 void on_update(sys::state& state) noexcept override {
389 auto type = retrieve<dcon::factory_type_id>(state, parent);
390 if(!type) {
391 c = dcon::commodity_id{};
392 return;
393 }
394 auto& inputs = state.world.factory_type_get_inputs(type);
395 if(n < int32_t(economy::commodity_set::set_size)) {
396 c = inputs.commodity_type[n];
397 frame = int32_t(state.world.commodity_get_icon(c));
398 }
399 }
400
403 }
404
405 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
406 if(c)
407 text::add_line(state, contents, state.world.commodity_get_name(c));
408 }
409 void render(sys::state& state, int32_t x, int32_t y) noexcept override {
410 if(c)
412 }
413};
414
416private:
417 dcon::factory_type_id factory_to_build{};
418
419public:
420 void on_create(sys::state& state) noexcept override {
422 set_visible(state, false);
423 }
424
425 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
426 if(name == "bg") {
427 return make_element_by_type<draggable_target>(state, id);
428 } else if(name == "state_name") {
429 return make_element_by_type<state_name_text>(state, id);
430 } else if(name == "factory_type") {
431 return make_element_by_type<factory_build_list>(state, id);
432 } else if(name == "output") {
433 return make_element_by_type<commodity_image>(state, id);
434 } else if(name == "building_name") {
435 return make_element_by_type<factory_title>(state, id);
436 } else if(name == "output_amount") {
437 return make_element_by_type<output_amount_text>(state, id);
438 } else if(name == "description_text") {
439 return make_element_by_type<factory_build_description>(state, id);
440 } else if(name == "needed_workers") {
441 return make_element_by_type<simple_text_element_base>(state, id);
442 } else if(name == "needed_workers_count") {
443 return make_element_by_type<needed_workers_count_text>(state, id);
444 } else if(name == "available_workers") {
445 return make_element_by_type<simple_text_element_base>(state, id);
446 } else if(name == "current_funds_label") {
447 return make_element_by_type<factory_current_funds_text>(state, id);
448 } else if(name == "base_price") {
449 return make_element_by_type<simple_text_element_base>(state, id);
450 } else if(name == "input_price") {
451 return make_element_by_type<simple_text_element_base>(state, id);
452 } else if(name == "total_price") {
453 return make_element_by_type<factory_build_cost_text>(state, id);
454 } else if(name == "you_have") {
455 return make_element_by_type<simple_text_element_base>(state, id);
456 } else if(name == "prod_cost") {
457 return make_element_by_type<simple_text_element_base>(state, id);
458 } else if(name == "cancel") {
459 return make_element_by_type<factory_build_cancel_button>(state, id);
460 } else if(name == "build") {
461 return make_element_by_type<factory_build_button>(state, id);
462 } else if(name.substr(0, 6) == "input_" && parsers::is_integer(name.data() + 6, name.data() + name.length())) {
463 auto ptr = make_element_by_type<input_n_image>(state, id);
464 std::from_chars(name.data() + 6, name.data() + name.length(), ptr->n);
465 return ptr;
466 //input_0_amount
467 } else if(name.substr(0, 6) == "input_" && name.substr(name.length() - 7) == "_amount" && parsers::is_integer(name.data() + 6, name.data() + name.length() - 7)) {
468 auto ptr = make_element_by_type<input_n_amout>(state, id);
469 std::from_chars(name.data() + 6, name.data() + name.length() - 7, ptr->n);
470 return ptr;
471 } else {
472 return nullptr;
473 }
474 }
475
476 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
477 if(payload.holds_type<element_selection_wrapper<dcon::factory_type_id>>()) {
478 factory_to_build = any_cast<element_selection_wrapper<dcon::factory_type_id>>(payload).data;
481 } else if(payload.holds_type<dcon::factory_type_id>()) {
482 payload.emplace<dcon::factory_type_id>(factory_to_build);
484 } else if(payload.holds_type<dcon::commodity_id>()) {
485 payload.emplace<dcon::commodity_id>(dcon::fatten(state.world, factory_to_build).get_output());
487 }
489 }
490};
491
492} // namespace ui
void impl_on_update(sys::state &state) noexcept override
element_base * parent
message_result impl_get(sys::state &state, Cyto::Any &payload) noexcept
element_data base_data
void set_visible(sys::state &state, bool vis)
tooltip_behavior has_tooltip(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
void button_shift_action(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
void button_shift_right_action(sys::state &state) noexcept override
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
tooltip_behavior has_tooltip(sys::state &state) noexcept override
std::string get_text(sys::state &state, dcon::factory_type_id ftid) noexcept
void on_update(sys::state &state) noexcept override
void on_create(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
void button_action(sys::state &state) noexcept override
tooltip_behavior has_tooltip(sys::state &state) noexcept override
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_update(sys::state &state) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
void on_update(sys::state &state) noexcept override
std::string_view get_row_element_name() override
void on_update(sys::state &state) noexcept override
std::string get_text(sys::state &state, dcon::factory_type_id ftid) noexcept
std::string get_text(sys::state &state, dcon::factory_type_id ftid) noexcept
void on_update(sys::state &state) noexcept override
std::unique_ptr< element_base > make_child(sys::state &state, std::string_view name, dcon::gui_def_id id) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_create(sys::state &state) noexcept override
std::string get_text(sys::state &state) noexcept
void on_update(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
std::string get_text(sys::state &state, dcon::factory_type_id fid) noexcept
void render(sys::state &state, int32_t x, int32_t y) noexcept override
void on_update(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
void render(sys::state &state, int32_t x, int32_t y) noexcept override
tooltip_behavior has_tooltip(sys::state &state) noexcept override
void update_tooltip(sys::state &state, int32_t x, int32_t y, text::columnar_layout &contents) noexcept override
message_result get(sys::state &state, Cyto::Any &payload) noexcept override
void on_create(sys::state &state) noexcept override
void on_update(sys::state &state) noexcept override
std::string get_text(sys::state &state, dcon::factory_type_id fid) noexcept
void on_update(sys::state &state) noexcept override
std::string get_text(sys::state &state, dcon::factory_type_id fid) noexcept
void set_text(sys::state &state, std::string const &new_text)
void on_create(sys::state &state) noexcept override
void get_desired_factory_types(sys::state &state, dcon::nation_id nid, dcon::market_id mid, std::vector< dcon::factory_type_id > &desired_types)
Definition: ai.cpp:1095
bool can_begin_factory_building_construction(sys::state &state, dcon::nation_id source, dcon::state_instance_id location, dcon::factory_type_id type, bool is_upgrade)
Definition: commands.cpp:512
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
float price(sys::state const &state, dcon::state_instance_id s, dcon::commodity_id c)
Definition: economy.cpp:150
float get_treasury(sys::state &state, dcon::nation_id n)
Definition: nations.cpp:1607
bool is_integer(char const *start, char const *end)
Definition: parsers.cpp:46
uint32_t pack_color(float r, float g, float b)
void add_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, embedded_flag ico)
Definition: text.cpp:1165
std::string format_money(float num)
Definition: text.cpp:1029
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1823
void localised_format_box(sys::state &state, layout_base &dest, layout_box &box, std::string_view key, text::substitution_map const &sub)
Definition: text.cpp:1904
std::string prettify(int64_t num)
Definition: text.cpp:762
endless_layout create_endless_layout(sys::state &state, layout &dest, layout_parameters const &params)
Definition: text.cpp:1100
std::string format_float(float num, size_t digits)
Definition: text.cpp:981
void add_line(sys::state &state, layout_base &dest, dcon::text_key txt, int32_t indent)
Definition: text.cpp:1923
void add_to_substitution_map(substitution_map &mp, variable_type key, substitution value)
Definition: text.cpp:1068
ankerl::unordered_dense::map< uint32_t, substitution > substitution_map
Definition: text.hpp:797
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
void close_layout_box(columnar_layout &dest, layout_box &box)
Definition: text.cpp:1831
int32_t to_generic(dcon::province_id v)
Definition: triggers.hpp:12
bool evaluate(sys::state &state, dcon::trigger_key key, int32_t primary, int32_t this_slot, int32_t from_slot)
Definition: triggers.cpp:5895
tooltip_behavior
void send(sys::state &state, element_base *parent, T value)
void trigger_description(sys::state &state, text::layout_base &layout, dcon::trigger_key k, int32_t primary_slot=-1, int32_t this_slot=-1, int32_t from_slot=-1)
message_result
uint uint32_t
static constexpr uint32_t set_size
Holds important data about the game world, state, and other data regarding windowing,...
union ui::element_data::internal_data data