add a callbacks module.

make last-chance spell function in lua for spells with none.
This commit is contained in:
Enno Rehling 2017-04-30 18:18:47 +02:00
parent 74673f172f
commit edd3b36a9f
8 changed files with 63 additions and 18 deletions

View File

@ -92,6 +92,20 @@ TOLUA_PKG(locale);
TOLUA_PKG(log); TOLUA_PKG(log);
TOLUA_PKG(game); TOLUA_PKG(game);
int tolua_toid(lua_State * L, int idx, int def)
{
int no = 0;
int type = lua_type(L, idx);
if (type == LUA_TNUMBER) {
no = (int)tolua_tonumber(L, idx, def);
}
else {
const char *str = tolua_tostring(L, idx, NULL);
no = str ? atoi36(str) : def;
}
return no;
}
int log_lua_error(lua_State * L) int log_lua_error(lua_State * L)
{ {
const char *error = lua_tostring(L, -1); const char *error = lua_tostring(L, -1);

View File

@ -18,6 +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_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

@ -455,20 +455,6 @@ use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord)
return result; return result;
} }
int tolua_toid(lua_State * L, int idx, int def)
{
int no = 0;
int type = lua_type(L, idx);
if (type == LUA_TNUMBER) {
no = (int)tolua_tonumber(L, idx, def);
}
else {
const char *str = tolua_tostring(L, idx, NULL);
no = str ? atoi36(str) : def;
}
return no;
}
void register_tolua_helpers(void) void register_tolua_helpers(void)
{ {
at_register(&at_direction); at_register(&at_direction);

View File

@ -16,7 +16,6 @@ extern "C" {
struct lua_State; struct lua_State;
void register_tolua_helpers(void); void register_tolua_helpers(void);
int tolua_toid(struct lua_State *L, int idx, int def);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -6,6 +6,7 @@ alliance.test.c
ally.test.c ally.test.c
build.test.c build.test.c
building.test.c building.test.c
# callbacks.test.c
command.test.c command.test.c
config.test.c config.test.c
# connection.test.c # connection.test.c
@ -38,6 +39,7 @@ alliance.c
ally.c ally.c
build.c build.c
building.c building.c
callbacks.c
command.c command.c
config.c config.c
connection.c connection.c

4
src/kernel/callbacks.c Normal file
View File

@ -0,0 +1,4 @@
#include <platform.h>
#include "callbacks.h"
struct callbacks callbacks;

35
src/kernel/callbacks.h Normal file
View File

@ -0,0 +1,35 @@
/*
Copyright (c) 1998-2015, 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.
**/
#ifndef H_KRNL_CALLBACKS_H
#define H_KRNL_CALLBACKS_H
#ifdef __cplusplus
extern "C" {
#endif
struct castorder;
extern struct callbacks {
int (*cast_spell)(struct castorder *co);
} callbacks;
#ifdef __cplusplus
}
#endif
#endif /* H_KRNL_CALLBACKS_H */

View File

@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/ally.h> #include <kernel/ally.h>
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/callbacks.h>
#include <kernel/curse.h> #include <kernel/curse.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h> #include <kernel/item.h>
@ -3006,7 +3007,10 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan
int cast_spell(struct castorder *co) int cast_spell(struct castorder *co)
{ {
const spell *sp = co->sp; const spell *sp = co->sp;
if (sp->cast_fun) {
return sp->cast_fun(co); return sp->cast_fun(co);
}
return callbacks.cast_spell(co);
} }
static critbit_tree cb_spellbooks; static critbit_tree cb_spellbooks;