forked from Cubitect/cubiomes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
finders.c
3277 lines (2850 loc) · 95.3 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 <math.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(_WIN32)
#include <direct.h>
#define IS_DIR_SEP(C) ((C) == '/' || (C) == '\\')
#define stat _stat
#define mkdir(P,X) _mkdir(P)
#define S_IFDIR _S_IFDIR
#else
#define IS_DIR_SEP(C) ((C) == '/')
#endif
//==============================================================================
// Saving & Loading Seeds
//==============================================================================
uint64_t *loadSavedSeeds(const char *fnam, uint64_t *scnt)
{
FILE *fp = fopen(fnam, "r");
uint64_t seed, i;
uint64_t *baseSeeds;
if (fp == NULL)
return NULL;
*scnt = 0;
while (!feof(fp))
{
if (fscanf(fp, "%" PRId64, (int64_t*)&seed) == 1) (*scnt)++;
else while (!feof(fp) && fgetc(fp) != '\n');
}
if (*scnt == 0)
return NULL;
baseSeeds = (uint64_t*) calloc(*scnt, sizeof(*baseSeeds));
rewind(fp);
for (i = 0; i < *scnt && !feof(fp);)
{
if (fscanf(fp, "%" PRId64, (int64_t*)&baseSeeds[i]) == 1) i++;
else while (!feof(fp) && fgetc(fp) != '\n');
}
fclose(fp);
return baseSeeds;
}
//==============================================================================
// 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)
{
uint64_t s;
uint64_t a, b;
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 = 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 = RUINED_PORTAL_N_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 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 = 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:
*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_16) {
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;
} else {
getRegPos(pos, &seed, regX, regZ, sconf);
return nextInt(&seed, 5) < 2;
}
case Bastion:
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) );
setSeed(&seed, getPopulationSeed(mc, seed, pos->x, pos->z) + sconf.salt);
if (mc <= MC_1_16) {
if (nextInt(&seed, 700) != 0)
return 0;
} else {
if (nextFloat(&seed) >= 1.0/700)
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 U(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 U(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;
}
//==============================================================================
// Multi-Structure Checks
//==============================================================================
// TODO: accurate seed testers for two or three structures in range
static int blocksInRange(Pos *p, int n, int x, int z, int ax, int az, double rsq)
{
int i, cnt;
cnt = 0;
for (i = 0; i < n; i++)
{
double dx = p[i].x - x;
double dz = p[i].z - z;
int px, pz;
for (px = 0; px < ax; px++)
{
for (pz = 0; pz < az; pz++)
{
double ddx = px + dx;
double ddz = pz + dz;
cnt += (ddx*ddx + ddz*ddz <= rsq);
}
}
}
return cnt;
}
STRUCT(afk_meta_t)
{
Pos *p;
int n;
int *buf;
int x0, z0, w, h, ax, az;
double rsq;
int best;
int sumn;
int64_t sumx, sumz;
};
static void checkAfkDist(afk_meta_t *d, int x, int z)
{
if (x < 0 || z < 0 || x >= d->w || z >= d->h)
return;
if (d->buf[z*d->w+x])
return;
int q = blocksInRange(d->p, d->n, x+d->x0, z+d->z0, d->ax, d->az, d->rsq);
d->buf[z*d->w+x] = q;
if (q >= d->best)
{
if (q > d->best)
{
d->best = q;
d->sumn = 1;
d->sumx = d->x0+x;
d->sumz = d->z0+z;
}
else
{
d->sumn += 1;
d->sumx += d->x0+x;
d->sumz += d->z0+z;
}
checkAfkDist(d, x, z-1);
checkAfkDist(d, x, z+1);
checkAfkDist(d, x-1, z);
checkAfkDist(d, x+1, z);
checkAfkDist(d, x-1, z-1);
checkAfkDist(d, x-1, z+1);
checkAfkDist(d, x+1, z-1);
checkAfkDist(d, x+1, z+1);
}
}
Pos getOptimalAfk(Pos p[4], int ax, int ay, int az, int *spcnt)
{
int64_t minX = INT_MAX, minZ = INT_MAX, maxX = INT_MIN, maxZ = INT_MIN;
int64_t w, h, i;
for (i = 0; i < 4; i++)
{
if (p[i].x < minX) minX = p[i].x;
if (p[i].z < minZ) minZ = p[i].z;
if (p[i].x > maxX) maxX = p[i].x;
if (p[i].z > maxZ) maxZ = p[i].z;
}
minX += ax/2;
minZ += az/2;
maxX += ax/2;
maxZ += az/2;
double rsq = 128.0*128.0 - ay*ay/4.0;
w = maxX - minX;
h = maxZ - minZ;
Pos afk = {p[0].x + ax / 2, p[0].z + az / 2};
int cnt = ax*az;
afk_meta_t d;
d.p = p;
d.n = 4;
d.buf = (int*) calloc(w*h, sizeof(int));
d.x0 = minX;
d.z0 = minZ;
d.w = w;
d.h = h;
d.ax = ax;
d.az = az;
d.rsq = rsq;
int v[6];
Pos dsp[6] = {
{(p[0].x + p[2].x) / 2, (p[0].z + p[2].z) / 2},
{(p[1].x + p[3].x) / 2, (p[1].z + p[3].z) / 2},
{(p[0].x + p[1].x) / 2, (p[0].z + p[1].z) / 2},
{(p[2].x + p[3].x) / 2, (p[2].z + p[3].z) / 2},
{(p[0].x + p[3].x) / 2, (p[0].z + p[3].z) / 2},
{(p[1].x + p[2].x) / 2, (p[1].z + p[2].z) / 2},
};
for (i = 0; i < 6; i++)
v[i] = blocksInRange(p, 4, dsp[i].x, dsp[i].z, ax, az, rsq);
for (i = 0; i < 6; i++)
{
// pick out the highest
int j, jmax = 0, vmax = 0;
for (j = 0; j < 6; j++)
{
if (v[j] > vmax)
{
jmax = j;
vmax = v[j];
}
}
if (vmax <= ax*az) // highest is less or equal to a single structure
break;
d.best = vmax;
d.sumn = 0;
d.sumx = 0;
d.sumz = 0;
checkAfkDist(&d, dsp[jmax].x - d.x0, dsp[jmax].z - d.z0);
if (d.best > cnt)
{
cnt = d.best;
afk.x = (int) round(d.sumx / (double)d.sumn);
afk.z = (int) round(d.sumz / (double)d.sumn);
if (cnt >= 3*ax*az)
break;
}
v[jmax] = 0;
}
if (spcnt)
*spcnt = cnt;
free(d.buf);
return afk;
}
#define MAX_PATHLEN 4096
STRUCT(linked_seeds_t)
{
uint64_t seeds[100];
size_t len;
linked_seeds_t *next;
};
STRUCT(threadinfo_t)
{
// seed range
uint64_t start, end;
const uint64_t *lowBits;
int lowBitCnt;
int lowBitN;
// testing function
int (*check)(uint64_t, void*);
void *data;
// output
char path[MAX_PATHLEN];
FILE *fp;
linked_seeds_t ls;
};
static int mkdirp(char *path)
{
int err = 0, len = strlen(path);
char *p = path;
#if defined(_WIN32)
if (p[1] == ':') p += 2;
#endif
while (IS_DIR_SEP(*p)) p++;
while (!err && p < path+len)
{
char *q = p;
while (*q && !IS_DIR_SEP(*q))
q++;
if (p != path) p[-1] = '/';
*q = 0;
struct stat st;
if (stat(path, &st) == -1)
err = mkdir(path, 0773);
else if (!S_ISDIR(st.st_mode))
err = 1;
p = q+1;
}
return err;
}
#ifdef USE_PTHREAD
static void *searchAll48Thread(void *data)
#else
static DWORD WINAPI searchAll48Thread(LPVOID data)
#endif
{
// TODO TEST:
// lower bits with various ranges
threadinfo_t *info = (threadinfo_t*)data;
uint64_t seed = info->start;
uint64_t end = info->end;
linked_seeds_t *lp = &info->ls;
lp->len = 0;
lp->next = NULL;
if (info->lowBits)
{
uint64_t hstep = 1ULL << info->lowBitN;
uint64_t hmask = ~(hstep - 1);
uint64_t mid;
int idx;
mid = info->start & hmask;
for (idx = 0; (seed = mid | info->lowBits[idx]) < info->start; idx++);
while (seed <= end)
{
if U(info->check(seed, info->data))
{
if (info->fp)
{
fprintf(info->fp, "%" PRId64"\n", (int64_t)seed);
fflush(info->fp);
}
else
{
lp->seeds[lp->len] = seed;
lp->len++;
if (lp->len >= sizeof(lp->seeds)/sizeof(uint64_t))
{
linked_seeds_t *n =
(linked_seeds_t*) malloc(sizeof(linked_seeds_t));
if (n == NULL)
exit(1);
lp->next = n;
lp = n;
lp->len = 0;
lp->next = NULL;
}
}
}
idx++;
if (idx >= info->lowBitCnt)
{
idx = 0;
mid += hstep;
}
seed = mid | info->lowBits[idx];
}
}
else
{
while (seed <= end)
{
if U(info->check(seed, info->data))
{
if (info->fp)
{
fprintf(info->fp, "%" PRId64"\n", (int64_t)seed);
fflush(info->fp);
}
else
{
lp->seeds[lp->len] = seed;
lp->len++;
if (lp->len >= sizeof(lp->seeds)/sizeof(uint64_t))
{
linked_seeds_t *n =
(linked_seeds_t*) malloc(sizeof(linked_seeds_t));
if (n == NULL)
exit(1);
lp->next = n;
lp = n;
lp->len = 0;
lp->next = NULL;
}
}
}
seed++;
}
}
#ifdef USE_PTHREAD
pthread_exit(NULL);
#endif
return 0;
}
int searchAll48(
uint64_t ** seedbuf,
uint64_t * buflen,
const char * path,
int threads,
const uint64_t * lowBits,
int lowBitCnt,
int lowBitN,
int (*check)(uint64_t s48, void *data),
void * data
)
{
threadinfo_t *info = (threadinfo_t*) malloc(threads* sizeof(*info));
thread_id_t *tids = (thread_id_t*) malloc(threads* sizeof(*tids));
int i, t;
int err = 0;
if (path)
{
size_t pathlen = strlen(path);
char dpath[MAX_PATHLEN];
// split path into directory and file and create missing directories
if (pathlen + 8 >= sizeof(dpath))
goto L_err;
strcpy(dpath, path);
for (i = pathlen-1; i >= 0; i--)
{
if (IS_DIR_SEP(dpath[i]))
{
dpath[i] = 0;
if (mkdirp(dpath))
goto L_err;
break;
}
}
}
else if (seedbuf == NULL || buflen == NULL)
{
// no file and no buffer return: no output possible
goto L_err;
}
// prepare the thread info and load progress if present
for (t = 0; t < threads; t++)
{
info[t].start = (t * (MASK48+1) / threads);
info[t].end = ((t+1) * (MASK48+1) / threads - 1);
info[t].lowBits = lowBits;
info[t].lowBitCnt = lowBitCnt;
info[t].lowBitN = lowBitN;
info[t].check = check;
info[t].data = data;
if (path)
{
// progress file of this thread
snprintf(info[t].path, sizeof(info[t].path), "%s.part%d", path, t);
FILE *fp = fopen(info[t].path, "a+");
if (fp == NULL)
goto L_err;
int c, nnl = 0;
char buf[32];
// find the last newline
for (i = 1; i < 32; i++)
{
if (fseek(fp, -i, SEEK_END)) break;
c = fgetc(fp);
if (c <= 0 || (nnl && c == '\n')) break;
nnl |= (c != '\n');
}
if (i < 32 && !fseek(fp, 1-i, SEEK_END) && fread(buf, i-1, 1, fp) > 0)
{
// read the last entry, and replace the start seed accordingly
int64_t lentry;
if (sscanf(buf, "%" PRId64, &lentry) == 1)
{
info[t].start = lentry;
printf("Continuing thread %d at seed %" PRId64 "\n",
t, lentry);
}
}
fseek(fp, 0, SEEK_END);
info[t].fp = fp;
}
else
{
info[t].path[0] = 0;
info[t].fp = NULL;
}
}
// run the threads
#ifdef USE_PTHREAD
for (t = 0; t < threads; t++)
{
pthread_create(&tids[t], NULL, searchAll48Thread, (void*)&info[t]);
}
for (t = 0; t < threads; t++)
{
pthread_join(tids[t], NULL);
}
#else
for (t = 0; t < threads; t++)
{
tids[t] = CreateThread(NULL, 0, searchAll48Thread,
(LPVOID)&info[t], 0, NULL);
}
WaitForMultipleObjects(threads, tids, TRUE, INFINITE);
#endif
if (path)
{
// merge partial files
FILE *fp = fopen(path, "w");
if (fp == NULL)
goto L_err;
for (t = 0; t < threads; t++)
{
rewind(info[t].fp);
char buffer[4097];
size_t n;
while ((n = fread(buffer, sizeof(char), 4096, info[t].fp)))
{
if (!fwrite(buffer, sizeof(char), n, fp))
{
fclose(fp);
goto L_err;
}
}
fclose(info[t].fp);
remove(info[t].path);
}
fclose(fp);
if (seedbuf && buflen)
{
*seedbuf = loadSavedSeeds(path, buflen);
}
}
else
{
// merge linked seed buffers
*buflen = 0;
for (t = 0; t < threads; t++)
{
linked_seeds_t *lp = &info[t].ls;
do
{
*buflen += lp->len;
lp = lp->next;
}
while (lp);
}
*seedbuf = (uint64_t*) malloc((*buflen) * sizeof(uint64_t));
if (*seedbuf == NULL)
exit(1);
i = 0;
for (t = 0; t < threads; t++)
{
linked_seeds_t *lp = &info[t].ls;
do
{
memcpy(*seedbuf + i, lp->seeds, lp->len * sizeof(uint64_t));
i += lp->len;
linked_seeds_t *tmp = lp;
lp = lp->next;
if (tmp != &info[t].ls)
free(tmp);
}
while (lp);
}
}
if (0)
L_err:
err = 1;
free(tids);
free(info);
return err;
}
static inline
int scanForQuadBits(const StructureConfig sconf, int radius, uint64_t s48,
uint64_t lbit, int lbitn, uint64_t invB, int64_t x, int64_t z,
int64_t w, int64_t h, Pos *qplist, int n)
{
const uint64_t m = (1ULL << lbitn);
const uint64_t A = 341873128712ULL;
// for lbitn=20: invB = 132477LL;
if (n < 1)
return 0;
lbit &= m-1;
int64_t i, j;
int cnt = 0;
for (i = x; i <= x+w; i++)
{
uint64_t sx = s48 + A * i;
j = (z & ~(m-1)) | ((lbit - sx) * invB & (m-1));
if (j < z)
j += m;
for (; j <= z+h; j += m)
{
uint64_t sp = moveStructure(s48, -i, -j);
if ((sp & (m-1)) != lbit)
continue;
if (isQuadBase(sconf, sp, radius))
{
qplist[cnt].x = i;
qplist[cnt].z = j;
cnt++;
if (cnt >= n)
return cnt;
}
}
}
return cnt;
}
int scanForQuads(
const StructureConfig sconf, int radius, uint64_t s48,
const uint64_t *lowBits, int lowBitCnt, int lowBitN, uint64_t salt,
int x, int z, int w, int h, Pos *qplist, int n)
{
int i, cnt = 0;
uint64_t invB;
if (lowBitN == 20)
invB = 132477ULL;
else if (lowBitN == 48)
invB = 211541297333629ULL;
else
invB = mulInv(132897987541ULL, (1ULL << lowBitN));
for (i = 0; i < lowBitCnt; i++)
{
cnt += scanForQuadBits(sconf, radius, s48, lowBits[i]-salt, lowBitN, invB,
x, z, w, h, qplist+cnt, n-cnt);
if (cnt >= n)
break;
}
return cnt;
}
//==============================================================================
// Checking Biomes & Biome Helper Functions
//==============================================================================
int getBiomeAtPos(const LayerStack *g, const Pos pos)
{
int *ids = allocCache(g->entry_1, 1, 1);
genArea(g->entry_1, ids, pos.x, pos.z, 1, 1);
int biomeID = ids[0];
free(ids);
return biomeID;
}
Pos findBiomePosition(
const int mcversion,
const Layer *l,
int *cache,
const int centerX,
const int centerZ,
const int range,
const char *isValid,
uint64_t *seed,
int *passes
)
{
int x1 = (centerX-range) >> 2;
int z1 = (centerZ-range) >> 2;
int x2 = (centerX+range) >> 2;
int z2 = (centerZ+range) >> 2;
int width = x2 - x1 + 1;
int height = z2 - z1 + 1;
int *ids;
int i, j, found;
Pos out;
if (l->scale != 4)
{
printf("WARN findBiomePosition: require scale = 4, but have %d.\n",
l->scale);
}
ids = cache ? cache : allocCache(l, width, height);
genArea(l, ids, x1, z1, width, height);
out.x = centerX;
out.z = centerZ;
found = 0;
if (mcversion >= MC_1_13)
{
for (i = 0, j = 2; i < width*height; i++)
{
if (!isValid[ids[i]]) continue;
if ((found == 0 || nextInt(seed, 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 (isValid[ids[i]] && (found == 0 || nextInt(seed, found + 1) == 0))
{
out.x = (x1 + i%width) << 2;