-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharenagenerator.cpp
More file actions
144 lines (130 loc) · 3.43 KB
/
arenagenerator.cpp
File metadata and controls
144 lines (130 loc) · 3.43 KB
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
#include "arenagenerator.h"
#include <random>
#include <stdio.h>
ArenaGenerator::ArenaGenerator()
{
death_threshold = 3;
birth_threshold = 5;
}
Array ArenaGenerator::generate(
const int width,
const int height,
const int iterations,
const int rate,
const int mirror_iterations
)
{
this->width = width;
this->height = height;
this->iterations = iterations;
this->rate = rate;
this->mirror_iterations = mirror_iterations;
Array result;
map = new int[width *height];
buffer = new int[width *height];
generate_random();
simulate(iterations);
mirror(false);
simulate(mirror_iterations);
mirror(true);
simulate(mirror_iterations);
for (int i = 0; i < width * height; i++)
{
result.append(map[i]);
}
delete map;
delete buffer;
return result;
}
void ArenaGenerator::generate_random()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution < > distrib(0, 100);
for (int i = 0; i < width * height; i++)
{
map[i] = distrib(gen) <= rate ? 1 : 0;
}
}
int ArenaGenerator::apply_rules(
const int c,
const int tl,
const int t,
const int tr,
const int r,
const int br,
const int b,
const int bl,
const int l
)
{
int sum = tl + t + tr + r + br + b + bl + l;
if (c == 0 && sum >= birth_threshold) return 1;
else if (c == 1 && sum <= death_threshold) return 0;
return c;
}
int ArenaGenerator::get_point(const int x, const int y) const
{
if (x < 0 || x >= width || y < 0 || y >= height) return 1;
return map[y *width + x];
}
void ArenaGenerator::simulate(const int n)
{
for (int i = 0; i < n; i++)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int tl = get_point(x - 1, y - 1);
int t = get_point(x, y - 1);
int tr = get_point(x + 1, y - 1);
int r = get_point(x + 1, y);
int br = get_point(x + 1, y + 1);
int b = get_point(x, y + 1);
int bl = get_point(x - 1, y + 1);
int l = get_point(x - 1, y);
buffer[(y *width) + x] = apply_rules(map[(y *width) + x], tl, t, tr, r, br, b, bl, l);
}
}
/* Move data back to map from buffer */
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
map[y * width + x] = buffer[y * width + x];
}
}
}
}
void ArenaGenerator::mirror(bool vertical)
{
if (vertical)
{
for (int y = 0; y < height/2; y++)
{
for (int x = 0; x < width; x++)
{
map[(height - y - 1) * width + x] = map[y * width + x];
}
}
}
else
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width/2; x++)
{
map[y * width + (width - x - 1)] = buffer[y * width + x];
}
}
}
}
void ArenaGenerator::set_thresholds(const int b, const int d)
{
birth_threshold = b;
death_threshold = d;
}
void ArenaGenerator::_bind_methods()
{
ClassDB::bind_method(D_METHOD("generate", "width", "height", "iterations", "rate", "mirror_iterations"), &ArenaGenerator::generate);
ClassDB::bind_method(D_METHOD("set_thresholds", "b", "d"), &ArenaGenerator::set_thresholds);
}