summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/mapdef.cc
blob: 3a185fad20d8da646ace6185bedd9269ca8822e2 (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
#include "mapdef.h"
#include <cstdarg>
#include <cstdio>
#include <cctype>

#define esc(s) escape_string(s, "\"", "\\")

std::string escape_string(std::string s, 
                          const std::string &toesc, 
                          const std::string &escwith)
{
    std::string::size_type start = 0;
    std::string::size_type found;
   
    while ((found = s.find_first_of(toesc, start)) != std::string::npos)
    {
        const char c = s[found];
        s.replace( found, 1, escwith + c );
        start = found + 1 + escwith.length();
    }

    return (s);
}

std::string clean_string(std::string s, const std::string &tokill)
{
    std::string::size_type found;
    while ((found = s.find_first_of(tokill)) != std::string::npos)
        s.erase(found, 1);
    return (s);
}

static std::string to_s(int num)
{
    char buf[20];
    snprintf(buf, sizeof buf, "%d", num);
    return (buf);
}

static std::string to_sl(long num)
{
    char buf[20];
    snprintf(buf, sizeof buf, "%ld", num);
    return (buf);
}

///////////////////////////////////////////////
// level_range
//

level_range::level_range(int s, int d)
    : shallowest(), deepest()
{
    set(s, d);
}

void level_range::set(int s, int d)
{
    shallowest = s;
    deepest    = d;
    if (deepest == -1)
        deepest = shallowest;
}

void level_range::reset()
{
    deepest = shallowest = -1;
}

bool level_range::contains(int x) const
{
    return (x >= shallowest && x <= deepest);
}

///////////////////////////////////////////////
// map_lines
//

map_lines::map_lines() : lines(), map_width(0)
{
}

map_lines::map_lines(int nlines, ...) : lines(), map_width(0)
{
    va_list args;
    va_start(args, nlines);

    for (int i = 0; i < nlines; ++i)
        add_line( va_arg(args, const char *) );

    va_end(args);
}

const std::vector<std::string> &map_lines::get_lines() const
{
    return (lines);
}

void map_lines::add_line(const std::string &s)
{
    lines.push_back(s);
    if ((int) s.length() > map_width)
        map_width = s.length();
}

int map_lines::width() const
{
    return map_width;
}

int map_lines::height() const
{
    return lines.size();
}

void map_lines::clear()
{
    lines.clear();
    map_width = 0;
}

std::string map_lines::get_initialiser() const
{
    std::string init = "  map_lines(" + to_s(lines.size()) + ",\n";
    for (int i = 0, size = lines.size(); i < size; ++i)
    {
        init += "    \"";
        init += esc(lines[i]);
        init += "\"";
        if (i < size - 1)
        {
            init += ",";
            init += "\n";
        }
    }
    init += ")";
    return (init);
}

///////////////////////////////////////////////
// map_def
//

void map_def::init()
{
    name.clear();
    tags.clear();
    place.clear();
    depth.reset();
    orient = MAP_NONE;

    // Base chance; this is not a percentage.
    chance = 10;

    // The map designer must explicitly disallow these if unwanted.
    flags = MAPF_MIRROR_VERTICAL | MAPF_MIRROR_HORIZONTAL;

    random_symbols.clear();

    map.clear();
    mons.clear();
}

std::string map_def::get_initialiser() const
{
    std::string sinit = "{\n";

    sinit += "  \"" + esc(name) + "\",\n";
    sinit += "  \"" + esc(tags) + "\",\n";
    sinit += "  \"" + esc(place) + "\",\n";

    sinit += "  level_range(" 
            + to_s(depth.shallowest) 
            + ", "
            + to_s(depth.deepest)
            + "),\n";

    sinit += "  (map_section_type) " + to_s(orient) + ",\n";

    sinit += "  // Map generation probability weight\n";
    sinit += "  " + to_s(chance) + ",\n";

    sinit += "  // Map flags\n";
    sinit += "  " + to_sl(flags) + "L,\n";
    sinit += map.get_initialiser() + ",\n";
    sinit += mons.get_initialiser() + ",\n";
    sinit += "  \"" + esc(random_symbols) + "\"\n";
    sinit += "},\n";

    return (sinit);
}

///////////////////////////////////////////////////////////////////
// mons_list
//

mons_list::mons_list(int nids, ...) : mons_names(), mons_ids()
{
    va_list args;
    va_start(args, nids);

    for (int i = 0; i < nids; ++i)
        mons_ids.push_back( va_arg(args, int) );

    va_end(args);
}

mons_list::mons_list() : mons_names(), mons_ids()
{
}

const std::vector<int> &mons_list::get_ids() const
{
    return (mons_ids);
}

void mons_list::clear()
{
    mons_names.clear();
    mons_ids.clear();
}

void mons_list::add_mons(const std::string &s)
{
    mons_names.push_back(s);
}

std::string mons_list::get_initialiser() const
{
    std::string init = "  mons_list(" + to_s(mons_names.size());
    if (mons_names.size())
        init += ",\n";
    for (int i = 0, size = mons_names.size(); i < size; ++i)
    {
        init += "    ";
        std::string can = canonical(mons_names[i]);
        if (can == "MONS_RANDOM" || can == "MONS_RANDOM_MONSTER")
            can = "RANDOM_MONSTER";

        if (can == "MONS_ANY_DEMON")
            can = "-100 - DEMON_RANDOM";
        else if (can.find("MONS_ANY_DEMON") == 0)
            can = "-100 - " + can.substr(9);

        init += can;
        if (i < size - 1)
            init += ",\n";
    }
    init += ")";

    return (init);
}

std::string mons_list::canonical(std::string name) const
{
    for (int i = 0, len = name.length(); i < len; ++i)
    {
        name[i] = toupper(name[i]);
        if (name[i] == ' ')
            name[i] = '_';
    }
    return "MONS_" + name;
}