move spell reporting attributes to a separate module.

This commit is contained in:
Enno Rehling 2018-02-08 18:33:58 +01:00
parent 87081a37bc
commit ec48017520
10 changed files with 146 additions and 92 deletions

View File

@ -93,6 +93,7 @@ set (ERESSEA_SRC
battle.c
alchemy.c
academy.c
chaos.c
upkeep.c
names.c
lighthouse.c
@ -121,7 +122,6 @@ set (ERESSEA_SRC
randenc.c
renumber.c
volcano.c
chaos.c
spy.c
study.c
summary.c

View File

@ -20,6 +20,7 @@ racename.c
raceprefix.c
reduceproduction.c
stealth.c
seenspell.c
targetregion.c
)
FOREACH(_FILE ${_FILES})

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "laws.h"
#include "move.h"
#include "magic.h"
/* attributes includes */
#include "follow.h"
@ -29,7 +30,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "iceberg.h"
#include "key.h"
#include "stealth.h"
#include "magic.h"
#include "moved.h"
#include "movement.h"
#include "dict.h"
@ -38,6 +38,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "racename.h"
#include "raceprefix.h"
#include "reduceproduction.h"
#include "seenspell.h"
#include "targetregion.h"
/* kernel includes */

126
src/attributes/seenspell.c Normal file
View File

@ -0,0 +1,126 @@
/*
Copyright (c) 1998-2014,
Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#ifdef _MSC_VER
#include <platform.h>
#endif
#include <kernel/faction.h>
#include <kernel/spell.h>
#include <kernel/spellbook.h>
#include <util/attrib.h>
#include <util/gamedata.h>
#include <util/log.h>
#include <util/macros.h>
#include "seenspell.h"
#include <selist.h>
#include <storage.h>
#include <stdlib.h>
/* ------------------------------------------------------------- */
/* 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.
*/
static int read_seenspell(attrib * a, void *owner, struct gamedata *data)
{
storage *store = data->store;
spell *sp = 0;
char token[32];
UNUSED_ARG(owner);
READ_TOK(store, token, sizeof(token));
if (data->version < UNIQUE_SPELLS_VERSION) {
READ_INT(store, 0); /* ignore mtype */
}
sp = find_spell(token);
if (!sp) {
log_info("read_seenspell: could not find spell '%s'\n", token);
return AT_READ_FAIL;
}
a->data.v = sp;
return AT_READ_OK;
}
static void
write_seenspell(const attrib * a, const void *owner, struct storage *store)
{
const spell *sp = (const spell *)a->data.v;
UNUSED_ARG(owner);
WRITE_TOK(store, sp->sname);
}
attrib_type at_seenspell = {
"seenspell", NULL, NULL, NULL, write_seenspell, read_seenspell
};
static bool already_seen(const faction * f, const spell * sp)
{
attrib *a;
for (a = a_find(f->attribs, &at_seenspell); a && a->type == &at_seenspell;
a = a->next) {
if (a->data.v == sp)
return true;
}
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
};
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) {
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;
}
}
}
}
}

View File

@ -0,0 +1,13 @@
#ifndef H_SEENSPELL
#define H_SEENSPELL
struct attrib_type;
struct spellbook;
struct faction;
void show_new_spells(struct faction * f, int level, const struct spellbook *book);
extern struct attrib_type at_reportspell;
extern struct attrib_type at_seenspell;
#endif

View File

@ -30,6 +30,7 @@ without prior permission by the authors of Eressea.
#include <attributes/otherfaction.h>
#include <attributes/racename.h>
#include <attributes/raceprefix.h>
#include <attributes/seenspell.h>
#include <attributes/stealth.h>
/* gamecode includes */

View File

@ -48,6 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* attributes includes */
#include <attributes/racename.h>
#include <attributes/raceprefix.h>
#include <attributes/seenspell.h>
#include <attributes/stealth.h>
#include <spells/buildingcurse.h>

View File

@ -101,20 +101,6 @@ const char *magic_school[MAXMAGIETYP] = {
"common"
};
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
};
/**
** at_icastle
** TODO: separate castle-appearance from illusion-effects
@ -354,82 +340,8 @@ sc_mage *get_mage_depr(const unit * u)
return NULL;
}
/* ------------------------------------------------------------- */
/* 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.
*/
static int read_seenspell(attrib * a, void *owner, struct gamedata *data)
{
storage *store = data->store;
spell *sp = 0;
char token[32];
UNUSED_ARG(owner);
READ_TOK(store, token, sizeof(token));
if (data->version < UNIQUE_SPELLS_VERSION) {
READ_INT(store, 0); /* ignore mtype */
}
sp = find_spell(token);
if (!sp) {
log_info("read_seenspell: could not find spell '%s'\n", token);
return AT_READ_FAIL;
}
a->data.v = sp;
return AT_READ_OK;
}
static void
write_seenspell(const attrib * a, const void *owner, struct storage *store)
{
const spell *sp = (const spell *)a->data.v;
UNUSED_ARG(owner);
WRITE_TOK(store, sp->sname);
}
attrib_type at_seenspell = {
"seenspell", NULL, NULL, NULL, write_seenspell, read_seenspell
};
#define MAXSPELLS 256
static bool already_seen(const faction * f, const spell * sp)
{
attrib *a;
for (a = a_find(f->attribs, &at_seenspell); a && a->type == &at_seenspell;
a = a->next) {
if (a->data.v == sp)
return true;
}
return false;
}
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) {
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;
}
}
}
}
}
/** update the spellbook with a new level
* Written for E3
*/

View File

@ -191,13 +191,11 @@ extern "C" {
void regenerate_aura(void);
extern struct attrib_type at_seenspell;
extern struct attrib_type at_mage;
extern struct attrib_type at_familiarmage;
extern struct attrib_type at_familiar;
extern struct attrib_type at_clonemage;
extern struct attrib_type at_clone;
extern struct attrib_type at_reportspell;
extern struct attrib_type at_icastle;
void make_icastle(struct building *b, const struct building_type *btype, int timeout);

View File

@ -39,6 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <attributes/overrideroads.h>
#include <attributes/otherfaction.h>
#include <attributes/reduceproduction.h>
#include <attributes/seenspell.h>
/* gamecode includes */
#include "alchemy.h"