Skip to content

Commit

Permalink
on_election_started & on_election_ended
Browse files Browse the repository at this point in the history
  • Loading branch information
Nivaturimika committed Feb 25, 2024
1 parent 8c6716c commit 3e74234
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/culture/politics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ void start_election(sys::state& state, dcon::nation_id n) {
sys::message_base_type::electionstart
});
}
event::fire_fixed_event(state, state.national_definitions.on_election_started, trigger::to_generic(n),
event::slot_type::nation, n, -1, event::slot_type::none);
}
}

Expand Down Expand Up @@ -836,6 +838,8 @@ void update_elections(sys::state& state) {
n, dcon::nation_id{}, dcon::nation_id{},
sys::message_base_type::electiondone
});
event::fire_fixed_event(state, state.national_definitions.on_election_finished, trigger::to_generic(n),
event::slot_type::nation, n, -1, event::slot_type::none);
} else {
uint32_t winner = 0;
float winner_amount = party_votes[0].vote;
Expand All @@ -858,6 +862,8 @@ void update_elections(sys::state& state) {
n, dcon::nation_id{}, dcon::nation_id{},
sys::message_base_type::electiondone
});
event::fire_fixed_event(state, state.national_definitions.on_election_finished, trigger::to_generic(n),
event::slot_type::nation, n, -1, event::slot_type::none);
}

} else if(next_election_date(state, n) <= state.current_date) {
Expand Down
6 changes: 6 additions & 0 deletions src/gamestate/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ uint8_t const* read_scenario_section(uint8_t const* ptr_in, uint8_t const* secti
ptr_in = deserialize(ptr_in, state.national_definitions.on_civilize);
ptr_in = deserialize(ptr_in, state.national_definitions.on_my_factories_nationalized);
ptr_in = deserialize(ptr_in, state.national_definitions.on_crisis_declare_interest);
ptr_in = deserialize(ptr_in, state.national_definitions.on_election_started);
ptr_in = deserialize(ptr_in, state.national_definitions.on_election_finished);
}
{ // provincial definitions
ptr_in = deserialize(ptr_in, state.province_definitions.canals);
Expand Down Expand Up @@ -456,6 +458,8 @@ uint8_t* write_scenario_section(uint8_t* ptr_in, sys::state& state) {
ptr_in = serialize(ptr_in, state.national_definitions.on_civilize);
ptr_in = serialize(ptr_in, state.national_definitions.on_my_factories_nationalized);
ptr_in = serialize(ptr_in, state.national_definitions.on_crisis_declare_interest);
ptr_in = serialize(ptr_in, state.national_definitions.on_election_started);
ptr_in = serialize(ptr_in, state.national_definitions.on_election_finished);
}
{ // provincial definitions
ptr_in = serialize(ptr_in, state.province_definitions.canals);
Expand Down Expand Up @@ -631,6 +635,8 @@ size_t sizeof_scenario_section(sys::state& state) {
sz += serialize_size(state.national_definitions.on_civilize);
sz += serialize_size(state.national_definitions.on_my_factories_nationalized);
sz += serialize_size(state.national_definitions.on_crisis_declare_interest);
sz += serialize_size(state.national_definitions.on_election_started);
sz += serialize_size(state.national_definitions.on_election_finished);
}
{ // provincial definitions
sz += serialize_size(state.province_definitions.canals);
Expand Down
2 changes: 1 addition & 1 deletion src/gamestate/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ inline uint8_t const* deserialize(uint8_t const* ptr_in, ankerl::unordered_dense
}

constexpr inline uint32_t save_file_version = 36;
constexpr inline uint32_t scenario_file_version = 121 + save_file_version;
constexpr inline uint32_t scenario_file_version = 122 + save_file_version;

struct scenario_header {
uint32_t version = scenario_file_version;
Expand Down
2 changes: 2 additions & 0 deletions src/nations/nations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ struct global_national_state {
std::vector<fixed_event> on_civilize;
std::vector<fixed_event> on_my_factories_nationalized;
std::vector<fixed_event> on_crisis_declare_interest;
std::vector<fixed_event> on_election_started;
std::vector<fixed_event> on_election_finished;

bool gc_pending = false;

Expand Down
8 changes: 8 additions & 0 deletions src/parsing/parser_defs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,12 @@ s_on_my_factories_nationalized
s_on_crisis_declare_interest
#any value int member_fn

s_on_election_started
#any value int member_fn

s_on_election_finished
#any value int member_fn

on_action_file
on_yearly_pulse group s_on_yearly_pulse member
on_quarterly_pulse group s_on_quarterly_pulse member
Expand All @@ -1771,6 +1777,8 @@ on_action_file
on_civilize group s_on_civilize member
on_my_factories_nationalized group s_on_my_factories_nationalized member
on_crisis_declare_interest group s_on_crisis_declare_interest member
on_election_started group s_on_election_started member
on_election_finished group s_on_election_finished member

rebel_gov_list
#any value text member_fn
Expand Down
30 changes: 30 additions & 0 deletions src/parsing/parsers_declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,36 @@ void s_on_crisis_declare_interest::any_value(std::string_view chance, associatio
}
}

void s_on_election_started::any_value(std::string_view chance, association_type, int32_t event, error_handler& err,
int32_t line, scenario_building_context& context) {
int32_t value = parse_int(chance, line, err);
if(auto it = context.map_of_national_events.find(event); it != context.map_of_national_events.end()) {
context.state.national_definitions.on_election_started.push_back(
nations::fixed_event{ int16_t(value), it->second.id, dcon::trigger_key{} });
} else {
auto id = context.state.world.create_national_event();
context.map_of_national_events.insert_or_assign(event,
pending_nat_event {id, trigger::slot_contents::nation, trigger::slot_contents::nation, trigger::slot_contents::empty});
context.state.national_definitions.on_election_started.push_back(
nations::fixed_event{ int16_t(value), id, dcon::trigger_key{} });
}
}

void s_on_election_finished::any_value(std::string_view chance, association_type, int32_t event, error_handler& err,
int32_t line, scenario_building_context& context) {
int32_t value = parse_int(chance, line, err);
if(auto it = context.map_of_national_events.find(event); it != context.map_of_national_events.end()) {
context.state.national_definitions.on_election_finished.push_back(
nations::fixed_event{ int16_t(value), it->second.id, dcon::trigger_key{} });
} else {
auto id = context.state.world.create_national_event();
context.map_of_national_events.insert_or_assign(event,
pending_nat_event {id, trigger::slot_contents::nation, trigger::slot_contents::nation, trigger::slot_contents::empty});
context.state.national_definitions.on_election_finished.push_back(
nations::fixed_event{ int16_t(value), id, dcon::trigger_key{} });
}
}

void s_on_my_factories_nationalized::any_value(std::string_view chance, association_type, int32_t event, error_handler& err,
int32_t line, scenario_building_context& context) {
int32_t value = parse_int(chance, line, err);
Expand Down
12 changes: 12 additions & 0 deletions src/parsing/parsers_declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,16 @@ struct s_on_crisis_declare_interest {
void any_value(std::string_view chance, association_type, int32_t event, error_handler& err, int32_t line,
scenario_building_context& context);
};
struct s_on_election_started {
void finish(scenario_building_context&) { }
void any_value(std::string_view chance, association_type, int32_t event, error_handler& err, int32_t line,
scenario_building_context& context);
};
struct s_on_election_finished {
void finish(scenario_building_context&) { }
void any_value(std::string_view chance, association_type, int32_t event, error_handler& err, int32_t line,
scenario_building_context& context);
};

struct s_on_my_factories_nationalized {
void finish(scenario_building_context&) { }
Expand All @@ -2121,6 +2131,8 @@ struct on_action_file {
s_on_civilize on_civilize;
s_on_my_factories_nationalized on_my_factories_nationalized;
s_on_crisis_declare_interest on_crisis_declare_interest;
s_on_election_started on_election_started;
s_on_election_finished on_election_finished;
};

struct rebel_context {
Expand Down

0 comments on commit 3e74234

Please sign in to comment.