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 "faction.h"
#include "race.h"
#include "spellbook.h"
#include "spell.h"
/* util includes */
#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->spellbook) {
eq->spellbook = create_spellbook(0);
}
spellbook_add(eq->spellbook, sp, level);
lazy_spell *ls = malloc(sizeof(lazy_spell));
ls->sp = NULL;
ls->level = 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 (eq->spellbook) {
quicklist * ql = eq->spellbook->spells;
if (eq->spells) {
quicklist * ql = eq->spells;
int qi;
sc_mage * mage = get_mage(u);
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);
}
}
@ -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) {
equipment **eqp = &equipment_sets;
while (*eqp) {
@ -231,9 +249,9 @@ void equipment_done(void) {
equipment *eq = *eqp;
*eqp = eq->next;
free(eq->name);
if (eq->spellbook) {
spellbook_clear(eq->spellbook);
free(eq->spellbook);
if (eq->spells) {
ql_foreach(eq->spells, free_ls);
ql_free(eq->spells);
}
while (eq->items) {
itemdata *next = eq->items->next;

View File

@ -48,7 +48,7 @@ extern "C" {
char *name;
struct itemdata *items;
char *skills[MAXSKILLS];
struct spellbook *spellbook;
struct quicklist *spells;
struct subset *subsets;
struct equipment *next;
void(*callback) (const struct equipment *, struct unit *);
@ -63,7 +63,7 @@ extern "C" {
const struct item_type *itype, const char *value);
void equipment_setskill(struct equipment *eq, skill_t sk,
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(*callback) (const struct equipment *, struct unit *));

View File

@ -34,7 +34,7 @@ void test_equipment(CuTest * tc)
equipment_setitem(eq, it_horses, "1");
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));
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) {
xmlNodePtr node = nsetItems->nodeTab[i];
xmlChar *propValue;
struct spell *sp;
int level;
const char *name;
propValue = xmlGetProp(node, BAD_CAST "name");
assert(propValue != NULL);
sp = find_spell((const char *)propValue);
if (!sp) {
log_error("no spell '%s' for equipment-set '%s'\n", (const char *)propValue, eq->name);
name = (const char *)propValue;
level = xml_ivalue(node, "level", 0);
if (level > 0) {
equipment_addspell(eq, name, level);
}
else {
int level = xml_ivalue(node, "level", 0);
if (level > 0) {
equipment_addspell(eq, sp, level);
}
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);
}
@ -1331,7 +1327,7 @@ static int parse_equipment(xmlDocPtr doc)
xmlXPathFreeObject(xpathResult);
xpathResult = xmlXPathEvalExpression(BAD_CAST "spell", xpath);
assert(!eq->spellbook);
assert(!eq->spells);
add_spells(eq, xpathResult->nodesetval);
xmlXPathFreeObject(xpathResult);