diamond-square-generator.ts 6.42 KB
Newer Older
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
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
import { Grid } from "./grid.js";

export class DiamondSquareGenerator {
    constructor(private grid: Grid) {}

    generate() {
        this.grid.clear();

        this.initialize();

        this.step(this.grid.getSize());

        this.grid.smooth();
    }

    /**
     * Initializes the grid by setting the four corner points.
     */
    private initialize() {
        this.grid.setValue(Math.random(), 0, 0);
        this.grid.setValue(Math.random(), this.grid.getSize() - 1, 0);
        this.grid.setValue(Math.random(), this.grid.getSize() - 1, this.grid.getSize() - 1);
        this.grid.setValue(Math.random(), 0, this.grid.getSize() - 1);
    }

    /**
     * Performs a single step of the Diamond Square Algorithm at the given scale.
     * 
     * This function will recursively call itself until no more steps can be done.
     * @param scale
     */
    private step(scale: number) {
        if (scale <= 2) {
            return;
        }

        this.diamond(scale);
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
38
        this.square(scale);
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
39
40
41
42
43
44
45
46

        this.step(Math.ceil(scale / 2));
    }

    /**
     * Performs the 'square' step of the Diamond Square Algorithm at the given scale.
     * @param scale
     */
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
47
    private diamond(scale: number) {
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
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
        const steps = Math.floor(this.grid.getSize() / (scale - 1));
        const magnitude = scale / this.grid.getSize();

        // Do square step
        for(let x = 0; x < steps; x++) {
            for(let y = 0; y < steps; y++) {
                const xCoord = x * (scale - 1);
                const yCoord = y * (scale - 1);

                const maxLength = scale - 1;

                const values = [
                    this.grid.getValue(xCoord, yCoord),
                    this.grid.getValue(xCoord + maxLength, yCoord),
                    this.grid.getValue(xCoord + maxLength, yCoord + maxLength),
                    this.grid.getValue(xCoord, yCoord + maxLength),
                ];

                const average = this.getAverage(values);

                this.grid.setValue(
                    average + this.getRandomValue(magnitude),
                    xCoord + (scale >> 1), yCoord + (scale >> 1)
                );
            }
        }
    }

    /**
     * Performs the 'diamond' step of the Diamond Square Algorithm at the given scale.
     * @param scale 
     */
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
80
    private square(scale: number) {
Mathyn Buiteveld's avatar
Mathyn Buiteveld committed
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
        const steps = Math.floor(this.grid.getSize() / (scale - 1));
        const magnitude = scale / this.grid.getSize();

        // Do diamond step
        for(let x = 0; x < steps; x++) {
            for(let y = 0; y < steps; y++) {
                const xCoord = x * (scale - 1);
                const yCoord = y * (scale - 1);

                const maxLength = scale - 1;
                const halfSize = scale >> 1;

                // Top
                {
                    const values: number[] = [
                        this.grid.getValue(xCoord, yCoord),
                        this.grid.getValue(xCoord + maxLength, yCoord),
                        this.grid.getValue(xCoord + halfSize, yCoord + halfSize)
                    ];

                    if (this.grid.isInGrid(xCoord + halfSize, yCoord - halfSize)) {
                        values.push(this.grid.getValue(xCoord + halfSize, yCoord - halfSize));
                    }

                    const average = this.getAverage(values);

                    this.grid.setValue(
                        average + this.getRandomValue(magnitude),
                        xCoord + halfSize, yCoord
                    );
                }

                // bottom
                {
                    const values: number[] = [
                        this.grid.getValue(xCoord, yCoord + maxLength),
                        this.grid.getValue(xCoord + maxLength, yCoord + maxLength),
                        this.grid.getValue(xCoord + halfSize, yCoord + halfSize)
                    ];

                    if (this.grid.isInGrid(xCoord + halfSize, yCoord + maxLength + halfSize)) {
                        values.push(this.grid.getValue(xCoord + halfSize, yCoord + maxLength + halfSize));
                    }

                    const average = this.getAverage(values);

                    this.grid.setValue(
                        average + this.getRandomValue(magnitude),
                        xCoord + halfSize, yCoord + maxLength
                    );
                }

                // left
                {
                    const values: number[] = [
                        this.grid.getValue(xCoord, yCoord),
                        this.grid.getValue(xCoord, yCoord + maxLength),
                        this.grid.getValue(xCoord + halfSize, yCoord + halfSize)
                    ];

                    if (this.grid.isInGrid(xCoord - halfSize, yCoord + halfSize)) {
                        values.push(this.grid.getValue(xCoord - halfSize, yCoord + halfSize));
                    }

                    const average = this.getAverage(values);

                    this.grid.setValue(
                        average + this.getRandomValue(magnitude),
                        xCoord, yCoord + halfSize
                    );
                }

                // right
                {
                    const values: number[] = [
                        this.grid.getValue(xCoord + maxLength, yCoord),
                        this.grid.getValue(xCoord + maxLength, yCoord + maxLength),
                        this.grid.getValue(xCoord + halfSize, yCoord + halfSize)
                    ];

                    if (this.grid.isInGrid(xCoord + maxLength + halfSize, yCoord + halfSize)) {
                        values.push(this.grid.getValue(xCoord + maxLength + halfSize, yCoord + halfSize));
                    }

                    const average = this.getAverage(values);

                    this.grid.setValue(
                        average + this.getRandomValue(magnitude),
                        xCoord + maxLength, yCoord + halfSize
                    );
                }
            }
        }
    }

    /**
     * Returns a random number for the given magnitude.
     * @param magnitude 
     */
    private getRandomValue(magnitude: number): number {
        return Math.random() * magnitude - (magnitude / 2);
    }

    /**
     * Calculates the average of the given numbers.
     * @param values 
     */
    private getAverage(values: number[]): number {
        const sum = values.reduce((previous, current) => previous + current, 0);

        return sum / values.length;
    }
}