From edd3b36a9fd6785ddc3507bc14077c2f13a03172 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 30 Apr 2017 18:18:47 +0200 Subject: [PATCH] add a callbacks module. make last-chance spell function in lua for spells with none. --- src/bindings.c | 14 ++++++++++++++ src/bindings.h | 1 + src/helpers.c | 14 -------------- src/helpers.h | 5 ++--- src/kernel/CMakeLists.txt | 2 ++ src/kernel/callbacks.c | 4 ++++ src/kernel/callbacks.h | 35 +++++++++++++++++++++++++++++++++++ src/magic.c | 6 +++++- 8 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 src/kernel/callbacks.c create mode 100644 src/kernel/callbacks.h diff --git a/src/bindings.c b/src/bindings.c index 1d6270b35..9ea548465 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -92,6 +92,20 @@ TOLUA_PKG(locale); TOLUA_PKG(log); 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) { const char *error = lua_tostring(L, -1); diff --git a/src/bindings.h b/src/bindings.h index 354bb199a..2275236cb 100755 --- a/src/bindings.h +++ b/src/bindings.h @@ -18,6 +18,7 @@ extern "C" { struct _dictionary_; struct selist; + int tolua_toid(struct lua_State * L, int idx, int def); int tolua_sqlite_open(struct lua_State *L); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_itemlist_next(struct lua_State *L); diff --git a/src/helpers.c b/src/helpers.c index 59695e9b4..924082bcd 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -455,20 +455,6 @@ use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord) 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) { at_register(&at_direction); diff --git a/src/helpers.h b/src/helpers.h index d77765187..5187f7bcf 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -14,9 +14,8 @@ without prior permission by the authors of Eressea. extern "C" { #endif - struct lua_State; - void register_tolua_helpers(void); - int tolua_toid(struct lua_State *L, int idx, int def); + struct lua_State; + void register_tolua_helpers(void); #ifdef __cplusplus } diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index b8423ae03..e572d0763 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -6,6 +6,7 @@ alliance.test.c ally.test.c build.test.c building.test.c +# callbacks.test.c command.test.c config.test.c # connection.test.c @@ -38,6 +39,7 @@ alliance.c ally.c build.c building.c +callbacks.c command.c config.c connection.c diff --git a/src/kernel/callbacks.c b/src/kernel/callbacks.c new file mode 100644 index 000000000..d8070bd04 --- /dev/null +++ b/src/kernel/callbacks.c @@ -0,0 +1,4 @@ +#include +#include "callbacks.h" + +struct callbacks callbacks; diff --git a/src/kernel/callbacks.h b/src/kernel/callbacks.h new file mode 100644 index 000000000..99db5e390 --- /dev/null +++ b/src/kernel/callbacks.h @@ -0,0 +1,35 @@ +/* +Copyright (c) 1998-2015, Enno Rehling +Katja Zedel + +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 */ diff --git a/src/magic.c b/src/magic.c index 602207c02..8db533923 100644 --- a/src/magic.c +++ b/src/magic.c @@ -27,6 +27,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include #include #include #include @@ -3006,7 +3007,10 @@ spell *unit_getspell(struct unit *u, const char *name, const struct locale * lan int cast_spell(struct castorder *co) { const spell *sp = co->sp; - return sp->cast_fun(co); + if (sp->cast_fun) { + return sp->cast_fun(co); + } + return callbacks.cast_spell(co); } static critbit_tree cb_spellbooks;