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 total = 0.0f;
82 for(uint32_t i = 0; i < economy::commodity_set::set_size; i++) {
83 auto cid = name.commodity_type[i];
84 if(bool(cid)) {
85 total += state.world.commodity_get_current_price(cid) * name.commodity_amounts[i] * admin_cost_factor;
86 }
87 } // Credit to leaf for this code :3
88 return text::format_money(total);
89 }
90
91 void on_update(sys::state& state) noexcept override {
92 auto content = retrieve<dcon::factory_type_id>(state, parent);
93 set_text(state, get_text(state, content));
94 }
95};
96
98public:
99 std::string get_text(sys::state& state, dcon::factory_type_id ftid) noexcept {
100 auto fat = dcon::fatten(state.world, ftid);
101 auto name = fat.get_construction_time();
102 return (text::prettify(name) + "d");
103 }
104
105 void on_update(sys::state& state) noexcept override {
106 auto content = retrieve<dcon::factory_type_id>(state, parent);
107 set_text(state, get_text(state, content));
108 }
109};
110
112public:
113 void on_update(sys::state& state) noexcept override {
114 auto sid = retrieve<dcon::state_instance_id>(state, parent);
115 auto content = retrieve<dcon::factory_type_id>(state, parent);
116 disabled = !command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
117 if(retrieve<bool>(state, parent)) {
118 color = sys::pack_color(196, 255, 196);
119 } else {
120 color = sys::pack_color(255, 255, 255);
121 }
122 }
123
124 void button_action(sys::state& state) noexcept override {
125 auto content = retrieve<dcon::factory_type_id>(state, parent);
127 }
128
131 }
132 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
133 if(retrieve<bool>(state, parent)) {
134 text::add_line(state, contents, "alice_recommended_build");
135 }
136 //
137 auto content = retrieve<dcon::factory_type_id>(state, parent);
138 auto sid = retrieve<dcon::state_instance_id>(state, parent);
139 auto n = state.world.state_ownership_get_nation(state.world.state_instance_get_state_ownership(sid));
140 //
141 text::add_line(state, contents, "alice_factory_base_workforce", text::variable_type::x, state.world.factory_type_get_base_workforce(content));
142 //
143 text::add_line(state, contents, "alice_factory_inputs");
144 auto const& cset = state.world.factory_type_get_inputs(content);
145 for(uint32_t i = 0; i < economy::commodity_set::set_size; i++) {
146 if(cset.commodity_type[i] && cset.commodity_amounts[i] > 0.0f) {
147 auto amount = cset.commodity_amounts[i];
148 auto cid = cset.commodity_type[i];
149 auto cost = state.world.commodity_get_current_price(cid);
150
152 text::add_to_substitution_map(m, text::variable_type::name, state.world.commodity_get_name(cid));
156 auto box = text::open_layout_box(contents, 0);
157 text::localised_format_box(state, contents, box, "alice_factory_input_item", m);
158 text::close_layout_box(contents, box);
159 }
160 }
161 //
162 float sum = 0.f;
163 if(auto b1 = state.world.factory_type_get_bonus_1_trigger(content); b1) {
164 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) });
166 sum -= state.world.factory_type_get_bonus_1_amount(content);
167 }
169 }
170 if(auto b2 = state.world.factory_type_get_bonus_2_trigger(content); b2) {
171 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) });
173 sum -= state.world.factory_type_get_bonus_2_amount(content);
174 }
176 }
177 if(auto b3 = state.world.factory_type_get_bonus_3_trigger(content); b3) {
178 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) });
180 sum -= state.world.factory_type_get_bonus_3_amount(content);
181 }
183 }
184 text::add_line(state, contents, "alice_factory_total_bonus", text::variable_type::x, text::fp_four_places{ sum });
185 }
186};
187
188class factory_build_item : public listbox_row_element_base<dcon::factory_type_id> {
189 std::vector<dcon::factory_type_id> desired_types;
190public:
191 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
192 if(name == "bg") {
193 auto ptr = make_element_by_type<factory_build_item_button>(state, id);
194 ptr->base_data.size.x *= 2; // Nudge
195 ptr->base_data.size.x += 42; // Nudge
196 ptr->base_data.size.y += 5; // Nudge
197 return ptr;
198
199 } else if(name == "output") {
200 return make_element_by_type<commodity_image>(state, id);
201
202 } else if(name == "name") {
203 return make_element_by_type<factory_build_output_name_text>(state, id);
204
205 } else if(name == "total_build_cost") {
206 return make_element_by_type<factory_build_cost_text>(state, id);
207
208 } else if(name == "buildtime") {
209 return make_element_by_type<factory_build_time_text>(state, id);
210
211 } else {
212 return nullptr;
213 }
214 }
215
216 void on_update(sys::state& state) noexcept override {
217 desired_types.clear();
218 ai::get_desired_factory_types(state, state.local_player_nation, desired_types);
219 }
220
221 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
222 if(payload.holds_type<dcon::commodity_id>()) {
223 payload.emplace<dcon::commodity_id>(dcon::fatten(state.world, content).get_output().id);
225 } else if(payload.holds_type<bool>()) {
226 auto sid = retrieve<dcon::state_instance_id>(state, parent);
227 bool is_hl = std::find(desired_types.begin(), desired_types.end(), content) != desired_types.end();
228 is_hl = is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, content, false);
229 payload.emplace<bool>(is_hl);
231 }
233 }
234};
235
236class factory_build_list : public listbox_element_base<factory_build_item, dcon::factory_type_id> {
237 std::vector<dcon::factory_type_id> desired_types;
238 bool is_highlighted(sys::state& state, dcon::state_instance_id sid, dcon::factory_type_id ftid) {
239 bool is_hl = std::find(desired_types.begin(), desired_types.end(), ftid) != desired_types.end();
240 return is_hl && command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false);
241 }
242protected:
243 std::string_view get_row_element_name() override {
244 return "new_factory_option";
245 }
246
247public:
248 void on_update(sys::state& state) noexcept override {
249 auto sid = retrieve<dcon::state_instance_id>(state, parent);
250 row_contents.clear();
251 desired_types.clear();
252 ai::get_desired_factory_types(state, state.local_player_nation, desired_types);
253 // First the desired factory types
254 for(const auto ftid : desired_types)
255 if(is_highlighted(state, sid, ftid))
256 row_contents.push_back(ftid);
257 // Then the buildable factories
258 for(const auto ftid : state.world.in_factory_type) {
259 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()) {
260 row_contents.push_back(ftid);
261 }
262 }
263 // Then the ones that can't be built
264 for(const auto ftid : state.world.in_factory_type) {
265 if(!command::can_begin_factory_building_construction(state, state.local_player_nation, sid, ftid, false)) {
266 row_contents.push_back(ftid);
267 }
268 }
269 update(state);
270 }
271};
272
274public:
275 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
276 auto fat = dcon::fatten(state.world, fid);
277 return text::produce_simple_string(state, fat.get_name());
278 }
279
280 void on_update(sys::state& state) noexcept override {
281 if(parent) {
282 Cyto::Any payload = dcon::factory_type_id{};
283 parent->impl_get(state, payload);
284 auto content = any_cast<dcon::factory_type_id>(payload);
285 set_text(state, get_text(state, content));
286 }
287 }
288};
289
291public:
292 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
293 auto fat = dcon::fatten(state.world, fid);
294 return text::prettify(fat.get_base_workforce());
295 }
296
297 void on_update(sys::state& state) noexcept override {
298 if(parent) {
299 Cyto::Any payload = dcon::factory_type_id{};
300 parent->impl_get(state, payload);
301 auto content = any_cast<dcon::factory_type_id>(payload);
302 set_text(state, get_text(state, content));
303 }
304 }
305};
306
308public:
309 std::string get_text(sys::state& state, dcon::factory_type_id fid) noexcept {
310 auto fat = dcon::fatten(state.world, fid);
311 return text::prettify(int64_t(fat.get_output_amount()));
312 }
313
314 void on_update(sys::state& state) noexcept override {
315 if(parent) {
316 Cyto::Any payload = dcon::factory_type_id{};
317 parent->impl_get(state, payload);
318 auto content = any_cast<dcon::factory_type_id>(payload);
319 set_text(state, get_text(state, content));
320 }
321 }
322};
323
325public:
326 std::string get_text(sys::state& state) noexcept {
327 return text::format_money(nations::get_treasury(state, state.local_player_nation));
328 }
329
330 void on_update(sys::state& state) noexcept override {
332 }
333};
334
336public:
337 void on_create(sys::state& state) noexcept override {
339 }
340
341 void on_update(sys::state& state) noexcept override {
342 auto content = retrieve<dcon::factory_type_id>(state, parent);
343 auto fat = dcon::fatten(state.world, content);
346 auto box = text::open_layout_box(layout, 0);
347 text::add_to_layout_box(state, layout, box, fat.get_description());
348 text::close_layout_box(layout, box);
349 }
350};
351
353public:
354 int32_t n = 0;
355
356 void on_update(sys::state& state) noexcept override {
357 auto type = retrieve<dcon::factory_type_id>(state, parent);
358 if(!type) {
359 set_text(state, "");
360 return;
361 }
362 auto& inputs = state.world.factory_type_get_inputs(type);
363 if(n < int32_t(economy::commodity_set::set_size)) {
364 auto amount = inputs.commodity_amounts[n];
365 if(amount > 0) {
366 set_text(state, text::format_float(amount, 2));
367 } else {
368 set_text(state, "");
369 }
370 }
371 }
372};
373
375public:
376 int32_t n = 0;
377 dcon::commodity_id c;
378
379 void on_update(sys::state& state) noexcept override {
380 auto type = retrieve<dcon::factory_type_id>(state, parent);
381 if(!type) {
382 c = dcon::commodity_id{};
383 return;
384 }
385 auto& inputs = state.world.factory_type_get_inputs(type);
386 if(n < int32_t(economy::commodity_set::set_size)) {
387 c = inputs.commodity_type[n];
388 frame = int32_t(state.world.commodity_get_icon(c));
389 }
390 }
391
394 }
395
396 void update_tooltip(sys::state& state, int32_t x, int32_t y, text::columnar_layout& contents) noexcept override {
397 if(c)
398 text::add_line(state, contents, state.world.commodity_get_name(c));
399 }
400 void render(sys::state& state, int32_t x, int32_t y) noexcept override {
401 if(c)
403 }
404};
405
407private:
408 dcon::factory_type_id factory_to_build{};
409
410public:
411 void on_create(sys::state& state) noexcept override {
413 set_visible(state, false);
414 }
415
416 std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override {
417 if(name == "bg") {
418 return make_element_by_type<draggable_target>(state, id);
419 } else if(name == "state_name") {
420 return make_element_by_type<state_name_text>(state, id);
421 } else if(name == "factory_type") {
422 return make_element_by_type<factory_build_list>(state, id);
423 } else if(name == "output") {
424 return make_element_by_type<commodity_image>(state, id);
425 } else if(name == "building_name") {
426 return make_element_by_type<factory_title>(state, id);
427 } else if(name == "output_amount") {
428 return make_element_by_type<output_amount_text>(state, id);
429 } else if(name == "description_text") {
430 return make_element_by_type<factory_build_description>(state, id);
431 } else if(name == "needed_workers") {
432 return make_element_by_type<simple_text_element_base>(state, id);
433 } else if(name == "needed_workers_count") {
434 return make_element_by_type<needed_workers_count_text>(state, id);
435 } else if(name == "available_workers") {
436 return make_element_by_type<simple_text_element_base>(state, id);
437 } else if(name == "current_funds_label") {
438 return make_element_by_type<factory_current_funds_text>(state, id);
439 } else if(name == "base_price") {
440 return make_element_by_type<simple_text_element_base>(state, id);
441 } else if(name == "input_price") {
442 return make_element_by_type<simple_text_element_base>(state, id);
443 } else if(name == "total_price") {
444 return make_element_by_type<factory_build_cost_text>(state, id);
445 } else if(name == "you_have") {
446 return make_element_by_type<simple_text_element_base>(state, id);
447 } else if(name == "prod_cost") {
448 return make_element_by_type<simple_text_element_base>(state, id);
449 } else if(name == "cancel") {
450 return make_element_by_type<factory_build_cancel_button>(state, id);
451 } else if(name == "build") {
452 return make_element_by_type<factory_build_button>(state, id);
453 } else if(name.substr(0, 6) == "input_" && parsers::is_integer(name.data() + 6, name.data() + name.length())) {
454 auto ptr = make_element_by_type<input_n_image>(state, id);
455 std::from_chars(name.data() + 6, name.data() + name.length(), ptr->n);
456 return ptr;
457 //input_0_amount
458 } else if(name.substr(0, 6) == "input_" && name.substr(name.length() - 7) == "_amount" && parsers::is_integer(name.data() + 6, name.data() + name.length() - 7)) {
459 auto ptr = make_element_by_type<input_n_amout>(state, id);
460 std::from_chars(name.data() + 6, name.data() + name.length() - 7, ptr->n);
461 return ptr;
462 } else {
463 return nullptr;
464 }
465 }
466
467 message_result get(sys::state& state, Cyto::Any& payload) noexcept override {
468 if(payload.holds_type<element_selection_wrapper<dcon::factory_type_id>>()) {
469 factory_to_build = any_cast<element_selection_wrapper<dcon::factory_type_id>>(payload).data;
472 } else if(payload.holds_type<dcon::factory_type_id>()) {
473 payload.emplace<dcon::factory_type_id>(factory_to_build);
475 } else if(payload.holds_type<dcon::commodity_id>()) {
476 payload.emplace<dcon::commodity_id>(dcon::fatten(state.world, factory_to_build).get_output());
478 }
480 }
481};
482
483} // 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, std::vector< dcon::factory_type_id > &desired_types)
Definition: ai.cpp:902
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:491
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
float get_treasury(sys::state &state, dcon::nation_id n)
Definition: nations.cpp:1042
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:1799
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:1880
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:1899
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:794
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:1807
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:5865
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
union ui::element_data::internal_data data