BUG 2346: extract a test for ship tactics bonus.

This commit is contained in:
Enno Rehling 2018-05-03 23:00:28 +02:00
parent 34c6222b8f
commit b31a1f798c
3 changed files with 31 additions and 6 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -10,6 +10,7 @@
#include <kernel/item.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/ship.h>
#include <kernel/unit.h>
#include <spells/buildingcurse.h>
@ -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;
}