Skip to content

Commit 57b7a64

Browse files
committed
draft for factory production
1 parent e98cd1f commit 57b7a64

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

src/openvic-simulation/economy/FactoryProducer.cpp

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

3+
#include "openvic-simulation/economy/Good.hpp"
4+
#include "openvic-simulation/pop/Pop.hpp"
5+
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
6+
7+
38
using namespace OpenVic;
49

510
FactoryProducer::FactoryProducer(
@@ -20,7 +25,8 @@ FactoryProducer::FactoryProducer(
2025
market_spendings_yesterday { new_market_spendings_yesterday }, paychecks_yesterday { new_paychecks_yesterday },
2126
unprofitable_days { new_unprofitable_days }, injected_days { new_injected_days },
2227
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) } {}
28+
profit_history_current { new_profit_history_current }, daily_profit_history { std::move(new_daily_profit_history) },
29+
employees_per_job_cache {} {}
2430
FactoryProducer::FactoryProducer(ProductionType const& new_production_type, const fixed_point_t new_size_multiplier)
2531
: FactoryProducer { new_production_type, new_size_multiplier, 0, 0, 0, {}, {}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {} } {}
2632

@@ -37,3 +43,54 @@ fixed_point_t FactoryProducer::get_average_profitability_last_seven_days() const
3743

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

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)