Project Alice
Loading...
Searching...
No Matches
gui_modifier_tooltips.cpp
Go to the documentation of this file.
2#include "dcon_generated.hpp"
3#include "demographics.hpp"
4#include "system_state.hpp"
6#include "text.hpp"
7#include "triggers.hpp"
9
10namespace ui {
11
13 integer,
14 percent,
17};
21 std::string_view name;
22};
23
24static const modifier_display_info province_modifier_names[sys::provincial_mod_offsets::count] = {
25#define MOD_LIST_ELEMENT(num, name, green_is_negative, display_type, locale_name) \
26 modifier_display_info{green_is_negative, display_type, locale_name},
28#undef MOD_LIST_ELEMENT
29};
30static const modifier_display_info national_modifier_names[sys::national_mod_offsets::count] = {
31#define MOD_LIST_ELEMENT(num, name, green_is_negative, display_type, locale_name) \
32 modifier_display_info{green_is_negative, display_type, locale_name},
34#undef MOD_LIST_ELEMENT
35};
36
38 switch(type) {
40 return (value >= 0.f ? "+" : "") + text::prettify(int64_t(value));
42 return (value >= 0.f ? "+" : "") + text::format_percentage(value, 1);
44 return(value >= 0.f ? "+" : "") + text::format_float(value, 2);
46 return (value >= 0.f ? "+" : "") + text::format_float(value, 3);
47 }
48 return "x%";
49}
50
51void modifier_description(sys::state& state, text::layout_base& layout, dcon::modifier_id mid, int32_t indentation) {
52 auto fat_id = dcon::fatten(state.world, mid);
53
54 auto const& prov_def = fat_id.get_province_values();
55 for(uint32_t i = 0; i < prov_def.modifier_definition_size; ++i) {
56 if(!bool(prov_def.offsets[i]))
57 break;
58 auto data = province_modifier_names[prov_def.offsets[i].index()];
59 auto box = text::open_layout_box(layout, indentation);
61 text::add_to_layout_box(state, layout, box, std::string_view{":"}, text::text_color::white);
63 auto color = data.positive_is_green ? (prov_def.values[i] >= 0.f ? text::text_color::green : text::text_color::red)
64 : (prov_def.values[i] >= 0.f ? text::text_color::red : text::text_color::green);
65 text::add_to_layout_box(state, layout, box, format_modifier_value(state, prov_def.values[i], data.type), color);
66 text::close_layout_box(layout, box);
67 }
68
69 auto const& nat_def = fat_id.get_national_values();
70 for(uint32_t i = 0; i < nat_def.modifier_definition_size; ++i) {
71 if(!bool(nat_def.offsets[i]))
72 break;
73 auto data = national_modifier_names[nat_def.offsets[i].index()];
74 auto box = text::open_layout_box(layout, indentation);
76 text::add_to_layout_box(state, layout, box, std::string_view{":"}, text::text_color::white);
78 auto color = data.positive_is_green ? (nat_def.values[i] >= 0.f ? text::text_color::green : text::text_color::red)
79 : (nat_def.values[i] >= 0.f ? text::text_color::red : text::text_color::green);
80 text::add_to_layout_box(state, layout, box, format_modifier_value(state, nat_def.values[i], data.type), color);
81 text::close_layout_box(layout, box);
82 }
83}
84
85void active_single_modifier_description(sys::state& state, text::layout_base& layout, dcon::modifier_id mid, int32_t indentation,
86 bool& header, dcon::national_modifier_value nmid, float scaled) {
87 if(scaled == 0.f)
88 return;
89 auto fat_id = dcon::fatten(state.world, mid);
90 auto const& def = fat_id.get_national_values();
91 for(uint32_t i = 0; i < def.modifier_definition_size; ++i) {
92 if(!bool(def.offsets[i]))
93 break;
94 if(def.offsets[i] != nmid)
95 continue;
96
97 if(!header) {
98 header = true;
99 auto box = text::open_layout_box(layout, 0);
100 text::add_to_layout_box(state, layout, box, text::produce_simple_string(state, national_modifier_names[nmid.index()].name),
102 text::add_to_layout_box(state, layout, box, std::string_view(":"), text::text_color::yellow);
103 text::close_layout_box(layout, box);
104 }
105
106 auto data = national_modifier_names[nmid.index()];
107 auto box = text::open_layout_box(layout, indentation);
109 text::add_to_layout_box(state, layout, box, std::string_view{":"}, text::text_color::white);
111 auto value = def.values[i] * scaled;
112 auto color = data.positive_is_green ? (value >= 0.f ? text::text_color::green : text::text_color::red)
114 text::add_to_layout_box(state, layout, box, format_modifier_value(state, value, data.type), color);
115 text::close_layout_box(layout, box);
116 }
117}
118void active_single_modifier_description(sys::state& state, text::layout_base& layout, dcon::modifier_id mid, int32_t indentation,
119 bool& header, dcon::provincial_modifier_value pmid, float scaled) {
120 if(scaled == 0.f)
121 return;
122 auto fat_id = dcon::fatten(state.world, mid);
123 auto const& def = fat_id.get_province_values();
124 for(uint32_t i = 0; i < def.modifier_definition_size; ++i) {
125 if(!bool(def.offsets[i]))
126 break;
127 if(def.offsets[i] != pmid)
128 continue;
129
130 if(!header) {
131 header = true;
132 auto box = text::open_layout_box(layout, 0);
133 text::add_to_layout_box(state, layout, box, text::produce_simple_string(state, province_modifier_names[pmid.index()].name),
135 text::add_to_layout_box(state, layout, box, std::string_view(":"), text::text_color::yellow);
136 text::close_layout_box(layout, box);
137 }
138
139 auto data = province_modifier_names[pmid.index()];
140 auto box = text::open_layout_box(layout, indentation);
142 text::add_to_layout_box(state, layout, box, std::string_view{":"}, text::text_color::white);
144 auto value = def.values[i] * scaled;
145 auto color = data.positive_is_green ? (value >= 0.f ? text::text_color::green : text::text_color::red)
147 text::add_to_layout_box(state, layout, box, format_modifier_value(state, value, data.type), color);
148 text::close_layout_box(layout, box);
149 }
150}
151
152template<typename T>
153void acting_modifiers_description_province(sys::state& state, text::layout_base& layout, dcon::province_id p, int32_t identation,
154 bool& header, T nmid) {
155 if(state.national_definitions.land_province)
156 active_single_modifier_description(state, layout, state.national_definitions.land_province, identation, header, nmid);
157 for(auto mpr : state.world.province_get_current_modifiers(p))
158 active_single_modifier_description(state, layout, mpr.mod_id, identation, header, nmid);
159 if(auto m = state.world.province_get_terrain(p); m)
160 active_single_modifier_description(state, layout, m, identation, header, nmid);
161 if(auto m = state.world.province_get_climate(p); m)
162 active_single_modifier_description(state, layout, m, identation, header, nmid);
163 if(auto m = state.world.province_get_continent(p); m)
164 active_single_modifier_description(state, layout, m, identation, header, nmid);
165 if(auto c = state.world.province_get_crime(p); c) {
166 if(auto m = state.culture_definitions.crimes[c].modifier; m)
167 active_single_modifier_description(state, layout, m, identation, header, nmid);
168 }
170 if(state.economy_definitions.building_definitions[int32_t(t)].province_modifier) {
171 active_single_modifier_description(state, layout, state.economy_definitions.building_definitions[int32_t(t)].province_modifier, identation,
172 header, nmid, state.world.province_get_building_level(p, uint8_t(t)));
173 }
174 }
175 if(state.national_definitions.infrastructure) {
176 active_single_modifier_description(state, layout, state.national_definitions.infrastructure, identation, header, nmid,
177 state.world.province_get_building_level(p, uint8_t(economy::province_building_type::railroad)) * state.economy_definitions.building_definitions[int32_t(economy::province_building_type::railroad)].infrastructure);
178 }
179 if(state.national_definitions.nationalism) {
180 active_single_modifier_description(state, layout, state.national_definitions.nationalism, identation, header, nmid,
181 (state.world.province_get_is_owner_core(p) ? 1.f : 0.f) * state.world.province_get_nationalism(p));
182 }
183 if(state.national_definitions.non_coastal) {
184 active_single_modifier_description(state, layout, state.national_definitions.non_coastal, identation, header, nmid,
185 !state.world.province_get_is_coast(p) ? 1.f : 0.f);
186 }
187 if(state.national_definitions.coastal) {
188 active_single_modifier_description(state, layout, state.national_definitions.coastal, identation, header, nmid,
189 state.world.province_get_is_coast(p) ? 1.f : 0.f);
190 }
191 if(state.national_definitions.overseas) {
192 active_single_modifier_description(state, layout, state.national_definitions.overseas, identation, header, nmid,
193 province::is_overseas(state, p) ? 1.f : 0.f);
194 }
195 if(state.national_definitions.core) {
196 active_single_modifier_description(state, layout, state.national_definitions.core, identation, header, nmid,
197 state.world.province_get_is_owner_core(p) ? 1.f : 0.f);
198 }
199 if(state.national_definitions.has_siege) {
200 active_single_modifier_description(state, layout, state.national_definitions.has_siege, identation, header, nmid,
202 }
203 if(state.national_definitions.blockaded) {
204 active_single_modifier_description(state, layout, state.national_definitions.blockaded, identation, header, nmid,
205 military::province_is_blockaded(state, p) ? 1.f : 0.f);
206 }
207}
208
209void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::province_id p, int32_t identation,
210 dcon::provincial_modifier_value nmid, bool have_header) {
211 bool header = !have_header;
212 acting_modifiers_description_province(state, layout, p, identation, header, nmid);
213}
214
215void active_modifiers_description(sys::state& state, text::layout_base& layout, dcon::nation_id n, int32_t identation,
216 dcon::national_modifier_value nmid, bool have_header) {
217 bool header = !have_header;
218 if(auto ts = state.world.nation_get_tech_school(n); ts)
219 active_single_modifier_description(state, layout, ts, identation, header, nmid);
220 if(auto nv = state.world.nation_get_national_value(n); nv)
221 active_single_modifier_description(state, layout, nv, identation, header, nmid);
222 for(auto mpr : state.world.nation_get_current_modifiers(n))
223 active_single_modifier_description(state, layout, mpr.mod_id, identation, header, nmid);
224 state.world.for_each_technology([&](dcon::technology_id t) {
225 auto tmod = state.world.technology_get_modifier(t);
226 if(tmod && state.world.nation_get_active_technologies(n, t))
227 active_single_modifier_description(state, layout, tmod, identation, header, nmid);
228 });
229 state.world.for_each_invention([&](dcon::invention_id i) {
230 auto tmod = state.world.invention_get_modifier(i);
231 if(tmod && state.world.nation_get_active_inventions(n, i))
232 active_single_modifier_description(state, layout, tmod, identation, header, nmid);
233 });
234 state.world.for_each_issue([&](dcon::issue_id i) {
235 auto iopt = state.world.nation_get_issues(n, i);
236 auto imod = state.world.issue_option_get_modifier(iopt);
237 if(imod &&
238 (state.world.nation_get_is_civilized(n) || state.world.issue_get_issue_type(i) == uint8_t(culture::issue_type::party)))
239 active_single_modifier_description(state, layout, imod, identation, header, nmid);
240 });
241 if(!state.world.nation_get_is_civilized(n)) {
242 state.world.for_each_reform([&](dcon::reform_id i) {
243 auto iopt = state.world.nation_get_reforms(n, i);
244 auto imod = state.world.reform_option_get_modifier(iopt);
245 if(imod)
246 active_single_modifier_description(state, layout, imod, identation, header, nmid);
247 });
248 }
249
250 auto in_wars = state.world.nation_get_war_participant(n);
251 if(in_wars.begin() != in_wars.end()) {
252 if(state.national_definitions.war)
253 active_single_modifier_description(state, layout, state.national_definitions.war, identation, header, nmid);
254 } else {
255 if(state.national_definitions.peace)
256 active_single_modifier_description(state, layout, state.national_definitions.peace, identation, header, nmid);
257 }
258
259 if(state.national_definitions.badboy) {
260 active_single_modifier_description(state, layout, state.national_definitions.badboy, identation, header, nmid,
261 state.world.nation_get_infamy(n));
262 }
263 if(state.national_definitions.plurality) {
264 active_single_modifier_description(state, layout, state.national_definitions.plurality, identation, header, nmid,
265 state.world.nation_get_plurality(n));
266 }
267 if(state.national_definitions.war_exhaustion) {
268 active_single_modifier_description(state, layout, state.national_definitions.war_exhaustion, identation, header, nmid,
269 state.world.nation_get_war_exhaustion(n));
270 }
271 if(state.national_definitions.average_literacy) {
272 auto total = state.world.nation_get_demographics(n, demographics::total);
273 active_single_modifier_description(state, layout, state.national_definitions.average_literacy, identation, header, nmid,
274 total > 0 ? state.world.nation_get_demographics(n, demographics::literacy) / total : 0.0f);
275 }
276 if(state.national_definitions.total_blockaded) {
277 auto bc = ve::to_float(state.world.nation_get_central_blockaded(n));
278 auto c = ve::to_float(state.world.nation_get_central_ports(n));
279 active_single_modifier_description(state, layout, state.national_definitions.total_blockaded, identation, header, nmid,
280 c > 0.0f ? bc / c : 0.0f);
281 }
282 if(state.national_definitions.total_occupation) {
283 auto nid = fatten(state.world, n);
284 auto cap_continent = nid.get_capital().get_continent();
285 float total = 0.0f;
286 float occupied = 0.0f;
287 for(auto owned : nid.get_province_ownership()) {
288 if(owned.get_province().get_continent() == cap_continent) {
289 total += 1.0f;
290 if(auto c = owned.get_province().get_nation_from_province_control().id; c && c != n) {
291 occupied += 1.0f;
292 }
293 }
294 }
295 active_single_modifier_description(state, layout, state.national_definitions.total_occupation, identation, header, nmid,
296 total > 0.0f ? occupied / total : 0.0f);
297 }
298
299 if(state.world.nation_get_is_civilized(n) == false) {
300 if(state.national_definitions.unciv_nation)
301 active_single_modifier_description(state, layout, state.national_definitions.unciv_nation, identation, header, nmid);
302 } else if(nations::is_great_power(state, n)) {
303 if(state.national_definitions.great_power)
304 active_single_modifier_description(state, layout, state.national_definitions.great_power, identation, header, nmid);
305 } else if(state.world.nation_get_rank(n) <= uint16_t(state.defines.colonial_rank)) {
306 if(state.national_definitions.second_power)
307 active_single_modifier_description(state, layout, state.national_definitions.second_power, identation, header, nmid);
308 } else {
309 if(state.national_definitions.civ_nation)
310 active_single_modifier_description(state, layout, state.national_definitions.civ_nation, identation, header, nmid);
311 }
312
313 if(state.national_definitions.disarming) {
314 if(bool(state.world.nation_get_disarmed_until(n)) && state.world.nation_get_disarmed_until(n) > state.current_date)
315 active_single_modifier_description(state, layout, state.national_definitions.disarming, identation, header, nmid);
316 }
317 if(state.national_definitions.in_bankrupcy) {
318 if(bool(state.world.nation_get_is_bankrupt(n)))
319 active_single_modifier_description(state, layout, state.national_definitions.in_bankrupcy, identation, header, nmid);
320 }
321 // TODO: debt
322
323 for(auto tm : state.national_definitions.triggered_modifiers) {
324 if(tm.trigger_condition && tm.linked_modifier) {
325 auto trigger_condition_satisfied =
326 trigger::evaluate(state, tm.trigger_condition, trigger::to_generic(n), trigger::to_generic(n), 0);
327 if(trigger_condition_satisfied)
328 active_single_modifier_description(state, layout, tm.linked_modifier, identation, header, nmid);
329 }
330 }
331
332 // Provinces of this nation
333 for(auto pc : state.world.nation_get_province_ownership_as_nation(n)) {
334 auto p = pc.get_province().id;
335 acting_modifiers_description_province<dcon::national_modifier_value>(state, layout, p, identation, header, nmid);
336 }
337}
338
339} // namespace ui
#define MOD_NAT_LIST
Definition: modifiers.hpp:64
#define MOD_PROV_LIST
Definition: modifiers.hpp:9
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
constexpr dcon::demographics_key total(0)
constexpr dcon::demographics_key literacy(5)
province_building_type
Definition: constants.hpp:578
bool province_is_under_siege(sys::state const &state, dcon::province_id ids)
Definition: military.cpp:453
bool province_is_blockaded(sys::state const &state, dcon::province_id ids)
Definition: military.cpp:422
bool is_great_power(sys::state const &state, dcon::nation_id id)
Definition: nations.cpp:503
bool is_overseas(sys::state const &state, dcon::province_id ids)
Definition: province.cpp:18
MOD_NAT_LIST constexpr uint32_t count
Definition: modifiers.hpp:215
MOD_PROV_LIST constexpr uint32_t count
Definition: modifiers.hpp:207
void add_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, embedded_flag ico)
Definition: text.cpp:1165
layout_box open_layout_box(layout_base &dest, int32_t indent)
Definition: text.cpp:1799
std::string prettify(int64_t num)
Definition: text.cpp:762
std::string format_float(float num, size_t digits)
Definition: text.cpp:981
std::string produce_simple_string(sys::state const &state, dcon::text_key id)
Definition: text.cpp:617
std::string format_percentage(float num, size_t digits)
Definition: text.cpp:977
void add_space_to_layout_box(sys::state &state, layout_base &dest, layout_box &box)
Definition: text.cpp:1788
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:5810
void active_single_modifier_description(sys::state &state, text::layout_base &layout, dcon::modifier_id mid, int32_t indentation, bool &header, dcon::national_modifier_value nmid, float scaled)
void modifier_description(sys::state &state, text::layout_base &layout, dcon::modifier_id mid, int32_t indentation=0)
void acting_modifiers_description_province(sys::state &state, text::layout_base &layout, dcon::province_id p, int32_t identation, bool &header, T nmid)
std::string format_modifier_value(sys::state &state, float value, modifier_display_type type)
void active_modifiers_description(sys::state &state, text::layout_base &layout, dcon::nation_id n, int32_t identation, dcon::national_modifier_value nmid, bool header)
float to_float(int32_t a)
uint uint32_t
uchar uint8_t