diff --git a/src/kernel/item.c b/src/kernel/item.c index 2fd76ded3..db1da94ad 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -73,7 +73,11 @@ static int res_changeaura(unit * u, const resource_type * rtype, int delta) static int res_changeperson(unit * u, const resource_type * rtype, int delta) { assert(rtype != NULL || !"not implemented"); - scale_number(u, u->number + delta); + if (u->number + delta >=0) { + scale_number(u, u->number + delta); + } else { + scale_number(u, 0); + } return u->number; } diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c index 6279bc0af..c9a144ede 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -9,26 +10,42 @@ #include #include +static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) { + int n; + change_resource(u, rtype, 4); + n = get_resource(u, rtype); + CuAssertPtrNotNull(tc, rtype->uchange); + CuAssertIntEquals(tc, n, rtype->uchange(u, rtype, 0)); + CuAssertIntEquals(tc, n-3, rtype->uchange(u, rtype, -3)); + CuAssertIntEquals(tc, n-3, get_resource(u, rtype)); + CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n)); +} + void test_change_item(CuTest * tc) { - rtype_uchange res_changeitem; - const resource_type * rtype; unit * u; - register_resources(); - res_changeitem = (rtype_uchange)get_function("changeitem"); - CuAssertPtrNotNull(tc, res_changeitem); - test_cleanup(); + register_resources(); + init_resources(); test_create_world(); - rtype = olditemtype[I_IRON]->rtype; u = test_create_unit(0, 0); - CuAssertIntEquals(tc, 0, res_changeitem(u, rtype, 0)); - i_change(&u->items, rtype->itype, 4); - CuAssertIntEquals(tc, 4, res_changeitem(u, rtype, 0)); - CuAssertIntEquals(tc, 1, res_changeitem(u, rtype, -3)); - CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype)); + test_uchange(tc, u, olditemtype[I_IRON]->rtype); +} + +void test_change_person(CuTest * tc) +{ + unit * u; + + test_cleanup(); + + register_resources(); + init_resources(); + test_create_world(); + + u = test_create_unit(0, 0); + test_uchange(tc, u, r_unit); } void test_resource_type(CuTest * tc) @@ -93,6 +110,7 @@ CuSuite *get_item_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_change_item); + SUITE_ADD_TEST(suite, test_change_person); SUITE_ADD_TEST(suite, test_resource_type); SUITE_ADD_TEST(suite, test_finditemtype); SUITE_ADD_TEST(suite, test_findresourcetype);