summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/levcomp.ypp
blob: 83ae14bf5e409f8163bbbff9ae1e7befb5adb94c (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
%{

#include "AppHdr.h"
#include "libutil.h"
#include "levcomp.h"
#include "stuff.h"
#include <map>

#define YYERROR_VERBOSE 1

int yylex();

extern int yylineno;

struct map_file_place
{
    std::string filename;
    int lineno;

    map_file_place(const std::string &s = "", int line = 0)
        : filename(s), lineno(line)
    {
    }
};

typedef std::map<std::string, map_file_place> map_load_info_t;

static map_load_info_t loaded_maps;

void yyerror(const char *e)
{
    fprintf(stderr, "%s:%d: %s\n", lc_desfile.c_str(), yylineno, e);
    // Bail bail bail.
    exit(1);
}

%}

%union
{
    int i;
    const char *text;
}

%token <i>      BRANCHDEF BRANCH DESC DEFAULT
%token <i>      DEFAULT_DEPTH SHUFFLE SUBST TAGS KFEAT KITEM KMONS
%token <i>      NAME DEPTH ORIENT PLACE CHANCE FLAGS MONS ITEM
%token <i>      ROOT_DEPTH ENTRY_MSG EXIT_MSG
%token <i>      ROCK_COLOUR FLOOR_COLOUR
%token <i>      ENCOMPASS FLOAT 
%token <i>      NORTH EAST SOUTH WEST
%token <i>      NORTHEAST SOUTHEAST SOUTHWEST NORTHWEST

%token <i>      LEVEL END PVAULT PMINIVAULT MONSTERS ENDMONSTERS

%token <i>      CHARACTER

%token <i>      NO_HMIRROR NO_VMIRROR NO_ROTATE

%token <i>      PANDEMONIC 
%token <i>      DASH COMMA QUOTE OPAREN CPAREN
%token <i>      INTEGER

%type  <i>      orient_name flagname

%token <text>   STRING MAP_LINE MONSTER_NAME ITEM_INFO
%token <text>   IDENTIFIER

%%

file            : definitions { }
                ;

definitions     : /* empty */               {}
                | definition definitions    {}
                ;

definition      : def       {}
                | level     {}
                ;

def             : defdepth
                ;

defdepth        : DEFAULT_DEPTH depth_range
                {
                    lc_default_depth = lc_range;
                }
                ;

level           : name metalines map_def metalines
                {
                    if (lc_map.orient == MAP_FLOAT
                        || lc_map.is_minivault())
                    {
                        if (lc_map.map.width() > GXM - MAPGEN_BORDER * 2
                            || lc_map.map.height() > GYM - MAPGEN_BORDER * 2)
                        {
                            char buf[300];
                            snprintf(buf, sizeof buf, 
                                 "%s is too big: %dx%d - max %dx%d",
                                 lc_map.is_minivault()? "Minivault" : "Float",
                                 lc_map.map.width(), lc_map.map.height(),
                                 GXM - MAPGEN_BORDER * 2,
                                 GYM - MAPGEN_BORDER * 2);
                            yyerror(buf);
                        }
                    }
                    else
                    {
                        if (lc_map.map.width() > GXM
                            || lc_map.map.height() > GYM)
                        {
                            char buf[300];
                            snprintf(buf, sizeof buf, 
                                 "Map is too big: %dx%d - max %dx%d",
                                 lc_map.map.width(), lc_map.map.height(),
                                 GXM, GYM);
                            yyerror(buf);
                        }
                    }

                    if (lc_map.map.height() == 0)
                        yyerror("Must define map.");

                    add_parsed_map( lc_map );
                }
                ;

name            : NAME STRING
                {
                    lc_map.init();
                    lc_map.depth = lc_default_depth;
                    lc_map.name = $2;

                    map_load_info_t::const_iterator i = 
                        loaded_maps.find($2);

                    if (i != loaded_maps.end())
                    {
                        yyerror(
                            make_stringf(
                                "Map named '%s' already loaded at %s:%d",
                                $2,
                                i->second.filename.c_str(),
                                i->second.lineno).c_str() );
                    }

                    loaded_maps[$2] = map_file_place(lc_desfile, yylineno);
                }
                ;

metalines       : /* no metadata */
                | metaline metalines
                ;

metaline        : place
                | depth
                | chance
                | orientation
                | flags
                | mons
                | items
                | subst
                | tags
                | shuffle
                | kfeat
                | kitem
                | kmons
                ;

kfeat           : KFEAT { }
                | KFEAT STRING
                {
                    std::string err = lc_map.add_key_feat($2);
                    if (!err.empty())
                        yyerror(
                            make_stringf("Bad arg to KFEAT: '%s' (%s)",
                                $2, err.c_str()).c_str());
                }

kmons           : KMONS { }
                | KMONS STRING
                {
                    std::string err = lc_map.add_key_mons($2);
                    if (!err.empty())
                        yyerror(
                            make_stringf("Bad arg to KMONS: '%s' (%s)",
                                $2, err.c_str()).c_str());
                }

kitem           : KITEM { }
                | KITEM STRING
                {
                    std::string err = lc_map.add_key_item($2);
                    if (!err.empty())
                        yyerror(
                            make_stringf("Bad arg to KITEM: '%s' (%s)",
                                $2, err.c_str()).c_str());
                }

shuffle         : SHUFFLE shuffle_specifiers {} 
                ;

shuffle_specifiers : shuffle_spec
                   | shuffle_spec COMMA shuffle_specifiers
                   ;

shuffle_spec    : ITEM_INFO
                {
                    std::string err = lc_map.map.add_shuffle($1);
                    if (!err.empty())
                        yyerror(
                            make_stringf(
                                "Bad shuffle argument: '%s' (%s)",
                                $1, err.c_str() ).c_str() );
                }

tags            : TAGS tagstrings {}
                ;

tagstrings      : /* empty */
                | STRING tagstrings
                {
                    lc_map.tags += " ";
                    lc_map.tags += $1;
                    lc_map.tags += " ";
                }
                ;

subst           : SUBST subst_specifiers { }
                ;

subst_specifiers : subst_spec
                 | subst_spec COMMA subst_specifiers
                 ;

subst_spec      : ITEM_INFO
                {
                    std::string err = lc_map.map.add_subst($1);
                    if (!err.empty())
                        yyerror(
                            make_stringf(
                                "Bad SUBST argument: '%s' (%s)",
                                $1, err.c_str() ).c_str() );
                }
                ;

items           : ITEM {}
                | ITEM item_specifiers {}
                ;

item_specifiers : item_specifier COMMA item_specifiers
                | item_specifier
                ;

item_specifier  : ITEM_INFO
                {
                    std::string error = lc_map.items.add_item($1);
                    if (error.size())
                    {
                        char errbuf[300];
                        snprintf(errbuf, sizeof errbuf,
                            "Invalid item descriptor: '%s' (%s)",
                                $1, error.c_str());
                        yyerror(errbuf);
                    }
                    if (lc_map.items.size() > 8)
                        yyerror("Too many items specified (max 8)");
                }
                
mons            : MONS {}
                | MONS mnames {}
                ;

mnames          : mname COMMA mnames
                | mname
                ;

mname           : MONSTER_NAME
                {
                    std::string err = lc_map.mons.add_mons($1);
                    if (!err.empty())
                    {
                        char buf[300];
                        snprintf(buf, sizeof buf, 
                                 "bad monster spec '%s' (%s)",
                                 $1, err.c_str());
                        yyerror(buf);
                    }
                    if (lc_map.mons.size() > 7)
                        yyerror("Too many monsters specified (max 7)");
                }
                ;

place           : PLACE STRING
                {
                    lc_map.place = $2;
                }
                ;

depth           : DEPTH {}
                | DEPTH depth_range
                {
                    lc_map.depth = lc_range;
                }
                ;

depth_range     : INTEGER DASH INTEGER
                {
                    lc_range.set($1, $3);
                }

                | INTEGER
                {
                    lc_range.set($1);
                }
                ;

chance          : CHANCE INTEGER
                {
                    lc_map.chance = $2;
                }
                ;

orientation     : ORIENT {}
                | ORIENT orient_name
                {
                    lc_map.orient = (map_section_type) $2;
                }
                ;

orient_name     : ENCOMPASS     { $$ = MAP_ENCOMPASS; }
                | NORTH         { $$ = MAP_NORTH; }
                | EAST          { $$ = MAP_EAST; }
                | SOUTH         { $$ = MAP_SOUTH; }
                | WEST          { $$ = MAP_WEST; }
                | NORTHEAST     { $$ = MAP_NORTHEAST; }
                | SOUTHEAST     { $$ = MAP_SOUTHEAST; }
                | SOUTHWEST     { $$ = MAP_SOUTHWEST; }
                | NORTHWEST     { $$ = MAP_NORTHWEST; }
                | FLOAT         { $$ = MAP_FLOAT; }
                ;

flags           : FLAGS flagnames {}
                ;

flagnames       : /* empty */
                | flagname flagnames
                {
                    switch ($1) {
                    case NO_HMIRROR:
                        lc_map.flags &= ~MAPF_MIRROR_HORIZONTAL;
                        break;
                    case NO_VMIRROR:
                        lc_map.flags &= ~MAPF_MIRROR_VERTICAL;
                        break;
                    case NO_ROTATE:
                        lc_map.flags &= ~MAPF_ROTATE;
                        break;
                    }
                }
                ;

flagname        : NO_HMIRROR    { $$ = NO_HMIRROR; }
                | NO_VMIRROR    { $$ = NO_VMIRROR; }
                | NO_ROTATE     { $$ = NO_ROTATE;  }
                ;

map_def         : map_lines
                ;

map_lines       : map_line
                | map_line map_lines
                ;

map_line        : MAP_LINE
                {
                    lc_map.map.add_line($1);
                }
                ;

%%