aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/map/State.cpp
blob: faf8d1bfc77c5c8ff79435011d7fca8f7d9a25a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "State.hpp"

#include "openvic-simulation/map/Map.hpp"

using namespace OpenVic;

State::State(
   Country const* owner, Province const* capital, Region::provinces_t&& provinces, Province::colony_status_t colony_status
) : owner { owner }, capital { capital }, provinces { std::move(provinces) }, colony_status { colony_status } {}

/* Whether two provinces in the same region should be grouped into the same state or not.
 * (Assumes both provinces non-null.) */
static bool provinces_belong_in_same_state(Province const* lhs, Province const* rhs) {
   return lhs->get_owner() == rhs->get_owner() && lhs->get_colony_status() == rhs->get_colony_status();
}

StateSet::StateSet(Map& map, Region const& new_region) : region { new_region } {
   if (region.get_meta()) {
      Logger::error("Cannot use meta region as state template!");
   }

   std::vector<Region::provinces_t> temp_provinces;

   for (Province const* province : region.get_provinces()) {
      // add to existing state if shared owner & status...
      for (Region::provinces_t& provinces : temp_provinces) {
         if (provinces_belong_in_same_state(provinces[0], province)) {
            provinces.push_back(province);
            // jump to the end of the outer loop, skipping the new state code
            goto loop_end;
         }
      }
      // ...otherwise start a new state
      temp_provinces.push_back({ province });
   loop_end:;
      /* Either the province was added to an existing state and the program jumped to here,
       * or it was used to create a new state and the program arrived here normally. */
   }

   for (Region::provinces_t& provinces : temp_provinces) {
      states.emplace_back(
         /* TODO: capital province logic */
         provinces[0]->get_owner(), provinces[0], std::move(provinces), provinces[0]->get_colony_status()
      );
   }

   // Go back and assign each new state to its provinces.
   for (State const& state : states) {
      for (Province const* province : state.get_provinces()) {
         map.remove_province_const(province)->set_state(&state);
      }
   }
}

StateSet::states_t& StateSet::get_states() {
   return states;
}

void StateManager::generate_states(Map& map) {
   regions.clear();
   regions.reserve(map.get_region_count());
   for(Region const& region : map.get_regions()) {
      if (!region.get_meta()) {
         regions.emplace_back(map, region);
      }
   }
   Logger::info("Generated states.");
}