-
Notifications
You must be signed in to change notification settings - Fork 6
/
gen.js
445 lines (380 loc) · 13.4 KB
/
gen.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
function loopThroughHeightmap(callback) {
world.elevation.forEach((row, x) => {
row.forEach((localElevation, y) => {
let localHumidity = world.humidity[x][y];
callback(localElevation, localHumidity, x, y);
});
});
}
function chooseRandom(array) {
return array[Math.floor(Math.random() * (array.length))];
}
/*GENERATION OF TERRAIN*/
var world, params;
function performStructureCheck(structureWeight, structureConditions) {
let structureRoll = Math.random();
if(structureRoll > structureWeight) return false
if(structureConditions == false) return false
return true
}
function createHeightmap({ base = 0, amplitude = 1, scale = 100, resolution = 256, roundFactor = 10, granularScale = 0.03, octaves = 5 } = {}, seed) {
const { Perlin2 } = require('tumult'),
small = granularScale * scale,
map = new Perlin2(seed);
var heightmap = Array(resolution).fill().map(() => Array(resolution).fill(0));
for (let x = 0; x < resolution; x++) {
for (let y = 0; y < resolution; y++) {
const value = base + (6 * map.gen(x / small, y / small) + 120 * map.octavate(octaves, x / scale, y / scale)) * amplitude;
heightmap[x][y] = Math.round(value * roundFactor) / roundFactor;
}
}
return heightmap;
}
function createStructure({type, x = 0, y = 0, ...otherData} = {}, {offset = 0.5, roundFactor = 10} = {}) {
var structure = {
type: type,
x: Math.round((x + (2 * offset * Math.random()) - offset) * roundFactor) / roundFactor,
y: Math.round((y + (2 * offset * Math.random()) - offset) * roundFactor) / roundFactor,
elevation: world.elevation[x][y],
humidity: world.humidity[x][y],
...otherData
};
return structure;
}
function generate({ resolution, hilliness, baseHumidity, humidityRange, biomeScale, landScale, seaLevel, roundFactor, granularScale, baseElevation, generateStructures, structureOffset, structureWeights, octaves } = params, seed = Math.random()) {
console.time("generate");
//softcapping resolution at 512
if (resolution > 512) console.warn("Map sizes above 512 not officially supported, any bugs related to this may not be fixed");
world = {
elevation: createHeightmap(
{
base: baseElevation,
amplitude: hilliness,
scale: landScale,
resolution: resolution,
roundFactor: roundFactor,
granularScale: granularScale,
octaves: octaves
},
seed + 0.01),
//elevation uses a different seed than humidity so that the heightmaps don't look identical
humidity: createHeightmap(
{
base: baseHumidity,
amplitude: humidityRange,
scale: biomeScale,
resolution: resolution,
roundFactor: roundFactor,
granularScale: granularScale
},
seed),
seaLevel: seaLevel,
seed: seed,
structures: [],
};
if (generateStructures) {
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
//cactus
if(performStructureCheck(structureWeights.cactus, (localElevation < 400 && localElevation > (seaLevel + 15) && localHumidity < -15 && localHumidity > -100))){
world.structures.push(createStructure(
{
type: "cactus",
x: x,
y: y,
height: Math.round(roundFactor * (Math.random() * 2.3 + 1)) / roundFactor
},
{
offset: structureOffset,
roundFactor: roundFactor
}));
}
//cactusDry
if(performStructureCheck(structureWeights.cactusDry, (localElevation < 400 && localElevation > (seaLevel + 15) && localHumidity < -100 && localHumidity > -250))){
world.structures.push(createStructure(
{
type: "cactus",
x: x,
y: y,
height: Math.round(roundFactor * (Math.random() * 1.9 + 1)) / roundFactor, //dry variant of cactus cannot generate as tall
...(Math.random() < 0.04) && { customColor: "#C81" } //4% of drycactuses will be "dead"
},
{
offset: structureOffset,
roundFactor: roundFactor
}));
}
//castle
if(performStructureCheck(structureWeights.castle, (localElevation < 900 && localElevation > (seaLevel + 15) && localHumidity > -80))){
world.structures.push(createStructure(
{
type: "castle",
x: x,
y: y,
customColor: chooseRandom(['#333', "#666", "#005", "#500", "#050"]),
height: Math.round(roundFactor * (Math.random() * 2 + 2)) / roundFactor,
},
{
offset: structureOffset,
roundFactor: roundFactor
}));
}
});
}
draw();
console.timeEnd("generate");
}
function polygon() {
generate({
...params,
granularScale: 0.000000001,
generateStructures: false //THERE ARE NO STRUCTURES IN THE POLYGON REALM MUAHAHAH
});
console.log("HAIL SIERPINSKI");
}
function regenerate(){
generate(params, world.seed);
}
/*DRAWING TERRAIN TO CANVAS*/
function drawPixel(ctx, width, height, x, y){
ctx.fillRect(width * x, height * y, width + 1, height + 1);
}
function drawTriangle(ctx, width, height, x, y){
let path = new Path2D();
path.moveTo(width * x, height * (y + 1));
path.lineTo(width * (x + 0.5), height * y);
path.lineTo(width * (x + 1), height * (y + 1));
ctx.fill(path);
}
function drawLand(mode = params.drawmode) {
const ctx = document.getElementById("terrainbox").getContext('2d'),
{ width, height } = document.getElementById("terrainbox"),
{ elevation } = world,
boxWidth = (width / elevation.length),
boxHeight = (height / elevation.length),
biomes = require('./biomes.json');
switch (mode) {
case "elevation":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
ctx.fillStyle = `rgb(0, ${localElevation / 10 + 30}, 0)`;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
break;
case "absolute":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
let zLevel = (localElevation + 1000) / 15;
ctx.fillStyle = `rgb(${zLevel}, ${zLevel}, 0)`;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
break;
case "humidity":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
ctx.fillStyle = `rgb(0, 0, ${(localHumidity + 100) / 2})`;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
break;
default:
case "normal":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
if (localElevation > 1300) ctx.fillStyle = biomes.peak;
else if (localElevation > 1100) ctx.fillStyle = biomes.mountain;
else if (localElevation > 850) ctx.fillStyle = biomes.mountain2;
else if (localElevation > 750) {
ctx.fillStyle = localHumidity > 0 ? biomes.mountain2 : biomes.mesa;
}
else if (localElevation > -100) {
ctx.fillStyle = biomes.desert;
if (localHumidity > -30 ) ctx.fillStyle = biomes.savannah;
if (localHumidity > 0 ) ctx.fillStyle = biomes.plains;
if (localHumidity > 150 ) ctx.fillStyle = biomes.forest;
if (localHumidity > 250 ) ctx.fillStyle = biomes.urwald;
}
else if (localElevation > -500){
ctx.fillStyle = localHumidity > 0 ? biomes.desert : biomes.canyon;
}
else ctx.fillStyle = biomes.desertabyss;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
}
}
function drawWater(mode = params.drawMode) {
const ctx = document.getElementById("waterbox").getContext('2d'),
{ width, height } = document.getElementById("waterbox"),
{ elevation, seaLevel } = world,
boxWidth = (width / elevation.length),
boxHeight = (height / elevation.length),
biomes = require('./biomes.json');
ctx.clearRect(0, 0, width, height);
switch (mode) {
case "elevation":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
if (localElevation > seaLevel) return
ctx.fillStyle = `rgb(0, 0, ${(seaLevel - localElevation) / 10 + 30})`;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
break;
case "absolute":
//absolute drawmode does not render water but I'm still putting it here so it matches
return
case "humidity":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
if (localElevation > seaLevel) return
ctx.fillStyle = '#07F';
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
break;
default:
case "normal":
loopThroughHeightmap((localElevation, localHumidity, x, y) => {
if (localElevation > seaLevel) return
else if (localElevation > seaLevel - 200) ctx.fillStyle = biomes.shore;
else if (localElevation > seaLevel - 800) ctx.fillStyle = biomes.water;
else if (localElevation > seaLevel - 1250) ctx.fillStyle = biomes.abyss;
else ctx.fillStyle = biomes.trench;
drawPixel(ctx, boxWidth, boxHeight, x, y);
});
}
}
function drawStructures(mode = params.drawMode) {
const ctx = document.getElementById("strbox").getContext('2d'),
{ width, height } = document.getElementById("strbox"),
{ elevation, structures } = world,
boxWidth = (width / elevation.length),
boxHeight = (height / elevation.length);
ctx.clearRect(0, 0, width, height);
if(mode != "normal") return //structures should only be visable in the normal drawmode
structures.forEach(structure => {
const {x, y, type, customColor} = structure;
if(type == "cactus"){
ctx.fillStyle = customColor || "#371";
drawPixel(ctx, boxWidth * 0.9, boxHeight * structure.height, x / 0.9, (y / structure.height) - structure.height);
}
if(type =="castle"){
ctx.fillStyle = customColor;
drawTriangle(ctx, boxWidth * 1.5, boxHeight * structure.height, x / 1.5, (y / structure.height) - structure.height)
drawPixel(ctx, boxWidth * 1.5, boxHeight * structure.height, (x / 1.5) - 0.05, 0.95 + (y / structure.height) - structure.height);
}
});
}
function draw(mode = params.drawMode) {
params.drawMode = mode || "normal";
drawLand(mode);
drawStructures(mode);
drawWater(mode);
}
/* SAVING AND LOADING WORLDS AND PARAMETERS*/
function saveWorld() {
const saveWorld = { ...world };
ipcRenderer.send("saveWorld", saveWorld);
}
async function loadWorld() {
const savedWorld = await ipcRenderer.invoke("loadWorld");
if (!savedWorld) return
world = { ...savedWorld };
//repairs worlds with missing data
const { seed, seaLevel, humidity, elevation } = world;
if (!seed) {
console.warn("No seed detected, defaulting to undefined");
world.seed = undefined;
}
if (seaLevel === undefined) {
console.warn("No sea level detected, defaulting to 0");
world.seaLevel = 0;
}
if (!humidity || !Array.isArray(humidity)) {
console.warn("No humidity heightmap detected, defaulting to seed");
const {baseHumidity, humidityRange, biomeScale, roundFactor, granularScale} = params;
world.humidity = createHeightmap({
base: baseHumidity,
amplitude: humidityRange,
scale: biomeScale,
resolution: elevation.length,
roundFactor: roundFactor,
granularScale: granularScale
}, seed);
}
if (!elevation || !Array.isArray(elevation)) {
console.error("World data irreversably corrupted");
return
}
draw();
}
function initParams() {
params = {
resolution: 256, //resolution of terrain array (n * n)
hilliness: 30, //amplitude of the elevation heightmap
humidityRange: 6, //amplitude for the humidity heightmap
baseElevation: 0, //baseline for elevation
baseHumidity: 50, //baseline for humidity
biomeScale: 155, //scale of humidity-based biomes
landScale: 100, //scale for landforms
granularScale: 0.03, //size of granular octave relative to the land and biomeScale
seaLevel: 0, //default sea level
drawMode: 'normal', //drawmode - valid values: normal, heightmap, humidity, absolute
roundFactor: 10, //to which decimal place terrain array values are rounded
waterDrawRate: 700, //how fast water is redrawn
retainParams: true, //whether params should be saved/loaded
generateStructures: true, //whether structures will be generated
structureOffset: 0.5, //how far structures can be moved in the x and y direction
structureWeights: {
cactus: 0.0071,
cactusDry: 0.0012,
castle: 0.00005
} //how often the various structures generate
};
}
function loadParams () {
const fs = require('fs');
initParams();
if (fs.existsSync(`${__dirname}/params.json`)){
let savedParams = JSON.parse(fs.readFileSync(`${__dirname}/params.json`));
if(savedParams.retainParams == false) {
params.retainParams = false;
return
}
for(let param in savedParams){
if(param == "drawMode") return //drawmode does not need to be saved
params[param] = savedParams[param];
}
console.log(`Loaded params from ${__dirname}/params.json`);
} else {
console.log("No params.json detected, creating one");
saveParams();
}
}
function saveParams(){
const fs = require('fs');
fs.writeFileSync(`${__dirname}/params.json`, JSON.stringify(params));
}
/*INTERPROCESS COMMUNICATION*/
const { ipcRenderer } = require("electron");
//detects setting change from the settings window and applies it
ipcRenderer.on("setting", (e, args) => {
const settingToChange = args[0],
newValue = args[1];
params[settingToChange] = newValue;
if (settingToChange == "seaLevel") {
const drawDelay = (world.elevation.length ** 2 / params.waterDrawRate) | 0;
world.seaLevel = newValue;
//prevents redrawing from happening too often as it slows things down
if (new Date() - drawWater.lastCall > drawDelay || !drawWater.lastCall) {
drawWater();
drawWater.lastCall = new Date();
}
}
if(settingToChange != "retainParams" && params.retainParams == false) return //only save params to params.json if allowed to do so
saveParams();
});
//sends settings to settings screen when it's loaded
ipcRenderer.on("loadSettings", (e, winID) => {
ipcRenderer.sendTo(winID, "sendSettings",
{
...params,
seaLevel: world.seaLevel
});
});
//keyboard shortcut to generate terrain (ctrl+g) and drawmodes (ctrl + 1,2,3)
ipcRenderer.on("shortcut", (e, ...args) => {
const shortcut = args[0],
shortcutArgs = args.slice(1);
window[shortcut](...shortcutArgs);
});