pirate/s_map.js
2017-06-25 22:40:20 +02:00

308 lines
11 KiB
JavaScript

module.exports = {
generateMap: function(width, height) {
var map = [];
var utilMap = [];
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] == "#") { //is ground
map[w][h] = "6"; //set ground
if(Math.random()<0.011) {
utilMap[w][h] = "flower";
} else if(Math.random()<0.011) {
utilMap[w][h] = "tree1";
} else if(Math.random()<0.011) {
utilMap[w][h] = "stone1";
} else if(Math.random()<0.011) {
utilMap[w][h] = "fass";
}
}
}
}
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
}
}
}
}
var cssMap = addCssClass(map);
return {"map": map, "cssMap":cssMap, "utilMap":utilMap};
}
};
function rand(p) {
if(p > Math.random()*100)
return true;
return false;
}
function addCssClass(map) {
var cssMap = [];
for(var w=0;w<map.length;w++) {
cssMap[w] = [];
for(var h=0;h<map[w].length;h++) {
var spClass = "ground_";
if(map[w][h]=="0" || (w<=0 || h<=0 || w>=map.length-1 || h>=map[w].length-1))
spClass = "water";
else if((map[w][h-1]=="0" && map[w][h]=="1" && map[w][h+1]=="2" && map[w-1][h+1]=="1") || (map[w-1][h-1]=="0" && map[w][h]=="1" && map[w+1][h]=="2" && map[w+1][h-1]=="1") || (map[w+1][h]=="2" && map[w][h]=="1" && map[w][h+1]=="2")) {
if(map[w][h+1]=="2" && map[w+1][h]=="2") {
if(map[w-1][h+1]=="1" && map[w+1][h-1]=="2")
spClass = "topDown_1";
else if(map[w+1][h-1]=="1" && map[w-1][h+1]=="2")
spClass = "leftRight_1";
else if(map[w+1][h-1]=="2" && map[w-1][h+1]=="2")
spClass = "leftRightTT_1";
else
spClass = "leftRightT_1";
}
else
spClass = "leftRightT_1";
}
else if((map[w][h+1]=="0" && map[w][h]=="1" && map[w][h-1]=="2" && map[w-1][h-1]=="1") || (map[w-1][h]=="0" && map[w][h]=="1" && map[w+1][h]=="2" && map[w+1][h+1]=="1") || (map[w+1][h]=="2" && map[w][h]=="1" && map[w][h-1]=="2")) {
if(map[w][h-1]=="2" && map[w+1][h]=="2") {
if(map[w-1][h-1]=="1" && map[w+1][h+1]=="2")
spClass = "topDown_1";
else if(map[w+1][h+1]=="1" && map[w-1][h-1]=="2")
spClass = "rightLeft_1";
else if(map[w-1][h-1]=="2" && map[w+1][h-1]=="2")
spClass = "rightLeftTT_1";
else
spClass = "rightLeftT_1";
}
else
spClass = "rightLeftT_1";
}
else if((map[w][h-1]=="0" && map[w][h]=="1" && map[w][h+1]=="2" && map[w+1][h+1]=="1") || (map[w+1][h]=="0" && map[w][h]=="1" && map[w-1][h]=="2" && map[w-1][h-1]=="1") || (map[w-1][h]=="2" && map[w][h]=="1" && map[w][h+1]=="2")) {
if(map[w][h+1]=="2" && map[w-1][h]=="2") {
if(map[w+1][h+1]=="1" && map[w-1][h-1]=="2")
spClass = "downTop_1";
else if(map[w-1][h-1]=="1" && map[w+1][h+1]=="2")
spClass = "leftRight_1";
else if(map[w-1][h-1]=="2" && map[w+1][h+1]=="2")
spClass = "rightLeftBB_1";
else
spClass = "rightLeftB_1";
}
else
spClass = "rightLeftB_1";
}
else if((map[w+1][h]=="0" && map[w][h]=="1" && map[w-1][h]=="2" && map[w-1][h+1]=="1") || (map[w][h+1]=="0" && map[w][h]=="1" && map[w][h-1]=="2" && map[w+1][h-1]=="1") || (map[w-1][h]=="2" && map[w][h]=="1" && map[w][h-1]=="2")) {
if(map[w][h-1]=="2" && map[w-1][h]=="2") {
if(map[w+1][h-1]=="1" && map[w-1][h+1]=="2")
spClass = "downTop_1";
else if(map[w-1][h+1]=="1" && map[w+1][h-1]=="2")
spClass = "rightLeft_1";
else if(map[w-1][h+1]=="2" && map[w-1][h+1]=="2")
spClass = "leftRightBB_1";
else
spClass = "leftRightB_1";
}
else
spClass = "leftRightB_1";
}
else if(map[w][h-1]=="3" && map[w][h]=="2" && map[w-1][h]=="3")
spClass = "rightLeftBU_2";
else if(map[w][h-1]=="4" && map[w][h]=="3" && map[w-1][h]=="4")
spClass = "rightLeftBU_3";
else if(map[w][h-1]=="6" && map[w][h]=="5" && map[w-1][h]=="6")
spClass = "rightLeftBU_5";
else if(map[w][h+1]=="3" && map[w][h]=="2" && map[w-1][h]=="3")
spClass = "leftRightBU_2";
else if(map[w][h+1]=="4" && map[w][h]=="3" && map[w-1][h]=="4")
spClass = "leftRightBU_3";
else if(map[w][h+1]=="6" && map[w][h]=="5" && map[w-1][h]=="6")
spClass = "leftRightBU_5";
else if(map[w][h+1]=="3" && map[w][h]=="2" && map[w+1][h]=="3")
spClass = "rightLeftTU_2";
else if(map[w][h+1]=="4" && map[w][h]=="3" && map[w+1][h]=="4")
spClass = "rightLeftTU_3";
else if(map[w][h+1]=="6" && map[w][h]=="5" && map[w+1][h]=="6")
spClass = "rightLeftTU_5";
else if(map[w][h-1]=="3" && map[w][h]=="2" && map[w+1][h]=="3")
spClass = "leftRightTU_2";
else if(map[w][h-1]=="4" && map[w][h]=="3" && map[w+1][h]=="4")
spClass = "leftRightTU_3";
else if(map[w][h-1]=="6" && map[w][h]=="5" && map[w+1][h]=="6")
spClass = "leftRightTU_5";
else if(map[w][h-1]=="0" && map[w][h]=="1" && map[w][h+1]=="2")
spClass = "leftRight_1";
else if(map[w][h-1]=="1" && map[w][h]=="2" && map[w][h+1]=="3")
spClass = "leftRight_2";
else if(map[w][h-1]=="2" && map[w][h]=="3" && map[w][h+1]=="4")
spClass = "leftRight_3";
else if(map[w][h]=="4")
spClass = "ground";
else if(map[w][h-1]=="4" && map[w][h]=="5" && map[w][h+1]=="6")
spClass = "leftRight_5";
else if(map[w][h]=="6")
spClass = "grass";
else if((map[w][h+1]=="1" && map[w][h]=="1" && map[w+1][h]=="1") || (map[w][h-1]=="1" && map[w][h]=="1" && map[w+1][h]=="1") || (map[w-1][h]=="1" && map[w][h]=="1" && map[w][h+1]=="1") || (map[w-1][h]=="1" && map[w][h]=="1" && map[w][h-1]=="1"))
spClass = "water";
else if(map[w][h+1]=="2" && map[w][h]=="2" && map[w+1][h]=="2")
spClass = "leftRightT_2";
else if(map[w][h+1]=="3" && map[w][h]=="3" && map[w+1][h]=="3")
spClass = "leftRightT_3";
else if(map[w][h+1]=="5" && map[w][h]=="5" && map[w+1][h]=="5")
spClass = "leftRightT_5";
else if(map[w-1][h]=="0" && map[w][h]=="1" && map[w+1][h]=="2")
spClass = "topDown_1";
else if(map[w-1][h]=="1" && map[w][h]=="2" && map[w+1][h]=="3")
spClass = "topDown_2";
else if(map[w-1][h]=="2" && map[w][h]=="3" && map[w+1][h]=="4")
spClass = "topDown_3";
else if(map[w-1][h]=="4" && map[w][h]=="5" && map[w+1][h]=="6")
spClass = "topDown_5";
else if(map[w][h+1]=="0" && map[w][h]=="1" && map[w][h-1]=="2")
spClass = "rightLeft_1";
else if(map[w][h+1]=="1" && map[w][h]=="2" && map[w][h-1]=="3")
spClass = "rightLeft_2";
else if(map[w][h+1]=="2" && map[w][h]=="3" && map[w][h-1]=="4")
spClass = "rightLeft_3";
else if(map[w][h+1]=="4" && map[w][h]=="5" && map[w][h-1]=="6")
spClass = "rightLeft_5";
else if(map[w][h-1]=="2" && map[w][h]=="2" && map[w+1][h]=="2")
spClass = "rightLeftT_2";
else if(map[w][h-1]=="3" && map[w][h]=="3" && map[w+1][h]=="3")
spClass = "rightLeftT_3";
else if(map[w][h-1]=="5" && map[w][h]=="5" && map[w+1][h]=="5")
spClass = "rightLeftT_5";
else if(map[w+1][h]=="0" && map[w][h]=="1" && map[w-1][h]=="2")
spClass = "downTop_1";
else if(map[w+1][h]=="1" && map[w][h]=="2" && map[w-1][h]=="3")
spClass = "downTop_2";
else if(map[w+1][h]=="2" && map[w][h]=="3" && map[w-1][h]=="4")
spClass = "downTop_3";
else if(map[w+1][h]=="4" && map[w][h]=="5" && map[w-1][h]=="6")
spClass = "downTop_5";
else if(map[w][h+1]=="2" && map[w][h]=="2" && map[w-1][h]=="2")
spClass = "leftRightB_2";
else if(map[w][h+1]=="3" && map[w][h]=="3" && map[w-1][h]=="3")
spClass = "leftRightB_3";
else if(map[w][h+1]=="5" && map[w][h]=="5" && map[w-1][h]=="5")
spClass = "leftRightB_5";
else if(map[w][h-1]=="2" && map[w][h]=="2" && map[w-1][h]=="2")
spClass = "rightLeftB_2";
else if(map[w][h-1]=="3" && map[w][h]=="3" && map[w-1][h]=="3")
spClass = "rightLeftB_3";
else if(map[w][h-1]=="5" && map[w][h]=="5" && map[w-1][h]=="5")
spClass = "rightLeftB_5";
cssMap[w][h] = spClass;
}
}
return cssMap;
}