constrict the seenspell module a bit more, move functionality inside.

This commit is contained in:
Enno Rehling 2018-02-08 19:37:47 +01:00
parent ec48017520
commit cfc3171021
4 changed files with 49 additions and 47 deletions

View File

@ -1,5 +1,5 @@
/*
Copyright (c) 1998-2014,
Copyright (c) 1998-2018,
Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
@ -37,11 +37,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* ------------------------------------------------------------- */
/* Ausgabe der Spruchbeschreibungen
* Anzeige des Spruchs nur, wenn die Stufe des besten Magiers vorher
* kleiner war (u->faction->seenspells). Ansonsten muss nur geprüft
* werden, ob dieser Magier den Spruch schon kennt, und andernfalls der
* Spruch zu seiner List-of-known-spells hinzugefügt werden.
*/
* Anzeige des Spruchs nur, wenn die Stufe des besten Magiers vorher
* kleiner war (u->faction->seenspells). Ansonsten muss nur geprüft
* werden, ob dieser Magier den Spruch schon kennt, und andernfalls der
* Spruch zu seiner List-of-known-spells hinzugefügt werden.
*/
static int read_seenspell(attrib * a, void *owner, struct gamedata *data)
{
@ -87,40 +87,32 @@ static bool already_seen(const faction * f, const spell * sp)
return false;
}
static void a_init_reportspell(struct attrib *a) {
a->data.v = calloc(1, sizeof(spellbook_entry));
}
static void a_finalize_reportspell(struct attrib *a) {
free(a->data.v);
}
attrib_type at_reportspell = {
"reportspell",
a_init_reportspell,
a_finalize_reportspell,
0, NO_WRITE, NO_READ
"reportspell", NULL
};
void show_new_spells(faction * f, int level, const spellbook *book)
void show_spell(faction *f, const spellbook_entry *sbe)
{
if (book) {
selist *ql = book->spells;
int qi;
for (qi = 0; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
if (sbe->level <= level) {
if (!already_seen(f, sbe->sp)) {
attrib * a = a_new(&at_reportspell);
spellbook_entry * entry = (spellbook_entry *)a->data.v;
entry->level = sbe->level;
entry->sp = sbe->sp;
a_add(&f->attribs, a);
a_add(&f->attribs, a_new(&at_seenspell))->data.v = sbe->sp;
}
}
}
if (!already_seen(f, sbe->sp)) {
attrib * a = a_new(&at_reportspell);
a->data.v = (void *)sbe;
a_add(&f->attribs, a);
a_add(&f->attribs, a_new(&at_seenspell))->data.v = sbe->sp;
}
}
void reset_seen_spells(faction *f, const struct spell *sp)
{
if (sp) {
attrib *a = a_find(f->attribs, &at_seenspell);
while (a && a->type == &at_seenspell && a->data.v != sp) {
a = a->next;
}
if (a) {
a_remove(&f->attribs, a);
}
}
else {
a_removeall(&f->attribs, &at_seenspell);
}
}

View File

@ -2,10 +2,12 @@
#define H_SEENSPELL
struct attrib_type;
struct spellbook;
struct spellbook_entry;
struct faction;
struct spell;
void show_new_spells(struct faction * f, int level, const struct spellbook *book);
void show_spell(struct faction * f, const struct spellbook_entry *sbe);
void reset_seen_spells(struct faction * f, const struct spell *sp);
extern struct attrib_type at_reportspell;
extern struct attrib_type at_seenspell;

View File

@ -2307,13 +2307,7 @@ static void reshow_other(unit * u, struct order *ord, const char *s) {
}
if (sp) {
attrib *a = a_find(u->faction->attribs, &at_seenspell);
while (a != NULL && a->type == &at_seenspell && a->data.v != sp) {
a = a->next;
}
if (a != NULL) {
a_remove(&u->faction->attribs, a);
}
reset_seen_spells(u->faction, sp);
found = true;
}
@ -2331,7 +2325,7 @@ static void reshow(unit * u, struct order *ord, const char *s, param_t p)
{
switch (p) {
case P_ZAUBER:
a_removeall(&u->faction->attribs, &at_seenspell);
reset_seen_spells(u->faction, NULL);
break;
case P_POTIONS:
if (!display_potions(u)) {
@ -3304,6 +3298,21 @@ static void copy_spells(const spellbook * src, spellbook * dst, int maxlevel)
}
}
static void show_new_spells(faction * f, int level, const spellbook *book)
{
if (book) {
selist *ql = book->spells;
int qi;
for (qi = 0; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
if (sbe->level <= level) {
show_spell(f, sbe);
}
}
}
}
static void update_spells(void)
{
faction *f;

View File

@ -241,7 +241,6 @@ extern "C" {
int u_hasspell(const struct unit *u, 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);
bool knowsspell(const struct region *r, const struct unit *u,
const struct spell * sp);
/* prüft, ob die Einheit diesen Spruch gerade beherrscht, dh