From 0472ac761ea132c228da41b3bdecce4edc228b28 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 14 Nov 2016 01:22:50 +0100 Subject: [PATCH] declutter save.h (items, spellbooks) --- src/kernel/save.c | 50 --------------------------------------- src/kernel/save.h | 3 --- src/kernel/spellbook.c | 53 ++++++++++++++++++++++++++++++++++++++++++ src/kernel/spellbook.h | 5 ++++ 4 files changed, 58 insertions(+), 53 deletions(-) diff --git a/src/kernel/save.c b/src/kernel/save.c index cefbf3cc5..143912832 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1194,56 +1194,6 @@ int get_spell_level_faction(const spell * sp, void * cbdata) return 0; } -void read_spellbook(spellbook **bookp, gamedata *data, int(*get_level)(const spell * sp, void *), void * cbdata) -{ - for (;;) { - spell *sp = 0; - char spname[64]; - int level = 0; - - READ_TOK(data->store, spname, sizeof(spname)); - if (strcmp(spname, "end") == 0) - break; - if (bookp) { - sp = find_spell(spname); - if (!sp) { - log_error("read_spells: could not find spell '%s'", spname); - } - } - if (data->version >= SPELLBOOK_VERSION) { - READ_INT(data->store, &level); - } - if (sp) { - spellbook * sb = *bookp; - if (level <= 0 && get_level) { - level = get_level(sp, cbdata); - } - if (!sb) { - *bookp = create_spellbook(0); - sb = *bookp; - } - if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) { - spellbook_add(sb, sp, level); - } - } - } -} - -void write_spellbook(const struct spellbook *book, struct storage *store) -{ - quicklist *ql; - int qi; - - if (book) { - for (ql = book->spells, qi = 0; ql; ql_advance(&ql, &qi, 1)) { - spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); - WRITE_TOK(store, sbe->sp->sname); - WRITE_INT(store, sbe->level); - } - } - WRITE_TOK(store, "end"); -} - static char * getpasswd(int fno) { const char *prefix = itoa36(fno); size_t len = strlen(prefix); diff --git a/src/kernel/save.h b/src/kernel/save.h index 4d310310c..acaf74a3b 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -50,9 +50,6 @@ extern "C" { int current_turn(void); - void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); - void write_spellbook(const struct spellbook *book, struct storage *store); - void write_unit(struct gamedata *data, const struct unit *u); struct unit *read_unit(struct gamedata *data); diff --git a/src/kernel/spellbook.c b/src/kernel/spellbook.c index 96410170b..da8fcd563 100644 --- a/src/kernel/spellbook.c +++ b/src/kernel/spellbook.c @@ -3,9 +3,12 @@ #include #include #include +#include #include "spellbook.h" +#include + #include #include #include @@ -18,6 +21,56 @@ spellbook * create_spellbook(const char * name) return result; } +void read_spellbook(spellbook **bookp, gamedata *data, int(*get_level)(const spell * sp, void *), void * cbdata) +{ + for (;;) { + spell *sp = 0; + char spname[64]; + int level = 0; + + READ_TOK(data->store, spname, sizeof(spname)); + if (strcmp(spname, "end") == 0) + break; + if (bookp) { + sp = find_spell(spname); + if (!sp) { + log_error("read_spells: could not find spell '%s'", spname); + } + } + if (data->version >= SPELLBOOK_VERSION) { + READ_INT(data->store, &level); + } + if (sp) { + spellbook * sb = *bookp; + if (level <= 0 && get_level) { + level = get_level(sp, cbdata); + } + if (!sb) { + *bookp = create_spellbook(0); + sb = *bookp; + } + if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) { + spellbook_add(sb, sp, level); + } + } + } +} + +void write_spellbook(const struct spellbook *book, struct storage *store) +{ + quicklist *ql; + int qi; + + if (book) { + for (ql = book->spells, qi = 0; ql; ql_advance(&ql, &qi, 1)) { + spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); + WRITE_TOK(store, sbe->sp->sname); + WRITE_INT(store, sbe->level); + } + } + WRITE_TOK(store, "end"); +} + void spellbook_add(spellbook *sb, struct spell * sp, int level) { spellbook_entry * sbe; diff --git a/src/kernel/spellbook.h b/src/kernel/spellbook.h index d0e25da23..84ec0ce5f 100644 --- a/src/kernel/spellbook.h +++ b/src/kernel/spellbook.h @@ -24,6 +24,8 @@ extern "C" { #endif struct spell; + struct storage; + struct gamedata; struct quicklist; typedef struct spellbook_entry { @@ -39,6 +41,9 @@ extern "C" { spellbook * create_spellbook(const char * name); + void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); + void write_spellbook(const struct spellbook *book, struct storage *store); + void spellbook_add(spellbook *sbp, struct spell * sp, int level); int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *), void * data); void spellbook_clear(spellbook *sb);