Merge pull request #280 from ennorehling/feature/bug-2131-familiar-skillbonus

Bug 2131: familiar skillbonus stopped working
This commit is contained in:
Enno Rehling 2015-08-26 08:50:49 +02:00
commit f5f24cc7aa
4 changed files with 118 additions and 4 deletions

View File

@ -131,7 +131,7 @@ int curse_age(attrib * a)
else if (c->duration != INT_MAX) { else if (c->duration != INT_MAX) {
c->duration = _max(0, c->duration - 1); 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) void destroy_curse(curse * c)

View File

@ -239,6 +239,116 @@ static void test_default_name(CuTest *tc) {
test_cleanup(); 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(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);
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));
test_cleanup();
}
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));
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) CuSuite *get_unit_suite(void)
{ {
@ -254,5 +364,9 @@ CuSuite *get_unit_suite(void)
SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); SUITE_ADD_TEST(suite, test_remove_empty_units_in_region);
SUITE_ADD_TEST(suite, test_names); SUITE_ADD_TEST(suite, test_names);
SUITE_ADD_TEST(suite, test_default_name); 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);
SUITE_ADD_TEST(suite, test_age_familiar);
return suite; return suite;
} }

View File

@ -256,7 +256,7 @@ int a_age(attrib ** p)
if (a->type->age) { if (a->type->age) {
int result = a->type->age(a); int result = a->type->age(a);
assert(result >= 0 || !"age() returned a negative value"); assert(result >= 0 || !"age() returned a negative value");
if (result == 0) { if (result == AT_AGE_REMOVE) {
a_remove(p, a); a_remove(p, a);
continue; continue;
} }

View File

@ -90,8 +90,8 @@ extern "C" {
#define AT_READ_OK 0 #define AT_READ_OK 0
#define AT_READ_FAIL -1 #define AT_READ_FAIL -1
#define AT_AGE_KEEP 0 /* keep the attribute for another turn */ #define AT_AGE_REMOVE 0 /* remove the attribute after calling age() */
#define AT_AGE_REMOVE 1 /* remove the attribute after calling age() */ #define AT_AGE_KEEP 1 /* keep the attribute for another turn */
#ifdef __cplusplus #ifdef __cplusplus
} }