make curse_active check for a magician.

fix tests that were using curses without a creator.
This commit is contained in:
Enno Rehling 2021-03-23 19:27:11 +01:00
parent 5e7fba50d6
commit a9a7609a9a
7 changed files with 51 additions and 33 deletions

View File

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

View File

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

View File

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

View File

@ -24,14 +24,14 @@
#include <CuTest.h>
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;
}

View File

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

View File

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

View File

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