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

using namespace OpenVic;

GoodInstance::GoodInstance(GoodDefinition const& new_good_definition)
  : HasIdentifierAndColour { new_good_definition },
   good_definition { new_good_definition },
   price { new_good_definition.get_base_price() },
   is_available { new_good_definition.get_is_available_from_start() },
   supply_running_total { fixed_point_t::_0() },
   total_supply_yesterday { fixed_point_t::_0() },
   market_sell_order_quantities {},
   market_sell_order_callbacks {}
   {}

void GoodInstance::add_market_sell_order(const fixed_point_t quantity, const std::function<void(SellResult)> callback) {
   supply_running_total += quantity;
   market_sell_order_quantities.push_back(quantity);
   market_sell_order_callbacks.push_back(callback);
}

void GoodInstance::clear_orders() {
   total_supply_yesterday = supply_running_total;
   supply_running_total = fixed_point_t::_0();
   market_sell_order_quantities.clear();
   market_sell_order_callbacks.clear();
}

bool GoodInstanceManager::setup(GoodDefinitionManager const& good_definition_manager) {
   if (good_instances_are_locked()) {
      Logger::error("Cannot set up good instances - they are already locked!");
      return false;
   }

   good_instances.reserve(good_definition_manager.get_good_definition_count());

   bool ret = true;

   for (GoodDefinition const& good : good_definition_manager.get_good_definitions()) {
      ret &= good_instances.add_item({ good });
   }

   lock_good_instances();

   return ret;
}