File: ng-init.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (434 lines) | stat: -rw-r--r-- 14,527 bytes parent folder | download | duplicates (2)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
 * @file
 * @brief Initializing non-player-related parts of a new game.
**/
/* TODO: 'you' shouldn't occur here.
 *       Some of these might fit better elsewhere.
 */

#include "AppHdr.h"

#include "mpr.h"
#include "ng-init.h"

#include "branch.h"
#include "describe.h"
#include "dungeon.h"
#include "end.h"
#include "item-name.h"
#include "libutil.h"
#include "maps.h"
#include "ng-init-branches.h"
#include "random.h"
#include "religion.h"
#include "state.h"
#include "stringutil.h"
#include "tag-version.h"
#include "unicode.h"

#ifdef DEBUG_DIAGNOSTICS
#define DEBUG_TEMPLES
#endif

static uint8_t _random_potion_description()
{
    const int desc = random2(PDQ_NQUALS * PDC_NCOLOURS);
    if (coinflip())
        return desc;
    return desc % PDC_NCOLOURS;
}

// Determine starting depths of branches.
void initialise_branch_depths()
{
    root_branch = BRANCH_DUNGEON;

    // XXX: Should this go elsewhere?
    branch_bribe.init(0);

    for (branch_iterator it; it; ++it)
        brentry[it->id].clear();

    if (crawl_state.game_is_sprint())
    {
        brdepth.init(-1);
        brdepth[BRANCH_DUNGEON] = 1;
        brdepth[BRANCH_CRUCIBLE] = 1;
        brdepth[BRANCH_ARENA] = 1;
        return;
    }

    for (int branch = 0; branch < NUM_BRANCHES; ++branch)
    {
        const Branch *b = &branches[branch];
        ASSERT(b->id == branch);
        branch_uniq_map_tags[branch].clear();
    }

    initialise_brentry();

    if (crawl_state.game_is_descent())
        brdepth[BRANCH_DUNGEON] = 12;
}

static void _use_overflow_temple(vector<god_type> temple_gods)
{
    CrawlVector &overflow_temples
        = you.props[OVERFLOW_TEMPLES_KEY].get_vector();

    const unsigned int level = random_range(MIN_OVERFLOW_LEVEL,
                                            MAX_OVERFLOW_LEVEL);

    // List of overflow temples on this level.
    CrawlVector &level_temples = overflow_temples[level - 1].get_vector();

    CrawlHashTable temple;

    CrawlVector &gods = temple[TEMPLE_GODS_KEY].new_vector(SV_BYTE);

    for (unsigned int i = 0; i < temple_gods.size(); i++)
        gods.push_back((char) temple_gods[i]);

    level_temples.push_back(temple);
}

