From 62ee1468b8cc5ff1a6fd5e1957368be9fe07148d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 5 Dec 2015 17:17:21 +0100 Subject: [PATCH] https://bugs.eressea.de/view.php?id=2166 * fix magic resistance calculation in calculate_armor. * add related tests. * refactor BF_CANATTACK -> BF_NO_ATTACK because it is easier. --- src/battle.c | 4 +-- src/battle.test.c | 66 ++++++++++++++++++++++++++++++++++++++++++ src/buildno.h | 2 +- src/kernel/race.c | 2 +- src/kernel/race.h | 2 +- src/kernel/race.test.c | 1 + src/kernel/xmlreader.c | 6 ++-- src/tests.c | 1 + 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/battle.c b/src/battle.c index f57e7b377..7541e54a6 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1181,7 +1181,7 @@ terminate(troop dt, troop at, int type, const char *damage, bool missile) const weapon_type *dwtype = NULL; const weapon_type *awtype = NULL; const weapon *weapon; - double res = 0.0; + double res = 1.0; int rda, sk = 0, sd; bool magic = false; @@ -3996,7 +3996,7 @@ static bool start_battle(region * r, battle ** bp) continue; } - if ((u_race(u)->battle_flags & BF_CANATTACK) == 0) { + if (u_race(u)->battle_flags & BF_NO_ATTACK) { ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "race_no_attack", "race", u_race(u))); continue; diff --git a/src/battle.test.c b/src/battle.test.c index 02213d35b..c995bf9dc 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -248,6 +248,7 @@ static void test_calculate_armor(CuTest * tc) i_change(&du->items, ishield, 1); i_change(&du->items, ichain, 1); dt.fighter = setup_fighter(&b, du); + rc->battle_flags &= ~BF_EQUIPMENT; CuAssertIntEquals_Msg(tc, "require BF_EQUIPMENT", 1, calculate_armor(dt, 0, 0, 0)); free_battle(b); @@ -274,6 +275,70 @@ static void test_calculate_armor(CuTest * tc) test_cleanup(); } +static void test_magic_resistance(CuTest *tc) +{ + troop dt; + battle *b; + region *r; + unit *du; + 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); + 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"); + du = test_create_unit(test_create_faction(rc), r); + dt.index = 0; + + dt.fighter = setup_fighter(&b, du); + calculate_armor(dt, 0, 0, &magres); + CuAssertDblEquals_Msg(tc, "magres unmodified", 0.0, magres, 0.01); + + magres = 1.0; + calculate_armor(dt, 0, 0, &magres); + CuAssertDblEquals_Msg(tc, "no magres bonus", 0.0, magic_resistance(du), 0.01); + CuAssertDblEquals_Msg(tc, "no magres bonus", 1.0, magres, 0.01); + + ashield->flags |= ATF_LAEN; + ashield->magres = 0.1; + magres = 1.0; + calculate_armor(dt, 0, 0, &magres); + free_battle(b); + + i_change(&du->items, ishield, 1); + i_change(&du->items, ichain, 1); + achain->flags |= ATF_LAEN; + achain->magres = 0.1; + ashield->flags |= ATF_LAEN; + ashield->magres = 0.1; + dt.fighter = setup_fighter(&b, du); + magres = 1.0; + calculate_armor(dt, 0, 0, &magres); + CuAssertDblEquals_Msg(tc, "laen bonus", 0.81, magres, 0.01); + free_battle(b); + + i_change(&du->items, ishield, -1); + i_change(&du->items, ichain, -1); + set_level(du, SK_MAGIC, 2); + dt.fighter = setup_fighter(&b, du); + magres = 1.0; + calculate_armor(dt, 0, 0, &magres); + CuAssertDblEquals_Msg(tc, "magic bonus", 0.1, magic_resistance(du), 0.01); + CuAssertDblEquals_Msg(tc, "magic bonus", 0.7, magres, 0.01); + + free_battle(b); + test_cleanup(); +} + static void test_projectile_armor(CuTest * tc) { troop dt; @@ -321,6 +386,7 @@ CuSuite *get_battle_suite(void) SUITE_ADD_TEST(suite, test_building_bonus_respects_size); SUITE_ADD_TEST(suite, test_building_defence_bonus); SUITE_ADD_TEST(suite, test_calculate_armor); + SUITE_ADD_TEST(suite, test_magic_resistance); SUITE_ADD_TEST(suite, test_projectile_armor); return suite; } diff --git a/src/buildno.h b/src/buildno.h index 78925eaed..e058f0b23 100644 --- a/src/buildno.h +++ b/src/buildno.h @@ -1,3 +1,3 @@ #define VERSION_MAJOR 3 #define VERSION_MINOR 7 -#define VERSION_BUILD 3 +#define VERSION_BUILD 4 diff --git a/src/kernel/race.c b/src/kernel/race.c index 7d881fe7f..81246f47c 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -183,7 +183,7 @@ race *rc_get_or_create(const char *zName) rc->recruit_multi = 1.0F; rc->regaura = 1.0F; rc->speed = 1.0F; - rc->battle_flags = BF_CANATTACK; + rc->battle_flags = 0; if (strchr(zName, ' ') != NULL) { log_error("race '%s' has an invalid name. remove spaces\n", zName); assert(strchr(zName, ' ') == NULL); diff --git a/src/kernel/race.h b/src/kernel/race.h index 0ae925c5d..1b4c58a87 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -231,7 +231,7 @@ extern "C" { #define BF_RES_CUT (1<<3) /* Halber Schaden durch CUT */ #define BF_RES_BASH (1<<4) /* Halber Schaden durch BASH */ #define BF_INV_NONMAGIC (1<<5) /* Immun gegen nichtmagischen Schaden */ -#define BF_CANATTACK (1<<6) /* Kann keine ATTACKIERE Befehle ausfuehren */ +#define BF_NO_ATTACK (1<<6) /* Kann keine ATTACKIERE Befehle ausfuehren */ extern int unit_old_max_hp(struct unit *u); extern const char *racename(const struct locale *lang, const struct unit *u, diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index ef4fc640e..aa37093d4 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -36,6 +36,7 @@ static void test_rc_defaults(CuTest *tc) { CuAssertIntEquals(tc, 0, rc->armor); CuAssertIntEquals(tc, 0, rc->at_bonus); CuAssertIntEquals(tc, 0, rc->df_bonus); + CuAssertIntEquals(tc, 0, rc->battle_flags); CuAssertIntEquals(tc, PERSON_WEIGHT, rc->weight); test_cleanup(); } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 28a351a72..6ea451442 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1721,7 +1721,7 @@ static int parse_races(xmlDocPtr doc) rc->ec_flags |= ECF_REC_UNLIMITED; if (xml_bvalue(node, "equipment", false)) - rc->battle_flags |= BF_EQUIPMENT; + rc->battle_flags |= BF_EQUIPMENT; // TODO: invert this flag, so rc_get_or_create gets simpler if (xml_bvalue(node, "noblock", false)) rc->battle_flags |= BF_NOBLOCK; if (xml_bvalue(node, "invinciblenonmagic", false)) @@ -1732,8 +1732,8 @@ static int parse_races(xmlDocPtr doc) rc->battle_flags |= BF_RES_CUT; if (xml_bvalue(node, "resistpierce", false)) rc->battle_flags |= BF_RES_PIERCE; - if (xml_bvalue(node, "canattack", true)) - rc->battle_flags |= BF_CANATTACK; // TODO: invert this flag, so rc_get_or_create gets simpler + if (xml_bvalue(node, "noattack", false)) + rc->battle_flags |= BF_NO_ATTACK; for (child = node->children; child; child = child->next) { if (strcmp((const char *)child->name, "ai") == 0) { diff --git a/src/tests.c b/src/tests.c index 8cf97bede..f8071914d 100644 --- a/src/tests.c +++ b/src/tests.c @@ -36,6 +36,7 @@ struct race *test_create_race(const char *name) race *rc = rc_get_or_create(name); rc->maintenance = 10; rc->ec_flags |= GETITEM; + rc->battle_flags = BF_EQUIPMENT; return rc; }