adding more tests.

test for build()
test for scale_number()
update submodules
This commit is contained in:
Enno Rehling 2014-08-26 20:06:28 +02:00
parent 2ceeb0b819
commit d435c13bfc
9 changed files with 1265 additions and 1069 deletions

@ -1 +1 @@
Subproject commit 2a7af5e2347217ea4efcf7fb3f0bc9c2681d1a17 Subproject commit 4ab2cfe0a6dd6d57ebb460f9b824f68dba50b7de

@ -1 +1 @@
Subproject commit f84066fb7d3254bdd9e89694acc4c1c20d001eed Subproject commit fe53382982d0f417b6f7ad0fe08e32393cb07c21

View File

@ -4,6 +4,7 @@ SET(_TEST_FILES
build.test.c build.test.c
config.test.c config.test.c
faction.test.c faction.test.c
unit.test.c
save.test.c save.test.c
ship.test.c ship.test.c
spell.test.c spell.test.c

View File

@ -521,9 +521,7 @@ int build(unit * u, const construction * ctype, int completed, int want)
} }
} }
if (want > 0) { if (want < n) n = want;
n = _min(want, n);
}
if (type->maxsize > 0) { if (type->maxsize > 0) {
n = _min(type->maxsize - completed, n); n = _min(type->maxsize - completed, n);

View File

@ -1,5 +1,6 @@
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/alchemy.h>
#include "types.h" #include "types.h"
#include "build.h" #include "build.h"
#include "order.h" #include "order.h"
@ -21,6 +22,7 @@ typedef struct build_fixture {
unit *u; unit *u;
region *r; region *r;
race *rc; race *rc;
construction cons;
} build_fixture; } build_fixture;
static unit * setup_build(build_fixture *bf) { static unit * setup_build(build_fixture *bf) {
@ -32,37 +34,159 @@ static unit * setup_build(build_fixture *bf) {
assert(bf->rc && bf->f && bf->r); assert(bf->rc && bf->f && bf->r);
bf->u = test_create_unit(bf->f, bf->r); bf->u = test_create_unit(bf->f, bf->r);
assert(bf->u); assert(bf->u);
bf->cons.materials = calloc(2, sizeof(requirement));
bf->cons.materials[0].number = 1;
bf->cons.materials[0].rtype = get_resourcetype(R_SILVER);
bf->cons.skill = SK_ARMORER;
bf->cons.minskill = 2;
bf->cons.maxsize = -1;
bf->cons.reqsize = 1;
return bf->u; return bf->u;
} }
static void test_build(CuTest *tc) { static void test_build_requires_materials(CuTest *tc) {
build_fixture bf; build_fixture bf = { 0 };
unit *u;
const struct item_type *itype;
u = setup_build(&bf);
set_level(u, SK_ARMORER, 2);
CuAssertIntEquals(tc, ENOMATERIALS, build(u, &bf.cons, 0, 1));
itype = bf.cons.materials[0].rtype->itype;
i_change(&u->items, itype, 2);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
CuAssertIntEquals(tc, 1, i_get(u->items, itype));
}
static void test_build_requires_building(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
building_type *btype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, 2);
bf.cons.btype = btype = bt_get_or_create("hodor");
btype->maxcapacity = 1;
btype->capacity = 1;
CuAssertIntEquals_Msg(tc, "must be inside a production building", EBUILDINGREQ, build(u, &bf.cons, 0, 1));
u->building = test_create_building(u->region, btype);
fset(u->building, BLD_WORKING);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
btype->maxcapacity = 0;
CuAssertIntEquals_Msg(tc, "cannot build when production building capacity exceeded", EBUILDINGREQ, build(u, &bf.cons, 0, 1));
}
static void test_build_failure_missing_skill(CuTest *tc) {
build_fixture bf = { 0 };
unit *u; unit *u;
construction cons = { 0 };
const struct resource_type *rtype; const struct resource_type *rtype;
u = setup_build(&bf); u = setup_build(&bf);
rtype = get_resourcetype(R_SILVER); rtype = bf.cons.materials[0].rtype;
assert(rtype);
cons.materials = calloc(2, sizeof(requirement));
cons.materials[0].number = 1;
cons.materials[0].rtype = rtype;
cons.skill = SK_ARMORER;
cons.minskill = 2;
cons.reqsize = 1;
CuAssertIntEquals(tc, ENEEDSKILL, build(u, &cons, 1, 1));
set_level(u, SK_ARMORER, 1);
CuAssertIntEquals(tc, ELOWSKILL, build(u, &cons, 1, 1));
set_level(u, SK_ARMORER, 2);
CuAssertIntEquals(tc, ENOMATERIALS, build(u, &cons, 1, 1));
i_change(&u->items, rtype->itype, 1); i_change(&u->items, rtype->itype, 1);
CuAssertIntEquals(tc, 1, build(u, &cons, 1, 1)); CuAssertIntEquals(tc, ENEEDSKILL, build(u, &bf.cons, 1, 1));
}
static void test_build_failure_low_skill(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill-1);
CuAssertIntEquals(tc, ELOWSKILL, build(u, &bf.cons, 0, 10));
}
static void test_build_failure_completed(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill);
bf.cons.maxsize = 1;
CuAssertIntEquals(tc, ECOMPLETE, build(u, &bf.cons, bf.cons.maxsize, 10));
CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype));
}
static void test_build_limits(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype)); CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
scale_number(u, 2); scale_number(u, 2);
set_level(u, SK_ARMORER, bf.cons.minskill);
i_change(&u->items, rtype->itype, 2); i_change(&u->items, rtype->itype, 2);
CuAssertIntEquals(tc, 2, build(u, &cons, 2, 2)); CuAssertIntEquals(tc, 2, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype)); CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
scale_number(u, 2);
set_level(u, SK_ARMORER, bf.cons.minskill * 2);
i_change(&u->items, rtype->itype, 4);
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
test_cleanup();
}
static void test_build_with_ring(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
item_type *ring;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
ring = it_get_or_create(rt_get_or_create("roqf"));
assert(rtype && ring);
set_level(u, SK_ARMORER, bf.cons.minskill);
i_change(&u->items, rtype->itype, 20);
i_change(&u->items, ring, 1);
CuAssertIntEquals(tc, 10, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 10, i_get(u->items, rtype->itype));
test_cleanup();
}
static void test_build_with_potion(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const potion_type *ptype;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
oldpotiontype[P_DOMORE] = ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
assert(rtype && ptype);
i_change(&u->items, rtype->itype, 20);
change_effect(u, ptype, 4);
set_level(u, SK_ARMORER, bf.cons.minskill);
CuAssertIntEquals(tc, 2, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 18, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 3, get_effect(u, ptype));
set_level(u, SK_ARMORER, bf.cons.minskill*2);
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
set_level(u, SK_ARMORER, bf.cons.minskill);
scale_number(u, 2); // OBS: this scales the effects, too:
CuAssertIntEquals(tc, 4, get_effect(u, ptype));
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
test_cleanup(); test_cleanup();
} }
@ -82,7 +206,7 @@ static void test_build_building_no_materials(CuTest *tc) {
static void test_build_building_with_golem(CuTest *tc) { static void test_build_building_with_golem(CuTest *tc) {
unit *u; unit *u;
build_fixture bf; build_fixture bf = { 0 };
const building_type *btype; const building_type *btype;
u = setup_build(&bf); u = setup_build(&bf);
@ -92,7 +216,7 @@ static void test_build_building_with_golem(CuTest *tc) {
assert(btype->construction); assert(btype->construction);
set_level(bf.u, SK_BUILDING, 1); set_level(bf.u, SK_BUILDING, 1);
CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, 0)); CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 1, 0));
CuAssertPtrNotNull(tc, u->region->buildings); CuAssertPtrNotNull(tc, u->region->buildings);
CuAssertIntEquals(tc, 1, u->region->buildings->size); CuAssertIntEquals(tc, 1, u->region->buildings->size);
CuAssertIntEquals(tc, 0, u->number); CuAssertIntEquals(tc, 0, u->number);
@ -124,7 +248,14 @@ static void test_build_building_success(CuTest *tc) {
CuSuite *get_build_suite(void) CuSuite *get_build_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_build); SUITE_ADD_TEST(suite, test_build_limits);
SUITE_ADD_TEST(suite, test_build_failure_low_skill);
SUITE_ADD_TEST(suite, test_build_failure_missing_skill);
SUITE_ADD_TEST(suite, test_build_requires_materials);
SUITE_ADD_TEST(suite, test_build_requires_building);
SUITE_ADD_TEST(suite, test_build_failure_completed);
SUITE_ADD_TEST(suite, test_build_with_ring);
SUITE_ADD_TEST(suite, test_build_with_potion);
SUITE_ADD_TEST(suite, test_build_building_success); SUITE_ADD_TEST(suite, test_build_building_success);
SUITE_ADD_TEST(suite, test_build_building_with_golem); SUITE_ADD_TEST(suite, test_build_building_with_golem);
SUITE_ADD_TEST(suite, test_build_building_no_materials); SUITE_ADD_TEST(suite, test_build_building_no_materials);

