Skip to content

Commit

Permalink
bug 2043: magicanalyse_building
Browse files Browse the repository at this point in the history
fix: wrong error message for permanent spells
fix: not found message even when curse was found
  • Loading branch information
ennorehling committed Nov 2, 2024
1 parent f1f33ae commit a20f294
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/spells.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,24 @@ static void magicanalyse_building(building * b, unit * mage, double force)
for (a = b->attribs; a; a = a->next) {
curse *c;
double probability;
int mon;

if (a->type != &at_curse)
continue;

found = true;
c = (curse *)a->data.v;
/* ist der curse schwaecher als der Analysezauber, so ergibt sich
* mehr als 100% probability und damit immer ein Erfolg. */
probability = curse_chance(c, force);
mon = c->duration + (rng_int() % 10) - 5;
if (mon < 1) mon = 1;

if (chance(probability)) { /* Analyse geglueckt */
if (c_flags(c) & CURSE_NOAGE) {
ADDMSG(&mage->faction->msgs, msg_message("analyse_building_age",
ADDMSG(&mage->faction->msgs, msg_message("analyse_building_noage",
"mage building curse", mage, b, c->type));
}
else {
int mon = c->duration + (rng_int() % 10) - 5;
if (mon < 1) mon = 1;
ADDMSG(&mage->faction->msgs, msg_message("analyse_building_age",
"mage building curse months", mage, b, c->type, mon));
}
Expand Down
58 changes: 57 additions & 1 deletion src/spells.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <spells/regioncurse.h>
#include <spells/unitcurse.h>
#include <spells/shipcurse.h>
#include <spells/buildingcurse.h>
#include <spells/flyingship.h>
#include <attributes/attributes.h>
#include <attributes/otherfaction.h>
Expand Down Expand Up @@ -1945,6 +1946,61 @@ static void test_analysemagic_ship(CuTest *tc)
test_teardown();
}

static void test_analysemagic_building(CuTest *tc)
{
struct region *r;
struct faction *f;
unit *u;
castorder co;
spellparameter param, *args = NULL;
message *m;
curse *c, *c2;

test_setup();
random_source_inject_constants(0.0, 0);
mt_create_va(mt_new("analyse_building_age", NULL), "mage:unit", "building:building", "curse:curse", "months:int", MT_NEW_END);
mt_create_va(mt_new("analyse_building_noage", NULL), "mage:unit", "building:building", "curse:curse", MT_NEW_END);
r = test_create_plain(0, 0);
f = test_create_faction();
f->magiegebiet = M_DRAIG;
u = test_create_unit(f, r);
u->building = test_create_building(r, NULL);

param.flag = TARGET_OK;
param.typ = SPP_BUILDING;
param.data.b = u->building;
arrput(args, param);

test_create_castorder(&co, u, 3, 4., 0, args);
CuAssertIntEquals(tc, co.level, sp_analysemagic(&co));
CuAssertIntEquals(tc, 1, test_count_messagetype(f->msgs, "analyse_building_nospell"));
CuAssertIntEquals(tc, 0, test_count_messagetype(f->msgs, "analyse_building_age"));
test_clear_messages(f);

c = create_curse(u, &u->building->attribs, &ct_strongwall, 5.0, 20, 1, 0);
CuAssertIntEquals(tc, co.level, sp_analysemagic(&co));
CuAssertIntEquals(tc, 0, test_count_messagetype(f->msgs, "analyse_building_nospell"));
/* curse is too strong, analysis fails */
CuAssertIntEquals(tc, 1, test_count_messagetype(f->msgs, "analyse_building_fail"));
test_clear_messages(f);

c->vigour = co.force;
c2 = create_curse(u, &u->building->attribs, &ct_magicwalls, co.force, 20, 1, 0);
CuAssertIntEquals(tc, co.level, sp_analysemagic(&co));
CuAssertIntEquals(tc, 0, test_count_messagetype(f->msgs, "analyse_building_nospell"));
CuAssertPtrNotNull(tc, m = test_find_faction_message(f, "analyse_building_age"));
CuAssertPtrEquals(tc, u, m->parameters[0].v);
CuAssertPtrEquals(tc, u->building, m->parameters[1].v);
CuAssertPtrEquals(tc, (void *)c->type, m->parameters[2].v);
CuAssertIntEquals(tc, 15, m->parameters[3].i);
CuAssertPtrNotNull(tc, m = test_find_faction_message(f, "analyse_building_noage"));
CuAssertPtrEquals(tc, u, m->parameters[0].v);
CuAssertPtrEquals(tc, u->building, m->parameters[1].v);
CuAssertPtrEquals(tc, (void *)c2->type, m->parameters[2].v);

test_teardown();
}

CuSuite *get_spells_suite(void)
{
CuSuite *suite = CuSuiteNew();
Expand Down Expand Up @@ -1995,7 +2051,7 @@ CuSuite *get_spells_suite(void)
SUITE_ADD_TEST(suite, test_analysemagic_unit);
//SUITE_ADD_TEST(suite, test_analysemagic_temp);
SUITE_ADD_TEST(suite, test_analysemagic_ship);
//SUITE_ADD_TEST(suite, test_analysemagic_building);
SUITE_ADD_TEST(suite, test_analysemagic_building);
//SUITE_ADD_TEST(suite, test_analysemagic_region);

return suite;
Expand Down

0 comments on commit a20f294

Please sign in to comment.