summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/l_mons.cc
blob: 3a4aa2af94d1922688898fe2b95904778e59b94c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "AppHdr.h"

#include "cluautil.h"
#include "l_libs.h"

#include "delay.h"
#include "mon-util.h"
#include "monstuff.h"

/////////////////////////////////////////////////////////////////////
// Monster handling

#define MONS_METATABLE "monster.monsaccess"

struct MonsterWrap
{
    monsters *mons;
    long      turn;
};

void push_monster(lua_State *ls, monsters *mons)
{
    MonsterWrap *mw = clua_new_userdata< MonsterWrap >(ls, MONS_METATABLE);
    mw->turn = you.num_turns;
    mw->mons = mons;
}

#define MDEF(name)                                                      \
    static int l_mons_##name(lua_State *ls, monsters *mons,             \
                             const char *attr)                         \

#define MDEFN(name, closure)                    \
    static int l_mons_##name(lua_State *ls, monsters *mons, const char *attrs) \
    {                                                                   \
    lua_pushlightuserdata(ls, mons);                                    \
    lua_pushcclosure(ls, l_mons_do_dismiss, 1);                         \
    return (1);                                                         \
    }

MDEF(name)
{
    PLUARET(string, mons_type_name(mons->type, DESC_PLAIN).c_str());
}

MDEF(x)
{
    PLUARET(number, int(mons->pos().x) - int(you.pos().x));
}

MDEF(y)
{
    PLUARET(number, int(mons->pos().y) - int(you.pos().y));
}

MDEF(hd)
{
    PLUARET(number, mons->hit_dice);
}

static const char *_monuse_names[] =
{
    "nothing", "open_doors", "starting_equipment", "weapons_armour",
    "magic_items"
};

static const char *_monuse_to_str(mon_itemuse_type utyp)
{
    COMPILE_CHECK(ARRAYSZ(_monuse_names) == NUM_MONUSE, c1);
    return _monuse_names[utyp];
}

MDEF(muse)
{
    if (const monsterentry *me = mons->find_monsterentry())
    {
        PLUARET(string, _monuse_to_str(me->gmon_use));
    }
    return (0);
}

static const char *_moneat_names[] =
{
    "nothing", "items", "corpses", "food"
};

static const char *_moneat_to_str(mon_itemeat_type etyp)
{
    COMPILE_CHECK(ARRAYSZ(_moneat_names) == NUM_MONEAT, c1);
    return _moneat_names[etyp];
}

MDEF(meat)
{
    if (const monsterentry *me = mons->find_monsterentry())
    {
        PLUARET(string, _moneat_to_str(me->gmon_eat));
    }
    return (0);
}

static int l_mons_do_dismiss(lua_State *ls)
{
    // dismiss is only callable from dlua, not from managed VMs (i.e.
    // end-user scripts cannot dismiss monsters).
    ASSERT_DLUA;
    monsters *mons =
        util_get_userdata<monsters>(ls, lua_upvalueindex(1));
    if (mons->alive())
    {
        mons->flags |= MF_HARD_RESET;
        monster_die(mons, KILL_DISMISSED, NON_MONSTER);
    }
    return (0);
}

MDEFN(dismiss, do_dismiss)

MDEF(experience)
{
    ASSERT_DLUA;
    PLUARET(number, exper_value(mons));
}

struct MonsAccessor
{
    const char *attribute;
    int (*accessor)(lua_State *ls, monsters *mons, const char *attr);
};

static MonsAccessor mons_attrs[] =
{
    { "name", l_mons_name },
    { "x"   , l_mons_x    },
    { "y"   , l_mons_y    },
    { "hd"  , l_mons_hd   },
    { "muse", l_mons_muse },
    { "meat", l_mons_meat },
    { "dismiss", l_mons_dismiss },
    { "experience", l_mons_experience },
};

static int monster_get(lua_State *ls)
{
    MonsterWrap *mw = clua_get_userdata< MonsterWrap >(ls, MONS_METATABLE);
    if (!mw || mw->turn != you.num_turns || !mw->mons)
        return (0);

    const char *attr = luaL_checkstring(ls, 2);
    if (!attr)
        return (0);

    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 (0);
}

static const char *_monster_behaviour_names[] = {
    "sleep",
    "wander",
    "seek",
    "flee",
    "cornered",
    "panic",
    "lurk"
};

static beh_type behaviour_by_name(const std::string &name) {
    ASSERT(ARRAYSZ(_monster_behaviour_names) == NUM_BEHAVIOURS);
    for (unsigned i = 0; i < ARRAYSZ(_monster_behaviour_names); ++i)
        if (name == _monster_behaviour_names[i])
            return static_cast<beh_type>(i);
    return NUM_BEHAVIOURS;
}

static int monster_set(lua_State *ls)
{
    // Changing monster behaviour is for the dungeon builder only,
    // never for user scripts.
    ASSERT_DLUA;

    MonsterWrap *mw = clua_get_userdata< MonsterWrap >(ls, MONS_METATABLE);
    if (!mw || !mw->mons)
        return (0);

    const char *attr = luaL_checkstring(ls, 2);
    if (!attr)
        return (0);

    if (!strcmp(attr, "beh")) {
        const beh_type beh =
            lua_isnumber(ls, 3) ?
            static_cast<beh_type>(luaL_checkint(ls, 3)) :
            lua_isstring(ls, 3) ? behaviour_by_name(lua_tostring(ls, 3)) :
            NUM_BEHAVIOURS;
        if (beh != NUM_BEHAVIOURS)
            mw->mons->behaviour = beh;
    }

    return (0);
}

static int mons_behaviour(lua_State *ls) {
    if (lua_gettop(ls) < 1)
        return (0);

    if (lua_isnumber(ls, 1)) {
        lua_pushvalue(ls, 1);
        return (1);
    }
    else if (lua_isstring(ls, 1)) {
        const beh_type beh = behaviour_by_name(lua_tostring(ls, 1));
        if (beh != NUM_BEHAVIOURS) {
            lua_pushnumber(ls, beh);
            return (1);
        }
    }
    return (0);
}

static const struct luaL_reg mons_lib[] =
{
    { "behaviour", mons_behaviour },
    { NULL, NULL }
};

void cluaopen_monsters(lua_State *ls)
{
    luaL_newmetatable(ls, MONS_METATABLE);
    lua_pushstring(ls, "__index");
    lua_pushcfunction(ls, monster_get);
    lua_settable(ls, -3);

    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);
}