2012-05-23 21:42:14 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
2012-05-24 05:22:12 +02:00
|
|
|
#include <kernel/spell.h>
|
2012-05-31 04:55:17 +02:00
|
|
|
#include <quicklist.h>
|
2012-05-27 18:52:44 +02:00
|
|
|
#include <util/log.h>
|
2012-05-23 21:42:14 +02:00
|
|
|
|
|
|
|
#include "spellbook.h"
|
|
|
|
|
2012-05-25 06:57:23 +02:00
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-05-25 06:57:23 +02:00
|
|
|
|
2012-05-24 05:22:12 +02:00
|
|
|
spellbook * create_spellbook(const char * name)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
spellbook *result = (spellbook *)malloc(sizeof(spellbook));
|
|
|
|
result->name = name ? _strdup(name) : 0;
|
|
|
|
result->spells = 0;
|
|
|
|
return result;
|
2012-05-24 05:22:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void spellbook_add(spellbook *sb, struct spell * sp, int level)
|
2012-05-23 21:42:14 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
spellbook_entry * sbe;
|
2012-05-24 05:22:12 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
assert(sb && sp && level > 0);
|
2012-05-27 18:52:44 +02:00
|
|
|
#ifndef NDEBUG
|
2015-01-30 20:37:14 +01:00
|
|
|
if (spellbook_get(sb, sp)) {
|
|
|
|
log_error("duplicate spell in spellbook '%s': '%s'\n", sb->name, sp->sname);
|
|
|
|
}
|
2012-05-27 18:52:44 +02:00
|
|
|
#endif
|
2015-01-30 20:37:14 +01:00
|
|
|
sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry));
|
|
|
|
sbe->sp = sp;
|
|
|
|
sbe->level = level;
|
|
|
|
ql_push(&sb->spells, sbe);
|
2012-05-23 21:42:14 +02:00
|
|
|
}
|
|
|
|
|
2012-05-25 06:57:23 +02:00
|
|
|
void spellbook_clear(spellbook *sb)
|
2012-05-23 21:42:14 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
2012-05-23 21:42:14 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
assert(sb);
|
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi);
|
|
|
|
free(sbe);
|
|
|
|
}
|
|
|
|
ql_free(sb->spells);
|
|
|
|
free(sb->name);
|
2012-05-23 21:42:14 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
int spellbook_foreach(spellbook *sb, int(*callback)(spellbook_entry *, void *), void * data)
|
2012-05-23 21:42:14 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
2012-05-23 21:42:14 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi);
|
|
|
|
int result = callback(sbe, data);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
2012-05-23 21:42:14 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2012-05-23 21:42:14 +02:00
|
|
|
}
|
2012-05-24 05:22:12 +02:00
|
|
|
|
2012-05-26 04:55:47 +02:00
|
|
|
spellbook_entry * spellbook_get(spellbook *sb, const struct spell * sp)
|
2012-05-24 05:22:12 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (sb) {
|
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
2012-05-24 05:22:12 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
spellbook_entry *sbe = (spellbook_entry *)ql_get(ql, qi);
|
|
|
|
if (sp == sbe->sp) {
|
|
|
|
return sbe;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 05:22:12 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return 0;
|
2012-05-24 05:22:12 +02:00
|
|
|
}
|
|
|
|
|