aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/economy/production/ResourceGatheringOperation.cpp
blob: 7f030ab3aec868d13c5eddad325f88357d442201 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "ResourceGatheringOperation.hpp"

#include <vector>

#include "openvic-simulation/economy/production/Employee.hpp"
#include "openvic-simulation/economy/production/ProductionType.hpp"
#include "openvic-simulation/map/ProvinceInstance.hpp"
#include "openvic-simulation/map/State.hpp"
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
#include "openvic-simulation/pop/Pop.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/utility/Logger.hpp"

using namespace OpenVic;

ResourceGatheringOperation::ResourceGatheringOperation(
   ProductionType const* new_production_type_nullable,
   fixed_point_t new_size_multiplier,
   fixed_point_t new_revenue_yesterday,
   fixed_point_t new_output_quantity_yesterday,
   fixed_point_t new_unsold_quantity_yesterday,
   std::vector<Employee>&& new_employees,
   decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys
) : production_type_nullable { new_production_type_nullable },
   revenue_yesterday { new_revenue_yesterday },
   output_quantity_yesterday { new_output_quantity_yesterday },
   unsold_quantity_yesterday { new_unsold_quantity_yesterday },
   size_multiplier { new_size_multiplier },
   employees { std::move(new_employees) },
   max_employee_count_cache { 0 },
   total_employees_count_cache { 0 },
   total_paid_employees_count_cache { 0 },
   total_owner_income_cache { },
   total_employee_income_cache { },
   employee_count_per_type_cache { &pop_type_keys }
{ }

ResourceGatheringOperation::ResourceGatheringOperation(decltype(employee_count_per_type_cache)::keys_t const& pop_type_keys) : ResourceGatheringOperation {
   nullptr, fixed_point_t::_0(),
   fixed_point_t::_0(), fixed_point_t::_0(),
   fixed_point_t::_0(), {}, pop_type_keys
} {}

void ResourceGatheringOperation::initialise_rgo_size_multiplier(
   ProvinceInstance& location,
   ModifierEffectCache const& modifier_effect_cache
) {
   if (production_type_nullable == nullptr) {
      size_multiplier = fixed_point_t::_0();
      max_employee_count_cache = fixed_point_t::_0();
      return;
   }
   
   ProductionType const& production_type = *production_type_nullable;
   std::vector<Job> const& jobs = production_type.get_jobs();
   IndexedMap<PopType, Pop::pop_size_t> const& province_pop_type_distribution = location.get_pop_type_distribution();
   
   Pop::pop_size_t total_worker_count_in_province = 0; //not counting equivalents
   for(Job const& job : jobs) {
      total_worker_count_in_province += province_pop_type_distribution[*job.get_pop_type()];
   }
   
   fixed_point_t base_size_modifier = fixed_point_t::_1();
   if (production_type.is_farm()) {
      base_size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local());
   }
   if (production_type.is_mine()) {
      base_size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local());
   }

   const fixed_point_t base_workforce_size = production_type.get_base_workforce_size();
   if (base_size_modifier == fixed_point_t::_0()) {
      size_multiplier = 0;
   } else {
      size_multiplier = ((total_worker_count_in_province / (base_size_modifier * base_workforce_size)).ceil() * fixed_point_t::_1_50()).floor();
   }

   const fixed_point_t size_modifier = calculate_size_modifier(location, modifier_effect_cache);
   max_employee_count_cache = (size_modifier * size_multiplier * base_workforce_size).floor();
}

fixed_point_t ResourceGatheringOperation::calculate_size_modifier(ProvinceInstance const& location, ModifierEffectCache const& modifier_effect_cache) const {
   if (production_type_nullable == nullptr) {
      return fixed_point_t::_1();
   }
   
   ProductionType const& production_type = *production_type_nullable;

   fixed_point_t size_modifier = fixed_point_t::_1();
   if (production_type.is_farm()) {
      size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_global())
         + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_size_local());
   }
   if (production_type.is_mine()) {
      size_modifier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_global())
         + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_size_local());
   }
   auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()];
   size_modifier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_size());
   return size_modifier > fixed_point_t::_0() ? size_modifier : fixed_point_t::_0();
}

