Project Alice
Loading...
Searching...
No Matches
migration.hpp
Go to the documentation of this file.
1#pragma once
2
3std::vector<uint32_t> migration_map_from(sys::state& state) {
4 uint32_t province_size = state.world.province_size();
5 uint32_t texture_size = province_size + 256 - province_size % 256;
6
7 std::vector<uint32_t> prov_color(texture_size * 2);
8
9 auto selected = state.map_state.selected_province;
10 auto for_nation = state.world.province_get_nation_from_province_ownership(selected);
11 if(for_nation) {
12 float mx = 0.0f;
13 float mn = 0.0f;
14 for(auto p : state.world.nation_get_province_ownership(for_nation)) {
15 auto v = p.get_province().get_daily_net_migration();
16 mn = std::min(mn, v);
17 mx = std::max(mx, v);
18 }
19 if(mx > mn) {
20 for(auto p : state.world.nation_get_province_ownership(for_nation)) {
21 auto v = p.get_province().get_daily_net_migration();
22
23 uint32_t color = ogl::color_gradient((v - mn) / (mx - mn),
24 sys::pack_color(46, 247, 15), // to green
25 sys::pack_color(247, 15, 15) // from red
26 );
27 auto i = province::to_map_id(p.get_province());
28 prov_color[i] = color;
29 prov_color[i + texture_size] = color;
30 }
31 }
32 } else {
33 static auto last_checked_date = sys::date{};
34 static std::vector<float> nation_totals;
35
36 if(state.ui_date != last_checked_date) {
37 last_checked_date = state.ui_date;
38
39 auto sz = state.world.nation_size();
40 if(uint32_t(nation_totals.size()) < sz) {
41 nation_totals.resize(sz);
42 }
43
44 for(uint32_t i = 0; i < sz; ++i) {
45 nation_totals[i] = 0.0f;
46 }
47 float least_neg = -1.0f;
48 float greatest_pos = 1.0f;
49 for(auto p : state.world.in_province) {
50 auto owner = p.get_nation_from_province_ownership();
51 if(owner && uint32_t(owner.id.index()) < sz) {
52 auto v = p.get_daily_net_immigration();
53 nation_totals[owner.id.index()] += v;
54 }
55 }
56 for(uint32_t i = 0; i < sz; ++i) {
57 if(nation_totals[i] < 0.0f)
58 least_neg = std::min(nation_totals[i], least_neg);
59 else
60 greatest_pos = std::max(nation_totals[i], greatest_pos);
61 }
62 for(uint32_t i = 0; i < sz; ++i) {
63 if(nation_totals[i] < 0.0f) {
64 nation_totals[i] = 0.5f - 0.5f * nation_totals[i] / least_neg;
65 } else if(nation_totals[i] > 0.0f) {
66 nation_totals[i] = 0.5f + 0.5f * nation_totals[i] / greatest_pos;
67 } else {
68 nation_totals[i] = 0.5f;
69 }
70 }
71 }
72 for(auto p : state.world.in_province) {
73 auto owner = p.get_nation_from_province_ownership();
74 if(owner && uint32_t(owner.id.index()) < nation_totals.size()) {
75 uint32_t color = ogl::color_gradient(nation_totals[owner.id.index()],
76 sys::pack_color(46, 247, 15), // to green
77 sys::pack_color(247, 15, 15) // from red
78 );
79 auto i = province::to_map_id(p);
80 prov_color[i] = color;
81 prov_color[i + texture_size] = color;
82 }
83 }
84 }
85 return prov_color;
86}
std::vector< uint32_t > migration_map_from(sys::state &state)
Definition: migration.hpp:3
uint32_t color_gradient(float percent, uint32_t top_color, uint32_t bot_color)
Definition: color.hpp:42
constexpr uint16_t to_map_id(dcon::province_id id)
Definition: province.hpp:10
uint32_t pack_color(float r, float g, float b)
uint uint32_t