From 9d1b62d4633906396aaf548467c23dc8dc275ba0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 26 May 2012 00:33:55 -0700 Subject: [PATCH] updating spell-lists is a thing of the past now almost everything uses books, except sc_mage --- src/gamecode/laws.c | 64 +++++++++++++++++++++++++++-------------- src/kernel/faction.c | 11 +++++++ src/kernel/faction.h | 1 + src/kernel/magic.c | 59 +++++++++++-------------------------- src/kernel/magic.h | 1 + src/kernel/magic_test.c | 3 +- src/kernel/save.c | 4 +-- src/kernel/unit.c | 8 ++++-- 8 files changed, 81 insertions(+), 70 deletions(-) diff --git a/src/gamecode/laws.c b/src/gamecode/laws.c index d4e591fd2..6dd2af667 100644 --- a/src/gamecode/laws.c +++ b/src/gamecode/laws.c @@ -3687,6 +3687,32 @@ static void defaultorders(void) /* ************************************************************ */ #define COMMONSPELLS 1 /* number of new common spells per level */ #define MAXMAGES 128 /* should be enough */ + +static int faction_getmages(faction * f, unit ** results, int numresults) +{ + unit *u; + int maxlevel = 0, n = 0; + + for (u = f->units; u; u = u->nextF) { + if (u->number > 0) { + sc_mage *mage = get_mage(u); + if (mage) { + int level = eff_skill(u, SK_MAGIC, u->region); + if (level > maxlevel) { + maxlevel = level; + } + if (nnext) { if (f->magiegebiet != M_NONE && !is_monsters(f)) { unit *mages[MAXMAGES]; - unit *u; - int maxlevel = 0, n = 0, i; - - for (u = f->units; u; u = u->nextF) { - if (u->number > 0) { - sc_mage *mage = get_mage(u); - if (mage) { - int level = eff_skill(u, SK_MAGIC, u->region); - if (level > maxlevel) - maxlevel = level; - assert(n < MAXMAGES); - mages[n++] = u; - } - } - } + int i; + int maxlevel = faction_getmages(f, mages, MAXMAGES); if (FactionSpells() && maxlevel > f->max_spelllevel) { static spellbook * common_spells; if (!common_spells) { const char *common_school = get_param(global.parameters, "rules.magic.common"); - common_spells = get_spellbook(common_school ? common_school : "common"); - } - if (common_spells) { - pick_random_spells(f, maxlevel, common_spells, COMMONSPELLS); + if (!common_school) common_school = "common"; + common_spells = get_spellbook(common_school); + if (!common_spells) { + log_error("could not find a book of common spells: '%s'\n", common_school); + } } + pick_random_spells(f, maxlevel, common_spells, COMMONSPELLS); } - for (i = 0; i != n; ++i) { - updatespelllist(mages[i]); + show_new_spells(f, maxlevel, faction_get_spellbook(f)); + for (i=0; i!=MAXMAGES && mages[i]; ++i) { + sc_mage *mage = get_mage(mages[i]); + if (mage && mage->spells) { +#ifdef TODO + int level = effskill(mages[i], SK_MAGIC); + show_new_spells(f, level, mage->spellbook); +#endif + } } } } diff --git a/src/kernel/faction.c b/src/kernel/faction.c index d9c701a41..dcfa7b59a 100644 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -511,3 +511,14 @@ struct alliance *f_get_alliance(const struct faction *f) } return NULL; } + +struct spellbook * faction_get_spellbook(struct faction *f) +{ + if (f->spellbook) { + return f->spellbook; + } + if (f->magiegebiet!=M_GRAY) { + return get_spellbook(magic_school[f->magiegebiet]); + } + return 0; +} diff --git a/src/kernel/faction.h b/src/kernel/faction.h index ec82bbe51..a81d0545c 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -152,6 +152,7 @@ extern "C" { void faction_setpassword(struct faction *self, const char *password); boolean valid_race(const struct faction *f, const struct race *rc); + struct spellbook * faction_get_spellbook(struct faction *f); #ifdef __cplusplus } #endif diff --git a/src/kernel/magic.c b/src/kernel/magic.c index 0518ea856..881ba67f0 100644 --- a/src/kernel/magic.c +++ b/src/kernel/magic.c @@ -409,50 +409,26 @@ static boolean already_seen(const faction * f, const spell * sp) return false; } -static void update_spells(faction * f, sc_mage * mage, int level, const spellbook *book) +void show_new_spells(faction * f, int level, const spellbook *book) { - boolean ismonster = is_monsters(f); - quicklist **dst, *ql = book->spells; - int qi; + if (book) { + quicklist *ql = book->spells; + int qi; - dst = get_spelllist(mage, f); - for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { - spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); - if (sbe->level <= level) { - spell * sp = sbe->sp; + for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { + spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi); + if (sbe->level <= level) { + spell * sp = sbe->sp; - if (!u_hasspell(mage, sp)) { - add_spell(dst, sp); - add_spellname(mage, sp); + if (!already_seen(f, sp)) { + attrib * a = a_new(&at_reportspell); + spellbook_entry * entry = (spellbook_entry *)a->data.v; + entry->level = level; + entry->sp = sp; + a_add(&f->attribs, a); + a_add(&f->attribs, a_new(&at_seenspell))->data.v = sp; + } } - if (!ismonster && !already_seen(f, sp)) { - attrib * a = a_new(&at_reportspell); - spellbook_entry * entry = (spellbook_entry *)a->data.v; - entry->level = level; - entry->sp = sp; - a_add(&f->attribs, a); - a_add(&f->attribs, a_new(&at_seenspell))->data.v = sp; - } - } - } -} - -void updatespelllist(unit * u) -{ - int sk = eff_skill(u, SK_MAGIC, u->region); - struct sc_mage *mage = get_mage(u); - - /* Magier mit M_GRAY bekommen weder Sprüche angezeigt noch - * neue Sprüche in ihre List-of-known-spells. Das sind zb alle alten - * Drachen, die noch den Skill Magie haben, und alle familiars */ - if (mage->magietyp != M_GRAY) { - spellbook * book; - book = get_spellbook(magic_school[mage->magietyp]); - - update_spells(u->faction, mage, sk, book); - - if (FactionSpells()) { - update_spells(u->faction, mage, sk, u->faction->spellbook); } } } @@ -1522,9 +1498,6 @@ void regeneration_magiepunkte(void) "unit region amount", u, r, regen)); } set_spellpoints(u, MIN(aura, auramax)); - - /* Zum letzten Mal Spruchliste aktualisieren */ - /*updatespelllist(u); */ } } } diff --git a/src/kernel/magic.h b/src/kernel/magic.h index 73ef8018b..da0b693db 100644 --- a/src/kernel/magic.h +++ b/src/kernel/magic.h @@ -255,6 +255,7 @@ typedef struct sc_mage { int u_hasspell(const sc_mage *mage, const struct spell *sp); /* prüft, ob der Spruch in der Spruchliste der Einheit steht. */ void pick_random_spells(struct faction *f, int level, struct spellbook * book, int num_spells); + void show_new_spells(struct faction * f, int level, const struct spellbook *book); void updatespelllist(struct unit *u); /* fügt alle Zauber des Magiegebietes der Einheit, deren Stufe kleiner * als das aktuelle Magietalent ist, in die Spruchliste der Einheit diff --git a/src/kernel/magic_test.c b/src/kernel/magic_test.c index 716ce3a85..1989dac52 100644 --- a/src/kernel/magic_test.c +++ b/src/kernel/magic_test.c @@ -247,7 +247,8 @@ void test_getspell_school(CuTest * tc) CuAssertPtrEquals(tc, 0, unit_getspell(u, "Herp-a-derp", lang)); - book = get_spellbook(magic_school[f->magiegebiet]); + book = faction_get_spellbook(f); + CuAssertPtrNotNull(tc, book); spellbook_add(book, sp, 1); CuAssertPtrEquals(tc, sp, unit_getspell(u, "Herp-a-derp", lang)); } diff --git a/src/kernel/save.c b/src/kernel/save.c index 545a45c6a..0de9d337c 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1615,8 +1615,9 @@ int readgame(const char *filename, int mode, int backup) n = store->r_int(store); assert(n < MAXREGIONS); - if (rmax < 0) + if (rmax < 0) { rmax = n; + } log_printf(stdout, " - Einzulesende Regionen: %d/%d\r", rmax, n); while (--n >= 0) { unit **up; @@ -1727,7 +1728,6 @@ int readgame(const char *filename, int mode, int backup) } if (mage->spellcount < 0) { mage->spellcount = 0; - updatespelllist(u); } } } diff --git a/src/kernel/unit.c b/src/kernel/unit.c index e73a9f617..532543553 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1742,10 +1742,12 @@ struct spellbook * unit_get_spellbook(const struct unit * u) { sc_mage * mage = get_mage(u); if (mage) { - if (u->faction->spellbook) { - return u->faction->spellbook; + if (mage->spells) { +#ifdef TODO + return mage->spellbook; +#endif } - return get_spellbook(magic_school[u->faction->magiegebiet]); + return faction_get_spellbook(u->faction); } return 0; } \ No newline at end of file