166 lines
3.4 KiB
JavaScript
166 lines
3.4 KiB
JavaScript
module.exports = {
|
|
generateMap: function(width, height) {
|
|
var 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 = 1;
|
|
if(map[w-1][h]=="#")
|
|
p+=43;
|
|
if(map[w][h-1]=="#")
|
|
p+=43;
|
|
if(rand(p))
|
|
map[w][h] = "#";
|
|
}
|
|
}
|
|
}
|
|
|
|
//preprocessing
|
|
|
|
|
|
for(i=0;i<2;i++) { //Reduce little ilands
|
|
for(var w=0;w<width;w++) {
|
|
for(var h=0;h<height;h++) {
|
|
if(map[w][h]=="#") {
|
|
var waterCnt = 0;
|
|
if(map[w-1][h]=="0")
|
|
waterCnt++;
|
|
if(map[w+1][h]=="0")
|
|
waterCnt++;
|
|
if(map[w][h-1]=="0")
|
|
waterCnt++;
|
|
if(map[w][h+1]=="0")
|
|
waterCnt++;
|
|
if(map[w-1][h-1]=="0")
|
|
waterCnt++;
|
|
if(map[w-1][h+1]=="0")
|
|
waterCnt++;
|
|
if(map[w+1][h-1]=="0")
|
|
waterCnt++;
|
|
if(map[w+1][h+1]=="0")
|
|
waterCnt++;
|
|
|
|
if(waterCnt > 4)
|
|
map[w][h] = "0";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<4;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];
|
|
}
|
|
}
|
|
|
|
for(var w=0;w<width;w++) {
|
|
for(var h=0;h<height;h++) {
|
|
if(orgMap[w][h]=="#" && !(w<=0 || h<=0 || w>=width-1 || h>=height-1)) {
|
|
map[w-1][h]="#"
|
|
map[w+1][h]="#"
|
|
map[w][h-1]="#"
|
|
map[w][h+1]="#"
|
|
map[w-1][h-1]="#"
|
|
map[w-1][h+1]="#"
|
|
map[w+1][h-1]="#"
|
|
map[w+1][h+1]="#"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for(i=0;i<4;i++) { //fill water holes
|
|
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)) {
|
|
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]=="0" && !(w<=0 || h<=0 || w>=width-1 || h>=height-1)) {
|
|
if(map[w+1][h]=="#") {
|
|
map[w][h] = "1";
|
|
}
|
|
if(map[w-1][h]=="#") {
|
|
map[w][h] = "2";
|
|
}
|
|
|
|
if(map[w][h-1]=="#") {
|
|
map[w][h] = "3";
|
|
}
|
|
|
|
if(map[w][h+1]=="#") {
|
|
map[w][h] = "4";
|
|
}
|
|
|
|
if(map[w][h+1]=="#" && map[w+1][h]=="#") {
|
|
map[w][h] = "5";
|
|
}
|
|
|
|
if(map[w][h-1]=="#" && map[w+1][h]=="#") {
|
|
map[w][h] = "6";
|
|
}
|
|
|
|
if(map[w][h+1]=="#" && map[w-1][h]=="#") {
|
|
map[w][h] = "7";
|
|
}
|
|
|
|
if(map[w][h-1]=="#" && map[w-1][h]=="#") {
|
|
map[w][h] = "8";
|
|
}
|
|
|
|
if(map[w-1][h-1]=="#") {
|
|
map[w][h] = "9";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return map;
|
|
}
|
|
};
|
|
|
|
function rand(p) {
|
|
if(p > Math.random()*100)
|
|
return true;
|
|
return false;
|
|
} |