From 3f0274fcebb6489bc4222a1d78f752c0b1588b96 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Feb 2023 02:21:04 +0200 Subject: [PATCH 17/17] sdl2: Make Alliance clause available in diplomacy dialog See osdn #46604 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/diplodlg.c | 4 ++-- common/player.c | 41 ++++++++++++++++++++----------------- common/player.h | 2 +- server/sanitycheck.c | 1 + server/savegame/savegame2.c | 3 ++- server/savegame/savegame3.c | 3 ++- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/client/gui-sdl2/diplodlg.c b/client/gui-sdl2/diplodlg.c index a09c837b2f..abeb233548 100644 --- a/client/gui-sdl2/diplodlg.c +++ b/client/gui-sdl2/diplodlg.c @@ -503,7 +503,7 @@ static struct advanced_dialog *popup_diplomatic_objects(struct player *pplayer0, bool peace = clause_enabled(CLAUSE_PEACE) && type != DS_PEACE && type != DS_TEAM; bool alliance = clause_enabled(CLAUSE_ALLIANCE) - && pplayer_can_make_treaty(pplayer0, pplayer1, DS_ALLIANCE); + && pplayer_can_make_treaty(pplayer0, pplayer1, DS_ALLIANCE) == DIPL_OK; if (ceasefire || peace || alliance) { buf = create_iconlabel_from_chars(NULL, pwindow->dst, @@ -1584,7 +1584,7 @@ void popup_diplomacy_dialog(struct player *pplayer) fc_snprintf(cbuf, sizeof(cbuf), _("Cancel Treaty")); } - /* cancel treaty */ + /* Cancel treaty */ buf = create_themeicon_button_from_chars(current_theme->units2_icon, pwindow->dst, cbuf, adj_font(12), 0); diff --git a/common/player.c b/common/player.c index 42e9ed5940..a96444015f 100644 --- a/common/player.c +++ b/common/player.c @@ -90,10 +90,10 @@ enum diplstate_type cancel_pact_result(enum diplstate_type oldstate) } /*******************************************************************//** - The senate may not allow you to break the treaty. In this - case you must first dissolve the senate then you can break - it. This is waived if you have statue of liberty since you - could easily just dissolve and then recreate it. + The senate may not allow you to break the treaty. In this + case you must first dissolve the senate then you can break + it. This is waived if you have statue of liberty since you + could easily just dissolve and then recreate it. ***********************************************************************/ enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1, const struct player *p2) @@ -114,21 +114,22 @@ enum dipl_reason pplayer_can_cancel_treaty(const struct player *p1, && get_player_bonus(p1, EFT_NO_ANARCHY) <= 0) { return DIPL_SENATE_BLOCKING; } + return DIPL_OK; } /*******************************************************************//** - Returns true iff p1 can be in alliance with p2. + Returns TRUE iff p1 can be in alliance with p2. - Check that we are not at war with any of p2's allies. Note - that for an alliance to be made, we need to check this both + Check that we are not at war with any of p2's allies. Note + that for an alliance to be made, we need to check this both ways. - The reason for this is to avoid the dread 'love-love-hate' + The reason for this is to avoid the dread 'love-love-hate' triad, in which p1 is allied to p2 is allied to p3 is at war with p1. These lead to strange situations. ***********************************************************************/ -static bool is_valid_alliance(const struct player *p1, +static bool is_valid_alliance(const struct player *p1, const struct player *p2) { players_iterate_alive(pplayer) { @@ -136,7 +137,7 @@ static bool is_valid_alliance(const struct player *p1, if (pplayer != p1 && pplayer != p2 - && ds == DS_WAR /* do not count 'never met' as war here */ + && ds == DS_WAR /* Do not count 'never met' as war here */ && pplayers_allied(p2, pplayer)) { return FALSE; } @@ -146,7 +147,8 @@ static bool is_valid_alliance(const struct player *p1, } /*******************************************************************//** - Returns true iff p1 can make given treaty with p2. + Returns the reason p1 can't make given treaty with p2, + or DIPL_OK if they can. We cannot regress in a treaty chain. So we cannot suggest 'Peace' if we are in 'Alliance'. Then you have to cancel. @@ -168,17 +170,17 @@ enum dipl_reason pplayer_can_make_treaty(const struct player *p1, || get_player_bonus(p2, EFT_NO_DIPLOMACY) > 0) { return DIPL_ERROR; } - if (treaty == DS_WAR - || treaty == DS_NO_CONTACT - || treaty == DS_ARMISTICE + if (treaty == DS_WAR + || treaty == DS_NO_CONTACT + || treaty == DS_ARMISTICE || treaty == DS_TEAM || treaty == DS_LAST) { - return DIPL_ERROR; /* these are not negotiable treaties */ + return DIPL_ERROR; /* These are not negotiable treaties */ } if (treaty == DS_CEASEFIRE && existing != DS_WAR) { - return DIPL_ERROR; /* only available from war */ + return DIPL_ERROR; /* Only available from war */ } - if (treaty == DS_PEACE + if (treaty == DS_PEACE && (existing != DS_WAR && existing != DS_CEASEFIRE)) { return DIPL_ERROR; } @@ -191,15 +193,16 @@ enum dipl_reason pplayer_can_make_treaty(const struct player *p1, return DIPL_ALLIANCE_PROBLEM_THEM; } } - /* this check must be last: */ + /* This check must be last: */ if (treaty == existing) { return DIPL_ERROR; } + return DIPL_OK; } /*******************************************************************//** - Check if pplayer has an embassy with pplayer2. We always have + Check if pplayer has an embassy with pplayer2. We always have an embassy with ourselves. ***********************************************************************/ bool player_has_embassy(const struct player *pplayer, diff --git a/common/player.h b/common/player.h index 37854f7e94..57ecf3e855 100644 --- a/common/player.h +++ b/common/player.h @@ -201,7 +201,7 @@ enum dipl_reason { DIPL_ALLIANCE_PROBLEM_US, DIPL_ALLIANCE_PROBLEM_THEM }; -/* the following are for "pacts" */ +/* The following are for "pacts" */ struct player_diplstate { enum diplstate_type type; /* this player's disposition towards other */ enum diplstate_type max_state; /* maximum treaty level ever had */ diff --git a/server/sanitycheck.c b/server/sanitycheck.c index 66e561edad..95bd54103b 100644 --- a/server/sanitycheck.c +++ b/server/sanitycheck.c @@ -563,6 +563,7 @@ static void check_players(const char *file, const char *function, int line) && pplayers_allied(pplayer, pplayer2)) { enum dipl_reason allied_players_can_be_allied = pplayer_can_make_treaty(pplayer, pplayer2, DS_ALLIANCE); + SANITY_CHECK(allied_players_can_be_allied != DIPL_ALLIANCE_PROBLEM_US); SANITY_CHECK(allied_players_can_be_allied diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index 7513c4ce74..b0d2516199 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -2740,6 +2740,7 @@ static void sg_load_players(struct loaddata *loading) if (pplayers_allied(plr, aplayer)) { enum dipl_reason can_ally = pplayer_can_make_treaty(plr, aplayer, DS_ALLIANCE); + if (can_ally == DIPL_ALLIANCE_PROBLEM_US || can_ally == DIPL_ALLIANCE_PROBLEM_THEM) { log_sg("Illegal alliance structure detected: " @@ -2763,7 +2764,7 @@ static void sg_load_players(struct loaddata *loading) } cities_iterate_end; } - /* Update all city information. This must come after all cities are + /* Update all city information. This must come after all cities are * loaded (in player_load) but before player (dumb) cities are loaded * in player_load_vision(). */ players_iterate(plr) { diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 8ced397ee7..1ef846627e 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -3828,6 +3828,7 @@ static void sg_load_players(struct loaddata *loading) if (pplayers_allied(plr, aplayer)) { enum dipl_reason can_ally = pplayer_can_make_treaty(plr, aplayer, DS_ALLIANCE); + if (can_ally == DIPL_ALLIANCE_PROBLEM_US || can_ally == DIPL_ALLIANCE_PROBLEM_THEM) { log_sg("Illegal alliance structure detected: " @@ -3851,7 +3852,7 @@ static void sg_load_players(struct loaddata *loading) } cities_iterate_end; } - /* Update all city information. This must come after all cities are + /* Update all city information. This must come after all cities are * loaded (in player_load) but before player (dumb) cities are loaded * in player_load_vision(). */ players_iterate(plr) { -- 2.39.1