// Determine which altars go into the Ecumenical Temple, which go into
// overflow temples, and on what level the overflow temples are.
void initialise_temples()
{
    //////////////////////////////////////////
    // First determine main temple map to use.
    map_def *main_temple = nullptr;
    int altar_count = 0;

    if (crawl_state.game_is_descent())
    {
        main_temple
            = const_cast<map_def*>(random_map_for_tag("temple_altars_0", false));

        if (main_temple == nullptr)
            end(1, false, "No temple of size 0");
    }
    else if (one_chance_in(100))
    {
        main_temple = const_cast<map_def*>(random_map_for_tag("temple_rare"));

        if (main_temple == nullptr)
            end(1, false, "No valid rare temples.");

        for (const auto &tag : main_temple->get_tags())
        {
            if (starts_with(tag, "temple_altars_"))
            {
                altar_count =
                    atoi(tag_without_prefix(tag,
                                            "temple_altars_").c_str());
            }
        }
    }
    else
    {
        // distribution of altar count has a mean at 13.5, with the extremes
        // occurring approximately 2.5% of the time each (a much thicker tail
        // than using 6 + random2avg(16,2)).
        // XXX: slightly out of date?
        if (coinflip())
            altar_count = max(6, 5 + random2max(9, 2));
        else
            altar_count = min(22, 14 + random2min(9, 2));

        const string vault_tag = make_stringf("temple_altars_%d", altar_count);
        do
        {
            main_temple
                = const_cast<map_def*>(random_map_for_tag(vault_tag, false));

            if (main_temple == nullptr)
                end(1, false, "No temple of size %d", altar_count);

        } while (main_temple->has_tag("temple_rare"));
    }

    you.props[TEMPLE_SIZE_KEY] = altar_count;
    you.props[TEMPLE_MAP_KEY] = main_temple->name;
    const unsigned int main_temple_size = altar_count;

#ifdef DEBUG_TEMPLES
    mprf(MSGCH_DIAGNOSTICS, "Chose main temple %s, size %u",
         main_temple->name.c_str(), main_temple_size);
#endif

    ///////////////////////////////////
    // Now set up the overflow temples.

    vector<god_type> god_list = temple_god_list();
    shuffle_array(god_list);

    vector<god_type> overflow_gods;

    while (god_list.size() > main_temple_size)
    {
        overflow_gods.push_back(god_list.back());
        god_list.pop_back();
    }

#ifdef DEBUG_TEMPLES
    mprf(MSGCH_DIAGNOSTICS, "%u overflow altars", (unsigned int)overflow_gods.size());
#endif

    you.props.erase(TEMPLE_GODS_KEY);      // shouldn't be set normally, but
    you.props.erase(OVERFLOW_TEMPLES_KEY); // may be in tests
    CrawlVector &temple_gods
        = you.props[TEMPLE_GODS_KEY].new_vector(SV_BYTE);

    for (unsigned int i = 0; i < god_list.size(); i++)
        temple_gods.push_back((char) god_list[i]);

    CrawlVector &overflow_temples
        = you.props[OVERFLOW_TEMPLES_KEY].new_vector(SV_VEC);
    overflow_temples.resize(MAX_OVERFLOW_LEVEL);

    // Count god overflow temple weights.
    int overflow_weights[NUM_GODS + 1];
    overflow_weights[0] = 0;

    for (unsigned int i = 1; i < NUM_GODS; i++)
    {
        string mapname = make_stringf("temple_overflow_generic_%d", i);
        mapref_vector maps = find_maps_for_tag(mapname);
        if (!maps.empty())
        {
            int chance = 0;
            for (auto map : maps)
            {
                // XXX: this should handle level depth better
                chance += map->weight(level_id(BRANCH_DUNGEON,
                                               MAX_OVERFLOW_LEVEL));
            }
            overflow_weights[i] = chance;
        }
        else
            overflow_weights[i] = 0;
    }

    // Check for temple_overflow vaults that specify certain gods.
    mapref_vector maps;
    // the >1 range is based on previous code; 1-altar temple_overflow maps
    // are placed by the next part, though their weight is not used here. There
    // are currently no such vaults with more than 3 altars, but there's not
    // much cost to checking a few higher.
    for (int num = 2; num <= 5; num++)
    {
        mapref_vector num_maps = find_maps_for_tag(
            make_stringf("temple_overflow_%d", num));
        maps.insert(maps.end(), num_maps.begin(), num_maps.end());
    }

    for (const map_def *map : maps)
    {
        if (overflow_gods.size() < 2)
            break;
        unsigned int num = 0;
        vector<god_type> this_temple_gods;
        for (const auto &tag : map->get_tags())
        {
            if (!starts_with(tag, "temple_overflow_"))
                continue;
            string temple_tag = tag_without_prefix(tag, "temple_overflow_");
            if (temple_tag.empty())
            {
                mprf(MSGCH_ERROR, "Malformed temple tag '%s' in map %s",
                    tag.c_str(), map->name.c_str());
                continue;
            }
            int test_num;
            if (parse_int(temple_tag.c_str(), test_num) && test_num > 0)
                num = test_num;
            else
            {
                replace(temple_tag.begin(), temple_tag.end(), '_', ' ');
                god_type this_god = str_to_god(temple_tag);
                if (this_god == GOD_NO_GOD)
                {
                    mprf(MSGCH_ERROR, "Malformed temple tag '%s' in map %s",
                        tag.c_str(), map->name.c_str());
                    continue;
                }
                this_temple_gods.push_back(this_god);
            }
            if (num == 0)
            {
                if (this_temple_gods.size() > 0)
                {
                    mprf(MSGCH_ERROR,
                        "Map %s has temple_overflow_god tags but no count tag",
                        map->name.c_str());
                }
                continue;
            }
        }
        // there is one vault that currently triggers this, where it allows
        // one of two specified gods on a particular altar. This code won't
        // handle (or error) on that case right now.
        if (num != this_temple_gods.size())
            continue;

        // does this temple place only gods that we need to place?
        bool ok = true;
        for (auto god : this_temple_gods)
            if (count(overflow_gods.begin(), overflow_gods.end(), god) == 0)
            {
                ok = false;
                break;
            }
        if (!ok)
            continue;
        // finally: this overflow vault will place a subset of our current
        // overflow list. Do we actually place it?
        // TODO: The weight calculation here is kind of odd, though based on
        // what it is directly replacing. It should sum all compatible
        // maps first. But, the end result of this choice isn't a map anyways...
        // More generally, I wonder if this list should be shuffled before this
        // step, so that it's not prioritizing smaller vaults?
        int chance = map->weight(level_id(BRANCH_DUNGEON,
                                           MAX_OVERFLOW_LEVEL));
        if (x_chance_in_y(chance, overflow_weights[num] + chance))
        {
            vector<god_type> new_overflow_gods;
            for (auto god : overflow_gods)
                if (count(this_temple_gods.begin(), this_temple_gods.end(), god) == 0)
                    new_overflow_gods.push_back(god);
            _use_overflow_temple(this_temple_gods);

            overflow_gods = new_overflow_gods;
        }
    }

    // NOTE: The overflow temples don't have to contain only one
    // altar; they can contain any number of altars, so long as there's
    // at least one vault definition with the tag "overflow_temple_num"
    // (where "num" is the number of altars).
    for (unsigned int i = 0, size = overflow_gods.size(); i < size; i++)
    {
        unsigned int remaining_size = size - i;
        // At least one god.
        vector<god_type> this_temple_gods;
        this_temple_gods.push_back(overflow_gods[i]);

        // Maybe place a larger overflow temple.
        if (remaining_size > 1 && one_chance_in(remaining_size + 1))
        {
            vector<pair<unsigned int, int> > num_weights;
            unsigned int num_gods = 1;

            // Randomly choose from the sizes which have maps.
            for (unsigned int j = 2; j <= remaining_size; j++)
                if (overflow_weights[j] > 0)
                    num_weights.emplace_back(j, overflow_weights[j]);

            if (!num_weights.empty())
                num_gods = *(random_choose_weighted(num_weights));

            // Add any extra gods (the first was added already).
            for (; num_gods > 1; i++, num_gods--)
                this_temple_gods.push_back(overflow_gods[i + 1]);
        }

        _use_overflow_temple(this_temple_gods);
    }
}

