remove the funcpointer from struct spell.

This commit is contained in:
Enno Rehling 2017-04-30 22:15:18 +02:00
parent 976b23be72
commit 80d696d02a
11 changed files with 63 additions and 78 deletions

View File

@ -210,7 +210,7 @@
<string name="concealing_aura"> <string name="concealing_aura">
<text locale="de">Dieser Zauber wird die gesamte Ausrüstung der Zieleinheit für einige Zeit vor den Blicken anderer verschleiern. Der Zauber schützt nicht vor Dieben und Spionen.</text> <text locale="de">Dieser Zauber wird die gesamte Ausrüstung der Zieleinheit für einige Zeit vor den Blicken anderer verschleiern. Der Zauber schützt nicht vor Dieben und Spionen.</text>
<text locale="en">This spell will hide the whole equipment of a target unit from the looks of others. It will not protect against thieves or spies.</text> <text locale="en">This spell will hide the whole equipment of a target unit from the looks of others. It will not protect against thieves or spies.</text>
</string> </string>
<string name="raindance"> <string name="raindance">
<text locale="de">Durch dieses uralte Tanzritual ruft der Zauberkundige die Kräfte des Lebens und der Fruchtbarkeit an. Die darauf folgenden Regenfälle begünstigen das Wachstum und erhöhen die Ernteerträge einiger Bauern der Region bis der Regen wieder nachlässt.</text> <text locale="de">Durch dieses uralte Tanzritual ruft der Zauberkundige die Kräfte des Lebens und der Fruchtbarkeit an. Die darauf folgenden Regenfälle begünstigen das Wachstum und erhöhen die Ernteerträge einiger Bauern der Region bis der Regen wieder nachlässt.</text>
<text locale="en">This ancient rite calls upon the forces of life and fertility. For the next few weeks, the peasants' harvest will be extraordinary good.</text> <text locale="en">This ancient rite calls upon the forces of life and fertility. For the next few weeks, the peasants' harvest will be extraordinary good.</text>

View File

@ -18,7 +18,7 @@ extern "C" {
struct _dictionary_; struct _dictionary_;
struct selist; struct selist;
int tolua_toid(struct lua_State * L, int idx, int def); int tolua_toid(struct lua_State *L, int idx, int def);
int tolua_sqlite_open(struct lua_State *L); int tolua_sqlite_open(struct lua_State *L);
int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d);
int tolua_itemlist_next(struct lua_State *L); int tolua_itemlist_next(struct lua_State *L);

View File

@ -23,6 +23,7 @@ without prior permission by the authors of Eressea.
#include <util/parser.h> #include <util/parser.h>
#include <util/resolve.h> #include <util/resolve.h>
#include <kernel/callbacks.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/callbacks.h> #include <kernel/callbacks.h>
#include <kernel/equipment.h> #include <kernel/equipment.h>
@ -452,21 +453,16 @@ void register_tolua_helpers(void)
callbacks.cast_spell = lua_callspell; callbacks.cast_spell = lua_callspell;
register_function((pf_generic)lua_initfamiliar, register_function((pf_generic)lua_initfamiliar, "lua_initfamiliar");
TOLUA_CAST "lua_initfamiliar"); register_function((pf_generic)lua_getresource, "lua_getresource");
register_function((pf_generic)lua_getresource, register_function((pf_generic)lua_changeresource, "lua_changeresource");
TOLUA_CAST "lua_getresource"); register_function((pf_generic)lua_equipmentcallback, "lua_equip");
register_function((pf_generic)lua_changeresource,
TOLUA_CAST "lua_changeresource");
register_function((pf_generic)lua_equipmentcallback,
TOLUA_CAST "lua_equip");
register_function((pf_generic)lua_wage, TOLUA_CAST "lua_wage"); register_function((pf_generic)lua_wage, "lua_wage");
register_function((pf_generic)lua_maintenance, register_function((pf_generic)lua_maintenance, "lua_maintenance");
TOLUA_CAST "lua_maintenance");
item_use_fun = use_item_lua; item_use_fun = use_item_lua;
res_produce_fun = produce_resource_lua; res_produce_fun = produce_resource_lua;
res_limit_fun = limit_resource_lua; res_limit_fun = limit_resource_lua;
register_item_give(lua_giveitem, TOLUA_CAST "lua_giveitem"); register_item_give(lua_giveitem, "lua_giveitem");
} }

View File

