diff --git a/src/bind_region.c b/src/bind_region.c index e56c3443c..1296e4a65 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -474,9 +474,6 @@ static int tolua_region_create(lua_State * L) } if (result) { terraform_region(result, terrain); - if (result->land) { - fix_demand(result); - } } tolua_pushusertype(L, result, TOLUA_CAST "region"); diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 6bb860ad7..d3a2e8e56 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -20,7 +20,7 @@ order.test.c plane.test.c pool.test.c race.test.c -# region.test.c +region.test.c # resources.test.c save.test.c ship.test.c diff --git a/src/kernel/curse.test.c b/src/kernel/curse.test.c index d7c86b9b0..0ca4510d9 100644 --- a/src/kernel/curse.test.c +++ b/src/kernel/curse.test.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -175,6 +176,43 @@ static void test_curse_cache(CuTest *tc) test_cleanup(); } +static void test_curse_ids(CuTest *tc) { + const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; + curse *c1, *c2; + attrib *a1 = 0, *a2 = 0; + + test_setup(); + rng_init(0); + c1 = create_curse(NULL, &a1, &ct_dummy, 1, 1, 1, 1); + rng_init(0); + c2 = create_curse(NULL, &a2, &ct_dummy, 1, 1, 1, 1); + CuAssertTrue(tc, c1->no != c2->no); + a_remove(&a1, a1); + a_remove(&a2, a2); + test_cleanup(); +} + +static void test_curse_flags(CuTest *tc) { + const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; + curse *c1, *c2; + unit *u; + + test_setup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + c1 = create_curse(u, &u->attribs, &ct_dummy, 1, 1, 1, 0); + CuAssertPtrEquals(tc, u, c1->magician); + CuAssertIntEquals(tc, 1, (int)c1->effect); + CuAssertIntEquals(tc, 1, (int)c1->vigour); + CuAssertIntEquals(tc, 1, c1->duration); + c2 = create_curse(u, &u->attribs, &ct_dummy, 1, 1, 1, 0); + CuAssertPtrEquals(tc, c1, c2); + CuAssertPtrEquals(tc, u, c1->magician); + CuAssertIntEquals(tc, 2, (int)c1->effect); + CuAssertIntEquals(tc, 1, (int)c1->vigour); + CuAssertIntEquals(tc, 1, c1->duration); + test_cleanup(); +} + CuSuite *get_curse_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -186,5 +224,7 @@ CuSuite *get_curse_suite(void) SUITE_ADD_TEST(suite, test_bad_dreams); SUITE_ADD_TEST(suite, test_memstream); SUITE_ADD_TEST(suite, test_write_flag); + SUITE_ADD_TEST(suite, test_curse_flags); + SUITE_ADD_TEST(suite, test_curse_ids); return suite; } diff --git a/src/kernel/region.c b/src/kernel/region.c index 4c66e04f8..a92a87291 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1009,6 +1009,20 @@ void setluxuries(region * r, const luxury_type * sale) } } +int fix_demand(region * rd) { + luxury_type * ltype; + int maxluxuries = get_maxluxuries(); + if (maxluxuries > 0) { + int sale = rng_int() % maxluxuries; + for (ltype = luxurytypes; sale != 0 && ltype; ltype = ltype->next) { + --sale; + } + setluxuries(rd, ltype); + return 0; + } + return -1; +} + void terraform_region(region * r, const terrain_type * terrain) { /* Resourcen, die nicht mehr vorkommen können, löschen */ @@ -1057,7 +1071,6 @@ void terraform_region(region * r, const terrain_type * terrain) rsetmoney(r, 0); freset(r, RF_ENCOUNTER); freset(r, RF_MALLORN); - /* Beschreibung und Namen löschen */ return; } @@ -1082,6 +1095,7 @@ void terraform_region(region * r, const terrain_type * terrain) r->land->ownership = NULL; region_set_morale(r, MORALE_DEFAULT, -1); region_setname(r, makename()); + fix_demand(r); for (d = 0; d != MAXDIRECTIONS; ++d) { region *nr = rconnect(r, d); if (nr && nr->land) { diff --git a/src/kernel/region.h b/src/kernel/region.h index 6e231529d..5efd416f6 100644 --- a/src/kernel/region.h +++ b/src/kernel/region.h @@ -160,6 +160,7 @@ extern "C" { #define reg_hashkey(r) (r->index) + extern int fix_demand(struct region *r); int distance(const struct region *, const struct region *); int koor_distance(int ax, int ay, int bx, int by); struct region *findregion(int x, int y); diff --git a/src/kernel/region.test.c b/src/kernel/region.test.c new file mode 100644 index 000000000..01ecae2a8 --- /dev/null +++ b/src/kernel/region.test.c @@ -0,0 +1,39 @@ +#include + +#include "region.h" +#include "terrain.h" +#include "item.h" + +#include +#include + +void test_terraform(CuTest *tc) { + region *r; + terrain_type *t_plain, *t_ocean; + item_type *itype; + + test_setup(); + itype = test_create_itemtype("ointment"); + itype->rtype->flags |= (RTF_ITEM | RTF_POOLED); + new_luxurytype(itype, 0); + + t_plain = test_create_terrain("plain", LAND_REGION); + t_ocean = test_create_terrain("ocean", SEA_REGION); + r = test_create_region(0, 0, t_ocean); + CuAssertPtrEquals(tc, 0, r->land); + terraform_region(r, t_plain); + CuAssertPtrNotNull(tc, r->land); + CuAssertPtrNotNull(tc, r->land->demands); + CuAssertPtrEquals(tc, itype, (void *)r->land->demands->type->itype); + CuAssertIntEquals(tc, 0, r->land->demands->type->price); + terraform_region(r, t_ocean); + CuAssertPtrEquals(tc, 0, r->land); + test_cleanup(); +} + +CuSuite *get_region_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_terraform); + return suite; +} diff --git a/src/kernel/save.c b/src/kernel/save.c index 2373d60a0..0b0ea222d 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1096,6 +1096,9 @@ static region *readregion(struct gamedata *data, int x, int y) READ_INT(data->store, &n); r_setdemand(r, rtype->ltype, n); } + if (!r->land->demands) { + fix_demand(r); + } read_items(data->store, &r->land->items); if (data->version >= REGIONOWNER_VERSION) { READ_INT(data->store, &n); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 201153663..ad8377254 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1717,7 +1717,7 @@ void renumber_unit(unit *u, int no) { uunhash(u); if (!ualias(u)) { attrib *a = a_add(&u->attribs, a_new(&at_alias)); - a->data.i = -u->no; + a->data.i = -u->no; // TODO: why is the alias negative? confusing! } u->no = no; uhash(u); diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 054a45652..c4b1f46ed 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -8,9 +8,10 @@ #include #include #include +#include #include #include -#include +#include #include #include #include @@ -454,6 +455,19 @@ static void test_remove_unit(CuTest *tc) { test_cleanup(); } +static void test_renumber_unit(CuTest *tc) { + unit *u1, *u2; + test_setup(); + u1 = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u2 = test_create_unit(u1->faction, u1->region); + rng_init(0); + renumber_unit(u1, 0); + rng_init(0); + renumber_unit(u2, 0); + CuAssertTrue(tc, u1->no != u2->no); + test_cleanup(); +} + CuSuite *get_unit_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -476,5 +490,6 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_age_familiar); SUITE_ADD_TEST(suite, test_inside_building); SUITE_ADD_TEST(suite, test_limited_skills); + SUITE_ADD_TEST(suite, test_renumber_unit); return suite; } diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index e8855683e..3bcf36848 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -132,20 +132,6 @@ static bool f_nolux(const region * r) return (r->land && count_demand(r) != get_maxluxuries()); } -int fix_demand(region * rd) { - luxury_type * ltype; - int maxluxuries = get_maxluxuries(); - if (maxluxuries > 0) { - int sale = rng_int() % maxluxuries; - for (ltype = luxurytypes; sale != 0 && ltype; ltype = ltype->next) { - --sale; - } - setluxuries(rd, ltype); - return 0; - } - return -1; -} - int fix_all_demand(region *rd) { region_list *rl, *rlist = NULL; recurse_regions(rd, &rlist, f_nolux); diff --git a/src/modules/autoseed.h b/src/modules/autoseed.h index 3cfe6225c..06ce646c2 100644 --- a/src/modules/autoseed.h +++ b/src/modules/autoseed.h @@ -35,7 +35,6 @@ extern "C" { extern int autoseed(newfaction ** players, int nsize, int max_agediff); extern newfaction *read_newfactions(const char *filename); - extern int fix_demand(struct region *r); extern const struct terrain_type *random_terrain(const struct terrain_type *terrains[], int distribution[], int size); diff --git a/src/test_eressea.c b/src/test_eressea.c index df4b762f3..002d9a0cb 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -100,6 +100,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(magic); ADD_SUITE(alchemy); ADD_SUITE(reports); + ADD_SUITE(region); ADD_SUITE(save); ADD_SUITE(ship); ADD_SUITE(spellbook); @@ -111,7 +112,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(messages); /* gamecode */ ADD_SUITE(report); -// ADD_SUITE(creport); + ADD_SUITE(creport); ADD_SUITE(prefix); ADD_SUITE(summary); ADD_SUITE(names);