diff options
author | Darshan Shaligram <dshaligram@users.sourceforge.net> | 2011-01-06 00:29:27 +0530 |
---|---|---|
committer | Darshan Shaligram <dshaligram@users.sourceforge.net> | 2011-01-06 14:30:09 +0530 |
commit | ec53c836936ba9c4ef812fa8093230464a289b61 (patch) | |
tree | 5c97974778baf3aea73e2a448c1ac528402fd32e /crawl-ref/source/l_mons.cc | |
parent | ff17aecca7bfc2a9827031357906f9a2f02b47d7 (diff) | |
download | crawl-ref-ec53c836936ba9c4ef812fa8093230464a289b61.tar.gz crawl-ref-ec53c836936ba9c4ef812fa8093230464a289b61.zip |
Make .des corpse generation use the actual monster
.des corpse generation now creates the named monster and converts it to a
corpse using the standard corpse-placement routine. Reusing the standard
corpse routines will make this code less likely to break in future. It also
has the advantage of giving the corpse-maker a real monster to inspect for
things like monster properties.
Also add some tests for custom monster names.
Diffstat (limited to 'crawl-ref/source/l_mons.cc')
-rw-r--r-- | crawl-ref/source/l_mons.cc | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/crawl-ref/source/l_mons.cc b/crawl-ref/source/l_mons.cc index 508772b37e..df0ad1ad0f 100644 --- a/crawl-ref/source/l_mons.cc +++ b/crawl-ref/source/l_mons.cc @@ -12,6 +12,16 @@ #include "mon-util.h" #include "mon-stuff.h" +#define WRAPPED_MONSTER(ls, name) \ + MonsterWrap *___mw = clua_get_userdata< MonsterWrap >(ls, MONS_METATABLE); \ + if (!___mw \ + || !___mw->mons \ + || CLua::get_vm(ls).managed_vm && ___mw->turn != you.num_turns) \ + { \ + luaL_argerror(ls, 1, "Invalid monster wrapper"); \ + } \ + monster *name(___mw->mons) + ///////////////////////////////////////////////////////////////////// // Monster handling @@ -106,9 +116,25 @@ LUAFN(l_mons_add_energy) mons->speed_increment += luaL_checkint(ls, 1); return (0); } - MDEFN(add_energy, add_energy) +#define LUANAMEFN(name, expr) \ + static int l_mons_##name##_fn(lua_State *ls) { \ + ASSERT_DLUA; \ + const monster* mons = \ + clua_get_lightuserdata<monster>(ls, lua_upvalueindex(1)); \ + const description_level_type dtype = \ + lua_isstring(ls, 1)? \ + description_type_by_name(luaL_checkstring(ls, 1)) \ + : DESC_PLAIN; \ + PLUARET(string, expr.c_str()); \ + } \ + MDEFN(name, name##_fn) \ + +LUANAMEFN(mname, mons->name(dtype, true)) +LUANAMEFN(mfull_name, mons->full_name(dtype, true)) +LUANAMEFN(mbase_name, mons->base_name(dtype, true)) + MDEF(hd) { PLUARET(number, mons->hit_dice); @@ -461,6 +487,9 @@ static MonsAccessor mons_attrs[] = { "targetx", l_mons_targetx }, { "targety", l_mons_targety }, + { "mname", l_mons_mname }, + { "mfull_name", l_mons_mfull_name }, + { "mbase_name", l_mons_mbase_name }, { "energy", l_mons_energy }, { "add_energy", l_mons_add_energy }, { "dismiss", l_mons_dismiss }, @@ -478,13 +507,7 @@ static MonsAccessor mons_attrs[] = static int monster_get(lua_State *ls) { - MonsterWrap *mw = clua_get_userdata< MonsterWrap >(ls, MONS_METATABLE); - if (!mw - || !mw->mons - || CLua::get_vm(ls).managed_vm && mw->turn != you.num_turns) - { - return (0); - } + WRAPPED_MONSTER(ls, mons); const char *attr = luaL_checkstring(ls, 2); if (!attr) @@ -492,7 +515,7 @@ static int monster_get(lua_State *ls) for (unsigned i = 0; i < sizeof(mons_attrs) / sizeof(mons_attrs[0]); ++i) if (!strcmp(attr, mons_attrs[i].attribute)) - return (mons_attrs[i].accessor(ls, mw->mons, attr)); + return (mons_attrs[i].accessor(ls, mons, attr)); return (0); } @@ -588,6 +611,7 @@ static const struct luaL_reg mons_lib[] = void dluaopen_monsters(lua_State *ls) { + lua_stack_cleaner stack_clean(ls); luaL_newmetatable(ls, MONS_METATABLE); lua_pushstring(ls, "__index"); lua_pushcfunction(ls, monster_get); @@ -596,9 +620,5 @@ void dluaopen_monsters(lua_State *ls) lua_pushstring(ls, "__newindex"); lua_pushcfunction(ls, monster_set); lua_settable(ls, -3); - - // Pop the metatable off the stack. - lua_pop(ls, 1); - - luaL_openlib(ls, "mons", mons_lib, 0); + luaL_register(ls, "mons", mons_lib); } |