From cba452786fc877c728ba5e9908d07650b3d6121d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 22 Dec 2017 11:15:17 +0100 Subject: [PATCH] BUG 2396: insect recruit messages fixed, and tests added. --- res/core/de/strings.xml | 14 ++++-------- res/core/messages.xml | 10 ++++++++- src/calendar.c | 3 ++- src/calendar.h | 1 + src/laws.c | 4 ---- src/move.c | 2 +- src/names.c | 4 ++-- src/report.c | 33 +++++++-------------------- src/reports.c | 30 +++++++++++++++++++++++++ src/reports.h | 2 ++ src/reports.test.c | 50 +++++++++++++++++++++++++++++++++++++---- 11 files changed, 105 insertions(+), 48 deletions(-) diff --git a/res/core/de/strings.xml b/res/core/de/strings.xml index b06ff0445..6bf4f5abe 100644 --- a/res/core/de/strings.xml +++ b/res/core/de/strings.xml @@ -818,6 +818,10 @@ Ereignisse + + Hinweise + Notifications + Botschaften @@ -7305,16 +7309,6 @@ regular spell - - Es ist Winter, und Insekten können nur in Wüsten oder mit Hilfe des Nestwärme-Tranks Personen rekrutieren. - It is winter, and insects can only recruit in deserts or with the aid of nestwarmth potions. - - - - Es ist Spätherbst, und diese Woche ist die letzte vor dem Winter, in der Insekten rekrutieren können. - It is the last week before winter in which insects can still recruit. - - Eigentümer Owner diff --git a/res/core/messages.xml b/res/core/messages.xml index 9b06b3f48..0411cad4d 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -1,5 +1,13 @@ + + Es ist Winter, und Insekten können nur in Wüsten oder mit Hilfe des Nestwärme-Tranks Personen rekrutieren. + It is winter, and insects can only recruit in deserts or with the aid of nestwarmth potions. + + + Es ist Spätherbst, und diese Woche ist die letzte vor dem Winter, in der Insekten rekrutieren können. + It is the last week before winter in which insects can still recruit. + @@ -7049,7 +7057,7 @@ "Please send in orders for the next turn if you want to continue playing." - + diff --git a/src/calendar.c b/src/calendar.c index 6fd717429..c93e5bcf8 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -44,9 +44,10 @@ const gamedate *get_gamedate(int turn, gamedate * gd) assert(gd); assert(t>=0); + gd->turn = turn; 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 */ - gd->year = t / (weeks_per_year)+1; + gd->year = 1 + t / weeks_per_year; gd->season = month_season ? month_season[gd->month] : 0; return gd; } diff --git a/src/calendar.h b/src/calendar.h index f8efa392c..af4bd6a4f 100644 --- a/src/calendar.h +++ b/src/calendar.h @@ -23,6 +23,7 @@ extern "C" { extern int weeks_per_month; typedef struct gamedate { + int turn; int year; int season; int month; diff --git a/src/laws.c b/src/laws.c index d12406048..128702869 100644 --- a/src/laws.c +++ b/src/laws.c @@ -1297,10 +1297,6 @@ void quit(void) } else { ++f->age; - if (f->age + 1 < NewbieImmunity()) { - ADDMSG(&f->msgs, msg_message("newbieimmunity", "turns", - NewbieImmunity() - f->age - 1)); - } fptr = &f->next; } } diff --git a/src/move.c b/src/move.c index 99585ad09..a815c86a0 100644 --- a/src/move.c +++ b/src/move.c @@ -681,7 +681,7 @@ int check_ship_allowed(struct ship *sh, const region * r) /* insekten dürfen nicht hier rein. haben wir welche? */ unit *u = ship_owner(sh); - if (is_freezing(u)) { + if (u && is_freezing(u)) { return SA_NO_INSECT; } } diff --git a/src/names.c b/src/names.c index d85b91805..d67f441a1 100644 --- a/src/names.c +++ b/src/names.c @@ -267,7 +267,7 @@ static void dragon_name(unit * u) str = locale_getstring(default_locale, zText); assert(str != NULL); - if (u->number > 1) { + if (u->region->land && (u->number > 1)) { const char *no_article = strchr((const char *)str, ' '); assert(no_article); /* TODO: localization */ @@ -282,7 +282,7 @@ static void dragon_name(unit * u) sprintf(name, "%s, %s", n, str); /* "Name, der Titel" */ } else { - if (rng_int() % 3 == 0) { + if (u->region->land && (rng_int() % 3 == 0)) { /* TODO: localization */ snprintf(name, sizeof(name), "%s %s von %s", n, str, rname(u->region, default_locale)); } diff --git a/src/report.c b/src/report.c index 23993f489..d6179ef37 100644 --- a/src/report.c +++ b/src/report.c @@ -42,11 +42,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* gamecode includes */ #include "alchemy.h" +#include "calendar.h" #include "economy.h" #include "move.h" #include "upkeep.h" #include "vortex.h" -#include "calendar.h" #include "teleport.h" /* kernel includes */ @@ -744,8 +744,13 @@ rp_messages(struct stream *out, message_list * msgs, faction * viewer, int inden newline(out); sprintf(cat_identifier, "section_%s", section->name); section_title = LOC(viewer->locale, cat_identifier); - centre(out, section_title, true); - newline(out); + if (section_title) { + centre(out, section_title, true); + newline(out); + } + else { + log_error("no title defined for section %s in locale %s", section->name, locale_name(viewer->locale)); + } k = 1; } nr_render(m->msg, viewer->locale, lbuf, sizeof(lbuf), viewer); @@ -2050,14 +2055,6 @@ report_plaintext(const char *filename, report_context * ctx, char buf[8192]; char *bufp; size_t size; - int thisseason; - int nextseason; - gamedate date; - - get_gamedate(turn + 1, &date); - thisseason = date.season; - get_gamedate(turn + 2, &date); - nextseason = date.season; if (F == NULL) { perror(filename); @@ -2161,20 +2158,6 @@ report_plaintext(const char *filename, report_context * ctx, centre(out, buf, true); } - /* Insekten-Winter-Warnung */ - if (f->race == get_race(RC_INSECT)) { - if (thisseason == 0) { - centre(out, LOC(f->locale, "nr_insectwinter"), true); - newline(out); - } - else { - if (nextseason == 0) { - centre(out, LOC(f->locale, "nr_insectfall"), true); - newline(out); - } - } - } - bufp = buf; size = sizeof(buf) - 1; bytes = snprintf(buf, size, "%s:", LOC(f->locale, "nr_options")); diff --git a/src/reports.c b/src/reports.c index 1516f3a65..776df247f 100644 --- a/src/reports.c +++ b/src/reports.c @@ -19,6 +19,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include "reports.h" + +#include "calendar.h" #include "guard.h" #include "laws.h" #include "spells.h" @@ -1396,6 +1398,29 @@ static void cb_add_seen(region *r, unit *u, void *cbdata) { } } +void report_warnings(faction *f, const gamedate *date) +{ + if (f->age < NewbieImmunity()) { + ADDMSG(&f->msgs, msg_message("newbieimmunity", "turns", + NewbieImmunity() - f->age)); + } + + if (date) { + if (f->race == get_race(RC_INSECT)) { + if (date->season == 0) { + ADDMSG(&f->msgs, msg_message("nr_insectwinter", "")); + } + else { + gamedate next; + get_gamedate(date->turn + 1, &next); + if (next.season == 0) { + ADDMSG(&f->msgs, msg_message("nr_insectfall", "")); + } + } + } + } +} + /** set region.seen based on visibility by one faction. * * this function may also update ctx->last and ctx->first for potential @@ -1408,6 +1433,11 @@ void prepare_report(report_context *ctx, faction *f) static bool rule_region_owners; static bool rule_lighthouse_units; const struct building_type *bt_lighthouse = bt_find("lighthouse"); + gamedate now; + + /* Insekten-Winter-Warnung */ + get_gamedate(turn, &now); + report_warnings(f, &now); if (bt_lighthouse && config_changed(&config)) { rule_region_owners = config_token("rules.region_owner_pay_building", bt_lighthouse->_name); diff --git a/src/reports.h b/src/reports.h index a4850a0c7..4407bb1c1 100644 --- a/src/reports.h +++ b/src/reports.h @@ -29,6 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif + struct gamedate; struct selist; struct stream; struct seen_region; @@ -116,6 +117,7 @@ extern "C" { int size, const struct faction *viewer, bool see_unit); int report_items(const struct unit *u, struct item *result, int size, const struct unit *owner, const struct faction *viewer); + void report_warnings(struct faction *f, const struct gamedate *date); void report_item(const struct unit *owner, const struct item *i, const struct faction *viewer, const char **name, const char **basename, int *number, bool singular); diff --git a/src/reports.test.c b/src/reports.test.c index 0c9cf2b78..9506a5a0b 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -1,12 +1,13 @@ #include #include "reports.h" -#include "move.h" -#include "spy.h" -#include "lighthouse.h" -#include "travelthru.h" +#include "calendar.h" #include "keyword.h" +#include "lighthouse.h" +#include "move.h" #include "spells.h" +#include "spy.h" +#include "travelthru.h" #include #include @@ -780,6 +781,45 @@ static void test_stealth_modifier(CuTest *tc) { test_cleanup(); } +static void test_insect_warnings(CuTest *tc) { + faction *f; + gamedate gd; + + /* OBS: in unit tests, get_gamedate always returns season = 0 */ + test_setup(); + f = test_create_faction(test_create_race("insect")); + + gd.turn = 0; + gd.season = 3; + report_warnings(f, &gd); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "nr_insectfall")); + + gd.season = 0; + report_warnings(f, &gd); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "nr_insectwinter")); + test_cleanup(); +} + +static void test_newbie_warning(CuTest *tc) { + faction *f; + + test_setup(); + f = test_create_faction(test_create_race("insect")); + config_set_int("NewbieImmunity", 3); + + f->age = 2; + report_warnings(f, NULL); + CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "newbieimmunity")); + test_clear_messages(f); + + f->age = 3; + report_warnings(f, NULL); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(f->msgs, "newbieimmunity")); + test_clear_messages(f); + + test_cleanup(); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -807,5 +847,7 @@ CuSuite *get_reports_suite(void) SUITE_ADD_TEST(suite, test_bufunit); SUITE_ADD_TEST(suite, test_bufunit_fstealth); SUITE_ADD_TEST(suite, test_arg_resources); + SUITE_ADD_TEST(suite, test_insect_warnings); + SUITE_ADD_TEST(suite, test_newbie_warning); return suite; }