pirate/s_map.js
2016-10-20 22:33:08 +02:00

164 lines
5.8 KiB
JavaScript

module.exports = {
generateMap: function(width, height) {
var map = [];
var utilMap = [];
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","0","0","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] = [];
utilMap[w] = [];
for(var h=0;h<height;h++) {
map[w][h] = "0";
utilMap[w][h] = "";
if(Math.random()<0.0006)
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 i=0;i<2;i++) {
for(var w=7;w<width-7;w++) {
for(var h=7;h<height-7;h++) {
if(map[w][h]=="0") {
if(map[w][h-1]=="#" || map[w][h-2]=="#" || map[w][h-3]=="#" || map[w][h-4]=="#" || map[w][h-5]=="#" || map[w][h-6]=="#") {
if(map[w][h+1]=="#" || map[w][h+2]=="#" || map[w][h+3]=="#" || map[w][h+4]=="#" || map[w][h+5]=="#" || map[w][h+6]=="#") {
map[w][h]="#";
}
}
if(map[w-1][h]=="#" || map[w-2][h]=="#" || map[w-3][h]=="#" || map[w-4][h]=="#" || map[w-5][h]=="#" || map[w-6][h]=="#") {
if(map[w+1][h]=="#" || map[w+2][h]=="#" || map[w+3][h]=="#" || map[w+4][h]=="#" || map[w+5][h]=="#" || map[w+6][h]=="#") {
map[w][h]="#";
}
}
if(map[w-1][h-1]=="#" || map[w-2][h-2]=="#" || map[w-3][h-3]=="#" || map[w-4][h-4]=="#" || map[w-5][h-5]=="#" || map[w-6][h-6]=="#") {
if(map[w+1][h+1]=="#" || map[w+2][h+2]=="#" || map[w+3][h+3]=="#" || map[w+4][h+4]=="#" || map[w+5][h+5]=="#" || map[w+6][h+6]=="#") {
map[w][h]="#";
}
}
if(map[w+1][h-1]=="#" || map[w+2][h-2]=="#" || map[w+3][h-3]=="#" || map[w+4][h-4]=="#" || map[w+5][h-5]=="#" || map[w+6][h-6]=="#") {
if(map[w-1][h+1]=="#" || map[w-2][h+2]=="#" || map[w-3][h+3]=="#" || map[w-4][h+4]=="#" || map[w-5][h+5]=="#" || map[w-6][h+6]=="#") {
map[w][h]="#";
}
}
}
}
}
}
for(var w=0;w<width;w++) {
for(var h=0;h<height;h++) {
if(h<=7 || w<=7 || w>width-7 || h>height-7) {
map[w][h] = "0"; //border water
}
}
}
/* 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] = "6"; //set ground
if(Math.random()<0.011) {
utilMap[w][h] = "flower";
}
}
}
}
var g = [6,5,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":map, "utilMap":utilMap};
}
};
function rand(p) {
if(p > Math.random()*100)
return true;
return false;
}