summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/l_dgntil.cc
blob: c93e931172eace4a88f05665108dc035467611be (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
/**
 * @file
 * @brief Tiles-specific dungeon builder functions.
**/

#include "AppHdr.h"

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

#include "branch.h"
#include "coord.h"
#include "mapdef.h"

#include "env.h"
#include "tiledef-dngn.h"
#include "tileview.h"
#include "strings.h"

tileidx_t get_tile_idx(lua_State *ls, int arg)
{
    if (!lua_isstring(ls, arg))
    {
        luaL_argerror(ls, arg, "Expected string for tile name");
        return 0;
    }

    const char *tile_name = luaL_checkstring(ls, arg);

    tileidx_t idx;
    if (!tile_dngn_index(tile_name, &idx))
    {
        string error = "Couldn't find tile '";
        error += tile_name;
        error += "'";
        luaL_argerror(ls, arg, error.c_str());
        return 0;
    }

    return idx;
}

LUAFN(dgn_lev_floortile)
{
    LEVEL(br, 1);

    tile_flavour flv;
    tile_default_flv(br, flv);

    const char *tile_name = tile_dngn_name(flv.floor);
    PLUARET(string, tile_name);
}

LUAFN(dgn_lev_rocktile)
{
    LEVEL(br, 1);

    tile_flavour flv;
    tile_default_flv(br, flv);

    const char *tile_name = tile_dngn_name(flv.wall);
    PLUARET(string, tile_name);
}

LUAFN(dgn_lrocktile)
{
    MAP(ls, 1, map);

    map->rock_tile = luaL_checkstring(ls, 2);
    PLUARET(string, map->rock_tile.c_str());
}

LUAFN(dgn_lfloortile)
{
    MAP(ls, 1, map);

    map->floor_tile = luaL_checkstring(ls, 2);
    PLUARET(string, map->floor_tile.c_str());
}

LUAFN(dgn_change_rock_tile)
{
    string tilename = luaL_checkstring(ls, 1);

    tileidx_t rock;
    if (!tile_dngn_index(tilename.c_str(), &rock))
    {
        string error = "Couldn't find tile '";
        error += tilename;
        error += "'";
        luaL_argerror(ls, 1, error.c_str());
        return 0;
    }
    env.tile_default.wall     = rock;
    env.tile_default.wall_idx =
        store_tilename_get_index(tilename);

    PLUARET(string, tilename.c_str());
}

LUAFN(dgn_change_floor_tile)
{
    string tilename = luaL_checkstring(ls, 1);

    tileidx_t floor;
    if (!tile_dngn_index(tilename.c_str(), &floor))
    {
        string error = "Couldn't find tile '";
        error += tilename;
        error += "'";
        luaL_argerror(ls, 1, error.c_str());
        return 0;
    }
    env.tile_default.floor     = floor;
    env.tile_default.floor_idx =
        store_tilename_get_index(tilename);

    PLUARET(string, tilename.c_str());
}

LUAFN(dgn_ftile)
{
    return dgn_map_add_transform(ls, &map_lines::add_floortile);
}

LUAFN(dgn_rtile)
{
    return dgn_map_add_transform(ls, &map_lines::add_rocktile);
}

LUAFN(dgn_tile)
{
    return dgn_map_add_transform(ls, &map_lines::add_spec_tile);
}

LUAFN(dgn_tile_feat_changed)
{
    COORDS(c, 1, 2);

    if (lua_isnil(ls, 3))
    {
        env.tile_flv(c).feat     = 0;
        env.tile_flv(c).feat_idx = 0;
        return 0;
    }

    string tilename = luaL_checkstring(ls, 3);

    tileidx_t feat;
    if (!tile_dngn_index(tilename.c_str(), &feat))
    {
        string error = "Couldn't find tile '";
        error += tilename;
        error += "'";
        luaL_argerror(ls, 1, error.c_str());
        return 0;
    }
    env.tile_flv(c).feat     = feat;
    env.tile_flv(c).feat_idx =
        store_tilename_get_index(tilename);

    return 0;
}

LUAFN(dgn_tile_floor_changed)
{
    COORDS(c, 1, 2);

    if (lua_isnil(ls, 3))
    {
        env.tile_flv(c).floor     = 0;
        env.tile_flv(c).floor_idx = 0;
        return 0;
    }

    string tilename = luaL_checkstring(ls, 3);

    tileidx_t floor;
    if (!tile_dngn_index(tilename.c_str(), &floor))
    {
        string error = "Couldn't find tile '";
        error += tilename;
        error += "'";
        luaL_argerror(ls, 1, error.c_str());
        return 0;
    }
    env.tile_flv(c).floor     = floor;
    env.tile_flv(c).floor_idx =
        store_tilename_get_index(tilename);

    return 0;
}

const struct luaL_reg dgn_tile_dlib[] =
{
{ "lrocktile", dgn_lrocktile },
{ "lfloortile", dgn_lfloortile },
{ "rtile", dgn_rtile },
{ "ftile", dgn_ftile },
{ "tile", dgn_tile },
{ "change_rock_tile", dgn_change_rock_tile },
{ "change_floor_tile", dgn_change_floor_tile },
{ "lev_floortile", dgn_lev_floortile },
{ "lev_rocktile", dgn_lev_rocktile },
{ "tile_feat_changed", dgn_tile_feat_changed },
{ "tile_floor_changed", dgn_tile_floor_changed },

{ NULL, NULL }
};