declutter save.h (items, spellbooks)

This commit is contained in:
Enno Rehling 2016-11-14 01:22:50 +01:00
parent d0b4f16213
commit 0472ac761e
4 changed files with 58 additions and 53 deletions

View File

@ -1194,56 +1194,6 @@ int get_spell_level_faction(const spell * sp, void * cbdata)
return 0; 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) { static char * getpasswd(int fno) {
const char *prefix = itoa36(fno); const char *prefix = itoa36(fno);
size_t len = strlen(prefix); size_t len = strlen(prefix);

View File

@ -50,9 +50,6 @@ extern "C" {
int current_turn(void); 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); void write_unit(struct gamedata *data, const struct unit *u);
struct unit *read_unit(struct gamedata *data); struct unit *read_unit(struct gamedata *data);

View File

@ -3,9 +3,12 @@
#include <kernel/spell.h> #include <kernel/spell.h>
#include <quicklist.h> #include <quicklist.h>
#include <util/log.h> #include <util/log.h>
#include <util/gamedata.h>
#include "spellbook.h" #include "spellbook.h"
#include <storage.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -18,6 +21,56 @@ spellbook * create_spellbook(const char * name)
return result; 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) void spellbook_add(spellbook *sb, struct spell * sp, int level)
{ {
spellbook_entry * sbe; spellbook_entry * sbe;

View File

@ -24,6 +24,8 @@ extern "C" {
#endif #endif
struct spell; struct spell;
struct storage;
struct gamedata;
struct quicklist; struct quicklist;
typedef struct spellbook_entry { typedef struct spellbook_entry {
@ -39,6 +41,9 @@ extern "C" {
spellbook * create_spellbook(const char * name); 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); void spellbook_add(spellbook *sbp, struct spell * sp, int level);
int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *), void * data); int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *), void * data);
void spellbook_clear(spellbook *sb); void spellbook_clear(spellbook *sb);