Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Read Synchrously
var fs = require("fs");
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var map = fs.readFileSync("newworld1-1.json");
var jsonMap = JSON.parse(map);
console.log("Output Content : \n"+ jsonMap.backgroundcolor);
var MAP_WIDTH = 256;
var MAP_HEIGHT = 256;
function setSimpleLayer(layer, tilePatchMap, amount, size, erodeLayer)
{
var layerOverlap = [];
for (var j = 0; j <= MAP_WIDTH; ++j)
{
for (var k = 0; k <= MAP_HEIGHT; ++k)
{
layerOverlap[(k * MAP_HEIGHT) + j] = 0;
}
}
for(var i = 0; i < amount; ++i)
{
var TL = getRandomInt(MAP_HEIGHT) * (MAP_HEIGHT) + getRandomInt(MAP_WIDTH);
var Width = (getRandomInt(size)+1) * (tilePatchMap[0].length-2) + 2;
var Height = (getRandomInt(size)+1) * (tilePatchMap.length-2) + 1;
console.log("Width:"+Width+",Height:"+Height);
var BR = TL + Height * (MAP_HEIGHT) + Width;
var BL = BR - Width;
var TR = TL + Width;
for (var j = 0; j <= Width; ++j)
{
for (var k = 0; k <= Height; ++k)
{
layerOverlap[TL + (k * MAP_HEIGHT) + j] += 1;
//console.log("setting ");
}
}
// Do the sides.
for (var j = 1; j < Width; ++j)
{
if (layer[TL + j] == "0")
layer[TL + j] = tilePatchMap[0][ 1+(j % (tilePatchMap[0].length-2))];
if (layer[BL + j] == "0")
layer[BL + j] = tilePatchMap[tilePatchMap.length-1][ 1+(j % (tilePatchMap[0].length-2))];
}
for (var j = 1; j < Height; ++j)
{
if (layer[TR + (j*MAP_HEIGHT)] == "0")
layer[TR + (j*MAP_HEIGHT)] = tilePatchMap[ 1+((j-1) % (tilePatchMap.length-2))][tilePatchMap[0].length-1];
if (layer[TL + (j*MAP_HEIGHT)] == "0")
layer[TL + (j*MAP_HEIGHT)] = tilePatchMap[ 1+((j-1) % (tilePatchMap.length-2))][0];
console.log("x_Index="+(1+((j-1) % (tilePatchMap.length-2))));
}
// Insert the Corners
if (layer[TL] == "0")
layer[TL] = tilePatchMap[0][0];
if (layer[TR] == "0")
layer[TR] = tilePatchMap[0][tilePatchMap[0].length-1];
if (layer[BL] == "0")
layer[BL] = tilePatchMap[tilePatchMap.length-1][0];
if (layer[BR] == "0")
layer[BR] = tilePatchMap[tilePatchMap.length-1][tilePatchMap[0].length-1];
for (var j = 1; j < Width; ++j)
{
for (var k = 1; k < Height; ++k)
{
layer[TL + (k * MAP_HEIGHT) + j] = tilePatchMap[1+(k % (tilePatchMap[0].length-2))][1+(j % (tilePatchMap[0].length-2))];
if (erodeLayer && erodeLayer.length > 0)
{
for (var m = 0; m < erodeLayer.length; ++m)
erodeLayer[m][TL + (k * MAP_HEIGHT) + j] = 0;
}
}
}
}
for (var j = 0; j <= MAP_WIDTH; ++j)
{
for (var k = 0; k <= MAP_HEIGHT; ++k)
{
if (layerOverlap[(k * MAP_HEIGHT) + j] >= 2)
{
layer[(k * MAP_HEIGHT) + j] = tilePatchMap[1+(k % (tilePatchMap[0].length-2))][1+(j % (tilePatchMap[0].length-2))];
}
}
}
}
function setFixedTileObject(layer, tilePatchMap, amount)
{
TiledObjectMap = [];
for(var i = 0; i < amount; ++i)
{
var TL = getRandomInt(MAP_HEIGHT) * (MAP_HEIGHT) + getRandomInt(MAP_WIDTH);
var Width = tilePatchMap.length;
var Height = tilePatchMap.length;
var BR = TL + Height * (MAP_HEIGHT) + Width;
var BL = BR - Width;
var TR = TL + Width;
var generate = true;
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
if (layer[TL + (k * MAP_HEIGHT) + j] > 0)
{
generate = false;
break;
}
}
}
if (generate)
{
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
layer[TL + (k * MAP_HEIGHT) + j] = tilePatchMap[k][j];
}
}
TiledObjectMap[i] = TL;
}
}
return TiledObjectMap;
}
function setOnFixedTileObject(layer, tilePatchMap, amount, baseTileObject, correctionOffset, avoidLayers)
{
TiledObjectMap = [];
for(var i = 0; i < amount; ++i)
{
if (baseTileObject[i] == 0) { continue; console.log("no base"); }
var Width = tilePatchMap[0].length;
var Height = tilePatchMap.length;
var TL = baseTileObject[i] - correctionOffset.y * MAP_HEIGHT - correctionOffset.x;
var BR = TL + Height * MAP_HEIGHT + Width;
var BL = BR - Width;
var TR = TL + Width;
var generate = true;
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
if (layer[TL + (k * MAP_HEIGHT) + j] > 0)
{
generate = false;
break;
}
for (var m = 0; m < avoidLayers.length; ++m)
{
if (avoidLayers[m][TL + (k * MAP_HEIGHT) + j] != 0)
{
generate = false;
break;
}
}
}
}
if (generate)
{
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
layer[TL + (k * MAP_HEIGHT) + j] = tilePatchMap[k][j];
}
}
TiledObjectMap[i] = TL;
}
}
return TiledObjectMap;
}
function setFixedObjectOnSimpleLayer(layer, tilePatchMap, amount, baselayer, avoidLayers)
{
for(var i = 0; i < amount; ++i)
{
var Width = tilePatchMap[0].length;
var Height = tilePatchMap.length;
var TL = getRandomInt(MAP_HEIGHT) * (MAP_HEIGHT) + getRandomInt(MAP_WIDTH);
var BR = TL + Height * MAP_HEIGHT + Width;
var BL = BR - Width;
var TR = TL + Width;
var generate = true;
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
if (typeof(baselayer) == "Array")
{
for (var m = 0; m < baselayer.length; ++m)
{
if (baselayer[m][TL + (k * MAP_HEIGHT) + j] == 0)
{
generate = false;
break;
}
}
}
else
{
if (baselayer[TL + (k * MAP_HEIGHT) + j] == 0)
{
generate = false;
break;
}
}
if (layer[TL + (k * MAP_HEIGHT) + j] != 0)
{
generate = false;
break;
}
for (var m = 0; m < avoidLayers.length; ++m)
{
if (avoidLayers[m][TL + (k * MAP_HEIGHT) + j] != 0)
{
generate = false;
break;
}
}
}
}
if (generate)
{
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
layer[TL + (k * MAP_HEIGHT) + j] = tilePatchMap[k][j];
}
}
}
}
}
function setFixedObject(layer, tilePatchMap, amount, avoidLayers)
{
for(var i = 0; i < amount; ++i)
{
var Width = tilePatchMap[0].length;
var Height = tilePatchMap.length;
var TL = getRandomInt(MAP_HEIGHT) * (MAP_HEIGHT) + getRandomInt(MAP_WIDTH);
var BR = TL + Height * MAP_HEIGHT + Width;
var BL = BR - Width;
var TR = TL + Width;
var generate = true;
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
if (layer[TL + (k * MAP_HEIGHT) + j] != 0)
{
generate = false;
break;
}
for (var m = 0; m < avoidLayers.length; ++m)
{
if (avoidLayers[m][TL + (k * MAP_HEIGHT) + j] != 0)
{
generate = false;
break;
}
}
}
}
if (generate)
{
for (var j = 0; j < Width; ++j)
{
for (var k = 0; k < Height; ++k)
{
layer[TL + (k * MAP_HEIGHT) + j] = tilePatchMap[k][j];
}
}
}
}
}
function DecorateLayer(layer, baseTiles, overlayTiles, Percentchance)
{
for (var j = 0; j <= MAP_WIDTH; ++j)
{
for (var k = 0; k <= MAP_HEIGHT; ++k)
{
for (var m = 0; m < baseTiles.length; ++m)
{
if (layer[(k * MAP_HEIGHT) + j] == baseTiles[m] && getRandomInt(100) <= Percentchance)
layer[(k * MAP_HEIGHT) + j] = overlayTiles[getRandomInt(overlayTiles.length)];
}
}
}
}
var layer = {
"collision": jsonMap.layers[0].data,
"sand": jsonMap.layers[1].data,
"sand objects": jsonMap.layers[2].data,
"ground": jsonMap.layers[3].data,
"mud": jsonMap.layers[4].data,
"mudlakes": jsonMap.layers[5].data,
"grass": jsonMap.layers[6].data,
"stone": jsonMap.layers[7].data,
"water": jsonMap.layers[8].data,
"plateau": jsonMap.layers[9].data,
"houses": jsonMap.layers[10].data,
"trees": jsonMap.layers[11].data,
"bigrocks": jsonMap.layers[12].data,
"randomobjects":jsonMap.layers[13].data,
};
// Generate Dynamic Patches
var mudPatch = [
[489,490,491,492,493,494],
[509,552,553,552,553,514],
[529,572,573,572,573,534],
[549,552,553,552,553,554],
[569,572,573,572,573,574],
[589,590,591,592,593,594],
];
setSimpleLayer(layer.mud, mudPatch, 50, 18, [layer.sand]);
var grassPatch = [
[ 92, 93, 94, 95, 96, 97],
[112,153,154,155,156,117],
[132,153,154,155,156,137],
[152,153,154,155,156,157],
[172,153,154,155,156,177],
[192,193,194,195,196,197],
];
setSimpleLayer(layer.grass, grassPatch, 80, 14, [layer.sand, layer.mud]);
var lakePatch = [
[611, 612, 612, 612, 613],
[631, 406, 406, 406, 633],
[631, 406, 406, 406, 633],
[631, 406, 406, 406, 633],
[651, 652, 652, 652, 653],
];
setSimpleLayer(layer.water, lakePatch, 20, 4, [layer.sand, layer.mud, layer.grass]);
// End Generate Dynamic Patches
/// HOUSES
var cementPatch = [
[0, 374,375,376,377,378],
[0, 394,395,396,397,398],
[413,414,415,416,417,418],
[433,434,435,436,437,438],
[0, 454,455,456,457,458],
[0, 474,475,476,477,478],
];
cementPatches = setFixedTileObject(layer.stone, cementPatch, 50);
var house1Patch = [
[ 44, 45, 46, 47, 48, 49, 50, 51],
[ 64, 65, 66, 67, 68, 69, 70, 71],
[ 84, 85, 86, 87, 88, 89, 90, 91],
[104,105,106,107,108,109,110,111],
[124,125,126,127,128,129,130,131],
[144,145,146,147,148,149,150,151],
[ 0,165,166,167,168,169,170,171],
[ 0,185,186,187,188,189,190,191],
];
setOnFixedTileObject(layer.houses, house1Patch, 20, cementPatches.slice(0,20), {y:4,x:1}, [layer.water]);
// Generate Trees
var tree1Patch = [
[213, 214, 215, 216],
[233, 234, 235, 236],
[253, 254, 255, 256],
[273, 274, 275, 276],
[293, 294, 295, 296],
[ 0, 314, 315, 0]
];
setFixedObjectOnSimpleLayer(layer.trees, tree1Patch, 800, layer.grass, [layer.water, layer.houses, layer.stone]);
var deadTree1 = [
[794, 795, 796],
[814, 815, 816],
[834, 835, 836],
[854, 855, 856],
[874, 875, 876],
[ 0, 895, 0]
];
setFixedObjectOnSimpleLayer(layer.trees, deadTree1, 400, layer.sand, [layer.water, layer.houses, layer.stone]);
var deadTree2 = [
[617, 618, 619],
[637, 638, 639],
[657, 658, 659],
[677, 678, 679],
[697, 698, 699],
];
setFixedObjectOnSimpleLayer(layer.trees, deadTree2, 400, layer.mud, [layer.water, layer.houses, layer.stone]);
// End Generate Trees
// Generate Plataeus
var dungeonEntrance1 = [
[1008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1007],
[1028, 1149, 0, 0, 0, 0, 0, 0, 0, 0, 1158, 1027],
[1048, 1169, 1170, 1171, 1172, 1351, 1352, 1353, 1176, 1177, 1178, 1047],
[1068, 1189, 1190, 1191, 1192, 1371, 1372, 1373, 1196, 1197, 1198, 1067],
[ 0, 1209, 1210, 1211, 1212, 1391, 1392, 1393, 1216, 1217, 1218, 0]
];
setFixedObject(layer.plateau, dungeonEntrance1, 10, [layer.water, layer.houses, layer.trees, layer.houses, layer.stone, layer.bigrocks]);
var miniPlataeu1 = [
[1008, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1007],
[1028, 1149, 0, 0, 0, 0, 0, 0, 0, 0, 1158, 1027],
[1048, 1169, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1047],
[1068, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198, 1067],
[ 0, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 1217, 1218, 0]
];
setFixedObject(layer.plateau, miniPlataeu1, 20, [layer.water, layer.houses, layer.trees, layer.houses, layer.stone, layer.bigrocks]);
// End Generate Plataeus
// Generate Rocks
var bigRock1 = [
[757, 758, 759],
[777, 778, 779],
[797, 798, 799],
[817, 818, 819]
];
setFixedObjectOnSimpleLayer(layer.bigrocks, bigRock1, 100, [layer.sand, layer.mud], [layer.water, layer.trees, layer.houses, layer.stone]);
var bigGrassRock1 = [
[18, 19, 20],
[38, 39, 40],
[58, 59, 60],
[78, 79, 80]
];
setFixedObjectOnSimpleLayer(layer.bigrocks, bigGrassRock1, 50, layer.grass, [layer.water, layer.trees, layer.houses, layer.stone]);
var bigGrassRock2 = [
[259, 260],
[279, 280]
];
setFixedObjectOnSimpleLayer(layer.bigrocks, bigGrassRock2, 50, layer.grass, [layer.water, layer.trees, layer.houses, layer.stone]);
var bigGrassRock3 = [
[257, 258],
[277, 278]
];
setFixedObjectOnSimpleLayer(layer.bigrocks, bigGrassRock3, 50, layer.grass, [layer.water, layer.trees, layer.houses, layer.stone]);
// End Generate Rocks
// Generate Decoration Layers
var mudCenterTiles = [
552,553,572,573
];
var mudDecorateTiles = [
550, 570
];
DecorateLayer(layer.mud, mudCenterTiles, mudDecorateTiles, 10);
var grassCenterTiles = [
153,154,155,156
];
var grassDecorateTiles = [
158,159,160,606,607,608,626,627,646,647
];
DecorateLayer(layer.grass, grassCenterTiles, grassDecorateTiles, 5);
// End Generate Decoration Layers
// Generate Random Objects.
var tentTiles = [
[141,142,143],
[161,162,163],
[181,182,183],
[201,202,203],
];
setFixedObject(layer.randomobjects, tentTiles, 10, [layer.water, layer.trees, layer.houses, layer.stone]);
// End Generate Random Objects.
fs.writeFile('newworld1-2.json', JSON.stringify(jsonMap), 'utf8', function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.