160 lines
5.1 KiB
JavaScript
160 lines
5.1 KiB
JavaScript
module.exports = {
|
|
generateMap: function(width, height) {
|
|
var map = [];
|
|
var i = 0;
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","4","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","4","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","4","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","4","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","4","4","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","4","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","4","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
map[i++] = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"];
|
|
|
|
var g = [6,5,4,3,2];
|
|
for(var k=0;k<g.length;k++) {
|
|
var i = g[k]+"";
|
|
for(var w=1;w<map.length-1;w++) {
|
|
for(var h=1;h<map[w].length-1;h++) {
|
|
if(map[w][h] == "0" && (map[w-1][h]==i || map[w][h-1]==i || map[w][h+1]==i || map[w+1][h]==i || map[w+1][h+1]==i || map[w-1][h-1]==i || map[w+1][h-1]==i || map[w-1][h+1]==i)) {
|
|
map[w][h] = (i-1)+""; //set ground
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return map;
|
|
|
|
|
|
for(var w=0;w<width;w++) {
|
|
map[w] = [];
|
|
for(var h=0;h<height;h++) {
|
|
//make border
|
|
if(w<=0 || h<=0 || w>=width-1 || h>=height-1) {
|
|
map[w][h] = "0";
|
|
} else {
|
|
map[w][h] = "0";
|
|
p = 0.1;
|
|
if(rand(p))
|
|
map[w][h] = "#";
|
|
}
|
|
}
|
|
}
|
|
|
|
//preprocessing
|
|
|
|
for(i=0;i<8;i++) { //expand all islands
|
|
var orgMap = [];
|
|
for(var w=0;w<width;w++) {
|
|
orgMap[w] = [];
|
|
for(var h=0;h<height;h++) {
|
|
orgMap[w][h] = map[w][h];
|
|
}
|
|
}
|
|
|
|
var exP = 80;
|
|
for(var w=1;w<width-1;w++) {
|
|
for(var h=1;h<height-1;h++) {
|
|
if(orgMap[w][h]=="#") {
|
|
if(rand(exP)) map[w-1][h]="#"
|
|
if(rand(exP)) map[w+1][h]="#"
|
|
if(rand(exP)) map[w][h-1]="#"
|
|
if(rand(exP)) map[w][h+1]="#"
|
|
if(rand(exP)) map[w-1][h-1]="#"
|
|
if(rand(exP)) map[w-1][h+1]="#"
|
|
if(rand(exP)) map[w+1][h-1]="#"
|
|
if(rand(exP)) map[w+1][h+1]="#"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for(var w=0;w<width;w++) {
|
|
for(var h=0;h<height;h++) {
|
|
if(h<4 || w<4 || w>width-5 || h>height-5) {
|
|
map[w][h] = "0"; //border water
|
|
}
|
|
}
|
|
}
|
|
|
|
for(i=0;i<10;i++) { //fill water holes
|
|
for(var w=1;w<width-1;w++) {
|
|
for(var h=1;h<height-1;h++) {
|
|
if(map[w][h]=="0") {
|
|
var groundCtn = 0;
|
|
if(map[w-1][h]=="#")
|
|
groundCtn++;
|
|
if(map[w+1][h]=="#")
|
|
groundCtn++;
|
|
if(map[w][h-1]=="#")
|
|
groundCtn++;
|
|
if(map[w][h+1]=="#")
|
|
groundCtn++;
|
|
if(map[w-1][h-1]=="#")
|
|
groundCtn++;
|
|
if(map[w-1][h+1]=="#")
|
|
groundCtn++;
|
|
if(map[w+1][h-1]=="#")
|
|
groundCtn++;
|
|
if(map[w+1][h+1]=="#")
|
|
groundCtn++;
|
|
|
|
if(groundCtn >= 5)
|
|
map[w][h] = "#";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* groundtypes
|
|
g = ground w = water
|
|
*/
|
|
|
|
|
|
//Set ground type
|
|
for(var w=0;w<width;w++) {
|
|
for(var h=0;h<height;h++) {
|
|
if(map[w][h] == "#") {
|
|
map[w][h] = "4"; //set ground
|
|
}
|
|
}
|
|
}
|
|
|
|
// var g = [4,3,2];
|
|
// for(var k=0;k<g.length;k++) {
|
|
// var i = g[k]+"";
|
|
// for(var w=0;w<width;w++) {
|
|
// for(var h=0;h<height;h++) {
|
|
// if(map[w][h] == "0" && !(w<=0 || h<=0 || w>=width-1 || h>=height-1) && (map[w-1][h]==i || map[w][h-1]==i || map[w][h+1]==i || map[w+1][h]==i || map[w+1][h+1]==i || map[w-1][h-1]==i || map[w+1][h-1]==i || map[w-1][h+1]==i)) {
|
|
// map[w][h] = (i-1)+""; //set ground
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
|
|
return map;
|
|
}
|
|
};
|
|
|
|
function rand(p) {
|
|
if(p > Math.random()*100)
|
|
return true;
|
|
return false;
|
|
} |