aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/BuildingInstance.cpp
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2023-11-14 22:47:35 +0100
committer hop311 <hop3114@gmail.com>2023-11-16 20:38:34 +0100
commit886b8b8f396438fc2b7da7d2508f2064d14150a8 (patch)
treeeeed419a7d97ecb58adf63a17eb9184db3e5ed7a /src/openvic-simulation/economy/BuildingInstance.cpp
parent8271b1519e095ee3e7245cde2f0b54561c3ec619 (diff)
Misc changes
Diffstat (limited to 'src/openvic-simulation/economy/BuildingInstance.cpp')
-rw-r--r--src/openvic-simulation/economy/BuildingInstance.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/openvic-simulation/economy/BuildingInstance.cpp b/src/openvic-simulation/economy/BuildingInstance.cpp
new file mode 100644
index 0000000..417cdda
--- /dev/null
+++ b/src/openvic-simulation/economy/BuildingInstance.cpp
@@ -0,0 +1,52 @@
+#include "BuildingInstance.hpp"
+
+using namespace OpenVic;
+
+BuildingInstance::BuildingInstance(BuildingType const& new_building_type, level_t new_level)
+ : HasIdentifier { 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();
+}
+
+void BuildingInstance::set_level(BuildingInstance::level_t new_level) {
+ level = new_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;
+ }
+ }
+}