forked from Cubitect/cubiomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.c
3143 lines (2685 loc) · 83.4 KB
/
layers.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 "layers.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
//==============================================================================
// Essentials
//==============================================================================
int biomeExists(int mc, int id)
{
if (id >= ocean && id <= mountain_edge) return 1;
if (id >= jungle && id <= jungle_hills) return mc >= MC_1_2;
if (id >= jungle_edge && id <= badlands_plateau) return mc >= MC_1_7;
if (id >= small_end_islands && id <= end_barrens) return mc >= MC_1_9;
if (id >= warm_ocean && id <= deep_frozen_ocean) return mc >= MC_1_13;
switch (id)
{
case the_void:
return mc >= MC_1_9;
case sunflower_plains:
case desert_lakes:
case gravelly_mountains:
case flower_forest:
case taiga_mountains:
case swamp_hills:
case ice_spikes:
case modified_jungle:
case modified_jungle_edge:
case tall_birch_forest:
case tall_birch_hills:
case dark_forest_hills:
case snowy_taiga_mountains:
case giant_spruce_taiga:
case giant_spruce_taiga_hills:
case modified_gravelly_mountains:
case shattered_savanna:
case shattered_savanna_plateau:
case eroded_badlands:
case modified_wooded_badlands_plateau:
case modified_badlands_plateau:
return mc >= MC_1_7;
case bamboo_jungle:
case bamboo_jungle_hills:
return mc >= MC_1_14;
case soul_sand_valley:
case crimson_forest:
case warped_forest:
case basalt_deltas:
return mc >= MC_1_16;
case dripstone_caves:
case lush_caves:
return mc >= MC_1_17;
default:
return 0;
}
}
int isOverworld(int mc, int id)
{
if (!biomeExists(mc, id))
return 0;
if (id >= small_end_islands && id <= end_barrens) return 0;
if (id >= soul_sand_valley && id <= basalt_deltas) return 0;
switch (id)
{
case nether_wastes:
case the_end:
return 0;
case frozen_ocean:
return mc <= MC_1_6 || mc >= MC_1_13;
case mountain_edge:
return mc <= MC_1_6;
case deep_warm_ocean:
case the_void:
return 0;
case tall_birch_hills:
return mc <= MC_1_8 || mc >= MC_1_11;
case dripstone_caves:
case lush_caves:
return 0;
}
return 1;
}
int getMutated(int mc, int id)
{
switch (id)
{
case plains: return sunflower_plains;
case desert: return desert_lakes;
case mountains: return gravelly_mountains;
case forest: return flower_forest;
case taiga: return taiga_mountains;
case swamp: return swamp_hills;
case snowy_tundra: return ice_spikes;
case jungle: return modified_jungle;
case jungle_edge: return modified_jungle_edge;
// emulate MC-98995
case birch_forest:
return (mc >= MC_1_9 && mc <= MC_1_10) ? tall_birch_hills : tall_birch_forest;
case birch_forest_hills:
return (mc >= MC_1_9 && mc <= MC_1_10) ? none : tall_birch_hills;
case dark_forest: return dark_forest_hills;
case snowy_taiga: return snowy_taiga_mountains;
case giant_tree_taiga: return giant_spruce_taiga;
case giant_tree_taiga_hills: return giant_spruce_taiga_hills;
case wooded_mountains: return modified_gravelly_mountains;
case savanna: return shattered_savanna;
case savanna_plateau: return shattered_savanna_plateau;
case badlands: return eroded_badlands;
case wooded_badlands_plateau: return modified_wooded_badlands_plateau;
case badlands_plateau: return modified_badlands_plateau;
default:
return none;
}
}
int getCategory(int mc, int id)
{
switch (id)
{
case beach:
case snowy_beach:
return beach;
case desert:
case desert_hills:
case desert_lakes:
return desert;
case mountains:
case mountain_edge:
case wooded_mountains:
case gravelly_mountains:
case modified_gravelly_mountains:
return mountains;
case forest:
case wooded_hills:
case birch_forest:
case birch_forest_hills:
case dark_forest:
case flower_forest:
case tall_birch_forest:
case tall_birch_hills:
case dark_forest_hills:
return forest;
case snowy_tundra:
case snowy_mountains:
case ice_spikes:
return snowy_tundra;
case jungle:
case jungle_hills:
case jungle_edge:
case modified_jungle:
case modified_jungle_edge:
case bamboo_jungle:
case bamboo_jungle_hills:
return jungle;
case badlands:
case eroded_badlands:
case modified_wooded_badlands_plateau:
case modified_badlands_plateau:
return mesa;
case wooded_badlands_plateau:
case badlands_plateau:
return mc <= MC_1_15 ? mesa : badlands_plateau;
case mushroom_fields:
case mushroom_field_shore:
return mushroom_fields;
case stone_shore:
return stone_shore;
case ocean:
case frozen_ocean:
case deep_ocean:
case warm_ocean:
case lukewarm_ocean:
case cold_ocean:
case deep_warm_ocean:
case deep_lukewarm_ocean:
case deep_cold_ocean:
case deep_frozen_ocean:
return ocean;
case plains:
case sunflower_plains:
return plains;
case river:
case frozen_river:
return river;
case savanna:
case savanna_plateau:
case shattered_savanna:
case shattered_savanna_plateau:
return savanna;
case swamp:
case swamp_hills:
return swamp;
case taiga:
case taiga_hills:
case snowy_taiga:
case snowy_taiga_hills:
case giant_tree_taiga:
case giant_tree_taiga_hills:
case taiga_mountains:
case snowy_taiga_mountains:
case giant_spruce_taiga:
case giant_spruce_taiga_hills:
return taiga;
default:
return none;
}
}
int areSimilar(int mc, int id1, int id2)
{
if (id1 == id2) return 1;
if (mc <= MC_1_15)
{
if (id1 == wooded_badlands_plateau || id1 == badlands_plateau)
return id2 == wooded_badlands_plateau || id2 == badlands_plateau;
}
return getCategory(mc, id1) == getCategory(mc, id2);
}
int isMesa(int id)
{
switch (id)
{
case badlands:
case eroded_badlands:
case modified_wooded_badlands_plateau:
case modified_badlands_plateau:
case wooded_badlands_plateau:
case badlands_plateau:
return 1;
default:
return 0;
}
}
int isShallowOcean(int id)
{
const uint64_t shallow_bits =
(1ULL << ocean) |
(1ULL << frozen_ocean) |
(1ULL << warm_ocean) |
(1ULL << lukewarm_ocean) |
(1ULL << cold_ocean);
return id < 64 && ((1ULL << id) & shallow_bits);
}
int isDeepOcean(int id)
{
const uint64_t deep_bits =
(1ULL << deep_ocean) |
(1ULL << deep_warm_ocean) |
(1ULL << deep_lukewarm_ocean) |
(1ULL << deep_cold_ocean) |
(1ULL << deep_frozen_ocean);
return id < 64 && ((1ULL << id) & deep_bits);
}
int isOceanic(int id)
{
const uint64_t ocean_bits =
(1ULL << ocean) |
(1ULL << frozen_ocean) |
(1ULL << warm_ocean) |
(1ULL << lukewarm_ocean) |
(1ULL << cold_ocean) |
(1ULL << deep_ocean) |
(1ULL << deep_warm_ocean) |
(1ULL << deep_lukewarm_ocean) |
(1ULL << deep_cold_ocean) |
(1ULL << deep_frozen_ocean);
return id < 64 && ((1ULL << id) & ocean_bits);
}
int isSnowy(int id)
{
switch (id)
{
case frozen_ocean:
case frozen_river:
case snowy_tundra:
case snowy_mountains:
case snowy_beach:
case snowy_taiga:
case snowy_taiga_hills:
case ice_spikes:
case snowy_taiga_mountains:
return 1;
default:
return 0;
}
}
void initBiomes()
{
}
void setLayerSeed(Layer *layer, uint64_t worldSeed)
{
if (layer->p2 != NULL)
setLayerSeed(layer->p2, worldSeed);
if (layer->p != NULL)
setLayerSeed(layer->p, worldSeed);
if (layer->noise != NULL)
{
uint64_t s;
setSeed(&s, worldSeed);
perlinInit((PerlinNoise*)layer->noise, &s);
}
uint64_t ls = layer->layerSalt;
if (ls == 0)
{ // Pre 1.13 the Hills branch stays zero-initialized
layer->startSalt = 0;
layer->startSeed = 0;
}
else if (ls == LAYER_INIT_SHA)
{ // Post 1.14 Voronoi uses SHA256 for initialization
layer->startSalt = getVoronoiSHA(worldSeed);
layer->startSeed = 0;
}
else
{
uint64_t st = worldSeed;
st = mcStepSeed(st, ls);
st = mcStepSeed(st, ls);
st = mcStepSeed(st, ls);
layer->startSalt = st;
layer->startSeed = mcStepSeed(st, 0);
}
}
//==============================================================================
// Noise
//==============================================================================
static double lerp(double part, double from, double to)
{
return from + part * (to - from);
}
static double lerp2(double dx, double dy, double v00, double v10, double v01, double v11)
{
return lerp(dy, lerp(dx, v00, v10), lerp(dx, v01, v11));
}
static double lerp3(double dx, double dy, double dz,
double v000, double v100, double v010, double v110,
double v001, double v101, double v011, double v111)
{
v000 = lerp2(dx, dy, v000, v100, v010, v110);
v001 = lerp2(dx, dy, v001, v101, v011, v111);
return lerp(dz, v000, v001);
}
static double clampedLerp(double part, double from, double to)
{
if (part <= 0) return from;
if (part >= 1) return to;
return lerp(part, from, to);
}
static double maintainPrecision(double x)
{
return x - floor(x / 33554432.0 + 0.5) * 33554432.0;
}
/* Table of vectors to cube edge centres (12 + 4 extra) */
const double cEdgeX[] = {1.0,-1.0, 1.0,-1.0, 1.0,-1.0, 1.0,-1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,-1.0, 0.0};
const double cEdgeY[] = {1.0, 1.0,-1.0,-1.0, 0.0, 0.0, 0.0, 0.0, 1.0,-1.0, 1.0,-1.0, 1.0,-1.0, 1.0,-1.0};
const double cEdgeZ[] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0,-1.0,-1.0, 1.0, 1.0,-1.0,-1.0, 0.0, 1.0, 0.0,-1.0};
// grad()
static double indexedLerp(int idx, double d1, double d2, double d3)
{
idx &= 0xf;
return cEdgeX[idx] * d1 + cEdgeY[idx] * d2 + cEdgeZ[idx] * d3;
}
void perlinInit(PerlinNoise *rnd, uint64_t *seed)
{
int i = 0;
memset(rnd, 0, sizeof(*rnd));
rnd->a = nextDouble(seed) * 256.0;
rnd->b = nextDouble(seed) * 256.0;
rnd->c = nextDouble(seed) * 256.0;
for (i = 0; i < 256; i++)
{
rnd->d[i] = i;
}
for (i = 0; i < 256; i++)
{
int n3 = nextInt(seed, 256 - i) + i;
int n4 = rnd->d[i];
rnd->d[i] = rnd->d[n3];
rnd->d[n3] = n4;
rnd->d[i + 256] = rnd->d[i];
}
}
double samplePerlin(const PerlinNoise *rnd, double d1, double d2, double d3,
double yamp, double ymin)
{
d1 += rnd->a;
d2 += rnd->b;
d3 += rnd->c;
int i1 = (int)d1 - (int)(d1 < 0);
int i2 = (int)d2 - (int)(d2 < 0);
int i3 = (int)d3 - (int)(d3 < 0);
d1 -= i1;
d2 -= i2;
d3 -= i3;
double t1 = d1*d1*d1 * (d1 * (d1*6.0-15.0) + 10.0);
double t2 = d2*d2*d2 * (d2 * (d2*6.0-15.0) + 10.0);
double t3 = d3*d3*d3 * (d3 * (d3*6.0-15.0) + 10.0);
if (yamp)
{
double yclamp = ymin < d2 ? ymin : d2;
d2 -= floor(yclamp / yamp) * yamp;
}
i1 &= 0xff;
i2 &= 0xff;
i3 &= 0xff;
int a1 = rnd->d[i1] + i2;
int a2 = rnd->d[a1] + i3;
int a3 = rnd->d[a1+1] + i3;
int b1 = rnd->d[i1+1] + i2;
int b2 = rnd->d[b1] + i3;
int b3 = rnd->d[b1+1] + i3;
double l1 = indexedLerp(rnd->d[a2], d1, d2, d3);
double l2 = indexedLerp(rnd->d[b2], d1-1, d2, d3);
double l3 = indexedLerp(rnd->d[a3], d1, d2-1, d3);
double l4 = indexedLerp(rnd->d[b3], d1-1, d2-1, d3);
double l5 = indexedLerp(rnd->d[a2+1], d1, d2, d3-1);
double l6 = indexedLerp(rnd->d[b2+1], d1-1, d2, d3-1);
double l7 = indexedLerp(rnd->d[a3+1], d1, d2-1, d3-1);
double l8 = indexedLerp(rnd->d[b3+1], d1-1, d2-1, d3-1);
l1 = lerp(t1, l1, l2);
l3 = lerp(t1, l3, l4);
l5 = lerp(t1, l5, l6);
l7 = lerp(t1, l7, l8);
l1 = lerp(t2, l1, l3);
l5 = lerp(t2, l5, l7);
return lerp(t3, l1, l5);
}
static double simplexGrad(int idx, double x, double y, double z, double d)
{
double con = d - x*x - y*y - z*z;
if (con < 0)
return 0;
con *= con;
return con * con * indexedLerp(idx, x, y, z);
}
double sampleSimplex2D(const PerlinNoise *rnd, double x, double y)
{
const double SKEW = 0.5 * (sqrt(3) - 1.0);
const double UNSKEW = (3.0 - sqrt(3)) / 6.0;
double hf = (x + y) * SKEW;
int hx = (int)floor(x + hf);
int hz = (int)floor(y + hf);
double mhxz = (hx + hz) * UNSKEW;
double x0 = x - (hx - mhxz);
double y0 = y - (hz - mhxz);
int offx = (x0 > y0);
int offz = !offx;
double x1 = x0 - offx + UNSKEW;
double y1 = y0 - offz + UNSKEW;
double x2 = x0 - 1.0 + 2.0 * UNSKEW;
double y2 = y0 - 1.0 + 2.0 * UNSKEW;
int gi0 = rnd->d[0xff & (hz)];
int gi1 = rnd->d[0xff & (hz + offz)];
int gi2 = rnd->d[0xff & (hz + 1)];
gi0 = rnd->d[0xff & (gi0 + hx)];
gi1 = rnd->d[0xff & (gi1 + hx + offx)];
gi2 = rnd->d[0xff & (gi2 + hx + 1)];
double t = 0;
t += simplexGrad(gi0 % 12, x0, y0, 0.0, 0.5);
t += simplexGrad(gi1 % 12, x1, y1, 0.0, 0.5);
t += simplexGrad(gi2 % 12, x2, y2, 0.0, 0.5);
return 70.0 * t;
}
void octaveInit(OctaveNoise *rnd, uint64_t *seed, PerlinNoise *octaves,
int omin, int len)
{
int end = omin+len-1;
int i;
if (len < 1 || end > 0)
{
printf("octavePerlinInit(): unsupported octave range\n");
return;
}
rnd->octaves = octaves;
rnd->octcnt = len;
if (end == 0)
{
perlinInit(&rnd->octaves[0], seed);
i = 1;
}
else
{
skipNextN(seed, -end*262);
i = 0;
}
for (; i < len; i++)
{
perlinInit(&rnd->octaves[i], seed);
}
rnd->persist = pow(2.0, end);
rnd->lacuna = 1.0 / ((1LL << len) - 1.0);
}
double sampleOctave(const OctaveNoise *rnd, double x, double y, double z)
{
double persist = rnd->persist;
double lacuna = rnd->lacuna;
double v = 0;
int i;
for (i = 0; i < rnd->octcnt; i++)
{
double ax = maintainPrecision(x * persist);
double ay = maintainPrecision(y * persist);
double az = maintainPrecision(z * persist);
v += lacuna * samplePerlin(rnd->octaves+i, ax, ay, az, 0, 0);
persist *= 0.5;
lacuna *= 2.0;
}
return v;
}
void doublePerlinInit(DoublePerlinNoise *rnd, uint64_t *seed,
PerlinNoise *octavesA, PerlinNoise *octavesB, int omin, int len)
{ // require: len >= 1 && omin+len <= 0
rnd->amplitude = (10.0 / 6.0) * len / (len + 1);
octaveInit(&rnd->octA, seed, octavesA, omin, len);
octaveInit(&rnd->octB, seed, octavesB, omin, len);
}
double sampleDoublePerlin(const DoublePerlinNoise *rnd,
double x, double y, double z)
{
const double f = 337.0 / 331.0;
double v = 0;
v += sampleOctave(&rnd->octA, x, y, z);
v += sampleOctave(&rnd->octB, x*f, y*f, z*f);
return v * rnd->amplitude;
}
void initSurfaceNoise(SurfaceNoise *rnd, uint64_t *seed,
double xzScale, double yScale, double xzFactor, double yFactor)
{
rnd->xzScale = xzScale;
rnd->yScale = yScale;
rnd->xzFactor = xzFactor;
rnd->yFactor = yFactor;
octaveInit(&rnd->octmin, seed, rnd->oct+0, -15, 16);
octaveInit(&rnd->octmax, seed, rnd->oct+16, -15, 16);
octaveInit(&rnd->octmain, seed, rnd->oct+32, -7, 8);
}
void initSurfaceNoiseEnd(SurfaceNoise *rnd, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
initSurfaceNoise(rnd, &s, 2.0, 1.0, 80.0, 160.0);
}
double sampleSurfaceNoise(const SurfaceNoise *rnd, int x, int y, int z)
{
double xzScale = 684.412 * rnd->xzScale;
double yScale = 684.412 * rnd->yScale;
double xzStep = xzScale / rnd->xzFactor;
double yStep = yScale / rnd->yFactor;
double minNoise = 0;
double maxNoise = 0;
double mainNoise = 0;
double persist = 1.0;
double dx, dy, dz, sy, ty;
int i;
for (i = 0; i < 16; i++)
{
dx = maintainPrecision(x * xzScale * persist);
dy = maintainPrecision(y * yScale * persist);
dz = maintainPrecision(z * xzScale * persist);
sy = yScale * persist;
ty = y * sy;
minNoise += samplePerlin(&rnd->octmin.octaves[i], dx, dy, dz, sy, ty) / persist;
maxNoise += samplePerlin(&rnd->octmax.octaves[i], dx, dy, dz, sy, ty) / persist;
if (i < 8)
{
dx = maintainPrecision(x * xzStep * persist);
dy = maintainPrecision(y * yStep * persist);
dz = maintainPrecision(z * xzStep * persist);
sy = yStep * persist;
ty = y * sy;
mainNoise += samplePerlin(&rnd->octmain.octaves[i], dx, dy, dz, sy, ty) / persist;
}
persist /= 2.0;
}
return clampedLerp(0.5 + 0.05*mainNoise, minNoise/512.0, maxNoise/512.0);
}
//==============================================================================
// Nether (1.16+) and End (1.9+) Biome Generation
//==============================================================================
void setNetherSeed(NetherNoise *nn, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
doublePerlinInit(&nn->temperature, &s, &nn->oct[0], &nn->oct[2], -7, 2);
setSeed(&s, seed+1);
doublePerlinInit(&nn->humidity, &s, &nn->oct[4], &nn->oct[6], -7, 2);
}
/* Gets the 3D nether biome at scale 1:4 (for 1.16+).
*/
int getNetherBiome(const NetherNoise *nn, int x, int y, int z, float *ndel)
{
const float npoints[5][4] = {
{ 0, 0, 0, nether_wastes },
{ 0, -0.5, 0, soul_sand_valley },
{ 0.4, 0, 0, crimson_forest },
{ 0, 0.5, 0.375*0.375, warped_forest },
{-0.5, 0, 0.175*0.175, basalt_deltas },
};
float temp = sampleDoublePerlin(&nn->temperature, x, y, z);
float humidity = sampleDoublePerlin(&nn->humidity, x, y, z);
int i, id = 0;
float dmin = FLT_MAX;
float dmin2 = FLT_MAX;
for (i = 0; i < 5; i++)
{
float dx = npoints[i][0] - temp;
float dy = npoints[i][1] - humidity;
float dsq = dx*dx + dy*dy + npoints[i][2];
if (dsq < dmin)
{
dmin2 = dmin;
dmin = dsq;
id = i;
}
else if (dsq < dmin2)
dmin2 = dsq;
}
if (ndel)
*ndel = sqrtf(dmin2) - sqrtf(dmin);
id = (int) npoints[id][3];
return id;
}
static void fillRad3D(int *out, int x, int z, int w, int h, int y, int yh, int id, float rad)
{
int r, rsq;
int i, j, k;
r = (int) (rad);
if (r <= 0)
return;
rsq = (int) floor(rad * rad);
for (k = -r; k <= r; k++)
{
int ak = y+k;
if (ak < 0 || ak >= yh)
continue;
int ksq = k*k;
int *yout = &out[(w*h)*ak];
for (j = -r; j <= r; j++)
{
int aj = z+j;
if (aj < 0 || aj >= h)
continue;
int jksq = j*j + ksq;
for (i = -r; i <= r; i++)
{
int ai = x+i;
if (ai < 0 || ai >= w)
continue;
int ijksq = i*i + jksq;
if (ijksq > rsq)
continue;
yout[aj*w+ai] = id;
}
}
}
}
int mapNether3D(const NetherNoise *nn, int *out, int x, int z, int w, int h,
int y, int yh, int scale, float confidence)
{
int i, j, k;
memset(out, 0, sizeof(int) * yh*w*h);
if (scale < 4)
scale = 1;
else
scale /= 4;
// The noisedelta is the distance between the first and second closest
// biomes within the noise space. Dividing this by the greatest possible
// gradient (~0.05) gives a minimum diameter of voxels around the sample
// cell that will have the same biome.
float invgrad = 1.0 / (confidence * 0.05 * 2) / scale;
for (k = 0; k < yh; k++)
{
int *yout = &out[(w*h)*k];
for (j = 0; j < h; j++)
{
for (i = 0; i < w; i++)
{
if (yout[j*w+i])
continue;
//yout[j*w+i] = getNetherBiome(nn, x+i, y+k, z+j, NULL);
//continue;
float noisedelta;
int xi = (x+i)*scale, yk = (y+k)*scale, zj = (z+j)*scale;
int v = getNetherBiome(nn, xi, yk, zj, &noisedelta);
yout[j*w+i] = v;
float cellrad = noisedelta * invgrad;
fillRad3D(out, i, j, w, h, k, yh, v, cellrad);
}
}
}
return 0;
}
int mapNether2D(const NetherNoise *nn, int *out, int x, int z, int w, int h)
{
return mapNether3D(nn, out, x, z, w, h, 0, 1, 4, 1.0);
}
void setEndSeed(EndNoise *en, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
skipNextN(&s, 17292);
perlinInit(en, &s);
}
static int getEndBiome(int hx, int hz, const uint16_t *hmap, int hw)
{
int i, j;
const uint16_t ds[26] = { // (25-2*i)*(25-2*i)
// 0 1 2 3 4 5 6 7 8 9 10 11 12
625, 529, 441, 361, 289, 225, 169, 121, 81, 49, 25, 9, 1,
// 13 14 15 16 17 18 19 20 21 22 23 24, 25
1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625,
};
const uint16_t *p_dsi = ds + (hx < 0);
const uint16_t *p_dsj = ds + (hz < 0);
const uint16_t *p_elev = hmap;
uint32_t h;
if (abs(hx) > 15 || abs(hz) > 15)
h = 14401;
else
h = 64 * (hx*hx + hz*hz);
for (j = 0; j < 25; j++)
{
uint16_t dsj = p_dsj[j];
uint16_t e;
uint32_t u;
for (i = 0; i < 25; i += 5)
{
if (U(e = p_elev[i+0]) && (u = (p_dsi[i+0] + (uint32_t)dsj) * e) < h) h = u;
if (U(e = p_elev[i+1]) && (u = (p_dsi[i+1] + (uint32_t)dsj) * e) < h) h = u;
if (U(e = p_elev[i+2]) && (u = (p_dsi[i+2] + (uint32_t)dsj) * e) < h) h = u;
if (U(e = p_elev[i+3]) && (u = (p_dsi[i+3] + (uint32_t)dsj) * e) < h) h = u;
if (U(e = p_elev[i+4]) && (u = (p_dsi[i+4] + (uint32_t)dsj) * e) < h) h = u;
}
p_elev += hw;
}
if (h < 3600)
return end_highlands;
else if (h <= 10000)
return end_midlands;
else if (h <= 14400)
return end_barrens;
return small_end_islands;
}
int mapEndBiome(const EndNoise *en, int *out, int x, int z, int w, int h)
{
int i, j;
int hw = w + 26;
int hh = h + 26;
uint16_t *hmap = (uint16_t*) malloc(hw * hh * sizeof(*hmap));
for (j = 0; j < hh; j++)
{
for (i = 0; i < hw; i++)
{
int64_t rx = x + i - 12;
int64_t rz = z + j - 12;
uint16_t v = 0;
if (rx*rx + rz*rz > 4096 && sampleSimplex2D(en, rx, rz) < -0.9f)
{
v = (llabs(rx) * 3439 + llabs(rz) * 147) % 13 + 9;
v *= v;
}
hmap[j*hw+i] = v;
}
}
for (j = 0; j < h; j++)
{
for (i = 0; i < w; i++)
{
int64_t hx = (i+x);
int64_t hz = (j+z);
if (hx*hx + hz*hz <= 4096L)
out[j*w+i] = the_end;
else
{
hx = 2*hx + 1;
hz = 2*hz + 1;
uint16_t *p_elev = &hmap[(hz/2-z)*hw + (hx/2-x)];
out[j*w+i] = getEndBiome(hx, hz, p_elev, hw);
}
}
}
free(hmap);
return 0;
}
int mapEnd(const EndNoise *en, int *out, int x, int z, int w, int h)
{
int cx = x >> 2;
int cz = z >> 2;
int cw = ((x+w) >> 2) + 1 - cx;
int ch = ((z+h) >> 2) + 1 - cz;
int *buf = (int*) malloc(cw * ch * sizeof(int));
mapEndBiome(en, buf, cx, cz, cw, ch);
int i, j;
for (j = 0; j < h; j++)
{
int cj = ((z+j) >> 2) - cz;
for (i = 0; i < w; i++)
{
int ci = ((x+i) >> 2) - cx;
int v = buf[cj*cw+ci];
out[j*w+i] = v;
}
}
free(buf);
return 0;
}
float getEndHeightNoise(const EndNoise *en, int x, int z)
{
int hx = x / 2;
int hz = z / 2;
int oddx = x % 2;
int oddz = z % 2;
int i, j;
int64_t h = 64 * (x*(int64_t)x + z*(int64_t)z);
for (j = -12; j <= 12; j++)
{
for (i = -12; i <= 12; i++)
{
int64_t rx = hx + i;
int64_t rz = hz + j;
uint16_t v = 0;
if (rx*rx + rz*rz > 4096 && sampleSimplex2D(en, rx, rz) < -0.9f)
{
v = (llabs(rx) * 3439 + llabs(rz) * 147) % 13 + 9;
rx = (oddx - i * 2);
rz = (oddz - j * 2);
int64_t noise = (rx*rx + rz*rz) * v*v;
if (noise < h)
h = noise;
}
}
}
float ret = 100 - sqrtf((float) h);
if (ret < -100) ret = -100;
if (ret > 80) ret = 80;
return ret;
}
void sampleNoiseColumnEnd(double column[], const SurfaceNoise *sn,
const EndNoise *en, int x, int z, int colymin, int colymax)
{
double depth = getEndHeightNoise(en, x, z) - 8.0f;
int y;
for (y = colymin; y <= colymax; y++)
{
double noise = sampleSurfaceNoise(sn, x, y, z);
noise += depth; // falloff for the End is just the depth
// clamp top and bottom slides from End settings
noise = clampedLerp((32 + 46 - y) / 64.0, -3000, noise);
noise = clampedLerp((y - 1) / 7.0, -30, noise);
column[y - colymin] = noise;
}
}
/* Given bordering noise columns and a fractional position between those,
* determine the surface block height (i.e. where the interpolated noise > 0).
* Note that the noise columns should be of size: ncolxz[ colymax-colymin+1 ]
*/
int getSurfaceHeight(
const double ncol00[], const double ncol01[],
const double ncol10[], const double ncol11[],
int colymin, int colymax, int blockspercell, double dx, double dz)
{
int y, celly;
for (celly = colymax-1; celly >= colymin; celly--)
{
int idx = celly - colymin;
double v000 = ncol00[idx];
double v001 = ncol01[idx];
double v100 = ncol10[idx];
double v101 = ncol11[idx];
double v010 = ncol00[idx+1];
double v011 = ncol01[idx+1];
double v110 = ncol10[idx+1];
double v111 = ncol11[idx+1];
for (y = blockspercell - 1; y >= 0; y--)
{
double dy = y / (double) blockspercell;