void initialise_item_descriptions()
{
    // Must remember to check for already existing colours/combinations.
    you.item_description.init(255);

    // The order here must match that of IDESC in describe.h
    const int max_item_number[6] = { NUM_WANDS,
                                     NUM_POTIONS,
                                     NUM_SCROLLS,
                                     NUM_JEWELLERY,
                                     NUM_SCROLLS,
                                     NUM_STAVES };

    for (int i = 0; i < NUM_IDESC; i++)
    {
        // Only loop until NUM_WANDS etc.
        for (int j = 0; j < max_item_number[i]; j++)
        {
            // Don't override predefines
            if (you.item_description[i][j] != 255)
                continue;

            // Pick a new description until it's good.
            while (true)
            {
                switch (i)
                {
                case IDESC_WANDS: // wands
                    you.item_description[i][j] = random2(NDSC_WAND_PRI
                                                         * NDSC_WAND_SEC);
                    if (coinflip())
                        you.item_description[i][j] %= NDSC_WAND_PRI;
                    break;

                case IDESC_POTIONS: // potions
                    you.item_description[i][j] = _random_potion_description();
                    break;


#if TAG_MAJOR_VERSION == 34
                case IDESC_SCROLLS_II: // unused but validated
#endif
                case IDESC_SCROLLS: // scrolls: random seed for the name
                {
                    // this is very weird and probably a linleyism.
                    const int seed_1 = random2(151); // why 151?
                    const int seed_2 = random2(151);
                    const int seed_3 = OBJ_SCROLLS; // yes, really
                    you.item_description[i][j] =   seed_1
                                                | (seed_2 << 8)
                                                | (seed_3 << 16);
                    break;
                }

                case IDESC_RINGS: // rings and amulets
                    you.item_description[i][j] = random2(NDSC_JEWEL_PRI
                                                         * NDSC_JEWEL_SEC);
                    if (coinflip())
                        you.item_description[i][j] %= NDSC_JEWEL_PRI;
                    break;

                case IDESC_STAVES: // staves
                    you.item_description[i][j] = random2(NDSC_STAVE_PRI
                                                         * NDSC_STAVE_SEC);
                    break;
                }

                bool is_ok = true;

                // Test whether we've used this description before.
                // Don't have p < j because some are preassigned.
                for (int p = 0; p < max_item_number[i]; p++)
                {
                    if (p == j)
                        continue;

                    if (you.item_description[i][p] == you.item_description[i][j])
                    {
                        is_ok = false;
                        break;
                    }
                }
                if (is_ok)
                    break;
            }
        }
    }
}

void fix_up_jiyva_name()
{
    you.jiyva_second_name = make_name(rng::get_uint32(), MNAME_JIYVA);
    ASSERT(you.jiyva_second_name[0] == 'J');
}