2022-04-30 21:49:20 +00:00
|
|
|
var noise = require('./perlin.js');
|
2022-04-20 18:53:36 +00:00
|
|
|
const util = require("./util.js")
|
|
|
|
|
|
|
|
function generateWorld(gridSize, perlinScale){
|
|
|
|
let x, y = 0
|
|
|
|
|
|
|
|
let tempWorld = Array.from(Array(gridSize[0]), () => new Array(gridSize[1]));
|
|
|
|
for (x = 0; x < gridSize[0]; x++){
|
|
|
|
for (y = 0; y < gridSize[1]; y++){
|
2022-04-21 03:23:51 +00:00
|
|
|
tempWorld[x][y] = {type: 0, structure: 0, color: null, owner: null}
|
2022-04-20 18:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
noise.seed(Math.random())
|
|
|
|
for(x = 0; x < gridSize[0]; x++){
|
|
|
|
for(y = 0; y < gridSize[1]; y++){
|
|
|
|
|
|
|
|
var value = (noise.perlin2(x/perlinScale, y/perlinScale))*10;
|
|
|
|
if (value >= -3) {
|
|
|
|
tempWorld[x][y].type = 0
|
|
|
|
} else if (value < -3) {
|
|
|
|
tempWorld[x][y].type = 1
|
|
|
|
}
|
|
|
|
if (value > 1.4) {
|
|
|
|
tempWorld[x][y].type = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < gridSize[0]*gridSize[1]/15; i){
|
|
|
|
x = util.randomNumber(0,gridSize[0]);
|
|
|
|
y = util.randomNumber(0,gridSize[1]);
|
|
|
|
if (tempWorld[x][y].type != 1) {
|
|
|
|
i++;
|
2022-04-21 03:23:51 +00:00
|
|
|
tempWorld[x][y].structure = util.randomNumber(1,4)
|
2022-04-20 18:53:36 +00:00
|
|
|
}
|
2022-04-21 03:23:51 +00:00
|
|
|
}
|
2022-04-20 18:53:36 +00:00
|
|
|
return tempWorld;
|
|
|
|
}
|
|
|
|
module.exports = { generateWorld };
|