-
Notifications
You must be signed in to change notification settings - Fork 28
/
OTMapGen.js
986 lines (832 loc) · 28.9 KB
/
OTMapGen.js
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
// OTBM2JSON lib
const otbm2json = require("otbm2json/otbm2json")
// Noise funcion
const noise = require("./lib/noise").noise
// Supplementary functions
const useBorders = require("./lib/border")
const clutter = require("./lib/clutter")
// JSON constants
const ITEMS = require("./json/items")
const mountains = require("./json/items/mountains")
const VERSIONS = require("./json/versions")
const DEFAULT_CONFIG = {
SEED: "",
WIDTH: 256,
HEIGHT: 256,
VERSION: "10.98",
TERRAIN_ONLY: false,
GENERATION: {
NOISE_INCREMENT: 1,
ISLAND_DISTANCE_DECREMENT: 0.92,
ISLAND_DISTANCE_EXPONENT: 0.25,
CAVE_DEPTH: 20,
CAVE_ROUGHNESS: 0.45,
CAVE_CHANCE: 0.09,
SAND_BIOME: true,
EUCLIDEAN: false,
SMOOTH_COASTLINE: true,
ADD_CAVES: true,
WATER_LEVEL: 2,
EXPONENT: 1.4,
LINEAR: 6,
MOUNTAIN_TYPE: "MOUNTAIN",
FREQUENCIES: [
{ f: 1, weight: 0.3 },
{ f: 2, weight: 0.2 },
{ f: 4, weight: 0.2 },
{ f: 8, weight: 0.125 },
{ f: 16, weight: 0.1 },
{ f: 32, weight: 0.05 },
{ f: 64, weight: 0.0025 },
],
},
}
const OTMapGenerator = function () {
/*
* Class OTMapGenerator
* Container for the OTMapGenerator class
*/
// Confirm the OTBM2JSON major version
if (otbm2json.__VERSION__.split(".").shift() !== "1") {
return console.log("Incompatible version of otbm2json; please update.")
}
// Constant size of RME tile area (255x255)
this.TILE_AREA_SIZE = 0xff
// Default configuration to be overwritten
this.CONFIGURATION = DEFAULT_CONFIG
}
OTMapGenerator.prototype.createRGBALayer = function (layer) {
/*
* Function OTMapGenerator::createRGBALayer
* Creates a RGBA layer
*/
function getMinimapColor(id, configuration) {
/*
* Function OTMapGenerator::createRGBALayer::getMinimapColor
* Maps tile identifier to minimap color
*/
// Color constants (Tibia)
const WATER_COLOR = 0x0148c2
const GRASS_COLOR = 0x00ff00
const SAND_COLOR = 0xffcc99
const MOUNTAIN_COLOR = 0x666666
const GRAVEL_COLOR = 0x999999
const SNOW_COLOR = 0xffffff
// Map tile to minimap color
// default to black
switch (id) {
case ITEMS.WATER_TILE_ID:
return WATER_COLOR
case ITEMS.GRASS_TILE_ID:
return GRASS_COLOR
case ITEMS.SAND_TILE_ID:
return SAND_COLOR
case mountains[configuration.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]:
return MOUNTAIN_COLOR
case ITEMS.GRAVEL_TILE_ID:
case ITEMS.STONE_TILE_ID:
return GRAVEL_COLOR
case ITEMS.SNOW_TILE_ID:
return SNOW_COLOR
default:
return 0x000000
}
}
function shouldOutline(layer, j, width) {
/*
* Function OTMapGenerator::createRGBALayer::shouldOutline
* Determines whether the pixel is an outline by checking
* is any of the 8 neighbouring pixels is filled
*/
return (
layer[j - 1] ||
layer[j + 1] ||
layer[j + width] ||
layer[j - width] ||
layer[j - 1 - width] ||
layer[j + 1 - width] ||
layer[j + 1 + width] ||
layer[j - 1 + width]
)
}
const OUTLINE_COLOR = 0x80
// Image will occupy four bytes per tile (pixel; RGBA)
var byteArray = new Uint8ClampedArray(
4 * Number(this.CONFIGURATION.WIDTH) * Number(this.CONFIGURATION.HEIGHT)
)
layer.forEach(function (value, i) {
// Indices per color
var R = 4 * i
var G = R + 1
var B = G + 1
var A = B + 1
// Set alpha value to 0xFF
byteArray[A] = 0xff
// No tile: skip or outline
if (value === 0) {
if (shouldOutline(layer, i, Number(this.CONFIGURATION.WIDTH))) {
byteArray[R] = OUTLINE_COLOR
byteArray[G] = OUTLINE_COLOR
byteArray[B] = OUTLINE_COLOR
}
return
}
// Color is the 6 byte hex RGB representation
hexColor = getMinimapColor(value, this.CONFIGURATION)
// Bitshift to extract component and write RGBA in the buffer (always 0xFF for A)
byteArray[R] = (hexColor >> 16) & 0xff
byteArray[G] = (hexColor >> 8) & 0xff
byteArray[B] = (hexColor >> 0) & 0xff
}, this)
return byteArray
}
OTMapGenerator.prototype.generateMinimap = function (configuration) {
/*
* OTMapGenerator.generateMinimapmap
* Generates clamped UInt8 buffer with RGBA values to be sent to canvas
*/
// Set the configuration
this.CONFIGURATION = configuration
// Create temporary layers
var pngLayers = this.generateMapLayers().map(this.createRGBALayer, this)
return {
data: pngLayers,
metadata: this.CONFIGURATION,
}
}
OTMapGenerator.prototype.generate = function (configuration) {
/*
* Function OTMapGenerator.generate
* Generates OTBM map and returns OTBMJSON representation
*/
this._initialized = Date.now()
if (configuration !== undefined) {
this.CONFIGURATION = configuration
}
// Default blueprint for OTBMJSON
// Create a deep copy in memory using the JSON module
var json = JSON.parse(JSON.stringify(require("./json/header")))
// Create temporary layers
var layers = this.generateMapLayers()
// Convert layers to tile areas
var tileAreas = this.generateTileAreas(layers)
// Add all tile areas to the JSON2OTBM structure
Object.keys(tileAreas).forEach(function (area) {
json.data.nodes[0].features.push(tileAreas[area])
})
// Write the map header
this.setMapHeader(json.data)
// Serialize the JSON using the OTBM2JSON lib
return otbm2json.serialize(json)
}
OTMapGenerator.prototype.setMapHeader = function (data) {
/*
* Function OTMapGenerator::setMapHeader
* Writes RME map header OTBM_MAP_DATA
*/
if (!VERSIONS.hasOwnProperty(this.CONFIGURATION.VERSION)) {
throw "The requested map version is not supported."
}
data.mapWidth = Number(this.CONFIGURATION.WIDTH)
data.mapHeight = Number(this.CONFIGURATION.HEIGHT)
var versionAttributes = VERSIONS[this.CONFIGURATION.VERSION]
data.version = versionAttributes.version
data.itemsMajorVersion = versionAttributes.itemsMajorVersion
data.itemsMinorVersion = versionAttributes.itemsMinorVersion
// Save the time & seed
data.nodes[0].description +=
new Date().toISOString() + " (" + this.CONFIGURATION.SEED + ")"
}
OTMapGenerator.prototype.generateMapLayers = function () {
/*
* Function OTMapGenerator::generateMapLayers
* Generates temporary layer with noise seeded tiles
* The layers can be later converted to area tiles for OTBM2JSON
*/
function createLayer() {
/* FUNCTION createLayer
* Creates an empty layer of map size (WIDTH x HEIGHT)
*/
return new Array(
Number(this.CONFIGURATION.WIDTH) * Number(this.CONFIGURATION.HEIGHT)
).fill(0)
}
var elevation, id
// Seed the noise function
noise.seed(this.CONFIGURATION.SEED)
// Create 8 zero filled layers
var layers = new Array(8).fill(0).map(createLayer.bind(this))
// Loop over the requested map width and height
for (var y = 0; y < Number(this.CONFIGURATION.HEIGHT); y++) {
for (var x = 0; x < Number(this.CONFIGURATION.WIDTH); x++) {
// Get the elevation
elevation = this.zNoiseFunction(x, y)
const additionalNoise = this.CONFIGURATION.GENERATION.SAND_BIOME
? 5 * this.zNoiseFunction(y, x)
: 0
id = this.getMapTileFromElevation(elevation, additionalNoise)
// Clamp the value
elevation = Math.max(Math.min(elevation, 7), 0)
// Fill the column with mountain tiles
this.fillColumn(layers, x, y, elevation, id)
}
}
// Option to smooth coast line
if (this.CONFIGURATION.GENERATION.SMOOTH_COASTLINE) {
layers = this.smoothCoastline(layers)
}
// Options to add caves
if (this.CONFIGURATION.GENERATION.ADD_CAVES) {
layers = this.digCaves(layers)
}
return layers
}
OTMapGenerator.prototype.smoothCoastline = function (layers) {
/*
* Function OTMapGenerator::smoothCoastline
* Algorithm that smoothes the coast line to get rid of "impossible" water borders
*/
var iterate = 1
var c = 0
// Constant iteration to remove impossible coastline tiles
while (iterate) {
iterate = 0
layers = layers.map(function (layer, i) {
// Coastline is only on the lowest floor
if (i !== 0) {
return layer
}
return layer.map(function (x, i) {
// Skip anything that is not a grass or sand tile
if (x !== ITEMS.GRASS_TILE_ID && x !== ITEMS.SAND_TILE_ID) {
return x
}
// Get the coordinate and the neighbours
var neighbours = this.getAdjacentTiles(layer, this.getCoordinates(i))
// If the tile needs to be eroded, we are required to reiterate
if (this.tileShouldErode(neighbours)) {
x = ITEMS.WATER_TILE_ID
iterate++
}
return x
}, this)
}, this)
console.log(
"Smoothing coastline <iteration " +
c++ +
">" +
" <" +
iterate +
" tiles eroded>"
)
}
return layers
}
OTMapGenerator.prototype.countNeighboursNegative = function (neighbours, id) {
/*
* Function OTMapGenerator::countNeighboursNegative
* Counts the number of neighbours that do not have a particular ID
*/
return Object.keys(neighbours).filter(function (x) {
return neighbours[x] !== ITEMS.GRAVEL_TILE_ID && neighbours[x] !== id
}).length
}
OTMapGenerator.prototype.countNeighbours = function (neighbours, id) {
/*
* Function OTMapGenerator::countNeighbours
* Counts the number of neighbours with particular ID
*/
return Object.keys(neighbours).filter(function (x) {
return neighbours[x] === id
}).length
}
OTMapGenerator.prototype.getMapTileFromElevation = function (
elevation,
additionalNoise
) {
/*
* Function OTMapGenerator::getMapTileFromElevation
* Maps particular elevation to tile id
*/
switch (true) {
case elevation < 0:
return ITEMS.WATER_TILE_ID
case elevation > 3:
if (additionalNoise > -1.5) {
return ITEMS.STONE_TILE_ID
} else {
return ITEMS.SNOW_TILE_ID
}
default:
switch (this.CONFIGURATION.GENERATION.MOUNTAIN_TYPE) {
case "ICY_MOUNTAIN":
if (elevation > 0) {
return ITEMS.ICE_TILE_ID
} else {
return ITEMS.SNOW_TILE_ID
}
default:
if (additionalNoise < -5) {
return ITEMS.SAND_TILE_ID
} else {
return ITEMS.GRASS_TILE_ID
}
}
}
}
OTMapGenerator.prototype.digCaves = function (layers) {
/*
* Function OTMapGenerator::digCaves
* Slow and pretty crappy algorithm to dig caves (FIXME)
*/
// Keep a reference to cave entrances
var entrances = new Array()
var self = this
for (var k = 0; k < Number(this.CONFIGURATION.GENERATION.CAVE_DEPTH); k++) {
console.log("Eroding caves <iteration " + k + ">")
layers = layers.map(function (layer, z) {
return layer.map(function (x, i) {
if (
x !==
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
return x
}
var coordinates = self.getCoordinates(i)
var neighbours = self.getAdjacentTiles(layer, coordinates)
if (
self.countNeighbours(neighbours, ITEMS.GRAVEL_TILE_ID) > 0 &&
self.countNeighboursNegative(
neighbours,
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) === 0 &&
Math.random() < Number(self.CONFIGURATION.GENERATION.CAVE_ROUGHNESS)
) {
return ITEMS.GRAVEL_TILE_ID
}
// Get neighbouring neighbours ;)
var NL = self.getAdjacentTiles(layer, {
x: coordinates.x - 1,
y: coordinates.y,
})
var NR = self.getAdjacentTiles(layer, {
x: coordinates.x + 1,
y: coordinates.y,
})
var NN = self.getAdjacentTiles(layer, {
x: coordinates.x,
y: coordinates.y - 1,
})
var NS = self.getAdjacentTiles(layer, {
x: coordinates.x,
y: coordinates.y + 1,
})
if (
Math.random() < Number(self.CONFIGURATION.GENERATION.CAVE_CHANCE) &&
NR.E ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
NL.W ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
x ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
self.countNeighbours(
neighbours,
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) === 5 &&
neighbours.W ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
neighbours.E ===
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
entrances.push({ z: z, c: coordinates })
return ITEMS.GRAVEL_TILE_ID
} else if (
Math.random() < Number(self.CONFIGURATION.GENERATION.CAVE_CHANCE) &&
NS.S ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
NN.N ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
x ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
self.countNeighbours(
neighbours,
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) === 5 &&
neighbours.S ===
mountains[
self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"
] &&
neighbours.N ===
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
entrances.push({ z: z, c: coordinates })
return ITEMS.GRAVEL_TILE_ID
}
return x
})
})
}
// Open 3x3 around the cave entrance
entrances.forEach(function (x) {
self.fillNeighbours(layers[x.z], x.c, ITEMS.GRAVEL_TILE_ID)
})
return layers
}
OTMapGenerator.prototype.fillNeighbours = function (layer, coordinates, id) {
/* FUNCTION fillNeighbours
* Fills all neighbouring tiles with particular ID
*/
layer[this.getIndex(coordinates.x - 1, coordinates.y)] = id
layer[this.getIndex(coordinates.x + 1, coordinates.y)] = id
layer[this.getIndex(coordinates.x, coordinates.y - 1)] = id
layer[this.getIndex(coordinates.x, coordinates.y + 1)] = id
layer[this.getIndex(coordinates.x + 1, coordinates.y + 1)] = id
layer[this.getIndex(coordinates.x + 1, coordinates.y - 1)] = id
layer[this.getIndex(coordinates.x - 1, coordinates.y + 1)] = id
layer[this.getIndex(coordinates.x - 1, coordinates.y - 1)] = id
}
OTMapGenerator.prototype.tileShouldErode = function (neighbours) {
/* FUNCTION tileShouldErode
* Returns whether a tile should be eroded by the coastline
*/
return (
(neighbours.N === ITEMS.WATER_TILE_ID &&
neighbours.S === ITEMS.WATER_TILE_ID) ||
(neighbours.E === ITEMS.WATER_TILE_ID &&
neighbours.W === ITEMS.WATER_TILE_ID) ||
((neighbours.E !== ITEMS.WATER_TILE_ID ||
neighbours.S !== ITEMS.WATER_TILE_ID) &&
neighbours.NE === ITEMS.WATER_TILE_ID &&
neighbours.SW === ITEMS.WATER_TILE_ID) ||
((neighbours.W !== ITEMS.WATER_TILE_ID ||
neighbours.N !== ITEMS.WATER_TILE_ID) &&
neighbours.SE === ITEMS.WATER_TILE_ID &&
neighbours.NW === ITEMS.WATER_TILE_ID) ||
(neighbours.N === ITEMS.WATER_TILE_ID &&
neighbours.E === ITEMS.WATER_TILE_ID &&
neighbours.S === ITEMS.WATER_TILE_ID) ||
(neighbours.E === ITEMS.WATER_TILE_ID &&
neighbours.S === ITEMS.WATER_TILE_ID &&
neighbours.W === ITEMS.WATER_TILE_ID) ||
(neighbours.S === ITEMS.WATER_TILE_ID &&
neighbours.W === ITEMS.WATER_TILE_ID &&
neighbours.N === ITEMS.WATER_TILE_ID) ||
(neighbours.W === ITEMS.WATER_TILE_ID &&
neighbours.N === ITEMS.WATER_TILE_ID &&
neighbours.E === ITEMS.WATER_TILE_ID)
)
}
OTMapGenerator.prototype.fillColumn = function (layers, x, y, z, id) {
/* FUNCTION fillColumn
* Fills a column at x, y until z, with id on top
*/
// Get the index of the tile
var index = this.getIndex(x, y)
// Set top item
layers[z][index] = id
// Fill downwards with mountain
for (var i = 0; i < z; i++) {
layers[i][index] =
mountains[this.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
}
}
OTMapGenerator.prototype.getIndex = function (x, y) {
/* FUNCTION getIndex
* Converts x, y to layer index
*/
return x + y * Number(this.CONFIGURATION.WIDTH)
}
OTMapGenerator.prototype.getAdjacentTiles = function (layer, coordinates) {
/* FUNCTION getAdjacentTiles
* Returns adjacent tiles of another tile
*/
var x = coordinates.x
var y = coordinates.y
return {
N: this.getTile(layer, x, y - 1),
NE: this.getTile(layer, x + 1, y - 1),
E: this.getTile(layer, x + 1, y),
SE: this.getTile(layer, x + 1, y + 1),
S: this.getTile(layer, x, y + 1),
SW: this.getTile(layer, x - 1, y + 1),
W: this.getTile(layer, x - 1, y),
NW: this.getTile(layer, x - 1, y - 1),
}
}
OTMapGenerator.prototype.getTile = function (layer, x, y) {
/* FUNCTION getTile
* Returns tile at layer & coordinates
*/
return layer[this.getIndex(x, y)]
}
OTMapGenerator.prototype.zNoiseFunction = function (x, y) {
/* FUNCTION zNoiseFunction
* Returns noise as a function of x, y
*
* MODIFY THESE PARAMETERS TO CREATE DIFFERENT MAPS!
* I DON'T KNOW ABOUT THE SENSITIVITY OF THESE PARAMETERS: JUST PLAY!
* See this: https://www.redblobgames.com/maps/terrain-from-noise/
*/
// Island parameters
/** Increments the noise by x (noise + x) */
const noiseIncrementer = Number(this.CONFIGURATION.GENERATION.NOISE_INCREMENT)
/** Multiplies the distance from center ((1 - x) * distance) */
const distanceMultiplier = Number(
this.CONFIGURATION.GENERATION.ISLAND_DISTANCE_DECREMENT
)
/** Exponential for the island distance from center */
const distanceExponential = Number(
this.CONFIGURATION.GENERATION.ISLAND_DISTANCE_EXPONENT
)
/** Exponential for the noise (for higher elevations) */
const noiseExponential = Number(this.CONFIGURATION.GENERATION.EXPONENT)
/** Multiplies the final noise result */
const noiseMultiplier = Number(this.CONFIGURATION.GENERATION.LINEAR)
/** Water level will decrement the final noise result */
const waterLevel = Number(this.CONFIGURATION.GENERATION.WATER_LEVEL)
// Scaled coordinates between -0.5 and 0.5
const nx = x / (Number(this.CONFIGURATION.WIDTH) - 1) - 0.5
const ny = y / (Number(this.CONFIGURATION.HEIGHT) - 1) - 0.5
let manhattanDistance
// Manhattan distance
if (this.CONFIGURATION.GENERATION.EUCLIDEAN) {
manhattanDistance = Math.sqrt(nx * nx + ny * ny)
} else {
manhattanDistance = 2 * Math.max(Math.abs(nx), Math.abs(ny))
}
// Get the noise value
let noise = this.sumFrequencies(nx, ny)
// Some exponent for mountains?
noise = Math.pow(noise, noiseExponential | 0)
// Use distance from center to create an island
return (
Math.round(
noiseMultiplier *
(noise + noiseIncrementer) *
(1 -
distanceMultiplier * Math.pow(manhattanDistance, distanceExponential))
) -
(waterLevel | 0)
)
}
OTMapGenerator.prototype.sumFrequencies = function (nx, ny) {
/* FUNCTION OTMapGenerator.sumFrequencies
* Sums all the noise frequencies
*/
return this.CONFIGURATION.GENERATION.FREQUENCIES.reduce(
function (total, x) {
return total + this.simplex2freq(Number(x.f), Number(x.weight), nx, ny)
}.bind(this),
0
)
}
OTMapGenerator.prototype.getCoordinates = function (index) {
/* FUNCTION getCoordinates
* Returns coordinates for a given layer index
*/
return {
x: index % Number(this.CONFIGURATION.WIDTH),
y: Math.floor(index / Number(this.CONFIGURATION.WIDTH)),
}
}
OTMapGenerator.prototype.simplex2freq = function (f, weight, nx, ny) {
/* FUNCTION simplex2freq
* Returns simplex noise on position nx, ny scaled between -0.5 and 0.5
* at a given frequency
*/
// Scale the frequency to the map size
fWidth = (f * Number(this.CONFIGURATION.WIDTH)) / this.TILE_AREA_SIZE
fHeight = (f * Number(this.CONFIGURATION.HEIGHT)) / this.TILE_AREA_SIZE
return weight * noise.simplex2(fWidth * nx, fHeight * ny)
}
OTMapGenerator.prototype.generateTileAreas = function (layers) {
/* FUNCTION generateTileAreas
* Converts layers to OTBM tile areas
*/
const {
getMountainWallOuter,
getFloatingBorder,
getWaterBorder,
getMountainBorders,
getMountainWall,
getWaterBorderSand,
getSandBorder,
getGrassBorder,
getMountainAdditionalItems,
} = useBorders(this.CONFIGURATION)
function createOTBMItem(id) {
/* FUNCTION createOTBMItem
* Creates OTBM_ITEM object for OTBM2JSON
*/
return {
type: otbm2json.HEADERS.OTBM_ITEM,
id: id,
}
}
// Create hashmap for the tile areas
var tileAreas = new Object()
var self = this
// Convert layers to OTBM tile areas
layers.forEach(function (layer, z) {
// Invert the depth
var areaZ = 7 - z
// For all tiles on each layer
layer.forEach(function (x, i) {
// Transform layer index to x, y coordinates
var coordinates = self.getCoordinates(i)
// Convert global x, y coordinates to tile area coordinates (0, 255, 510, 765)
var areaX =
self.TILE_AREA_SIZE * Math.floor(coordinates.x / self.TILE_AREA_SIZE)
var areaY =
self.TILE_AREA_SIZE * Math.floor(coordinates.y / self.TILE_AREA_SIZE)
// Create a tile area identifier for use in a hashmap
var areaIdentifier = areaX + "." + areaY + "." + areaZ
// If the tile area does not exist create it
if (!tileAreas.hasOwnProperty(areaIdentifier)) {
tileAreas[areaIdentifier] = {
type: otbm2json.HEADERS.OTBM_TILE_AREA,
x: areaX,
y: areaY,
z: areaZ,
tiles: new Array(),
}
}
// Items to be placed on a tile (e.g. borders)
var items = new Array()
if (!self.CONFIGURATION.TERRAIN_ONLY) {
// Get the tile neighbours and determine bordering logic
var neighbours = self.getAdjacentTiles(layer, coordinates)
// Mountain tile: border outside
if (
x !==
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
pushItemToArray(items, getMountainWallOuter(neighbours))
}
// All empty tiles can be skipped
if (x === 0) {
return
}
// Mountain tile: border inside
if (
!items.length &&
x ===
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
pushItemToArray(items, getMountainWall(neighbours))
}
/** Additional items inside the mountain tile */
if (
x ===
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
pushItemToArray(items, getMountainAdditionalItems(neighbours, items))
}
n =
(self.simplex2freq(8, 3, coordinates.x, coordinates.y) +
self.simplex2freq(16, 0.5, coordinates.x, coordinates.y) +
self.simplex2freq(32, 0.5, coordinates.x, coordinates.y)) /
4
// Crappy noise map to put forests (FIXME)
// Check if the tile is occupied
if (!items.length && x === ITEMS.GRASS_TILE_ID) {
if (n > 0 && Math.random() < 0.4) {
pushItemToArray(items, clutter.randomTree())
}
}
var nWaterNeighbours = self.countNeighbours(
neighbours,
ITEMS.WATER_TILE_ID
)
// Add a random water plant
if (!items.length && x === ITEMS.WATER_TILE_ID) {
pushItemToArray(
items,
clutter.randomWaterPlant(
self.countNeighbours(neighbours, ITEMS.GRASS_TILE_ID)
)
)
}
if (
!items.length &&
(x === ITEMS.GRASS_TILE_ID || x === ITEMS.SAND_TILE_ID) &&
nWaterNeighbours !== 0
) {
if (n > 0 && Math.random() < 0.075) {
pushItemToArray(items, clutter.randomSandstoneMossy())
}
}
// Clutter to be added to a sand tile
if (!items.length && x === ITEMS.SAND_TILE_ID) {
if (n > 0 && Math.random() < 0.25 && nWaterNeighbours === 0) {
pushItemToArray(items, clutter.randomPebble())
} else if (n > 0.33 && Math.random() < 0.25) {
pushItemToArray(items, clutter.randomCactus())
} else if (Math.random() < 0.45) {
pushItemToArray(items, clutter.randomPalmTree(neighbours))
} else if (z === 0 && Math.random() < 0.075) {
pushItemToArray(items, clutter.randomShell())
} else if (Math.random() < 0.015) {
pushItemToArray(items, clutter.randomSandstone())
}
}
// clutter to be added to ice tile
if (x === ITEMS.ICE_TILE_ID || x === ITEMS.SNOW_TILE_ID) {
if (n > 0.25) {
items.push(clutter.randomIces())
}
if (n > 0 && Math.random() < 0.2) {
items.push(clutter.randomSnows())
}
if (n > 0 && Math.random() < 0.1) {
items.push(clutter.randomPebble())
}
if (n > 0 && Math.random() < 0.05) {
items.push(6966)
}
}
// Add a random water plant
if (x === ITEMS.STONE_TILE_ID) {
if (n > 0.25) {
items.push(clutter.randomTileMoss())
}
if (n > 0 && Math.random() < 0.5) {
items.push(clutter.randomPebble())
}
}
// Add grass border to gravel tile
if (x === ITEMS.GRAVEL_TILE_ID) {
pushItemToArray(items, getGrassBorder(neighbours))
}
// Add water border for sand tile
if (x === ITEMS.SAND_TILE_ID) {
pushItemToArray(items, getWaterBorderSand(neighbours))
}
// Add sand border to gravel and grass
if (x === ITEMS.GRAVEL_TILE_ID || x === ITEMS.GRASS_TILE_ID) {
Array.prototype.push.apply(items, getSandBorder(neighbours))
}
// Border grass & water interface
if (x === ITEMS.GRASS_TILE_ID || x === ITEMS.SNOW_TILE_ID) {
pushItemToArray(items, getWaterBorder(neighbours))
}
// Add wide border on top of mountain (multiple)
if (
x === ITEMS.GRASS_TILE_ID ||
x === ITEMS.STONE_TILE_ID ||
x === ITEMS.SAND_TILE_ID ||
x === ITEMS.ICE_TILE_ID ||
x === ITEMS.SNOW_TILE_ID
) {
Array.prototype.push.apply(items, getFloatingBorder(neighbours))
}
// Border at foot of mountain
if (
x !==
mountains[self.CONFIGURATION.GENERATION.MOUNTAIN_TYPE + "_TILE_ID"]
) {
const borders = getMountainBorders(neighbours)
if (borders.length) {
borders.map((item) => pushItemToArray(items, item))
}
}
// Version filter remove anything below a certain ID
items = items.filter(function (id) {
return id !== 0 && id < VERSIONS[self.CONFIGURATION.VERSION].maxId
})
}
// Add the tile to the tile area
// Make sure to give coordinates in RELATIVE tile area coordinates
tileAreas[areaIdentifier].tiles.push({
type: otbm2json.HEADERS.OTBM_TILE,
x: coordinates.x % self.TILE_AREA_SIZE,
y: coordinates.y % self.TILE_AREA_SIZE,
tileid: clutter.randomizeTile(x),
items: items.map(createOTBMItem),
})
})
})
return tileAreas
}
/** If source item is an array, will push all of the array items. Otherwise, just push the single item */
function pushItemToArray(array, item) {
if (item && item !== null) {
array.push(...(typeof item === "object" ? item : [item]))
}
}
module.exports.OTMapGenerator = OTMapGenerator
module.exports.DEFAULT_CONFIG = DEFAULT_CONFIG