From ac61019dd3f01fb2e3c74a01c5dc9665b4953590 Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Tue, 11 May 2021 12:15:13 +0200 Subject: [PATCH 08/11] unit_win_chance(): take action parameter. Make unit_win_chance() aware of what action is being performed when the information is there. See osdn #42229 --- ai/default/aiair.c | 2 +- ai/default/aihunt.c | 3 ++- ai/default/aiparatrooper.c | 3 ++- ai/default/aiunit.c | 2 +- client/gui-sdl2/dialogs.c | 4 ++-- client/text.c | 5 +++-- common/actions.c | 3 ++- common/combat.c | 7 ++++--- common/combat.h | 3 ++- server/unittools.c | 2 +- 10 files changed, 20 insertions(+), 14 deletions(-) diff --git a/ai/default/aiair.c b/ai/default/aiair.c index 7965f01173..5cff7afbac 100644 --- a/ai/default/aiair.c +++ b/ai/default/aiair.c @@ -156,7 +156,7 @@ static int dai_evaluate_tile_for_air_attack(struct unit *punit, } unit_attack = (int) (PROB_MULTIPLIER - * unit_win_chance(punit, pdefender)); + * unit_win_chance(punit, pdefender, NULL)); victim_defence = PROB_MULTIPLIER - unit_attack; diff --git a/ai/default/aihunt.c b/ai/default/aihunt.c index 7009cb986e..ccd598796c 100644 --- a/ai/default/aihunt.c +++ b/ai/default/aihunt.c @@ -530,7 +530,8 @@ int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, dai_hunter_juiciness(pplayer, punit, target, &stackthreat, &stackcost); stackcost *= unit_win_chance(punit, get_defender(punit, unit_tile(target), - NULL)); + NULL), + 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 1c8f8a2b0a..cf994ea4fa 100644 --- a/ai/default/aiparatrooper.c +++ b/ai/default/aiparatrooper.c @@ -169,7 +169,8 @@ static struct tile *find_best_tile_to_paradrop_to(struct ai_type *ait, } else { val += get_defender(punit, target, NULL)->hp * 100; } - val *= unit_win_chance(punit, get_defender(punit, target, NULL)); + val *= unit_win_chance(punit, get_defender(punit, target, NULL), + NULL); val += pterrain->defense_bonus / 10; val -= punit->hp * 100; diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c index 6f222f4ce8..f6a93a45d8 100644 --- a/ai/default/aiunit.c +++ b/ai/default/aiunit.c @@ -475,7 +475,7 @@ static int dai_rampage_want(struct unit *punit, struct tile *ptile) /* If we have non-zero attack rating... */ if (attack > 0 && is_my_turn(punit, pdef)) { - double chance = unit_win_chance(punit, pdef); + double chance = unit_win_chance(punit, pdef, NULL); int desire = avg_benefit(benefit, loss, chance); /* No need to amortize, our operation takes one turn. */ diff --git a/client/gui-sdl2/dialogs.c b/client/gui-sdl2/dialogs.c index 27e7320c20..59aad77510 100644 --- a/client/gui-sdl2/dialogs.c +++ b/client/gui-sdl2/dialogs.c @@ -194,10 +194,10 @@ static bool sdl_get_chance_to_win(int *att_chance, int *def_chance, } /* chance to win when active unit is attacking the selected unit */ - *att_chance = unit_win_chance(my_unit, enemy_unit) * 100; + *att_chance = unit_win_chance(my_unit, enemy_unit, NULL) * 100; /* chance to win when selected unit is attacking the active unit */ - *def_chance = (1.0 - unit_win_chance(enemy_unit, my_unit)) * 100; + *def_chance = (1.0 - unit_win_chance(enemy_unit, my_unit, NULL)) * 100; return TRUE; } diff --git a/client/text.c b/client/text.c index 7f281094cd..68d879509b 100644 --- a/client/text.c +++ b/client/text.c @@ -405,8 +405,9 @@ const char *popup_info_text(struct tile *ptile) unit_list_iterate(ptile->units, tile_unit) { if (unit_owner(tile_unit) != unit_owner(pfocus_unit)) { - int att = unit_win_chance(pfocus_unit, tile_unit) * 100; - int def = (1.0 - unit_win_chance(tile_unit, pfocus_unit)) * 100; + int att = unit_win_chance(pfocus_unit, tile_unit, NULL) * 100; + int def = (1.0 - unit_win_chance(tile_unit, pfocus_unit, + NULL)) * 100; found = TRUE; diff --git a/common/actions.c b/common/actions.c index aff00e0161..2fe4eba544 100644 --- a/common/actions.c +++ b/common/actions.c @@ -5664,7 +5664,8 @@ action_prob(const action_id wanted_action, paction); if (can_player_see_unit(actor_player, defender_unit)) { - double unconverted = unit_win_chance(actor_unit, defender_unit); + double unconverted = unit_win_chance(actor_unit, defender_unit, + paction); chance.min = MAX(ACTPROB_VAL_MIN, floor((double)ACTPROB_VAL_MAX * unconverted)); diff --git a/common/combat.c b/common/combat.c index 9ebe2ba00d..dfbd330503 100644 --- a/common/combat.c +++ b/common/combat.c @@ -464,7 +464,8 @@ Returns a double in the range [0;1] indicating the attackers chance of winning. The calculation takes all factors into account. ***********************************************************************/ double unit_win_chance(const struct unit *attacker, - const struct unit *defender) + const struct unit *defender, + const struct action *paction) { int def_power = get_total_defense_power(attacker, defender); int att_power = get_total_attack_power(attacker, defender); @@ -822,7 +823,7 @@ struct unit *get_defender(const struct unit *attacker, int defense_rating = get_defense_rating(attacker, defender); /* This will make units roughly evenly good defenders look alike. */ int unit_def - = (int) (100000 * (1 - unit_win_chance(attacker, defender))); + = (int) (100000 * (1 - unit_win_chance(attacker, defender, paction))); fc_assert_action(0 <= unit_def, continue); @@ -875,7 +876,7 @@ struct unit *get_attacker(const struct unit *defender, if (pplayers_allied(unit_owner(defender), unit_owner(attacker))) { return NULL; } - unit_a = (int) (100000 * (unit_win_chance(attacker, defender))); + unit_a = (int) (100000 * (unit_win_chance(attacker, defender, NULL))); if (unit_a > bestvalue || (unit_a == bestvalue && build_cost < best_cost)) { bestvalue = unit_a; diff --git a/common/combat.h b/common/combat.h index 2c68b37424..19030cda3e 100644 --- a/common/combat.h +++ b/common/combat.h @@ -63,7 +63,8 @@ void get_modified_firepower(const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp); double unit_win_chance(const struct unit *attacker, - const struct unit *defender); + const struct unit *defender, + const struct action *paction); bool unit_really_ignores_citywalls(const struct unit *punit); struct city *sdi_try_defend(const struct player *owner, diff --git a/server/unittools.c b/server/unittools.c index 98bc0e3472..3a3b5c17ff 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -3257,7 +3257,7 @@ static bool unit_survive_autoattack(struct unit *punit) } if (NULL != enemy_defender) { - punitwin = unit_win_chance(punit, enemy_defender); + punitwin = unit_win_chance(punit, enemy_defender, NULL); } else { /* 'penemy' can attack 'punit' but it may be not reciproque. */ punitwin = 1.0; -- 2.30.2