2022-06-19 20:21:33 +00:00
|
|
|
const util = require("./util.js")
|
2022-06-21 14:46:34 +00:00
|
|
|
const log = require("./log.js")
|
2022-06-21 15:32:56 +00:00
|
|
|
const searchLoc =
|
|
|
|
[
|
|
|
|
[-1,1], [0,1], [1,1],
|
|
|
|
[-1,0], [1,0],
|
|
|
|
[-1,-1],[0,-1],[1,-1]
|
|
|
|
]
|
2022-06-19 20:21:33 +00:00
|
|
|
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-06-21 15:32:56 +00:00
|
|
|
tempWorld[x][y] = {type: 0, structure: 0, flag: 0, mask: true}
|
2022-06-19 20:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:32:56 +00:00
|
|
|
// Generate board
|
|
|
|
for(let x = 0; x < gridSize[0]; x++){
|
|
|
|
for(let y = 0; y < gridSize[1]; y++){
|
|
|
|
const value = util.randomNumber(0, 6);
|
2022-06-21 14:46:34 +00:00
|
|
|
var n;
|
|
|
|
switch (value) {
|
2022-06-21 15:32:56 +00:00
|
|
|
case 0:
|
|
|
|
n = 0;
|
|
|
|
break;
|
2022-06-21 14:46:34 +00:00
|
|
|
case 1:
|
|
|
|
n = 0
|
|
|
|
break;
|
|
|
|
case 2:
|
2022-06-21 15:32:56 +00:00
|
|
|
n = 0
|
2022-06-21 14:46:34 +00:00
|
|
|
break;
|
2022-06-21 15:32:56 +00:00
|
|
|
case 3:
|
2022-06-21 14:46:34 +00:00
|
|
|
n = 0
|
|
|
|
break;
|
2022-06-21 15:32:56 +00:00
|
|
|
case 4:
|
|
|
|
n = 0
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
n = 5
|
|
|
|
break;
|
2022-06-21 14:46:34 +00:00
|
|
|
default:
|
|
|
|
log.log("OTHER")
|
|
|
|
n = 7
|
2022-06-19 20:21:33 +00:00
|
|
|
|
|
|
|
}
|
2022-06-21 14:46:34 +00:00
|
|
|
tempWorld[x][y].type = n
|
2022-06-19 20:21:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 15:32:56 +00:00
|
|
|
// Place Numbers
|
|
|
|
for(let x = 0; x < gridSize[0]; x++){
|
|
|
|
for(let y = 0; y < gridSize[1]; y++){
|
|
|
|
if (tempWorld[x][y].type === 0) {
|
|
|
|
var counter = 0;
|
|
|
|
searchLoc.forEach(location => {
|
|
|
|
const tempx = x + location[0]
|
|
|
|
const tempy = y + location[1]
|
|
|
|
if (tempx >= 0 && tempy >= 0 && tempx < gridSize[0] && tempy < gridSize[1]) {
|
|
|
|
if (tempWorld[tempx][tempy].type === 5) {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (counter !== 0) {
|
|
|
|
tempWorld[x][y].type = 15 + counter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 14:46:34 +00:00
|
|
|
|
2022-06-19 20:21:33 +00:00
|
|
|
return tempWorld;
|
|
|
|
}
|
2022-06-21 14:46:34 +00:00
|
|
|
|
2022-06-19 20:21:33 +00:00
|
|
|
module.exports = { generateWorld };
|