@ -15,6 +15,7 @@ extern "C" {
#endif #endif
struct lua_State; struct lua_State;
void register_tolua_helpers(void); void register_tolua_helpers(void);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -594,9 +594,6 @@ static void json_spells(cJSON *json) {
if (strcmp(item->string, "index") == 0) { if (strcmp(item->string, "index") == 0) {
continue; continue;
} }
else if (strcmp(item->string, "cast") == 0) {
sp->cast_fun = (spell_f)get_function(item->valuestring);
}
else if (strcmp(item->string, "syntax") == 0) { else if (strcmp(item->string, "syntax") == 0) {
sp->syntax = strdup(item->valuestring); sp->syntax = strdup(item->valuestring);
} }

View File

@ -33,7 +33,30 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static critbit_tree cb_fumbles;
static critbit_tree cb_spell_fun;
void add_spellcast(const char *sname, spell_f fun)
{
size_t len;
char data[64];
len = cb_new_kv(sname, strlen(sname), &fun, sizeof(fun), data);
assert(len <= sizeof(data));
cb_insert(&cb_spell_fun, data, len);
}
spell_f get_spellcast(const char *sname)
{
void * match;
spell_f result = NULL;
if (cb_find_prefix(&cb_spell_fun, sname, strlen(sname) + 1, &match, 1, 0)) {
cb_get_kv(match, &result, sizeof(result));
}
return result;
}
static critbit_tree cb_fumble_fun;
void add_fumble(const char *sname, fumble_f fun) void add_fumble(const char *sname, fumble_f fun)
{ {
size_t len; size_t len;
@ -41,7 +64,7 @@ void add_fumble(const char *sname, fumble_f fun)
len = cb_new_kv(sname, strlen(sname), &fun, sizeof(fun), data); len = cb_new_kv(sname, strlen(sname), &fun, sizeof(fun), data);
assert(len <= sizeof(data)); assert(len <= sizeof(data));
cb_insert(&cb_fumbles, data, len); cb_insert(&cb_fumble_fun, data, len);
} }
fumble_f get_fumble(const char *sname) fumble_f get_fumble(const char *sname)
@ -49,7 +72,7 @@ fumble_f get_fumble(const char *sname)
void * match; void * match;
fumble_f result = NULL; fumble_f result = NULL;
if (cb_find_prefix(&cb_fumbles, sname, strlen(sname) + 1, &match, 1, 0)) { if (cb_find_prefix(&cb_fumble_fun, sname, strlen(sname) + 1, &match, 1, 0)) {
cb_get_kv(match, &result, sizeof(result)); cb_get_kv(match, &result, sizeof(result));
} }
return result; return result;
@ -71,7 +94,8 @@ static void free_spell_cb(void *cbdata) {
} }
void free_spells(void) { void free_spells(void) {
cb_clear(&cb_fumbles); cb_clear(&cb_fumble_fun);
cb_clear(&cb_spell_fun);
cb_clear(&cb_spells); cb_clear(&cb_spells);
selist_foreach(spells, free_spell_cb); selist_foreach(spells, free_spell_cb);
selist_free(spells); selist_free(spells);

View File

@ -41,7 +41,6 @@ extern "C" {
int sptyp; int sptyp;
int rank; /* Reihenfolge der Zauber */ int rank; /* Reihenfolge der Zauber */
struct spell_component *components; struct spell_component *components;
spell_f cast_fun;
} spell; } spell;
typedef struct spellref { typedef struct spellref {
@ -52,6 +51,9 @@ extern "C" {
void add_fumble(const char *sname, fumble_f fun); void add_fumble(const char *sname, fumble_f fun);
fumble_f get_fumble(const char *sname); fumble_f get_fumble(const char *sname);
void add_spellcast(const char *sname, spell_f fun);
spell_f get_spellcast(const char *sname);
struct spellref *spellref_create(struct spell *sp, const char *name); struct spellref *spellref_create(struct spell *sp, const char *name);
void spellref_free(struct spellref *spref); void spellref_free(struct spellref *spref);
struct spell *spellref_get(struct spellref *spref); struct spell *spellref_get(struct spellref *spref);

View File

@ -279,11 +279,8 @@ parse_function(xmlNodePtr node, pf_generic * funPtr, xmlChar ** namePtr)
xmlChar *propValue = xmlGetProp(node, BAD_CAST "value"); xmlChar *propValue = xmlGetProp(node, BAD_CAST "value");
assert(propValue != NULL); assert(propValue != NULL);
fun = get_function((const char *)propValue); fun = get_function((const char *)propValue);
if (fun != NULL) { xmlFree(propValue);
xmlFree(propValue); propValue = xmlGetProp(node, BAD_CAST "name");
propValue = xmlGetProp(node, BAD_CAST "name");
}
*namePtr = propValue; *namePtr = propValue;
*funPtr = fun; *funPtr = fun;
return 0; return 0;
@ -1311,7 +1308,6 @@ static int parse_spellbooks(xmlDocPtr doc)
static int parse_spells(xmlDocPtr doc) static int parse_spells(xmlDocPtr doc)
{ {
pf_generic cast = 0;
xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathContextPtr xpath = xmlXPathNewContext(doc);
xmlXPathObjectPtr spells; xmlXPathObjectPtr spells;
@ -1388,44 +1384,9 @@ static int parse_spells(xmlDocPtr doc)
sp->sptyp |= REGIONSPELL; sp->sptyp |= REGIONSPELL;
k = xml_ivalue(node, "combat", 0); k = xml_ivalue(node, "combat", 0);
if (k >= 0 && k <= 3) if (k >= 0 && k <= 3) {
sp->sptyp |= modes[k]; sp->sptyp |= modes[k];
/* reading eressea/spells/spell/function */
xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "function", xpath);
if (result->nodesetval->nodeNr == 0) {
cast = get_function(sp->sname);
if (!cast) {
log_info("no spell cast function registered for '%s'\n", sp->sname);
}
} }
else {
for (k = 0; k != result->nodesetval->nodeNr; ++k) {
xmlNodePtr node = result->nodesetval->nodeTab[k];
pf_generic fun;
parse_function(node, &fun, &propValue);
assert(propValue != NULL);
if (strcmp((const char *)propValue, "cast") == 0) {
if (fun) {
cast = fun;
}
else {
log_error("unknown function name '%s' for spell '%s'\n", (const char *)propValue, sp->sname);
}
}
else {
log_error("unknown function type '%s' for spell '%s'\n", (const char *)propValue, sp->sname);
}
xmlFree(propValue);
}
}
sp->cast_fun = (spell_f)cast;
xmlXPathFreeObject(result);
/* reading eressea/spells/spell/resource */ /* reading eressea/spells/spell/resource */
xpath->node = node; xpath->node = node;
result = xmlXPathEvalExpression(BAD_CAST "resource", xpath); result = xmlXPathEvalExpression(BAD_CAST "resource", xpath);

View File

@ -23,6 +23,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "skill.h" #include "skill.h"
#include "study.h" #include "study.h"
#include "helpers.h"
#include "laws.h" #include "laws.h"
#include <kernel/ally.h> #include <kernel/ally.h>
@ -3009,12 +3010,9 @@ int cast_spell(struct castorder *co)
const char *fname = co->sp->sname; const char *fname = co->sp->sname;
const char *hashpos = strchr(fname, '#'); const char *hashpos = strchr(fname, '#');
char fbuf[64]; char fbuf[64];
spell_f fun;
const spell *sp = co->sp; const spell *sp = co->sp;
if (sp->cast_fun) {
return sp->cast_fun(co);
}
if (hashpos != NULL) { if (hashpos != NULL) {
ptrdiff_t len = hashpos - fname; ptrdiff_t len = hashpos - fname;
assert(len < (ptrdiff_t) sizeof(fbuf)); assert(len < (ptrdiff_t) sizeof(fbuf));
@ -3023,7 +3021,12 @@ int cast_spell(struct castorder *co)
fname = fbuf; fname = fbuf;
} }
return callbacks.cast_spell(co, fname); fun = get_spellcast(sp->sname);
if (!fun) {
log_warning("no spell function for %s, try callback", sp->sname);
return callbacks.cast_spell(co, fname);
}
return fun(co);
} }
static critbit_tree cb_spellbooks; static critbit_tree cb_spellbooks;

View File

@ -377,8 +377,8 @@ void test_multi_cast(CuTest *tc) {
struct locale * lang; struct locale * lang;
test_setup(); test_setup();
add_spellcast("fireball", cast_fireball);
sp = create_spell("fireball", 0); sp = create_spell("fireball", 0);
sp->cast_fun = cast_fireball;
CuAssertPtrEquals(tc, sp, find_spell("fireball")); CuAssertPtrEquals(tc, sp, find_spell("fireball"));
lang = test_create_locale(); lang = test_create_locale();

View File

@ -6615,12 +6615,10 @@ static spelldata spell_functions[] = {
static void register_spelldata(void) static void register_spelldata(void)
{ {
int i; int i;
char zText[32];
strcpy(zText, "fumble_");
for (i = 0; spell_functions[i].sname; ++i) { for (i = 0; spell_functions[i].sname; ++i) {
spelldata *data = spell_functions + i; spelldata *data = spell_functions + i;
if (data->cast) { if (data->cast) {
register_function((pf_generic)data->cast, data->sname); add_spellcast(data->sname, data->cast);
} }
if (data->fumble) { if (data->fumble) {
add_fumble(data->sname, data->fumble); add_fumble(data->sname, data->fumble);
@ -6733,11 +6731,14 @@ void register_spells(void)
ct_register(&ct_firewall); ct_register(&ct_firewall);
ct_register(&ct_deathcloud); ct_register(&ct_deathcloud);
register_function((pf_generic)sp_blessedharvest, "cast_blessedharvest"); add_spellcast("blabbermouth", sp_babbler);
register_function((pf_generic)sp_summon_familiar, "cast_familiar"); add_spellcast("summon_familiar", sp_summon_familiar);
register_function((pf_generic)sp_babbler, "cast_babbler"); add_spellcast("meteor_rain", sp_kampfzauber);
register_function((pf_generic)sp_readmind, "cast_readmind"); add_spellcast("fireball", sp_kampfzauber);
register_function((pf_generic)sp_kampfzauber, "combat_spell"); add_spellcast("hail", sp_kampfzauber);
add_spellcast("readmind", sp_readmind);
add_spellcast("blessedharvest", sp_blessedharvest);
add_spellcast("raindance", sp_blessedharvest);
register_spelldata(); register_spelldata();