summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/dgn-height.cc
blob: 8a52736fb5bbf6b2d06b61aea79c7b687a872dac (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
/*
 *  File:       dgn-height.cc
 *  Summary:    Dungeon heightmap routines.
 */

#include "AppHdr.h"

#include "coord.h"
#include "coordit.h"
#include "dgn-height.h"
#include "dungeon.h"
#include "random.h"

const int SHOALS_ISLAND_COLLIDE_DIST2 = 5 * 5;

void dgn_initialise_heightmap(int height)
{
    env.heightmap.reset(new grid_heightmap);
    for (rectangle_iterator ri(0); ri; ++ri)
        dgn_height_at(*ri) = height;
}

int resolve_range(int_range range, int nrolls)
{
    return random_range(range.first, range.second, nrolls);
}

void dgn_island_centred_at(const coord_def &c,
                           int n_points,
                           int radius,
                           int_range height_delta_range,
                           int border_margin,
                           bool make_atoll)
{
    for (int i = 0; i < n_points; ++i) {
        const int thisrad = make_atoll? radius : random2(1 + radius);
        const coord_def p = dgn_random_point_from(c, thisrad, border_margin);
        if (!p.origin())
            dgn_height_at(p) += resolve_range(height_delta_range);
    }
}

void dgn_smooth_height_at(coord_def c, int radius, int max_height)
{
    const int height = dgn_height_at(c);
    if (max_height != DGN_UNDEFINED_HEIGHT && height > max_height)
        return;

    const int max_delta = radius * radius * 2 + 2;
    int divisor = 0;
    int total = 0;
    for (int y = c.y - radius; y <= c.y + radius; ++y) {
        for (int x = c.x - radius; x <= c.x + radius; ++x) {
            const coord_def p(x, y);
            if (!in_bounds(p))
                continue;
            const int nheight = dgn_height_at(p);
            if (max_height != DGN_UNDEFINED_HEIGHT && nheight > max_height)
                continue;
            const coord_def off = c - p;
            const int weight = max_delta - off.abs();
            divisor += weight;
            total += nheight * weight;
        }
    }
    dgn_height_at(c) = total / divisor;
}

void dgn_smooth_heights(int radius, int npasses)
{
    for (int i = 0; i < npasses; ++i)
    {
        for (rectangle_iterator ri(0); ri; ++ri)
            dgn_smooth_height_at(*ri, radius);
    }
}

//////////////////////////////////////////////////////////////////////
// dgn_island_plan

dgn_island_plan dgn_island_plan::shoals_islands(int margin)
{
    dgn_island_plan plan;
    plan.level_border_depth = margin;
    plan.n_aux_centres = int_range(0, 3);
    plan.aux_centre_offset_range = int_range(2, 10);

    plan.atoll_roll = 10;
    plan.island_separation_dist2 = SHOALS_ISLAND_COLLIDE_DIST2;

    plan.n_island_centre_delta_points = int_range(50, 60);
    plan.island_centre_radius_range = int_range(3, 10);
    plan.island_centre_point_height_increment = int_range(40, 60);

    plan.n_island_aux_delta_points = int_range(25, 45);
    plan.island_aux_radius_range = int_range(2, 7);
    plan.island_aux_point_height_increment = int_range(25, 35);

    return (plan);
}

void dgn_island_plan::build(int nislands)
{
    for (int i = 0; i < nislands; ++i)
        build_island();
}

coord_def dgn_island_plan::pick_island_spot()
{
    coord_def c;
    // Try to find a spot that's not too close to other islands; this
    // is not a guarantee, though.
    for (int i = 0; i < 15; ++i)
    {
        // Primary island centres should have a little clearance
        // around them, so use 2x the actual margin.
        c = dgn_random_point_in_margin(level_border_depth * 2);

        bool collides = false;
        for (int j = 0, size = islands.size(); j < size; ++j)
        {
            const coord_def island = islands[j];
            const coord_def dist = island - c;
            if (dist.abs() < island_separation_dist2)
            {
                collides = true;
                break;
            }
        }
        if (!collides)
            break;
    }
    islands.push_back(c);
    return c;
}

void dgn_island_plan::build_island()
{
    const coord_def c = pick_island_spot();
    dgn_island_centred_at(c, resolve_range(n_island_centre_delta_points),
                          resolve_range(island_centre_radius_range),
                          island_centre_point_height_increment,
                          level_border_depth,
                          x_chance_in_y(atoll_roll, 100));

    const int additional_heights = resolve_range(n_aux_centres);
    for (int i = 0; i < additional_heights; ++i) {
        const int addition_offset = resolve_range(aux_centre_offset_range);

        const coord_def offsetC =
            dgn_random_point_from(c, addition_offset, level_border_depth);
        if (!offsetC.origin())
            dgn_island_centred_at(
                offsetC, resolve_range(n_island_aux_delta_points),
                resolve_range(island_aux_radius_range),
                island_aux_point_height_increment,
                level_border_depth,
                x_chance_in_y(atoll_roll, 100));
    }
}

coord_def dgn_island_plan::pick_and_remove_random_island()
{
    if (islands.empty())
        return coord_def(0, 0);

    const int lucky_island = random2(islands.size());
    const coord_def c = islands[lucky_island];
    islands.erase(islands.begin() + lucky_island);
    return c;
}