-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_point_baseline.cpp
217 lines (199 loc) · 9.41 KB
/
single_point_baseline.cpp
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
#include <iostream>
#include <chrono>
#include <cstring>
#include "biomeGen.h"
#include "terrainGen.h"
static_assert(std::numeric_limits<double>::is_iec559, "This code requires IEEE-754 doubles");
static inline double lerp(double x, double a, double b) {
return a + x * (b - a);
}
static inline double grad(uint8_t hash, double x, double y, double z) {
switch (hash & 0xFu) {
case 0x0:
return x + y;
case 0x1:
return -x + y;
case 0x2:
return x - y;
case 0x3:
return -x - y;
case 0x4:
return x + z;
case 0x5:
return -x + z;
case 0x6:
return x - z;
case 0x7:
return -x - z;
case 0x8:
return y + z;
case 0x9:
return -y + z;
case 0xA:
return y - z;
case 0xB:
return -y - z;
case 0xC:
return y + x;
case 0xD:
return -y + z;
case 0xE:
return y - x;
case 0xF:
return -y - z;
default:
return 0; // never happens
}
}
static inline double grad2D(uint8_t hash,double x,double z){
return grad(hash,x,0,z);
}
static inline void generateFixedPermutations(double **buffer, double x, double z, int sizeX, int sizeZ, double noiseFactorX, double noiseFactorZ, double octaveSize, PermutationTable permutationTable) {
int index = 0;
uint8_t *permutations = permutationTable.permutations;
double octaveWidth = 1.0 / octaveSize;
for (int X = 0; X < sizeX; X++) {
double xCoord = (x + (double) X) * noiseFactorX + permutationTable.xo;
int clampedXCoord = (int) xCoord;
if (xCoord < (double) clampedXCoord) {
clampedXCoord--;
}
auto xBottoms = (int32_t) ((uint32_t) clampedXCoord & 0xffu);
xCoord -= clampedXCoord;
double fadeX = xCoord * xCoord * xCoord * (xCoord * (xCoord * 6.0 - 15.0) + 10.0);
for (int Z = 0; Z < sizeZ; Z++) {
double zCoord = (z + (double) Z) * noiseFactorZ + permutationTable.zo;
int clampedZCoord = (int) zCoord;
if (zCoord < (double) clampedZCoord) {
clampedZCoord--;
}
auto zBottoms = (int32_t) ((uint32_t) clampedZCoord & 0xffu);
zCoord -= clampedZCoord;
double fadeZ = zCoord * zCoord * zCoord * (zCoord * (zCoord * 6.0 - 15.0) + 10.0);
int hashXZ = permutations[permutations[xBottoms]] + zBottoms;
int hashOffXZ = permutations[permutations[xBottoms + 1]] + zBottoms;
double x1 = lerp(fadeX, grad2D(permutations[hashXZ], xCoord, zCoord), grad2D(permutations[hashOffXZ], xCoord - 1.0, zCoord));
double x2 = lerp(fadeX, grad2D(permutations[hashXZ + 1], xCoord, zCoord - 1.0), grad2D(permutations[hashOffXZ + 1], xCoord - 1.0, zCoord - 1.0));
double y1 = lerp(fadeZ, x1, x2);
(*buffer)[index] = (*buffer)[index] + y1 * octaveWidth;
index++;
}
}
}
static inline void generateNormalPermutations(double **buffer, double x, double y, double z, int sizeX, int sizeY, int sizeZ, double noiseFactorX, double noiseFactorY, double noiseFactorZ, double octaveSize, PermutationTable permutationTable) {
uint8_t *permutations = permutationTable.permutations;
double octaveWidth = 1.0 / octaveSize;
int32_t i2 = -1;
double x1 = 0.0;
double x2 = 0.0;
double xx1 = 0.0;
double xx2 = 0.0;
double t;
double w;
int columnIndex = 0;
for (int X = 0; X < sizeX; X++) {
double xCoord = (x + (double) X) * noiseFactorX + permutationTable.xo;
auto clampedXcoord = (int32_t) xCoord;
if (xCoord < (double) clampedXcoord) {
clampedXcoord--;
}
auto xBottoms = (uint8_t) ((uint32_t) clampedXcoord & 0xffu);
xCoord -= clampedXcoord;
t = xCoord * 6 - 15;
w = (xCoord * t + 10);
double fadeX = xCoord * xCoord * xCoord * w;
for (int Z = 0; Z < sizeZ; Z++) {
double zCoord = (z + (double) Z) * noiseFactorZ + permutationTable.zo;
auto clampedZCoord = (int32_t) zCoord;
if (zCoord < (double) clampedZCoord) {
clampedZCoord--;
}
auto zBottoms = (uint8_t) ((uint32_t) clampedZCoord & 0xffu);
zCoord -= clampedZCoord;
t = zCoord * 6 - 15;
w = (zCoord * t + 10);
double fadeZ = zCoord * zCoord * zCoord * w;
for (int Y = 0; Y < sizeY; Y++) {
double yCoords = (y + (double) Y) * noiseFactorY + permutationTable.yo;
auto clampedYCoords = (int32_t) yCoords;
if (yCoords < (double) clampedYCoords) {
clampedYCoords--;
}
auto yBottoms = (uint8_t) ((uint32_t) clampedYCoords & 0xffu);
yCoords -= clampedYCoords;
t = yCoords * 6 - 15;
w = yCoords * t + 10;
double fadeY = yCoords * yCoords * yCoords * w;
// ZCoord
if (Y == 0 || yBottoms != i2) { // this is wrong on so many levels, same ybottoms doesnt mean x and z were the same...
i2 = yBottoms;
uint16_t k2 = permutations[permutations[xBottoms] + yBottoms] + zBottoms;
uint16_t l2 = permutations[permutations[xBottoms] + yBottoms + 1] + zBottoms;
uint16_t k3 = permutations[permutations[xBottoms + 1] + yBottoms] + zBottoms;
uint16_t l3 = permutations[permutations[xBottoms + 1] + yBottoms + 1] + zBottoms;
x1 = lerp(fadeX, grad(permutations[k2], xCoord, yCoords, zCoord), grad(permutations[k3], xCoord - 1.0, yCoords, zCoord));
x2 = lerp(fadeX, grad(permutations[l2], xCoord, yCoords - 1.0, zCoord), grad(permutations[l3], xCoord - 1.0, yCoords - 1.0, zCoord));
xx1 = lerp(fadeX, grad(permutations[k2 + 1], xCoord, yCoords, zCoord - 1.0), grad(permutations[k3 + 1], xCoord - 1.0, yCoords, zCoord - 1.0));
xx2 = lerp(fadeX, grad(permutations[l2 + 1], xCoord, yCoords - 1.0, zCoord - 1.0), grad(permutations[l3 + 1], xCoord - 1.0, yCoords - 1.0, zCoord - 1.0));
}
double y1 = lerp(fadeY, x1, x2);
double y2 = lerp(fadeY, xx1, xx2);
(*buffer)[columnIndex] = (*buffer)[columnIndex] + lerp(fadeZ, y1, y2) * octaveWidth;
columnIndex++;
}
}
}
}
static inline void generateNoise(double *buffer, double chunkX, double chunkY, double chunkZ, int sizeX, int sizeY, int sizeZ, double offsetX, double offsetY, double offsetZ, PermutationTable *permutationTable, int nbOctaves) {
memset(buffer,0,sizeof(double)*sizeX * sizeZ*sizeY);
double octavesFactor = 1.0;
for (int octave = 0; octave < nbOctaves; octave++) {
generateNormalPermutations(&buffer, chunkX, chunkY, chunkZ, sizeX, sizeY, sizeZ, offsetX * octavesFactor, offsetY * octavesFactor, offsetZ * octavesFactor, octavesFactor, permutationTable[octave]);
octavesFactor /= 2.0;
}
}
static inline void generateFixedNoise(double *buffer, double chunkX, double chunkZ, int sizeX, int sizeZ, double offsetX, double offsetZ, PermutationTable *permutationTable, int nbOctaves) {
memset(buffer,0,sizeof(double)*sizeX * sizeZ);
double octavesFactor = 1.0;
for (int octave = 0; octave < nbOctaves; octave++) {
generateFixedPermutations(&buffer, chunkX, chunkZ, sizeX, sizeZ, offsetX * octavesFactor, offsetZ * octavesFactor, octavesFactor, permutationTable[octave]);
octavesFactor /= 2.0;
}
}
static inline void replaceBlockForBiomes(int chunkX, int chunkZ, TerrainNoises terrainNoises) {
double noiseFactor = 0.03125;
auto *sandFields = new double[16 * 16];
auto *gravelField = new double[16 * 16];
auto *heightField = new double[16 * 16];
generateNoise(sandFields, chunkX * 16, chunkZ * 16, 0.0, 16, 16, 1, noiseFactor, noiseFactor, 1.0, terrainNoises.shoresBottomComposition, 4);
// beware this error in alpha ;)
generateFixedNoise(gravelField, chunkZ * 16, chunkX * 16, 16, 16, noiseFactor, noiseFactor, terrainNoises.shoresBottomComposition, 4);
generateNoise(heightField, chunkX * 16, chunkZ * 16, 0.0, 16, 16, 1, noiseFactor * 2.0, noiseFactor * 2.0, noiseFactor * 2.0, terrainNoises.surfaceElevation, 4);
for (int i = 0; i < 256; ++i) printf("%f ",sandFields[i]);
printf("\n");
for (int i = 0; i < 256; ++i)printf("%f ",heightField[i]);
printf("\n");
// REMOVED
delete[] sandFields;
delete[] gravelField;
delete[] heightField;
}
void TerrainInternalWrapper(uint64_t worldSeed, int32_t chunkX, int32_t chunkZ) {
auto *terrainNoises = new TerrainNoises;
Random worldRandom = get_random(worldSeed);
// can be optimized out
PermutationTable *octaves = terrainNoises->minLimit;
initOctaves(octaves, &worldRandom, 16);
octaves = terrainNoises->maxLimit;
initOctaves(octaves, &worldRandom, 16);
octaves = terrainNoises->mainLimit;
initOctaves(octaves, &worldRandom, 8);
//crucial
octaves = terrainNoises->shoresBottomComposition;
initOctaves(octaves, &worldRandom, 4);
octaves = terrainNoises->surfaceElevation;
initOctaves(octaves, &worldRandom, 4);
replaceBlockForBiomes(chunkX, chunkZ, *terrainNoises);
delete terrainNoises;
}
int main() {TerrainInternalWrapper(18420882071630 ,-3 ,6);}