Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/openvic-simulation/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ InstanceManager::InstanceManager(
good_instance_manager
},
artisanal_producer_deps {
new_definition_manager.get_define_manager().get_economy_defines(),
new_definition_manager.get_modifier_manager().get_modifier_effect_cache()
},
country_instance_deps {
Expand Down
29 changes: 25 additions & 4 deletions src/openvic-simulation/economy/production/ArtisanalProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstddef>

#include "openvic-simulation/country/CountryInstance.hpp"
#include "openvic-simulation/defines/EconomyDefines.hpp"
#include "openvic-simulation/economy/GoodDefinition.hpp"
#include "openvic-simulation/economy/GoodInstance.hpp"
#include "openvic-simulation/economy/production/ProductionType.hpp"
Expand All @@ -24,7 +25,8 @@ ArtisanalProducer::ArtisanalProducer(
fixed_point_map_t<GoodDefinition const*>&& new_stockpile,
ProductionType const* const new_production_type,
fixed_point_t new_current_production
) : modifier_effect_cache { artisanal_producer_deps.modifier_effect_cache },
) : economy_defines { artisanal_producer_deps.economy_defines },
modifier_effect_cache { artisanal_producer_deps.modifier_effect_cache },
stockpile { std::move(new_stockpile) },
production_type_nullable { nullptr },
current_production { new_current_production }
Expand Down Expand Up @@ -374,9 +376,10 @@ ProductionType const* ArtisanalProducer::pick_production_type(

const fixed_point_t revenue = pop.get_artisanal_revenue();
const fixed_point_t costs = costs_of_production;
const fixed_point_t base_chance_to_switch_while_profitable = economy_defines.get_goods_focus_swap_chance();
if (production_type_nullable == nullptr || (revenue <= costs)) {
should_pick_new_production_type = true;
} else {
} else if (base_chance_to_switch_while_profitable > 0) {
const fixed_point_t current_score = calculate_production_type_score(
revenue,
costs,
Expand Down Expand Up @@ -406,8 +409,26 @@ ProductionType const* ArtisanalProducer::pick_production_type(
}
}

//TODO decide based on score and randomness and defines.economy.GOODS_FOCUS_SWAP_CHANCE
should_pick_new_production_type = relative_score < fixed_point_t::_0_50;
//picked so the line hits 0 at relative_score = 2/3 and the area under the curve equals 1
//2/3 was picked as being in the top 1/3 of production types makes it very unlikely you'll profit from switching.
constexpr fixed_point_t slope = fixed_point_t{-9} / 2;
constexpr fixed_point_t offset = 3;

const fixed_point_t change_modifier_from_relative_score = std::max(fixed_point_t::_0, slope * relative_score + offset);
const fixed_point_t switch_chance = base_chance_to_switch_while_profitable * change_modifier_from_relative_score;
if (switch_chance >= 1) {
should_pick_new_production_type = true;
} else {
constexpr fixed_point_t weights_sum = 1;
const fixed_point_t keep_current_chance = weights_sum - switch_chance;
const std::array<fixed_point_t, 2> weights { keep_current_chance, switch_chance };
const size_t should_switch = sample_weighted_index(
random_number_generator(),
weights,
weights_sum
);
should_pick_new_production_type = should_switch == 1;
}
}

if (!should_pick_new_production_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace OpenVic {
struct ArtisanalProducerDeps;
struct EconomyDefines;
struct GoodDefinition;
struct GoodInstanceManager;
struct ModifierEffectCache;
Expand All @@ -21,6 +22,7 @@ namespace OpenVic {

struct ArtisanalProducer {
private:
EconomyDefines const& economy_defines;
ModifierEffectCache const& modifier_effect_cache;
fixed_point_map_t<GoodDefinition const*> stockpile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#pragma once

namespace OpenVic {
struct EconomyDefines;
struct ModifierEffectCache;

struct ArtisanalProducerDeps {
EconomyDefines const& economy_defines;
ModifierEffectCache const& modifier_effect_cache;
};
}
2 changes: 1 addition & 1 deletion src/openvic-simulation/utility/WeightedSampling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OpenVic {
OV_ALWAYS_INLINE static size_t sample_weighted_index(
const uint32_t random_value,
const std::span<fixed_point_t> positive_weights,
const std::span<const fixed_point_t> positive_weights,
const fixed_point_t weights_sum
) {
if (positive_weights.empty() || weights_sum <= 0) {
Expand Down