testing lazy find_spell calls for equipment configuration

This commit is contained in:
Enno Rehling 2016-09-24 17:29:55 +02:00
parent 423e293745
commit 42c44724f8
4 changed files with 41 additions and 27 deletions

View File

@ -25,7 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "unit.h" #include "unit.h"
#include "faction.h" #include "faction.h"
#include "race.h" #include "race.h"
#include "spellbook.h" #include "spell.h"
/* util includes */ /* util includes */
#include <quicklist.h> #include <quicklist.h>
@ -86,13 +86,20 @@ void equipment_setskill(equipment * eq, skill_t sk, const char *value)
} }
} }
void equipment_addspell(equipment * eq, struct spell * sp, int level) typedef struct lazy_spell {
char *name;
struct spell *sp;
int level;
} lazy_spell;
void equipment_addspell(equipment * eq, const char * name, int level)
{ {
if (eq) { if (eq) {
if (!eq->spellbook) { lazy_spell *ls = malloc(sizeof(lazy_spell));
eq->spellbook = create_spellbook(0); ls->sp = NULL;
} ls->level = level;
spellbook_add(eq->spellbook, sp, level); ls->name = _strdup(name);
ql_push(&eq->spells, ls);
} }
} }
@ -148,13 +155,18 @@ void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask)
} }
if (mask & EQUIP_SPELLS) { if (mask & EQUIP_SPELLS) {
if (eq->spellbook) { if (eq->spells) {
quicklist * ql = eq->spellbook->spells; quicklist * ql = eq->spells;
int qi; int qi;
sc_mage * mage = get_mage(u); sc_mage * mage = get_mage(u);
for (qi = 0; ql; ql_advance(&ql, &qi, 1)) { for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi); lazy_spell *sbe = (lazy_spell *)ql_get(ql, qi);
if (!sbe->sp) {
sbe->sp = find_spell(sbe->name);
free(sbe->name);
sbe->name = NULL;
}
unit_add_spell(u, mage, sbe->sp, sbe->level); unit_add_spell(u, mage, sbe->sp, sbe->level);
} }
} }
@ -224,6 +236,12 @@ void equip_items(struct item **items, const struct equipment *eq)
} }
} }
void free_ls(void *arg) {
lazy_spell *ls = (lazy_spell*)arg;
free(ls->name);
free(ls);
}
void equipment_done(void) { void equipment_done(void) {
equipment **eqp = &equipment_sets; equipment **eqp = &equipment_sets;
while (*eqp) { while (*eqp) {
@ -231,9 +249,9 @@ void equipment_done(void) {
equipment *eq = *eqp; equipment *eq = *eqp;
*eqp = eq->next; *eqp = eq->next;
free(eq->name); free(eq->name);
if (eq->spellbook) { if (eq->spells) {
spellbook_clear(eq->spellbook); ql_foreach(eq->spells, free_ls);
free(eq->spellbook); ql_free(eq->spells);
} }
while (eq->items) { while (eq->items) {
itemdata *next = eq->items->next; itemdata *next = eq->items->next;

View File

@ -48,7 +48,7 @@ extern "C" {
char *name; char *name;
struct itemdata *items; struct itemdata *items;
char *skills[MAXSKILLS]; char *skills[MAXSKILLS];
struct spellbook *spellbook; struct quicklist *spells;
struct subset *subsets; struct subset *subsets;
struct equipment *next; struct equipment *next;
void(*callback) (const struct equipment *, struct unit *); void(*callback) (const struct equipment *, struct unit *);
@ -63,7 +63,7 @@ extern "C" {
const struct item_type *itype, const char *value); const struct item_type *itype, const char *value);
void equipment_setskill(struct equipment *eq, skill_t sk, void equipment_setskill(struct equipment *eq, skill_t sk,
const char *value); const char *value);
void equipment_addspell(struct equipment *eq, struct spell *sp, int level); void equipment_addspell(struct equipment *eq, const char *name, int level);
void equipment_setcallback(struct equipment *eq, void equipment_setcallback(struct equipment *eq,
void(*callback) (const struct equipment *, struct unit *)); void(*callback) (const struct equipment *, struct unit *));

View File

@ -34,7 +34,7 @@ void test_equipment(CuTest * tc)
equipment_setitem(eq, it_horses, "1"); equipment_setitem(eq, it_horses, "1");
equipment_setskill(eq, SK_MAGIC, "5"); equipment_setskill(eq, SK_MAGIC, "5");
equipment_addspell(eq, sp, 1); equipment_addspell(eq, sp->sname, 1);
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
equip_unit_mask(u, eq, EQUIP_ALL); equip_unit_mask(u, eq, EQUIP_ALL);

View File

@ -1191,22 +1191,18 @@ static void add_spells(equipment * eq, xmlNodeSetPtr nsetItems)
for (i = 0; i != nsetItems->nodeNr; ++i) { for (i = 0; i != nsetItems->nodeNr; ++i) {
xmlNodePtr node = nsetItems->nodeTab[i]; xmlNodePtr node = nsetItems->nodeTab[i];
xmlChar *propValue; xmlChar *propValue;
struct spell *sp; int level;
const char *name;
propValue = xmlGetProp(node, BAD_CAST "name"); propValue = xmlGetProp(node, BAD_CAST "name");
assert(propValue != NULL); assert(propValue != NULL);
sp = find_spell((const char *)propValue); name = (const char *)propValue;
if (!sp) { level = xml_ivalue(node, "level", 0);
log_error("no spell '%s' for equipment-set '%s'\n", (const char *)propValue, eq->name);
}
else {
int level = xml_ivalue(node, "level", 0);
if (level > 0) { if (level > 0) {
equipment_addspell(eq, sp, level); equipment_addspell(eq, name, level);
} }
else { else {
log_error("spell '%s' for equipment-set '%s' has no level\n", sp->sname, eq->name); log_error("spell '%s' for equipment-set '%s' has no level\n", name, eq->name);
}
} }
xmlFree(propValue); xmlFree(propValue);
} }
@ -1331,7 +1327,7 @@ static int parse_equipment(xmlDocPtr doc)
xmlXPathFreeObject(xpathResult); xmlXPathFreeObject(xpathResult);
xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath); xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath);
assert(!eq->spellbook); assert(!eq->spells);
add_spells(eq, xpathResult->nodesetval); add_spells(eq, xpathResult->nodesetval);
xmlXPathFreeObject(xpathResult); xmlXPathFreeObject(xpathResult);