11 return opt.effect || opt.name;
15 for(
auto i =
state.pending_n_event.size(); i-- > 0;) {
16 if(
state.pending_n_event[i].date == e.
date &&
state.pending_n_event[i].e == e.
e &&
19 state.pending_n_event[i].r_lo == e.
r_lo) {
24 state.pending_n_event[i] =
state.pending_n_event.back();
25 state.pending_n_event.pop_back();
27 if(
state.world.national_event_get_options(e.
e)[opt].effect) {
37 for(
auto i =
state.pending_f_n_event.size(); i-- > 0;) {
38 if(
state.pending_f_n_event[i].date == e.
date &&
state.pending_f_n_event[i].e == e.
e &&
state.pending_f_n_event[i].n == e.
n &&
39 state.pending_f_n_event[i].r_hi == e.
r_hi &&
state.pending_f_n_event[i].r_lo == e.
r_lo) {
41 if(opt >
state.world.free_national_event_get_options(e.
e).size() || !
is_valid_option(
state.world.free_national_event_get_options(e.
e)[opt]))
44 state.pending_f_n_event[i] =
state.pending_f_n_event.back();
45 state.pending_f_n_event.pop_back();
47 if(
state.world.free_national_event_get_options(e.
e)[opt].effect) {
57 for(
auto i =
state.pending_p_event.size(); i-- > 0;) {
58 if(
state.pending_p_event[i].date == e.
date &&
state.pending_p_event[i].e == e.
e &&
62 if(opt >
state.world.provincial_event_get_options(e.
e).size() || !
is_valid_option(
state.world.provincial_event_get_options(e.
e)[opt]))
65 state.pending_p_event[i] =
state.pending_p_event.back();
66 state.pending_p_event.pop_back();
68 if(
state.world.provincial_event_get_options(e.
e)[opt].effect) {
78 for(
auto i =
state.pending_f_p_event.size(); i-- > 0;) {
79 if(
state.pending_f_p_event[i].date == e.
date &&
state.pending_f_p_event[i].e == e.
e &&
state.pending_f_p_event[i].p == e.
p &&
80 state.pending_f_p_event[i].r_hi == e.
r_hi &&
state.pending_f_p_event[i].r_lo == e.
r_lo) {
82 if(opt >
state.world.free_provincial_event_get_options(e.
e).size() || !
is_valid_option(
state.world.free_provincial_event_get_options(e.
e)[opt]))
85 state.pending_f_p_event[i] =
state.pending_f_p_event.back();
86 state.pending_f_p_event.pop_back();
88 if(
state.world.free_provincial_event_get_options(e.
e)[opt].effect) {
101 for(
const auto& opt : fat_id.get_options()) {
116 if(
state.world.national_event_get_is_major(e)) {
135 n, dcon::nation_id{}, dcon::nation_id{},
138 }
else if(n ==
state.local_player_nation) {
157 n, dcon::nation_id{}, dcon::nation_id{},
162 if(
auto immediate =
state.world.national_event_get_immediate_effect(e); immediate) {
165 if(
state.world.nation_get_is_player_controlled(n)) {
167 state.pending_n_event.push_back(new_event);
168 if(n ==
state.local_player_nation)
169 state.new_n_event.push(new_event);
171 auto& opt =
state.world.national_event_get_options(e);
174 for(
uint32_t i = 0; i < opt.size(); ++i) {
175 if(opt[i].ai_chance) {
183 for(
uint32_t i = 0; i < opt.size(); ++i) {
184 if(opt[i].ai_chance) {
185 rvalue -= odds[i] / total;
205 if(
state.world.free_national_event_get_only_once(e) &&
state.world.free_national_event_get_has_been_triggered(e))
210 state.world.free_national_event_set_has_been_triggered(e,
true);
211 if(
state.world.free_national_event_get_is_major(e)) {
230 n, dcon::nation_id{}, dcon::nation_id{},
233 }
else if(n ==
state.local_player_nation) {
252 n, dcon::nation_id{}, dcon::nation_id{},
256 if(
auto immediate =
state.world.free_national_event_get_immediate_effect(e); immediate) {
259 if(
state.world.nation_get_is_player_controlled(n)) {
261 state.pending_f_n_event.push_back(new_event);
262 if(n ==
state.local_player_nation)
263 state.new_f_n_event.push(new_event);
265 auto& opt =
state.world.free_national_event_get_options(e);
268 for(
uint32_t i = 0; i < opt.size(); ++i) {
269 if(opt[i].ai_chance) {
277 for(
uint32_t i = 0; i < opt.size(); ++i) {
278 if(opt[i].ai_chance) {
279 rvalue -= odds[i] / total;
301 if(
auto immediate =
state.world.provincial_event_get_immediate_effect(e); immediate) {
304 auto owner =
state.world.province_get_nation_from_province_ownership(p);
305 if(owner ==
state.local_player_nation) {
324 owner, dcon::nation_id{}, dcon::nation_id{},
328 if(
state.world.nation_get_is_player_controlled(owner)) {
330 state.pending_p_event.push_back(new_event);
331 if(owner ==
state.local_player_nation)
332 state.new_p_event.push(new_event);
334 auto& opt =
state.world.provincial_event_get_options(e);
337 for(
uint32_t i = 0; i < opt.size(); ++i) {
338 if(opt[i].ai_chance) {
346 for(
uint32_t i = 0; i < opt.size(); ++i) {
347 if(opt[i].ai_chance) {
348 rvalue -= odds[i] / total;
366 if(
state.world.free_provincial_event_get_only_once(e) &&
state.world.free_provincial_event_get_has_been_triggered(e))
371 state.world.free_provincial_event_set_has_been_triggered(e,
true);
372 if(
auto immediate =
state.world.free_provincial_event_get_immediate_effect(e); immediate) {
375 auto owner =
state.world.province_get_nation_from_province_ownership(p);
376 if(owner ==
state.local_player_nation) {
395 owner, dcon::nation_id{}, dcon::nation_id{},
399 if(
state.world.nation_get_is_player_controlled(owner)) {
401 state.pending_f_p_event.push_back(new_event);
402 if(owner ==
state.local_player_nation)
403 state.new_f_p_event.push(new_event);
405 auto& opt =
state.world.free_provincial_event_get_options(e);
408 for(
uint32_t i = 0; i < opt.size(); ++i) {
409 if(opt[i].ai_chance) {
417 for(
uint32_t i = 0; i < opt.size(); ++i) {
418 if(opt[i].ai_chance) {
419 rvalue -= odds[i] / total;
438 dcon::free_national_event_id
e;
441 return other.n ==
n && other.e ==
e;
444 return other.n !=
n ? (
n.value < other.n.value) : (
e.value < other.e.value);
449 dcon::free_provincial_event_id
e;
452 return other.p ==
p && other.e ==
e;
455 return other.p !=
p ? (
p.value < other.p.value) : (
e.value < other.e.value);
460 if(
state.world.national_event_get_allow_multiple_instances(e))
462 for(int32_t i = 0; i < int32_t(
state.future_n_event.size()); i++) {
463 auto const& nev =
state.future_n_event[i];
466 && nev.date == date) {
475 bool fired_n =
false;
477 for(int32_t j = int32_t(n_n_events - 1); j >= 0; j--) {
478 auto const& e =
state.future_n_event[j];
479 if(e.date <=
state.current_date) {
485 for(int32_t j = int32_t(n_n_events - 1); j >= 0; j--) {
486 auto const& e =
state.future_n_event[j];
487 if(e.date <=
state.current_date) {
488 std::swap(
state.future_n_event[j],
state.future_n_event[
state.future_n_event.size() - 1]);
489 state.future_n_event.pop_back();
494 bool fired_p =
false;
496 for(int32_t j = int32_t(n_p_events - 1); j >= 0; j--) {
497 auto const& e =
state.future_p_event[j];
498 if(e.date <=
state.current_date) {
504 for(int32_t j = int32_t(n_p_events - 1); j >= 0; j--) {
505 auto const& e =
state.future_p_event[j];
506 if(e.date <=
state.current_date) {
507 std::swap(
state.future_p_event[j],
state.future_p_event[
state.future_p_event.size() - 1]);
508 state.future_p_event.pop_back();
512 if(!fired_p && !fired_n)
518 uint32_t n_block_size =
state.world.free_national_event_size() / 32;
519 uint32_t p_block_size =
state.world.free_provincial_event_size() / 32;
523 concurrency::combinable<std::vector<event_nation_pair>> events_triggered;
525 auto n_block_end = block_index == 31 ?
state.world.free_national_event_size() : n_block_size * (block_index + 1);
526 concurrency::parallel_for(n_block_size * block_index, n_block_end, [&](
uint32_t i) {
527 dcon::free_national_event_id
id{dcon::national_event_id::value_base_t(i)};
528 auto mod =
state.world.free_national_event_get_mtth(
id);
529 auto t =
state.world.free_national_event_get_trigger(
id);
531 if(
state.world.free_national_event_get_only_once(
id) ==
false ||
state.world.free_national_event_get_has_been_triggered(
id) ==
false) {
532 ve::execute_serial_fast<dcon::nation_id>(
state.world.nation_size(), [&](
auto ids) {
539 ? (state.world.nation_get_owned_province_count(ids) != 0) && trigger::evaluate(state, t, trigger::to_generic(ids), trigger::to_generic(ids), 0)
540 : (state.world.nation_get_owned_province_count(ids) != 0);
541 if(ve::compress_mask(some_exist).v != 0) {
543 trigger::evaluate_multiplicative_modifier(state, mod, trigger::to_generic(ids), trigger::to_generic(ids), 0) : ve::fp_vector{ 1.0f };
544 auto adj_chance = 1.0f - ve::select(chances <= 1.0f, 1.0f, 1.0f / (chances));
545 auto adj_chance_2 = adj_chance * adj_chance;
546 auto adj_chance_4 = adj_chance_2 * adj_chance_2;
547 auto adj_chance_8 = adj_chance_4 * adj_chance_4;
548 auto adj_chance_16 = adj_chance_8 * adj_chance_8;
551 [&](dcon::nation_id n, float c, bool condition) {
552 auto owned_range = state.world.nation_get_province_ownership(n);
553 if(condition && owned_range.begin() != owned_range.end()) {
554 if(float(rng::get_random(state, uint32_t((i << 1) ^ n.index())) & 0xFFFFFF) / float(0xFFFFFF + 1) >= c) {
555 events_triggered.local().push_back(event_nation_pair{n, id});
559 ids, adj_chance_16, some_exist);
566 auto total_vector = events_triggered.combine([](
auto& a,
auto& b) {
567 std::vector<event_nation_pair> result(a.begin(), a.end());
568 result.insert(result.end(), b.begin(), b.end());
571 std::sort(total_vector.begin(), total_vector.end());
572 for(
auto& v : total_vector) {
578 concurrency::combinable<std::vector<event_prov_pair>> p_events_triggered;
580 auto p_block_end = block_index == 31 ?
state.world.free_provincial_event_size() : p_block_size * (block_index + 1);
581 concurrency::parallel_for(p_block_size * block_index, p_block_end, [&](
uint32_t i) {
582 dcon::free_provincial_event_id
id{dcon::free_provincial_event_id::value_base_t(i)};
583 auto mod =
state.world.free_provincial_event_get_mtth(
id);
584 auto t =
state.world.free_provincial_event_get_trigger(
id);
586 if(
state.world.free_provincial_event_get_only_once(
id) ==
false ||
state.world.free_provincial_event_get_has_been_triggered(
id) ==
false) {
587 ve::execute_serial_fast<dcon::province_id>(
uint32_t(
state.province_definitions.first_sea_province.index()),
588 [&](ve::contiguous_tags<dcon::province_id> ids) {
593 auto owners = state.world.province_get_nation_from_province_ownership(ids);
594 auto some_exist = t ? (owners != dcon::nation_id{}) &&
600 :
ve::fp_vector{ 2.0f };
601 auto adj_chance = 1.0f -
ve::select(chances <= 2.0f, 1.0f, 2.0f / chances);
602 auto adj_chance_2 = adj_chance * adj_chance;
603 auto adj_chance_4 = adj_chance_2 * adj_chance_2;
604 auto adj_chance_8 = adj_chance_4 * adj_chance_4;
605 auto adj_chance_16 = adj_chance_8 * adj_chance_8;
608 [&](dcon::province_id p, dcon::nation_id o,
float c,
bool condition) {
611 p_events_triggered.local().push_back(event_prov_pair{ p, id });
615 ids,
owners, adj_chance_16, some_exist);
621 auto total_p_vector = p_events_triggered.combine([](
auto& a,
auto& b) {
622 std::vector<event_prov_pair>
result(a.begin(), a.end());
626 std::sort(total_p_vector.begin(), total_p_vector.end());
627 for(
auto& v : total_p_vector) {
633 for(
auto i =
state.pending_n_event.size(); i-- > 0;) {
634 if(
state.pending_n_event[i].date + expiration_in_days <=
state.current_date) {
635 auto&
opt =
state.world.national_event_get_options(
state.pending_n_event[i].e);
640 state.pending_n_event[i].r_hi);
645 state.pending_n_event[i] =
state.pending_n_event.back();
646 state.pending_n_event.pop_back();
650 for(
auto i =
state.pending_f_n_event.size(); i-- > 0;) {
651 if(
state.pending_f_n_event[i].date + expiration_in_days <=
state.current_date) {
652 auto&
opt =
state.world.free_national_event_get_options(
state.pending_f_n_event[i].e);
657 state.pending_f_n_event[i].r_hi);
662 state.pending_f_n_event[i] =
state.pending_f_n_event.back();
663 state.pending_f_n_event.pop_back();
667 for(
auto i =
state.pending_p_event.size(); i-- > 0;) {
668 if(
state.pending_p_event[i].date + expiration_in_days <=
state.current_date) {
669 auto&
opt =
state.world.provincial_event_get_options(
state.pending_p_event[i].e);
674 state.pending_p_event[i].r_hi);
679 state.pending_p_event[i] =
state.pending_p_event.back();
680 state.pending_p_event.pop_back();
684 for(
auto i =
state.pending_f_p_event.size(); i-- > 0;) {
685 if(
state.pending_f_p_event[i].date + expiration_in_days <=
state.current_date) {
686 auto&
opt =
state.world.free_provincial_event_get_options(
state.pending_f_p_event[i].e);
691 state.pending_f_p_event[i].r_hi);
696 state.pending_f_p_event[i] =
state.pending_f_p_event.back();
697 state.pending_f_p_event.pop_back();
705 dcon::national_event_id
e;
710 static std::vector<internal_n_epair> valid_list;
712 int32_t total_chances = 0;
715 total_chances += fe.chance;
719 if(
auto possible_events = valid_list.size(); possible_events > 0) {
720 int32_t random_value = int32_t(
rng::get_random(state,
uint32_t(primary_slot + (state.world.nation_get_owned_province_count(this_slot) << 3))) % total_chances);
721 for(
auto& fe : valid_list) {
722 random_value -= fe.chance;
723 if(random_value < 0) {
732 static std::vector<internal_n_epair> valid_list;
734 int32_t total_chances = 0;
737 total_chances += fe.chance;
741 if(
auto possible_events = valid_list.size(); possible_events > 0) {
742 int32_t random_value = int32_t(
rng::get_random(state,
uint32_t(primary_slot + (state.world.nation_get_owned_province_count(this_slot) << 3))) % total_chances);
743 for(
auto& fe : valid_list) {
744 random_value -= fe.chance;
745 if(random_value < 0) {
754 dcon::provincial_event_id
e;
760 static std::vector<internal_p_epair> valid_list;
763 int32_t total_chances = 0;
767 total_chances += fe.chance;
772 auto possible_events = valid_list.size();
773 if(possible_events > 0) {
777 for(
auto& fe : valid_list) {
778 random_value -= fe.chance;
779 if(random_value < 0) {
788 for(
auto const& fe : state.national_definitions.on_election_tick) {
790 return fe.issue_group;
793 return dcon::issue_id{};
#define assert(condition)
pop_satisfaction_wrapper_fat fatten(data_container const &c, pop_satisfaction_wrapper_id id) noexcept
void execute(sys::state &state, dcon::effect_key key, int32_t primary, int32_t this_slot, int32_t from_slot, uint32_t r_lo, uint32_t r_hi)
void take_option(sys::state &state, pending_human_n_event const &e, uint8_t opt)
void update_future_events(sys::state &state)
void trigger_national_event(sys::state &state, dcon::national_event_id e, dcon::nation_id n, uint32_t r_lo, uint32_t r_hi, int32_t primary_slot, slot_type pt, int32_t from_slot, slot_type ft)
void fire_fixed_event(sys::state &state, std::vector< nations::fixed_event > const &v, int32_t primary_slot, slot_type pt, dcon::nation_id this_slot, int32_t from_slot, slot_type ft)
dcon::issue_id get_election_event_issue(sys::state &state, dcon::national_event_id e)
bool would_be_duplicate_instance(sys::state &state, dcon::national_event_id e, dcon::nation_id n, sys::date date)
void update_events(sys::state &state)
void trigger_provincial_event(sys::state &state, dcon::provincial_event_id e, dcon::province_id p, uint32_t r_hi, uint32_t r_lo, int32_t from_slot, slot_type ft)
bool event_has_options(sys::state &state, T id)
bool is_valid_option(sys::event_option const &opt)
void post(sys::state &state, message &&m)
uint64_t get_random(sys::state const &state, uint32_t value_in)
constexpr int32_t max_event_options
void add_line_break_to_layout_box(sys::state &state, layout_base &dest, layout_box &box)
void add_to_layout_box(sys::state &state, layout_base &dest, layout_box &box, embedded_flag ico)
layout_box open_layout_box(layout_base &dest, int32_t indent)
ankerl::unordered_dense::map< uint32_t, substitution > substitution_map
void close_layout_box(columnar_layout &dest, layout_box &box)
int32_t to_generic(dcon::province_id v)
dcon::province_id to_prov(int32_t v)
bool evaluate(sys::state &state, dcon::trigger_key key, int32_t primary, int32_t this_slot, int32_t from_slot)
float evaluate_multiplicative_modifier(sys::state &state, dcon::value_modifier_key modifier, int32_t primary, int32_t this_slot, int32_t from_slot)
void populate_event_submap(sys::state &state, text::substitution_map &sub, std::variant< event::pending_human_n_event, event::pending_human_f_n_event, event::pending_human_p_event, event::pending_human_f_p_event > const &phe) noexcept
T select(bool v, T a, T b)
bool compress_mask(bool v)
bool operator==(event_nation_pair const &other) const noexcept
bool operator<(event_nation_pair const &other) const noexcept
dcon::free_national_event_id e
bool operator<(event_prov_pair const &other) const noexcept
bool operator==(event_prov_pair const &other) const noexcept
dcon::free_provincial_event_id e
dcon::national_event_id e
dcon::provincial_event_id e
dcon::free_national_event_id e
dcon::free_provincial_event_id e
dcon::national_event_id e
dcon::provincial_event_id e