Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/schombert/Project-Alice int…
Browse files Browse the repository at this point in the history
…o HEAD
  • Loading branch information
Lizzie841 committed Apr 7, 2024
2 parents 0f81da1 + f33848c commit 6531f62
Show file tree
Hide file tree
Showing 21 changed files with 324 additions and 156 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ list(APPEND ALICE_INCREMENTAL_SOURCES_LIST
"src/gui/gui_modifier_tooltips.cpp"
"src/gui/gui_province_window.cpp"
"src/gui/gui_trigger_tooltips.cpp"
"src/gui/gui_error_window.cpp"
"src/gui/map_tooltip.cpp"
"src/gui/topbar_subwindows/gui_population_window.cpp"
"src/gui/topbar_subwindows/gui_production_window.cpp"
Expand Down
2 changes: 1 addition & 1 deletion src/entry_point_nix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ int main(int argc, char **argv) {
game_state.quit_signaled.store(true, std::memory_order_release);
update_thread.join();

network::finish(game_state);
network::finish(game_state, true);
return EXIT_SUCCESS;
}
4 changes: 2 additions & 2 deletions src/entry_point_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ int WINAPI wWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPWSTR
std::this_thread::sleep_for(std::chrono::milliseconds(15));
}
//Reload savefile
network::finish(game_state);
network::finish(game_state, true);
game_state.actual_game_speed = 0;
//
if(sys::try_read_scenario_and_save_file(game_state, parsed_cmd[1])) {
Expand Down Expand Up @@ -246,7 +246,7 @@ int WINAPI wWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPWSTR
update_thread.join();
}

network::finish(game_state);
network::finish(game_state, true);
CoUninitialize();
}
return 0;
Expand Down
95 changes: 51 additions & 44 deletions src/gamestate/diplomatic_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,38 @@ bool can_accept_crisis_peace_offer(sys::state& state, dcon::nation_id from, dcon
return true;
}

bool can_accept(sys::state& state, message const& m) {
switch(m.type) {
case type::none:
return true;
case type::access_request:
return command::can_ask_for_access(state, m.from, m.to, true);
case type::alliance_request:
return command::can_ask_for_alliance(state, m.from, m.to, true);
case type::call_ally_request:
return command::can_call_to_arms(state, m.from, m.to, m.data.war, true);
case type::be_crisis_primary_attacker:
case type::be_crisis_primary_defender:
case type::peace_offer:
return true;
case type::take_crisis_side_offer:
return can_accept_crisis_offer(state, m.from, m.to, m.data.crisis_offer);
case type::crisis_peace_offer:
return can_accept_crisis_peace_offer(state, m.from, m.to, m.data.peace);
case type::state_transfer:
return command::can_state_transfer(state, m.from, m.to, m.data.state);
}
return true;
}