void ResourceGatheringOperation::rgo_tick(ProvinceInstance& location, ModifierEffectCache const& modifier_effect_cache) {
   if (production_type_nullable == nullptr | location.get_owner() == nullptr) {
      output_quantity_yesterday = 0;
      revenue_yesterday = 0;
      return;
   }
   
   ProductionType const& production_type = *production_type_nullable;
   std::vector<Job> const& jobs = production_type.get_jobs();
   IndexedMap<PopType, Pop::pop_size_t> const& province_pop_type_distribution = location.get_pop_type_distribution();
   
   Pop::pop_size_t total_worker_count_in_province = 0; //not counting equivalents
   for(Job const& job : jobs) {
      total_worker_count_in_province += province_pop_type_distribution[*job.get_pop_type()];
   }
   
   hire(location, total_worker_count_in_province);

   Pop::pop_size_t total_owner_count_in_state_cache = 0;
   std::vector<Pop*> const* owner_pops_cache = nullptr;

   if (production_type.get_owner().has_value()) {
      PopType const& owner_pop_type = *production_type.get_owner()->get_pop_type();
      total_owner_count_in_state_cache = location.get_state()->get_pop_type_distribution()[owner_pop_type];
      owner_pops_cache = &location.get_mutable_state()->get_mutable_pops_cache_by_type()[owner_pop_type];
   }

   output_quantity_yesterday = produce(
      location,
      owner_pops_cache,
      total_owner_count_in_state_cache,
      modifier_effect_cache
   );

   revenue_yesterday = output_quantity_yesterday * production_type.get_output_good().get_base_price(); //TODO sell on market

   pay_employees(
      location,
      revenue_yesterday,
      total_worker_count_in_province,
      owner_pops_cache,
      total_owner_count_in_state_cache
   ); 
}

void ResourceGatheringOperation::hire(ProvinceInstance& location, Pop::pop_size_t available_worker_count) {
   total_employees_count_cache = 0;
   total_paid_employees_count_cache=0;
   if (production_type_nullable == nullptr) {
      employees.clear();
      employee_count_per_type_cache.fill(fixed_point_t::_0());
      return;
   }

   ProductionType const& production_type = *production_type_nullable;
   if (max_employee_count_cache <= 0) { return; }
   if (available_worker_count <= 0) { return; }

   fixed_point_t proportion_to_hire;
   if (max_employee_count_cache >= available_worker_count) {
      //hire everyone
      proportion_to_hire = fixed_point_t::_1();
   } else {
      //hire all pops proportionally
      const fixed_point_t max_worker_count_real = max_employee_count_cache, available_worker_count_real = available_worker_count;
      proportion_to_hire = max_worker_count_real / available_worker_count_real;
   }
   
   std::vector<Job> const& jobs = production_type.get_jobs();
   for (Pop& pop : location.get_mutable_pops()){
      PopType const& pop_type = *pop.get_type();
      for(Job const& job : jobs) {
         if (job.get_pop_type() == &pop_type) {
            const Pop::pop_size_t pop_size_to_hire = static_cast<Pop::pop_size_t>((proportion_to_hire * pop.get_size()).floor());
            employee_count_per_type_cache[pop_type] += pop_size_to_hire;
            employees.emplace_back(pop, pop_size_to_hire);
            total_employees_count_cache += pop_size_to_hire;
            if (!pop_type.get_is_slave()) {
               total_paid_employees_count_cache += pop_size_to_hire;
            }
            break;
         }
      }
   }
}

