From 41dade86fd207d2a639eec499c4c1e20a816d665 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Wed, 15 May 2024 18:38:11 +0200 Subject: [PATCH 5/5] Pass briber unit to Unit_Bribe_Cost_Pct effects as other_context See RM #620 Signed-off-by: Alina Lenk --- ai/default/aidiplomat.c | 2 +- client/text.c | 4 ++-- common/unit.c | 45 ++++++++++++++++++++++++----------------- common/unit.h | 3 ++- server/diplomats.c | 2 +- server/unithand.c | 3 ++- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/ai/default/aidiplomat.c b/ai/default/aidiplomat.c index a9abb25d80..81193c258a 100644 --- a/ai/default/aidiplomat.c +++ b/ai/default/aidiplomat.c @@ -648,7 +648,7 @@ static bool dai_diplomat_bribe_nearby(struct ai_type *ait, } /* Should we make the expense? */ - cost = unit_bribe_cost(pvictim, pplayer); + cost = unit_bribe_cost(pvictim, pplayer, punit); if (!threat) { /* Don't empty our treasure without good reason! */ gold_avail = pplayer->economic.gold - dai_gold_reserve(pplayer); diff --git a/client/text.c b/client/text.c index 1fb71803ca..467cd28114 100644 --- a/client/text.c +++ b/client/text.c @@ -426,11 +426,11 @@ const char *popup_info_text(struct tile *ptile) if (owner == plr || client_is_global_observer()) { /* Show bribe cost for own units. */ astr_add_line(&str, _("Probable bribe cost: %d"), - unit_bribe_cost(punit, nullptr)); + unit_bribe_cost(punit, nullptr, nullptr)); } else { /* We can only give an (lower) boundary for units of other players. */ astr_add_line(&str, _("Estimated bribe cost: > %d"), - unit_bribe_cost(punit, plr)); + unit_bribe_cost(punit, plr, nullptr)); } if ((plr == nullptr || owner == plr) diff --git a/common/unit.c b/common/unit.c index 6690b5bbf2..77005192cf 100644 --- a/common/unit.c +++ b/common/unit.c @@ -2337,11 +2337,13 @@ void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, unit. For a damaged unit the price is reduced. For a veteran unit, it is increased. - @param punit Unit to bribe - @param briber Player that wants to bribe - @return Bribe cost + @param punit Unit to bribe + @param briber Player that wants to bribe + @param briber_unit Unit that does the bribing + @return Bribe cost **************************************************************************/ -int unit_bribe_cost(struct unit *punit, struct player *briber) +int unit_bribe_cost(const struct unit *punit, const struct player *briber, + const struct unit *briber_unit) { int cost, default_hp; struct tile *ptile = unit_tile(punit); @@ -2378,21 +2380,26 @@ int unit_bribe_cost(struct unit *punit, struct player *briber) cost *= unit_build_shield_cost_base(punit) / 10.0; /* Rule set specific cost modification */ - cost += (cost - * get_target_bonus_effects(NULL, - &(const struct req_context) { - .player = owner, - .city = game_city_by_number( - punit->homecity - ), - .tile = ptile, - .unit = punit, - .unittype = ptype, - }, - &(const struct req_context) { - .player = briber, - }, - EFT_UNIT_BRIBE_COST_PCT)) + cost += (cost * get_target_bonus_effects(NULL, + &(const struct req_context) { + .player = owner, + .city = game_city_by_number(punit->homecity), + .tile = ptile, + .unit = punit, + .unittype = ptype, + }, + &(const struct req_context) { + .player = briber, + .unit = briber_unit, + .unittype = briber_unit ? unit_type_get(briber_unit) + : nullptr, + .tile = briber_unit ? unit_tile(briber_unit) + : nullptr, + .city = briber_unit + ? game_city_by_number(briber_unit->homecity) + : nullptr, + }, + EFT_UNIT_BRIBE_COST_PCT)) / 100; /* Veterans are not cheap. */ diff --git a/common/unit.h b/common/unit.h index 718f16c179..5d4f166a1b 100644 --- a/common/unit.h +++ b/common/unit.h @@ -532,7 +532,8 @@ void *unit_ai_data(const struct unit *punit, const struct ai_type *ai); void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data); -int unit_bribe_cost(struct unit *punit, struct player *briber); +int unit_bribe_cost(const struct unit *punit, const struct player *briber, + const struct unit *briber_unit); bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force); diff --git a/server/diplomats.c b/server/diplomats.c index 1a4e4920b3..7daf79d0ff 100644 --- a/server/diplomats.c +++ b/server/diplomats.c @@ -682,7 +682,7 @@ bool diplomat_bribe(struct player *pplayer, struct unit *pdiplomat, log_debug("bribe-unit: unit: %d", pdiplomat->id); /* Get bribe cost, ignoring any previously saved value. */ - bribe_cost = unit_bribe_cost(pvictim, pplayer); + bribe_cost = unit_bribe_cost(pvictim, pplayer, pdiplomat); /* If player doesn't have enough gold, can't bribe. */ if (pplayer->economic.gold < bribe_cost) { diff --git a/server/unithand.c b/server/unithand.c index a53dbd395a..b8f34f15df 100644 --- a/server/unithand.c +++ b/server/unithand.c @@ -3220,7 +3220,8 @@ void handle_unit_action_query(struct connection *pc, pactor, punit)) { dsend_packet_unit_action_answer(pc, actor_id, target_id, - unit_bribe_cost(punit, pplayer), + unit_bribe_cost(punit, pplayer, + pactor), action_type, request_kind); } else { illegal_action(pplayer, pactor, action_type, -- 2.34.1