From f594a908dfda416a7e304009ac6a789f71d0ad6d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 6 Oct 2017 21:53:07 +0200 Subject: [PATCH] deprecate old get_mage implementation. fix test_familiar_equip with new get_mage. --- src/bind_unit.c | 6 +++--- src/creport.c | 2 +- src/give.c | 2 +- src/kernel/equipment.c | 2 +- src/kernel/equipment.test.c | 2 +- src/kernel/save.c | 2 +- src/kernel/unit.c | 4 ++-- src/laws.c | 4 ++-- src/magic.c | 37 +++++++++++++++++++++++-------------- src/magic.h | 1 + src/magic.test.c | 2 +- src/move.c | 2 +- src/reports.c | 5 +++-- src/spells.c | 14 +++++++------- src/spy.c | 2 +- src/study.c | 6 +++--- src/study.test.c | 6 +++--- 17 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/bind_unit.c b/src/bind_unit.c index d95d9d930..1e1aee00e 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -273,7 +273,7 @@ static int tolua_unit_set_guard(lua_State * L) static const char *unit_getmagic(const unit * u) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); return mage ? magic_school[mage->magietyp] : NULL; } @@ -286,7 +286,7 @@ static int tolua_unit_get_magic(lua_State * L) static void unit_setmagic(unit * u, const char *type) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); int mtype; for (mtype = 0; mtype != MAXMAGIETYP; ++mtype) { if (strcmp(magic_school[mtype], type) == 0) @@ -753,7 +753,7 @@ static int tolua_unit_get_items(lua_State * L) static int tolua_unit_get_spells(lua_State * L) { unit *self = (unit *) tolua_tousertype(L, 1, 0); - sc_mage *mage = self ? get_mage(self) : 0; + sc_mage *mage = self ? get_mage_depr(self) : 0; spellbook *sb = mage ? mage->spellbook : 0; selist *slist = 0; if (sb) { diff --git a/src/creport.c b/src/creport.c index b68f3e4fe..4ec7f9b0b 100644 --- a/src/creport.c +++ b/src/creport.c @@ -954,7 +954,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, } /* spells that this unit can cast */ - mage = get_mage(u); + mage = get_mage_depr(u); if (mage) { int i, maxlevel = effskill(u, SK_MAGIC, 0); cr_output_spells(out, u, maxlevel); diff --git a/src/give.c b/src/give.c index 18d355031..da75e56c5 100644 --- a/src/give.c +++ b/src/give.c @@ -591,7 +591,7 @@ void give_unit(unit * u, unit * u2, order * ord) cmistake(u, ord, 155, MSG_COMMERCE); return; } - mage = get_mage(u); + mage = get_mage_depr(u); if (!mage || u2->faction->magiegebiet != mage->magietyp) { cmistake(u, ord, 157, MSG_COMMERCE); return; diff --git a/src/kernel/equipment.c b/src/kernel/equipment.c index b87f401dd..f24d4acfd 100644 --- a/src/kernel/equipment.c +++ b/src/kernel/equipment.c @@ -121,7 +121,7 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask) if (eq->spells) { selist * ql = eq->spells; int qi; - sc_mage * mage = get_mage(u); + sc_mage * mage = get_mage_depr(u); for (qi = 0; ql; selist_advance(&ql, &qi, 1)) { lazy_spell *sbe = (lazy_spell *)selist_get(ql, qi); diff --git a/src/kernel/equipment.test.c b/src/kernel/equipment.test.c index 7952001fe..0e0ebf196 100644 --- a/src/kernel/equipment.test.c +++ b/src/kernel/equipment.test.c @@ -39,7 +39,7 @@ static void test_equipment(CuTest * tc) CuAssertIntEquals(tc, 1, i_get(u->items, it_horses)); CuAssertIntEquals(tc, 5, get_level(u, SK_MAGIC)); - mage = get_mage(u); + mage = get_mage_depr(u); CuAssertPtrNotNull(tc, mage); CuAssertPtrNotNull(tc, mage->spellbook); CuAssertTrue(tc, u_hasspell(u, sp)); diff --git a/src/kernel/save.c b/src/kernel/save.c index ed64b7bfd..23d6f3e71 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1791,7 +1791,7 @@ int read_game(gamedata *data) else { for (u = f->units; u; u = u->nextF) { if (data->version < SPELL_LEVEL_VERSION) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); if (mage) { faction *f = u->faction; int skl = effskill(u, SK_MAGIC, 0); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index a1ad74038..674bdad08 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1814,7 +1814,7 @@ void u_setrace(struct unit *u, const struct race *rc) void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level) { - sc_mage *mage = m ? m : get_mage(u); + sc_mage *mage = m ? m : get_mage_depr(u); if (!mage) { log_debug("adding new spell %s to a previously non-mage unit %s\n", sp->sname, unitname(u)); @@ -1828,7 +1828,7 @@ void unit_add_spell(unit * u, sc_mage * m, struct spell * sp, int level) struct spellbook * unit_get_spellbook(const struct unit * u) { - sc_mage * mage = get_mage(u); + sc_mage * mage = get_mage_depr(u); if (mage) { if (mage->spellbook) { return mage->spellbook; diff --git a/src/laws.c b/src/laws.c index e4c861177..f60af7f15 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3387,7 +3387,7 @@ static int faction_getmages(faction * f, unit ** results, int numresults) for (u = f->units; u; u = u->nextF) { if (u->number > 0) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); if (mage) { int level = effskill(u, SK_MAGIC, 0); if (level > maxlevel) { @@ -3446,7 +3446,7 @@ static void update_spells(void) show_new_spells(f, maxlevel, faction_get_spellbook(f)); for (i = 0; i != MAXMAGES && mages[i]; ++i) { unit * u = mages[i]; - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); if (mage && mage->spellbook) { int level = effskill(u, SK_MAGIC, 0); show_new_spells(f, level, mage->spellbook); diff --git a/src/magic.c b/src/magic.c index 21db1e672..7ec220875 100644 --- a/src/magic.c +++ b/src/magic.c @@ -323,10 +323,19 @@ attrib_type at_mage = { bool is_mage(const unit * u) { - return get_mage(u) != NULL; + return get_mage_depr(u) != NULL; } sc_mage *get_mage(const unit * u) +{ + attrib *a = a_find(u->attribs, &at_mage); + if (a) { + return (sc_mage *)a->data.v; + } + return NULL; +} + +sc_mage *get_mage_depr(const unit * u) { if (has_skill(u, SK_MAGIC)) { attrib *a = a_find(u->attribs, &at_mage); @@ -334,7 +343,7 @@ sc_mage *get_mage(const unit * u) return (sc_mage *)a->data.v; } } - return (sc_mage *)NULL; + return NULL; } /* ------------------------------------------------------------- */ @@ -507,7 +516,7 @@ int u_hasspell(const unit *u, const struct spell *sp) int get_combatspelllevel(const unit * u, int nr) { - sc_mage *m = get_mage(u); + sc_mage *m = get_mage_depr(u); assert(nr < MAXCOMBATSPELLS); if (m) { @@ -525,7 +534,7 @@ const spell *get_combatspell(const unit * u, int nr) sc_mage *m; assert(nr < MAXCOMBATSPELLS); - m = get_mage(u); + m = get_mage_depr(u); if (m) { return m->combatspells[nr].sp; } @@ -534,7 +543,7 @@ const spell *get_combatspell(const unit * u, int nr) void set_combatspell(unit * u, spell * sp, struct order *ord, int level) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); int i = -1; assert(mage || !"trying to set a combat spell for non-mage"); @@ -574,7 +583,7 @@ void unset_combatspell(unit * u, spell * sp) int nr = 0; int i; - m = get_mage(u); + m = get_mage_depr(u); if (!m) return; @@ -610,7 +619,7 @@ int get_spellpoints(const unit * u) { sc_mage *m; - m = get_mage(u); + m = get_mage_depr(u); if (!m) return 0; @@ -621,7 +630,7 @@ void set_spellpoints(unit * u, int sp) { sc_mage *m; - m = get_mage(u); + m = get_mage_depr(u); if (!m) return; @@ -638,7 +647,7 @@ int change_spellpoints(unit * u, int mp) sc_mage *m; int sp; - m = get_mage(u); + m = get_mage_depr(u); if (!m) { return 0; } @@ -657,7 +666,7 @@ static int get_spchange(const unit * u) { sc_mage *m; - m = get_mage(u); + m = get_mage_depr(u); if (!m) return 0; @@ -711,7 +720,7 @@ int change_maxspellpoints(unit * u, int csp) { sc_mage *m; - m = get_mage(u); + m = get_mage_depr(u); if (!m) { return 0; } @@ -729,7 +738,7 @@ int countspells(unit * u, int step) sc_mage *m; int count; - m = get_mage(u); + m = get_mage_depr(u); if (!m) return 0; @@ -1313,7 +1322,7 @@ bool fumble(region * r, unit * u, const spell * sp, int cast_grade) } /* CHAOSPATZERCHANCE 10 : +10% Chance zu Patzern */ - mage = get_mage(u); + mage = get_mage_depr(u); if (mage->magietyp == M_DRAIG) { fumble_chance += CHAOSPATZERCHANCE; } @@ -2238,7 +2247,7 @@ void create_newfamiliar(unit * mage, unit * fam) snprintf(eqname, sizeof(eqname), "fam_%s", rc->_name); eq = get_equipment(eqname); if (eq != NULL) { - equip_items(&fam->items, eq); + equip_unit(fam, eq); } else { log_info("could not perform initialization for familiar %s.\n", rc->_name); diff --git a/src/magic.h b/src/magic.h index 329a0012a..403dafdc6 100644 --- a/src/magic.h +++ b/src/magic.h @@ -221,6 +221,7 @@ extern "C" { /* macht die struct unit zu einem neuen Magier: legt die struct u->mage an * und initialisiert den Magiertypus mit mtyp. */ sc_mage *get_mage(const struct unit *u); + sc_mage *get_mage_depr(const struct unit *u); /* gibt u->mage zurück, bei nicht-Magiern *NULL */ bool is_mage(const struct unit *u); /* gibt true, wenn u->mage gesetzt. */ diff --git a/src/magic.test.c b/src/magic.test.c index b069dd165..eea9ec575 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -549,7 +549,7 @@ static void test_familiar_equip(CuTest *tc) { CuAssertPtrNotNull(tc, mage); CuAssertPtrNotNull(tc, mage->spellbook); set_level(u, SK_MAGIC, 1); - CuAssertPtrEquals(tc, mage, get_mage(u)); + CuAssertPtrEquals(tc, mage, get_mage_depr(u)); CuAssertTrue(tc, u_hasspell(u, sp)); test_cleanup(); diff --git a/src/move.c b/src/move.c index 33ed38126..2b0e4d8d7 100644 --- a/src/move.c +++ b/src/move.c @@ -1451,7 +1451,7 @@ static int movement_speed(unit * u) * Nicht kumulativ mit anderen Beschleunigungen! */ if (mp * dk <= BP_WALKING * u_race(u)->speed && is_astral(u->region) && is_mage(u)) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); if (mage->magietyp == M_TYBIED || mage->magietyp == M_ILLAUN) { mp *= 2; } diff --git a/src/reports.c b/src/reports.c index 1182f89c9..3d762a4ce 100644 --- a/src/reports.c +++ b/src/reports.c @@ -819,8 +819,9 @@ spskill(char *buffer, size_t size, const struct locale * lang, bufp = STRLCPY(bufp, " ", size); if (sv->id == SK_MAGIC) { - sc_mage *mage = get_mage(u); - if (mage && mage->magietyp != M_GRAY) { + sc_mage *mage = get_mage_depr(u); + assert(mage); + if (mage->magietyp != M_GRAY) { bufp = STRLCPY(bufp, LOC(lang, mkname("school", magic_school[mage->magietyp])), size); bufp = STRLCPY(bufp, " ", size); diff --git a/src/spells.c b/src/spells.c index 0e9f5cffd..526761169 100644 --- a/src/spells.c +++ b/src/spells.c @@ -731,7 +731,7 @@ static int sp_transferaura(castorder * co) int cast_level = co->level; spellparameter *pa = co->par; unit *u; - sc_mage *scm_dst, *scm_src = get_mage(mage); + sc_mage *scm_dst, *scm_src = get_mage_depr(mage); /* wenn kein Ziel gefunden, Zauber abbrechen */ if (pa->param[0]->flag == TARGET_NOTFOUND) @@ -745,7 +745,7 @@ static int sp_transferaura(castorder * co) /* Wieviel Transferieren? */ aura = pa->param[1]->data.i; u = pa->param[0]->data.u; - scm_dst = get_mage(u); + scm_dst = get_mage_depr(u); if (scm_dst == NULL) { /* "Zu dieser Einheit kann ich keine Aura uebertragen." */ @@ -5813,7 +5813,7 @@ int sp_permtransfer(castorder * co) change_maxspellpoints(mage, -aura); change_spellpoints(mage, -aura); - if (get_mage(tu)->magietyp == get_mage(mage)->magietyp) { + if (get_mage_depr(tu)->magietyp == get_mage_depr(mage)->magietyp) { change_maxspellpoints(tu, aura / 2); } else { @@ -5936,18 +5936,18 @@ int sp_stealaura(castorder * co) /* Zieleinheit */ u = pa->param[0]->data.u; - if (!get_mage(u)) { + if (!get_mage_depr(u)) { ADDMSG(&mage->faction->msgs, msg_message("stealaura_fail", "unit target", mage, u)); ADDMSG(&u->faction->msgs, msg_message("stealaura_fail_detect", "unit", u)); return 0; } - taura = (get_mage(u)->spellpoints * (rng_int() % (int)(3 * power) + 1)) / 100; + taura = (get_mage_depr(u)->spellpoints * (rng_int() % (int)(3 * power) + 1)) / 100; if (taura > 0) { - get_mage(u)->spellpoints -= taura; - get_mage(mage)->spellpoints += taura; + get_mage_depr(u)->spellpoints -= taura; + get_mage_depr(mage)->spellpoints += taura; /* sprintf(buf, "%s entzieht %s %d Aura.", unitname(mage), unitname(u), taura); */ ADDMSG(&mage->faction->msgs, msg_message("stealaura_success", diff --git a/src/spy.c b/src/spy.c index 837bc28ee..210be63e4 100644 --- a/src/spy.c +++ b/src/spy.c @@ -68,7 +68,7 @@ void spy_message(int spy, const unit * u, const unit * target) ADDMSG(&u->faction->msgs, msg_message("spyreport", "spy target status", u, target, status)); if (spy > 20) { - sc_mage *mage = get_mage(target); + sc_mage *mage = get_mage_depr(target); /* for mages, spells and magic school */ if (mage) { ADDMSG(&u->faction->msgs, msg_message("spyreport_mage", "spy target type", u, diff --git a/src/study.c b/src/study.c index 6949db22e..88550fc30 100644 --- a/src/study.c +++ b/src/study.c @@ -427,8 +427,8 @@ int teach_cmd(unit * teacher, struct order *ord) if (sk == SK_MAGIC) { /* ist der Magier schon spezialisiert, so versteht er nur noch * Lehrer seines Gebietes */ - sc_mage *mage1 = get_mage(teacher); - sc_mage *mage2 = get_mage(student); + sc_mage *mage1 = get_mage_depr(teacher); + sc_mage *mage2 = get_mage_depr(student); if (mage2 && mage1 && mage2->magietyp != M_GRAY && mage1->magietyp != mage2->magietyp) { if (feedback) { @@ -782,7 +782,7 @@ int study_cmd(unit * u, order * ord) } } else if (sk == SK_MAGIC) { - sc_mage *mage = get_mage(u); + sc_mage *mage = get_mage_depr(u); if (!mage) { mage = create_mage(u, u->faction->magiegebiet); } diff --git a/src/study.test.c b/src/study.test.c index 21f76ad4c..75c169b74 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -409,11 +409,11 @@ static void test_study_magic(CuTest *tc) { study_cmd(u, u->thisorder); CuAssertIntEquals(tc, M_GWYRRD, f->magiegebiet); CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype)); - CuAssertPtrNotNull(tc, get_mage(u)); + CuAssertPtrNotNull(tc, get_mage_depr(u)); CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "error65")); - CuAssertIntEquals(tc, M_GWYRRD, get_mage(u)->magietyp); + CuAssertIntEquals(tc, M_GWYRRD, get_mage_depr(u)->magietyp); - /* the static cost array in study_cost prevents this test: + /* TODO: the static cost array in study_cost prevents this test: test_clear_messages(f); config_set("skills.cost.magic", "50"); i_change(&u->items, rtype->itype, 50);