From ce312afc95caac39420b5173a3429646f6f4f218 Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Tue, 31 May 2016 01:49:37 +0200 Subject: [PATCH] fix dragon growl for regions with an apostrophe --- res/core/messages.xml | 11 ++++++ src/monsters.c | 30 ++++------------ src/monsters.test.c | 84 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 24 deletions(-) diff --git a/res/core/messages.xml b/res/core/messages.xml index 15de4a153..410b673e1 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -8441,4 +8441,15 @@ "$unit($unit) in $region($region): '$order($command)' - Heroes cannot recruit." + + + + + + + + "$unit($dragon): \"$if($eq($choice,0), "Groaaaam... ", "")$if($eq($choice,1), "Chrrrr... ", "")$if($eq($choice,2), "Roooarrr... ", "")$if($eq($choice,3), "Tschrrrk... ", "")$if($eq($choice,4), "Shhhhhh... ", "")$if($eq($number,1), "Ich rieche", "Wir riechen") etwas in $region($target)\"." + "$unit($dragon): \"$if($eq($choice,0), "Groaaaam... ", "")$if($eq($choice,1), "Chrrrr... ", "")$if($eq($choice,2), "Roooarrr... ", "")$if($eq($choice,3), "Tschrrrk... ", "")$if($eq($choice,4), "Shhhhhh... ", "")$if($eq($number,1), "I smell", "We smell") something in $region($target)\"." + + diff --git a/src/monsters.c b/src/monsters.c index 674ab0999..e5f07c2f8 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -547,21 +547,13 @@ static order *monster_seeks_target(region * r, unit * u) } #endif -static const char *random_growl(void) +void random_growl(const unit *u, region *target, int rand) { - switch (rng_int() % 5) { - case 0: - return "Groammm"; - case 1: - return "Roaaarrrr"; - case 2: - return "Chhhhhhhhhh"; - case 3: - return "Tschrrrkk"; - case 4: - return "Schhhh"; + const struct locale *lang = u->faction->locale; + if (rname(target, lang)) { + message *msg = msg_message("dragon_growl", "dragon number target choice", u, u->number, target, rand); + ADDMSG(&u->region->msgs, msg); } - return ""; } extern struct attrib_type at_direction; @@ -707,17 +699,7 @@ static order *plan_dragon(unit * u) reduce_weight(u); } if (rng_int() % 100 < 15) { - const struct locale *lang = u->faction->locale; - /* do a growl */ - if (rname(tr, lang)) { - addlist(&u->orders, - create_order(K_MAIL, lang, "%s '%s... %s %s %s'", - LOC(lang, parameters[P_REGION]), - random_growl(), - u->number == - 1 ? "Ich rieche" : "Wir riechen", - "etwas in", rname(tr, u->faction->locale))); - } + random_growl(u, tr, rng_int() % 5); } } else { diff --git a/src/monsters.test.c b/src/monsters.test.c index a7a4d9b18..41b655cd6 100644 --- a/src/monsters.test.c +++ b/src/monsters.test.c @@ -12,10 +12,13 @@ #include "monster.h" #include "guard.h" +#include "reports.h" #include "skill.h" #include "study.h" #include +#include +#include #include #include @@ -185,11 +188,83 @@ static void test_dragon_attacks_the_rich(CuTest * tc) test_cleanup(); } +typedef struct msg_info { + const char *name; + int numpar; + const char **argv; + const char **pnames, **tnames; + variant *values; +} msg_info; + +static msg_info *test_create_messagetype(const struct locale *lang, const char *name, int num_args, ...) { + va_list vargs; + const message_type *mtype; + char zBuffer[128]; + int i; + msg_info *info = malloc(sizeof(msg_info)); + info->argv = calloc(num_args + 1, sizeof(char *)); + info->pnames = calloc(num_args, sizeof(char *)); + info->tnames = calloc(num_args, sizeof(char *)); + info->values = calloc(num_args, sizeof(variant_type)); + + va_start(vargs, num_args); + + info->numpar = num_args; + info->name = name; + + for(i = 0; ipnames[i] = va_arg(vargs, char *); + info->tnames[i] = va_arg(vargs, char *); + sprintf(zBuffer, "%s:%s", info->pnames[i], info->tnames[i]); + info->argv[i] = _strdup(zBuffer); + } + + mtype = mt_register(mt_new(name, info->argv)); + nrt_register(mtype, lang, "mocktext", 0, "mocksection"); + + for(i = 0; itypes[i]->vtype == VAR_VOIDPTR) { + info->values[i].v = va_arg(vargs, char *); + } else if (mtype->types[i]->vtype == VAR_INT) { + info->values[i].i = va_arg(vargs, int); + } else { + assert(!"unknown variant type"); + } + } + va_end(vargs); + return info; +} + +static void assert_message(CuTest * tc, message *msg, msg_info *info) +{ + const message_type *mtype = msg->type; + assert(mtype); + int i; + + CuAssertStrEquals(tc, info->name, mtype->name); + CuAssertIntEquals(tc, info->numpar, mtype->nparameters); + for (i = 0; i != mtype->nparameters; ++i) { + CuAssertStrEquals(tc, info->pnames[i], mtype->pnames[i]); + CuAssertStrEquals(tc, info->tnames[i], mtype->types[i]->name); + if (mtype->types[i]->vtype == VAR_VOIDPTR) { + CuAssertPtrEquals(tc, info->values[i].v, msg->parameters[i].v); + } else if (mtype->types[i]->vtype == VAR_INT) { + CuAssertIntEquals(tc, info->values[i].i, msg->parameters[i].i); + } else { + assert(!"unknown variant type"); + } + } +} + +extern void random_growl(const unit *u, region *tr, int rand); + static void test_dragon_moves(CuTest * tc) { faction *f, *f2; region *r; unit *u, *m; + struct message *msg; + struct msg_info *msginfo; create_monsters(&f, &f2, &u, &m); rsetmoney(findregion(1, 0), 1000); @@ -202,6 +277,15 @@ static void test_dragon_moves(CuTest * tc) plan_monsters(f2); CuAssertPtrNotNull(tc, find_order("move east", m)); + + register_reports(); + msginfo = test_create_messagetype(f->locale, "dragon_growl", 4, "dragon", "unit", "number", "int", "target", "region", "choice", "int", m, 1, findregion(1,0), 3); + + random_growl(m, findregion(1, 0), 3); + + msg = test_get_last_message(r->msgs); + assert_message(tc, msg, msginfo); + test_cleanup(); }