Skip to content

Commit f275652

Browse files
committed
draft for factory production
1 parent e98cd1f commit f275652

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/openvic-simulation/economy/FactoryProducer.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#include "FactoryProducer.hpp"
22

3+
#include <map>
4+
5+
#include "economy/Good.hpp"
6+
#include "pop/Pop.hpp"
7+
#include "types/fixed_point/FixedPoint.hpp"
8+
9+
310
using namespace OpenVic;
411

512
FactoryProducer::FactoryProducer(
@@ -20,7 +27,8 @@ FactoryProducer::FactoryProducer(
2027
market_spendings_yesterday { new_market_spendings_yesterday }, paychecks_yesterday { new_paychecks_yesterday },
2128
unprofitable_days { new_unprofitable_days }, injected_days { new_injected_days },
2229
days_without_input { new_days_without_input }, hiring_priority { new_hiring_priority },
23-
profit_history_current { new_profit_history_current }, daily_profit_history { std::move(new_daily_profit_history) } {}
30+
profit_history_current { new_profit_history_current }, daily_profit_history { std::move(new_daily_profit_history) },
31+
employees_per_job_cache {} {}
2432
FactoryProducer::FactoryProducer(ProductionType const& new_production_type, const fixed_point_t new_size_multiplier)
2533
: FactoryProducer { new_production_type, new_size_multiplier, 0, 0, 0, {}, {}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {} } {}
2634

@@ -37,3 +45,54 @@ fixed_point_t FactoryProducer::get_average_profitability_last_seven_days() const
3745

3846
return sum / (1 + profit_history_current);
3947
}
48+
49+
void FactoryProducer::produce(
50+
const fixed_point_t input_modifier, const fixed_point_t throughput_modifier, const fixed_point_t output_modifier
51+
) {
52+
fixed_point_t my_input_multiplier = input_modifier, my_throughput_multiplier = throughput_modifier,
53+
my_output_multiplier = output_modifier;
54+
for (Job const& job : production_type.get_jobs()) {
55+
const fixed_point_t number_of_employees_in_job = employees_per_job_cache[&job];
56+
switch (job.get_effect_type()) {
57+
case Job::effect_t::INPUT:
58+
my_input_multiplier += job.get_effect_multiplier() * number_of_employees_in_job /
59+
(size_multiplier * production_type.get_base_workforce_size());
60+
break;
61+
case Job::effect_t::OUTPUT:
62+
my_output_multiplier += job.get_effect_multiplier() * number_of_employees_in_job /
63+
(size_multiplier * production_type.get_base_workforce_size());
64+
break;
65+
case Job::effect_t::THROUGHPUT:
66+
my_throughput_multiplier *=
67+
job.get_effect_multiplier() * number_of_employees_in_job / production_type.get_base_workforce_size();
68+
break;
69+
}
70+
}
71+
72+
my_input_multiplier *= my_throughput_multiplier;
73+
my_output_multiplier *= my_throughput_multiplier;
74+
75+
fixed_point_t lack_of_inputs_multiplier = 1;
76+
for (const auto& [good, base_input_amount] : production_type.get_input_goods()) {
77+
const fixed_point_t desired_input = base_input_amount * my_input_multiplier;
78+
const fixed_point_t in_stock = stockpile[good];
79+
if (desired_input > in_stock) {
80+
fixed_point_t relative_inputs = in_stock / desired_input;
81+
if (relative_inputs < lack_of_inputs_multiplier) {
82+
lack_of_inputs_multiplier = relative_inputs;
83+
}
84+
}
85+
}
86+
87+
if (lack_of_inputs_multiplier < 1) {
88+
my_input_multiplier *= lack_of_inputs_multiplier;
89+
my_output_multiplier *= lack_of_inputs_multiplier;
90+
days_without_input++;
91+
}
92+
93+
for (const auto& [good, base_input_amount] : production_type.get_input_goods()) {
94+
stockpile[good] -= base_input_amount * my_input_multiplier;
95+
}
96+
97+
output_quantity_yesterday = production_type.get_base_output_quantity() * my_output_multiplier;
98+
}

src/openvic-simulation/economy/FactoryProducer.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace OpenVic {
1111
class FactoryProducer final {
1212
private:
1313
static constexpr uint8_t DAYS_OF_HISTORY = 7;
14+
ordered_map<Job const*, Pop::pop_size_t> employees_per_job_cache;
15+
1416
uint8_t PROPERTY(profit_history_current);
1517
std::array<fixed_point_t, DAYS_OF_HISTORY> PROPERTY(daily_profit_history);
1618
ProductionType const& PROPERTY(production_type);
@@ -45,5 +47,8 @@ namespace OpenVic {
4547

4648
fixed_point_t get_profitability_yesterday() const;
4749
fixed_point_t get_average_profitability_last_seven_days() const;
50+
void produce(
51+
const fixed_point_t input_modifier, const fixed_point_t throughput_modifier, const fixed_point_t output_modifier
52+
);
4853
};
4954
}

src/openvic-simulation/pop/Pop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ bool PopManager::load_pop_into_vector(
533533
*non_integer_size = true;
534534
}
535535

536-
if (culture != nullptr && religion != nullptr && size >= 1) {
537-
vec.emplace_back(Pop { type, *culture, *religion, size.to_int64_t(), militancy, consciousness, rebel_type });
536+
if (culture != nullptr && religion != nullptr && size >= 1 && size <= std::numeric_limits<Pop::pop_size_t>::max()) {
537+
vec.emplace_back(Pop { type, *culture, *religion, size.to_int32_t(), militancy, consciousness, rebel_type });
538538
} else {
539539
Logger::warning(
540540
"Some pop arguments are invalid: culture = ", culture, ", religion = ", religion, ", size = ", size

src/openvic-simulation/pop/Pop.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace OpenVic {
3030
struct Pop {
3131
friend struct PopManager;
3232

33-
using pop_size_t = int64_t;
33+
using pop_size_t = int32_t;
3434

3535
static constexpr pop_size_t MAX_SIZE = std::numeric_limits<pop_size_t>::max();
3636

0 commit comments

Comments
 (0)