aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/BuildingInstance.cpp
blob: 597b89ef1900a9ad6530e20fbe8118712d2a73a8 (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
#include "BuildingInstance.hpp"

using namespace OpenVic;

BuildingInstance::BuildingInstance(BuildingType const& new_building_type, level_t new_level)
   : HasIdentifier { new_building_type.get_identifier() }, building_type { new_building_type }, level { new_level },
   expansion_state { ExpansionState::CannotExpand } {}

bool BuildingInstance::_can_expand() const {
   return level < building_type.get_max_level();
}

bool BuildingInstance::expand() {
   if (expansion_state == ExpansionState::CanExpand) {
      expansion_state = ExpansionState::Preparing;
      expansion_progress = 0.0f;
      return true;
   }
   return false;
}

/* REQUIREMENTS:
 * MAP-71, MAP-74, MAP-77
 */
void BuildingInstance::update_state(Date today) {
   switch (expansion_state) {
   case ExpansionState::Preparing:
      start_date = today;
      end_date = start_date + building_type.get_build_time();
      break;
   case ExpansionState::Expanding:
      expansion_progress = static_cast<double>(today - start_date) / static_cast<double>(end_date - start_date);
      break;
   default: expansion_state = _can_expand() ? ExpansionState::CanExpand : ExpansionState::CannotExpand;
   }
}

void BuildingInstance::tick(Date today) {
   if (expansion_state == ExpansionState::Preparing) {
      expansion_state = ExpansionState::Expanding;
   }
   if (expansion_state == ExpansionState::Expanding) {
      if (end_date <= today) {
         level++;
         expansion_state = ExpansionState::CannotExpand;
      }
   }
}