From a9a7609a9aece6cc8f4d7e88d1ccb45a479bbf68 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 23 Mar 2021 19:27:11 +0100 Subject: [PATCH] make curse_active check for a magician. fix tests that were using curses without a creator. --- scripts/tests/e2/spells.lua | 15 +++------------ src/battle.test.c | 4 ++-- src/kernel/curse.c | 4 ++-- src/kernel/curse.test.c | 38 ++++++++++++++++++++++++++++++++----- src/kernel/ship.test.c | 12 ++++++------ src/kernel/unit.c | 3 +-- src/spells.test.c | 8 ++++---- 7 files changed, 51 insertions(+), 33 deletions(-) diff --git a/scripts/tests/e2/spells.lua b/scripts/tests/e2/spells.lua index 6f43acaca..bb338c5b7 100644 --- a/scripts/tests/e2/spells.lua +++ b/scripts/tests/e2/spells.lua @@ -355,20 +355,11 @@ function test_dream_magician_dies() process_orders() - u2:clear_orders() - -- how to kill a mage ... - u3:add_order("ATTACKIERE " .. itoa36(u2.id)) - u1:add_order("KÄMPFE NICHT") - u2:add_order("KÄMPFE AGGRESSIV") - - init_reports() - write_reports() - assert_equal(2, u1:eff_skill("melee")) + u2.number = 0 + assert_equal(1, u1:eff_skill("melee")) process_orders() -- u2 is dead - - -- in a perfect world, this would be a test that the curse has no effect. However, with rng == 0, the duration of the dream curse is only 1 week, so it would have faded anyway. But we should at least not crash. - assert_equal(0, u2.number) + assert_nil(get_unit(u2.id)) assert_equal(1, u1:eff_skill("melee")) end diff --git a/src/battle.test.c b/src/battle.test.c index 10bcae721..9a4514ec1 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -769,10 +769,10 @@ static void test_battle_skilldiff_building(CuTest *tc) CuAssertIntEquals(tc, 1, building_protection(ud->building)); CuAssertIntEquals(tc, -1, skilldiff(ta, td, 0)); - create_curse(NULL, &ud->building->attribs, &ct_magicwalls, 1, 1, 1, 1); + create_curse(ud, &ud->building->attribs, &ct_magicwalls, 1, 1, 1, 1); CuAssertIntEquals(tc, -2, skilldiff(ta, td, 0)); - create_curse(NULL, &ud->building->attribs, &ct_strongwall, 1, 1, 2, 1); + create_curse(ud, &ud->building->attribs, &ct_strongwall, 1, 1, 2, 1); CuAssertIntEquals(tc, -4, skilldiff(ta, td, 0)); free_battle(b); diff --git a/src/kernel/curse.c b/src/kernel/curse.c index 8e8df36ac..10c72b5cf 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -529,7 +529,7 @@ curse *create_curse(unit * magician, attrib ** ap, const curse_type * ct, /* die Kraft eines Spruchs darf nicht 0 sein */ assert(vigour > 0); - c = get_curse(*ap, ct); + c = ap ? get_curse(*ap, ct) : NULL; if (c && (c_flags(c) & CURSE_ONLYONE)) { return NULL; @@ -652,7 +652,7 @@ bool curse_active(const curse * c) if (c->vigour <= 0) return false; - return true; + return c->magician && c->magician->region && (c->magician->number > 0); } bool is_cursed_with(const attrib * ap, const curse * c) diff --git a/src/kernel/curse.test.c b/src/kernel/curse.test.c index aff354407..f13e646eb 100644 --- a/src/kernel/curse.test.c +++ b/src/kernel/curse.test.c @@ -24,14 +24,14 @@ #include +static const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; + static void test_curse(CuTest * tc) { attrib *attrs = NULL; curse *c, *result; int cid; - curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; - test_setup(); c = create_curse(NULL, &attrs, &ct_dummy, 1.0, 1, 1, 1); cid = c->no; @@ -162,7 +162,6 @@ static void test_write_flag(CuTest *tc) { } static void test_curse_ids(CuTest *tc) { - const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; curse *c1, *c2; attrib *a1 = 0, *a2 = 0; @@ -178,12 +177,11 @@ static void test_curse_ids(CuTest *tc) { } static void test_curse_flags(CuTest *tc) { - const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL }; curse *c1, *c2; unit *u; test_setup(); - u = test_create_unit(test_create_faction(), test_create_region(0, 0, NULL)); + u = test_create_unit(test_create_faction(), test_create_plain(0, 0)); c1 = create_curse(u, &u->attribs, &ct_dummy, 1, 1, 1, 0); CuAssertPtrEquals(tc, u, c1->magician); CuAssertIntEquals(tc, 1, (int)c1->effect); @@ -198,6 +196,35 @@ static void test_curse_flags(CuTest *tc) { test_teardown(); } +static void test_curse_active(CuTest *tc) { + curse *c; + unit *u; + + test_setup(); + + u = test_create_unit(test_create_faction(), test_create_plain(0, 0)); + CuAssertTrue(tc, !curse_active(NULL)); + c = create_curse(u, &u->attribs, &ct_dummy, 1.0, 1, 1, 0); + CuAssertIntEquals(tc, 0, c->mask); + CuAssertTrue(tc, curse_active(c)); + c->mask = CURSE_ISNEW; + CuAssertTrue(tc, !curse_active(c)); + c->mask = 0; + + u->number = 0; + CuAssertTrue(tc, !curse_active(c)); + u->number = 1; + + c->magician = NULL; + CuAssertTrue(tc, !curse_active(c)); + c->magician = u; + + u->region = NULL; + CuAssertTrue(tc, !curse_active(c)); + + test_teardown(); +} + CuSuite *get_curse_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -210,5 +237,6 @@ CuSuite *get_curse_suite(void) SUITE_ADD_TEST(suite, test_write_flag); SUITE_ADD_TEST(suite, test_curse_flags); SUITE_ADD_TEST(suite, test_curse_ids); + SUITE_ADD_TEST(suite, test_curse_active); return suite; } diff --git a/src/kernel/ship.test.c b/src/kernel/ship.test.c index 25c573d7f..c8a7d91cb 100644 --- a/src/kernel/ship.test.c +++ b/src/kernel/ship.test.c @@ -469,7 +469,7 @@ static void test_shipspeed_stormwind(CuTest *tc) { register_shipcurse(); assert(sh && cap && crew); - create_curse(0, &sh->attribs, &ct_stormwind, 1, 1, 1, 0); + create_curse(cap, &sh->attribs, &ct_stormwind, 1, 1, 1, 0); CuAssertPtrNotNull(tc, sh->attribs); CuAssertIntEquals_Msg(tc, "stormwind doubles ship range", sh->type->range * 2, shipspeed(sh, cap)); a_age(&sh->attribs, sh); @@ -487,7 +487,7 @@ static void test_shipspeed_nodrift(CuTest *tc) { register_shipcurse(); assert(sh && cap && crew); - create_curse(0, &sh->attribs, &ct_nodrift, 1, 1, 1, 0); + create_curse(cap, &sh->attribs, &ct_nodrift, 1, 1, 1, 0); CuAssertIntEquals_Msg(tc, "nodrift adds +1 to range", sh->type->range + 1, shipspeed(sh, cap)); test_teardown(); } @@ -502,7 +502,7 @@ static void test_shipspeed_shipspeedup(CuTest *tc) { register_shipcurse(); assert(sh && cap && crew); - create_curse(0, &sh->attribs, &ct_shipspeedup, 1, 1, 3, 0); + create_curse(cap, &sh->attribs, &ct_shipspeedup, 1, 1, 3, 0); CuAssertIntEquals_Msg(tc, "shipspeedup adds effect to range", sh->type->range + 3, shipspeed(sh, cap)); test_teardown(); } @@ -572,15 +572,15 @@ static void test_maximum_shipspeed(CuTest *tc) { f = test_create_faction_ex(rc, NULL); setup_crew(sh, f, &cap, &crew); CuAssertIntEquals(tc, sh->type->range + 1, shipspeed(sh, cap)); - create_curse(0, &sh->attribs, &ct_stormwind, 1, 1, 1, 0); + create_curse(cap, &sh->attribs, &ct_stormwind, 1, 1, 1, 0); CuAssertIntEquals(tc, 2 * sh->type->range + 1, shipspeed(sh, cap)); - create_curse(0, &sh->attribs, &ct_nodrift, 1, 1, 1, 0); + create_curse(cap, &sh->attribs, &ct_nodrift, 1, 1, 1, 0); CuAssertIntEquals(tc, 2 * sh->type->range + 2, shipspeed(sh, cap)); a = a_new(&at_speedup); a->data.i = 3; a_add(&sh->attribs, a); CuAssertIntEquals(tc, 2 * sh->type->range + 5, shipspeed(sh, cap)); - create_curse(0, &sh->attribs, &ct_shipspeedup, 1, 1, 4, 0); + create_curse(cap, &sh->attribs, &ct_shipspeedup, 1, 1, 4, 0); CuAssertIntEquals(tc, 2 * sh->type->range + 9, shipspeed(sh, cap)); } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 1c5639701..433bf0dd1 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1077,8 +1077,7 @@ static int att_modification(const unit * u, skill_t sk) attrib *a = a_find(u->region->attribs, &at_curse); while (a && a->type == &at_curse) { curse *c = (curse *)a->data.v; - if (c->magician && curse_active(c) && c->type == &ct_gbdream && - c->magician->number > 0 && c->magician->region) { + if (c->type == &ct_gbdream && curse_active(c)) { int effect = curse_geteffect_int(c); bool allied = alliedunit(c->magician, u->faction, HELP_GUARD); if (allied) { diff --git a/src/spells.test.c b/src/spells.test.c index 1fb75ecd5..af107cee0 100644 --- a/src/spells.test.c +++ b/src/spells.test.c @@ -168,7 +168,7 @@ static void test_view_reality(CuTest *tc) { CuAssertIntEquals(tc, -1, get_observer(r, f)); /* target region r exists, but astral space is blocked */ - c = create_curse(NULL, &ra->attribs, &ct_astralblock, 50.0, 1, 50, 0); + c = create_curse(u, &ra->attribs, &ct_astralblock, 50.0, 1, 50, 0); test_create_castorder(&co, u, 10, 10.0, 0, NULL); CuAssertIntEquals(tc, 0, sp_viewreality(&co)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error216")); @@ -177,7 +177,7 @@ static void test_view_reality(CuTest *tc) { remove_curse(&ra->attribs, c); /* target region r exists, but astral interference is blocked */ - c = create_curse(NULL, &r->attribs, &ct_astralblock, 50.0, 1, 50, 0); + c = create_curse(u, &r->attribs, &ct_astralblock, 50.0, 1, 50, 0); test_create_castorder(&co, u, 10, 10.0, 0, NULL); CuAssertIntEquals(tc, 0, sp_viewreality(&co)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error216")); @@ -246,7 +246,7 @@ static void test_show_astral(CuTest *tc) { free_castorder(&co); /* astral block on r */ - c = create_curse(NULL, &r->attribs, &ct_astralblock, 50.0, 1, 50, 0); + c = create_curse(u, &r->attribs, &ct_astralblock, 50.0, 1, 50, 0); test_create_castorder(&co, u, 9, 10.0, 0, NULL); CuAssertIntEquals(tc, 0, sp_showastral(&co)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error216")); @@ -254,7 +254,7 @@ static void test_show_astral(CuTest *tc) { remove_curse(&r->attribs, c); /* astral block on rx */ - c = create_curse(NULL, &rx->attribs, &ct_astralblock, 50.0, 1, 50, 0); + c = create_curse(u, &rx->attribs, &ct_astralblock, 50.0, 1, 50, 0); test_create_castorder(&co, u, 9, 10.0, 0, NULL); CuAssertIntEquals(tc, 0, sp_showastral(&co)); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "error220"));