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

split units when embarking feature #1240

Merged
merged 3 commits into from
Apr 7, 2024
Merged
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
69 changes: 60 additions & 9 deletions src/gui/gui_unit_panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,17 +792,68 @@ class unit_details_load_army_button : public button_element_base {
bool visible = false;

void button_action(sys::state& state) noexcept override {
auto n = retrieve<dcon::army_id>(state, parent);
command::embark_army(state, state.local_player_nation, n);
auto a = retrieve<dcon::army_id>(state, parent);
auto p = state.world.army_get_location_from_army_location(a);
int32_t max_cap = 0;
for(auto n : state.world.province_get_navy_location(p)) {
if(n.get_navy().get_controller_from_navy_control() == state.local_player_nation &&
!bool(n.get_navy().get_battle_from_navy_battle_participation())) {
max_cap = std::max(military::free_transport_capacity(state, n.get_navy()), max_cap);
}
}
if(!military::can_embark_onto_sea_tile(state, state.local_player_nation, p, a)
&& max_cap > 0) { //require splitting
auto regs = state.world.army_get_army_membership(a);
int32_t army_cap = int32_t(regs.end() - regs.begin());
int32_t to_split = army_cap - max_cap;
//can mark 10 regiments to be split at a time
std::array<dcon::regiment_id, command::num_packed_units> data;
int32_t i = 0;
data.fill(dcon::regiment_id{});
for(auto reg : state.world.army_get_army_membership(a)) {
if(to_split == 0)
break;
//
data[i] = reg.get_regiment();
++i;
if(i >= int32_t(command::num_packed_units)) { //reached max allowed
command::mark_regiments_to_split(state, state.local_player_nation, data);
data.fill(dcon::regiment_id{});
i = 0;
}
//
--to_split;
}
if(i > 0) { //leftovers
command::mark_regiments_to_split(state, state.local_player_nation, data);
}
command::split_army(state, state.local_player_nation, a);
command::embark_army(state, state.local_player_nation, a);
} else { //no split
command::embark_army(state, state.local_player_nation, a);
}
}
void on_update(sys::state& state) noexcept override {
auto n = retrieve<dcon::army_id>(state, parent);
auto tprted = state.world.army_get_navy_from_army_transport(n);
auto loc = state.world.army_get_location_from_army_location(n);

visible = !bool(tprted);

disabled = !military::can_embark_onto_sea_tile(state, state.local_player_nation, loc, n);
auto a = retrieve<dcon::army_id>(state, parent);
auto p = state.world.army_get_location_from_army_location(a);
visible = !bool(state.world.army_get_navy_from_army_transport(a)); //not already in ship
disabled = true;
frame = 0;
if(visible) {
int32_t max_cap = 0;
for(auto n : state.world.province_get_navy_location(p)) {
if(n.get_navy().get_controller_from_navy_control() == state.local_player_nation &&
!bool(n.get_navy().get_battle_from_navy_battle_participation())) {
max_cap = std::max(military::free_transport_capacity(state, n.get_navy()), max_cap);
}
}
disabled = max_cap <= 0;
//require splitting
if(!military::can_embark_onto_sea_tile(state, state.local_player_nation, p, a)
&& max_cap > 0) {
frame = 1;
}
}
}
tooltip_behavior has_tooltip(sys::state& state) noexcept override {
return tooltip_behavior::variable_tooltip;
Expand Down
Loading