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>
|
|
|
|
#include <kernel/magic.h>
|
2012-05-23 21:42:14 +02:00
|
|
|
#include <util/quicklist.h>
|
|
|
|
|
|
|
|
#include "spellbook.h"
|
|
|
|
|
2012-05-25 06:57:23 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-05-24 05:22:12 +02:00
|
|
|
spellbook * create_spellbook(const char * name)
|
|
|
|
{
|
|
|
|
spellbook *result = (spellbook *)malloc(sizeof(spellbook));
|
2012-05-24 09:56:54 +02:00
|
|
|
result->name = name ? strdup(name) : 0;
|
2012-05-24 05:22:12 +02:00
|
|
|
result->spells = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spellbook_add(spellbook *sb, struct spell * sp, int level)
|
2012-05-23 21:42:14 +02:00
|
|
|
{
|
|
|
|
spellbook_entry * sbe = (spellbook_entry *)malloc(sizeof(spellbook_entry));
|
2012-05-24 05:22:12 +02:00
|
|
|
|
|
|
|
assert(sb);
|
2012-05-23 21:42:14 +02:00
|
|
|
sbe->sp = sp;
|
|
|
|
sbe->level = level;
|
2012-05-24 05:22:12 +02:00
|
|
|
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
|
|
|
{
|
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
|
|
|
|
2012-05-24 05:22:12 +02:00
|
|
|
assert(sb);
|
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
2012-05-23 21:42:14 +02:00
|
|
|
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
|
|
|
|
free(sbe);
|
|
|
|
}
|
2012-05-24 05:22:12 +02:00
|
|
|
ql_free(sb->spells);
|
2012-05-23 21:42:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int spellbook_foreach(spellbook *sb, int (*callback)(spellbook_entry *, void *), void * data)
|
|
|
|
{
|
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
|
|
|
|
2012-05-24 05:22:12 +02:00
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
2012-05-23 21:42:14 +02:00
|
|
|
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
|
|
|
|
int result = callback(sbe, data);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-24 05:22:12 +02:00
|
|
|
|
2012-05-24 09:56:54 +02:00
|
|
|
spellbook_entry * spellbook_get(spellbook *sb, struct spell * sp)
|
2012-05-24 05:22:12 +02:00
|
|
|
{
|
|
|
|
quicklist *ql;
|
|
|
|
int qi;
|
|
|
|
|
2012-05-25 05:35:13 +02:00
|
|
|
assert(sb);
|
2012-05-24 05:22:12 +02:00
|
|
|
for (qi = 0, ql = sb->spells; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
spellbook_entry *sbe = (spellbook_entry *) ql_get(ql, qi);
|
2012-05-24 09:56:54 +02:00
|
|
|
if (sp==sbe->sp) {
|
2012-05-24 05:22:12 +02:00
|
|
|
return sbe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|