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

using namespace OpenVic;

BuildingInstance::BuildingInstance(BuildingType const& new_building_type, level_t new_level)
  : HasIdentifier { new_building_type },
   building_type { new_building_type },
   level { new_level },
   expansion_state { ExpansionState::CannotExpand },
   start_date {},
   end_date {},
   expansion_progress { 0.0f } {}

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_gamestate(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<float>((today - start_date).to_int()) / static_cast<float>((end_date - start_date).to_int());
      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;
      }
   }
}