From 4d061d8592295e75dcdaaf9074dcf82a847d9a69 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 4 Aug 2015 22:47:55 +0200 Subject: [PATCH 1/4] repair update_long_order and K_DESTROY handling according to discussion in https://bugs.eressea.de/view.php?id=2080 --- res/core/messages.xml | 11 --- scripts/eressea/jsreport.lua | 2 - src/battle.c | 2 +- src/creport.c | 3 +- src/economy.c | 31 ++----- src/kernel/build.c | 16 ++-- src/kernel/config.c | 5 -- src/kernel/config.h | 1 - src/kernel/order.c | 9 +- src/kernel/order.h | 4 +- src/kernel/save.c | 6 +- src/laws.c | 170 +++++++++++++++-------------------- src/report.c | 7 +- src/reports.c | 6 +- 14 files changed, 109 insertions(+), 164 deletions(-) diff --git a/res/core/messages.xml b/res/core/messages.xml index 4e78fc094..237ab8c7a 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -2830,17 +2830,6 @@ "$unit($unit) in $region($region) produziert $int($amount)$if($eq($wanted,$amount),""," von $int($wanted)") $resource($resource,$wanted)." "$unit($unit) in $region($region) produces $int($amount)$if($eq($wanted,$amount),""," of $int($wanted)") $resource($resource,$amount)." - - - - - - - - - "$unit($unit) in $region($region) produziert $int($amount)$if($eq($wanted,$amount),""," von $int($wanted)") $resource($resource,$wanted)." - "$unit($unit) in $region($region) produces $int($amount)$if($eq($wanted,$amount),""," of $int($wanted)") $resource($resource,$amount)." - diff --git a/scripts/eressea/jsreport.lua b/scripts/eressea/jsreport.lua index 442b27d5f..0351efd12 100644 --- a/scripts/eressea/jsreport.lua +++ b/scripts/eressea/jsreport.lua @@ -1,7 +1,5 @@ local pkg = {} -print("loading jsreport module") - function pkg.init() eressea.settings.set("feature.jsreport.enable", "1") end diff --git a/src/battle.c b/src/battle.c index 4719da5b7..d45cf02e7 100644 --- a/src/battle.c +++ b/src/battle.c @@ -3232,7 +3232,7 @@ fighter *make_fighter(battle * b, unit * u, side * s1, bool attack) } /* Illusionen und Zauber kaempfen nicht */ - if (fval(u_race(u), RCF_ILLUSIONARY) || idle(u->faction) || u->number == 0) { + if (fval(u_race(u), RCF_ILLUSIONARY) || u->number == 0) { return NULL; } if (s1 == NULL) { diff --git a/src/creport.c b/src/creport.c index 38e63e299..fefd7934e 100644 --- a/src/creport.c +++ b/src/creport.c @@ -929,7 +929,8 @@ static void cr_output_unit(FILE * F, const region * r, const faction * f, } } for (ord = u->orders; ord; ord = ord->next) { - if (u->old_orders && is_repeated(ord)) + keyword_t kwd = getkeyword(ord); + if (u->old_orders && is_repeated(kwd)) continue; /* unit has defaults */ if (is_persistent(ord)) { fwriteorder(F, ord, f->locale, true); diff --git a/src/economy.c b/src/economy.c index 23ccb3fde..8cf46845b 100644 --- a/src/economy.c +++ b/src/economy.c @@ -563,7 +563,7 @@ static void recruit(unit * u, struct order *ord, request ** recruitorders) return; } } - if (!playerrace(rc) || idle(u->faction)) { + if (!playerrace(rc)) { cmistake(u, ord, 139, MSG_EVENT); return; } @@ -979,26 +979,13 @@ void economics(region * r) remove_empty_units_in_region(r); for (u = r->units; u; u = u->next) { - order *ord; - bool destroyed = false; - if (u->number > 0) { - for (ord = u->orders; ord; ord = ord->next) { - keyword_t kwd = getkeyword(ord); - if (kwd == K_DESTROY) { - if (!destroyed) { - if (destroy_cmd(u, ord) != 0) - ord = NULL; - destroyed = true; - } - } - if (u->orders == NULL) { - break; - } + order *ord = u->thisorder; + keyword_t kwd = getkeyword(ord); + if (kwd == K_DESTROY) { + if (destroy_cmd(u, ord) == 0) { + fset(u, UFL_LONGACTION | UFL_NOTMOVING); } } - if (destroyed) { - fset(u, UFL_LONGACTION | UFL_NOTMOVING); - } } } @@ -1051,7 +1038,7 @@ static void manufacture(unit * u, const item_type * itype, int want) i_change(&u->items, itype, n); if (want == INT_MAX) want = n; - ADDMSG(&u->faction->msgs, msg_message("manufacture", + ADDMSG(&u->faction->msgs, msg_message("produce", "unit region amount wanted resource", u, u->region, n, want, itype->rtype)); } @@ -1466,7 +1453,7 @@ static void create_potion(unit * u, const potion_type * ptype, int want) i_change(&u->items, ptype->itype, built); if (want == INT_MAX) want = built; - ADDMSG(&u->faction->msgs, msg_message("manufacture", + ADDMSG(&u->faction->msgs, msg_message("produce", "unit region amount wanted resource", u, u->region, built, want, ptype->itype->rtype)); break; @@ -3216,7 +3203,7 @@ void produce(struct region *r) continue; if (fval(u, UFL_LONGACTION) && u->thisorder == NULL) { - /* this message was already given in laws.setdefaults + /* this message was already given in laws.c:update_long_order cmistake(u, u->thisorder, 52, MSG_PRODUCE); */ continue; diff --git a/src/kernel/build.c b/src/kernel/build.c index fcb116741..37006e968 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -151,11 +151,11 @@ int destroy_cmd(unit * u, struct order *ord) int n = INT_MAX; if (u->number < 1) - return 0; + return 1; if (fval(u, UFL_LONGACTION)) { cmistake(u, ord, 52, MSG_PRODUCE); - return 0; + return 52; } init_order(ord); @@ -183,11 +183,11 @@ int destroy_cmd(unit * u, struct order *ord) if (u != building_owner(b)) { cmistake(u, ord, 138, MSG_PRODUCE); - return 0; + return 138; } if (fval(b->type, BTF_INDESTRUCTIBLE)) { cmistake(u, ord, 138, MSG_PRODUCE); - return 0; + return 138; } if (n >= b->size) { /* destroy completly */ @@ -213,11 +213,11 @@ int destroy_cmd(unit * u, struct order *ord) if (u != ship_owner(sh)) { cmistake(u, ord, 138, MSG_PRODUCE); - return 0; + return 138; } if (fval(r->terrain, SEA_REGION)) { cmistake(u, ord, 14, MSG_EVENT); - return 0; + return 14; } if (n >= (sh->size * 100) / sh->type->construction->maxsize) { @@ -242,11 +242,11 @@ int destroy_cmd(unit * u, struct order *ord) } else { cmistake(u, ord, 138, MSG_PRODUCE); - return 0; + return 138; } if (con) { - /* TODO: Nicht an ZERSTÖRE mit Punktangabe angepaßt! */ + /* TODO: Nicht an ZERSTÖRE mit Punktangabe angepasst! */ int c; for (c = 0; con->materials[c].number; ++c) { const requirement *rq = con->materials + c; diff --git a/src/kernel/config.c b/src/kernel/config.c index 699044520..ece8ca72f 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -906,11 +906,6 @@ int newcontainerid(void) return random_no; } -bool idle(faction * f) -{ - return (bool)(f ? false : true); -} - int maxworkingpeasants(const struct region *r) { int size = production(r); diff --git a/src/kernel/config.h b/src/kernel/config.h index 4ca76871c..62fa2d4d6 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -179,7 +179,6 @@ extern "C" { bool has_limited_skills(const struct unit *u); const struct race *findrace(const char *, const struct locale *); - bool idle(struct faction *f); bool unit_has_cursed_item(const struct unit *u); /* grammatik-flags: */ diff --git a/src/kernel/order.c b/src/kernel/order.c index 20cce5853..08d16088f 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -389,9 +389,8 @@ order *parse_order(const char *s, const struct locale * lang) * \return true if the order is long * \sa is_exclusive(), is_repeated(), is_persistent() */ -bool is_repeated(const order * ord) +bool is_repeated(keyword_t kwd) { - keyword_t kwd = ORD_KEYWORD(ord); switch (kwd) { case K_CAST: case K_BUY: @@ -468,10 +467,8 @@ bool is_exclusive(const order * ord) * \return true if the order is long * \sa is_exclusive(), is_repeated(), is_persistent() */ -bool is_long(const order * ord) +bool is_long(keyword_t kwd) { - keyword_t kwd = ORD_KEYWORD(ord); - switch (kwd) { case K_CAST: case K_BUY: @@ -522,7 +519,7 @@ bool is_persistent(const order * ord) case K_KOMMENTAR: return true; default: - return ord->_persistent || is_repeated(ord); + return ord->_persistent || is_repeated(kwd); } } diff --git a/src/kernel/order.h b/src/kernel/order.h index 27c61681c..75d741e42 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -55,8 +55,8 @@ extern "C" { char* get_command(const order *ord, char *buffer, size_t size); bool is_persistent(const order * ord); bool is_exclusive(const order * ord); - bool is_repeated(const order * ord); - bool is_long(const order * ord); + bool is_repeated(keyword_t kwd); + bool is_long(keyword_t kwd); char *write_order(const order * ord, char *buffer, size_t size); keyword_t init_order(const struct order *ord); diff --git a/src/kernel/save.c b/src/kernel/save.c index fba9b6083..1d610d013 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -139,7 +139,8 @@ static unit *unitorders(FILE * F, int enc, struct faction *f) ordp = &u->old_orders; while (*ordp) { order *ord = *ordp; - if (!is_repeated(ord)) { + keyword_t kwd = getkeyword(ord); + if (!is_repeated(kwd)) { *ordp = ord->next; ord->next = NULL; free_order(ord); @@ -777,7 +778,8 @@ void write_unit(struct gamedata *data, const unit * u) } } for (ord = u->orders; ord; ord = ord->next) { - if (u->old_orders && is_repeated(ord)) + keyword_t kwd = getkeyword(ord); + if (u->old_orders && is_repeated(kwd)) continue; /* has new defaults */ if (is_persistent(ord)) { if (++p < MAXPERSISTENT) { diff --git a/src/laws.c b/src/laws.c index 76658a2e3..40ffac896 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3407,126 +3407,101 @@ void new_units(void) } } -/** Checks for two long orders and issues a warning if necessary. - */ -void check_long_orders(unit * u) -{ - order *ord; - keyword_t otherorder = MAXKEYWORDS; - - for (ord = u->orders; ord; ord = ord->next) { - if (getkeyword(ord) == NOKEYWORD) { - cmistake(u, ord, 22, MSG_EVENT); - } - else if (is_long(ord)) { - keyword_t longorder = getkeyword(ord); - if (otherorder != MAXKEYWORDS) { - switch (longorder) { - case K_CAST: - if (otherorder != longorder) { - cmistake(u, ord, 52, MSG_EVENT); - } - break; - case K_BUY: - if (otherorder == K_SELL) { - otherorder = K_BUY; - } - else { - cmistake(u, ord, 52, MSG_EVENT); - } - break; - case K_SELL: - if (otherorder != K_SELL && otherorder != K_BUY) { - cmistake(u, ord, 52, MSG_EVENT); - } - break; - default: - cmistake(u, ord, 52, MSG_EVENT); - } - } - else { - otherorder = longorder; - } - } - } -} - void update_long_order(unit * u) { order *ord; - bool trade = false; + bool exclusive = true; + keyword_t thiskwd = NOKEYWORD; bool hunger = LongHunger(u); freset(u, UFL_MOVED); freset(u, UFL_LONGACTION); - if (hunger) { - /* Hungernde Einheiten führen NUR den default-Befehl aus */ - set_order(&u->thisorder, default_order(u->faction->locale)); - } - else { - check_long_orders(u); - } + /* check all orders for a potential new long order this round: */ for (ord = u->orders; ord; ord = ord->next) { - if (getkeyword(ord) == NOKEYWORD) - continue; + keyword_t kwd = getkeyword(ord); + if (kwd == NOKEYWORD) continue; - if (u->old_orders && is_repeated(ord)) { + if (u->old_orders && is_repeated(kwd)) { /* this new order will replace the old defaults */ free_orders(&u->old_orders); - if (hunger) - break; } - if (hunger) - continue; - if (is_exclusive(ord)) { - /* Ãœber dieser Zeile nur Befehle, die auch eine idle Faction machen darf */ - if (idle(u->faction)) { - set_order(&u->thisorder, default_order(u->faction->locale)); - } - else { - set_order(&u->thisorder, copy_order(ord)); + // hungry units do not get long orders: + if (hunger) { + if (u->old_orders) { + // keep looking for repeated orders that might clear the old_orders + continue; } break; } - else { - keyword_t keyword = getkeyword(ord); - switch (keyword) { - /* Wenn gehandelt wird, darf kein langer Befehl ausgeführt - * werden. Da Handel erst nach anderen langen Befehlen kommt, - * muss das vorher abgefangen werden. Wir merken uns also - * hier, ob die Einheit handelt. */ - case K_BUY: - case K_SELL: - /* Wenn die Einheit handelt, muss der Default-Befehl gelöscht - * werden. - * Wird je diese Ausschliesslichkeit aufgehoben, muss man aufpassen - * mit der Reihenfolge von Kaufen, Verkaufen etc., damit es Spielern - * nicht moeglich ist, Schulden zu machen. */ - trade = true; - break; - case K_CAST: - /* dient dazu, das neben Zaubern kein weiterer Befehl - * ausgeführt werden kann, Zaubern ist ein kurzer Befehl */ - set_order(&u->thisorder, copy_order(ord)); - break; + if (is_long(kwd)) { + if (thiskwd == NOKEYWORD) { + // we have found the (first) long order + // some long orders can have multiple instances: + switch (kwd) { + /* Wenn gehandelt wird, darf kein langer Befehl ausgeführt + * werden. Da Handel erst nach anderen langen Befehlen kommt, + * muss das vorher abgefangen werden. Wir merken uns also + * hier, ob die Einheit handelt. */ + case K_BUY: + case K_SELL: + case K_CAST: + // non-exclusive orders can be used with others. BUY can be paired with SELL, + // CAST with other CAST orders. compatibility is checked once the second + // long order is analyzed (below). + exclusive = false; + break; - default: - break; + default: + set_order(&u->thisorder, copy_order(ord)); + break; + } + thiskwd = kwd; + } + else { + // we have found a second long order. this is okay for some, but not all commands. + // u->thisorder is already set, and should not have to be updated. + switch (kwd) { + case K_CAST: + if (thiskwd != K_CAST) { + cmistake(u, ord, 52, MSG_EVENT); + } + break; + case K_SELL: + if (thiskwd != K_SELL && thiskwd != K_BUY) { + cmistake(u, ord, 52, MSG_EVENT); + } + break; + case K_BUY: + if (thiskwd != K_SELL) { + cmistake(u, ord, 52, MSG_EVENT); + } + else { + thiskwd = K_BUY; + } + break; + default: +#ifdef TODO // TODO: decide https://bugs.eressea.de/view.php?id=2080#c6011 + if (kwd < thiskwd) { + /* swap thisorder for the new one */ + order *tmp = ord; + ord = u->thisorder; + u->thisorder = tmp; + } +#endif + cmistake(u, ord, 52, MSG_EVENT); + break; + } } } } - if (hunger) { - return; - } - /* Wenn die Einheit handelt, muss der Default-Befehl gelöscht - * werden. */ - - if (trade) { - /* fset(u, UFL_LONGACTION|UFL_NOTMOVING); */ + // Hungernde Einheiten führen NUR den default-Befehl aus + set_order(&u->thisorder, default_order(u->faction->locale)); + } else if (!exclusive) { + // Wenn die Einheit handelt oder zaubert, muss der Default-Befehl gelöscht werden. set_order(&u->thisorder, NULL); } } @@ -3696,6 +3671,7 @@ void defaultorders(void) free_order(ord); if (!neworders) { /* lange Befehle aus orders und old_orders löschen zu gunsten des neuen */ + // TODO: why only is_exclusive, not is_long? what about CAST, BUY, SELL? remove_exclusive(&u->orders); remove_exclusive(&u->old_orders); neworders = true; diff --git a/src/report.c b/src/report.c index 0b591b5ed..3d57ee4c2 100644 --- a/src/report.c +++ b/src/report.c @@ -1471,14 +1471,12 @@ report_template(const char *filename, report_context * ctx, const char *charset) newline(out); newline(out); - sprintf(buf, "%s %s \"%s\"", LOC(f->locale, "ERESSEA"), factionid(f), - LOC(f->locale, "enterpasswd")); + sprintf(buf, "%s %s \"%s\"", LOC(f->locale, "ERESSEA"), factionid(f), f->passw); rps_nowrap(out, buf); newline(out); newline(out); sprintf(buf, "; ECHECK -l -w4 -r%d -v%s", f->race->recruitcost, ECHECK_VERSION); - /* -v3.4: ECheck Version 3.4.x */ rps_nowrap(out, buf); newline(out); @@ -1575,7 +1573,8 @@ report_template(const char *filename, report_context * ctx, const char *charset) newline(out); } for (ord = u->orders; ord; ord = ord->next) { - if (u->old_orders && is_repeated(ord)) + keyword_t kwd = getkeyword(ord); + if (u->old_orders && is_repeated(kwd)) continue; /* unit has defaults */ if (is_persistent(ord)) { strcpy(buf, " "); diff --git a/src/reports.c b/src/reports.c index 128815f71..88b11e31d 100644 --- a/src/reports.c +++ b/src/reports.c @@ -793,7 +793,8 @@ size_t size) bool printed = 0; order *ord;; for (ord = u->old_orders; ord; ord = ord->next) { - if (is_repeated(ord)) { + keyword_t kwd = getkeyword(ord); + if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { bytes = buforder(bufp, size, ord, printed++); if (wrptr(&bufp, &size, bytes) != 0) @@ -805,7 +806,8 @@ size_t size) } if (printed < ORDERS_IN_NR) for (ord = u->orders; ord; ord = ord->next) { - if (is_repeated(ord)) { + keyword_t kwd = getkeyword(ord); + if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { bytes = buforder(bufp, size, ord, printed++); if (wrptr(&bufp, &size, bytes) != 0) From 928b9966d00af9cc9182e2b42f6b105c20957b14 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 4 Aug 2015 23:04:00 +0200 Subject: [PATCH 2/4] fix broken test (select DESTROY over MOVE), start writing unit tests for update_long_order --- src/laws.c | 16 ++++++++-------- src/laws.test.c | 13 +++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/laws.c b/src/laws.c index 40ffac896..b9df102cd 100755 --- a/src/laws.c +++ b/src/laws.c @@ -3483,15 +3483,15 @@ void update_long_order(unit * u) } break; default: -#ifdef TODO // TODO: decide https://bugs.eressea.de/view.php?id=2080#c6011 - if (kwd < thiskwd) { - /* swap thisorder for the new one */ - order *tmp = ord; - ord = u->thisorder; - u->thisorder = tmp; + // TODO: decide https://bugs.eressea.de/view.php?id=2080#c6011 + if (kwd > thiskwd) { + // swap out thisorder for the new one + cmistake(u, u->thisorder, 52, MSG_EVENT); + set_order(&u->thisorder, copy_order(ord)); + } + else { + cmistake(u, ord, 52, MSG_EVENT); } -#endif - cmistake(u, ord, 52, MSG_EVENT); break; } } diff --git a/src/laws.test.c b/src/laws.test.c index b5429e1aa..b6b13d37c 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -768,9 +768,22 @@ static void test_luck_message(CuTest *tc) { test_cleanup(); } +static void test_update_long_order(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrEquals(tc, 0, u->orders); + CuAssertPtrEquals(tc, 0, u->old_orders); + test_cleanup(); +} + CuSuite *get_laws_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_update_long_order); SUITE_ADD_TEST(suite, test_new_building_can_be_renamed); SUITE_ADD_TEST(suite, test_rename_building); SUITE_ADD_TEST(suite, test_rename_building_twice); From c22636ca13f1c7c10089b2f31dffbbdd55eda39d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 5 Aug 2015 00:03:30 +0200 Subject: [PATCH 3/4] unit tests for update_long_order. e3 tests are still broken. --- src/kernel/config.c | 10 ++++ src/kernel/config.h | 3 + src/kernel/order.c | 22 +++++++- src/laws.test.c | 130 +++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 160 insertions(+), 5 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index ece8ca72f..8698fde51 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1681,6 +1681,11 @@ void kernel_init(void) } static order * defaults[MAXLOCALES]; +keyword_t default_keyword = NOKEYWORD; + +void set_default_order(int kwd) { + default_keyword = (keyword_t)kwd; +} order *default_order(const struct locale *lang) { @@ -1688,6 +1693,11 @@ order *default_order(const struct locale *lang) int i = locale_index(lang); order *result = 0; assert(i < MAXLOCALES); + + if (default_keyword!=NOKEYWORD) { + return create_order(default_keyword, lang, 0); + } + result = defaults[i]; if (!result && usedefault) { const char * str = LOC(lang, "defaultorder"); diff --git a/src/kernel/config.h b/src/kernel/config.h index 62fa2d4d6..5e7e77899 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -286,7 +286,10 @@ extern "C" { int AllianceAuto(void); /* flags that allied factions get automatically */ int AllianceRestricted(void); /* flags restricted to allied factions */ int HelpMask(void); /* flags restricted to allied factions */ + struct order *default_order(const struct locale *lang); + void set_default_order(int kwd); + int entertainmoney(const struct region *r); void free_gamedata(void); diff --git a/src/kernel/order.c b/src/kernel/order.c index 08d16088f..008bfe0fe 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -252,6 +252,19 @@ static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) return data; } +static void free_localedata(int lindex) { + int i; + for (i = 0; i != MAXKEYWORDS; ++i) { + release_data(locale_array[lindex]->short_orders[i]); + locale_array[lindex]->short_orders[i] = 0; + } + for (i = 0; i != MAXSKILLS; ++i) { + release_data(locale_array[lindex]->study_orders[i]); + locale_array[lindex]->study_orders[i] = 0; + } + locale_array[lindex]->lang = 0; +} + static order *create_order_i(keyword_t kwd, const char *sptr, bool persistent, const struct locale *lang) { @@ -276,7 +289,12 @@ static order *create_order_i(keyword_t kwd, const char *sptr, bool persistent, lindex = locale_index(lang); assert(lindex < MAXLOCALES); - locale_array[lindex] = (locale_data *)calloc(1, sizeof(locale_data)); + if (!locale_array[lindex]) { + locale_array[lindex] = (locale_data *)calloc(1, sizeof(locale_data)); + } + else if (locale_array[lindex]->lang != lang) { + free_localedata(lindex); + } locale_array[lindex]->lang = lang; ord = (order *)malloc(sizeof(order)); @@ -292,13 +310,13 @@ order *create_order(keyword_t kwd, const struct locale * lang, const char *params, ...) { char zBuffer[DISPLAYSIZE]; - assert(lang); if (params) { char *bufp = zBuffer; int bytes; size_t size = sizeof(zBuffer) - 1; va_list marker; + assert(lang); va_start(marker, params); while (*params) { if (*params == '%') { diff --git a/src/laws.test.c b/src/laws.test.c index b6b13d37c..900336d5c 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -768,22 +768,146 @@ static void test_luck_message(CuTest *tc) { test_cleanup(); } -static void test_update_long_order(CuTest *tc) { +static void test_long_order_normal(CuTest *tc) { + // TODO: write more tests + unit *u; + order *ord; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fset(u, UFL_MOVED); + fset(u, UFL_LONGACTION); + u->faction->locale = get_or_create_locale("de"); + ord = create_order(K_MOVE, u->faction->locale, 0); + unit_addorder(u, ord); + update_long_order(u); + CuAssertPtrEquals(tc, ord->data, u->thisorder->data); + CuAssertIntEquals(tc, 0, fval(u, UFL_MOVED)); + CuAssertIntEquals(tc, 0, fval(u, UFL_LONGACTION)); + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + CuAssertPtrEquals(tc, 0, u->old_orders); + test_cleanup(); +} + +static void test_long_order_none(CuTest *tc) { // TODO: write more tests unit *u; test_cleanup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); update_long_order(u); CuAssertPtrEquals(tc, 0, u->thisorder); CuAssertPtrEquals(tc, 0, u->orders); - CuAssertPtrEquals(tc, 0, u->old_orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + test_cleanup(); +} + +static void test_long_order_cast(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_CAST, u->faction->locale, 0)); + unit_addorder(u, create_order(K_CAST, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + test_cleanup(); +} + +static void test_long_order_buy_sell(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); + unit_addorder(u, create_order(K_SELL, u->faction->locale, 0)); + unit_addorder(u, create_order(K_SELL, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + test_cleanup(); +} + +static void test_long_order_multi_long(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_MOVE, u->faction->locale, 0)); + unit_addorder(u, create_order(K_DESTROY, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrNotNull(tc, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertStrEquals(tc, "error52", test_get_messagetype(u->faction->msgs->begin->msg)); + test_cleanup(); +} + +static void test_long_order_multi_buy(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); + unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertStrEquals(tc, "error52", test_get_messagetype(u->faction->msgs->begin->msg)); + test_cleanup(); +} + +static void test_long_order_buy_cast(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); + unit_addorder(u, create_order(K_CAST, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertStrEquals(tc, "error52", test_get_messagetype(u->faction->msgs->begin->msg)); + test_cleanup(); +} + +static void test_long_order_hungry(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + set_param(&global.parameters, "hunger.long", "1"); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fset(u, UFL_HUNGER); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_MOVE, u->faction->locale, 0)); + unit_addorder(u, create_order(K_DESTROY, u->faction->locale, 0)); + set_default_order(K_WORK); + update_long_order(u); + CuAssertIntEquals(tc, K_WORK, getkeyword(u->thisorder)); + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + set_default_order(NOKEYWORD); test_cleanup(); } CuSuite *get_laws_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_update_long_order); + SUITE_ADD_TEST(suite, test_long_order_normal); + SUITE_ADD_TEST(suite, test_long_order_none); + SUITE_ADD_TEST(suite, test_long_order_cast); + SUITE_ADD_TEST(suite, test_long_order_buy_sell); + SUITE_ADD_TEST(suite, test_long_order_multi_long); + SUITE_ADD_TEST(suite, test_long_order_multi_buy); + SUITE_ADD_TEST(suite, test_long_order_buy_cast); + SUITE_ADD_TEST(suite, test_long_order_hungry); SUITE_ADD_TEST(suite, test_new_building_can_be_renamed); SUITE_ADD_TEST(suite, test_rename_building); SUITE_ADD_TEST(suite, test_rename_building_twice); From 420574c7e46346619b5eda02176ec43adaaf8a44 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 5 Aug 2015 10:25:25 +0200 Subject: [PATCH 4/4] add unit-test for casting spells, fix spell-casting (was looking for thisorder), all E3 tests pass again --- scripts/tests/e3/rules.lua | 1 - scripts/tests/e3/spells.lua | 1 - src/laws.test.c | 17 +++++++++++++++++ src/magic.c | 26 ++++++++++++-------------- src/magic.test.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/strings.c | 1 + 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/scripts/tests/e3/rules.lua b/scripts/tests/e3/rules.lua index 0e9d52147..902a91741 100644 --- a/scripts/tests/e3/rules.lua +++ b/scripts/tests/e3/rules.lua @@ -460,7 +460,6 @@ function test_canoe_passes_through_land() u1:add_order("NACH O O O") process_orders() assert_equal(land, u2.region, "canoe did not stop at coast") - u1:add_order("NACH O O O") process_orders() assert_equal(dst, sh.region, "canoe could not leave coast") assert_equal(dst, u1.region, "canoe could not leave coast") diff --git a/scripts/tests/e3/spells.lua b/scripts/tests/e3/spells.lua index c4f0aadaf..f99e4687b 100644 --- a/scripts/tests/e3/spells.lua +++ b/scripts/tests/e3/spells.lua @@ -58,7 +58,6 @@ function test_magic() u:add_spell("protective_runes") u:add_spell("analyze_magic") u:clear_orders() - u:add_order("ZAUBERE \"Runen des Schutzes\" BURG " .. itoa36(b.id)); u.building = b u:add_order("ZAUBERE \"Magie analysieren\" BURG " .. itoa36(b.id)); process_orders() diff --git a/src/laws.test.c b/src/laws.test.c index 900336d5c..12c2e8ecd 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -863,6 +863,22 @@ static void test_long_order_multi_buy(CuTest *tc) { test_cleanup(); } +static void test_long_order_multi_sell(CuTest *tc) { + // TODO: write more tests + unit *u; + test_cleanup(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = get_or_create_locale("de"); + unit_addorder(u, create_order(K_SELL, u->faction->locale, 0)); + unit_addorder(u, create_order(K_BUY, u->faction->locale, 0)); + unit_addorder(u, create_order(K_SELL, u->faction->locale, 0)); + update_long_order(u); + CuAssertPtrEquals(tc, 0, u->thisorder); + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, 0, u->faction->msgs); + test_cleanup(); +} + static void test_long_order_buy_cast(CuTest *tc) { // TODO: write more tests unit *u; @@ -906,6 +922,7 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_long_order_buy_sell); SUITE_ADD_TEST(suite, test_long_order_multi_long); SUITE_ADD_TEST(suite, test_long_order_multi_buy); + SUITE_ADD_TEST(suite, test_long_order_multi_sell); SUITE_ADD_TEST(suite, test_long_order_buy_cast); SUITE_ADD_TEST(suite, test_long_order_hungry); SUITE_ADD_TEST(suite, test_new_building_can_be_renamed); diff --git a/src/magic.c b/src/magic.c index 943c59db2..075880020 100644 --- a/src/magic.c +++ b/src/magic.c @@ -777,7 +777,7 @@ int spellcost(unit * u, const spell * sp) int count = countspells(u, 0); const resource_type *r_aura = get_resourcetype(R_AURA); - for (k = 0; sp->components[k].type; k++) { + for (k = 0; sp->components && sp->components[k].type; k++) { if (sp->components[k].type == r_aura) { aura = sp->components[k].amount; } @@ -798,7 +798,7 @@ static int spl_costtyp(const spell * sp) int k; int costtyp = SPC_FIX; - for (k = 0; sp->components[k].type; k++) { + for (k = 0; sp->components && sp->components[k].type; k++) { if (costtyp == SPC_LINEAR) return SPC_LINEAR; @@ -827,7 +827,7 @@ int eff_spelllevel(unit * u, const spell * sp, int cast_level, int range) int k, maxlevel, needplevel; int costtyp = SPC_FIX; - for (k = 0; sp->components[k].type; k++) { + for (k = 0; sp->components && sp->components[k].type; k++) { if (cast_level == 0) return 0; @@ -894,7 +894,7 @@ void pay_spell(unit * u, const spell * sp, int cast_level, int range) int resuse; assert(cast_level > 0); - for (k = 0; sp->components[k].type; k++) { + for (k = 0; sp->components && sp->components[k].type; k++) { if (sp->components[k].type == r_aura) { resuse = spellcost(u, sp) * range; } @@ -954,7 +954,7 @@ cancast(unit * u, const spell * sp, int level, int range, struct order * ord) return false; } - for (k = 0; sp->components[k].type; ++k) { + for (k = 0; sp->components && sp->components[k].type; ++k) { if (sp->components[k].amount > 0) { const resource_type *rtype = sp->components[k].type; int itemhave; @@ -2768,15 +2768,13 @@ void magic(void) continue; } - if (u->thisorder != NULL) { - for (ord = u->orders; ord; ord = ord->next) { - if (getkeyword(ord) == K_CAST) { - castorder *co = cast_cmd(u, ord); - fset(u, UFL_LONGACTION | UFL_NOTMOVING); - if (co) { - const spell *sp = co->sp; - add_castorder(&spellranks[sp->rank], co); - } + for (ord = u->orders; ord; ord = ord->next) { + if (getkeyword(ord) == K_CAST) { + castorder *co = cast_cmd(u, ord); + fset(u, UFL_LONGACTION | UFL_NOTMOVING); + if (co) { + const spell *sp = co->sp; + add_castorder(&spellranks[sp->rank], co); } } } diff --git a/src/magic.test.c b/src/magic.test.c index 9a01d5d4e..600f724ca 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -3,6 +3,7 @@ #include "magic.h" #include +#include #include #include #include @@ -382,9 +383,45 @@ void test_hasspell(CuTest * tc) test_cleanup(); } +static quicklist * casts; + +static int cast_fireball(struct castorder * co) { + ql_push(&casts, co); + return 0; +} + +void test_multi_cast(CuTest *tc) { + unit *u; + spell *sp; + struct locale * lang; + + test_cleanup(); + sp = create_spell("fireball", 0); + sp->cast = cast_fireball; + CuAssertPtrEquals(tc, sp, find_spell("fireball")); + + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + u->faction->locale = lang = get_or_create_locale("de"); + locale_setstring(lang, mkname("spell", sp->sname), "Feuerball"); + CuAssertStrEquals(tc, "Feuerball", spell_name(sp, lang)); + set_level(u, SK_MAGIC, 10); + unit_add_spell(u, 0, sp, 1); + CuAssertPtrEquals(tc, sp, unit_getspell(u, "Feuerball", lang)); + + unit_addorder(u, create_order(K_CAST, u->faction->locale, "Feuerball")); + unit_addorder(u, create_order(K_CAST, u->faction->locale, "Feuerball")); + CuAssertPtrEquals(tc, casts, 0); + magic(); + CuAssertPtrNotNull(tc, casts); + CuAssertIntEquals(tc, 2, ql_length(casts)); + ql_free(casts); + test_cleanup(); +} + CuSuite *get_magic_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_multi_cast); SUITE_ADD_TEST(suite, test_updatespells); SUITE_ADD_TEST(suite, test_spellbooks); SUITE_ADD_TEST(suite, test_pay_spell); diff --git a/src/util/strings.c b/src/util/strings.c index 3f3d7dc36..b3801d52f 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -43,6 +43,7 @@ char *set_string(char **s, const char *neu) unsigned int hashstring(const char *s) { unsigned int key = 0; + assert(s); while (*s) { key = key * 37 + *s++; }