extensive tests for armor calculation

This commit is contained in:
Enno Rehling 2015-11-03 18:59:09 +01:00
parent cc89e9c9da
commit f959f43f1e
3 changed files with 66 additions and 15 deletions

View File

@ -1089,13 +1089,13 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty
if (armor) { if (armor) {
ar += armor->prot; ar += armor->prot;
if (armor->projectile > 0 && chance(armor->projectile)) { if (armor->projectile > 0 && chance(armor->projectile)) {
return false; return -1;
} }
} }
if (shield) { if (shield) {
ar += shield->prot; ar += shield->prot;
if (shield->projectile > 0 && chance(shield->projectile)) { if (shield->projectile > 0 && chance(shield->projectile)) {
return false; return -1;
} }
} }
@ -1209,6 +1209,9 @@ terminate(troop dt, troop at, int type, const char *damage, bool missile)
} }
ar = calculate_armor(dt, dwtype, awtype, magic ? &res : 0); ar = calculate_armor(dt, dwtype, awtype, magic ? &res : 0);
if (ar < 0) {
return false;
}
if (magic) { if (magic) {
da = (int)(_max(da * res, 0)); da = (int)(_max(da * res, 0));

View File

@ -204,28 +204,76 @@ static void test_building_defence_bonus(CuTest * tc)
test_cleanup(); test_cleanup();
} }
fighter *setup_fighter(battle **bp, unit *u) {
battle *b;
*bp = b = make_battle(u->region);
return make_fighter(b, u, make_side(b, u->faction, 0, 0, 0), false);
}
static void test_calculate_armor(CuTest * tc) static void test_calculate_armor(CuTest * tc)
{ {
troop dt; troop dt;
battle *b; battle *b;
region *r; region *r;
unit *du; unit *du;
side *ds; weapon_type *wtype;
fighter *df; armor_type *ashield, *achain;
weapon_type *awtype = 0, *dwtype = 0; item_type *ibelt, *ishield, *ichain;
int result; race *rc;
double magres = 0.0;
test_cleanup(); test_cleanup();
r = test_create_region(0, 0, 0); r = test_create_region(0, 0, 0);
du = test_create_unit(test_create_faction(NULL), r); ibelt = it_get_or_create(rt_get_or_create("trollbelt"));
ishield = it_get_or_create(rt_get_or_create("shield"));
b = make_battle(r); ashield = new_armortype(ishield, 0.0, 0.5, 1, ATF_SHIELD);
ds = make_side(b, du->faction, 0, 0, 0); ichain = it_get_or_create(rt_get_or_create("chainmail"));
df = make_fighter(b, du, ds, false); achain = new_armortype(ichain, 0.0, 0.5, 3, ATF_NONE);
dt.fighter = df; wtype = new_weapontype(it_get_or_create(rt_get_or_create("sword")), 0, 0.5, 0, 0, 0, 0, SK_MELEE, 1);
rc = test_create_race("human");
dt.index = 0; dt.index = 0;
result = calculate_armor(dt, dwtype, awtype, 0); du = test_create_unit(test_create_faction(rc), r);
CuAssertIntEquals(tc, 0, result);
dt.fighter = setup_fighter(&b, du);
CuAssertIntEquals_Msg(tc, "default ac", 0, calculate_armor(dt, 0, 0, &magres));
CuAssertDblEquals_Msg(tc, "magres unmodified", 0.0, magres, 0.01);
free_battle(b);
i_change(&du->items, ibelt, 1);
dt.fighter = setup_fighter(&b, du);
CuAssertIntEquals_Msg(tc, "magical armor", 1, calculate_armor(dt, 0, 0, 0));
rc->armor = 2;
CuAssertIntEquals_Msg(tc, "natural armor", 3, calculate_armor(dt, 0, 0, 0));
rc->armor = 0;
free_battle(b);
i_change(&du->items, ishield, 1);
i_change(&du->items, ichain, 1);
dt.fighter = setup_fighter(&b, du);
CuAssertIntEquals_Msg(tc, "require BF_EQUIPMENT", 1, calculate_armor(dt, 0, 0, 0));
free_battle(b);
rc->battle_flags |= BF_EQUIPMENT;
dt.fighter = setup_fighter(&b, du);
CuAssertIntEquals_Msg(tc, "stack equipment rc", 5, calculate_armor(dt, 0, 0, 0));
rc->armor = 2;
CuAssertIntEquals_Msg(tc, "natural armor adds 50%", 6, calculate_armor(dt, 0, 0, 0));
wtype->flags = WTF_NONE;
CuAssertIntEquals_Msg(tc, "regular weapon has no effect", 6, calculate_armor(dt, 0, wtype, 0));
wtype->flags = WTF_ARMORPIERCING;
CuAssertIntEquals_Msg(tc, "armor piercing weapon", 3, calculate_armor(dt, 0, wtype, 0));
wtype->flags = WTF_NONE;
CuAssertIntEquals_Msg(tc, "magical attack", 3, calculate_armor(dt, 0, 0, &magres));
CuAssertDblEquals_Msg(tc, "magres unmodified", 0.0, magres, 0.01);
ashield->flags |= ATF_LAEN;
achain->flags |= ATF_LAEN;
magres = 1.0;
CuAssertIntEquals_Msg(tc, "laen armor", 3, calculate_armor(dt, 0, 0, &magres));
CuAssertDblEquals_Msg(tc, "laen magres bonus", 0.25, magres, 0.01);
free_battle(b);
test_cleanup(); test_cleanup();
} }

View File

@ -277,7 +277,7 @@ weapon_type *new_weapontype(item_type * itype,
weapon_type *wtype; weapon_type *wtype;
assert(minskill > 0); assert(minskill > 0);
assert(resource2weapon(itype->rtype) == NULL); assert(itype && (!itype->rtype || !resource2weapon(itype->rtype)));
wtype = calloc(sizeof(weapon_type), 1); wtype = calloc(sizeof(weapon_type), 1);
if (damage) { if (damage) {