From ec4801752041b8cdbff47ce937dfd7cab1a64a59 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 8 Feb 2018 18:33:58 +0100 Subject: [PATCH] move spell reporting attributes to a separate module. --- src/CMakeLists.txt | 2 +- src/attributes/CMakeLists.txt | 1 + src/attributes/attributes.c | 3 +- src/attributes/seenspell.c | 126 ++++++++++++++++++++++++++++++++++ src/attributes/seenspell.h | 13 ++++ src/creport.c | 1 + src/laws.c | 1 + src/magic.c | 88 ------------------------ src/magic.h | 2 - src/report.c | 1 + 10 files changed, 146 insertions(+), 92 deletions(-) create mode 100644 src/attributes/seenspell.c create mode 100644 src/attributes/seenspell.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a17a45e9..a74d1d732 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/attributes/CMakeLists.txt b/src/attributes/CMakeLists.txt index fb077cfe9..c76f2458a 100644 --- a/src/attributes/CMakeLists.txt +++ b/src/attributes/CMakeLists.txt @@ -20,6 +20,7 @@ racename.c raceprefix.c reduceproduction.c stealth.c +seenspell.c targetregion.c ) FOREACH(_FILE ${_FILES}) diff --git a/src/attributes/attributes.c b/src/attributes/attributes.c index a5911200c..cb6e335b5 100644 --- a/src/attributes/attributes.c +++ b/src/attributes/attributes.c @@ -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 */ diff --git a/src/attributes/seenspell.c b/src/attributes/seenspell.c new file mode 100644 index 000000000..99ac84873 --- /dev/null +++ b/src/attributes/seenspell.c @@ -0,0 +1,126 @@ +/* +Copyright (c) 1998-2014, +Enno Rehling +Katja Zedel + +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 +#endif +#include +#include +#include +#include +#include +#include +#include + +#include "seenspell.h" + +#include +#include + +#include + +/* ------------------------------------------------------------- */ +/* 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; + } + } + } + } +} + diff --git a/src/attributes/seenspell.h b/src/attributes/seenspell.h new file mode 100644 index 000000000..488430575 --- /dev/null +++ b/src/attributes/seenspell.h @@ -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 diff --git a/src/creport.c b/src/creport.c index fa17c426d..04f6c6bc4 100644 --- a/src/creport.c +++ b/src/creport.c @@ -30,6 +30,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include /* gamecode includes */ diff --git a/src/laws.c b/src/laws.c index 85b820323..9e2eb9471 100644 --- a/src/laws.c +++ b/src/laws.c @@ -48,6 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* attributes includes */ #include #include +#include #include #include diff --git a/src/magic.c b/src/magic.c index 6855c307f..4e9c87b4e 100644 --- a/src/magic.c +++ b/src/magic.c @@ -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 */ diff --git a/src/magic.h b/src/magic.h index 403dafdc6..101ef5d38 100644 --- a/src/magic.h +++ b/src/magic.h @@ -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); diff --git a/src/report.c b/src/report.c index 6543b153b..30ea34fdf 100644 --- a/src/report.c +++ b/src/report.c @@ -39,6 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include /* gamecode includes */ #include "alchemy.h"