diff --git a/src/battle.c b/src/battle.c index fb91a1998..01b865d61 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1539,6 +1539,17 @@ static int get_tactics(const side * as, const side * ds) return result - defense; } +double tactics_chance(const unit *u, int skilldiff) { + double tacch = 0.1 * skilldiff; + if (fval(u->region->terrain, SEA_REGION)) { + const ship *sh = u->ship; + if (sh) { + tacch *= sh->type->tac_bonus; + } + } + return tacch; +} + static troop select_opponent(battle * b, troop at, int mindist, int maxdist) { fighter *af = at.fighter; @@ -1560,12 +1571,7 @@ static troop select_opponent(battle * b, troop at, int mindist, int maxdist) /* percentage chance to get this attack */ if (tactics > 0) { - double tacch = 0.1 * tactics; - if (fval(b->region->terrain, SEA_REGION)) { - ship *sh = at.fighter->unit->ship; - if (sh) - tacch *= sh->type->tac_bonus; - } + double tacch = tactics_chance(af->unit, tactics); if (!chance(tacch)) { dt.fighter = NULL; } diff --git a/src/battle.h b/src/battle.h index f2dd1fea3..c9429a6b3 100644 --- a/src/battle.h +++ b/src/battle.h @@ -270,6 +270,7 @@ extern "C" { const char *sidename(const struct side * s); void battle_message_faction(struct battle * b, struct faction * f, struct message *m); + double tactics_chance(const struct unit *u, int skilldiff); #ifdef __cplusplus } #endif diff --git a/src/battle.test.c b/src/battle.test.c index f293f967d..56ca24ac4 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -665,6 +666,22 @@ static void test_drain_exp(CuTest *tc) test_teardown(); } +static void test_tactics_chance(CuTest *tc) { + unit *u; + ship_type *stype; + + test_setup(); + u = test_create_unit(test_create_faction(NULL), test_create_ocean(0, 0)); + CuAssertDblEquals(tc, 0.1, tactics_chance(u, 1), 0.01); + CuAssertDblEquals(tc, 0.3, tactics_chance(u, 3), 0.01); + stype = test_create_shiptype("brot"); + u->ship = test_create_ship(u->region, stype); + CuAssertDblEquals(tc, 0.2, tactics_chance(u, 2), 0.01); + stype->tac_bonus = 2.0; + CuAssertDblEquals(tc, 0.4, tactics_chance(u, 2), 0.01); + test_teardown(); +} + CuSuite *get_battle_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -681,6 +698,7 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_natural_armor); SUITE_ADD_TEST(suite, test_magic_resistance); SUITE_ADD_TEST(suite, test_projectile_armor); + SUITE_ADD_TEST(suite, test_tactics_chance); DISABLE_TEST(suite, test_drain_exp); return suite; }