aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/GoodInstance.cpp
blob: e48624c05353a521345f16abb8857be5cd53a0a3 (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
#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_buy_up_to_order(
   const fixed_point_t max_quantity,
   const fixed_point_t money_to_spend,
   const std::function<void(const BuyResult)> callback
) {
   demand_running_total += max_quantity;
   buy_up_to_order_max_quantities.push_back(max_quantity);
   buy_up_to_order_money_to_spend.push_back(money_to_spend);
   buy_up_to_order_callbacks.push_back(callback);
}

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_demand_yesterday = demand_running_total;
   demand_running_total = fixed_point_t::_0();
   total_supply_yesterday = supply_running_total;
   supply_running_total = fixed_point_t::_0();

   buy_up_to_order_max_quantities.clear();
   buy_up_to_order_callbacks.clear();
   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;
}