-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpawn.c
328 lines (290 loc) · 13.5 KB
/
Spawn.c
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
#include "Spawn.h"
// The maximum fitness that each climate by itself can equate to.
const double U_MAX_CLIMATE_FITNESSES[NP_MAX] = {169783107.122, 56953151.7314, 1521389678.6949422, 227197622.444, 0., 384556370.747};
double U_getFitness(const Pos *coord, const double *temperature, const double *humidity, const double *continentalness, const double *erosion, const double *weirdness, const bool post1_21_1) {
double offset, fitness = 0.;
// U_getFitnessBounded(coord, temperature, humidity, continentalness, erosion, weirdness, NULL, &fitness);
// Distance
if (coord) {
uint64_t squaredEuclid = (uint64_t)(coord->x) * coord->x + (uint64_t)(coord->z) * coord->z;
fitness = post1_21_1 ? squaredEuclid : (squaredEuclid*squaredEuclid)/390625.;
}
const double MULT = post1_21_1 ? 419430400000000. : 100000000.;
// Continentalness
if (continentalness) {
offset = *continentalness < -0.11 ? *continentalness + 0.11 : *continentalness > 1. ? *continentalness - 1. : 0.;
fitness += MULT*offset*offset;
}
// Weirdness
if (weirdness) {
double absWeirdness = fabs(*weirdness);
offset = absWeirdness < 0.16 ? 0.16 - absWeirdness : absWeirdness > 1 ? absWeirdness - 1. : 0.;
fitness += MULT*offset*offset;
}
// Erosion
if (erosion) {
double absErosion = fabs(*erosion);
offset = absErosion > 1. ? absErosion - 1. : 0.;
fitness += MULT*offset*offset;
}
// Temperature
if (temperature) {
double absTemperature = fabs(*temperature);
offset = absTemperature > 1. ? absTemperature - 1. : 0.;
fitness += MULT*offset*offset;
}
// Humidity
if (humidity) {
double absHumidity = fabs(*humidity);
offset = absHumidity > 1. ? absHumidity - 1. : 0.;
fitness += MULT*offset*offset;
}
return fitness;
}
bool U_getFitnessBounded(const Pos *coord, const double *temperature, const double *humidity, const double *continentalness, const double *erosion, const double *weirdness, const double *upperBound, const bool post1_21_1, double *fitness) {
if (!fitness) return false;
// Distance
if (coord) {
uint64_t squaredEuclid = (uint64_t)(coord->x) * coord->x + (uint64_t)(coord->z) * coord->z;
*fitness = post1_21_1 ? squaredEuclid : (squaredEuclid*squaredEuclid)/390625.;
}
if (upperBound && *fitness >= *upperBound) return false;
const double MULT = post1_21_1 ? 419430400000000. : 100000000.;
double offset;
// Continentalness
if (continentalness) {
offset = *continentalness < -0.11 ? *continentalness + 0.11 : *continentalness > 1. ? *continentalness - 1. : 0.;
*fitness += MULT*offset*offset;
}
if (upperBound && *fitness >= *upperBound) return false;
// Weirdness
if (weirdness) {
double absWeirdness = fabs(*weirdness);
offset = absWeirdness < 0.16 ? 0.16 - absWeirdness : absWeirdness > 1. ? absWeirdness - 1. : 0.;
*fitness += MULT*offset*offset;
}
if (upperBound && *fitness >= *upperBound) return false;
// Erosion
if (erosion) {
double absErosion = fabs(*erosion);
offset = absErosion > 1. ? absErosion - 1. : 0.;
*fitness += MULT*offset*offset;
}
if (upperBound && *fitness >= *upperBound) return false;
// Temperature
if (temperature) {
double absTemperature = fabs(*temperature);
offset = absTemperature > 1. ? absTemperature - 1. : 0.;
*fitness += MULT*offset*offset;
}
if (upperBound && *fitness >= *upperBound) return false;
// Humidity
if (humidity) {
double absHumidity = fabs(*humidity);
offset = absHumidity > 1. ? absHumidity - 1. : 0.;
*fitness += MULT*offset*offset;
}
return !upperBound || *fitness < *upperBound;
}
double U_sampleAndGetFitness(const Pos *coord, const PerlinNoise *oct, const bool post1_21_1, const bool largeBiomesFlag) {
// U_sampleAndGetFitnessBounded(coord, oct, NULL, largeBiomesFlag, &fitness);
if (!coord) return -INFINITY;
// Distance
uint64_t squaredEuclid = (uint64_t)(coord->x) * coord->x + (uint64_t)(coord->z) * coord->z;
double fitness = post1_21_1 ? squaredEuclid : (squaredEuclid*squaredEuclid)/390625.;
const double MULT = post1_21_1 ? 419430400000000. : 100000000.;
double px = coord->x, pz = coord->z;
U_sampleClimate(NP_SHIFT, oct, &px, &pz, largeBiomesFlag);
// Continentalness
double sample = U_sampleClimate(NP_CONTINENTALNESS, oct, &px, &pz, largeBiomesFlag);
double offset = sample < -0.11 ? sample + 0.11 : sample > 1. ? sample - 1. : 0.;
fitness += MULT*offset*offset;
// Weirdness
sample = fabs(U_sampleClimate(NP_WEIRDNESS, oct, &px, &pz, largeBiomesFlag));
offset = sample < 0.16 ? 0.16 - sample : sample > 1. ? sample - 1. : 0.;
fitness += MULT*offset*offset;
// Erosion
sample = fabs(U_sampleClimate(NP_EROSION, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
fitness += MULT*offset*offset;
// Temperature
sample = fabs(U_sampleClimate(NP_TEMPERATURE, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
fitness += MULT*offset*offset;
// Humidity
sample = fabs(U_sampleClimate(NP_HUMIDITY, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
fitness += MULT*offset*offset;
return fitness;
}
bool U_sampleAndGetFitnessBounded(const Pos *coord, const PerlinNoise *oct, const double *upperBound, const bool post1_21_1, const bool largeBiomesFlag, double *fitness) {
if (!coord || !fitness) return false;
// Distance
uint64_t squaredEuclid = (uint64_t)(coord->x) * coord->x + (uint64_t)(coord->z) * coord->z;
*fitness = post1_21_1 ? squaredEuclid : (squaredEuclid*squaredEuclid)/390625.;
if (upperBound && *fitness >= *upperBound) return false;
const double MULT = post1_21_1 ? 419430400000000. : 100000000.;
double px = coord->x, pz = coord->z;
U_sampleClimate(NP_SHIFT, oct, &px, &pz, largeBiomesFlag);
// Continentalness
double sample = U_sampleClimate(NP_CONTINENTALNESS, oct, &px, &pz, largeBiomesFlag);
double offset = sample < -0.11 ? sample + 0.11 : sample > 1. ? sample - 1. : 0.;
*fitness += MULT*offset*offset;
if (upperBound && *fitness >= *upperBound) return false;
// Weirdness
sample = fabs(U_sampleClimate(NP_WEIRDNESS, oct, &px, &pz, largeBiomesFlag));
offset = sample < 0.16 ? 0.16 - sample : sample > 1. ? sample - 1. : 0.;
*fitness += MULT*offset*offset;
if (upperBound && *fitness >= *upperBound) return false;
// Erosion
sample = fabs(U_sampleClimate(NP_EROSION, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
*fitness += MULT*offset*offset;
if (upperBound && *fitness >= *upperBound) return false;
// Temperature
sample = fabs(U_sampleClimate(NP_TEMPERATURE, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
*fitness += MULT*offset*offset;
if (upperBound && *fitness >= *upperBound) return false;
// Humidity
sample = fabs(U_sampleClimate(NP_HUMIDITY, oct, &px, &pz, largeBiomesFlag));
offset = sample > 1. ? sample - 1. : 0.;
*fitness += MULT*offset*offset;
return !upperBound || *fitness < *upperBound;
}
double U_getEffectiveDistance(const double fitness, const bool post1_21_1) {
if (fitness < 0.) return -INFINITY;
return post1_21_1 ? sqrt(fitness) : 25.*pow(fitness, 1./4);
}
double U_getEffectiveTemperature(const double fitness, const bool post1_21_1) {
if (fitness < 0.) return -INFINITY;
return 1. + sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
double U_getEffectiveHumidity(const double fitness, const bool post1_21_1) {
// return U_getEffectiveTemperature(fitness);
if (fitness < 0.) return -INFINITY;
return 1. + sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
double U_getEffectiveContinentalness(const double fitness, const bool post1_21_1) {
if (fitness < 0) return -INFINITY;
return -0.11 - sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
double U_getEffectiveErosion(const double fitness, const bool post1_21_1) {
// return U_getEffectiveTemperature(fitness);
if (fitness < 0.) return -INFINITY;
return 1. + sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
double U_getEffectiveWeirdnessOuter(const double fitness, const bool post1_21_1) {
// return U_getEffectiveTemperature(fitness);
if (fitness < 0.) return -INFINITY;
return 1. + sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
double U_getEffectiveWeirdnessInner(const double fitness, const bool post1_21_1) {
if (fitness < 0.) return -INFINITY;
if (fitness > (post1_21_1 ? 67108864000000. : 2560000.)) return INFINITY;
return 0.16 - sqrt(fitness)/(post1_21_1 ? 20480000. : 10000.);
}
bool U_singleStageSpawnBounded(PerlinNoise *oct, const SpawnResult *firstStageChosenResult, const double fitnessLowerBound, const bool post1_21_1, const bool largeBiomesFlag, SpawnResult *chosenResult, double *chosenFitness) {
#ifndef _SPAWN_TABLES_ARE_PRESENT
double fitness, bestFitness = INFINITY;
const double maxRad = 512.*(1 + 3*!firstStageChosenResult);
const double radInc = 32.*(1 + 3*!firstStageChosenResult);
for (double rad = 0.; rad <= maxRad; rad += radInc) {
for (double ang = 0.; ang <= U_TWO_PI; ang += rad ? radInc/rad : INFINITY) {
Pos pos = {sin(ang) * rad, cos(ang) * rad};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, post1_21_1, largeBiomesFlag, &fitness)) continue;
if (chosenResult) {
chosenResult->pos.x = pos.x;
chosenResult->pos.z = pos.z;
}
bestFitness = fitness;
if (*chosenFitness) *chosenFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
}
return true;
#else
static const int *TABLES[] = {U_SPAWN_FIRST_STAGE_VALS, U_SPAWN_SECOND_STAGE_VALS};
const int **CHOSEN_TABLE = firstStageChosenResult ? U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index] : U_SPAWN_FIRST_STAGE_VALS;
double bestFitness = INFINITY;
// TODO: Continue as soon as an individual samplePerlin pushes fitness over fitness?
for (size_t i = 0; i < sizeof(CHOSEN_TABLE)/sizeof(*CHOSEN_TABLE); ++i) {
double fitness = CHOSEN_TABLE[i][U_spawn_table_fitness];
if (fitness >= bestFitness) continue;
Pos pos = {CHOSEN_TABLE[i][U_spawn_table_x], CHOSEN_TABLE[i][U_spawn_table_z]};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, post1_21_1, largeBiomesFlag, &fitness)) continue;
if (chosenResult) chosenResult = i;
if (*chosenFitness) *chosenFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
return true;
#endif
}
bool U_firstStageSpawnBounded(PerlinNoise *oct, const double fitnessLowerBound, const bool post1_21_1, const bool largeBiomesFlag, SpawnResult *chosenResult, double *chosenFitness) {
#ifndef _SPAWN_TABLES_ARE_PRESENT
// Fallback in case tables were not linked
double fitness, bestFitness = INFINITY;
for (double rad = 0.; rad <= 2048.; rad += 512.) {
for (double ang = 0.; ang <= U_TWO_PI; ang += rad ? 512./rad : INFINITY) {
Pos pos = {sin(ang) * rad, cos(ang) * rad};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, post1_21_1, largeBiomesFlag, &fitness)) continue;
if (chosenResult) {
chosenResult->pos.x = pos.x;
chosenResult->pos.z = pos.z;
}
bestFitness = fitness;
if (*chosenFitness) *chosenFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
}
return true;
#else
double bestFitness = INFINITY;
// TODO: Continue as soon as an individual samplePerlin pushes fitness over fitness?
for (size_t i = 0; i < sizeof(U_SPAWN_FIRST_STAGE_VALS)/sizeof(*U_SPAWN_FIRST_STAGE_VALS); ++i) {
double fitness = U_SPAWN_FIRST_STAGE_VALS[i][U_spawn_table_fitness];
if (fitness >= bestFitness) continue;
Pos pos = {U_SPAWN_FIRST_STAGE_VALS[i][U_spawn_table_x], U_SPAWN_FIRST_STAGE_VALS[i][U_spawn_table_z]};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, largeBiomesFlag, &fitness)) continue;
if (chosenResult) chosenResult = i;
if (*chosenFitness) *chosenFitness = fitness;
bestFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
return true;
#endif
}
bool U_secondStageSpawnBounded(PerlinNoise *oct, const SpawnResult *firstStageChosenResult, const double firstStageChosenFitness, const double fitnessLowerBound, const bool post1_21_1, const bool largeBiomesFlag, SpawnResult *chosenResult, double *chosenFitness) {
#ifndef _SPAWN_TABLES_ARE_PRESENT
// Fallback in case tables were not linked
double fitness, bestFitness = firstStageChosenFitness;
for (double rad = 32.; rad <= 512.; rad += 32.) {
for (double ang = 0.; ang <= U_TWO_PI; ang += 32./rad) {
Pos pos = {firstStageChosenResult->pos.x + (int)(sin(ang) * rad), firstStageChosenResult->pos.z + (int)(cos(ang) * rad)};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, post1_21_1, largeBiomesFlag, &fitness)) continue;
if (chosenResult) {
chosenResult->pos.x = pos.x;
chosenResult->pos.z = pos.z;
}
bestFitness = fitness;
if (*chosenFitness) *chosenFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
}
return true;
#else
double bestFitness = firstStageChosenFitness;
// TODO: Continue as soon as an individual samplePerlin pushes fitness over fitness?
for (size_t i = 0; i < sizeof(U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index])/sizeof(*U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index]); ++i) {
double fitness = U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index][i][U_spawn_table_fitness];
if (fitness >= bestFitness) continue;
Pos pos = {U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index][i][U_spawn_table_x], U_SPAWN_SECOND_STAGE_VALS[firstStageChosenResult->index][i][U_spawn_table_z]};
if (!U_sampleAndGetFitnessBounded(&pos, oct, &bestFitness, post1_21_1, largeBiomesFlag, &fitness)) continue;
if (chosenResult) chosenResult = i;
if (*chosenFitness) *chosenFitness = fitness;
bestFitness = fitness;
if (bestFitness < fitnessLowerBound) return false;
}
return true;
#endif
}