From cc3c5580d41ea346fd91d4e455ba7cf7932f30e9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 7 Jul 2015 20:23:24 +0200 Subject: [PATCH 1/4] rewrite and unit tests --- res/core/messages.xml | 1 + scripts/tests/e2/destroy.lua | 3 ++ src/CMakeLists.txt | 1 + src/spells.c | 3 ++ src/spells/CMakeLists.txt | 1 + src/spells/buildingcurse.c | 5 +-- src/spells/buildingcurse.h | 3 ++ src/spells/magicresistance.c | 25 ++++++++++++++ src/spells/magicresistance.test.c | 56 +++++++++++++++++++++++++++++++ src/spells/unitcurse.c | 5 --- 10 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 src/spells/magicresistance.c create mode 100644 src/spells/magicresistance.test.c diff --git a/res/core/messages.xml b/res/core/messages.xml index 259e73318..22a8ea841 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -392,6 +392,7 @@ + diff --git a/scripts/tests/e2/destroy.lua b/scripts/tests/e2/destroy.lua index 258146941..a4e84330d 100644 --- a/scripts/tests/e2/destroy.lua +++ b/scripts/tests/e2/destroy.lua @@ -17,6 +17,9 @@ function test_dont_move_after_destroy() u:add_order("NACH O") u:add_order("ZERSTOERE " .. itoa36(u.building.id)) process_orders() + if not u.region then + print("shit happened ", u.number) + end assert_equal(r1, u.region) assert_equal(nil, u.building) end diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71651c1a0..0bbe4f45b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -193,6 +193,7 @@ set(TESTS_SRC spells.test.c spy.test.c upkeep.test.c + spells/magicresistance.test.c ${ATTRIBUTES_TESTS} ${UTIL_TESTS} ${KERNEL_TESTS} diff --git a/src/spells.c b/src/spells.c index b7e1e6868..1d07c2d21 100644 --- a/src/spells.c +++ b/src/spells.c @@ -6767,6 +6767,8 @@ static int sp_readmind(castorder * co) return cast_level; } +void register_magicresistance(void); + void register_spells(void) { register_borders(); @@ -6793,4 +6795,5 @@ void register_spells(void) register_regioncurse(); register_shipcurse(); register_buildingcurse(); + register_magicresistance(); } diff --git a/src/spells/CMakeLists.txt b/src/spells/CMakeLists.txt index 0fbd5c5a0..335189c21 100644 --- a/src/spells/CMakeLists.txt +++ b/src/spells/CMakeLists.txt @@ -7,6 +7,7 @@ combatspells.c regioncurse.c shipcurse.c unitcurse.c +magicresistance.c ) FOREACH(_FILE ${_FILES}) LIST(APPEND _SOURCES ${PROJECT_NAME}/${_FILE}) diff --git a/src/spells/buildingcurse.c b/src/spells/buildingcurse.c index 0f7a17f5b..ae2f941bb 100644 --- a/src/spells/buildingcurse.c +++ b/src/spells/buildingcurse.c @@ -33,14 +33,15 @@ #include #include -static message *cinfo_building(const void *obj, objtype_t typ, const curse * c, +message *cinfo_building(const void *obj, objtype_t typ, const curse * c, int self) { unused_arg(typ); assert(typ == TYP_BUILDING); if (self != 0) { /* owner or inside */ - return msg_message(mkname("curseinfo", c->type->cname), "id", c->no); + building *b = (building *)obj; + return msg_message(mkname("curseinfo", c->type->cname), "id building", c->no, b); } return msg_message(mkname("curseinfo", "buildingunknown"), "id", c->no); } diff --git a/src/spells/buildingcurse.h b/src/spells/buildingcurse.h index d6366c920..46989829a 100644 --- a/src/spells/buildingcurse.h +++ b/src/spells/buildingcurse.h @@ -13,14 +13,17 @@ #ifndef _BCURSE_H #define _BCURSE_H +#include #ifdef __cplusplus extern "C" { #endif struct locale; struct curse; + struct message; extern void register_buildingcurse(void); + struct message *cinfo_building(const void *obj, objtype_t typ, const struct curse * c, int self); #ifdef __cplusplus } diff --git a/src/spells/magicresistance.c b/src/spells/magicresistance.c new file mode 100644 index 000000000..3ec15011c --- /dev/null +++ b/src/spells/magicresistance.c @@ -0,0 +1,25 @@ +#include +#include +#include "unitcurse.h" +#include "buildingcurse.h" +#include + +static struct message *cinfo_magicresistance(const void *obj, objtype_t typ, const struct curse * c, int self) +{ + if (typ == TYP_UNIT) { + return cinfo_unit(obj, typ, c, self); + } + if (typ == TYP_BUILDING) { + return cinfo_building(obj, typ, c, self); + } + return 0; +} + +static struct curse_type ct_magicresistance = { + "magicresistance", CURSETYP_UNIT, CURSE_SPREADMODULO, M_MEN, cinfo_magicresistance +}; + +void register_magicresistance(void) +{ + ct_register(&ct_magicresistance); +} diff --git a/src/spells/magicresistance.test.c b/src/spells/magicresistance.test.c new file mode 100644 index 000000000..bd2d4b69b --- /dev/null +++ b/src/spells/magicresistance.test.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "spells.h" + +#include +#include + +#include +#include +#include +#include + + +static struct castorder *test_create_castorder(castorder *order, unit *u, const char *name, int level, float force, int range) { + struct locale * lang; + spell *sp; + + lang = get_or_create_locale("en"); + sp = create_spell(name, 0); + return order = create_castorder(order, u, NULL, sp, u->region, level, force, range, create_order(K_CAST, lang, ""), NULL); +} + +static void test_magicresistance(CuTest *tc) { + struct region *r; + struct faction *f1, *f2; + unit *u1, *u2; + int level; + curse *c; + + test_cleanup(); + test_create_world(); + r=findregion(0, 0); + f1 = test_create_faction(test_create_race("human")); + u1 = test_create_unit(f1, r); + + f2 = test_create_faction(test_create_race("human")); + u2 = test_create_unit(f2, r); + + test_cleanup(); +} + +CuSuite *get_magicresistance_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_magicresistance); + return suite; +} diff --git a/src/spells/unitcurse.c b/src/spells/unitcurse.c index 80ebe56bb..7e3f5e18e 100644 --- a/src/spells/unitcurse.c +++ b/src/spells/unitcurse.c @@ -301,10 +301,6 @@ static struct curse_type ct_oldrace = { "oldrace", CURSETYP_NORM, CURSE_SPREADALWAYS, NO_MERGE, NULL }; -static struct curse_type ct_magicresistance = { - "magicresistance", CURSETYP_UNIT, CURSE_SPREADMODULO, M_MEN, cinfo_unit -}; - /* ------------------------------------------------------------- */ /* * C_SKILL @@ -365,5 +361,4 @@ void register_unitcurse(void) ct_register(&ct_itemcloak); ct_register(&ct_fumble); ct_register(&ct_oldrace); - ct_register(&ct_magicresistance); } From d7f4fbe05de9ad8bcd7fc470700a465331585d56 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 7 Jul 2015 21:10:18 +0200 Subject: [PATCH 2/4] remove enum constants for curses that are no longer referred to by them (deprecated style). --- src/kernel/curse.c | 8 +------- src/kernel/curse.h | 9 --------- src/spells.c | 6 ------ 3 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/kernel/curse.c b/src/kernel/curse.c index af63681e6..f2e99778f 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -742,13 +742,7 @@ static const char *oldnames[MAXCURSE] = { "speed", "orcish", "magicboost", - "insectfur", - "strength", - "worse", - "magicresistance", - "itemcloak", - "sparkle", - "skillmod" + "insectfur" }; const char *oldcursename(int id) diff --git a/src/kernel/curse.h b/src/kernel/curse.h index 212090281..86d94c935 100644 --- a/src/kernel/curse.h +++ b/src/kernel/curse.h @@ -98,7 +98,6 @@ extern "C" { */ enum { - /* struct's vom typ curse: */ C_FOGTRAP, C_ANTIMAGICZONE, C_FARVISION, @@ -128,18 +127,10 @@ extern "C" { C_RIOT, /*region in Aufruhr */ C_NOCOST, C_CURSED_BY_THE_GODS, - /* struct's vom untertyp curse_unit: */ C_SPEED, /* Beschleunigt */ C_ORC, C_MBOOST, C_KAELTESCHUTZ, - C_STRENGTH, - C_ALLSKILLS, - C_MAGICRESISTANCE, /* 44 - verändert Magieresistenz */ - C_ITEMCLOAK, - C_SPARKLE, - /* struct's vom untertyp curse_skill: */ - C_SKILL, MAXCURSE /* OBS: when removing curses, remember to update read_ccompat() */ }; diff --git a/src/spells.c b/src/spells.c index 1d07c2d21..1723c0fa7 100644 --- a/src/spells.c +++ b/src/spells.c @@ -5011,12 +5011,6 @@ int sp_resist_magic_bonus(castorder * co) u = pa->param[n]->data.u; - /* Ist die Einheit schon verzaubert, wirkt sich dies nur auf die - * Menge der Verzauberten Personen aus. - if (is_cursed(u->attribs, C_MAGICRESISTANCE, 0)) - continue; - */ - m = _min(u->number, victims); victims -= m; From 6abf8f7adb09fc39b60c980e9c0ec93f948eab8e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 8 Jul 2015 05:12:26 +0200 Subject: [PATCH 3/4] bug 1692: rewrite of magicresistance curse-info, especially for buildings (homestome spell). --- res/core/messages.xml | 12 ++++++++- src/kernel/messages.c | 2 -- src/spells/buildingcurse.c | 9 +++---- src/spells/magicresistance.c | 14 +++++++--- src/spells/magicresistance.test.c | 43 ++++++++++++++++++++++++++++--- src/spells/unitcurse.c | 1 + src/test_eressea.c | 1 + 7 files changed, 67 insertions(+), 15 deletions(-) diff --git a/res/core/messages.xml b/res/core/messages.xml index 22a8ea841..4e78fc094 100644 --- a/res/core/messages.xml +++ b/res/core/messages.xml @@ -392,13 +392,20 @@ - "Die natürliche Widerstandskraft gegen Verzauberung ist gestärkt. ($int36($id))" "The magical resistance has been strengthened. ($int36($id))" + + + + + + "Die natürliche Widerstandskraft gegen Verzauberung ist gestärkt. ($int36($id))" + "The magical resistance has been strengthened. ($int36($id))" + @@ -415,6 +422,7 @@ + "Diese Mauern wirken, als wären sie direkt aus der Erde gewachsen und nicht erbaut. ($int36($id))" @@ -422,6 +430,7 @@ + "Ein magischer Schimmer liegt auf diesen Mauern. ($int36($id))" @@ -542,6 +551,7 @@ + "Der Zahn der Zeit kann diesen Mauern nichts anhaben. ($int36($id))" "Time cannot touch these walls. ($int36($id))" diff --git a/src/kernel/messages.c b/src/kernel/messages.c index af0e4170b..cd5719c42 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -188,7 +188,6 @@ message *msg_message(const char *name, const char *sig, ...) } else { log_error("invalid parameter %s for message type %s\n", paramname, mtype->name); - assert(!"program aborted."); } while (*ic && !isalnum(*ic)) ic++; @@ -196,7 +195,6 @@ message *msg_message(const char *name, const char *sig, ...) va_end(vargs); if (argnum != mtype->nparameters) { log_error("not enough parameters for message type %s\n", mtype->name); - assert(!"program aborted."); } return msg_create(mtype, args); diff --git a/src/spells/buildingcurse.c b/src/spells/buildingcurse.c index ae2f941bb..d3b5ddf36 100644 --- a/src/spells/buildingcurse.c +++ b/src/spells/buildingcurse.c @@ -36,14 +36,13 @@ message *cinfo_building(const void *obj, objtype_t typ, const curse * c, int self) { + const building *b = (const building *)obj; unused_arg(typ); assert(typ == TYP_BUILDING); + assert(obj); + assert(c); - if (self != 0) { /* owner or inside */ - building *b = (building *)obj; - return msg_message(mkname("curseinfo", c->type->cname), "id building", c->no, b); - } - return msg_message(mkname("curseinfo", "buildingunknown"), "id", c->no); + return msg_message(mkname("curseinfo", self ? c->type->cname : "buildingunknown"), "id building", c->no, b); } /* CurseInfo mit Spezialabfragen */ diff --git a/src/spells/magicresistance.c b/src/spells/magicresistance.c index 3ec15011c..0a4ffaa9f 100644 --- a/src/spells/magicresistance.c +++ b/src/spells/magicresistance.c @@ -1,16 +1,22 @@ #include #include -#include "unitcurse.h" -#include "buildingcurse.h" #include +#include +#include static struct message *cinfo_magicresistance(const void *obj, objtype_t typ, const struct curse * c, int self) { if (typ == TYP_UNIT) { - return cinfo_unit(obj, typ, c, self); + if (self != 0) { + const struct unit *u = (const struct unit *)obj; + return msg_message(mkname("curseinfo", c->type->cname), "unit id", u, + c->no); + } + return NULL; } if (typ == TYP_BUILDING) { - return cinfo_building(obj, typ, c, self); + const struct building *b = (const struct building *)obj; + return msg_message(mkname("curseinfo", self ? "homestone" : "buildingunknown"), "id building", c->no, b); } return 0; } diff --git a/src/spells/magicresistance.test.c b/src/spells/magicresistance.test.c index bd2d4b69b..1a63fb737 100644 --- a/src/spells/magicresistance.test.c +++ b/src/spells/magicresistance.test.c @@ -1,11 +1,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -29,11 +31,11 @@ static struct castorder *test_create_castorder(castorder *order, unit *u, const return order = create_castorder(order, u, NULL, sp, u->region, level, force, range, create_order(K_CAST, lang, ""), NULL); } -static void test_magicresistance(CuTest *tc) { +static void test_magicresistance_unit(CuTest *tc) { struct region *r; struct faction *f1, *f2; unit *u1, *u2; - int level; + message *msg; curse *c; test_cleanup(); @@ -45,12 +47,47 @@ static void test_magicresistance(CuTest *tc) { f2 = test_create_faction(test_create_race("human")); u2 = test_create_unit(f2, r); + c = create_curse(u1, &u2->attribs, ct_find("magicresistance"), 10, 20, 30, u2->number); + CuAssertPtrNotNull(tc, u2->attribs); + CuAssertPtrEquals(tc, (void *)&at_curse, (void *)u2->attribs->type); + msg = c->type->curseinfo(u2, TYP_UNIT, c, 1); + CuAssertPtrNotNull(tc, msg); + CuAssertStrEquals(tc, "curseinfo::magicresistance", test_get_messagetype(msg)); + + test_cleanup(); +} + +static void test_magicresistance_building(CuTest *tc) { + struct region *r; + struct faction *f1, *f2; + unit *u1; + building *b1; + message *msg; + curse *c; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + f1 = test_create_faction(test_create_race("human")); + u1 = test_create_unit(f1, r); + + f2 = test_create_faction(test_create_race("human")); + b1 = test_create_building(r, test_create_buildingtype("castle")); + + c = create_curse(u1, &b1->attribs, ct_find("magicresistance"), 10, 20, 30, 0); + CuAssertPtrNotNull(tc, b1->attribs); + CuAssertPtrEquals(tc, (void *)&at_curse, (void *)b1->attribs->type); + msg = c->type->curseinfo(b1, TYP_BUILDING, c, 1); + CuAssertPtrNotNull(tc, msg); + CuAssertStrEquals(tc, "curseinfo::homestone", test_get_messagetype(msg)); + test_cleanup(); } CuSuite *get_magicresistance_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, test_magicresistance); + SUITE_ADD_TEST(suite, test_magicresistance_unit); + SUITE_ADD_TEST(suite, test_magicresistance_building); return suite; } diff --git a/src/spells/unitcurse.c b/src/spells/unitcurse.c index 7e3f5e18e..73925a55a 100644 --- a/src/spells/unitcurse.c +++ b/src/spells/unitcurse.c @@ -159,6 +159,7 @@ message *cinfo_unit(const void *obj, objtype_t typ, const curse * c, int self) { unused_arg(typ); assert(typ == TYP_UNIT); + assert(obj); if (self != 0) { unit *u = (unit *)obj; diff --git a/src/test_eressea.c b/src/test_eressea.c index baa70ae2c..b5a2e2a42 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -74,6 +74,7 @@ int RunAllTests(void) RUN_TESTS(suite, building); RUN_TESTS(suite, spell); RUN_TESTS(suite, spells); + RUN_TESTS(suite, magicresistance); RUN_TESTS(suite, ally); RUN_TESTS(suite, messages); /* gamecode */ From efe2185b13e63faa9c9c5edf8ef440caebf34153 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 8 Jul 2015 05:17:10 +0200 Subject: [PATCH 4/4] fix build, eliminate unused variables/functions. --- src/spells/magicresistance.test.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/spells/magicresistance.test.c b/src/spells/magicresistance.test.c index 1a63fb737..72269718c 100644 --- a/src/spells/magicresistance.test.c +++ b/src/spells/magicresistance.test.c @@ -22,15 +22,6 @@ #include -static struct castorder *test_create_castorder(castorder *order, unit *u, const char *name, int level, float force, int range) { - struct locale * lang; - spell *sp; - - lang = get_or_create_locale("en"); - sp = create_spell(name, 0); - return order = create_castorder(order, u, NULL, sp, u->region, level, force, range, create_order(K_CAST, lang, ""), NULL); -} - static void test_magicresistance_unit(CuTest *tc) { struct region *r; struct faction *f1, *f2; @@ -59,7 +50,7 @@ static void test_magicresistance_unit(CuTest *tc) { static void test_magicresistance_building(CuTest *tc) { struct region *r; - struct faction *f1, *f2; + struct faction *f1; unit *u1; building *b1; message *msg; @@ -71,7 +62,6 @@ static void test_magicresistance_building(CuTest *tc) { f1 = test_create_faction(test_create_race("human")); u1 = test_create_unit(f1, r); - f2 = test_create_faction(test_create_race("human")); b1 = test_create_building(r, test_create_buildingtype("castle")); c = create_curse(u1, &b1->attribs, ct_find("magicresistance"), 10, 20, 30, 0);