-
Notifications
You must be signed in to change notification settings - Fork 105
/
biomenoise.c
1975 lines (1777 loc) · 66.7 KB
/
biomenoise.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 "biomenoise.h"
#include "tables/btree18.h"
#include "tables/btree192.h"
#include "tables/btree19.h"
#include "tables/btree20.h"
#include "tables/btree21wd.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
//==============================================================================
// Noise
//==============================================================================
void initSurfaceNoise(SurfaceNoise *sn, int dim, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
octaveInit(&sn->octmin, &s, sn->oct+0, -15, 16);
octaveInit(&sn->octmax, &s, sn->oct+16, -15, 16);
octaveInit(&sn->octmain, &s, sn->oct+32, -7, 8);
if (dim == DIM_END)
{
sn->xzScale = 2.0;
sn->yScale = 1.0;
sn->xzFactor = 80;
sn->yFactor = 160;
}
else // DIM_OVERWORLD
{
octaveInit(&sn->octsurf, &s, sn->oct+40, -3, 4);
skipNextN(&s, 262*10);
octaveInit(&sn->octdepth, &s, sn->oct+44, -15, 16);
sn->xzScale = 0.9999999814507745;
sn->yScale = 0.9999999814507745;
sn->xzFactor = 80;
sn->yFactor = 160;
}
}
void initSurfaceNoiseBeta(SurfaceNoiseBeta *snb, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
octaveInitBeta(&snb->octmin, &s, snb->oct+0, 16, 684.412, 0.5, 1.0, 2.0);
octaveInitBeta(&snb->octmax, &s, snb->oct+16, 16, 684.412, 0.5, 1.0, 2.0);
octaveInitBeta(&snb->octmain, &s, snb->oct+32, 8, 684.412/80.0, 0.5, 1.0, 2.0);
skipNextN(&s, 262*8);
octaveInitBeta(&snb->octcontA, &s, snb->oct+40, 10, 1.121, 0.5, 1.0, 2.0);
octaveInitBeta(&snb->octcontB, &s, snb->oct+50, 16, 200.0, 0.5, 1.0, 2.0);
}
double sampleSurfaceNoiseBetween(const SurfaceNoise *sn, int x, int y, int z,
double noiseMin, double noiseMax)
{
double persist, amp;
double dx, dy, dz, sy;
int i;
double xzScale = 684.412 * sn->xzScale;
double yScale = 684.412 * sn->yScale;
double vmin = 0;
double vmax = 0;
persist = 1.0 / 32768.0;
amp = 64.0;
for (i = 15; i >= 0; i--)
{
dx = x * xzScale * persist;
dz = z * xzScale * persist;
sy = yScale * persist;
dy = y * sy;
vmin += samplePerlin(&sn->octmin.octaves[i], dx, dy, dz, sy, dy) * amp;
vmax += samplePerlin(&sn->octmax.octaves[i], dx, dy, dz, sy, dy) * amp;
if (vmin - amp > noiseMax && vmax - amp > noiseMax)
return noiseMax;
if (vmin + amp < noiseMin && vmax + amp < noiseMin)
return noiseMin;
amp *= 0.5;
persist *= 2.0;
}
double xzStep = xzScale / sn->xzFactor;
double yStep = yScale / sn->yFactor;
double vmain = 0.5;
persist = 1.0 / 128.0;
amp = 0.05 * 128.0;
for (i = 7; i >= 0; i--)
{
dx = x * xzStep * persist;
dz = z * xzStep * persist;
sy = yStep * persist;
dy = y * sy;
vmain += samplePerlin(&sn->octmain.octaves[i], dx, dy, dz, sy, dy) * amp;
if (vmain - amp > 1) return vmax;
if (vmain + amp < 0) return vmin;
amp *= 0.5;
persist *= 2.0;
}
return clampedLerp(vmain, vmin, vmax);
}
double sampleSurfaceNoise(const SurfaceNoise *sn, int x, int y, int z)
{
double xzScale = 684.412 * sn->xzScale;
double yScale = 684.412 * sn->yScale;
double xzStep = xzScale / sn->xzFactor;
double yStep = yScale / sn->yFactor;
double minNoise = 0;
double maxNoise = 0;
double mainNoise = 0;
double persist = 1.0;
double contrib = 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(&sn->octmin.octaves[i], dx, dy, dz, sy, ty) * contrib;
maxNoise += samplePerlin(&sn->octmax.octaves[i], dx, dy, dz, sy, ty) * contrib;
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(&sn->octmain.octaves[i], dx, dy, dz, sy, ty) * contrib;
}
persist *= 0.5;
contrib *= 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 },
};
y = 0;
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 y, int z, int sx, int sy, int sz,
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 >= sy)
continue;
int ksq = k*k;
int *yout = &out[(int64_t)ak*sx*sz];
for (j = -r; j <= r; j++)
{
int aj = z+j;
if (aj < 0 || aj >= sz)
continue;
int jksq = j*j + ksq;
for (i = -r; i <= r; i++)
{
int ai = x+i;
if (ai < 0 || ai >= sx)
continue;
int ijksq = i*i + jksq;
if (ijksq > rsq)
continue;
yout[(int64_t)aj*sx+ai] = id;
}
}
}
}
int mapNether3D(const NetherNoise *nn, int *out, Range r, float confidence)
{
int64_t i, j, k;
if (r.sy <= 0)
r.sy = 1;
if (r.scale <= 3)
{
printf("mapNether3D() invalid scale for this function\n");
return 1;
}
int scale = r.scale / 4;
memset(out, 0, sizeof(int) * r.sx*r.sy*r.sz);
// 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 < r.sy; k++)
{
int *yout = &out[k*r.sx*r.sz];
for (j = 0; j < r.sz; j++)
{
for (i = 0; i < r.sx; i++)
{
if (yout[j*r.sx+i])
continue;
//yout[j*w+i] = getNetherBiome(nn, x+i, y+k, z+j, NULL);
//continue;
float noisedelta;
int xi = (r.x+i)*scale;
int yk = (r.y+k);
int zj = (r.z+j)*scale;
int v = getNetherBiome(nn, xi, yk, zj, &noisedelta);
yout[j*r.sx+i] = v;
float cellrad = noisedelta * invgrad;
fillRad3D(out, i, j, k, r.sx, r.sy, r.sz, v, cellrad);
}
}
}
return 0;
}
int mapNether2D(const NetherNoise *nn, int *out, int x, int z, int w, int h)
{
Range r = {4, x, z, w, h, 0, 1};
return mapNether3D(nn, out, r, 1.0);
}
int genNetherScaled(const NetherNoise *nn, int *out, Range r, int mc, uint64_t sha)
{
if (r.scale <= 0) r.scale = 4;
if (r.sy == 0) r.sy = 1;
uint64_t siz = (uint64_t)r.sx*r.sy*r.sz;
if (mc <= MC_1_15)
{
uint64_t i;
for (i = 0; i < siz; i++)
out[i] = nether_wastes;
return 0;
}
if (r.scale == 1)
{
Range s = getVoronoiSrcRange(r);
int *src;
if (siz > 1)
{ // the source range is large enough that we can try optimizing
src = out + siz;
int err = mapNether3D(nn, src, s, 1.0);
if (err)
return err;
}
else
{
src = NULL;
}
int i, j, k;
int *p = out;
for (k = 0; k < r.sy; k++)
{
for (j = 0; j < r.sz; j++)
{
for (i = 0; i < r.sx; i++)
{
int x4, z4, y4;
voronoiAccess3D(sha, r.x+i, r.y+k, r.z+j, &x4, &y4, &z4);
if (src)
{
x4 -= s.x; y4 -= s.y; z4 -= s.z;
*p = src[(int64_t)y4*s.sx*s.sz + (int64_t)z4*s.sx + x4];
}
else
{
*p = getNetherBiome(nn, x4, y4, z4, NULL);
}
p++;
}
}
}
return 0;
}
else
{
return mapNether3D(nn, out, r, 1.0);
}
}
void setEndSeed(EndNoise *en, int mc, uint64_t seed)
{
uint64_t s;
setSeed(&s, seed);
skipNextN(&s, 17292);
perlinInit(&en->perlin, &s);
en->mc = mc;
}
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 = 64 * (hx*hx + hz*hz);
else
h = 14401;
for (j = 0; j < 25; j++)
{
uint16_t dsj = p_dsj[j];
uint16_t e;
uint32_t u;
// force unroll for(i=0;i<25;i++) in a cross compatible way
#define x5(i,x) { x; i++; x; i++; x; i++; x; i++; x; i++; }
#define for25(i,x) { i = 0; x5(i,x) x5(i,x) x5(i,x) x5(i,x) x5(i,x) }
for25(i,
if unlikely(e = p_elev[i])
{
if ((u = (p_dsi[i] + (uint32_t)dsj) * e) < h)
h = u;
}
);
#undef for25
#undef x5
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)
{
int64_t i, j;
int64_t hw = w + 26;
int64_t hh = h + 26;
uint16_t *hmap = (uint16_t*) malloc(sizeof(*hmap) * hw * hh);
for (j = 0; j < hh; j++)
{
for (i = 0; i < hw; i++)
{
int64_t rx = x + i - 12;
int64_t rz = z + j - 12;
uint64_t rsq = rx * rx + rz * rz;
uint16_t v = 0;
if (rsq > 4096 && sampleSimplex2D(&en->perlin, rx, rz) < -0.9f)
{
//v = (llabs(rx) * 3439 + llabs(rz) * 147) % 13 + 9;
v = (unsigned int)(
fabsf((float)rx) * 3439.0f + fabsf((float)rz) * 147.0f
) % 13 + 9;
v *= v;
}
hmap[(int64_t)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);
uint64_t rsq = hx * hx + hz * hz;
if (rsq <= 4096L)
out[j*w+i] = the_end;
else
{
hx = 2*hx + 1;
hz = 2*hz + 1;
if (en->mc > MC_1_13)
{ // add outer end rings
rsq = hx * hx + hz * hz;
if ((int)rsq < 0)
{
out[j*w+i] = end_barrens;
continue;
}
}
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;
int64_t cw = ((x+w) >> 2) + 1 - cx;
int64_t ch = ((z+h) >> 2) + 1 - cz;
int *buf = (int*) malloc(sizeof(int) * cw * ch);
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;
}
/* Samples the End height. The coordinates used here represent eight blocks per
* cell. By default a range of 12 cells is sampled, which can be overriden for
* optimization purposes.
*/
float getEndHeightNoise(const EndNoise *en, int x, int z, int range)
{
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);
if (range == 0)
range = 12;
for (j = -range; j <= range; j++)
{
for (i = -range; i <= range; i++)
{
int64_t rx = hx + i;
int64_t rz = hz + j;
uint64_t rsq = rx*rx + rz*rz;
uint16_t v = 0;
if (rsq > 4096 && sampleSimplex2D(&en->perlin, rx, rz) < -0.9f)
{
//v = (llabs(rx) * 3439 + llabs(rz) * 147) % 13 + 9;
v = (unsigned int)(
fabsf((float)rx) * 3439.0f + fabsf((float)rz) * 147.0f
) % 13 + 9;
rx = (oddx - i * 2);
rz = (oddz - j * 2);
rsq = rx*rx + rz*rz;
int64_t noise = rsq * 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)
{
// 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
};
int y;
if (en->mc > MC_1_13)
{ // add outer end rings
uint64_t rsq = (uint64_t) x * x + (uint64_t) z * z;
if ((int)rsq < 0)
{
for (y = colymin; y <= colymax; y++)
column[y - colymin] = nan("");
return;
}
}
// depth is between [-108, +72]
// noise is between [-128, +128]
// for a sold block we need the upper drop as:
// (72 + 128) * u - 3000 * (1-u) > 0 => upper_drop = u < 15/16
// which occurs at y = 18 for the highest relevant noise cell
// for the lower drop we need:
// (72 + 128) * l - 30 * (1-l) > 0 => lower_drop = l > 3/23
// which occurs at y = 3 for the lowest relevant noise cell
double depth = getEndHeightNoise(en, x, z, 0) - 8.0f;
for (y = colymin; y <= colymax; y++)
{
if (lower_drop[y] == 0.0) {
column[y - colymin] = -30;
continue;
}
double noise = sampleSurfaceNoiseBetween(sn, x, y, z, -128, +128);
double clamped = noise + depth;
clamped = lerp(upper_drop[y], -3000, clamped);
clamped = lerp(lower_drop[y], -30, clamped);
column[y - colymin] = clamped;
}
}
/* 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;
double noise = lerp3(dy, dx, dz, // Note: not x, y, z
v000, v010, v100, v110,
v001, v011, v101, v111);
if (noise > 0)
return celly * blockspercell + y;
}
}
return 0;
}
int getEndSurfaceHeight(int mc, uint64_t seed, int x, int z)
{
EndNoise en;
setEndSeed(&en, mc, seed);
SurfaceNoise sn;
initSurfaceNoise(&sn, DIM_END, seed);
// end noise columns vary on a grid of cell size = eight
int cellx = (x >> 3);
int cellz = (z >> 3);
double dx = (x & 7) / 8.0;
double dz = (z & 7) / 8.0;
// abusing enum for local compile time constants rather than enumeration
enum { y0 = 0, y1 = 32, yn = y1-y0+1 };
double ncol00[yn];
double ncol01[yn];
double ncol10[yn];
double ncol11[yn];
sampleNoiseColumnEnd(ncol00, &sn, &en, cellx, cellz, y0, y1);
sampleNoiseColumnEnd(ncol01, &sn, &en, cellx, cellz+1, y0, y1);
sampleNoiseColumnEnd(ncol10, &sn, &en, cellx+1, cellz, y0, y1);
sampleNoiseColumnEnd(ncol11, &sn, &en, cellx+1, cellz+1, y0, y1);
return getSurfaceHeight(ncol00, ncol01, ncol10, ncol11, y0, y1, 4, dx, dz);
}
int mapEndSurfaceHeight(float *y, const EndNoise *en, const SurfaceNoise *sn,
int x, int z, int w, int h, int scale, int ymin)
{
if (scale != 1 && scale != 2 && scale != 4 && scale != 8)
return 1;
int y0 = ymin >> 2;
if (y0 < 2) y0 = 2;
if (y0 > 17) y0 = 17;
int y1 = 18;
int yn = y1 - y0 + 1;
double cellmid = scale > 1 ? scale / 16.0 : 0;
int cellsiz = 8 / scale;
int cx = floordiv(x, cellsiz);
int cz = floordiv(z, cellsiz);
int cw = floordiv(x + w - 1, cellsiz) - cx + 2;
int i, j;
double *buf = malloc(sizeof(double) * yn * cw * 2);
double *ncol[2];
ncol[0] = buf;
ncol[1] = buf + yn * cw;
for (i = 0; i < cw; i++)
sampleNoiseColumnEnd(ncol[1]+i*yn, sn, en, cx+i, cz+0, y0, y1);
for (j = 0; j < h; j++)
{
int cj = floordiv(z + j, cellsiz);
int dj = z + j - cj * cellsiz;
if (j == 0 || dj == 0)
{
double *tmp = ncol[0];
ncol[0] = ncol[1];
ncol[1] = tmp;
for (i = 0; i < cw; i++)
sampleNoiseColumnEnd(ncol[1]+i*yn, sn, en, cx+i, cj+1, y0, y1);
}
for (i = 0; i < w; i++)
{
int ci = floordiv(x + i, cellsiz);
int di = x + i - ci * cellsiz;
double dx = di / (double) cellsiz + cellmid;
double dz = dj / (double) cellsiz + cellmid;
double *ncol0 = ncol[0] + (ci - cx) * yn;
double *ncol1 = ncol[1] + (ci - cx) * yn;
y[j*w+i] = getSurfaceHeight(ncol0, ncol1, ncol0+yn, ncol1+yn,
y0, y1, 4, dx, dz);
}
}
free(buf);
return 0;
}
int genEndScaled(const EndNoise *en, int *out, Range r, int mc, uint64_t sha)
{
if (mc < MC_1_0)
return 1;
if (r.sy == 0)
r.sy = 1;
if (mc <= MC_1_8)
{
uint64_t i, siz = (uint64_t)r.sx*r.sy*r.sz;
for (i = 0; i < siz; i++)
out[i] = the_end;
return 0;
}
int err, iy;
if (r.scale == 1)
{
Range s = getVoronoiSrcRange(r);
err = mapEnd(en, out, s.x, s.z, s.sx, s.sz);
if (err) return err;
if (mc <= MC_1_14)
{ // up to 1.14 voronoi noise is planar
Layer lvoronoi;
memset(&lvoronoi, 0, sizeof(Layer));
lvoronoi.startSalt = getLayerSalt(10);
err = mapVoronoi114(&lvoronoi, out, r.x, r.z, r.sx, r.sz);
if (err) return err;
}
else
{ // in 1.15 voronoi noise varies vertically in the End
int *src = out + (int64_t)r.sx*r.sy*r.sz;
memmove(src, out, sizeof(int)*s.sx*s.sz);
for (iy = 0; iy < r.sy; iy++)
{
mapVoronoiPlane(
sha, out+r.sx*r.sz*iy, src,
r.x,r.z,r.sx,r.sz, r.y+iy,
s.x,s.z,s.sx,s.sz);
}
return 0; // 3D expansion is done => return
}
}
else if (r.scale == 4)
{
err = mapEnd(en, out, r.x, r.z, r.sx, r.sz);
if (err) return err;
}
else if (r.scale == 16)
{
err = mapEndBiome(en, out, r.x, r.z, r.sx, r.sz);
if (err) return err;
}
else
{
float d = r.scale / 8.0;
int i, j;
for (j = 0; j < r.sz; j++)
{
for (i = 0; i < r.sx; i++)
{
int64_t hx = (int64_t)((i+r.x) * d);
int64_t hz = (int64_t)((j+r.z) * d);
uint64_t rsq = hx*hx + hz*hz;
if (rsq <= 16384L)
{
out[j*r.sx+i] = the_end;
continue;
}
else if (mc > MC_1_13 && (int)(rsq) < 0)
{
out[j*r.sx+i] = end_barrens;
continue;
}
float h = getEndHeightNoise(en, hx, hz, 4);
if (h > 40)
out[j*r.sx+i] = end_highlands;
else if (h >= 0)
out[j*r.sx+i] = end_midlands;
else if (h >= -20)
out[j*r.sx+i] = end_barrens;
else
out[j*r.sx+i] = small_end_islands;
}
}
}
// expanding 2D into 3D
for (iy = 1; iy < r.sy; iy++)
{
int64_t i, siz = (int64_t)r.sx*r.sz;
for (i = 0; i < siz; i++)
out[iy*siz + i] = out[i];
}
return 0;
}
//==============================================================================
// Overworld and Nether Biome Generation 1.18
//==============================================================================
static int init_climate_seed(
DoublePerlinNoise *dpn, PerlinNoise *oct,
uint64_t xlo, uint64_t xhi, int large, int nptype, int nmax
)
{
Xoroshiro pxr;
int n = 0;
switch (nptype)
{
case NP_SHIFT: {
static const double amp[] = {1, 1, 1, 0};
// md5 "minecraft:offset"
pxr.lo = xlo ^ 0x080518cf6af25384;
pxr.hi = xhi ^ 0x3f3dfb40a54febd5;
n += xDoublePerlinInit(dpn, &pxr, oct, amp, -3, 4, nmax);
} break;
case NP_TEMPERATURE: {
static const double amp[] = {1.5, 0, 1, 0, 0, 0};
// md5 "minecraft:temperature" or "minecraft:temperature_large"
pxr.lo = xlo ^ (large ? 0x944b0073edf549db : 0x5c7e6b29735f0d7f);
pxr.hi = xhi ^ (large ? 0x4ff44347e9d22b96 : 0xf7d86f1bbc734988);
n += xDoublePerlinInit(dpn, &pxr, oct, amp, large ? -12 : -10, 6, nmax);
} break;
case NP_HUMIDITY: {
static const double amp[] = {1, 1, 0, 0, 0, 0};
// md5 "minecraft:vegetation" or "minecraft:vegetation_large"
pxr.lo = xlo ^ (large ? 0x71b8ab943dbd5301 : 0x81bb4d22e8dc168e);
pxr.hi = xhi ^ (large ? 0xbb63ddcf39ff7a2b : 0xf1c8b4bea16303cd);
n += xDoublePerlinInit(dpn, &pxr, oct, amp, large ? -10 : -8, 6, nmax);
} break;
case NP_CONTINENTALNESS: {
static const double amp[] = {1, 1, 2, 2, 2, 1, 1, 1, 1};
// md5 "minecraft:continentalness" or "minecraft:continentalness_large"
pxr.lo = xlo ^ (large ? 0x9a3f51a113fce8dc : 0x83886c9d0ae3a662);
pxr.hi = xhi ^ (large ? 0xee2dbd157e5dcdad : 0xafa638a61b42e8ad);
n += xDoublePerlinInit(dpn, &pxr, oct, amp, large ? -11 : -9, 9, nmax);
} break;
case NP_EROSION: {
static const double amp[] = {1, 1, 0, 1, 1};
// md5 "minecraft:erosion" or "minecraft:erosion_large"
pxr.lo = xlo ^ (large ? 0x8c984b1f8702a951 : 0xd02491e6058f6fd8);
pxr.hi = xhi ^ (large ? 0xead7b1f92bae535f : 0x4792512c94c17a80);
n += xDoublePerlinInit(dpn, &pxr, oct, amp, large ? -11 : -9, 5, nmax);
} break;
case NP_WEIRDNESS: {
static const double amp[] = {1, 2, 1, 0, 0, 0};
// md5 "minecraft:ridge"
pxr.lo = xlo ^ 0xefc8ef4d36102b34;
pxr.hi = xhi ^ 0x1beeeb324a0f24ea;
n += xDoublePerlinInit(dpn, &pxr, oct, amp, -7, 6, nmax);
} break;
default:
printf("unsupported climate parameter %d\n", nptype);
exit(1);
}
return n;
}
void setBiomeSeed(BiomeNoise *bn, uint64_t seed, int large)
{
Xoroshiro pxr;
xSetSeed(&pxr, seed);
uint64_t xlo = xNextLong(&pxr);
uint64_t xhi = xNextLong(&pxr);
int n = 0, i = 0;
for (; i < NP_MAX; i++)
n += init_climate_seed(&bn->climate[i], bn->oct+n, xlo, xhi, large, i, -1);
if ((size_t)n > sizeof(bn->oct) / sizeof(*bn->oct))
{
printf("setBiomeSeed(): BiomeNoise is malformed, buffer too small\n");
exit(1);
}
bn->nptype = -1;
}
void setBetaBiomeSeed(BiomeNoiseBeta *bnb, uint64_t seed)
{
uint64_t seedScratch;
setSeed(&seedScratch, seed*9871);
octaveInitBeta(bnb->climate, &seedScratch, bnb->oct,
4, 0.025/1.5, 0.25, 0.55, 2.0);
setSeed(&seedScratch, seed*39811);
octaveInitBeta(bnb->climate+1, &seedScratch, bnb->oct+4,
4, 0.05/1.5, 1./3, 0.55, 2.0);
setSeed(&seedScratch, seed*0x84a59L);
octaveInitBeta(bnb->climate+2, &seedScratch, bnb->oct+8,
2, 0.25/1.5, 10./17, 0.55, 2.0);
bnb->nptype = -1;
}
enum { SP_CONTINENTALNESS, SP_EROSION, SP_RIDGES, SP_WEIRDNESS };
static void addSplineVal(Spline *rsp, float loc, Spline *val, float der)
{
rsp->loc[rsp->len] = loc;
rsp->val[rsp->len] = val;
rsp->der[rsp->len] = der;
rsp->len++;
//if (rsp->len > 12) {
// printf("addSplineVal(): too many spline points\n");
// exit(1);
//}
}
static Spline *createFixSpline(SplineStack *ss, float val)
{
FixSpline *sp = &ss->fstack[ss->flen++];
sp->len = 1;
sp->val = val;
return (Spline*)sp;
}
static float getOffsetValue(float weirdness, float continentalness)
{
float f0 = 1.0F - (1.0F - continentalness) * 0.5F;
float f1 = 0.5F * (1.0F - continentalness);
float f2 = (weirdness + 1.17F) * 0.46082947F;
float off = f2 * f0 - f1;
if (weirdness < -0.7F)
return off > -0.2222F ? off : -0.2222F;
else
return off > 0 ? off : 0;
}
static Spline *createSpline_38219(SplineStack *ss, float f, int bl)
{
Spline *sp = &ss->stack[ss->len++];
sp->typ = SP_RIDGES;
float i = getOffsetValue(-1.0F, f);
float k = getOffsetValue( 1.0F, f);
float l = 1.0F - (1.0F - f) * 0.5F;
float u = 0.5F * (1.0F - f);
l = u / (0.46082947F * l) - 1.17F;
if (-0.65F < l && l < 1.0F)
{
float p, q, r, s;
u = getOffsetValue(-0.65F, f);
p = getOffsetValue(-0.75F, f);
q = (p - i) * 4.0F;
r = getOffsetValue(l, f);
s = (k - r) / (1.0F - l);
addSplineVal(sp, -1.0F, createFixSpline(ss, i), q);
addSplineVal(sp, -0.75F, createFixSpline(ss, p), 0);
addSplineVal(sp, -0.65F, createFixSpline(ss, u), 0);