From 5fc15878795df002011e542b88c1129cdf3163ec Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 25 Aug 2015 22:50:58 +0200 Subject: [PATCH 1/4] age_unit accidentally returned AT_AGE_REMOVE (caused by a recent change to curse_age) --- src/kernel/curse.c | 2 +- src/util/attrib.c | 2 +- src/util/attrib.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kernel/curse.c b/src/kernel/curse.c index afa8ac2c3..828552a3a 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -131,7 +131,7 @@ int curse_age(attrib * a) else if (c->duration != INT_MAX) { c->duration = _max(0, c->duration - 1); } - return c->duration; + return (c->duration > 0) ? AT_AGE_KEEP : AT_AGE_REMOVE; } void destroy_curse(curse * c) diff --git a/src/util/attrib.c b/src/util/attrib.c index fb50d8dfc..26869cfee 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -256,7 +256,7 @@ int a_age(attrib ** p) if (a->type->age) { int result = a->type->age(a); assert(result >= 0 || !"age() returned a negative value"); - if (result == 0) { + if (result == AT_AGE_REMOVE) { a_remove(p, a); continue; } diff --git a/src/util/attrib.h b/src/util/attrib.h index cbba4ebf4..c235fd67a 100644 --- a/src/util/attrib.h +++ b/src/util/attrib.h @@ -90,8 +90,8 @@ extern "C" { #define AT_READ_OK 0 #define AT_READ_FAIL -1 -#define AT_AGE_KEEP 0 /* keep the attribute for another turn */ -#define AT_AGE_REMOVE 1 /* remove the attribute after calling age() */ +#define AT_AGE_REMOVE 0 /* remove the attribute after calling age() */ +#define AT_AGE_KEEP 1 /* keep the attribute for another turn */ #ifdef __cplusplus } From 4dee1744a22809ad4fa7a0bfd46c1dc3b33ab6b2 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 26 Aug 2015 08:22:51 +0200 Subject: [PATCH 2/4] write some tests around effskill and skillmod. --- src/kernel/unit.test.c | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 7f281fc36..9c00fd304 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -239,6 +239,60 @@ static void test_default_name(CuTest *tc) { test_cleanup(); } +static int cb_skillmod(const unit *u, const region *r, skill_t sk, int level) { + unused_arg(u); + unused_arg(r); + unused_arg(sk); + return level + 3; +} + +static void test_skillmod(CuTest *tc) { + unit *u; + attrib *a; + + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + set_level(u, SK_ARMORER, 5); + CuAssertIntEquals(tc, 5, effskill(u, SK_ARMORER)); + + a_add(&u->attribs, a = make_skillmod(SK_ARMORER, SMF_ALWAYS, 0, 2.0, 0)); + CuAssertIntEquals(tc, 10, effskill(u, SK_ARMORER)); + a_remove(&u->attribs, a); + + a_add(&u->attribs, a = make_skillmod(SK_ARMORER, SMF_ALWAYS, 0, 0, 2)); + CuAssertIntEquals(tc, 7, effskill(u, SK_ARMORER)); + a_remove(&u->attribs, a); + + a_add(&u->attribs, a = make_skillmod(SK_ARMORER, SMF_ALWAYS, cb_skillmod, 0, 0)); + CuAssertIntEquals(tc, 8, effskill(u, SK_ARMORER)); + a_remove(&u->attribs, a); + + test_cleanup(); +} + +static void test_skill_hunger(CuTest *tc) { + unit *u; + + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + set_level(u, SK_ARMORER, 6); + set_level(u, SK_SAILING, 6); + fset(u, UFL_HUNGER); + + set_param(&global.parameters, "rules.hunger.reduces_skill", "0"); + CuAssertIntEquals(tc, 6, effskill(u, SK_ARMORER)); + CuAssertIntEquals(tc, 6, effskill(u, SK_SAILING)); + + set_param(&global.parameters, "rules.hunger.reduces_skill", "1"); + CuAssertIntEquals(tc, 3, effskill(u, SK_ARMORER)); + CuAssertIntEquals(tc, 3, effskill(u, SK_SAILING)); + + set_param(&global.parameters, "rules.hunger.reduces_skill", "2"); + CuAssertIntEquals(tc, 3, effskill(u, SK_ARMORER)); + CuAssertIntEquals(tc, 5, effskill(u, SK_SAILING)); + set_level(u, SK_SAILING, 2); + CuAssertIntEquals(tc, 1, effskill(u, SK_SAILING)); +} CuSuite *get_unit_suite(void) { @@ -254,5 +308,7 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); SUITE_ADD_TEST(suite, test_names); SUITE_ADD_TEST(suite, test_default_name); + SUITE_ADD_TEST(suite, test_skillmod); + SUITE_ADD_TEST(suite, test_skill_hunger); return suite; } From b27f0787a8e2cc3d8dcc8097c3f59ef53b548c14 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 26 Aug 2015 08:36:25 +0200 Subject: [PATCH 3/4] test familiar skill bonus --- src/kernel/unit.test.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 9c00fd304..f10d0e8db 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -259,6 +259,10 @@ static void test_skillmod(CuTest *tc) { CuAssertIntEquals(tc, 10, effskill(u, SK_ARMORER)); a_remove(&u->attribs, a); + a_add(&u->attribs, a = make_skillmod(NOSKILL, SMF_ALWAYS, 0, 2.0, 0)); // NOSKILL means any skill + CuAssertIntEquals(tc, 10, effskill(u, SK_ARMORER)); + a_remove(&u->attribs, a); + a_add(&u->attribs, a = make_skillmod(SK_ARMORER, SMF_ALWAYS, 0, 0, 2)); CuAssertIntEquals(tc, 7, effskill(u, SK_ARMORER)); a_remove(&u->attribs, a); @@ -294,6 +298,33 @@ static void test_skill_hunger(CuTest *tc) { CuAssertIntEquals(tc, 1, effskill(u, SK_SAILING)); } +static void test_skill_familiar(CuTest *tc) { + unit *mag, *fam; + region *r; + + test_cleanup(); + + // setup two units + mag = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fam = test_create_unit(mag->faction, test_create_region(0, 0, 0)); + set_level(fam, SK_PERCEPTION, 6); + CuAssertIntEquals(tc, 6, effskill(fam, SK_PERCEPTION)); + set_level(mag, SK_PERCEPTION, 6); + CuAssertIntEquals(tc, 6, effskill(mag, SK_PERCEPTION)); + + // make them mage and familiar to each other + CuAssertIntEquals(tc, true, create_newfamiliar(mag, fam)); + + // when they are in the same region, the mage gets half their skill as a bonus + CuAssertIntEquals(tc, 6, effskill(fam, SK_PERCEPTION)); + CuAssertIntEquals(tc, 9, effskill(mag, SK_PERCEPTION)); + + // when they are further apart, divide bonus by distance + r = test_create_region(3, 0, 0); + move_unit(fam, r, &r->units); + CuAssertIntEquals(tc, 7, effskill(mag, SK_PERCEPTION)); +} + CuSuite *get_unit_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -310,5 +341,6 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_default_name); SUITE_ADD_TEST(suite, test_skillmod); SUITE_ADD_TEST(suite, test_skill_hunger); + SUITE_ADD_TEST(suite, test_skill_familiar); return suite; } From c5f4d213cefebe4b0086c70a7e857d7f13847b59 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 26 Aug 2015 08:42:45 +0200 Subject: [PATCH 4/4] test familiar/mage connection and ageing of same. --- src/kernel/unit.test.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index f10d0e8db..47133fdc9 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -296,6 +296,7 @@ static void test_skill_hunger(CuTest *tc) { CuAssertIntEquals(tc, 5, effskill(u, SK_SAILING)); set_level(u, SK_SAILING, 2); CuAssertIntEquals(tc, 1, effskill(u, SK_SAILING)); + test_cleanup(); } static void test_skill_familiar(CuTest *tc) { @@ -323,6 +324,30 @@ static void test_skill_familiar(CuTest *tc) { r = test_create_region(3, 0, 0); move_unit(fam, r, &r->units); CuAssertIntEquals(tc, 7, effskill(mag, SK_PERCEPTION)); + test_cleanup(); +} + +static void test_age_familiar(CuTest *tc) { + unit *mag, *fam; + + test_cleanup(); + + // setup two units + mag = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fam = test_create_unit(mag->faction, test_create_region(0, 0, 0)); + CuAssertPtrEquals(tc, 0, get_familiar(mag)); + CuAssertPtrEquals(tc, 0, get_familiar_mage(fam)); + CuAssertIntEquals(tc, true, create_newfamiliar(mag, fam)); + CuAssertPtrEquals(tc, fam, get_familiar(mag)); + CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); + a_age(&fam->attribs); + a_age(&mag->attribs); + CuAssertPtrEquals(tc, fam, get_familiar(mag)); + CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); + set_number(fam, 0); + a_age(&mag->attribs); + CuAssertPtrEquals(tc, 0, get_familiar(mag)); + test_cleanup(); } CuSuite *get_unit_suite(void) @@ -342,5 +367,6 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_skillmod); SUITE_ADD_TEST(suite, test_skill_hunger); SUITE_ADD_TEST(suite, test_skill_familiar); + SUITE_ADD_TEST(suite, test_age_familiar); return suite; }