server/src/kernel/spell.c

128 lines
3.2 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include <kernel/config.h>
#include "spell.h"
/* kernel includes */
#include "magic.h"
#include "unit.h"
/* libc includes */
#include <assert.h>
#include <string.h>
/* util includes */
#include <util/goodies.h>
#include <util/language.h>
#include <util/log.h>
#include <util/umlaut.h>
#include <util/quicklist.h>
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
quicklist *spells = NULL;
2010-08-08 10:06:34 +02:00
spell * create_spell(const char * name)
{
spell * sp = (spell *) calloc(1, sizeof(spell));
sp->sname = strdup(name);
return sp;
}
2011-03-07 08:02:35 +01:00
void register_spell(spell * sp)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
if (sp->id == 0) {
2010-08-08 10:06:34 +02:00
sp->id = hashstring(sp->sname);
}
add_spell(&spells, sp);
2010-08-08 10:06:34 +02:00
}
spell *find_spell(const char *name)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
quicklist *ql = spells;
int qi;
2011-03-07 08:02:35 +01:00
for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
spell *sp = (spell *) ql_get(ql, qi);
if (strcmp(name, sp->sname) == 0) {
return sp;
2010-08-08 10:06:34 +02:00
}
}
log_warning(("find_spell: could not find spell '%s'\n", name));
return 0;
2010-08-08 10:06:34 +02:00
}
/* ------------------------------------------------------------- */
/* Spruch identifizieren */
spell *get_spellfromtoken(sc_mage *mage, const char *name,
2011-03-07 08:02:35 +01:00
const struct locale * lang)
2010-08-08 10:06:34 +02:00
{
variant token;
struct spell_names * names = mage->spellnames;
for (;names;names=names->next) {
if (names->lang==lang) break;
}
if (!names) {
quicklist *ql = mage->spells;
int qi;
names = (spell_names *)calloc(1, sizeof(spell_names));
names->next = mage->spellnames;
names->lang = lang;
names->tokens = (tnode *)calloc(1, sizeof(tnode));
for (qi = 0, ql = mage->spells; ql; ql_advance(&ql, &qi, 1)) {
spell *sp = (spell *) ql_get(ql, qi);
const char *n = spell_name(sp, lang);
token.v = sp;
addtoken(names->tokens, n, token);
2010-08-08 10:06:34 +02:00
}
mage->spellnames = names;
2010-08-08 10:06:34 +02:00
}
if (findtoken(names->tokens, name, &token) != E_TOK_NOMATCH) {
return (spell *) token.v;
}
return 0;
2010-08-08 10:06:34 +02:00
}
spell *find_spellbyid(unsigned int id)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
quicklist *ql;
int qi;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
assert(id >= 0);
if (id == 0)
return NULL;
for (qi = 0, ql = spells; ql; ql_advance(&ql, &qi, 1)) {
spell *sp = (spell *) ql_get(ql, qi);
if (sp->id == id) {
return sp;
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
for (qi = 0, ql = spells; ql; ql_advance(&ql, &qi, 1)) {
spell *sp = (spell *) ql_get(ql, qi);
2010-08-08 10:06:34 +02:00
unsigned int hashid = hashstring(sp->sname);
2011-03-07 08:02:35 +01:00
if (hashid == id) {
return sp;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
log_warning(("cannot find spell by id: %u\n", id));
return NULL;
}