void accept(sys::state& state, message const& m) {
if(!can_accept(state, m))
return;

switch(m.type) {
case type::none:
break;
case type::access_request: {
if(!command::can_ask_for_access(state, m.from, m.to, true))
return;

nations::adjust_relationship(state, m.from, m.to, state.defines.askmilaccess_relation_on_accept);
auto rel = state.world.get_unilateral_relationship_by_unilateral_pair(m.to, m.from);
if(!rel) {
Expand All @@ -294,25 +318,16 @@ void accept(sys::state& state, message const& m) {
m.to, m.from, dcon::nation_id{},
sys::message_base_type::mil_access_start
});

break;
}
case type::alliance_request: {
if(!command::can_ask_for_alliance(state, m.from, m.to, true))
return;

nations::adjust_relationship(state, m.from, m.to, state.defines.alliance_relation_on_accept);
nations::make_alliance(state, m.from, m.to);
break;
}
case type::call_ally_request: {
if(!command::can_call_to_arms(state, m.from, m.to, m.data.war, true))
return;

military::add_to_war(state, m.data.war, m.to, military::is_attacker(state, m.data.war, m.from));

nations::adjust_relationship(state, m.from, m.to, state.defines.callally_relation_on_accept);

notification::post(state, notification::message{
[from = m.from, to = m.to, pa = state.world.war_get_primary_attacker(m.data.war), pd = state.world.war_get_primary_defender(m.data.war), name = state.world.war_get_name(m.data.war), tag = state.world.war_get_over_tag(m.data.war), st = state.world.war_get_over_state(m.data.war)](sys::state& state, text::layout_base& contents) {
text::substitution_map sub;
Expand Down Expand Up @@ -343,45 +358,37 @@ void accept(sys::state& state, message const& m) {
military::implement_peace_offer(state, m.data.peace);
break;
case type::take_crisis_side_offer:
if(can_accept_crisis_offer(state, m.from, m.to, m.data.crisis_offer)) {
add_to_crisis_with_offer(state, m.from, m.to, m.data.crisis_offer);

notification::post(state, notification::message{
[source = m.from, target = m.to](sys::state& state, text::layout_base& contents) {
text::add_line(state, contents, "msg_crisis_joffer_accepted_1", text::variable_type::x, target, text::variable_type::y, source);
add_to_crisis_with_offer(state, m.from, m.to, m.data.crisis_offer);
notification::post(state, notification::message{
[source = m.from, target = m.to](sys::state& state, text::layout_base& contents) {
text::add_line(state, contents, "msg_crisis_joffer_accepted_1", text::variable_type::x, target, text::variable_type::y, source);

},
"msg_crisis_joffer_accepted",
m.to, m.from, dcon::nation_id{},
sys::message_base_type::crisis_join_offer_accepted
});
}
},
"msg_crisis_joffer_accepted",
m.to, m.from, dcon::nation_id{},
sys::message_base_type::crisis_join_offer_accepted
});
break;
case type::crisis_peace_offer:
if(can_accept_crisis_peace_offer(state, m.from, m.to, m.data.peace)) {
nations::accept_crisis_peace_offer(state, m.from, m.to, m.data.peace);

notification::post(state, notification::message{
[source = m.from, target = m.to](sys::state& state, text::layout_base& contents) {
text::add_line(state, contents, "msg_crisis_settled_1", text::variable_type::x, target, text::variable_type::y, source);
},
"msg_crisis_settled_title",
m.to, m.from, dcon::nation_id{},
sys::message_base_type::crisis_resolution_accepted
});
}
nations::accept_crisis_peace_offer(state, m.from, m.to, m.data.peace);
notification::post(state, notification::message{
[source = m.from, target = m.to](sys::state& state, text::layout_base& contents) {
text::add_line(state, contents, "msg_crisis_settled_1", text::variable_type::x, target, text::variable_type::y, source);
},
"msg_crisis_settled_title",
m.to, m.from, dcon::nation_id{},
sys::message_base_type::crisis_resolution_accepted
});
break;
case type::state_transfer:
if(command::can_state_transfer(state, m.from, m.to, m.data.state)) {
for(const auto ab : state.world.state_definition_get_abstract_state_membership(m.data.state)) {
if(ab.get_province().get_province_ownership().get_nation() == m.from) {
province::change_province_owner(state, ab.get_province(), m.to);
}
for(const auto ab : state.world.state_definition_get_abstract_state_membership(m.data.state)) {
if(ab.get_province().get_province_ownership().get_nation() == m.from) {
province::change_province_owner(state, ab.get_province(), m.to);
}
province::update_connected_regions(state);
province::update_cached_values(state);
nations::update_cached_values(state);
}
province::update_connected_regions(state);
province::update_cached_values(state);
nations::update_cached_values(state);
break;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gamestate/diplomatic_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bool ai_will_accept(sys::state& state, message const& m);

void decline(sys::state& state, message const& m);
void accept(sys::state& state, message const& m);
bool can_accept(sys::state& state, message const& m);

void post(sys::state& state, message const& m);
void update_pending(sys::state& state);
Expand Down
1 change: 1 addition & 0 deletions src/gamestate/system_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "gui_land_combat.hpp"
#include "gui_chat_window.hpp"
#include "gui_state_select.hpp"
#include "gui_error_window.hpp"
#include "gui_diplomacy_request_topbar.hpp"
#include "map_tooltip.hpp"
#include "unit_tooltip.hpp"
Expand Down
7 changes: 4 additions & 3 deletions src/gui/gui_diplomacy_request_topbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ class diplomatic_message_topbar_listbox : public overlapping_listbox_element_bas
public:
std::vector< diplomatic_message::message> messages;
void on_update(sys::state& state) noexcept override {
auto it = std::remove_if(messages.begin(), messages.end(),
[&](auto& m) { return m.when + diplomatic_message::expiration_in_days <= state.current_date; });
auto r = std::distance(it, messages.end());
auto it = std::remove_if(messages.begin(), messages.end(), [&](auto& m) {
return m.when + diplomatic_message::expiration_in_days <= state.current_date
|| !diplomatic_message::can_accept(state, m);
});
messages.erase(it, messages.end());

row_contents.clear();
Expand Down
7 changes: 4 additions & 3 deletions src/gui/gui_diplomacy_request_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,10 @@ class diplomacy_request_window : public window_element_base {
}

void on_update(sys::state& state) noexcept override {
auto it = std::remove_if(messages.begin(), messages.end(),
[&](auto& m) { return m.when + diplomatic_message::expiration_in_days <= state.current_date; });
auto r = std::distance(it, messages.end());
auto it = std::remove_if(messages.begin(), messages.end(), [&](auto& m) {
return m.when + diplomatic_message::expiration_in_days <= state.current_date
|| !diplomatic_message::can_accept(state, m);
});
messages.erase(it, messages.end());

if(messages.empty()) {
Expand Down
83 changes: 83 additions & 0 deletions src/gui/gui_error_window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "gui_error_window.hpp"

namespace ui {

void error_body_text::populate_layout(sys::state& state, text::endless_layout& contents) noexcept {
auto box = text::open_layout_box(contents);
text::add_to_layout_box(state, contents, box, msg);
text::close_layout_box(contents, box);
}
void error_body_text::on_create(sys::state& state) noexcept {
base_data.size.y = int16_t(150);
scrollable_text::on_create(state);
}
void error_body_text::on_update(sys::state& state) noexcept {
text::alignment align = text::alignment::left;
switch(base_data.data.text.get_alignment()) {
case ui::alignment::right:
align = text::alignment::right;
break;
case ui::alignment::centered:
align = text::alignment::center;
break;
default:
break;
}
auto border = base_data.data.text.border_size;
auto color = delegate->black_text ? text::text_color::black : text::text_color::white;
auto container = text::create_endless_layout(
delegate->internal_layout,
text::layout_parameters{
border.x,
border.y,
int16_t(base_data.size.x - border.x * 2),
int16_t(base_data.size.y - border.y * 2),
base_data.data.text.font_handle,
0,
align,
color,
false });
populate_layout(state, container);
}

std::unique_ptr<element_base> error_dialog_window::make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept {
if(name == "title") {
auto ptr = make_element_by_type<simple_text_element_base>(state, id);
title = ptr.get();
return ptr;
} else if(name == "description") {
auto ptr = make_element_by_type<error_body_text>(state, id);
body = ptr.get();
return ptr;
} else if(name == "agreebutton") {
return make_element_by_type<generic_close_button>(state, id);
} else if(name == "background") {
auto ptr = make_element_by_type<draggable_target>(state, id);
ptr->base_data.size = base_data.size;
return ptr;
} else {
return nullptr;
}
}
void popup_error_window(sys::state& state, std::string_view title, std::string_view body) {
if(state.ui_state.error_win == nullptr) {
auto new_elm = ui::make_element_by_type<ui::error_dialog_window>(state, "defaultinfodialog");
state.ui_state.error_win = new_elm.get();
if(state.mode == sys::game_mode_type::pick_nation) {
state.ui_state.nation_picker->add_child_to_front(std::move(new_elm));
} else if(state.mode == sys::game_mode_type::in_game) {
state.ui_state.root->add_child_to_front(std::move(new_elm));
} else if(state.mode == sys::game_mode_type::end_screen) {
state.ui_state.end_screen->add_child_to_front(std::move(new_elm));
} else if(state.mode == sys::game_mode_type::select_states) {
state.ui_state.select_states_legend->add_child_to_front(std::move(new_elm));
}
}

auto win = static_cast<ui::error_dialog_window*>(state.ui_state.error_win);
win->title->set_text(state, std::string(title));
win->body->msg = std::string(body);
win->set_visible(state, true);
win->impl_on_update(state);
}
}
22 changes: 22 additions & 0 deletions src/gui/gui_error_window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "gui_common_elements.hpp"
#include "gui_element_types.hpp"
#include <string_view>

namespace ui {
class error_body_text : public scrollable_text {
void populate_layout(sys::state& state, text::endless_layout& contents) noexcept;
public:
std::string msg = "";
void on_create(sys::state& state) noexcept override;
void on_update(sys::state& state) noexcept override;
};
class error_dialog_window : public window_element_base {
public:
simple_text_element_base* title = nullptr;
error_body_text* body = nullptr;
std::unique_ptr<element_base> make_child(sys::state& state, std::string_view name, dcon::gui_def_id id) noexcept override;
};
void popup_error_window(sys::state& state, std::string_view title, std::string_view body);
} // namespace ui
1 change: 1 addition & 0 deletions src/gui/gui_graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ struct state {
element_base* map_rank_legend = nullptr;
element_base* map_rec_legend = nullptr;
element_base* tl_chat_list = nullptr;
element_base* error_win = nullptr;
element_base* naval_combat_window = nullptr;
element_base* army_combat_window = nullptr;
element_base* change_leader_window = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/gui/gui_nation_picker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ class select_save_game : public button_element_base {
if(i->is_new_game) {
if(!sys::try_read_scenario_as_save_file(state, state.loaded_scenario_file)) {
auto msg = std::string("Scenario file ") + simple_fs::native_to_utf8(state.loaded_scenario_file) + " could not be loaded.";
window::emit_error_message(msg, false);
ui::popup_error_window(state, "Save Error", msg);
} else {
loaded = true;
}
} else {
if(!sys::try_read_save_file(state, i->file_name)) {
auto msg = std::string("Save file ") + simple_fs::native_to_utf8(i->file_name) + " could not be loaded.";
window::emit_error_message(msg, false);
ui::popup_error_window(state, "Save Error", msg);
} else {
loaded = true;
}
Expand Down
Loading

0 comments on commit 6531f62

Please sign in to comment.