From 42783ff02c9aa0bf90df1f2e440919293ba1faee Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 5 May 2015 08:44:58 -0700 Subject: [PATCH] binding a magician's spellbook to Lua, with test. --- scripts/tests/study.lua | 17 ++++++++++++++++- src/bind_unit.c | 7 +++---- src/bindings.c | 32 ++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/scripts/tests/study.lua b/scripts/tests/study.lua index 7d27cc7d2..e5ab84018 100644 --- a/scripts/tests/study.lua +++ b/scripts/tests/study.lua @@ -7,7 +7,8 @@ function setup() "races" : { "human" : {} }, "terrains" : { "plain" : { "flags" : [ "land" ] } }, "keywords" : { "de" : { "study": "LERNEN" } }, - "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } } + "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } }, + "spells" : { "fireball" : { "syntax" : "u+" } } }]] eressea.game.reset() eressea.config.reset(); @@ -36,3 +37,17 @@ function test_study_expensive() assert_equal(1, u:get_skill("alchemy")) assert_equal(0, u:get_item("money")) end + +function test_unit_spells() + local r = region.create(0, 0, "plain") + local f = faction.create("test@example.com", "human", "de") + local u = unit.create(f, r, 1) + u.magic = "gray" + u:set_skill("magic", 1) + u:add_spell("toast") + assert_equal(nil, u.spells) + u:add_spell("fireball", 2) + local sp = u.spells() + assert_equal("fireball", sp.name) + assert_equal(2, sp.level) +end diff --git a/src/bind_unit.c b/src/bind_unit.c index cf2a3ac37..4f262114c 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -560,7 +560,7 @@ static int tolua_unit_addspell(lua_State * L) spell *sp = find_spell(str); if (!sp) { - log_error("spell %s could not be found\n", str); + log_warning("spell %s could not be found\n", str); return EINVAL; } else { @@ -763,15 +763,14 @@ static int tolua_unit_get_spells(lua_State * L) unit *self = (unit *) tolua_tousertype(L, 1, 0); sc_mage *mage = self ? get_mage(self) : 0; spellbook *sb = mage ? mage->spellbook : 0; + quicklist *slist = 0; if (sb) { - quicklist *slist = 0; quicklist **slist_ptr = &sb->spells; if (slist_ptr) { slist = *slist_ptr; } - return tolua_quicklist_push(L, "spell_list", "spell", slist); } - return 0; + return tolua_quicklist_push(L, "spellbook", "spell_entry", slist); } #ifdef TODO /* spellbooks */ diff --git a/src/bindings.c b/src/bindings.c index 0385506cf..169c91d0c 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -47,6 +47,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include "creport.h" #include "economy.h" @@ -988,9 +989,22 @@ static int tolua_get_spell_level(lua_State * L) static int tolua_get_spell_name(lua_State * L) { - const struct locale *lang = default_locale; spell *self = (spell *)tolua_tousertype(L, 1, 0); - lua_pushstring(L, spell_name(self, lang)); + lua_pushstring(L, self->sname); + return 1; +} + +static int tolua_get_spell_entry_name(lua_State * L) +{ + spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0); + lua_pushstring(L, self->sp->sname); + return 1; +} + +static int tolua_get_spell_entry_level(lua_State * L) +{ + spellbook_entry *self = (spellbook_entry*)tolua_tousertype(L, 1, 0); + lua_pushinteger(L, self->level); return 1; } @@ -1085,6 +1099,8 @@ int tolua_bindings_open(lua_State * L) tolua_bind_open(L); /* register user types */ + tolua_usertype(L, TOLUA_CAST "spellbook"); + tolua_usertype(L, TOLUA_CAST "spell_entry"); tolua_usertype(L, TOLUA_CAST "spell"); tolua_usertype(L, TOLUA_CAST "spell_list"); tolua_usertype(L, TOLUA_CAST "order"); @@ -1112,12 +1128,16 @@ int tolua_bindings_open(lua_State * L) { tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_name); tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_name, 0); -#ifdef TODO - tolua_variable(L, TOLUA_CAST "school", tolua_get_spell_school, 0); - tolua_variable(L, TOLUA_CAST "level", tolua_get_spell_level, 0); -#endif tolua_variable(L, TOLUA_CAST "text", tolua_get_spell_text, 0); } tolua_endmodule(L); + tolua_cclass(L, TOLUA_CAST "spell_entry", TOLUA_CAST "spell_entry", TOLUA_CAST "", + NULL); + tolua_beginmodule(L, TOLUA_CAST "spell_entry"); + { + tolua_function(L, TOLUA_CAST "__tostring", tolua_get_spell_entry_name); + tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_entry_name, 0); + tolua_variable(L, TOLUA_CAST "level", tolua_get_spell_entry_level, 0); + } tolua_endmodule(L); tolua_module(L, TOLUA_CAST "report", 1); tolua_beginmodule(L, TOLUA_CAST "report"); {