Apply the new ground_level method.
[crawl.git] / crawl-ref / source / l_moninf.cc
blob5cb3d4fa9fed973db596fd5a52cc8888409decef
1 /*
2 * File: l_moninf.cc
3 * Summary: User-accessible monster info.
4 */
6 #include "AppHdr.h"
8 #include "l_libs.h"
10 #include "cluautil.h"
11 #include "coord.h"
12 #include "env.h"
13 #include "libutil.h"
14 #include "mon-info.h"
15 #include "player.h"
17 #define MONINF_METATABLE "monster.info"
19 void lua_push_moninf(lua_State *ls, monster_info *mi)
21 monster_info **miref =
22 clua_new_userdata<monster_info *>(ls, MONINF_METATABLE);
23 *miref = new monster_info(*mi);
26 #define MONINF(ls, n, var) \
27 monster_info *var = *(monster_info **) \
28 luaL_checkudata(ls, n, MONINF_METATABLE)
30 #define MIRET1(type, field, cfield) \
31 static int moninf_get_##field(lua_State *ls) \
32 { \
33 MONINF(ls, 1, mi); \
34 lua_push##type(ls, mi->cfield); \
35 return (1); \
38 #define MIREG(field) { #field, moninf_get_##field }
40 MIRET1(number, damage_level, dam)
41 MIRET1(boolean, is_safe, is(MB_SAFE))
42 MIRET1(string, mname, mname.c_str())
43 MIRET1(number, type, type)
44 MIRET1(number, base_type, base_type)
45 MIRET1(number, number, number)
46 MIRET1(number, colour, colour)
48 LUAFN(moninf_get_is)
50 MONINF(ls, 1, mi);
51 int num = luaL_checknumber(ls, 2);
52 lua_pushboolean(ls, mi->is(num));
53 return (1);
56 LUAFN(moninf_get_damage_desc)
58 MONINF(ls, 1, mi);
59 std::string s = mi->damage_desc();
60 lua_pushstring(ls, s.c_str());
61 return (1);
64 LUAFN(moninf_get_desc)
66 MONINF(ls, 1, mi);
67 std::string desc;
68 int col;
69 mi->to_string(1, desc, col);
70 lua_pushstring(ls, desc.c_str());
71 return (1);
74 static const struct luaL_reg moninf_lib[] =
76 MIREG(type),
77 MIREG(base_type),
78 MIREG(number),
79 MIREG(colour),
80 MIREG(mname),
81 MIREG(is),
82 MIREG(is_safe),
83 MIREG(damage_level),
84 MIREG(damage_desc),
85 MIREG(desc),
87 { NULL, NULL }
90 // XXX: unify with directn.cc/h
91 // This uses relative coordinates with origin the player.
92 bool in_show_bounds(const coord_def &s)
94 return (s.rdist() <= ENV_SHOW_OFFSET);
97 LUAFN(mi_get_monster_at)
99 COORDSHOW(s, 1, 2)
100 coord_def p = player2grid(s);
101 if (!you.see_cell(p))
102 return (0);
103 if (env.mgrid(p) == NON_MONSTER)
104 return (0);
105 monster* m = &env.mons[env.mgrid(p)];
106 if (!m->visible_to(&you))
107 return (0);
108 monster_info mi(m);
109 lua_push_moninf(ls, &mi);
110 return (1);
113 static const struct luaL_reg mon_lib[] =
115 { "get_monster_at", mi_get_monster_at },
117 { NULL, NULL }
120 void cluaopen_moninf(lua_State *ls)
122 clua_register_metatable(ls, MONINF_METATABLE, moninf_lib,
123 lua_object_gc<monster_info>);
124 luaL_openlib(ls, "monster", mon_lib, 0);