Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DONOT MERGE YET] opt: is_core bitfield optimization #913

Closed
wants to merge 5 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/gamestate/dcon_generated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,11 @@ object {
name{ former_rebel_controller }
type{ dcon::rebel_faction_id }
}

property{
name{ is_core }
type{array{national_identity_id}{bitfield}}
}
}

relationship{
Expand Down
7 changes: 7 additions & 0 deletions src/gamestate/system_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3492,6 +3492,13 @@ void state::fill_unsaved_data() { // reconstructs derived values that are not di
world.nation_resize_max_building_level(economy::max_building_types);

world.province_resize_modifier_values(provincial_mod_offsets::count);
world.province_resize_is_core(world.national_identity_size());
for(auto p : world.in_province) {
for(auto ni : world.in_national_identity) {
p.set_is_core(ni, false);
}
}
world.for_each_core([&](dcon::core_id id) { world.province_set_is_core(world.core_get_province(id), world.core_get_identity(id), true); });

world.nation_resize_demographics(demographics::size(*this));
world.state_instance_resize_demographics(demographics::size(*this));
Expand Down
27 changes: 14 additions & 13 deletions src/military/military.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ int32_t supply_limit_in_province(sys::state& state, dcon::nation_id n, dcon::pro
} else if(auto uni_rel = state.world.get_unilateral_relationship_by_unilateral_pair(prov_controller, n);
state.world.unilateral_relationship_get_military_access(uni_rel)) {
modifier = 2.0f;
} else if(bool(state.world.get_core_by_prov_tag_key(p, state.world.nation_get_identity_from_identity_holder(n)))) {
} else if(state.world.nation_get_identity_from_identity_holder(n) && bool(state.world.province_get_is_core(p, state.world.nation_get_identity_from_identity_holder(n)))) {
modifier = 2.0f;
} else if(state.world.province_get_siege_progress(p) > 0.0f) {
modifier = 2.0f;
Expand Down Expand Up @@ -2927,8 +2927,8 @@ void implement_war_goal(sys::state& state, dcon::war_id war, dcon::cb_type_id wa
if(prov.get_province().get_nation_from_province_ownership() == target) {
province::conquer_province(state, prov.get_province(), holder);
if((bits & cb_flag::po_remove_cores) != 0) {
auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag);
if(find_core) {
state.world.province_set_is_core(prov.get_province(), target_tag, false);
if(auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag); find_core) {
state.world.delete_core(find_core);
}
}
Expand All @@ -2948,8 +2948,8 @@ void implement_war_goal(sys::state& state, dcon::war_id war, dcon::cb_type_id wa
if(prov.get_province().get_nation_from_province_ownership() == target) {
province::conquer_province(state, prov.get_province(), holder);
if((bits & cb_flag::po_remove_cores) != 0) {
auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag);
if(find_core) {
state.world.province_set_is_core(prov.get_province(), target_tag, false);
if(auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag); find_core) {
state.world.delete_core(find_core);
}
}
Expand All @@ -2968,8 +2968,8 @@ void implement_war_goal(sys::state& state, dcon::war_id war, dcon::cb_type_id wa
if(prov.get_province().get_nation_from_province_ownership() == target) {
province::conquer_province(state, prov.get_province(), from);
if((bits & cb_flag::po_remove_cores) != 0) {
auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag);
if(find_core) {
state.world.province_set_is_core(prov.get_province(), target_tag, false);
if(auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag); find_core) {
state.world.delete_core(find_core);
}
}
Expand All @@ -2989,8 +2989,8 @@ void implement_war_goal(sys::state& state, dcon::war_id war, dcon::cb_type_id wa
if(prov.get_province().get_nation_from_province_ownership() == target) {
province::conquer_province(state, prov.get_province(), from);
if((bits & cb_flag::po_remove_cores) != 0) {
auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag);
if(find_core) {
state.world.province_set_is_core(prov.get_province(), target_tag, false);
if(auto find_core = state.world.get_core_by_prov_tag_key(prov.get_province(), target_tag); find_core) {
state.world.delete_core(find_core);
}
}
Expand Down Expand Up @@ -3021,8 +3021,8 @@ void implement_war_goal(sys::state& state, dcon::war_id war, dcon::cb_type_id wa
auto prov = (*target_owns.begin()).get_province();
province::conquer_province(state, prov, from);
if((bits & cb_flag::po_remove_cores) != 0) {
auto find_core = state.world.get_core_by_prov_tag_key(prov, target_tag);
if(find_core) {
state.world.province_set_is_core(prov, target_tag, false);
if(auto find_core = state.world.get_core_by_prov_tag_key(prov, target_tag); find_core) {
state.world.delete_core(find_core);
}
}
Expand Down Expand Up @@ -6398,8 +6398,9 @@ void update_siege_progress(sys::state& state) {
auto army_stats = army_controller ? army_controller : ar.get_army().get_army_rebel_control().get_controller().get_ruler_from_rebellion_within();

owner_involved = owner_involved || owner == army_controller;
core_owner_involved =
core_owner_involved || bool(state.world.get_core_by_prov_tag_key(prov, state.world.nation_get_identity_from_identity_holder(army_controller)));
core_owner_involved = core_owner_involved
|| (state.world.nation_get_identity_from_identity_holder(army_controller)
&& bool(state.world.province_get_is_core(prov, state.world.nation_get_identity_from_identity_holder(army_controller))));

for(auto r : ar.get_army().get_army_membership()) {
auto reg_str = r.get_regiment().get_strength();
Expand Down
17 changes: 10 additions & 7 deletions src/nations/nations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,8 @@ bool has_reform_available(sys::state& state, dcon::nation_id n) {
}

bool has_decision_available(sys::state& state, dcon::nation_id n) {
if(!n)
return false;
for(uint32_t i = state.world.decision_size(); i-- > 0;) {
dcon::decision_id did{dcon::decision_id::value_base_t(i)};
if(n != state.local_player_nation || !state.world.decision_get_hide_notification(did)) {
Expand All @@ -930,7 +932,6 @@ bool has_decision_available(sys::state& state, dcon::nation_id n) {
}
}
}

return false;
}

Expand Down Expand Up @@ -1691,10 +1692,9 @@ bool can_put_flashpoint_focus_in_state(sys::state& state, dcon::state_instance_i
if(fp_focus_nation.get_rank() > uint16_t(state.defines.colonial_rank)) {
auto d = si.get_definition();
for(auto p : state.world.state_definition_get_abstract_state_membership(d)) {
if(p.get_province().get_nation_from_province_ownership() == owner) {
if(state.world.get_core_by_prov_tag_key(p.get_province(), fp_ident))
return true;
}
if(p.get_province().get_nation_from_province_ownership() == owner
&& state.world.province_get_is_core(p.get_province(), fp_ident))
return true;
}
}

Expand Down Expand Up @@ -2610,6 +2610,7 @@ void update_pop_acceptance(sys::state& state, dcon::nation_id n) {
}
}


void liberate_nation_from(sys::state& state, dcon::national_identity_id liberated, dcon::nation_id from) {
if(!liberated)
return;
Expand Down Expand Up @@ -2648,8 +2649,8 @@ void release_nation_from(sys::state& state, dcon::national_identity_id liberated
nations::create_nation_based_on_template(state, holder, from);
}
for(auto c : state.world.national_identity_get_core(liberated)) {
if(c.get_province().get_nation_from_province_ownership() == from &&
!(state.world.get_core_by_prov_tag_key(c.get_province(), source_tag))) {
if(c.get_province().get_nation_from_province_ownership() == from
&& !(state.world.get_core_by_prov_tag_key(c.get_province(), source_tag))) {
province::change_province_owner(state, c.get_province(), holder);
}
}
Expand All @@ -2661,7 +2662,9 @@ void release_nation_from(sys::state& state, dcon::national_identity_id liberated
void remove_cores_from_owned(sys::state& state, dcon::nation_id n, dcon::national_identity_id tag) {
for(auto prov : state.world.nation_get_province_ownership(n)) {
if(auto core = state.world.get_core_by_prov_tag_key(prov.get_province(), tag); core) {
assert(state.world.province_get_is_core(prov.get_province(), tag));
state.world.delete_core(core);
state.world.province_set_is_core(prov.get_province(), tag, false);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/provinces/province.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,7 @@ void change_province_owner(sys::state& state, dcon::province_id id, dcon::nation
military::eject_ships(state, id);
military::update_blackflag_status(state, id);

state.world.province_set_is_owner_core(id,
bool(state.world.get_core_by_prov_tag_key(id, state.world.nation_get_identity_from_identity_holder(new_owner))));
state.world.province_set_is_owner_core(id, bool(state.world.province_get_is_core(id, state.world.nation_get_identity_from_identity_holder(new_owner))));

if(old_si) {
dcon::province_id a_province;
Expand Down Expand Up @@ -1528,6 +1527,7 @@ bool state_is_coastal_non_core_nb(sys::state& state, dcon::state_instance_id s)
void add_core(sys::state& state, dcon::province_id prov, dcon::national_identity_id tag) {
if(tag && prov) {
state.world.try_create_core(prov, tag);
state.world.province_set_is_core(prov, tag, true);
if(state.world.province_get_nation_from_province_ownership(prov) ==
state.world.national_identity_get_nation_from_identity_holder(tag)) {
state.world.province_set_is_owner_core(prov, true);
Expand All @@ -1538,7 +1538,9 @@ void add_core(sys::state& state, dcon::province_id prov, dcon::national_identity
void remove_core(sys::state& state, dcon::province_id prov, dcon::national_identity_id tag) {
auto core_rel = state.world.get_core_by_prov_tag_key(prov, tag);
if(core_rel) {
assert(state.world.province_get_is_core(prov, tag)); //in sync
state.world.delete_core(core_rel);
state.world.province_set_is_core(prov, tag, false);
if(state.world.province_get_nation_from_province_ownership(prov) ==
state.world.national_identity_get_nation_from_identity_holder(tag)) {
state.world.province_set_is_owner_core(prov, false);
Expand Down
62 changes: 17 additions & 45 deletions src/scripting/triggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ TRIGGER_FUNCTION(tf_have_core_in_nation_tag) {
auto h = ws.world.national_identity_get_nation_from_identity_holder(trigger::payload(tval[1]).tag_id);
return compare_to_true(tval[0], ve::apply([&](dcon::national_identity_id n) {
for(auto p : ws.world.nation_get_province_ownership(h)) {
if(ws.world.get_core_by_prov_tag_key(p.get_province(), n))
if(n && ws.world.province_get_is_core(p.get_province(), n))
return true;
}
return false;
Expand All @@ -1651,7 +1651,7 @@ TRIGGER_FUNCTION(tf_have_core_in_nation_tag) {
TRIGGER_FUNCTION(tf_have_core_in_nation_this) {
return compare_to_true(tval[0], ve::apply([&](dcon::national_identity_id n, dcon::nation_id h) {
for(auto p : ws.world.nation_get_province_ownership(h)) {
if(ws.world.get_core_by_prov_tag_key(p.get_province(), n))
if(n && ws.world.province_get_is_core(p.get_province(), n))
return true;
}
return false;
Expand All @@ -1660,7 +1660,7 @@ TRIGGER_FUNCTION(tf_have_core_in_nation_this) {
TRIGGER_FUNCTION(tf_have_core_in_nation_from) {
return compare_to_true(tval[0], ve::apply([&](dcon::national_identity_id n, dcon::nation_id h) {
for(auto p : ws.world.nation_get_province_ownership(h)) {
if(ws.world.get_core_by_prov_tag_key(p.get_province(), n))
if(n && ws.world.province_get_is_core(p.get_province(), n))
return true;
}
return false;
Expand Down Expand Up @@ -1856,11 +1856,7 @@ TRIGGER_FUNCTION(tf_is_core_integer) {
auto tag = ws.world.nation_get_identity_from_identity_holder(to_nation(primary_slot));
auto result = ve::apply(
[&ws, pid](dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
tag);
return compare_to_true(tval[0], result);
Expand All @@ -1869,11 +1865,7 @@ TRIGGER_FUNCTION(tf_is_core_this_nation) {
auto tag = ws.world.nation_get_identity_from_identity_holder(to_nation(this_slot));
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), tag);
return compare_to_true(tval[0], result);
Expand All @@ -1883,11 +1875,7 @@ TRIGGER_FUNCTION(tf_is_core_this_state) {
auto tag = ws.world.nation_get_identity_from_identity_holder(owner);
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), tag);
return compare_to_true(tval[0], result);
Expand All @@ -1897,11 +1885,7 @@ TRIGGER_FUNCTION(tf_is_core_this_province) {
auto tag = ws.world.nation_get_identity_from_identity_holder(owner);
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), tag);
return compare_to_true(tval[0], result);
Expand All @@ -1911,11 +1895,7 @@ TRIGGER_FUNCTION(tf_is_core_this_pop) {
auto tag = ws.world.nation_get_identity_from_identity_holder(owner);
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), tag);
return compare_to_true(tval[0], result);
Expand All @@ -1924,11 +1904,7 @@ TRIGGER_FUNCTION(tf_is_core_from_nation) {
auto tag = ws.world.nation_get_identity_from_identity_holder(to_nation(from_slot));
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), tag);
return compare_to_true(tval[0], result);
Expand All @@ -1937,11 +1913,7 @@ TRIGGER_FUNCTION(tf_is_core_reb) {
auto rtags = ws.world.rebel_faction_get_defection_target(to_rebel(from_slot));
auto result = ve::apply(
[&ws](dcon::province_id pid, dcon::national_identity_id t) {
for(auto c : ws.world.province_get_core(pid)) {
if(c.get_identity() == t)
return true;
}
return false;
return t && ws.world.province_get_is_core(pid, t);
},
to_prov(primary_slot), rtags);
return compare_to_true(tval[0], result);
Expand All @@ -1951,7 +1923,7 @@ TRIGGER_FUNCTION(tf_is_core_state_from_nation) {
auto result = ve::apply(
[&ws](dcon::state_instance_id sid, dcon::national_identity_id t) {
for(auto p : ws.world.state_definition_get_abstract_state_membership(ws.world.state_instance_get_definition(sid))) {
if(!(ws.world.get_core_by_prov_tag_key(p.get_province(), t)))
if(!(t && ws.world.province_get_is_core(p.get_province(), t)))
return false;
}
return true;
Expand All @@ -1964,7 +1936,7 @@ TRIGGER_FUNCTION(tf_is_core_state_this_nation) {
auto result = ve::apply(
[&ws](dcon::state_instance_id sid, dcon::national_identity_id t) {
for(auto p : ws.world.state_definition_get_abstract_state_membership(ws.world.state_instance_get_definition(sid))) {
if(!(ws.world.get_core_by_prov_tag_key(p.get_province(), t)))
if(!(t && ws.world.province_get_is_core(p.get_province(), t)))
return false;
}
return true;
Expand All @@ -1977,7 +1949,7 @@ TRIGGER_FUNCTION(tf_is_core_state_this_province) {
auto result = ve::apply(
[&ws](dcon::state_instance_id sid, dcon::national_identity_id t) {
for(auto p : ws.world.state_definition_get_abstract_state_membership(ws.world.state_instance_get_definition(sid))) {
if(!(ws.world.get_core_by_prov_tag_key(p.get_province(), t)))
if(!(t && ws.world.province_get_is_core(p.get_province(), t)))
return false;
}
return true;
Expand All @@ -1990,7 +1962,7 @@ TRIGGER_FUNCTION(tf_is_core_state_this_pop) {
auto result = ve::apply(
[&ws](dcon::state_instance_id sid, dcon::national_identity_id t) {
for(auto p : ws.world.state_definition_get_abstract_state_membership(ws.world.state_instance_get_definition(sid))) {
if(!(ws.world.get_core_by_prov_tag_key(p.get_province(), t)))
if(!(t && ws.world.province_get_is_core(p.get_province(), t)))
return false;
}
return true;
Expand All @@ -2004,15 +1976,15 @@ TRIGGER_FUNCTION(tf_is_core_boolean) {
TRIGGER_FUNCTION(tf_is_core_tag) {
auto result = ve::apply(
[&ws, t = trigger::payload(tval[1]).tag_id](dcon::province_id pid) {
return bool(ws.world.get_core_by_prov_tag_key(pid, t));
return t && bool(ws.world.province_get_is_core(pid, t));
},
to_prov(primary_slot));
return compare_to_true(tval[0], result);
}
TRIGGER_FUNCTION(tf_is_core_pop_tag) {
auto result = ve::apply(
[&ws, t = trigger::payload(tval[1]).tag_id](dcon::province_id pid) {
return bool(ws.world.get_core_by_prov_tag_key(pid, t));
return t && bool(ws.world.province_get_is_core(pid, t));
},
ws.world.pop_get_province_from_pop_location(to_pop(primary_slot)));
return compare_to_true(tval[0], result);
Expand All @@ -2021,7 +1993,7 @@ TRIGGER_FUNCTION(tf_is_core_state_tag) {
auto result = ve::apply(
[&ws, t = trigger::payload(tval[1]).tag_id](dcon::state_instance_id sid) {
for(auto p : ws.world.state_definition_get_abstract_state_membership(ws.world.state_instance_get_definition(sid))) {
if(!(ws.world.get_core_by_prov_tag_key(p.get_province(), t)))
if(!(t && ws.world.province_get_is_core(p.get_province(), t)))
return false;
}
return true;
Expand Down