spellbooks should not use spellref, it's unnecessary.

This commit is contained in:
Enno Rehling 2017-02-02 20:08:04 +01:00
parent 0c08a64495
commit 456d1bd196
11 changed files with 48 additions and 69 deletions

View File

@ -719,7 +719,7 @@ static void cr_output_spells(stream *out, const unit * u, int maxlevel)
spellbook_entry * sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry * sbe = (spellbook_entry *)selist_get(ql, qi);
if (sbe->level <= maxlevel) { if (sbe->level <= maxlevel) {
// TODO: no need to deref spref here, spref->name == sp->sname // TODO: no need to deref spref here, spref->name == sp->sname
spell * sp = spellref_get(sbe->spref); spell * sp = sbe->sp;
const char *name = translate(mkname("spell", sp->sname), spell_name(sp, f->locale)); const char *name = translate(mkname("spell", sp->sname), spell_name(sp, f->locale));
if (!header) { if (!header) {
stream_printf(out, "SPRUECHE\n"); stream_printf(out, "SPRUECHE\n");
@ -1622,7 +1622,7 @@ report_computer(const char *filename, report_context * ctx, const char *bom)
a = a_find(f->attribs, &at_reportspell); a = a_find(f->attribs, &at_reportspell);
while (a && a->type == &at_reportspell) { while (a && a->type == &at_reportspell) {
spellbook_entry *sbe = (spellbook_entry *)a->data.v; spellbook_entry *sbe = (spellbook_entry *)a->data.v;
cr_reportspell(F, spellref_get(sbe->spref), sbe->level, f->locale); cr_reportspell(F, sbe->sp, sbe->level, f->locale);
a = a->next; a = a->next;
} }
for (a = a_find(f->attribs, &at_showitem); a && a->type == &at_showitem; for (a = a_find(f->attribs, &at_showitem); a && a->type == &at_showitem;

View File

@ -146,12 +146,18 @@ spell *find_spellbyid(unsigned int id)
return NULL; return NULL;
} }
struct spellref *spellref_create(const char *name) struct spellref *spellref_create(spell *sp, const char *name)
{ {
spellref *spref = malloc(sizeof(spellref)); spellref *spref = malloc(sizeof(spellref));
spref->name = strdup(name); if (sp) {
spref->sp = NULL; spref->sp = sp;
spref->name = strdup(sp->sname);
}
else if (name) {
spref->name = strdup(name);
spref->sp = NULL;
}
return spref; return spref;
} }
@ -166,16 +172,12 @@ void spellref_free(spellref *spref)
struct spell *spellref_get(struct spellref *spref) struct spell *spellref_get(struct spellref *spref)
{ {
if (!spref->sp) { if (!spref->sp) {
assert(spref->name);
spref->sp = find_spell(spref->name); spref->sp = find_spell(spref->name);
if (spref->sp) {
free(spref->name);
spref->name = NULL;
}
} }
return spref->sp; return spref->sp;
} }
spellref *spellref_copy(spellref *spref)
{
spellref *result = spellref_create(spref->name);
if (spref->sp) {
result->sp = spref->sp;
}
return result;
}

View File

@ -50,9 +50,8 @@ extern "C" {
struct spell *sp; struct spell *sp;
} spellref; } spellref;
struct spellref *spellref_create(const char *name); struct spellref *spellref_create(struct spell *sp, const char *name);
void spellref_free(struct spellref *spref); void spellref_free(struct spellref *spref);
struct spellref *spellref_copy(struct spellref *spref);
struct spell *spellref_get(struct spellref *spref); struct spell *spellref_get(struct spellref *spref);
int sp_antimagiczone(struct castorder *co); int sp_antimagiczone(struct castorder *co);

View File

@ -74,13 +74,14 @@ static void test_spellref(CuTest *tc)
spellref *ref; spellref *ref;
spell *sp; spell *sp;
test_setup(); test_setup();
ref = spellref_create("hodor"); ref = spellref_create(NULL, "hodor");
CuAssertPtrNotNull(tc, ref); CuAssertPtrNotNull(tc, ref);
CuAssertPtrEquals(tc, NULL, ref->sp);
CuAssertStrEquals(tc, "hodor", ref->name);
CuAssertPtrEquals(tc, NULL, spellref_get(ref)); CuAssertPtrEquals(tc, NULL, spellref_get(ref));
sp = create_spell("hodor", 0); sp = create_spell("hodor", 0);
CuAssertPtrNotNull(tc, sp); CuAssertPtrNotNull(tc, sp);
CuAssertPtrEquals(tc, sp, spellref_get(ref)); CuAssertPtrEquals(tc, sp, spellref_get(ref));
CuAssertPtrEquals(tc, NULL, ref->name);
spellref_free(ref); spellref_free(ref);
test_cleanup(); test_cleanup();
} }

View File

@ -50,7 +50,7 @@ void read_spellbook(spellbook **bookp, gamedata *data, int(*get_level)(const spe
sb = *bookp; sb = *bookp;
} }
if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) { if (level > 0 && (data->version >= SPELLBOOK_VERSION || !spellbook_get(sb, sp))) {
spellbook_add_spell(sb, sp, level); spellbook_add(sb, sp, level);
} }
} }
} }
@ -64,43 +64,25 @@ void write_spellbook(const struct spellbook *book, struct storage *store)
if (book) { if (book) {
for (ql = book->spells, qi = 0; ql; selist_advance(&ql, &qi, 1)) { for (ql = book->spells, qi = 0; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
WRITE_TOK(store, sbe->spref->name); WRITE_TOK(store, sbe->sp->sname);
WRITE_INT(store, sbe->level); WRITE_INT(store, sbe->level);
} }
} }
WRITE_TOK(store, "end"); WRITE_TOK(store, "end");
} }
void spellbook_add(spellbook *sb, spellref *spref, int level) void spellbook_add(spellbook *sb, spell *sp, int level)
{ {
spellbook_entry * sbe; spellbook_entry * sbe;
assert(sb && spref && level > 0); assert(sb && sp && level > 0);
#ifndef NDEBUG
if (spellbook_get(sb, spellref_get(spref))) {
log_error("duplicate spell in spellbook '%s': '%s'\n", sb->name, spref->name);
}
#endif
sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry));
sbe->spref = spellref_copy(spref);
sbe->level = level;
selist_push(&sb->spells, sbe);
}
void spellbook_add_spell(spellbook *sb, spell *sp, int level)
{
spellbook_entry * sbe;
assert(sb && sp->sname && level > 0);
#ifndef NDEBUG #ifndef NDEBUG
if (spellbook_get(sb, sp)) { if (spellbook_get(sb, sp)) {
log_error("duplicate spell in spellbook '%s': '%s'\n", sb->name, sp->sname); log_error("duplicate spell in spellbook '%s': '%s'\n", sb->name, sp->sname);
} }
#endif #endif
sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry)); sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry));
// FIXME: create a better API to create spellref from spell sbe->sp = sp;
sbe->spref = spellref_create(sp->sname);
sbe->spref->sp = sp;
sbe->level = level; sbe->level = level;
selist_push(&sb->spells, sbe); selist_push(&sb->spells, sbe);
} }
@ -113,7 +95,6 @@ void spellbook_clear(spellbook *sb)
assert(sb); assert(sb);
for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
spellref_free(sbe->spref);
free(sbe); free(sbe);
} }
selist_free(sb->spells); selist_free(sb->spells);
@ -135,7 +116,7 @@ int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *),
return 0; return 0;
} }
spellbook_entry * spellbook_get(spellbook *sb, const struct spell * sp) spellbook_entry * spellbook_get(spellbook *sb, const struct spell *sp)
{ {
if (sb) { if (sb) {
selist *ql; selist *ql;
@ -143,7 +124,7 @@ spellbook_entry * spellbook_get(spellbook *sb, const struct spell * sp)
for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
if (sp == spellref_get(sbe->spref)) { if (sbe->sp==sp) {
return sbe; return sbe;
} }
} }

View File

@ -29,7 +29,7 @@ extern "C" {
struct selist; struct selist;
typedef struct spellbook_entry { typedef struct spellbook_entry {
struct spellref * spref; struct spell *sp;
int level; int level;
} spellbook_entry; } spellbook_entry;
@ -44,11 +44,10 @@ extern "C" {
void read_spellbook(struct spellbook **bookp, struct gamedata *data, int(*get_level)(const struct spell * sp, void *), void * cbdata); 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_spellbook(const struct spellbook *book, struct storage *store);
void spellbook_add(spellbook *sbp, struct spellref *spref, int level); void spellbook_add(spellbook *sbp, struct spell *sp, int level);
void spellbook_add_spell(spellbook *sb, 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);
spellbook_entry * spellbook_get(spellbook *sb, const struct spell * sp); spellbook_entry * spellbook_get(spellbook *sb, const struct spell *sp);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -79,7 +79,7 @@ static spellref *xml_spellref(xmlNode * node, const char *name)
{ {
xmlChar *propValue = xmlGetProp(node, BAD_CAST name); xmlChar *propValue = xmlGetProp(node, BAD_CAST name);
if (propValue != NULL) { if (propValue != NULL) {
spellref *ref = spellref_create((const char *)propValue); spellref *ref = spellref_create(NULL, (const char *)propValue);
xmlFree(propValue); xmlFree(propValue);
return ref; return ref;
} }
@ -2075,10 +2075,10 @@ void register_xmlreader(void)
xml_register_callback(parse_buildings); /* requires resources */ xml_register_callback(parse_buildings); /* requires resources */
xml_register_callback(parse_ships); /* requires terrains */ xml_register_callback(parse_ships); /* requires terrains */
xml_register_callback(parse_races); /* requires spells */ xml_register_callback(parse_races);
xml_register_callback(parse_spellbooks); /* requires spells */
xml_register_callback(parse_equipment); /* requires spells */
xml_register_callback(parse_spells); /* requires resources */
xml_register_callback(parse_calendar); xml_register_callback(parse_calendar);
xml_register_callback(parse_spells); /* requires resources */
xml_register_callback(parse_equipment); /* requires spells */
xml_register_callback(parse_spellbooks); /* requires spells */
} }
#endif #endif

View File

@ -3458,8 +3458,8 @@ static void copy_spells(const spellbook * src, spellbook * dst, int maxlevel)
for (qi = 0, ql = src->spells; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0, ql = src->spells; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry * sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry * sbe = (spellbook_entry *)selist_get(ql, qi);
if (sbe->level <= maxlevel) { if (sbe->level <= maxlevel) {
if (!spellbook_get(dst, spellref_get(sbe->spref))) { if (!spellbook_get(dst, sbe->sp)) {
spellbook_add(dst, sbe->spref, sbe->level); spellbook_add(dst, sbe->sp, sbe->level);
} }
} }
} }

View File

@ -430,14 +430,13 @@ void show_new_spells(faction * f, int level, const spellbook *book)
for (qi = 0; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
if (sbe->level <= level) { if (sbe->level <= level) {
spell *sp = spellref_get(sbe->spref); if (!already_seen(f, sbe->sp)) {
if (!already_seen(f, sp)) {
attrib * a = a_new(&at_reportspell); attrib * a = a_new(&at_reportspell);
spellbook_entry * entry = (spellbook_entry *)a->data.v; spellbook_entry * entry = (spellbook_entry *)a->data.v;
entry->level = sbe->level; entry->level = sbe->level;
entry->spref = spellref_copy(sbe->spref); entry->sp = sbe->sp;
a_add(&f->attribs, a); a_add(&f->attribs, a);
a_add(&f->attribs, a_new(&at_seenspell))->data.v = sp; a_add(&f->attribs, a_new(&at_seenspell))->data.v = sbe->sp;
} }
} }
} }
@ -482,8 +481,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells
sbe = 0; sbe = 0;
} }
else { else {
spell *sp = spellref_get(sbe->spref); if (f->spellbook && spellbook_get(f->spellbook, sbe->sp)) {
if (f->spellbook && spellbook_get(f->spellbook, sp)) {
// already have this spell, remove it from the list of candidates // already have this spell, remove it from the list of candidates
commonspells[spellno] = commonspells[--numspells]; commonspells[spellno] = commonspells[--numspells];
if (maxspell > numspells) { if (maxspell > numspells) {
@ -498,7 +496,7 @@ void pick_random_spells(faction * f, int level, spellbook * book, int num_spells
if (!f->spellbook) { if (!f->spellbook) {
f->spellbook = create_spellbook(0); f->spellbook = create_spellbook(0);
} }
spellbook_add(f->spellbook, sbe->spref, sbe->level); spellbook_add(f->spellbook, sbe->sp, sbe->level);
commonspells[spellno] = commonspells[--numspells]; commonspells[spellno] = commonspells[--numspells];
} }
} }
@ -2971,15 +2969,14 @@ static void select_spellbook(void **tokens, spellbook *sb, const struct locale *
for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) { for (qi = 0, ql = sb->spells; ql; selist_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi); spellbook_entry *sbe = (spellbook_entry *)selist_get(ql, qi);
spell *sp = spellref_get(sbe->spref);
const char *n = spell_name(sp, lang); const char *n = spell_name(sbe->sp, lang);
if (!n) { if (!n) {
log_error("no translation in locale %s for spell %s\n", locale_name(lang), sp->sname); log_error("no translation in locale %s for spell %s\n", locale_name(lang), sbe->sp->sname);
} }
else { else {
variant token; variant token;
token.v = sp; token.v = sbe->sp;
addtoken((struct tnode **)tokens, n, token); addtoken((struct tnode **)tokens, n, token);
} }
} }

View File

@ -255,7 +255,7 @@ void nr_spell_syntax(struct stream *out, spellbook_entry * sbe, const struct loc
char buf[4096]; char buf[4096];
char *bufp = buf; char *bufp = buf;
size_t size = sizeof(buf) - 1; size_t size = sizeof(buf) - 1;
const spell * sp = spellref_get(sbe->spref); const spell * sp = sbe->sp;
const char *params = sp->parameter; const char *params = sp->parameter;
if (sp->sptyp & ISCOMBATSPELL) { if (sp->sptyp & ISCOMBATSPELL) {
@ -445,7 +445,7 @@ void nr_spell(struct stream *out, spellbook_entry * sbe, const struct locale *la
char buf[4096]; char buf[4096];
char *startp, *bufp = buf; char *startp, *bufp = buf;
size_t size = sizeof(buf) - 1; size_t size = sizeof(buf) - 1;
const spell * sp = spellref_get(sbe->spref); const spell * sp = sbe->sp;
newline(out); newline(out);
centre(out, spell_name(sp, lang), true); centre(out, spell_name(sp, lang), true);

View File

@ -681,7 +681,7 @@ size_t size)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
// TODO: no need to deref the spellref here (spref->name is good) // TODO: no need to deref the spellref here (spref->name is good)
bufp = STRLCPY(bufp, spell_name(spellref_get(sbe->spref), f->locale), size); bufp = STRLCPY(bufp, spell_name(sbe->sp, f->locale), size);
} }
} }