/
map.cpp
10408 lines (7083 loc) · 296 KB
/
map.cpp
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 "map.h"
#include "HashTable.h"
#include "monument.h"
#include "arcReport.h"
#include "CoordinateTimeTracking.h"
#include "eveMovingGrid.h"
// cell pixel dimension on client
#define CELL_D 128
#include "minorGems/util/random/JenkinsRandomSource.h"
#include "minorGems/util/random/CustomRandomSource.h"
#include "minorGems/util/stringUtils.h"
#include "minorGems/util/SimpleVector.h"
#include "minorGems/util/SettingsManager.h"
#include "minorGems/util/log/AppLog.h"
#include "minorGems/system/Time.h"
#include "minorGems/formats/encodingUtils.h"
#include "kissdb.h"
//#include "stackdb.h"
//#include "lineardb.h"
#include "lineardb3.h"
#include "minorGems/util/crc32.h"
/*
#define DB KISSDB
#define DB_open KISSDB_open
#define DB_close KISSDB_close
#define DB_get KISSDB_get
#define DB_put KISSDB_put
// no distinction between insert and replace in KISSS
#define DB_put_new KISSDB_put
#define DB_Iterator KISSDB_Iterator
#define DB_Iterator_init KISSDB_Iterator_init
#define DB_Iterator_next KISSDB_Iterator_next
#define DB_maxStack (int)( db.num_hash_tables )
// no support for shrinking
#define DB_getShrinkSize( dbP, n ) dbP->hashTableSize
#define DB_getCurrentSize( dbP ) dbP->hashTableSize
// no support for counting records
#define DB_getNumRecords( dbP ) 0
*/
/*
#define DB STACKDB
#define DB_open STACKDB_open
#define DB_close STACKDB_close
#define DB_get STACKDB_get
#define DB_put STACKDB_put
// stack DB has faster insert
#define DB_put_new STACKDB_put_new
#define DB_Iterator STACKDB_Iterator
#define DB_Iterator_init STACKDB_Iterator_init
#define DB_Iterator_next STACKDB_Iterator_next
#define DB_maxStack db.maxStackDepth
// no support for shrinking
#define DB_getShrinkSize( dbP, n ) dbP->hashTableSize
#define DB_getCurrentSize( dbP ) dbP->hashTableSize
// no support for counting records
#define DB_getNumRecords( dbP ) 0
*/
/*
#define DB LINEARDB
#define DB_open LINEARDB_open
#define DB_close LINEARDB_close
#define DB_get LINEARDB_get
#define DB_put LINEARDB_put
// no distinction between put and put_new in lineardb
#define DB_put_new LINEARDB_put
#define DB_Iterator LINEARDB_Iterator
#define DB_Iterator_init LINEARDB_Iterator_init
#define DB_Iterator_next LINEARDB_Iterator_next
#define DB_maxStack db.maxProbeDepth
#define DB_getShrinkSize LINEARDB_getShrinkSize
#define DB_getCurrentSize LINEARDB_getCurrentSize
#define DB_getNumRecords LINEARDB_getNumRecords
*/
#define DB LINEARDB3
#define DB_open LINEARDB3_open
#define DB_close LINEARDB3_close
#define DB_get LINEARDB3_get
#define DB_put LINEARDB3_put
// no distinction between put and put_new in lineardb3
#define DB_put_new LINEARDB3_put
#define DB_Iterator LINEARDB3_Iterator
#define DB_Iterator_init LINEARDB3_Iterator_init
#define DB_Iterator_next LINEARDB3_Iterator_next
#define DB_maxStack db.maxOverflowDepth
#define DB_getShrinkSize LINEARDB3_getShrinkSize
#define DB_getCurrentSize LINEARDB3_getCurrentSize
#define DB_getNumRecords LINEARDB3_getNumRecords
#include "dbCommon.h"
#include <stdarg.h>
#include <math.h>
#include <limits.h>
#include <stdint.h>
#include "../gameSource/transitionBank.h"
#include "../gameSource/objectBank.h"
#include "../gameSource/GridPos.h"
#include "../gameSource/GridPos.h"
#include "../gameSource/objectMetadata.h"
#include "../gameSource/settingsToggle.h"
timeSec_t startFrozenTime = -1;
timeSec_t frozenTime() {
if( startFrozenTime == -1 ) {
startFrozenTime = Time::timeSec();
}
return startFrozenTime;
}
timeSec_t startFastTime = -1;
timeSec_t fastTime() {
if( startFastTime == -1 ) {
startFastTime = Time::timeSec();
}
return 1000 * ( Time::timeSec() - startFastTime ) + startFastTime;
}
timeSec_t slowTime() {
if( startFastTime == -1 ) {
startFastTime = Time::timeSec();
}
return ( Time::timeSec() - startFastTime ) / 4 + startFastTime;
}
// can replace with frozenTime to freeze time
// or slowTime to slow it down
#define MAP_TIMESEC Time::timeSec()
//#define MAP_TIMESEC frozenTime()
//#define MAP_TIMESEC fastTime()
//#define MAP_TIMESEC slowTime()
extern GridPos getClosestPlayerPos( int inX, int inY );
extern int getNumPlayers();
// track recent placements to determine camp where
// we'll stick next Eve
#define NUM_RECENT_PLACEMENTS 100
typedef struct RecentPlacement {
GridPos pos;
// depth of object in tech tree
int depth;
} RecentPlacement;
static RecentPlacement recentPlacements[ NUM_RECENT_PLACEMENTS ];
// ring buffer
static int nextPlacementIndex = 0;
static int eveRadiusStart = 2;
static int eveRadius = eveRadiusStart;
GridPos eveLocation = { 0,0 };
static int eveLocationUsage = 0;
static int maxEveLocationUsage = 3;
// eves are placed along an Archimedean spiral
// we track the angle of the last Eve to compute the position on
// the spiral of the next Eve
static double eveAngle = 2 * M_PI;
static char eveStartSpiralPosSet = false;
static GridPos eveStartSpiralPos = { 0, 0 };
static int evePrimaryLocSpacingX = 0;
static int evePrimaryLocSpacingY = 0;
static int evePrimaryLocObjectID = -1;
static SimpleVector<int> eveSecondaryLocObjectIDs;
static GridPos lastEvePrimaryLocation = {0,0};
static SimpleVector<GridPos> recentlyUsedPrimaryEvePositions;
static SimpleVector<int> recentlyUsedPrimaryEvePositionPlayerIDs;
// when they were place, so they can time out
static SimpleVector<double> recentlyUsedPrimaryEvePositionTimes;
// one hour
static double recentlyUsedPrimaryEvePositionTimeout = 3600;
static int eveHomeMarkerObjectID = -1;
static char allowSecondPlaceBiomes = false;
// what human-placed stuff, together, counts as a camp
static int campRadius = 20;
static float minEveCampRespawnAge = 60.0;
static int barrierRadius = 250;
static int barrierOn = 1;
static int longTermCullEnabled = 1;
static unsigned int biomeRandSeedA = 727;
static unsigned int biomeRandSeedB = 941;
static SimpleVector<int> barrierItemList;
static FILE *mapChangeLogFile = NULL;
static double mapChangeLogTimeStart = -1;
extern int apocalypsePossible;
extern char apocalypseTriggered;
extern GridPos apocalypseLocation;
// what object is placed on edge of map
static int edgeObjectID = 0;
static int currentResponsiblePlayer = -1;
void setResponsiblePlayer( int inPlayerID ) {
currentResponsiblePlayer = inPlayerID;
}
static double gapIntScale = 1000000.0;
// object ids that occur naturally on map at random, per biome
static int numBiomes;
static int *biomes;
static float *biomeWeights;
static float *biomeCumuWeights;
static float biomeTotalWeight;
static int regularBiomeLimit;
static int numSpecialBiomes;
static int *specialBiomes;
static float *specialBiomeCumuWeights;
static float specialBiomeTotalWeight;
static int specialBiomeBandMode;
static int specialBiomeBandThickness;
static SimpleVector<int> specialBiomeBandOrder;
// contains indices into biomes array instead of biome numbers
static SimpleVector<int> specialBiomeBandIndexOrder;
static SimpleVector<int> specialBiomeBandYCenter;
static int minActivePlayersForBirthlands;
static int polylingualNoHomeland;
// the biome index to use in place of special biomes outside of the north-most
// or south-most band
static int specialBiomeBandDefaultIndex;
// one vector per biome
static SimpleVector<int> *naturalMapIDs;
static SimpleVector<float> *naturalMapChances;
typedef struct MapGridPlacement {
int id;
int spacingX, spacingY;
int phaseX, phaseY;
int wiggleScaleX, wiggleScaleY;
SimpleVector<int> permittedBiomes;
} MapGridPlacement;
static SimpleVector<MapGridPlacement> gridPlacements;
static SimpleVector<int> allNaturalMapIDs;
static float *totalChanceWeight;
static int getBiomeIndex( int inBiome ) {
for( int i=0; i<numBiomes; i++ ) {
if( biomes[i] == inBiome ) {
return i;
}
}
return -1;
}
// tracking when a given map cell was last seen
static DB lookTimeDB;
static char lookTimeDBOpen = false;
static DB db;
static char dbOpen = false;
static DB timeDB;
static char timeDBOpen = false;
static DB biomeDB;
static char biomeDBOpen = false;
static DB floorDB;
static char floorDBOpen = false;
static DB floorTimeDB;
static char floorTimeDBOpen = false;
static DB graveDB;
static char graveDBOpen = false;
// per-player memory of where they should spawn as eve
static DB eveDB;
static char eveDBOpen = false;
static DB metaDB;
static char metaDBOpen = false;
static int randSeed = 124567;
//static JenkinsRandomSource randSource( randSeed );
static CustomRandomSource randSource( randSeed );
#define DECAY_SLOT 1
#define NUM_CONT_SLOT 2
#define FIRST_CONT_SLOT 3
#define NO_DECAY_SLOT -1
// decay slots for contained items start after container slots
// 15 minutes
static int maxSecondsForActiveDecayTracking = 900;
// 15 seconds (before no-look regions are purged from live tracking)
static int maxSecondsNoLookDecayTracking = 15;
// live players look at their surrounding map region every 5 seconds
// we count a region as stale after no one looks at it for 10 seconds
// (we actually purge the live tracking of that region after 15 seconds).
// This gives us some wiggle room with the timing, so we always make
// sure to re-look at a region (when walking back into it) that is >10
// seconds old, because it may (or may not) have fallen out of our live
// tracking (if our re-look time was 15 seconds to match the time stuff actually
// is dropped from live tracking, we might miss some stuff, depending
// on how the check calls are interleaved time-wise).
static int noLookCountAsStaleSeconds = 10;
typedef struct LiveDecayRecord {
int x, y;
// 0 means main object decay
// 1 - NUM_CONT_SLOT means contained object decay
int slot;
timeSec_t etaTimeSeconds;
// 0 means main object
// >0 indexs sub containers of object
int subCont;
// the transition that will apply when this decay happens
// this allows us to avoid marking certain types of move decays
// as stale when not looked at in a while (all other types of decays
// go stale)
// Can be NULL if we don't care about the transition
// associated with this decay (for contained item decay, for example)
TransRecord *applicableTrans;
} LiveDecayRecord;
#include "minorGems/util/MinPriorityQueue.h"
static MinPriorityQueue<LiveDecayRecord> liveDecayQueue;
// for quick lookup of existing records in liveDecayQueue
// store the eta time here
// before storing a new record in the queue, we can check this hash
// table to see whether it already exists
static HashTable<timeSec_t> liveDecayRecordPresentHashTable( 1024 );
// times in seconds that a tracked live decay map cell or slot
// was last looked at
static HashTable<timeSec_t> liveDecayRecordLastLookTimeHashTable( 1024 );
typedef struct ContRecord {
int maxSlots;
int maxSubSlots;
} ContRecord;
static ContRecord defaultContRecord = { 0, 0 };
// track max tracked contained for each x,y
// this allows us to update last look times without getting contained count
// from map
// indexed as x, y, 0, 0
static HashTable<ContRecord>
liveDecayRecordLastLookTimeMaxContainedHashTable( 1024, defaultContRecord );
static CoordinateTimeTracking lookTimeTracking;
// track currently in-process movements so that we can be queried
// about whether arrival has happened or not
typedef struct MovementRecord {
int x, y;
int sourceX, sourceY;
int id;
char deadly;
double etaTime;
double totalTime;
} MovementRecord;
// clock time in fractional seconds of destination ETA
// indexed as x, y, 0
static HashTable<double> liveMovementEtaTimes( 1024, 0 );
static MinPriorityQueue<MovementRecord> liveMovements;
// track all map changes that happened since the last
// call to stepMap
static SimpleVector<ChangePosition> mapChangePosSinceLastStep;
static char anyBiomesInDB = false;
static int maxBiomeXLoc = -2000000000;
static int maxBiomeYLoc = -2000000000;
static int minBiomeXLoc = 2000000000;
static int minBiomeYLoc = 2000000000;
// if true, rest of natural map is blank
static char useTestMap = false;
// read from testMap.txt
// unless testMapStale.txt is present
// each line contains data in this order:
// x y biome floor id_and_contained
// id and contained are in CONTAINER OBJECT FORMAT described in protocol.txt
// biome = -1 means use naturally-occurring biome
typedef struct TestMapRecord {
int x, y;
int biome;
int floor;
int id;
SimpleVector<int> contained;
SimpleVector< SimpleVector<int> > subContained;
} TestMapRecord;
typedef struct Homeland {
int x, y;
int radius;
int lineageEveID;
double lastBabyBirthTime;
char expired;
char changed;
// did the creation of this homeland tapout-trigger
// a +primaryHomeland object?
char primary;
// should Eve placement ignore this homeland?
char ignoredForEve;
// was this homeland the first for this lineageEveID?
// this can be true even if not primary, if eve resettles an old village
// and does not tapout trigger anything
char firstHomelandForFamily;
} Homeland;
static SimpleVector<Homeland> homelands;
static void expireHomeland( Homeland *inH ) {
inH->expired = true;
inH->changed = true;
if( ! inH->primary ) {
// apply expiration transition to whatever is at center of
// non-primary homeland
// (object operating on itself defines this)
int centerID = getMapObject( inH->x, inH->y );
if( centerID > 0 ) {
TransRecord *expireTrans = getTrans( centerID, centerID );
if( expireTrans != NULL ) {
setMapObject( inH->x, inH->y, expireTrans->newTarget );
}
}
}
}
// NULL if not found
static Homeland *getHomeland( int inX, int inY,
char includeExpired = false ) {
double t = Time::getCurrentTime();
int staleTime =
SettingsManager::getIntSetting( "homelandStaleSeconds", 3600 );
double tooOldTime = t - staleTime;
for( int i=0; i<homelands.size(); i++ ) {
Homeland *h = homelands.getElement( i );
// watch for stale
if( ! h->expired && h->lastBabyBirthTime < tooOldTime ) {
expireHomeland( h );
}
if( ! includeExpired && h->expired ) {
continue;
}
if( inX < h->x + h->radius &&
inX > h->x - h->radius &&
inY < h->y + h->radius &&
inY > h->y - h->radius ) {
return h;
}
}
return NULL;
}
static char hasPrimaryHomeland( int inLineageEveID ) {
for( int i=0; i<homelands.size(); i++ ) {
Homeland *h = homelands.getElement( i );
if( h->primary && h->lineageEveID == inLineageEveID ) {
return true;
}
}
return false;
}
#include "../commonSource/fractalNoise.h"
timeSec_t dbLookTimeGet( int inX, int inY );
void dbLookTimePut( int inX, int inY, timeSec_t inTime );
// returns -1 if not found
static int biomeDBGet( int inX, int inY,
int *outSecondPlaceBiome = NULL,
double *outSecondPlaceGap = NULL ) {
unsigned char key[8];
unsigned char value[12];
// look for changes to default in database
intPairToKey( inX, inY, key );
int result = DB_get( &biomeDB, key, value );
if( result == 0 ) {
// found
int biome = valueToInt( &( value[0] ) );
if( outSecondPlaceBiome != NULL ) {
*outSecondPlaceBiome = valueToInt( &( value[4] ) );
}
if( outSecondPlaceGap != NULL ) {
*outSecondPlaceGap = valueToInt( &( value[8] ) ) / gapIntScale;
}
return biome;
}
else {
return -1;
}
}
static void biomeDBPut( int inX, int inY, int inValue, int inSecondPlace,
double inSecondPlaceGap ) {
unsigned char key[8];
unsigned char value[12];
intPairToKey( inX, inY, key );
intToValue( inValue, &( value[0] ) );
intToValue( inSecondPlace, &( value[4] ) );
intToValue( lrint( inSecondPlaceGap * gapIntScale ),
&( value[8] ) );
anyBiomesInDB = true;
if( inX > maxBiomeXLoc ) {
maxBiomeXLoc = inX;
}
if( inX < minBiomeXLoc ) {
minBiomeXLoc = inX;
}
if( inY > maxBiomeYLoc ) {
maxBiomeYLoc = inY;
}
if( inY < minBiomeYLoc ) {
minBiomeYLoc = inY;
}
DB_put( &biomeDB, key, value );
}
// returns -1 on failure, 1 on success
static int eveDBGet( const char *inEmail, int *outX, int *outY,
int *outRadius ) {
unsigned char key[50];
unsigned char value[12];
emailToKey( inEmail, key );
int result = DB_get( &eveDB, key, value );
if( result == 0 ) {
// found
*outX = valueToInt( &( value[0] ) );
*outY = valueToInt( &( value[4] ) );
*outRadius = valueToInt( &( value[8] ) );
return 1;
}
else {
return -1;
}
}
static void eveDBPut( const char *inEmail, int inX, int inY, int inRadius ) {
unsigned char key[50];
unsigned char value[12];
emailToKey( inEmail, key );
intToValue( inX, &( value[0] ) );
intToValue( inY, &( value[4] ) );
intToValue( inRadius, &( value[8] ) );
DB_put( &eveDB, key, value );
}
static void dbFloorPut( int inX, int inY, int inValue );
// inKnee in 0..inf, smaller values make harder knees
// intput in 0..1
// output in 0..1
// from Simplest AI trick in the book:
// Normalized Tunable SIgmoid Function
// Dino Dini, GDC 2013
double sigmoid( double inInput, double inKnee ) {
// in -1,-1
double shiftedInput = inInput * 2 - 1;
double sign = 1;
if( shiftedInput < 0 ) {
sign = -1;
}
double k = -1 - inKnee;
double absInput = fabs( shiftedInput );
// out in -1..1
double out = sign * absInput * k / ( 1 + k - absInput );
return ( out + 1 ) * 0.5;
}
// optimization:
// cache biomeIndex results in RAM
// 3.1 MB of RAM for this.
#define BIOME_CACHE_SIZE 131072
typedef struct BiomeCacheRecord {
int x, y;
int biome, secondPlace;
double secondPlaceGap;
} BiomeCacheRecord;
static BiomeCacheRecord biomeCache[ BIOME_CACHE_SIZE ];
#define CACHE_PRIME_A 776509273
#define CACHE_PRIME_B 904124281
#define CACHE_PRIME_C 528383237
#define CACHE_PRIME_D 148497157
static int computeXYCacheHash( int inKeyA, int inKeyB ) {
int hashKey = ( inKeyA * CACHE_PRIME_A +
inKeyB * CACHE_PRIME_B ) % BIOME_CACHE_SIZE;
if( hashKey < 0 ) {
hashKey += BIOME_CACHE_SIZE;
}
return hashKey;
}
static void initBiomeCache() {
BiomeCacheRecord blankRecord = { 0, 0, -2, 0, 0 };
for( int i=0; i<BIOME_CACHE_SIZE; i++ ) {
biomeCache[i] = blankRecord;
}
}
// returns -2 on miss
static int biomeGetCached( int inX, int inY,
int *outSecondPlaceIndex,
double *outSecondPlaceGap ) {
BiomeCacheRecord r =
biomeCache[ computeXYCacheHash( inX, inY ) ];
if( r.x == inX && r.y == inY ) {
*outSecondPlaceIndex = r.secondPlace;
*outSecondPlaceGap = r.secondPlaceGap;
return r.biome;
}
else {
return -2;
}
}
static void biomePutCached( int inX, int inY, int inBiome, int inSecondPlace,
double inSecondPlaceGap ) {
BiomeCacheRecord r = { inX, inY, inBiome, inSecondPlace, inSecondPlaceGap };
biomeCache[ computeXYCacheHash( inX, inY ) ] = r;
}
static int getSpecialBiomeIndexForYBand( int inY, char *outOfBand = NULL ) {
if( outOfBand != NULL ) {
*outOfBand = false;
}
// new method, use y centers and thickness
int radius = specialBiomeBandThickness / 2;
for( int i=0; i<specialBiomeBandYCenter.size(); i++ ) {
int yCenter = specialBiomeBandYCenter.getElementDirect( i );
if( abs( inY - yCenter ) <= radius ) {
return specialBiomeBandIndexOrder.getElementDirect( i );
}
}
// else not in radius of any band
if( outOfBand != NULL ) {
*outOfBand = true;
}
return specialBiomeBandDefaultIndex;
}
// new code, topographic rings
static int computeMapBiomeIndex( int inX, int inY,
int *outSecondPlaceIndex = NULL,
double *outSecondPlaceGap = NULL ) {
int secondPlace = -1;
double secondPlaceGap = 0;
int pickedBiome = biomeGetCached( inX, inY, &secondPlace, &secondPlaceGap );
if( pickedBiome != -2 ) {
// hit cached
if( outSecondPlaceIndex != NULL ) {
*outSecondPlaceIndex = secondPlace;
}
if( outSecondPlaceGap != NULL ) {
*outSecondPlaceGap = secondPlaceGap;
}
return pickedBiome;
}
// else cache miss
pickedBiome = -1;
// try topographical altitude mapping
setXYRandomSeed( biomeRandSeedA, biomeRandSeedB );
double randVal =
( getXYFractal( inX, inY,
0.55,
0.83332 + 0.08333 * numBiomes ) );
// push into range 0..1, based on sampled min/max values
randVal -= 0.099668;
randVal *= 1.268963;
// flatten middle
//randVal = ( pow( 2*(randVal - 0.5 ), 3 ) + 1 ) / 2;
// push into range 0..1 with manually tweaked values
// these values make it pretty even in terms of distribution:
//randVal -= 0.319;
//randVal *= 3;
// these values are more intuitve to make a map that looks good
//randVal -= 0.23;
//randVal *= 1.9;
// apply gamma correction
//randVal = pow( randVal, 1.5 );
/*
randVal += 0.4* sin( inX / 40.0 );
randVal += 0.4 *sin( inY / 40.0 );
randVal += 0.8;
randVal /= 2.6;
*/