binding a magician's spellbook to Lua, with test.

This commit is contained in:
Enno Rehling 2015-05-05 08:44:58 -07:00
parent 0db74d1c09
commit 42783ff02c
3 changed files with 45 additions and 11 deletions

View File

@ -7,7 +7,8 @@ function setup()
"races" : { "human" : {} }, "races" : { "human" : {} },
"terrains" : { "plain" : { "flags" : [ "land" ] } }, "terrains" : { "plain" : { "flags" : [ "land" ] } },
"keywords" : { "de" : { "study": "LERNEN" } }, "keywords" : { "de" : { "study": "LERNEN" } },
"skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } } "skills" : { "de": { "alchemy" : "Alchemie", "crossbow" : "Armbrust" } },
"spells" : { "fireball" : { "syntax" : "u+" } }
}]] }]]
eressea.game.reset() eressea.game.reset()
eressea.config.reset(); eressea.config.reset();
@ -36,3 +37,17 @@ function test_study_expensive()
assert_equal(1, u:get_skill("alchemy")) assert_equal(1, u:get_skill("alchemy"))
assert_equal(0, u:get_item("money")) assert_equal(0, u:get_item("money"))
end 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

View File

@ -560,7 +560,7 @@ static int tolua_unit_addspell(lua_State * L)
spell *sp = find_spell(str); spell *sp = find_spell(str);
if (!sp) { if (!sp) {
log_error("spell %s could not be found\n", str); log_warning("spell %s could not be found\n", str);
return EINVAL; return EINVAL;
} }
else { else {
@ -763,15 +763,14 @@ static int tolua_unit_get_spells(lua_State * L)
unit *self = (unit *) tolua_tousertype(L, 1, 0); unit *self = (unit *) tolua_tousertype(L, 1, 0);
sc_mage *mage = self ? get_mage(self) : 0; sc_mage *mage = self ? get_mage(self) : 0;
spellbook *sb = mage ? mage->spellbook : 0; spellbook *sb = mage ? mage->spellbook : 0;
if (sb) {
quicklist *slist = 0; quicklist *slist = 0;
if (sb) {
quicklist **slist_ptr = &sb->spells; quicklist **slist_ptr = &sb->spells;
if (slist_ptr) { if (slist_ptr) {
slist = *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 */ #ifdef TODO /* spellbooks */

View File

@ -47,6 +47,7 @@ without prior permission by the authors of Eressea.
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/save.h> #include <kernel/save.h>
#include <kernel/spell.h> #include <kernel/spell.h>
#include <kernel/spellbook.h>
#include "creport.h" #include "creport.h"
#include "economy.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) static int tolua_get_spell_name(lua_State * L)
{ {
const struct locale *lang = default_locale;
spell *self = (spell *)tolua_tousertype(L, 1, 0); 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; return 1;
} }
@ -1085,6 +1099,8 @@ int tolua_bindings_open(lua_State * L)
tolua_bind_open(L); tolua_bind_open(L);
/* register user types */ /* 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");
tolua_usertype(L, TOLUA_CAST "spell_list"); tolua_usertype(L, TOLUA_CAST "spell_list");
tolua_usertype(L, TOLUA_CAST "order"); 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_function(L, TOLUA_CAST "__tostring", tolua_get_spell_name);
tolua_variable(L, TOLUA_CAST "name", tolua_get_spell_name, 0); 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_variable(L, TOLUA_CAST "text", tolua_get_spell_text, 0);
} tolua_endmodule(L); } 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_module(L, TOLUA_CAST "report", 1);
tolua_beginmodule(L, TOLUA_CAST "report"); tolua_beginmodule(L, TOLUA_CAST "report");
{ {