fixed_point_t ResourceGatheringOperation::produce(
   ProvinceInstance& location,
   std::vector<Pop*> const* const owner_pops_cache,
   const Pop::pop_size_t total_owner_count_in_state_cache,
   ModifierEffectCache const& modifier_effect_cache
) {
   const fixed_point_t size_modifier = calculate_size_modifier(location, modifier_effect_cache);
   if (size_modifier == fixed_point_t::_0()){
      return fixed_point_t::_0();
   }

   if (production_type_nullable == nullptr || max_employee_count_cache <= 0) {
      return fixed_point_t::_0();
   }
   
   ProductionType const& production_type = *production_type_nullable;
   fixed_point_t throughput_multiplier = fixed_point_t::_1();
   fixed_point_t output_multilpier = fixed_point_t::_1();

   std::optional<Job> const& owner = production_type.get_owner();
   if (owner.has_value()) {
      State const* state_ptr = location.get_state();
      if (state_ptr == nullptr) {
         Logger::error("Province ", location.get_identifier(), " has no state.");
         return fixed_point_t::_0();
      }

      State const& state = *state_ptr;
      const Pop::pop_size_t state_population = state.get_total_population();
      Job const& owner_job = owner.value();

      if (total_owner_count_in_state_cache > 0) {
         switch (owner_job.get_effect_type()) {
            case Job::effect_t::OUTPUT:
               output_multilpier += owner_job.get_effect_multiplier() * total_owner_count_in_state_cache / state_population;
               break;
            case Job::effect_t::THROUGHPUT:
               throughput_multiplier += owner_job.get_effect_multiplier() * total_owner_count_in_state_cache / state_population;
               break;
            default:
               Logger::error("Invalid job effect in RGO ",production_type.get_identifier());
               break;
         }
      }
   }

   throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_rgo_throughput())
      +location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_local_rgo_throughput());
   output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_rgo_output())
      +location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_local_rgo_output());

   if (production_type.is_farm()) {
      throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_throughput_global());
      output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_global())
         + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_farm_rgo_output_local());
   }
   if (production_type.is_mine()) {
      throughput_multiplier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_throughput_global());
      output_multilpier += location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_global())
         + location.get_modifier_effect_value_nullcheck(modifier_effect_cache.get_mine_rgo_output_local());
   }
   auto const& good_effects = modifier_effect_cache.get_good_effects()[production_type.get_output_good()];
   throughput_multiplier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_throughput());
   output_multilpier += location.get_modifier_effect_value_nullcheck(good_effects.get_rgo_goods_output());

   fixed_point_t throughput_from_workers = fixed_point_t::_0();
   fixed_point_t output_from_workers = fixed_point_t::_1();
   for (PopType const& pop_type : *employee_count_per_type_cache.get_keys()) {
      const Pop::pop_size_t employees_of_type = employee_count_per_type_cache[pop_type];
      
      for(Job const& job : production_type.get_jobs()) {
         if (job.get_pop_type() != &pop_type) {
            continue;
         }

         fixed_point_t const effect_multiplier = job.get_effect_multiplier();
         fixed_point_t relative_to_workforce = fixed_point_t::parse(employees_of_type) / fixed_point_t::parse(max_employee_count_cache);
         fixed_point_t const amount = job.get_amount();
         if (effect_multiplier != fixed_point_t::_1() && relative_to_workforce > amount) {
            relative_to_workforce = amount;
         }
         switch (job.get_effect_type()) {
            case Job::effect_t::OUTPUT:
               output_from_workers += effect_multiplier * relative_to_workforce;
               break;
            case Job::effect_t::THROUGHPUT:
               throughput_from_workers += effect_multiplier * relative_to_workforce;
               break;
            default:
               Logger::error("Invalid job effect in RGO ",production_type.get_identifier());
               break;
         }
      }
   }
   
   //if province is overseas multiply by (1 + overseas penalty)

   return production_type.get_base_output_quantity()
      * size_modifier * size_multiplier
      * throughput_multiplier * throughput_from_workers
      * output_multilpier * output_from_workers;
}

void ResourceGatheringOperation::pay_employees(
   ProvinceInstance& location,
   const fixed_point_t revenue,
   const Pop::pop_size_t total_worker_count_in_province,
   std::vector<Pop*> const* const owner_pops_cache,
   const Pop::pop_size_t total_owner_count_in_state_cache
) {
   total_owner_income_cache = 0;
   total_employee_income_cache = 0;
   if (revenue <= 0 || total_worker_count_in_province <= 0) {
      if (revenue < 0) { Logger::error("Negative revenue for province ", location.get_identifier()); }
      if (total_worker_count_in_province < 0) { Logger::error("Negative total worker count for province ", location.get_identifier()); }
      return;
   }

   fixed_point_t revenue_left = revenue;
   if (total_owner_count_in_state_cache > 0) {     
      fixed_point_t owner_share = (fixed_point_t::_2() * total_owner_count_in_state_cache / total_worker_count_in_province);
      constexpr fixed_point_t upper_limit = fixed_point_t::_0_50();
      if (owner_share > upper_limit) {
         owner_share = upper_limit;
      }

      for(Pop* owner_pop_ptr : *owner_pops_cache) {
         Pop& owner_pop = *owner_pop_ptr;
         const fixed_point_t income_for_this_pop = revenue_left * owner_share * owner_pop.get_size() / total_owner_count_in_state_cache;
         owner_pop.add_rgo_owner_income(income_for_this_pop);
         total_owner_income_cache += income_for_this_pop;
      }
      revenue_left *= (fixed_point_t::_1() - owner_share);
   }

   if (total_paid_employees_count_cache > 0) {
      for (Employee& employee : employees) {
         Pop& employee_pop = employee.pop;
         
         PopType const* employee_pop_type = employee_pop.get_type();
         if (employee_pop_type == nullptr) {
            Logger::error("employee has nullptr pop_type.");
            return;
         }

         if (employee_pop_type->get_is_slave()) {
            continue;
         }

         const Pop::pop_size_t employee_size = employee.get_size();
         const fixed_point_t income_for_this_pop = revenue_left * employee_size / total_paid_employees_count_cache;
         employee_pop.add_rgo_worker_income(income_for_this_pop);
         total_employee_income_cache += income_for_this_pop;
      }
   } else {
      //scenario slaves only
      //Money is removed from system in Victoria 2.
   }
}