diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42a65d3be..0da16a3cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -92,6 +92,7 @@ set (ERESSEA_SRC study.c summary.c monsters.c + wormhole.c ${SPELLS_SRC} ${RACES_SRC} ${ITEMS_SRC} @@ -153,6 +154,7 @@ target_link_libraries(eressea ) set(TESTS_SRC + wormhole.test.c test_eressea.c tests.c battle.test.c diff --git a/src/eressea.c b/src/eressea.c index a0d16ebd1..bcc794ea1 100755 --- a/src/eressea.c +++ b/src/eressea.c @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -25,6 +24,7 @@ #include "items.h" #include "creport.h" #include "names.h" +#include "wormhole.h" void game_done(void) { diff --git a/src/gmtool.c b/src/gmtool.c index ec1a77ab3..c0d72895d 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -17,6 +17,7 @@ #include "gmtool_structs.h" #include "console.h" #include "listbox.h" +#include "wormhole.h" #include #include @@ -26,7 +27,6 @@ #if ARENA_MODULE #include #endif -#include #include #if DUNGEON_MODULE #include diff --git a/src/laws.c b/src/laws.c index 602adf816..d329959cd 100755 --- a/src/laws.c +++ b/src/laws.c @@ -22,18 +22,18 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include "laws.h" #include -#include +#include "alchemy.h" +#include "battle.h" #include "economy.h" +#include "keyword.h" +#include "market.h" #include "monster.h" +#include "move.h" #include "randenc.h" #include "spy.h" #include "study.h" -#include "market.h" -#include "keyword.h" -#include "move.h" -#include "battle.h" -#include "alchemy.h" +#include "wormhole.h" /* kernel includes */ #include diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt index 14882c139..ad77cbf13 100644 --- a/src/modules/CMakeLists.txt +++ b/src/modules/CMakeLists.txt @@ -7,7 +7,6 @@ gmcmd.c museum.c score.c weather.c -wormhole.c xmas.c ) FOREACH(_FILE ${_FILES}) diff --git a/src/test_eressea.c b/src/test_eressea.c index a721141d6..ca9b298cd 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -60,6 +60,7 @@ int RunAllTests(void) ADD_TESTS(suite, stealth); ADD_TESTS(suite, upkeep); ADD_TESTS(suite, vortex); + ADD_TESTS(suite, wormhole); CuSuiteRun(suite); CuSuiteSummary(suite, output); diff --git a/src/tests.c b/src/tests.c index 6b24b3802..e5017b5ad 100644 --- a/src/tests.c +++ b/src/tests.c @@ -115,7 +115,9 @@ building_type * test_create_buildingtype(const char * name) btype->construction->materials[1].number = 0; btype->construction->materials[0].number = 1; btype->construction->materials[0].rtype = get_resourcetype(R_STONE); - locale_setstring(default_locale, name, name); + if (default_locale) { + locale_setstring(default_locale, name, name); + } bt_register(btype); return btype; } diff --git a/src/util/umlaut.c b/src/util/umlaut.c index fb9f93bb4..61dc06ba1 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -116,22 +116,8 @@ void addtoken(void ** root, const char *str, variant id) const char str[3]; } replace[] = { /* match lower-case (!) umlauts and others to transcriptions */ - { - 228, "AE" }, /* auml */ - { - 246, "OE" }, /* ouml */ - { - 252, "UE" }, /* uuml */ - { - 223, "SS" }, /* szlig */ - { - 230, "AE" }, /* norsk */ - { - 248, "OE" }, /* norsk */ - { - 229, "AA" }, /* norsk */ - { - 0, "" } + { 228, "AE" }, { 246, "OE" }, { 252, "UE" }, { 223, "SS" }, + { 230, "AE" }, { 248, "OE" }, { 229, "AA" }, { 0, "" } }; assert(root && str); diff --git a/src/vortex.test.c b/src/vortex.test.c index 152def397..e51b9e1c6 100644 --- a/src/vortex.test.c +++ b/src/vortex.test.c @@ -35,13 +35,9 @@ static void test_move_to_vortex(CuTest *tc) { CuAssertPtrEquals(tc, r2, r); } -static void test_vortex(CuTest *tc) { -} - CuSuite *get_vortex_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_vortex); SUITE_ADD_TEST(suite, test_move_to_vortex); return suite; } diff --git a/src/modules/wormhole.c b/src/wormhole.c similarity index 90% rename from src/modules/wormhole.c rename to src/wormhole.c index abb2a7fde..6889e22a9 100644 --- a/src/modules/wormhole.c +++ b/src/wormhole.c @@ -173,20 +173,13 @@ make_wormhole(const building_type * bt_wormhole, region * r1, region * r2) ADDMSG(&r2->msgs, msg_message("wormhole_appear", "region", r2)); } -void create_wormholes(void) -{ #define WORMHOLE_CHANCE 10000 - const building_type *bt_wormhole = bt_find("wormhole"); - quicklist *ql, *rlist = 0; - region *r = regions; - int qi, i = 0, count = 0; - region **match; - if (bt_wormhole == NULL) - return; - /* - * select a list of regions. we'll sort them by age later. - */ +void select_wormhole_regions(quicklist **rlistp, int *countp) { + quicklist *rlist = 0; + region *r = regions; + int count = 0; + while (r != NULL) { int next = rng_int() % (2 * WORMHOLE_CHANCE); while (r != NULL && (next != 0 || !good_region(r))) { @@ -202,21 +195,47 @@ void create_wormholes(void) r = r->next; } - if (count < 2) - return; + *countp = count; + *rlistp = rlist; +} - match = (region **)malloc(sizeof(region *) * count); +void sort_wormhole_regions(quicklist *rlist, region **match, int count) { + quicklist *ql; + int qi, i = 0; for (ql = rlist, qi = 0; i != count; ql_advance(&ql, &qi, 1)) { match[i++] = (region *)ql_get(ql, qi); } qsort(match, count, sizeof(region *), cmp_age); - ql_free(rlist); +} +void make_wormholes(region **match, int count, const building_type *bt_wormhole) { + int i; count /= 2; for (i = 0; i != count; ++i) { make_wormhole(bt_wormhole, match[i], match[i + count]); } +} + +void create_wormholes(void) +{ + const building_type *bt_wormhole = bt_find("wormhole"); + quicklist *rlist = 0; + int count = 0; + region **match; + + if (bt_wormhole == NULL) { + return; + } + + select_wormhole_regions(&rlist, &count); + if (count < 2) { + return; + } + match = (region **)malloc(sizeof(region *) * count); + sort_wormhole_regions(rlist, match, count); + ql_free(rlist); + make_wormholes(match, count, bt_wormhole); free(match); } diff --git a/src/modules/wormhole.h b/src/wormhole.h similarity index 100% rename from src/modules/wormhole.h rename to src/wormhole.h diff --git a/src/wormhole.test.c b/src/wormhole.test.c new file mode 100644 index 000000000..03bf14ab9 --- /dev/null +++ b/src/wormhole.test.c @@ -0,0 +1,68 @@ +#include +#include + +#include "wormhole.h" +#include "tests.h" + +#include +#include +#include + +#include + +#include + +#include + +void sort_wormhole_regions(quicklist *rlist, region **match, int count); +void make_wormholes(region **match, int count, const building_type *bt_wormhole); + +static void test_make_wormholes(CuTest *tc) { + region *r1, *r2, *match[2]; + terrain_type *t_plain; + building_type *btype; + + test_cleanup(); + t_plain = test_create_terrain("plain", LAND_REGION); + btype = test_create_buildingtype("wormhole"); + match[0] = r1 = test_create_region(0, 0, t_plain); + match[1] = r2 = test_create_region(1, 0, t_plain); + make_wormholes(match, 2, btype); + CuAssertPtrNotNull(tc, r1->buildings); + CuAssertPtrNotNull(tc, r1->buildings->attribs); + CuAssertPtrEquals(tc, (void *)r1->buildings->type, (void *)btype); + CuAssertPtrNotNull(tc, r2->buildings); + CuAssertPtrNotNull(tc, r2->buildings->attribs); + CuAssertPtrEquals(tc, (void *)r2->buildings->type, (void *)btype); + CuAssertPtrEquals(tc, (void *)r1->buildings->attribs->type, (void *)r2->buildings->attribs->type); + CuAssertStrEquals(tc, r1->buildings->attribs->type->name, "wormhole"); + test_cleanup(); +} + +static void test_sort_wormhole_regions(CuTest *tc) { + region *r1, *r2, *match[2]; + terrain_type *t_plain; + quicklist *rlist = 0; + + test_cleanup(); + t_plain = test_create_terrain("plain", LAND_REGION); + r1 = test_create_region(0, 0, t_plain); + r2 = test_create_region(1, 0, t_plain); + r1->age = 4; + r2->age = 2; + ql_push(&rlist, r1); + ql_push(&rlist, r2); + sort_wormhole_regions(rlist, match, 2); + CuAssertPtrEquals(tc, r2, match[0]); + CuAssertPtrEquals(tc, r1, match[1]); + ql_free(rlist); + test_cleanup(); +} + +CuSuite *get_wormhole_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_sort_wormhole_regions); + SUITE_ADD_TEST(suite, test_make_wormholes); + return suite; +}