forked from github/server
extensive tests for armor calculation
This commit is contained in:
parent
cc89e9c9da
commit
f959f43f1e
|
@ -1089,13 +1089,13 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty
|
|||
if (armor) {
|
||||
ar += armor->prot;
|
||||
if (armor->projectile > 0 && chance(armor->projectile)) {
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (shield) {
|
||||
ar += shield->prot;
|
||||
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);
|
||||
if (ar < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (magic) {
|
||||
da = (int)(_max(da * res, 0));
|
||||
|
|
|
@ -204,28 +204,76 @@ static void test_building_defence_bonus(CuTest * tc)
|
|||
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)
|
||||
{
|
||||
troop dt;
|
||||
battle *b;
|
||||
region *r;
|
||||
unit *du;
|
||||
side *ds;
|
||||
fighter *df;
|
||||
weapon_type *awtype = 0, *dwtype = 0;
|
||||
int result;
|
||||
weapon_type *wtype;
|
||||
armor_type *ashield, *achain;
|
||||
item_type *ibelt, *ishield, *ichain;
|
||||
race *rc;
|
||||
double magres = 0.0;
|
||||
|
||||
test_cleanup();
|
||||
r = test_create_region(0, 0, 0);
|
||||
du = test_create_unit(test_create_faction(NULL), r);
|
||||
|
||||
b = make_battle(r);
|
||||
ds = make_side(b, du->faction, 0, 0, 0);
|
||||
df = make_fighter(b, du, ds, false);
|
||||
dt.fighter = df;
|
||||
ibelt = it_get_or_create(rt_get_or_create("trollbelt"));
|
||||
ishield = it_get_or_create(rt_get_or_create("shield"));
|
||||
ashield = new_armortype(ishield, 0.0, 0.5, 1, ATF_SHIELD);
|
||||
ichain = it_get_or_create(rt_get_or_create("chainmail"));
|
||||
achain = new_armortype(ichain, 0.0, 0.5, 3, ATF_NONE);
|
||||
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;
|
||||
result = calculate_armor(dt, dwtype, awtype, 0);
|
||||
CuAssertIntEquals(tc, 0, result);
|
||||
du = test_create_unit(test_create_faction(rc), r);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -277,7 +277,7 @@ weapon_type *new_weapontype(item_type * itype,
|
|||
weapon_type *wtype;
|
||||
|
||||
assert(minskill > 0);
|
||||
assert(resource2weapon(itype->rtype) == NULL);
|
||||
assert(itype && (!itype->rtype || !resource2weapon(itype->rtype)));
|
||||
|
||||
wtype = calloc(sizeof(weapon_type), 1);
|
||||
if (damage) {
|
||||
|
|
Loading…
Reference in New Issue