From 5ededa1083ce8fbc612516e77d2721b1dec05e3e Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Tue, 11 May 2021 12:11:51 +0200 Subject: [PATCH 6/7] get_defender(): take action parameter. Make get_defender() aware of what action is being performed when the information is there. See osdn #42226 --- ai/default/aiair.c | 2 +- ai/default/aihunt.c | 3 ++- ai/default/aiparatrooper.c | 4 ++-- ai/default/aiunit.c | 6 +++--- ai/default/daimilitary.c | 4 ++-- client/gui-sdl2/dialogs.c | 3 ++- common/actions.c | 3 ++- common/combat.c | 3 ++- common/combat.h | 3 ++- server/unithand.c | 2 +- server/unittools.c | 2 +- 11 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ai/default/aiair.c b/ai/default/aiair.c index c6fcb77e4a..7965f01173 100644 --- a/ai/default/aiair.c +++ b/ai/default/aiair.c @@ -131,7 +131,7 @@ static int dai_evaluate_tile_for_air_attack(struct unit *punit, #define PROB_MULTIPLIER 100 /* should unify with those in combat.c */ if (!can_unit_attack_tile(punit, NULL, dst_tile) - || !(pdefender = get_defender(punit, dst_tile))) { + || !(pdefender = get_defender(punit, dst_tile, NULL))) { return 0; } diff --git a/ai/default/aihunt.c b/ai/default/aihunt.c index 8b71fa82c9..7009cb986e 100644 --- a/ai/default/aihunt.c +++ b/ai/default/aihunt.c @@ -529,7 +529,8 @@ int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, * if any. */ dai_hunter_juiciness(pplayer, punit, target, &stackthreat, &stackcost); stackcost *= unit_win_chance(punit, get_defender(punit, - unit_tile(target))); + unit_tile(target), + NULL)); if (stackcost < unit_build_shield_cost_base(punit)) { UNIT_LOG(LOGLEVEL_HUNT, punit, "%d is too expensive (it %d vs us %d)", target->id, stackcost, diff --git a/ai/default/aiparatrooper.c b/ai/default/aiparatrooper.c index 08692f8e68..1c8f8a2b0a 100644 --- a/ai/default/aiparatrooper.c +++ b/ai/default/aiparatrooper.c @@ -167,9 +167,9 @@ static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait, } } unit_list_iterate_end; } else { - val += get_defender(punit, target)->hp * 100; + val += get_defender(punit, target, NULL)->hp * 100; } - val *= unit_win_chance(punit, get_defender(punit, target)); + val *= unit_win_chance(punit, get_defender(punit, target, NULL)); val += pterrain->defense_bonus / 10; val -= punit->hp * 100; diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c index 95f8d3039d..6f222f4ce8 100644 --- a/ai/default/aiunit.c +++ b/ai/default/aiunit.c @@ -455,7 +455,7 @@ static int dai_rampage_want(struct unit *punit, struct tile *ptile) CHECK_UNIT(punit); if (can_unit_attack_tile(punit, NULL, ptile) - && (pdef = get_defender(punit, ptile))) { + && (pdef = get_defender(punit, ptile, NULL))) { /* See description of kill_desire() about these variables. */ int attack = unit_att_rating_now(punit); int benefit = stack_cost(punit, pdef); @@ -1373,7 +1373,7 @@ int find_something_to_kill(struct ai_type *ait, struct player *pplayer, } if (can_unit_attack_tile(punit, NULL, city_tile(acity)) - && (pdefender = get_defender(punit, city_tile(acity)))) { + && (pdefender = get_defender(punit, city_tile(acity), NULL))) { vulnerability = unit_def_rating_squared(punit, pdefender); benefit = unit_build_shield_cost_base(pdefender); } else { @@ -1543,7 +1543,7 @@ int find_something_to_kill(struct ai_type *ait, struct player *pplayer, * We cannot use can_player_attack_tile, because we might not * be at war with aplayer yet */ if (!can_unit_attack_tile(punit, NULL, atile) - || aunit != get_defender(punit, atile)) { + || aunit != get_defender(punit, atile, NULL)) { /* We cannot attack it, or it is not the main defender. */ continue; } diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c index 6843d90ed9..3029a10c94 100644 --- a/ai/default/daimilitary.c +++ b/ai/default/daimilitary.c @@ -1321,7 +1321,7 @@ static struct adv_choice *kill_something_with(struct ai_type *ait, struct player def_vet = 0; } - pdef = get_defender(myunit, ptile); + pdef = get_defender(myunit, ptile, NULL); if (pdef) { int m = unittype_def_rating_squared(unit_type_get(myunit), unit_type_get(pdef), city_owner(acity), ptile, FALSE, @@ -1353,7 +1353,7 @@ static struct adv_choice *kill_something_with(struct ai_type *ait, struct player ferry_map = NULL; } - pdef = get_defender(myunit, ptile); + pdef = get_defender(myunit, ptile, NULL); if (!pdef) { /* Nobody to attack! */ goto cleanup; diff --git a/client/gui-sdl2/dialogs.c b/client/gui-sdl2/dialogs.c index a2182a53d3..27e7320c20 100644 --- a/client/gui-sdl2/dialogs.c +++ b/client/gui-sdl2/dialogs.c @@ -1891,7 +1891,8 @@ void popup_advanced_terrain_dialog(struct tile *ptile, Uint16 pos_x, Uint16 pos_ #define ADV_NUM_SEEN 15 - defender = (focus_unit ? get_defender(focus_unit, ptile) : NULL); + defender = (focus_unit ? get_defender(focus_unit, ptile, NULL) + : NULL); attacker = (focus_unit ? get_attacker(focus_unit, ptile) : NULL); for (i = 0; i < n; i++) { punit = unit_list_get(ptile->units, i); diff --git a/common/actions.c b/common/actions.c index a4a6efc3e7..cab339aa91 100644 --- a/common/actions.c +++ b/common/actions.c @@ -5654,7 +5654,8 @@ action_prob(const action_id wanted_action, break; case ACTRES_ATTACK: { - struct unit *defender_unit = get_defender(actor_unit, target_tile); + struct unit *defender_unit = get_defender(actor_unit, target_tile, + paction); if (can_player_see_unit(actor_player, defender_unit)) { double unconverted = unit_win_chance(actor_unit, defender_unit); diff --git a/common/combat.c b/common/combat.c index dc97e650b6..b3d4bd4dd2 100644 --- a/common/combat.c +++ b/common/combat.c @@ -796,7 +796,8 @@ static int get_defense_rating(const struct unit *attacker, this. ***********************************************************************/ struct unit *get_defender(const struct unit *attacker, - const struct tile *ptile) + const struct tile *ptile, + const struct action *paction) { struct unit *bestdef = NULL; int bestvalue = -99, best_cost = 0, rating_of_best = 0; diff --git a/common/combat.h b/common/combat.h index 991a5f41c9..abf3db3eb4 100644 --- a/common/combat.h +++ b/common/combat.h @@ -87,7 +87,8 @@ int get_total_attack_power(const struct unit *attacker, const struct unit *defender); struct unit *get_defender(const struct unit *attacker, - const struct tile *ptile); + const struct tile *ptile, + const struct action *paction); struct unit *get_attacker(const struct unit *defender, const struct tile *ptile); diff --git a/server/unithand.c b/server/unithand.c index fb7ee21763..21396221f9 100644 --- a/server/unithand.c +++ b/server/unithand.c @@ -4470,7 +4470,7 @@ static bool do_attack(struct unit *punit, struct tile *def_tile, struct unit *pdefender; const struct unit_type *act_utype = unit_type_get(punit); - if (!(pdefender = get_defender(punit, def_tile))) { + if (!(pdefender = get_defender(punit, def_tile, paction))) { /* Can't fight air... */ return FALSE; } diff --git a/server/unittools.c b/server/unittools.c index f1bdf0ce9b..98bc0e3472 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -3244,7 +3244,7 @@ static bool unit_survive_autoattack(struct unit *punit) autoattack_prob_list_iterate_safe(autoattack, peprob, penemy) { int sanity2 = penemy->id; struct tile *ptile = unit_tile(penemy); - struct unit *enemy_defender = get_defender(punit, ptile); + struct unit *enemy_defender = get_defender(punit, ptile, NULL); double punitwin, penemywin; double threshold = 0.25; struct tile *tgt_tile = unit_tile(punit); -- 2.30.2