-
Notifications
You must be signed in to change notification settings - Fork 106
/
finders.c
5619 lines (5167 loc) · 179 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 "biomes.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#define PI 3.14159265358979323846
//==============================================================================
// 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)
{
static const StructureConfig
// for desert pyramids, jungle temples, witch huts and igloos prior to 1.13
s_feature = { 14357617, 32, 24, Feature, 0,0},
s_igloo_112 = { 14357617, 32, 24, Igloo, 0,0},
s_swamp_hut_112 = { 14357617, 32, 24, Swamp_Hut, 0,0},
s_desert_pyramid_112 = { 14357617, 32, 24, Desert_Pyramid, 0,0},
s_jungle_temple_112 = { 14357617, 32, 24, Jungle_Pyramid, 0,0},
// ocean features before 1.16
s_ocean_ruin_115 = { 14357621, 16, 8, Ocean_Ruin, 0,0},
s_shipwreck_115 = {165745295, 16, 8, Shipwreck, 0,0},
// 1.13 separated feature seeds by type
s_desert_pyramid = { 14357617, 32, 24, Desert_Pyramid, 0,0},
s_igloo = { 14357618, 32, 24, Igloo, 0,0},
s_jungle_temple = { 14357619, 32, 24, Jungle_Pyramid, 0,0},
s_swamp_hut = { 14357620, 32, 24, Swamp_Hut, 0,0},
s_outpost = {165745296, 32, 24, Outpost, 0,0},
s_village_117 = { 10387312, 32, 24, Village, 0,0},
s_village = { 10387312, 34, 26, Village, 0,0},
s_ocean_ruin = { 14357621, 20, 12, Ocean_Ruin, 0,0},
s_shipwreck = {165745295, 24, 20, Shipwreck, 0,0},
s_monument = { 10387313, 32, 27, Monument, 0,0},
s_mansion = { 10387319, 80, 60, Mansion, 0,0},
s_ruined_portal = { 34222645, 40, 25, Ruined_Portal, 0,0},
s_ruined_portal_n = { 34222645, 40, 25, Ruined_Portal, DIM_NETHER,0},
s_ruined_portal_n_117 = { 34222645, 25, 15, Ruined_Portal_N, DIM_NETHER,0},
s_ancient_city = { 20083232, 24, 16, Ancient_City, 0,0},
s_trail_ruins = { 83469867, 34, 26, Trail_Ruins, 0,0},
s_trial_chambers = { 94251327, 34, 22, Trial_Chambers, 0,0},
s_treasure = { 10387320, 1, 1, Treasure, 0,0},
s_mineshaft = { 0, 1, 1, Mineshaft, 0,0},
s_desert_well_115 = { 30010, 1, 1, Desert_Well, 0, 1.f/1000},
s_desert_well_117 = { 40013, 1, 1, Desert_Well, 0, 1.f/1000},
s_desert_well = { 40002, 1, 1, Desert_Well, 0, 1.f/1000},
s_geode_117 = { 20000, 1, 1, Geode, 0, 1.f/24},
s_geode = { 20002, 1, 1, Geode, 0, 1.f/24},
// nether and end structures
s_fortress_115 = { 0, 16, 8, Fortress, DIM_NETHER,0},
s_fortress = { 30084232, 27, 23, Fortress, DIM_NETHER,0},
s_bastion = { 30084232, 27, 23, Bastion, DIM_NETHER,0},
s_end_city = { 10387313, 20, 9, End_City, DIM_END,0},
// for the scattered return gateways
s_end_gateway_115 = { 30000, 1, 1, End_Gateway, DIM_END, 700},
s_end_gateway_116 = { 40013, 1, 1, End_Gateway, DIM_END, 700},
s_end_gateway_117 = { 40013, 1, 1, End_Gateway, DIM_END, 1.f/700},
s_end_gateway = { 40000, 1, 1, End_Gateway, DIM_END, 1.f/700},
s_end_island_116 = { 0, 1, 1, End_Island, DIM_END, 14},
s_end_island = { 0, 1, 1, End_Island, DIM_END, 1.f/14}
;
switch (structureType)
{
case Feature:
*sconf = s_feature;
return mc <= MC_1_12;
case Desert_Pyramid:
*sconf = mc <= MC_1_12 ? s_desert_pyramid_112 : s_desert_pyramid;
return mc >= MC_1_3;
case Jungle_Pyramid:
*sconf = mc <= MC_1_12 ? s_jungle_temple_112 : s_jungle_temple;
return mc >= MC_1_3;
case Swamp_Hut:
*sconf = mc <= MC_1_12 ? s_swamp_hut_112 : s_swamp_hut;
return mc >= MC_1_4;
case Igloo:
*sconf = mc <= MC_1_12 ? s_igloo_112 : s_igloo;
return mc >= MC_1_9;
case Village:
*sconf = mc <= MC_1_17 ? s_village_117 : s_village;
return mc >= MC_B1_8;
case Ocean_Ruin:
*sconf = mc <= MC_1_15 ? s_ocean_ruin_115 : s_ocean_ruin;
return mc >= MC_1_13;
case Shipwreck:
*sconf = mc <= MC_1_15 ? s_shipwreck_115 : s_shipwreck;
return mc >= MC_1_13;
case Ruined_Portal:
*sconf = s_ruined_portal;
return mc >= MC_1_16_1;
case Ruined_Portal_N:
*sconf = mc <= MC_1_17 ? s_ruined_portal_n_117 : s_ruined_portal_n;
return mc >= MC_1_16_1;
case Monument:
*sconf = s_monument;
return mc >= MC_1_8;
case End_City:
*sconf = s_end_city;
return mc >= MC_1_9;
case Mansion:
*sconf = s_mansion;
return mc >= MC_1_11;
case Outpost:
*sconf = s_outpost;
return mc >= MC_1_14;
case Ancient_City:
*sconf = s_ancient_city;
return mc >= MC_1_19_2;
case Treasure:
*sconf = s_treasure;
return mc >= MC_1_13;
case Mineshaft:
*sconf = s_mineshaft;
return mc >= MC_B1_8;
case Fortress:
*sconf = mc <= MC_1_15 ? s_fortress_115 : s_fortress;
return mc >= MC_1_0;
case Bastion:
*sconf = s_bastion;
return mc >= MC_1_16_1;
case End_Gateway:
if (mc <= MC_1_15) *sconf = s_end_gateway_115;
else if (mc <= MC_1_16) *sconf = s_end_gateway_116;
else if (mc <= MC_1_17) *sconf = s_end_gateway_117;
else *sconf = s_end_gateway;
// 1.11 and 1.12 generate gateways using a random source that passed
// the block filling, making them much more difficult to predict
return mc >= MC_1_13;
case End_Island:
if (mc <= MC_1_16) *sconf = s_end_island_116;
else *sconf = s_end_island;
return mc >= MC_1_13; // we only support decorator features for 1.13+
case Desert_Well:
if (mc <= MC_1_15) *sconf = s_desert_well_115;
else if (mc <= MC_1_17) *sconf = s_desert_well_117;
else *sconf = s_desert_well;
// wells were introduced in 1.2, but we only support decorator features
// for 1.13+
return mc >= MC_1_13;
case Geode:
*sconf = mc <= MC_1_17 ? s_geode_117 : s_geode;
return mc >= MC_1_17;
case Trail_Ruins:
*sconf = s_trail_ruins;
return mc >= MC_1_20;
case Trial_Chambers:
*sconf = s_trial_chambers;
return mc >= MC_1_21_1;
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 = ((uint64_t)rx * sc.regionSize + nextInt(s, sc.chunkRange)) << 4;
p->z = ((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:
case Trail_Ruins:
case Trial_Chambers:
*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 = regX * 16 + 9;
pos->z = regZ * 16 + 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_1) {
getRegPos(pos, &seed, regX, regZ, sconf);
return nextInt(&seed, 5) < 2;
} else {
setAttemptSeed(&seed, regX * 16, regZ * 16);
int valid = nextInt(&seed, 3) == 0;
pos->x = (regX * 16 + nextInt(&seed, 8) + 4) * 16;
pos->z = (regZ * 16 + nextInt(&seed, 8) + 4) * 16;
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:
case End_Island:
case Desert_Well:
case Geode:
// decorator features
pos->x = regX * 16;
pos->z = regZ * 16;
seed = getPopulationSeed(mc, seed, pos->x, pos->z);
if (mc >= MC_1_18)
{
Xoroshiro xr;
xSetSeed(&xr, seed + sconf.salt);
if (xNextFloat(&xr) >= sconf.rarity)
return 0;
pos->x += xNextIntJ(&xr, 16);
pos->z += xNextIntJ(&xr, 16);
}
else
{
setSeed(&seed, seed + sconf.salt);
if (sconf.rarity < 1.0) {
if (nextFloat(&seed) >= sconf.rarity)
return 0;
} else {
if (nextInt(&seed, (int)sconf.rarity) != 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 = i * 16;
out[n].z = j * 16;
}
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 = i * 16;
out[n].z = j * 16;
}
n++;
}
}
}
}
}
return n;
}
int getEndIslands(EndIsland islands[2], int mc, uint64_t seed, int chunkX, int chunkZ)
{
StructureConfig sconf;
if (!getStructureConfig(End_Island, mc, &sconf))
return 0;
int x = chunkX * 16;
int z = chunkZ * 16;
uint64_t rng = getPopulationSeed(mc, seed, x, z);
Xoroshiro xr;
float r;
if (mc <= MC_1_16)
{
setSeed(&rng, rng + sconf.salt);
if (nextInt(&rng, (int)sconf.rarity) != 0)
return 0;
islands[0].x = nextInt(&rng, 16) + x;
islands[0].y = nextInt(&rng, 16) + 55;
islands[0].z = nextInt(&rng, 16) + z;
if (nextInt(&rng, 4) != 0)
{
islands[0].r = nextInt(&rng, 3) + 4;
return 1;
}
islands[1].x = nextInt(&rng, 16) + x;
islands[1].y = nextInt(&rng, 16) + 55;
islands[1].z = nextInt(&rng, 16) + z;
islands[0].r = nextInt(&rng, 3) + 4;
for (r = islands[0].r; r > 0.5; r -= nextInt(&rng, 2) + 0.5);
islands[1].r = nextInt(&rng, 3) + 4;
return 2;
}
else if (mc <= MC_1_17)
{
setSeed(&rng, rng + sconf.salt);
if (nextFloat(&rng) >= sconf.rarity)
return 0;
int second = nextInt(&rng, 4) == 0;
islands[0].x = nextInt(&rng, 16) + x;
islands[0].z = nextInt(&rng, 16) + z;
islands[0].y = nextInt(&rng, 16) + 55;
islands[0].r = nextInt(&rng, 3) + 4;
for (r = islands[0].r; r > 0.5; r -= nextInt(&rng, 2) + 0.5);
if (!second)
return 1;
islands[1].x = nextInt(&rng, 16) + x;
islands[1].z = nextInt(&rng, 16) + z;
islands[1].y = nextInt(&rng, 16) + 55;
islands[1].r = nextInt(&rng, 3) + 4;
return 2;
}
else
{
xSetSeed(&xr, rng + sconf.salt);
if (xNextFloat(&xr) >= sconf.rarity)
return 0;
int second = (xNextIntJ(&xr, 4) == 3);
islands[0].x = xNextIntJ(&xr, 16) + x;
islands[0].z = xNextIntJ(&xr, 16) + z;
islands[0].y = xNextIntJ(&xr, 16) + 55;
islands[0].r = xNextIntJ(&xr, 3) + 4;
if (!second)
return 1;
for (r = islands[0].r; r > 0.5; r -= xNextIntJ(&xr, 2) + 0.5);
islands[1].x = xNextIntJ(&xr, 16) + x;
islands[1].z = xNextIntJ(&xr, 16) + z;
islands[1].y = xNextIntJ(&xr, 16) + 55;
islands[1].r = xNextIntJ(&xr, 3) + 4;
return 2;
}
}
static void applyEndIslandHeight(float *y, const EndIsland *island,
int x, int z, int w, int h, int scale)
{
int r = island->r;
int r2 = (r + 1) * (r + 1);
int x0 = floordiv(island->x - r, scale);
int z0 = floordiv(island->z - r, scale);
int x1 = floordiv(island->x + r, scale);
int z1 = floordiv(island->z + r, scale);
int ds = 0;
int i, j;
for (j = z0; j <= z1; j++)
{
if (j < z || j >= z+h)
continue;
int dz = j * scale - island->z + ds;
for (i = x0; i <= x1; i++)
{
if (i < x || i >= x+w)
continue;
int dx = i * scale - island->x + ds;
if (dx*dx + dz*dz > r2)
continue;
int idx = (j - z) * w + (i - x);
if (y[idx] < island->y)
y[idx] = island->y;
}
}
}
int mapEndIslandHeight(float *y, const EndNoise *en, uint64_t seed,
int x, int z, int w, int h, int scale)
{
int rmax = (6 + scale - 1) / scale;
int cx = floordiv(x - rmax, 16 / scale);
int cz = floordiv(z - rmax, 16 / scale);
int cw = floordiv(x + w + rmax, 16 / scale) - cx + 1;
int ch = floordiv(z + h + rmax, 16 / scale) - cz + 1;
int ci, cj;
int *ids = (int*) malloc(sizeof(int) * cw * ch);
mapEndBiome(en, ids, cx, cz, cw, ch);
for (cj = 0; cj < ch; cj++)
{
for (ci = 0; ci < cw; ci++)
{
if (ids[cj*cw + ci] != small_end_islands)
continue;
EndIsland islands[2];
int n = getEndIslands(islands, en->mc, seed, cx+ci, cz+cj);
while (n --> 0)
applyEndIslandHeight(y, islands+n, x, z, w, h, scale);
}
}
free(ids);
return 0;
}
float getEndHeightNoise(const EndNoise *en, int x, int z, int range);
int isEndChunkEmpty(const EndNoise *en, const SurfaceNoise *sn, uint64_t seed,
int chunkX, int chunkZ)
{
int i, j, k;
int x = chunkX * 2;
int z = chunkZ * 2;
double depth[3][3];
float y[256];
// check if small end islands intersect this chunk
for (j = -1; j <= +1; j++)
{
for (i = -1; i <= +1; i++)
{
EndIsland is[2];
int n = getEndIslands(is, en->mc, seed, chunkX+i, chunkZ+j);
while (n --> 0)
{
if (is[n].x + is[n].r <= chunkX*16) continue;
if (is[n].z + is[n].r <= chunkZ*16) continue;
if (is[n].x - is[n].r > chunkX*16 + 15) continue;
if (is[n].z - is[n].r > chunkZ*16 + 15) continue;
int id;
mapEndBiome(en, &id, is[n].x >> 4, is[n].z >> 4, 1, 1);
if (id == small_end_islands)
return 0;
}
}
}
// clamped (32 + 46 - y) / 64.0
static const double upper_drop[] = {
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 0-7
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 63./64, // 8-15
62./64, 61./64, 60./64, 59./64, 58./64, 57./64, 56./64, 55./64, // 16-23
54./64, 53./64, 52./64, 51./64, 50./64, 49./64, 48./64, 47./64, // 24-31
46./64 // 32
};
// clamped (y - 1) / 7.0
static const double lower_drop[] = {
0.0, 0.0, 1./7, 2./7, 3./7, 4./7, 5./7, 6./7, // 0-7
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 8-15
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 16-23
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 24-31
1.0, // 32
};
// inverse of clamping func: ( 30 * (1-l) / l + 3000 * (1-u) ) / u
static const double inverse_drop[] = {
1e9, 1e9, 180.0, 75.0, 40.0, 22.5, 12.0, 5.0, // 0-7
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // 8-14
1000./21, 3000./31, 9000./61, 200.0, // 15-18
};
const double eps = 0.001;
// get the inner depth values and see if they imply blocks in the chunk
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
depth[i][j] = getEndHeightNoise(en, x+i, z+j, 0) - 8.0f;
for (k = 8; k <= 14; k++)
{
double u = upper_drop[k];
double l = lower_drop[k];
double noise = depth[i][j];
double pivot = inverse_drop[k] - noise;
noise += sampleSurfaceNoiseBetween(sn, x+i, k, z+j, pivot-eps, pivot+eps);
noise = lerp(u, -3000, noise);
noise = lerp(l, -30, noise);
if (noise > 0)
return 0;
}
}
}
// fill in the depth values at the boundaries to neighbouring chunks
for (i = 0; i < 3; i++)
depth[i][2] = getEndHeightNoise(en, x+i, z+2, 0) - 8.0f;
for (j = 0; j < 2; j++)
depth[2][j] = getEndHeightNoise(en, x+2, z+j, 0) - 8.0f;
// see if none of the noise values can generate blocks
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
for (k = 2; k < 18; k++)
{
double u = upper_drop[k];
double l = lower_drop[k];
double noise = depth[i][j];
double pivot = inverse_drop[k] - noise;
noise += sampleSurfaceNoiseBetween(sn, x+i, k, z+j, pivot-eps, pivot+eps);
noise = lerp(u, -3000, noise);
noise = lerp(l, -30, noise);
if (noise > 0)
goto L_check_full;
}
}
}
return 1;
L_check_full:
mapEndSurfaceHeight(y, en, sn, chunkX*16, chunkZ*16, 16, 16, 1, 0);
for (k = 0; k < 256; k++)
if (y[k] != 0) return 0;
return 1;
}
//==============================================================================
// Checking Biomes & Biome Helper Functions
//==============================================================================
static int id_matches(int id, uint64_t b, uint64_t m)
{
return id < 128 ? !!(b & (1ULL << id)) : !!(m & (1ULL << (id-128)));
}
Pos locateBiome(
const Generator *g, int x, int y, int z, int radius,
uint64_t validB, uint64_t validM, 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++)
{
// emulate order-dependent biome generation MC-241546
//int id = getBiomeAt(g, 4, x+i, y, z+j);
int id = sampleBiomeNoise(&g->bn, NULL, x+i, y, z+j, &dat, 0);
if (!id_matches(id, validB, validM))
continue;
if (found == 0 || nextInt(rng, found+1) == 0)
{
out.x = (x+i) * 4;
out.z = (z+j) * 4;
}
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 (!id_matches(ids[i], validB, validM))
continue;
if (found == 0 || nextInt(rng, j++) == 0)
{
out.x = (x1 + i%width) * 4;
out.z = (z1 + i/width) * 4;
found = 1;
}
}
found = j - 2;
}
else
{
for (i = 0; i < width*height; i++)
{
if (!id_matches(ids[i], validB, validM))
continue;
if (found == 0 || nextInt(rng, found + 1) == 0)
{
out.x = (x1 + i%width) * 4;
out.z = (z1 + i/width) * 4;
++found;
}
}
}
free(ids);
}
if (passes != NULL)
{
*passes = found;
}
return out;
}
int areBiomesViable(
const Generator *g, int x, int y, int z, int rad,
uint64_t validB, uint64_t validM, 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 i, j, id, viable = 1;
int *ids = NULL;
// In 1.18+ the area is also checked in y, forming a cube volume.
// However, this function is only used for monuments, which need ocean or
// river, where we can get away with just checking the lowest y for caves.
y = (y - rad) >> 2;
// check corners
Pos corners[4] = { {x1,z1}, {x2,z2}, {x1,z2}, {x2,z1} };
for (i = 0; i < 4; i++)
{
id = getBiomeAt(g, 4, corners[i].x, y, corners[i].z);
if (id < 0 || !id_matches(id, validB, validM))
goto L_no;
}
if (approx >= 1) goto L_yes;
if (g->mc >= MC_1_18)
{
for (i = 0; i < sx; i++)
{
uint64_t dat = 0;
for (j = 0; j < sz; j++)
{
if (g->mc >= MC_1_18)
id = sampleBiomeNoise(&g->bn, NULL, x1+i, y, z1+j, &dat, 0);
else
id = getBiomeAt(g, 4, x1+i, y, z1+j);
if (id < 0 || !id_matches(id, validB, validM))
goto L_no;
}
}
}
else
{
Range r = {4, x1, z1, sx, sz, y, 1};
ids = allocCache(g, r);
if (genBiomes(g, ids, r))
goto L_no;
for (i = 0; i < sx*sz; i++)
{
if (id < 0 || !id_matches(ids[i], validB, validM))
goto L_no;
}
}
if (0) L_yes: viable = 1;
if (0) L_no: viable = 0;
if (ids)
free(ids);
return viable;
}
//==============================================================================
// Finding Strongholds and Spawn
//==============================================================================
int isStrongholdBiome(int mc, int id)
{
if (!isOverworld(mc, id))
return 0;
if (isOceanic(id))
return 0;
switch (id)
{
case plains:
case mushroom_fields:
case taiga_hills:
return mc >= MC_1_7;
case swamp:
return mc <= MC_1_6;
case river:
case frozen_river:
case beach:
case snowy_beach:
case swamp_hills:
return 0;
case mushroom_field_shore:
return mc >= MC_1_13;
case stone_shore:
return mc <= MC_1_17;
case bamboo_jungle:
case bamboo_jungle_hills:
// simulate MC-199298
return mc <= MC_1_15 || mc >= MC_1_18;
case mangrove_swamp:
case deep_dark:
return 0;
default:
return 1;
}
}
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) * 16) + 8;
p.z = ((int)round(sin(angle) * dist) * 16) + 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)
{
uint64_t validB = 0, validM = 0;
int i;
for (i = 0; i < 64; i++)
{
if (isStrongholdBiome(sh->mc, i))
validB |= (1ULL << i);
if (isStrongholdBiome(sh->mc, i+128))
validM |= (1ULL << i);
}
if (sh->mc > MC_1_19_2)
{
if (g)
{
uint64_t lbr = sh->rnds;
setSeed(&lbr, nextLong(&sh->rnds));
sh->pos = locateBiome(g, sh->nextapprox.x, 0, sh->nextapprox.z, 112,
validB, validM, &lbr, NULL);
}
else
{
nextLong(&sh->rnds);
sh->pos = sh->nextapprox;
}
}
else if (sh->mc >= MC_B1_8)
{
sh->pos = locateBiome(g, sh->nextapprox.x, 0, sh->nextapprox.z, 112,
validB, validM, &sh->rnds, NULL);
}
else
{
return 0;
}
// staircase is located at (4, 4) in chunk
sh->pos.x = (sh->pos.x & ~15) + 4;
sh->pos.z = (sh->pos.z & ~15) + 4;
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) * 16) + 8;
sh->nextapprox.z = ((int)round(sin(sh->angle) * sh->dist) * 16) + 8;
sh->index++;
return (sh->mc >= MC_1_9 ? 128 : 3) - (sh->index-1);
}
static
uint64_t calcFitness(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;
ds = ds1 <= ds2 ? ds1 : ds2;
// apply dependence on distance from origin
a = (int64_t)x*x;
b = (int64_t)z*z;
if (g->mc <= MC_1_21_1)
{
double s = (double)(a + b) / (2500 * 2500);
q = (uint64_t)(s*s * 1e8) + ds;
}
else
{
q = ds * (2048LL * 2048LL) + a + b;
}
return q;
}
static
void findFittest(const Generator *g, Pos *pos, uint64_t *fitness, double maxrad, double step)
{
double rad, ang;
Pos p = *pos;
for (rad = step; rad <= maxrad; rad += step)
{
for (ang = 0; ang <= PI*2; ang += step/rad)
{
int x = p.x + (int)(sin(ang) * rad);
int z = p.z + (int)(cos(ang) * rad);
uint64_t fit = calcFitness(g, x, z);
// Then update pos and fitness if combined total is lower/better
if (fit < *fitness)
{
pos->x = x;
pos->z = z;
*fitness = fit;
}