From 2e3f68ed879724e2ba980fd6d4cc9ec5fa222501 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 13:26:54 +0200 Subject: [PATCH 1/9] add a config_set_int convenience function. --- src/kernel/config.c | 9 ++++++++- src/kernel/config.h | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 02ed9894d..1076e9432 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -782,11 +782,18 @@ void config_set_from(const dictionary *d, const char *valid_keys[]) } } -void config_set(const char *key, const char *value) { +void config_set(const char *key, const char *value) +{ ++config_cache_key; set_param(&configuration, key, value); } +void config_set_int(const char *key, int value) +{ + ++config_cache_key; + set_param(&configuration, key, itoa10(value)); +} + const char *config_get(const char *key) { return get_param(configuration, key); } diff --git a/src/kernel/config.h b/src/kernel/config.h index f80214fde..4cf0650ff 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -121,6 +121,7 @@ extern "C" { void free_params(struct param **pp); void config_set(const char *key, const char *value); + void config_set_int(const char *key, int value); void config_set_from(const struct _dictionary_ *d, const char *keys[]); const char *config_get(const char *key); int config_get_int(const char *key, int def); From 3e1462a1923316898efa304b4c5189eb2ba8bba8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 13:35:59 +0200 Subject: [PATCH 2/9] add empty calendar unit test. --- src/CMakeLists.txt | 28 ++++++++++++++-------------- src/calendar.test.c | 25 +++++++++++++++++++++++++ src/test_eressea.c | 25 +++++++++++++------------ 3 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 src/calendar.test.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ebe914dfe..c4c306118 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -197,42 +197,42 @@ target_link_libraries(convert ) set(TESTS_SRC - monsters.test.c - names.test.c - donations.test.c - wormhole.test.c - alchemy.test.c - guard.test.c test_eressea.c tests.c + alchemy.test.c battle.test.c - vortex.test.c - tests.test.c - volcano.test.c - reports.test.c + calendar.test.c creport.test.c - report.test.c - summary.test.c - travelthru.test.c direction.test.c + donations.test.c economy.test.c + give.test.c + guard.test.c json.test.c keyword.test.c - give.test.c laws.test.c lighthouse.test.c magic.test.c market.test.c monsters.test.c move.test.c + names.test.c piracy.test.c prefix.test.c renumber.test.c + reports.test.c + report.test.c + summary.test.c skill.test.c spells.test.c spy.test.c study.test.c + tests.test.c + travelthru.test.c upkeep.test.c + volcano.test.c + vortex.test.c + wormhole.test.c spells/flyingship.test.c spells/magicresistance.test.c triggers/shock.test.c diff --git a/src/calendar.test.c b/src/calendar.test.c new file mode 100644 index 000000000..e8df8cd58 --- /dev/null +++ b/src/calendar.test.c @@ -0,0 +1,25 @@ +#include + +#include "calendar.h" + +#include + +#include +#include "tests.h" + +static void test_calendar(CuTest * tc) +{ + gamedate gd; + + test_setup(); + get_gamedate(0, &gd); + CuAssertIntEquals(tc, 0, gd.month); + test_cleanup(); +} + +CuSuite *get_calendar_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_calendar); + return suite; +} diff --git a/src/test_eressea.c b/src/test_eressea.c index 1d991725f..5361ddf5e 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -116,35 +116,36 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(ally); ADD_SUITE(messages); /* gamecode */ - ADD_SUITE(guard); - ADD_SUITE(report); - ADD_SUITE(creport); - ADD_SUITE(summary); - ADD_SUITE(names); ADD_SUITE(battle); - ADD_SUITE(volcano); + ADD_SUITE(calendar); + ADD_SUITE(creport); ADD_SUITE(donations); - ADD_SUITE(travelthru); ADD_SUITE(economy); ADD_SUITE(flyingship); ADD_SUITE(give); + ADD_SUITE(guard); + ADD_SUITE(key); ADD_SUITE(laws); ADD_SUITE(lighthouse); ADD_SUITE(market); ADD_SUITE(monsters); ADD_SUITE(move); + ADD_SUITE(names); + ADD_SUITE(otherfaction); ADD_SUITE(piracy); ADD_SUITE(prefix); ADD_SUITE(renumber); - ADD_SUITE(key); + ADD_SUITE(report); + ADD_SUITE(shock); + ADD_SUITE(spy); ADD_SUITE(stealth); - ADD_SUITE(otherfaction); + ADD_SUITE(study); + ADD_SUITE(summary); + ADD_SUITE(travelthru); ADD_SUITE(upkeep); + ADD_SUITE(volcano); ADD_SUITE(vortex); ADD_SUITE(wormhole); - ADD_SUITE(spy); - ADD_SUITE(study); - ADD_SUITE(shock); if (suites) { CuSuite *summary = CuSuiteNew(); From fa7e21b78382fe1fb783a00a12f1575e145e2f3b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 13:40:43 +0200 Subject: [PATCH 3/9] very simple calendar test. --- src/calendar.test.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/calendar.test.c b/src/calendar.test.c index e8df8cd58..488db3bfe 100644 --- a/src/calendar.test.c +++ b/src/calendar.test.c @@ -7,19 +7,49 @@ #include #include "tests.h" +static void test_calendar_config(CuTest * tc) +{ + gamedate gd; + + test_setup(); + get_gamedate(0, &gd); + CuAssertIntEquals(tc, 0, first_turn()); + config_set_int("game.start", 42); + CuAssertIntEquals(tc, 42, first_turn()); + test_cleanup(); +} + static void test_calendar(CuTest * tc) { gamedate gd; test_setup(); get_gamedate(0, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); CuAssertIntEquals(tc, 0, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + + get_gamedate(1, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 0, gd.month); + CuAssertIntEquals(tc, 1, gd.week); + + config_set_int("game.start", 42); + get_gamedate(42, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 0, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + test_cleanup(); } CuSuite *get_calendar_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_calendar_config); SUITE_ADD_TEST(suite, test_calendar); return suite; } From 29061fa4baab2000402a9c13f9295822fd9eae15 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 15:50:19 +0200 Subject: [PATCH 4/9] fleshing out the calendar tests --- src/calendar.c | 6 +++++- src/calendar.test.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/tests.c | 2 ++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/calendar.c b/src/calendar.c index d2a70c974..4c1534a27 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -6,7 +6,7 @@ #include int first_month = 0; -int weeks_per_month = 4; +int weeks_per_month = 3; int months_per_year = 12; char **seasonnames = NULL; char **weeknames = NULL; @@ -73,4 +73,8 @@ void calendar_cleanup(void) weeknames = 0; free(weeknames2); weeknames2 = 0; + + first_month = 0; + weeks_per_month = 3; + months_per_year = 12; } diff --git a/src/calendar.test.c b/src/calendar.test.c index 488db3bfe..e858747ec 100644 --- a/src/calendar.test.c +++ b/src/calendar.test.c @@ -36,6 +36,18 @@ static void test_calendar(CuTest * tc) CuAssertIntEquals(tc, 0, gd.month); CuAssertIntEquals(tc, 1, gd.week); + get_gamedate(weeks_per_month, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 1, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + + get_gamedate(weeks_per_month*months_per_year, &gd); + CuAssertIntEquals(tc, 2, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 0, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + config_set_int("game.start", 42); get_gamedate(42, &gd); CuAssertIntEquals(tc, 1, gd.year); @@ -43,6 +55,36 @@ static void test_calendar(CuTest * tc) CuAssertIntEquals(tc, 0, gd.month); CuAssertIntEquals(tc, 0, gd.week); + first_month = 2; + get_gamedate(42, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 2, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + + test_cleanup(); +} + +static void test_calendar_season(CuTest * tc) +{ + gamedate gd; + + test_setup(); + month_season = calloc(months_per_year, sizeof(int)); + + get_gamedate(0, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 0, gd.season); + CuAssertIntEquals(tc, 0, gd.month); + CuAssertIntEquals(tc, 0, gd.week); + + month_season[1] = 1; + get_gamedate(weeks_per_month + 1, &gd); + CuAssertIntEquals(tc, 1, gd.year); + CuAssertIntEquals(tc, 1, gd.season); + CuAssertIntEquals(tc, 1, gd.month); + CuAssertIntEquals(tc, 1, gd.week); + test_cleanup(); } @@ -51,5 +93,6 @@ CuSuite *get_calendar_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_calendar_config); SUITE_ADD_TEST(suite, test_calendar); + SUITE_ADD_TEST(suite, test_calendar_season); return suite; } diff --git a/src/tests.c b/src/tests.c index 19deb91ea..60447f5d8 100644 --- a/src/tests.c +++ b/src/tests.c @@ -3,6 +3,7 @@ #include "keyword.h" #include "prefix.h" #include "reports.h" +#include "calendar.h" #include #include @@ -206,6 +207,7 @@ static void test_reset(void) { free_resources(); free_config(); default_locale = 0; + calendar_cleanup(); close_orders(); free_locales(); free_spells(); From bf935f5bb7b44c87e17a58508cb7425f18c2e048 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 16:11:45 +0200 Subject: [PATCH 5/9] gamedate: no negative turns allowed. feed the beast assert. --- src/bindings.c | 2 +- src/calendar.c | 3 +-- src/kernel/config.c | 2 +- src/laws.c | 1 + src/tests.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/bindings.c b/src/bindings.c index 2ea6f1657..33d609ee0 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -921,7 +921,7 @@ static int init_data(const char *filename, const char *catalog) if (l) { return l; } - if (turn < 0) { + if (turn <= 0) { turn = first_turn(); } return 0; diff --git a/src/calendar.c b/src/calendar.c index 4c1534a27..1804ff497 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -27,8 +27,7 @@ const gamedate *get_gamedate(int turn, gamedate * gd) int t = turn - first_turn(); assert(gd); - if (t < 0) - t = turn; + assert(t >= 0); gd->week = t % weeks_per_month; /* 0 - weeks_per_month-1 */ gd->month = (t / weeks_per_month + first_month) % months_per_year; /* 0 - months_per_year-1 */ diff --git a/src/kernel/config.c b/src/kernel/config.c index 1076e9432..37765914b 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -96,7 +96,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. struct settings global; bool lomem = false; -int turn = -1; +int turn = 0; const char *parameters[MAXPARAMS] = { "LOCALE", diff --git a/src/laws.c b/src/laws.c index 3ab17f1ab..250738f76 100644 --- a/src/laws.c +++ b/src/laws.c @@ -4183,6 +4183,7 @@ static void reset_game(void) void turn_begin(void) { + assert(turn >= 0); ++turn; reset_game(); } diff --git a/src/tests.c b/src/tests.c index 60447f5d8..ec2ac2a2f 100644 --- a/src/tests.c +++ b/src/tests.c @@ -193,7 +193,7 @@ void test_log_stderr(int flags) { static void test_reset(void) { int i; - turn = 0; + turn = 1; default_locale = 0; if (errno) { From ad465f1028b7a53edf9f7d70a78e69614a05f892 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 17:29:55 +0200 Subject: [PATCH 6/9] Ponnuki should not be aggressive. --- scripts/eressea/ponnuki.lua | 1 + src/monsters.c | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/scripts/eressea/ponnuki.lua b/scripts/eressea/ponnuki.lua index 29f882ab5..1a5d1959e 100644 --- a/scripts/eressea/ponnuki.lua +++ b/scripts/eressea/ponnuki.lua @@ -38,6 +38,7 @@ function ponnuki.init() u.name = "Ponnuki" u.info = "Go, Ponnuki, Go!" u.race_name = "Ritter von Go" + u.status = 5 -- FLIEHE print(u:show()) end else diff --git a/src/monsters.c b/src/monsters.c index d60268858..62c982532 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -748,10 +748,6 @@ void plan_monsters(faction * f) produceexp(u, SK_PERCEPTION, u->number); } - if (u->status > ST_BEHIND) { - setstatus(u, ST_FIGHT); - /* all monsters fight */ - } if (attacking && (!r->land || is_guard(u))) { monster_attacks(u, true, false); } From 7806f4991f2f151be0d123c9c9bdd78c5f787436 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 17:46:51 +0200 Subject: [PATCH 7/9] store calendar configuration nin JSON, not XML. --- conf/calendar.json | 21 ++++++++++ conf/e2/config.json | 10 +++-- conf/e2/rules.xml | 1 - conf/e3/config.json | 2 + conf/e3/rules.xml | 2 - res/core/calendar.xml | 23 ----------- res/core/de/strings.xml | 12 ++++-- res/e3a/strings.xml | 8 +--- scripts/eressea/frost.lua | 4 +- scripts/eressea/xmasitems.lua | 4 +- scripts/tests/e2/e2features.lua | 4 +- scripts/tests/e3/rules.lua | 2 +- src/calendar.c | 52 +++++++++++------------ src/calendar.h | 13 +++--- src/kernel/jsonconf.c | 73 +++++++++++++++++++++++++++++++++ src/kernel/xmlreader.c | 5 ++- src/move.h | 1 + src/report.c | 8 ++-- src/summary.c | 11 +++-- 19 files changed, 163 insertions(+), 93 deletions(-) create mode 100644 conf/calendar.json delete mode 100644 res/core/calendar.xml diff --git a/conf/calendar.json b/conf/calendar.json new file mode 100644 index 000000000..8a134dc40 --- /dev/null +++ b/conf/calendar.json @@ -0,0 +1,21 @@ +{ + "calendar" : { + "months" : [ + { "storm" : 60, "season" : 2 }, + { "storm" : 10, "season" : 2 }, + + { "storm" : 60, "season" : 3 }, + { "storm" : 10, "season" : 3 }, + + { "storm" : 60, "season" : 0 }, + { "storm" : 80, "season" : 0 }, + + { "storm" : 50, "season" : 1 }, + { "storm" : 30, "season" : 1 }, + { "storm" : 60, "season" : 1 } + ], + "weeks" : [ + "firstweek", "secondweek", "thirdweek" + ] + } +} diff --git a/conf/e2/config.json b/conf/e2/config.json index 46ee01497..84265b78f 100644 --- a/conf/e2/config.json +++ b/conf/e2/config.json @@ -1,9 +1,10 @@ { - "include": [ - "keywords.json", - "prefixes.json", + "include": [ + "keywords.json", + "calendar.json", + "prefixes.json", "e2/terrains.json" - ], + ], "disabled": [ "jsreport" ], @@ -22,6 +23,7 @@ "hunger.long": true, "init_spells": 0, "game.era": 2, + "game.start": 184, "rules.reserve.twophase": true, "rules.give.max_men": -1, "rules.check_overload": false, diff --git a/conf/e2/rules.xml b/conf/e2/rules.xml index 8d0b01b27..c237773ab 100644 --- a/conf/e2/rules.xml +++ b/conf/e2/rules.xml @@ -16,7 +16,6 @@ - diff --git a/conf/e3/config.json b/conf/e3/config.json index beb45f35d..adbfa3526 100644 --- a/conf/e3/config.json +++ b/conf/e3/config.json @@ -1,6 +1,7 @@ { "include": [ "keywords.json", + "calendar.json", "prefixes.json", "e3/terrains.json" ], @@ -52,6 +53,7 @@ "study.expensivemigrants": true, "study.speedup": 2, "game.era": 3, + "game.start": 1, "rules.reserve.twophase": true, "rules.owners.force_leave": false, "rules.wage.function": 2, diff --git a/conf/e3/rules.xml b/conf/e3/rules.xml index d59787551..b86a397c8 100644 --- a/conf/e3/rules.xml +++ b/conf/e3/rules.xml @@ -21,8 +21,6 @@ - - diff --git a/res/core/calendar.xml b/res/core/calendar.xml deleted file mode 100644 index d96037698..000000000 --- a/res/core/calendar.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/res/core/de/strings.xml b/res/core/de/strings.xml index 349892e4e..c26bfe03b 100644 --- a/res/core/de/strings.xml +++ b/res/core/de/strings.xml @@ -6523,13 +6523,17 @@ Sonnenfeuer sunfire - + + neuer Zeitrechnung + of the new age + + des zweiten Zeitalters the second age - - neuer Zeitrechnung - of the new age + + des dritten Zeitalters + the third age diff --git a/res/e3a/strings.xml b/res/e3a/strings.xml index eb9f48cf3..e3c5b10ae 100644 --- a/res/e3a/strings.xml +++ b/res/e3a/strings.xml @@ -38,12 +38,6 @@ adamantium plates - - - des dritten Zeitalters - the third age - - Packeis fast ice @@ -87,6 +81,7 @@ repeating crossbows + Gerüst scaffolding @@ -103,6 +98,7 @@ Wache watch + Marktplatz marketplace diff --git a/scripts/eressea/frost.lua b/scripts/eressea/frost.lua index 2c2df4dae..beeef68f6 100644 --- a/scripts/eressea/frost.lua +++ b/scripts/eressea/frost.lua @@ -1,11 +1,11 @@ local function is_winter(turn) local season = get_season(turn) - return season == "calendar::winter" + return season == "winter" end local function is_spring(turn) local season = get_season(turn) - return season == "calendar::spring" + return season == "spring" end local function freeze(r, chance) diff --git a/scripts/eressea/xmasitems.lua b/scripts/eressea/xmasitems.lua index 27ea641ee..dffea2675 100644 --- a/scripts/eressea/xmasitems.lua +++ b/scripts/eressea/xmasitems.lua @@ -87,7 +87,7 @@ local self = {} function self.update() local turn = get_turn() local season = get_season(turn) - if season == "calendar::winter" then + if season == "winter" then eressea.log.debug("it is " .. season .. ", the christmas trees do their magic") local msg = message.create("xmastree_effect") for r in regions() do @@ -103,7 +103,7 @@ function self.update() end else local prevseason = get_season(turn-1) - if prevseason == "calendar::winter" then + if prevseason == "winter" then -- we celebrate knut and kick out the trees. for r in regions() do if r:get_key("xm06") then diff --git a/scripts/tests/e2/e2features.lua b/scripts/tests/e2/e2features.lua index eb118c931..6495bd5e6 100644 --- a/scripts/tests/e2/e2features.lua +++ b/scripts/tests/e2/e2features.lua @@ -11,8 +11,8 @@ function setup() end function test_calendar() - assert_equal(get_season(1011), "calendar::winter") - assert_equal(get_season(1012), "calendar::spring") + assert_equal("winter", get_season(1011)) + assert_equal("spring", get_season(1012)) end function test_herbalism() diff --git a/scripts/tests/e3/rules.lua b/scripts/tests/e3/rules.lua index 60adf77d1..7eb2f13a3 100644 --- a/scripts/tests/e3/rules.lua +++ b/scripts/tests/e3/rules.lua @@ -38,7 +38,7 @@ function teardown() end function test_calendar() - assert_equal(get_season(396), "calendar::winter") + assert_equal("winter", get_season(396)) end function disable_test_bug_1738_build_castle_e3() diff --git a/src/calendar.c b/src/calendar.c index 1804ff497..6fd717429 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -1,20 +1,35 @@ #include #include "calendar.h" +#include "move.h" /* storms */ + #include #include #include +#include int first_month = 0; int weeks_per_month = 3; int months_per_year = 12; -char **seasonnames = NULL; +const char *seasonnames[CALENDAR_SEASONS] = { "winter", "spring", "summer", "fall" }; char **weeknames = NULL; char **weeknames2 = NULL; -char **monthnames = NULL; int *month_season = NULL; -char *agename = NULL; -int seasons = 0; + +const char *calendar_month(int index) +{ + static char result[20]; + snprintf(result, sizeof(result), "month_%d", index + 1); + return result; +} + +const char *calendar_era(void) +{ + static char result[20]; + int era = config_get_int("game.era", 1); + snprintf(result, sizeof(result), "age_%d", era); + return result; +} int first_turn(void) { @@ -27,7 +42,7 @@ const gamedate *get_gamedate(int turn, gamedate * gd) int t = turn - first_turn(); assert(gd); - assert(t >= 0); + assert(t>=0); gd->week = t % weeks_per_month; /* 0 - weeks_per_month-1 */ gd->month = (t / weeks_per_month + first_month) % months_per_year; /* 0 - months_per_year-1 */ @@ -40,34 +55,17 @@ void calendar_cleanup(void) { int i; - free(agename); - - if (seasonnames) { - for (i = 0; i != seasons; ++i) { - free(seasonnames[i]); - } - free(seasonnames); - seasonnames = 0; - } - - if (monthnames) { - for (i = 0; i != months_per_year; ++i) { - free(monthnames[i]); - } - free(storms); - storms = 0; - free(month_season); - month_season = 0; - free(monthnames); - monthnames = 0; - } - for (i = 0; i != weeks_per_month; ++i) { if (weeknames) free(weeknames[i]); if (weeknames2) free(weeknames2[i]); } + + free(storms); + storms = 0; + free(month_season); + month_season = 0; free(weeknames); weeknames = 0; free(weeknames2); diff --git a/src/calendar.h b/src/calendar.h index d398cd77c..f8efa392c 100644 --- a/src/calendar.h +++ b/src/calendar.h @@ -11,17 +11,12 @@ extern "C" { SEASON_SUMMER, SEASON_AUTUMN }; - - extern char *agename; - extern int first_month; - - extern int seasons; - extern char **seasonnames; +#define CALENDAR_SEASONS 4 + extern const char *seasonnames[CALENDAR_SEASONS]; extern int months_per_year; - extern char **monthnames; extern int *month_season; - extern int *storms; /* in movement.c */ + extern int first_month; extern char **weeknames; extern char **weeknames2; @@ -36,6 +31,8 @@ extern "C" { const gamedate *get_gamedate(int turn, gamedate * gd); void calendar_cleanup(void); +const char *calendar_month(int index); +const char *calendar_era(void); int first_turn(void); #ifdef __cplusplus diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index 9f96ea9d7..b7d79b64d 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -33,6 +33,8 @@ without prior permission by the authors of Eressea. /* game modules */ #include "prefix.h" +#include "move.h" +#include "calendar.h" /* util includes */ #include @@ -684,6 +686,74 @@ static void json_direction(cJSON *json, struct locale *lang) { } } +static void json_calendar(cJSON *json) { + cJSON *child; + if (json->type != cJSON_Object) { + log_error("calendar is not an object: %d", json->type); + return; + } + for (child = json->child; child; child = child->next) { + if (strcmp(child->string, "start") == 0) { + config_set_int("game.start", child->valueint); + } + else if (strcmp(child->string, "weeks") == 0) { + cJSON *entry; + int i; + if (child->type != cJSON_Array) { + log_error("calendar.weeks is not an array: %d", json->type); + return; + } + weeks_per_month = cJSON_GetArraySize(child); + free(weeknames); + weeknames = malloc(sizeof(char *) * weeks_per_month); + for (i = 0, entry = child->child; entry; entry = entry->next, ++i) { + if (entry->type == cJSON_String) { + weeknames[i] = strdup(entry->valuestring); + } + else { + log_error("calendar.weeks[%d] is not a string: %d", i, json->type); + free(weeknames); + weeknames = NULL; + return; + } + } + assert(i == weeks_per_month); + free(weeknames2); + weeknames2 = malloc(sizeof(char *) * weeks_per_month); + for (i = 0; i != weeks_per_month; ++i) { + weeknames2[i] = malloc(strlen(weeknames[i]) + 3); + sprintf(weeknames2[i], "%s_d", weeknames[i]); + } + } + else if (strcmp(child->string, "months") == 0) { + cJSON *jmonth; + int i; + if (child->type != cJSON_Array) { + log_error("calendar.seasons is not an array: %d", json->type); + return; + } + free(month_season); + month_season = NULL; + free(storms); + months_per_year = cJSON_GetArraySize(child); + storms = malloc(sizeof(int) * months_per_year); + month_season = malloc(sizeof(int) * months_per_year); + for (i = 0, jmonth = child->child; jmonth; jmonth = jmonth->next, ++i) { + if (jmonth->type == cJSON_Object) { + storms[i] = cJSON_GetObjectItem(jmonth, "storm")->valueint; + month_season[i] = cJSON_GetObjectItem(jmonth, "season")->valueint; + } + else { + log_error("calendar.months[%d] is not an object: %d", i, json->type); + free(storms); + storms = NULL; + return; + } + } + } + } +} + static void json_directions(cJSON *json) { cJSON *child; if (json->type != cJSON_Object) { @@ -890,6 +960,9 @@ void json_config(cJSON *json) { else if (strcmp(child->string, "strings") == 0) { json_strings(child); } + else if (strcmp(child->string, "calendar") == 0) { + json_calendar(child); + } else if (strcmp(child->string, "directions") == 0) { json_directions(child); } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 080ff4207..7ba3ceb94 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -30,6 +30,7 @@ without prior permission by the authors of Eressea. #include "spellbook.h" #include "calendar.h" #include "prefix.h" +#include "move.h" #include "vortex.h" @@ -398,6 +399,7 @@ static int parse_buildings(xmlDocPtr doc) return 0; } +#if 0 static int parse_calendar(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); @@ -523,7 +525,7 @@ static int parse_calendar(xmlDocPtr doc) return 0; } - +#endif static int parse_ships(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); @@ -1869,7 +1871,6 @@ static int parse_strings(xmlDocPtr doc) void register_xmlreader(void) { xml_register_callback(parse_races); - xml_register_callback(parse_calendar); xml_register_callback(parse_resources); xml_register_callback(parse_buildings); /* requires resources */ diff --git a/src/move.h b/src/move.h index 4d0b4e9f2..d5b07fef9 100644 --- a/src/move.h +++ b/src/move.h @@ -34,6 +34,7 @@ extern "C" { struct order; extern struct attrib_type at_shiptrail; + extern int *storms; /* die Zahlen sind genau äquivalent zu den race Flags */ #define MV_CANNOTMOVE (1<<5) diff --git a/src/report.c b/src/report.c index dbcf54ff1..188d8dfa9 100644 --- a/src/report.c +++ b/src/report.c @@ -123,13 +123,15 @@ static char *gamedate_season(const struct locale *lang) static char buf[256]; /* FIXME: static return value */ gamedate gd; + assert(weeknames); + get_gamedate(turn, &gd); sprintf(buf, (const char *)LOC(lang, "nr_calendar_season"), - LOC(lang, weeknames[gd.week]), - LOC(lang, monthnames[gd.month]), + LOC(lang, mkname("calendar", weeknames[gd.week])), + LOC(lang, mkname("calendar", calendar_month(gd.month))), gd.year, - agename ? LOC(lang, agename) : "", LOC(lang, seasonnames[gd.season])); + LOC(lang, mkname("calendar", calendar_era())), LOC(lang, mkname("calendar", seasonnames[gd.season]))); return buf; } diff --git a/src/summary.c b/src/summary.c index c18e54ee5..90cab98c0 100644 --- a/src/summary.c +++ b/src/summary.c @@ -151,13 +151,12 @@ static char *gamedate2(const struct locale *lang) if (weeknames2) { week = weeknames2[gd.week]; } - if (monthnames) { - month = monthnames[gd.month]; - } + month = calendar_month(gd.month); sprintf(buf, "in %s des Monats %s im Jahre %d %s.", - LOC(lang, week), - LOC(lang, month), - gd.year, agename ? LOC(lang, agename) : ""); + LOC(lang, mkname("calendar", week)), + LOC(lang, mkname("calendar", month)), + gd.year, + LOC(lang, mkname("calendar", calendar_era()))); return buf; } From 80ab32cf5128c0412b41d5744e495af4900f0b4d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 17:48:59 +0200 Subject: [PATCH 8/9] remove calendar.xml from rules. --- conf/e2/rules.xml | 1 - conf/e3/rules.xml | 1 - 2 files changed, 2 deletions(-) diff --git a/conf/e2/rules.xml b/conf/e2/rules.xml index c237773ab..7bde2205f 100644 --- a/conf/e2/rules.xml +++ b/conf/e2/rules.xml @@ -15,7 +15,6 @@ - diff --git a/conf/e3/rules.xml b/conf/e3/rules.xml index b86a397c8..7fbf7dc6e 100644 --- a/conf/e3/rules.xml +++ b/conf/e3/rules.xml @@ -10,7 +10,6 @@ - From 8765204e0060d6071690f567194fb7da83f0e4fd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 18:10:18 +0200 Subject: [PATCH 9/9] add unit tests for calendar parsing. --- src/kernel/jsonconf.test.c | 64 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 456a27d22..2f54eeb8d 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -13,6 +13,8 @@ #include "order.h" #include "terrain.h" +#include "move.h" +#include "calendar.h" #include "prefix.h" #include "util/language.h" @@ -75,7 +77,7 @@ static void test_settings(CuTest * tc) "\"float\" : 1.5 }}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); config_set("game.id", "42"); /* should not be replaced */ config_set("game.name", "Eressea"); /* should not be replaced */ json_config(json); @@ -99,7 +101,7 @@ static void test_prefixes(CuTest * tc) "]}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); json_config(json); CuAssertPtrNotNull(tc, race_prefixes); CuAssertStrEquals(tc, "snow", race_prefixes[0]); @@ -119,7 +121,7 @@ static void test_disable(CuTest * tc) "]}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); CuAssertTrue(tc, skill_enabled(SK_ALCHEMY)); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, !keyword_disabled(K_PAY)); @@ -135,6 +137,31 @@ static void test_disable(CuTest * tc) test_cleanup(); } +static void test_calendar(CuTest * tc) +{ + const char * data = "{\"calendar\": { " + "\"weeks\" : [ \"one\", \"two\", \"three\" ]," + "\"months\" : [" + "{ \"storm\" : 99, \"season\" : 1 }," + "{ \"storm\" : 22, \"season\" : 2 }" + "]" + "}}"; + cJSON *json = cJSON_Parse(data); + + test_setup(); + json_config(json); + CuAssertPtrNotNull(tc, storms); + CuAssertIntEquals(tc, 2, months_per_year); + CuAssertIntEquals(tc, 3, weeks_per_month); + CuAssertIntEquals(tc, 99, storms[0]); + CuAssertIntEquals(tc, 22, storms[1]); + CuAssertPtrNotNull(tc, month_season); + CuAssertIntEquals(tc, 1, month_season[0]); + CuAssertIntEquals(tc, 2, month_season[1]); + cJSON_Delete(json); + test_cleanup(); +} + static void test_races(CuTest * tc) { const char * data = "{\"races\": { \"orc\" : { " @@ -155,7 +182,7 @@ static void test_races(CuTest * tc) cJSON *json = cJSON_Parse(data); const struct race *rc; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, races); @@ -189,7 +216,7 @@ static void test_findrace(CuTest *tc) { const race *rc; CuAssertPtrNotNull(tc, json); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrEquals(tc, 0, (void *)findrace("Zwerg", lang)); @@ -211,7 +238,7 @@ static void test_items(CuTest * tc) cJSON *json = cJSON_Parse(data); const item_type * itype; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, it_find("axe")); @@ -249,7 +276,7 @@ static void test_ships(CuTest * tc) const ship_type *st; const terrain_type *ter; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, shiptypes); @@ -286,7 +313,7 @@ static void test_castles(CuTest *tc) { cJSON *json = cJSON_Parse(data); const building_type *bt; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); @@ -311,7 +338,7 @@ static void test_spells(CuTest * tc) cJSON *json = cJSON_Parse(data); const spell *sp; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, find_spell("fireball")); @@ -350,7 +377,7 @@ static void test_buildings(CuTest * tc) cJSON *json = cJSON_Parse(building_data); const building_type *bt; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); @@ -400,7 +427,7 @@ static void test_buildings_default(CuTest * tc) const building_type *bt; building_type clone; - test_cleanup(); + test_setup(); bt = bt_get_or_create("house"); clone = *bt; @@ -425,7 +452,7 @@ static void test_ships_default(CuTest * tc) const ship_type *st; ship_type clone; - test_cleanup(); + test_setup(); st = st_get_or_create("hodor"); clone = *st; @@ -446,7 +473,7 @@ static void test_configs(CuTest * tc) FILE *F; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); F = fopen("test.json", "w"); fwrite(building_data, 1, strlen(building_data), F); @@ -475,7 +502,7 @@ static void test_terrains(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, (void *)get_terrain("plain")); @@ -511,7 +538,7 @@ static void test_directions(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NODIRECTION, get_direction("ost", lang)); @@ -533,7 +560,7 @@ static void test_skills(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); @@ -558,7 +585,7 @@ static void test_keywords(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOKEYWORD, get_keyword("potato", lang)); @@ -583,7 +610,7 @@ static void test_strings(CuTest * tc) cJSON *json = cJSON_Parse(data); CuAssertPtrNotNull(tc, json); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, lang); CuAssertPtrEquals(tc, NULL, (void *)LOC(lang, "move")); @@ -640,6 +667,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_prefixes); SUITE_ADD_TEST(suite, test_disable); SUITE_ADD_TEST(suite, test_infinitive_from_config); + SUITE_ADD_TEST(suite, test_calendar); return suite; }