add unit-test for casting spells, fix spell-casting (was looking for thisorder), all E3 tests pass again

This commit is contained in:
Enno Rehling 2015-08-05 10:25:25 +02:00
parent c22636ca13
commit 420574c7e4
6 changed files with 67 additions and 16 deletions

View File

@ -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")

View File

@ -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()

View File

@ -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);

View File

@ -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,7 +2768,6 @@ 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);
@ -2781,7 +2780,6 @@ void magic(void)
}
}
}
}
/* Da sich die Aura und Komponenten in der Zwischenzeit verändert
* haben können und sich durch vorherige Sprüche das Zaubern

View File

@ -3,6 +3,7 @@
#include "magic.h"
#include <kernel/faction.h>
#include <kernel/order.h>
#include <kernel/item.h>
#include <kernel/region.h>
#include <kernel/spell.h>
@ -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);

View File

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