From a4f9da4c2defdeedba6e172d8c8538f4b1aefe78 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Tue, 20 Oct 2009 00:05:15 +0200 Subject: Move part of dgn_lib out to l_dgnevt.cc. --- crawl-ref/source/l_dgnevt.cc | 67 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'crawl-ref/source/l_dgnevt.cc') diff --git a/crawl-ref/source/l_dgnevt.cc b/crawl-ref/source/l_dgnevt.cc index 875c90b467..73574309fe 100644 --- a/crawl-ref/source/l_dgnevt.cc +++ b/crawl-ref/source/l_dgnevt.cc @@ -5,6 +5,10 @@ #include "dgnevent.h" +/* + * Methods for DEVENT_METATABLE. + */ + static int dgnevent_type(lua_State *ls) { DEVENT(ls, 1, dev); @@ -45,7 +49,7 @@ static int dgnevent_arg2(lua_State *ls) PLUARET(number, dev->arg2); } -const struct luaL_reg dgnevent_lib[] = +static const struct luaL_reg dgnevent_lib[] = { { "type", dgnevent_type }, { "pos", dgnevent_place }, @@ -60,3 +64,64 @@ void luaopen_dgnevent(lua_State *ls) { luaopen_setmeta(ls, "dgnevent", dgnevent_lib, DEVENT_METATABLE); } + +/* + * Functions for library "dgn". + */ + +static const char *dgn_event_type_names[] = +{ +"none", "turn", "mons_move", "player_move", "leave_level", +"entering_level", "entered_level", "player_los", "player_climb", +"monster_dies", "item_pickup", "item_moved", "feat_change", +"wall_hit" +}; + +static dgn_event_type dgn_event_type_by_name(const std::string &name) +{ + for (unsigned i = 0; i < ARRAYSZ(dgn_event_type_names); ++i) + if (dgn_event_type_names[i] == name) + return static_cast(i? 1 << (i - 1) : 0); + return (DET_NONE); +} + +static const char *dgn_event_type_name(unsigned evmask) +{ + if (evmask == 0) + return (dgn_event_type_names[0]); + + for (unsigned i = 1; i < ARRAYSZ(dgn_event_type_names); ++i) + if (evmask & (1 << (i - 1))) + return (dgn_event_type_names[i]); + + return (dgn_event_type_names[0]); +} + +static void dgn_push_event_type(lua_State *ls, int n) +{ + if (lua_isstring(ls, n)) + lua_pushnumber(ls, dgn_event_type_by_name(lua_tostring(ls, n))); + else if (lua_isnumber(ls, n)) + lua_pushstring(ls, dgn_event_type_name(luaL_checkint(ls, n))); + else + lua_pushnil(ls); +} + +static int dgn_dgn_event(lua_State *ls) +{ + const int start = lua_isuserdata(ls, 1)? 2 : 1; + int retvals = 0; + for (int i = start, nargs = lua_gettop(ls); i <= nargs; ++i) + { + dgn_push_event_type(ls, i); + retvals++; + } + return (retvals); +} + +const struct luaL_reg dgn_event_lib[] = +{ +{ "dgn_event_type", dgn_dgn_event }, + +{ NULL, NULL } +}; -- cgit v1.2.3-54-g00ecf