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++; }