forked from Cubitect/cubiomes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
finders.c
3988 lines (3622 loc) · 130 KB
/
finders.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "finders.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#define PI 3.141592653589793
//==============================================================================
// Finding Structure Positions
//==============================================================================
void setAttemptSeed(uint64_t *s, int cx, int cz)
{
*s ^= (uint64_t)(cx >> 4) ^ ( (uint64_t)(cz >> 4) << 4 );
setSeed(s, *s);
next(s, 31);
}
uint64_t getPopulationSeed(int mc, uint64_t ws, int x, int z)
{
Xoroshiro xr;
uint64_t s;
uint64_t a, b;
if (mc >= MC_1_18)
{
xSetSeed(&xr, ws);
a = xNextLongJ(&xr);
b = xNextLongJ(&xr);
}
else
{
setSeed(&s, ws);
a = nextLong(&s);
b = nextLong(&s);
}
if (mc >= MC_1_13)
{
a |= 1; b |= 1;
}
else
{
a = (int64_t)a / 2 * 2 + 1;
b = (int64_t)b / 2 * 2 + 1;
}
return (x * a + z * b) ^ ws;
}
int getStructureConfig(int structureType, int mc, StructureConfig *sconf)
{
switch (structureType)
{
case Feature:
*sconf = FEATURE_CONFIG;
return mc <= MC_1_12;
case Desert_Pyramid:
*sconf = mc <= MC_1_12 ? DESERT_PYRAMID_CONFIG_112 : DESERT_PYRAMID_CONFIG;
return mc >= MC_1_3;
case Jungle_Pyramid:
*sconf = mc <= MC_1_12 ? JUNGLE_PYRAMID_CONFIG_112 : JUNGLE_PYRAMID_CONFIG;
return mc >= MC_1_3;
case Swamp_Hut:
*sconf = mc <= MC_1_12 ? SWAMP_HUT_CONFIG_112 : SWAMP_HUT_CONFIG;
return mc >= MC_1_4;
case Igloo:
*sconf = mc <= MC_1_12 ? IGLOO_CONFIG_112 : IGLOO_CONFIG;
return mc >= MC_1_9;
case Village:
*sconf = mc <= MC_1_17 ? VILLAGE_CONFIG_117 : VILLAGE_CONFIG;
return 1;
case Ocean_Ruin:
*sconf = mc <= MC_1_15 ? OCEAN_RUIN_CONFIG_115 : OCEAN_RUIN_CONFIG;
return mc >= MC_1_13;
case Shipwreck:
*sconf = mc <= MC_1_15 ? SHIPWRECK_CONFIG_115 : SHIPWRECK_CONFIG;
return mc >= MC_1_13;
case Ruined_Portal:
*sconf = RUINED_PORTAL_CONFIG;
return mc >= MC_1_16;
case Ruined_Portal_N:
*sconf = mc <= MC_1_17 ? RUINED_PORTAL_N_CONFIG_117 : RUINED_PORTAL_CONFIG;
return mc >= MC_1_16;
case Monument:
*sconf = MONUMENT_CONFIG;
return mc >= MC_1_8;
case End_City:
*sconf = END_CITY_CONFIG;
return mc >= MC_1_9;
case Mansion:
*sconf = MANSION_CONFIG;
return mc >= MC_1_11;
case Outpost:
*sconf = OUTPOST_CONFIG;
return mc >= MC_1_14;
case Ancient_City:
*sconf = ANCIENT_CITY_CONFIG;
return mc >= MC_1_19;
case Treasure:
*sconf = TREASURE_CONFIG;
return mc >= MC_1_13;
case Mineshaft:
*sconf = MINESHAFT_CONFIG;
return 1;
case Fortress:
*sconf = mc <= MC_1_15 ? FORTRESS_CONFIG_115 : FORTRESS_CONFIG;
return 1;
case Bastion:
*sconf = BASTION_CONFIG;
return mc >= MC_1_16;
case End_Gateway:
*sconf = mc <= MC_1_15 ? END_GATEWAY_CONFIG_115 : END_GATEWAY_CONFIG;
return mc >= MC_1_13;
default:
memset(sconf, 0, sizeof(StructureConfig));
return 0;
}
}
// like getFeaturePos(), but modifies the rng seed
static inline
void getRegPos(Pos *p, uint64_t *s, int rx, int rz, StructureConfig sc)
{
setSeed(s, rx*341873128712ULL + rz*132897987541ULL + *s + sc.salt);
p->x = (int)(((uint64_t)rx * sc.regionSize + nextInt(s, sc.chunkRange)) << 4);
p->z = (int)(((uint64_t)rz * sc.regionSize + nextInt(s, sc.chunkRange)) << 4);
}
int getStructurePos(int structureType, int mc, uint64_t seed, int regX, int regZ, Pos *pos)
{
StructureConfig sconf;
#if STRUCT_CONFIG_OVERRIDE
if (!getStructureConfig_override(structureType, mc, &sconf))
#else
if (!getStructureConfig(structureType, mc, &sconf))
#endif
{
return 0;
}
switch (structureType)
{
case Feature:
case Desert_Pyramid:
case Jungle_Pyramid:
case Swamp_Hut:
case Igloo:
case Village:
case Ocean_Ruin:
case Shipwreck:
case Ruined_Portal:
case Ruined_Portal_N:
case Ancient_City:
*pos = getFeaturePos(sconf, seed, regX, regZ);
return 1;
case Monument:
case Mansion:
*pos = getLargeStructurePos(sconf, seed, regX, regZ);
return 1;
case End_City:
*pos = getLargeStructurePos(sconf, seed, regX, regZ);
return (pos->x*(int64_t)pos->x + pos->z*(int64_t)pos->z) >= 1008*1008LL;
case Outpost:
*pos = getFeaturePos(sconf, seed, regX, regZ);
setAttemptSeed(&seed, (pos->x) >> 4, (pos->z) >> 4);
return nextInt(&seed, 5) == 0;
case Treasure:
pos->x = (int)( ((uint32_t)regX << 4) + 9 );
pos->z = (int)( ((uint32_t)regZ << 4) + 9 );
seed = regX*341873128712ULL + regZ*132897987541ULL + seed + sconf.salt;
setSeed(&seed, seed);
return nextFloat(&seed) < 0.01;
case Mineshaft:
return getMineshafts(mc, seed, regX, regZ, regX, regZ, pos, 1);
case Fortress:
if (mc >= MC_1_18) {
*pos = getFeaturePos(sconf, seed, regX, regZ);
return 1; // fortresses gen where bastions don't (biome dependent)
} else if (mc >= MC_1_16) {
getRegPos(pos, &seed, regX, regZ, sconf);
return nextInt(&seed, 5) < 2;
} else {
setAttemptSeed(&seed, regX << 4, regZ << 4);
int valid = nextInt(&seed, 3) == 0;
pos->x = (int)((((uint64_t)regX << 4) + nextInt(&seed,8) + 4) << 4);
pos->z = (int)((((uint64_t)regZ << 4) + nextInt(&seed,8) + 4) << 4);
return valid;
}
case Bastion:
if (mc >= MC_1_18) {
*pos = getFeaturePos(sconf, seed, regX, regZ);
seed = chunkGenerateRnd(seed, pos->x >> 4, pos->z >> 4);
return nextInt(&seed, 5) >= 2;
} else {
getRegPos(pos, &seed, regX, regZ, sconf);
return nextInt(&seed, 5) >= 2;
}
case End_Gateway:
pos->x = (int)( ((uint32_t)regX << 4) );
pos->z = (int)( ((uint32_t)regZ << 4) );
seed = getPopulationSeed(mc, seed, pos->x, pos->z);
if (mc >= MC_1_18)
{
Xoroshiro xr;
seed += 10000*4;
xSetSeed(&xr, seed);
if (xNextFloat(&xr) >= 1.0/700)
return 0;
pos->x += xNextIntJ(&xr, 16);
pos->z += xNextIntJ(&xr, 16);
}
else
{
setSeed(&seed, seed + sconf.salt);
if (mc >= MC_1_17) {
if (nextFloat(&seed) >= 1.0/700)
return 0;
} else {
if (nextInt(&seed, 700) != 0)
return 0;
}
pos->x += nextInt(&seed, 16);
pos->z += nextInt(&seed, 16);
}
return 1;
default:
fprintf(stderr,
"ERR getStructurePos: unsupported structure type %d\n", structureType);
exit(-1);
}
return 0;
}
int getMineshafts(int mc, uint64_t seed, int cx0, int cz0, int cx1, int cz1,
Pos *out, int nout)
{
uint64_t s;
setSeed(&s, seed);
uint64_t a = nextLong(&s);
uint64_t b = nextLong(&s);
int i, j;
int n = 0;
for (i = cx0; i <= cx1; i++)
{
uint64_t aix = i * a ^ seed;
for (j = cz0; j <= cz1; j++)
{
setSeed(&s, aix ^ j * b);
if (mc >= MC_1_13)
{
if unlikely(nextDouble(&s) < 0.004)
{
if (out && n < nout)
{
out[n].x = (int)((uint32_t)i << 4);
out[n].z = (int)((uint32_t)j << 4);
}
n++;
}
}
else
{
skipNextN(&s, 1);
if unlikely(nextDouble(&s) < 0.004)
{
int d = i;
if (-i > d) d = -i;
if (+j > d) d = +j;
if (-j > d) d = -j;
if (d >= 80 || nextInt(&s, 80) < d)
{
if (out && n < nout)
{
out[n].x = (int)((uint32_t)i << 4);
out[n].z = (int)((uint32_t)j << 4);
}
n++;
}
}
}
}
}
return n;
}
//==============================================================================
// Checking Biomes & Biome Helper Functions
//==============================================================================
Pos locateBiome(
const Generator *g, int x, int y, int z, int radius,
const char *validBiomes, uint64_t *rng, int *passes)
{
Pos out = {x, z};
int i, j, found;
found = 0;
if (g->mc >= MC_1_18)
{
x >>= 2;
z >>= 2;
radius >>= 2;
uint64_t dat = 0;
for (j = -radius; j <= radius; j++)
{
for (i = -radius; i <= radius; i++)
{
int id, xi = x+i, zj = z+j;
// emulate dependent biome generation MC-241546
//id = getBiomeAt(g, 4, xi, y, zj);
id = sampleBiomeNoise(&g->bn, NULL, xi, y, zj, &dat, 0);
if (!validBiomes[id]) continue;
if (found == 0 || nextInt(rng, found+1) == 0)
{
out.x = (x+i) << 2;
out.z = (z+j) << 2;
}
found++;
}
}
}
else
{
int x1 = (x-radius) >> 2;
int z1 = (z-radius) >> 2;
int x2 = (x+radius) >> 2;
int z2 = (z+radius) >> 2;
int width = x2 - x1 + 1;
int height = z2 - z1 + 1;
Range r = {4, x1, z1, width, height, y, 1};
int *ids = allocCache(g, r);
genBiomes(g, ids, r);
if (g->mc >= MC_1_13)
{
for (i = 0, j = 2; i < width*height; i++)
{
if (!validBiomes[ids[i]]) continue;
if (found == 0 || nextInt(rng, j++) == 0)
{
out.x = (x1 + i%width) << 2;
out.z = (z1 + i/width) << 2;
found = 1;
}
}
found = j - 2;
}
else
{
for (i = 0; i < width*height; i++)
{
if (!validBiomes[ids[i]]) continue;
if (found == 0 || nextInt(rng, found + 1) == 0)
{
out.x = (x1 + i%width) << 2;
out.z = (z1 + i/width) << 2;
++found;
}
}
}
free(ids);
}
if (passes != NULL)
{
*passes = found;
}
return out;
}
static inline int valid_1x1(const Generator *g, int x, int y, int z,
Range r, int *buf, const char *valid)
{
int *p = buf + (x-r.x) + (z-r.z)*r.sx + (y-r.y)*(r.sx*r.sz);
if (*p)
return 1;
*p = -1;
int id = getBiomeAt(g, 4, x, y, z);
return valid[id];
}
int areBiomesViable(
const Generator *g, int x, int y, int z, int rad,
const char *validBiomes, int approx)
{
int x1 = (x - rad) >> 2, x2 = (x + rad) >> 2, sx = x2 - x1 + 1;
int z1 = (z - rad) >> 2, z2 = (z + rad) >> 2, sz = z2 - z1 + 1;
int y1, y2, sy;
if (g->mc >= MC_1_18)
{
y1 = (y - rad) >> 2, y2 = (y + rad) >> 2, sy = y2 - y1 + 1;
}
else
{
y1 = y2 = 0, sy = 1;
}
Range r = {4, x1, z1, sx, sz, y1, sy};
int *ids = allocCache(g, r);
int i, j, k;
int viable = 1;
const char *v = validBiomes;
// check corners
if (!valid_1x1(g, x1, y1, z1, r, ids, v)) goto L_no;
if (!valid_1x1(g, x2, y2, z2, r, ids, v)) goto L_no;
if (!valid_1x1(g, x1, y1, z2, r, ids, v)) goto L_no;
if (!valid_1x1(g, x2, y2, z1, r, ids, v)) goto L_no;
if (g->mc >= MC_1_18)
{ // 3D
if (!valid_1x1(g, x1, y2, z1, r, ids, v)) goto L_no;
if (!valid_1x1(g, x2, y1, z2, r, ids, v)) goto L_no;
if (!valid_1x1(g, x1, y2, z2, r, ids, v)) goto L_no;
if (!valid_1x1(g, x2, y1, z1, r, ids, v)) goto L_no;
}
if (approx >= 1) goto L_yes;
if (g->mc >= MC_1_18)
{
for (i = 0; i < sx; i++)
{
for (j = 0; j < sy; j++)
{
for (k = 0; k < sz; k++)
{
if (!valid_1x1(g, x1+i, y1+j, z1+k, r, ids, v))
goto L_no;
}
}
}
}
else if ((viable = !genBiomes(g, ids, r)))
{
for (i = 0; i < sx*sy*sz; i++)
{
if (!v[ids[i]])
goto L_no;
}
}
if (0) L_yes: viable = 1;
if (0) L_no: viable = 0;
free(ids);
return viable;
}
//==============================================================================
// Finding Strongholds and Spawn
//==============================================================================
const char* getValidStrongholdBiomes(int mc)
{
static const int strongholdBiomes[] = {
plains, desert, mountains, forest, taiga, snowy_tundra, snowy_mountains,
mushroom_fields, mushroom_field_shore, desert_hills, wooded_hills,
taiga_hills, mountain_edge, jungle,jungle_hills, jungle_edge,
stone_shore, birch_forest, birch_forest_hills, dark_forest, snowy_taiga,
snowy_taiga_hills, giant_tree_taiga, giant_tree_taiga_hills,
wooded_mountains, savanna, savanna_plateau, badlands,
wooded_badlands_plateau, badlands_plateau, sunflower_plains,
desert_lakes, gravelly_mountains, flower_forest, taiga_mountains,
ice_spikes, modified_jungle, modified_jungle_edge, tall_birch_forest,
tall_birch_hills, dark_forest_hills, snowy_taiga_mountains,
giant_spruce_taiga, giant_spruce_taiga_hills,
modified_gravelly_mountains, shattered_savanna,
shattered_savanna_plateau, eroded_badlands,
modified_wooded_badlands_plateau, modified_badlands_plateau,
bamboo_jungle, bamboo_jungle_hills, dripstone_caves, lush_caves, meadow,
grove, snowy_slopes, stony_peaks, jagged_peaks, frozen_peaks,
};
unsigned int i;
static char v15[256], v17[256], v18[256];
char *valid = (mc <= MC_1_15 ? v15 : mc <= MC_1_17 ? v17 : v18);
if (!valid[strongholdBiomes[0]])
{
for (i = 0; i < sizeof(strongholdBiomes)/sizeof(int); i++)
valid[ strongholdBiomes[i] ] = 1;
if (mc >= MC_1_18)
{
valid[stone_shore] = 0;
}
else if (mc >= MC_1_16)
{ // simulate MC-199298
valid[bamboo_jungle] = 0;
valid[bamboo_jungle_hills] = 0;
}
}
return valid;
}
Pos initFirstStronghold(StrongholdIter *sh, int mc, uint64_t s48)
{
double dist, angle;
uint64_t rnds;
Pos p;
setSeed(&rnds, s48);
angle = 2.0 * PI * nextDouble(&rnds);
if (mc >= MC_1_9)
dist = (4.0 * 32.0) + (nextDouble(&rnds) - 0.5) * 32 * 2.5;
else
dist = (1.25 + nextDouble(&rnds)) * 32.0;
p.x = ((int)round(cos(angle) * dist) << 4) + 8;
p.z = ((int)round(sin(angle) * dist) << 4) + 8;
if (sh)
{
sh->pos.x = sh->pos.z = 0;
sh->nextapprox = p;
sh->index = 0;
sh->ringnum = 0;
sh->ringmax = 3;
sh->ringidx = 0;
sh->angle = angle;
sh->dist = dist;
sh->rnds = rnds;
sh->mc = mc;
}
return p;
}
int nextStronghold(StrongholdIter *sh, const Generator *g)
{
sh->pos = locateBiome(g, sh->nextapprox.x, 0, sh->nextapprox.z, 112,
getValidStrongholdBiomes(sh->mc), &sh->rnds, NULL);
sh->ringidx++;
sh->angle += 2 * PI / sh->ringmax;
if (sh->ringidx == sh->ringmax)
{
sh->ringnum++;
sh->ringidx = 0;
sh->ringmax = sh->ringmax + 2*sh->ringmax / (sh->ringnum+1);
if (sh->ringmax > 128-sh->index)
sh->ringmax = 128-sh->index;
sh->angle += nextDouble(&sh->rnds) * PI * 2.0;
}
if (sh->mc >= MC_1_9)
{
sh->dist = (4.0 * 32.0) + (6.0 * sh->ringnum * 32.0) +
(nextDouble(&sh->rnds) - 0.5) * 32 * 2.5;
}
else
{
sh->dist = (1.25 + nextDouble(&sh->rnds)) * 32.0;
}
sh->nextapprox.x = ((int)round(cos(sh->angle) * sh->dist) << 4) + 8;
sh->nextapprox.z = ((int)round(sin(sh->angle) * sh->dist) << 4) + 8;
sh->index++;
return (sh->mc >= MC_1_9 ? 128 : 3) - (sh->index-1);
}
static double getGrassProbability(uint64_t seed, int biome, int x, int z)
{
(void) seed, (void) biome, (void) x, (void) z;
// TODO: Use ChunkGeneratorOverworld.generateHeightmap for better estimate.
// TODO: Try to determine the actual probabilities and build a statistic.
switch (biome)
{
case plains: return 1.0;
case mountains: return 0.8; // height dependent
case forest: return 1.0;
case taiga: return 1.0;
case swamp: return 0.3; // height dependent
case river: return 0.15;
case beach: return 0.0;
case snowy_tundra: return 0.02;
case snowy_mountains: return 0.02;
case wooded_hills: return 1.0;
case taiga_hills: return 1.0;
case mountain_edge: return 1.0; // height dependent
case jungle: return 1.0;
case jungle_hills: return 1.0;
case jungle_edge: return 1.0;
case birch_forest: return 1.0;
case birch_forest_hills: return 1.0;
case dark_forest: return 0.9;
case snowy_taiga: return 0.1; // below trees
case snowy_taiga_hills: return 0.1; // below trees
case giant_tree_taiga: return 0.6;
case giant_tree_taiga_hills: return 0.6;
case wooded_mountains: return 0.2; // height dependent
case savanna: return 1.0;
case savanna_plateau: return 0.9;
case wooded_badlands_plateau: return 0.0; // height dependent
case badlands_plateau: return 0.0; // height dependent
case sunflower_plains: return 1.0;
case gravelly_mountains: return 0.2;
case flower_forest: return 1.0;
case taiga_mountains: return 1.0;
case swamp_hills: return 0.9;
case modified_jungle: return 1.0;
case modified_jungle_edge: return 1.0;
case tall_birch_forest: return 1.0;
case tall_birch_hills: return 1.0;
case dark_forest_hills: return 0.9;
case snowy_taiga_mountains: return 0.1;
case giant_spruce_taiga: return 0.6;
case giant_spruce_taiga_hills: return 0.6;
case modified_gravelly_mountains: return 0.2;
case shattered_savanna: return 1.0;
case shattered_savanna_plateau: return 1.0;
case bamboo_jungle: return 0.4;
case bamboo_jungle_hills: return 0.4;
// NOTE: in rare circumstances you can get also get grassy islands that are
// completely in ocean variants...
default: return 0;
}
}
static const char* getValidSpawnBiomes()
{
static const int biomesToSpawnIn[] = {
forest, plains, taiga, taiga_hills, wooded_hills, jungle, jungle_hills
};
static char isValid[256];
unsigned int i;
if (!isValid[biomesToSpawnIn[0]])
for (i = 0; i < sizeof(biomesToSpawnIn) / sizeof(int); i++)
isValid[ biomesToSpawnIn[i] ] = 1;
return isValid;
}
static int findServerSpawn(const Generator *g, int chunkX, int chunkZ,
double *bx, double *bz, double *bn, double *accum)
{
int x, z;
if (g->mc >= MC_1_18)
{
// It seems the search for spawn in 1.18 looks for a block with a
// solid top and a height above sea level. We can approximate this by
// looking for a non-ocean biome at y=63 ~> y=16 at scale 1:4.
for (x = 0; x < 4; x++)
{
for (z = 0; z < 4; z++)
{
int x4 = (chunkX << 2) + x, z4 = (chunkZ << 2) + z;
int id = getBiomeAt(g, 4, x4, 16, z4);
if (isOceanic(id) || id == river)
continue;
*bx = x4 << 2;
*bz = z4 << 2;
*bn = 1;
return 1;
}
}
return 0;
}
else
{
Range r = {1, chunkX << 4, chunkZ << 4, 16, 16, 0, 1};
int *area = allocCache(g, r);
genBiomes(g, area, r);
for (x = 0; x < 16; x++)
{
for (z = 0; z < 16; z++)
{
Pos pos = {r.x+x, r.z+z};
int id = area[z*16 + x];
double gp = getGrassProbability(g->mc, id, pos.x, pos.z);
if (gp == 0)
continue;
*bx += *accum * gp * pos.x;
*bz += *accum * gp * pos.z;
*bn += *accum * gp;
*accum *= 1 - gp;
if (*accum < 0.001)
{
free(area);
return 1;
}
}
}
free(area);
return 0;
}
}
static
uint64_t getSpawnDist(const Generator *g, int x, int z)
{
int64_t np[6];
uint32_t flags = SAMPLE_NO_DEPTH | SAMPLE_NO_BIOME;
sampleBiomeNoise(&g->bn, np, x>>2, 0, z>>2, NULL, flags);
const int64_t spawn_np[][2] = {
{-10000,10000},{-10000,10000},{-1100,10000},{-10000,10000},{0,0},
{-10000,-1600},{1600,10000} // [6]: weirdness for the second noise point
};
uint64_t ds = 0, ds1 = 0, ds2 = 0;
uint64_t a, b, q, i;
for (i = 0; i < 5; i++)
{
a = +np[i] - (uint64_t)spawn_np[i][1];
b = -np[i] + (uint64_t)spawn_np[i][0];
q = (int64_t)a > 0 ? a : (int64_t)b > 0 ? b : 0;
ds += q * q;
}
a = +np[5] - (uint64_t)spawn_np[5][1];
b = -np[5] + (uint64_t)spawn_np[5][0];
q = (int64_t)a > 0 ? a : (int64_t)b > 0 ? b : 0;
ds1 = ds + q*q;
a = +np[5] - (uint64_t)spawn_np[6][1];
b = -np[5] + (uint64_t)spawn_np[6][0];
q = (int64_t)a > 0 ? a : (int64_t)b > 0 ? b : 0;
ds2 = ds + q*q;
return ds1 <= ds2 ? ds1 : ds2;
}
static
void findFittest(const Generator *g, Pos *pos, uint64_t *fitness, double maxrad, double step)
{
double rad = step, ang = 0;
Pos p = *pos;
while (rad <= maxrad)
{
int x = p.x + (int)(sin(ang) * rad);
int z = p.z + (int)(cos(ang) * rad);
// calc fitness
double d = ((double)x*x + (double)z*z) / (2500*2500);
uint64_t fit = (uint64_t)(d*d * 1e8);
// calculate the distance to the noise points for spawn
fit += getSpawnDist(g, x, z);
if (fit < *fitness)
{
pos->x = x;
pos->z = z;
*fitness = fit;
}
ang += step / rad;
if (ang <= PI*2)
continue;
ang = 0;
rad += step;
}
}
static
Pos findFittestPos(const Generator *g)
{
Pos spawn = {0, 0};
uint64_t fitness = getSpawnDist(g, 0, 0);
findFittest(g, &spawn, &fitness, 2048.0, 512.0);
findFittest(g, &spawn, &fitness, 512.0, 32.0);
// center of chunk
spawn.x = ((spawn.x >> 4) << 4) + 8;
spawn.z = ((spawn.z >> 4) << 4) + 8;
return spawn;
}
Pos getSpawn(const Generator *g)
{
const char *isSpawnBiome = getValidSpawnBiomes();
Pos spawn;
int found;
int i;
uint64_t rnd = 0;
if (g->mc <= MC_1_17)
{
setSeed(&rnd, g->seed);
spawn = locateBiome(g, 0, 63, 0, 256, isSpawnBiome, &rnd, &found);
if (!found)
{
spawn.x = spawn.z = 8;
}
}
else
{
spawn = findFittestPos(g);
}
double accum = 1;
double bx = 0;
double bz = 0;
double bn = 0;
double gp;
if (g->mc >= MC_1_13)
{
int j, k, u, v;
j = k = u = v = 0;
for (i = 0; i < 1024; i++)
{
if (j > -16 && j <= 16 && k > -16 && k <= 16)
{
if (findServerSpawn(g, (spawn.x>>4)+j, (spawn.z>>4)+k,
&bx, &bz, &bn, &accum))
{
spawn.x = (int) round(bx / bn);
spawn.z = (int) round(bz / bn);
return spawn;
}
}
if (j == k || (j < 0 && j == -k) || (j > 0 && j == 1 - k))
{
int tmp = u;
u = -v;
v = tmp;
}
j += u;
k += v;
}
}
else
{
for (i = 0; i < 1000; i++)
{
int biome = getBiomeAt(g, 1, spawn.x, 0, spawn.z);
gp = getGrassProbability(g->seed, biome, spawn.x, spawn.z);
bx += accum * gp * spawn.x;
bz += accum * gp * spawn.z;
bn += accum * gp;
accum *= 1 - gp;
if (accum < 0.001)
{
spawn.x = (int) round(bx / bn);
spawn.z = (int) round(bz / bn);
break;
}
spawn.x += nextInt(&rnd, 64) - nextInt(&rnd, 64);
spawn.z += nextInt(&rnd, 64) - nextInt(&rnd, 64);
}
}
return spawn;
}
Pos estimateSpawn(const Generator *g)
{
const char *isSpawnBiome = getValidSpawnBiomes();
Pos spawn;
if (g->mc <= MC_1_17)
{
int found;
uint64_t rnd;
setSeed(&rnd, g->seed);
spawn = locateBiome(g, 0, 63, 0, 256, isSpawnBiome, &rnd, &found);
if (!found)
{
spawn.x = spawn.z = 8;
}
if (g->mc >= MC_1_13)
{
spawn.x &= ~0xf;
spawn.z &= ~0xf;
}
}
else
{
spawn = findFittestPos(g);
}
return spawn;
}
//==============================================================================
// Validating Structure Positions
//==============================================================================
int isViableFeatureBiome(int mc, int structureType, int biomeID)
{
switch (structureType)
{
case Desert_Pyramid:
return biomeID == desert || biomeID == desert_hills;
case Jungle_Pyramid:
return (biomeID == jungle || biomeID == jungle_hills ||
biomeID == bamboo_jungle || biomeID == bamboo_jungle_hills);
case Swamp_Hut:
return biomeID == swamp;
case Igloo:
if (mc < MC_1_9) return 0;
return biomeID == snowy_tundra || biomeID == snowy_taiga || biomeID == snowy_slopes;
case Ocean_Ruin:
if (mc < MC_1_13) return 0;
return isOceanic(biomeID);
case Shipwreck:
if (mc < MC_1_13) return 0;
return isOceanic(biomeID) || biomeID == beach || biomeID == snowy_beach;
case Ruined_Portal:
case Ruined_Portal_N:
return mc >= MC_1_16;
case Ancient_City:
if (mc < MC_1_19) return 0;
return biomeID == deep_dark;
case Treasure:
if (mc < MC_1_13) return 0;
return biomeID == beach || biomeID == snowy_beach;
case Mineshaft:
return isOverworld(mc, biomeID);
case Monument:
if (mc < MC_1_8) return 0;
return isDeepOcean(biomeID);
case Outpost:
if (mc < MC_1_14) return 0;
if (mc >= MC_1_18) {
switch (biomeID) {
case desert:
case plains:
case savanna:
case snowy_plains:
case taiga:
case meadow:
case frozen_peaks:
case jagged_peaks:
case stony_peaks:
case snowy_slopes:
case grove:
return 1;
default:
return 0;
}
}
// fall through
case Village:
if (biomeID == plains || biomeID == desert || biomeID == savanna)
return 1;
if (mc >= MC_1_10 && biomeID == taiga)
return 1;
if (mc >= MC_1_14 && biomeID == snowy_tundra)
return 1;
if (mc >= MC_1_18 && biomeID == meadow)
return 1;
return 0;