aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/production/ArtisanalProducer.cpp
blob: 2613d12eccc0c1bfa8c41e924d48deb9df03ac72 (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
63
64
65
#include "ArtisanalProducer.hpp"

#include "openvic-simulation/economy/GoodDefinition.hpp"
#include "openvic-simulation/economy/trading/BuyResult.hpp"
#include "openvic-simulation/economy/trading/SellResult.hpp"

using namespace OpenVic;

ArtisanalProducer::ArtisanalProducer(
   MarketInstance& new_market_instance,
   ModifierEffectCache const& new_modifier_effect_cache,
   Pop& new_pop,
   Pop::pop_size_t new_previous_pop_size,
   fixed_point_t new_inputs_bought_scalar,
   ProductionType const& new_production_type,
   fixed_point_t new_current_production
) : market_instance { new_market_instance },
   modifier_effect_cache { new_modifier_effect_cache },
   pop { new_pop },
   previous_pop_size { new_previous_pop_size },
   inputs_bought_scalar { new_inputs_bought_scalar },
   production_type { new_production_type },
   current_production { new_current_production }
   {}

void ArtisanalProducer::artisan_tick() {
   current_production = production_type.get_base_output_quantity()
      * inputs_bought_scalar
      * previous_pop_size / production_type.get_base_workforce_size();

   GoodDefinition const& output_good = production_type.get_output_good();
   if (current_production > 0) {
      market_instance.place_market_sell_order({
         output_good,
         current_production,
         [this](const SellResult sell_result) -> void {
            //TODO add artisanal income to pop (part of https://github.com/OpenVicProject/OpenVic-Simulation/issues/225 )
         }
      });
   }

   const fixed_point_t total_cash_to_spend = pop.get_cash();
   if (production_type.get_input_goods().empty()) {
      inputs_bought_scalar = fixed_point_t::_1();
   }
   else if (total_cash_to_spend > 0) {
      int input_goods_count = static_cast<int>(production_type.get_input_goods().size());
      for (auto const& [input_good_ptr, base_desired_quantity] : production_type.get_input_goods()) {
         const fixed_point_t money_to_spend = total_cash_to_spend / input_goods_count;
         const fixed_point_t desired_quantity = base_desired_quantity * pop.get_size() / production_type.get_base_workforce_size();
         //TODO pop cash -= money_to_spend
         market_instance.place_buy_up_to_order({
            *input_good_ptr,
            desired_quantity,
            money_to_spend,
            [this, desired_quantity](const BuyResult buy_result) -> void {
               //TODO pop cash += buy_result.get_money_left()
               inputs_bought_scalar = std::min(inputs_bought_scalar, buy_result.get_quantity_bought() / desired_quantity);
            }
         });
      }
   }

   previous_pop_size = pop.get_size();
}