View File

@ -1,7 +1,7 @@
/* /*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de> Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de> Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above purpose with or without fee is hereby granted, provided that the above
@ -74,7 +74,7 @@ attrib_type at_creator = {
#define UMAXHASH MAXUNITS #define UMAXHASH MAXUNITS
static unit *unithash[UMAXHASH]; static unit *unithash[UMAXHASH];
static unit *delmarker = (unit *) unithash; /* a funny hack */ static unit *delmarker = (unit *)unithash; /* a funny hack */
#define HASH_STATISTICS 1 #define HASH_STATISTICS 1
#if HASH_STATISTICS #if HASH_STATISTICS
@ -134,7 +134,7 @@ static dead *deadhash[DMAXHASH];
static void dhash(int no, faction * f) static void dhash(int no, faction * f)
{ {
dead *hash = (dead *) calloc(1, sizeof(dead)); dead *hash = (dead *)calloc(1, sizeof(dead));
dead *old = deadhash[no % DMAXHASH]; dead *old = deadhash[no % DMAXHASH];
hash->no = no; hash->no = no;
hash->f = f; hash->f = f;
@ -177,7 +177,8 @@ static buddy *get_friends(const unit * u, int *numfriends)
int allied = 0; int allied = 0;
if (get_param_int(global.parameters, "rules.alliances", 0) != 0) { if (get_param_int(global.parameters, "rules.alliances", 0) != 0) {
allied = (f->alliance && f->alliance == u2->faction->alliance); allied = (f->alliance && f->alliance == u2->faction->alliance);
} else if (alliedunit(u, u2->faction, HELP_MONEY) }
else if (alliedunit(u, u2->faction, HELP_MONEY)
&& alliedunit(u2, f, HELP_GIVE)) { && alliedunit(u2, f, HELP_GIVE)) {
allied = 1; allied = 1;
} }
@ -196,7 +197,8 @@ static buddy *get_friends(const unit * u, int *numfriends)
nf->unit = u2; nf->unit = u2;
nf->number = 0; nf->number = 0;
*fr = nf; *fr = nf;
} else if (nf->faction == u2->faction }
else if (nf->faction == u2->faction
&& (u_race(u2)->ec_flags & GIVEITEM)) { && (u_race(u2)->ec_flags & GIVEITEM)) {
/* we don't like to gift it to units that won't give it back */ /* we don't like to gift it to units that won't give it back */
if ((u_race(nf->unit)->ec_flags & GIVEITEM) == 0) { if ((u_race(nf->unit)->ec_flags & GIVEITEM) == 0) {
@ -256,7 +258,8 @@ int gift_items(unit * u, int flags)
i_merge(&u2->items, &u->items); i_merge(&u2->items, &u->items);
u->items = NULL; u->items = NULL;
break; break;
} else { }
else {
u3 = u2; u3 = u2;
} }
} }
@ -318,7 +321,8 @@ int gift_items(unit * u, int flags)
if (itm->number > 0 && (itm->type->flags & ITF_NOTLOST)) { if (itm->number > 0 && (itm->type->flags & ITF_NOTLOST)) {
itm_p = &itm->next; itm_p = &itm->next;
retval = -1; retval = -1;
} else { }
else {
i_remove(itm_p, itm); i_remove(itm_p, itm);
i_free(itm); i_free(itm);
} }
@ -444,7 +448,8 @@ const char *u_description(const unit * u, const struct locale *lang)
{ {
if (u->display && u->display[0]) { if (u->display && u->display[0]) {
return u->display; return u->display;
} else if (u_race(u)->describe) { }
else if (u_race(u)->describe) {
return u_race(u)->describe(u, lang); return u_race(u)->describe(u, lang);
} }
return NULL; return NULL;
@ -522,7 +527,7 @@ unit *utarget(const unit * u)
return NULL; return NULL;
a = a_find(u->attribs, &at_target); a = a_find(u->attribs, &at_target);
assert(a || !"flag set, but no target found"); assert(a || !"flag set, but no target found");
return (unit *) a->data.v; return (unit *)a->data.v;
} }
void usettarget(unit * u, const unit * t) void usettarget(unit * u, const unit * t)
@ -534,7 +539,8 @@ void usettarget(unit * u, const unit * t)
if (!t) { if (!t) {
a_remove(&u->attribs, a); a_remove(&u->attribs, a);
freset(u, UFL_TARGET); freset(u, UFL_TARGET);
} else { }
else {
a->data.v = (void *)t; a->data.v = (void *)t;
fset(u, UFL_TARGET); fset(u, UFL_TARGET);
} }
@ -589,7 +595,8 @@ void usetsiege(unit * u, const struct building *t)
if (!t) { if (!t) {
a_remove(&u->attribs, a); a_remove(&u->attribs, a);
freset(u, UFL_SIEGE); freset(u, UFL_SIEGE);
} else { }
else {
a->data.v = (void *)t; a->data.v = (void *)t;
fset(u, UFL_SIEGE); fset(u, UFL_SIEGE);
} }
@ -628,7 +635,7 @@ bool ucontact(const unit * u, const unit * u2)
/* Explizites KONTAKTIERE */ /* Explizites KONTAKTIERE */
for (ru = a_find(u->attribs, &at_contact); ru && ru->type == &at_contact; for (ru = a_find(u->attribs, &at_contact); ru && ru->type == &at_contact;
ru = ru->next) { ru = ru->next) {
if (((unit *) ru->data.v) == u2) { if (((unit *)ru->data.v) == u2) {
return true; return true;
} }
} }
@ -661,11 +668,11 @@ int resolve_unit(variant id, void *address)
if (id.i != 0) { if (id.i != 0) {
u = findunit(id.i); u = findunit(id.i);
if (u == NULL) { if (u == NULL) {
*(unit **) address = NULL; *(unit **)address = NULL;
return -1; return -1;
} }
} }
*(unit **) address = u; *(unit **)address = u;
return 0; return 0;
} }
@ -780,7 +787,7 @@ ship *leftship(const unit * u)
* Zeitpunkt noch auf ein existierendes Schiff zeigt! */ * Zeitpunkt noch auf ein existierendes Schiff zeigt! */
if (a) if (a)
return (ship *) (a->data.v); return (ship *)(a->data.v);
return NULL; return NULL;
} }
@ -808,7 +815,7 @@ void leave_ship(unit * u)
struct ship *sh = u->ship; struct ship *sh = u->ship;
u->ship = 0; u->ship = 0;
if (sh->_owner==u) { if (sh->_owner == u) {
ship_update_owner(sh); ship_update_owner(sh);
sh->_owner = ship_owner(sh); sh->_owner = ship_owner(sh);
} }
@ -820,9 +827,9 @@ void leave_building(unit * u)
building * b = u->building; building * b = u->building;
u->building = 0; u->building = 0;
if (b->_owner==u) { if (b->_owner == u) {
building_update_owner(b); building_update_owner(b);
assert(b->_owner!=u); assert(b->_owner != u);
} }
} }
@ -855,7 +862,8 @@ bool leave(unit * u, bool force)
} }
if (u->building) { if (u->building) {
leave_building(u); leave_building(u);
} else if (u->ship) { }
else if (u->ship) {
leave_ship(u); leave_ship(u);
} }
return true; return true;
@ -908,7 +916,8 @@ void move_unit(unit * u, region * r, unit ** ulist)
#endif #endif
} }
translist(&u->region->units, ulist, u); translist(&u->region->units, ulist, u);
} else { }
else {
addlist(ulist, u); addlist(ulist, u);
} }
@ -951,7 +960,8 @@ void transfermen(unit * u, unit * u2, int n)
/* new unit, easy to solve */ /* new unit, easy to solve */
level = sv->level; level = sv->level;
weeks = sv->weeks; weeks = sv->weeks;
} else { }
else {
double dlevel = 0.0; double dlevel = 0.0;
if (sv && sv->level) { if (sv && sv->level) {
@ -972,7 +982,8 @@ void transfermen(unit * u, unit * u2, int n)
* next level */ * next level */
level = (int)dlevel; level = (int)dlevel;
weeks = (level + 1) - (int)((dlevel - level) * (level + 1)); weeks = (level + 1) - (int)((dlevel - level) * (level + 1));
} else { }
else {
/* make it harder to reach the next level. /* make it harder to reach the next level.
* weeks+level is the max difficulty, 1 - the fraction between * weeks+level is the max difficulty, 1 - the fraction between
* level and dlevel applied to the number of weeks between this * level and dlevel applied to the number of weeks between this
@ -989,14 +1000,15 @@ void transfermen(unit * u, unit * u2, int n)
assert(sn->weeks > 0 && sn->weeks <= sn->level * 2 + 1); assert(sn->weeks > 0 && sn->weeks <= sn->level * 2 + 1);
assert(u2->number != 0 || (sn->level == sv->level assert(u2->number != 0 || (sn->level == sv->level
&& sn->weeks == sv->weeks)); && sn->weeks == sv->weeks));
} else if (sn) { }
else if (sn) {
remove_skill(u2, sk); remove_skill(u2, sk);
sn = NULL; sn = NULL;
} }
} }
a = a_find(u->attribs, &at_effect); a = a_find(u->attribs, &at_effect);
while (a && a->type == &at_effect) { while (a && a->type == &at_effect) {
effect_data *olde = (effect_data *) a->data.v; effect_data *olde = (effect_data *)a->data.v;
if (olde->value) if (olde->value)
change_effect(u2, olde->type, olde->value); change_effect(u2, olde->type, olde->value);
a = a->next; a = a->next;
@ -1020,19 +1032,21 @@ void transfermen(unit * u, unit * u2, int n)
a = a_find(u2->attribs, &at_effect); a = a_find(u2->attribs, &at_effect);
while (a && a->type == &at_effect) { while (a && a->type == &at_effect) {
attrib *an = a->next; attrib *an = a->next;
effect_data *olde = (effect_data *) a->data.v; effect_data *olde = (effect_data *)a->data.v;
int e = get_effect(u, olde->type); int e = get_effect(u, olde->type);
if (e != 0) if (e != 0)
change_effect(u2, olde->type, -e); change_effect(u2, olde->type, -e);
a = an; a = an;
} }
} else if (r->land) { }
else if (r->land) {
if ((u_race(u)->ec_flags & ECF_REC_ETHEREAL) == 0) { if ((u_race(u)->ec_flags & ECF_REC_ETHEREAL) == 0) {
const race *rc = u_race(u); const race *rc = u_race(u);
if (rc->ec_flags & ECF_REC_HORSES) { /* Zentauren an die Pferde */ if (rc->ec_flags & ECF_REC_HORSES) { /* Zentauren an die Pferde */
int h = rhorses(r) + n; int h = rhorses(r) + n;
rsethorses(r, h); rsethorses(r, h);
} else { }
else {
int p = rpeasants(r); int p = rpeasants(r);
p += (int)(n * rc->recruit_multi); p += (int)(n * rc->recruit_multi);
rsetpeasants(r, p); rsetpeasants(r, p);
@ -1049,10 +1063,12 @@ struct building *inside_building(const struct unit *u)
if (!fval(u->building, BLD_WORKING)) { if (!fval(u->building, BLD_WORKING)) {
/* Unterhalt nicht bezahlt */ /* Unterhalt nicht bezahlt */
return NULL; return NULL;
} else if (u->building->size < u->building->type->maxsize) { }
else if (u->building->size < u->building->type->maxsize) {
/* Gebaeude noch nicht fertig */ /* Gebaeude noch nicht fertig */
return NULL; return NULL;
} else { }
else {
int p = 0, cap = buildingcapacity(u->building); int p = 0, cap = buildingcapacity(u->building);
const unit *u2; const unit *u2;
for (u2 = u->region->units; u2; u2 = u2->next) { for (u2 = u->region->units; u2; u2 = u2->next) {
@ -1143,7 +1159,8 @@ bool learn_skill(unit * u, skill_t sk, double chance)
if (sv->id == sk) { if (sv->id == sk) {
if (sv->weeks <= 1) { if (sv->weeks <= 1) {
sk_set(sv, sv->level + 1); sk_set(sv, sv->level + 1);
} else { }
else {
sv->weeks--; sv->weeks--;
} }
return true; return true;
@ -1258,7 +1275,7 @@ static int att_modification(const unit * u, skill_t sk)
if (skillmod_ct) { if (skillmod_ct) {
attrib *a = a_find(u->attribs, &at_curse); attrib *a = a_find(u->attribs, &at_curse);
while (a && a->type == &at_curse) { while (a && a->type == &at_curse) {
curse *c = (curse *) a->data.v; curse *c = (curse *)a->data.v;
if (c->type == skillmod_ct && c->data.i == sk) { if (c->type == skillmod_ct && c->data.i == sk) {
result += curse_geteffect(c); result += curse_geteffect(c);
break; break;
@ -1274,7 +1291,7 @@ static int att_modification(const unit * u, skill_t sk)
double bonus = 0, malus = 0; double bonus = 0, malus = 0;
attrib *a = a_find(u->region->attribs, &at_curse); attrib *a = a_find(u->region->attribs, &at_curse);
while (a && a->type == &at_curse) { while (a && a->type == &at_curse) {
curse *c = (curse *) a->data.v; curse *c = (curse *)a->data.v;
if (curse_active(c) && c->type == gbdream_ct) { if (curse_active(c) && c->type == gbdream_ct) {
double mod = curse_geteffect(c); double mod = curse_geteffect(c);
unit *mage = c->magician; unit *mage = c->magician;
@ -1284,7 +1301,8 @@ static int att_modification(const unit * u, skill_t sk)
|| alliedunit(mage, u->faction, HELP_GUARD)) { || alliedunit(mage, u->faction, HELP_GUARD)) {
bonus = mod; bonus = mod;
} }
} else if (mod < malus) { }
else if (mod < malus) {
if (mage == NULL || !alliedunit(mage, u->faction, HELP_GUARD)) { if (mage == NULL || !alliedunit(mage, u->faction, HELP_GUARD)) {
malus = mod; malus = mod;
} }
@ -1300,7 +1318,7 @@ static int att_modification(const unit * u, skill_t sk)
int int
get_modifier(const unit * u, skill_t sk, int level, const region * r, get_modifier(const unit * u, skill_t sk, int level, const region * r,
bool noitem) bool noitem)
{ {
int bskill = level; int bskill = level;
int skill = bskill; int skill = bskill;
@ -1424,10 +1442,12 @@ void name_unit(unit * u)
const char *gen_name = u_race(u)->generate_name(u); const char *gen_name = u_race(u)->generate_name(u);
if (gen_name) { if (gen_name) {
unit_setname(u, gen_name); unit_setname(u, gen_name);
} else { }
else {
unit_setname(u, racename(u->faction->locale, u, u_race(u))); unit_setname(u, racename(u->faction->locale, u, u_race(u)));
} }
} else { }
else {
char name[32]; char name[32];
const char * result; const char * result;
const struct locale * lang = u->faction ? u->faction->locale : default_locale; const struct locale * lang = u->faction ? u->faction->locale : default_locale;
@ -1441,7 +1461,8 @@ void name_unit(unit * u)
} }
} }
result = prefix[i]; result = prefix[i];
} else { }
else {
result = parameters[P_UNIT]; result = parameters[P_UNIT];
} }
strlcpy(name, result, sizeof(name)); strlcpy(name, result, sizeof(name));
@ -1495,7 +1516,8 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
if (!dname) { if (!dname) {
name_unit(u); name_unit(u);
} else { }
else {
u->name = _strdup(dname); u->name = _strdup(dname);
} }
@ -1539,7 +1561,7 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
if (creator->faction == f && fval(creator, UFL_GROUP)) { if (creator->faction == f && fval(creator, UFL_GROUP)) {
a = a_find(creator->attribs, &at_group); a = a_find(creator->attribs, &at_group);
if (a) { if (a) {
group *g = (group *) a->data.v; group *g = (group *)a->data.v;
set_group(u, g); set_group(u, g);
} }
} }
@ -1699,7 +1721,6 @@ int unit_max_hp(const unit * u)
void scale_number(unit * u, int n) void scale_number(unit * u, int n)
{ {
skill_t sk;
const attrib *a; const attrib *a;
int remain; int remain;
@ -1714,14 +1735,15 @@ void scale_number(unit * u, int n)
assert(u->hp >= 0); assert(u->hp >= 0);
if ((rng_int() % u->number) < remain) if ((rng_int() % u->number) < remain)
++u->hp; /* Nachkommastellen */ ++u->hp; /* Nachkommastellen */
} else { }
else {
remain = 0; remain = 0;
u->hp = 0; u->hp = 0;
} }
if (u->number > 0) { if (u->number > 0) {
for (a = a_find(u->attribs, &at_effect); a && a->type == &at_effect; for (a = a_find(u->attribs, &at_effect); a && a->type == &at_effect;
a = a->next) { a = a->next) {
effect_data *data = (effect_data *) a->data.v; effect_data *data = (effect_data *)a->data.v;
int snew = data->value / u->number * n; int snew = data->value / u->number * n;
if (n) { if (n) {
remain = data->value - snew / n * u->number; remain = data->value - snew / n * u->number;
@ -1734,6 +1756,7 @@ void scale_number(unit * u, int n)
} }
} }
if (u->number == 0 || n == 0) { if (u->number == 0 || n == 0) {
skill_t sk;
for (sk = 0; sk < MAXSKILLS; sk++) { for (sk = 0; sk < MAXSKILLS; sk++) {
remove_skill(u, sk); remove_skill(u, sk);
} }
@ -1767,7 +1790,7 @@ void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level)
if (!mage) { if (!mage) {
log_debug("adding new spell %s to a previously non-mage unit %s\n", sp->sname, unitname(u)); log_debug("adding new spell %s to a previously non-mage unit %s\n", sp->sname, unitname(u));
mage = create_mage(u, u->faction?u->faction->magiegebiet:M_GRAY); mage = create_mage(u, u->faction ? u->faction->magiegebiet : M_GRAY);
} }
if (!mage->spellbook) { if (!mage->spellbook) {
mage->spellbook = create_spellbook(0); mage->spellbook = create_spellbook(0);
@ -1782,7 +1805,7 @@ struct spellbook * unit_get_spellbook(const struct unit * u)
if (mage->spellbook) { if (mage->spellbook) {
return mage->spellbook; return mage->spellbook;
} }
if (mage->magietyp!=M_GRAY) { if (mage->magietyp != M_GRAY) {
return faction_get_spellbook(u->faction); return faction_get_spellbook(u->faction);
} }
} }

41
src/kernel/unit.test.c Normal file
View File

@ -0,0 +1,41 @@
#include <platform.h>
#include <kernel/config.h>
#include "alchemy.h"
#include "unit.h"
#include "item.h"
#include "region.h"
#include <CuTest.h>
#include <tests.h>
#include <stdlib.h>
#include <assert.h>
static void test_scale_number(CuTest *tc) {
unit *u;
const struct potion_type *ptype;
test_cleanup();
test_create_world();
ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
change_effect(u, ptype, 1);
CuAssertIntEquals(tc, 1, u->number);
CuAssertIntEquals(tc, 1, u->hp);
CuAssertIntEquals(tc, 1, get_effect(u, ptype));
scale_number(u, 2);
CuAssertIntEquals(tc, 2, u->number);
CuAssertIntEquals(tc, 2, u->hp);
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
set_level(u, SK_ALCHEMY, 1);
scale_number(u, 0);
CuAssertIntEquals(tc, 0, get_level(u, SK_ALCHEMY));
test_cleanup();
}
CuSuite *get_unit_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_scale_number);
return suite;
}

View File

@ -515,6 +515,7 @@ int autoseed(newfaction ** players, int nsize, int max_agediff)
static const terrain_type **terrainarr = 0; static const terrain_type **terrainarr = 0;
static int *distribution; static int *distribution;
assert(players);
if (nterrains < 0) { if (nterrains < 0) {
int n = 0; int n = 0;
const terrain_type *terrain = terrains(); const terrain_type *terrain = terrains();

View File

@ -36,6 +36,7 @@ int RunAllTests(void)
ADD_TESTS(suite, umlaut); ADD_TESTS(suite, umlaut);
ADD_TESTS(suite, strings); ADD_TESTS(suite, strings);
/* kernel */ /* kernel */
ADD_TESTS(suite, unit);
ADD_TESTS(suite, faction); ADD_TESTS(suite, faction);
ADD_TESTS(suite, build); ADD_TESTS(suite, build);
ADD_TESTS(suite, pool); ADD_TESTS(suite, pool);