Last active
January 15, 2016 22:10
-
-
Save nemesiscodex/cd3c37fcc86f9bb3e7de to your computer and use it in GitHub Desktop.
Dungeon map algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The MIT License (MIT) | |
* Copyright (c) 2016 Julio Reyes <[email protected]> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
* x: row size | |
* y: column size | |
* rooms: ammount of rooms | |
*/ | |
var MAX_SIZE = 35, | |
MIN_SIZE = 10; | |
function generateMap(x, y, rooms) { | |
function getImmutable(rows) { | |
//require ImmutableJS | |
var ret = Immutable.List(rows); | |
for (var i = 0; i < ret.size; i++) { | |
ret = ret.set(i, Immutable.List(ret.get(i))); | |
} | |
return ret; | |
} | |
function checkCollide(rows, x1, y1, x2, y2) { | |
if (rows.length < x2 || rows[0].lenth < y2) return true; | |
for (var i = x1; i <= x2; i++) { | |
for (var j = y1; j <= y2; j++) { | |
if (rows[i] == undefined) break; | |
if (rows[i][j] !== 1) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
function createPath(rows, x1, y1, x2, y2) { | |
var found = false; | |
var pos = undefined; | |
var count = 2; | |
while (!found && count > 0) { | |
count--; | |
pos = _.random(y1 + 2, y2 - 2); | |
for (var i = x1 - 1; i > 0; i--) { | |
if (rows[i][pos] === 0) { | |
found = true; | |
break; | |
} | |
} | |
if (found) { | |
for (var i = x1 - 1; i > 0; i--) { | |
if (rows[i][pos] === 0) break; | |
rows[i][pos] = 0; | |
} | |
found = false; | |
count--; | |
} | |
pos = _.random(x1 + 2, x2 - 2); | |
for (var i = y1 - 1; i > 0; i--) { | |
if (rows[pos][i] === 0) { | |
found = true; | |
break; | |
} | |
} | |
if (found) { | |
for (var i = y1 - 1; i > 0; i--) { | |
if (rows[pos][i] === 0) break; | |
rows[pos][i] = 0; | |
} | |
found = false; | |
count--; | |
} | |
} | |
} | |
function generateRooms(rows, N, x, y) { | |
function size() { | |
return _.random(MIN_SIZE, MAX_SIZE); | |
} | |
var k = undefined, | |
l = undefined, | |
m = undefined, | |
n = undefined; | |
for (var i = 1; i < x; i++) { | |
for (var j = 1; j < y; j++) { | |
k = size(); | |
l = size(); | |
if (!checkCollide(rows, i, j, i + k + 3, j + l + 3)) { | |
for (var _m = i + 3; _m < i + k; _m++) { | |
for (var _n = j + 3; _n < j + l; _n++) { | |
rows[_m][_n] = 0; | |
} | |
} | |
createPath(rows, i + 3, j + 3, i + k - 1, j + l - 1); | |
j = j + l; | |
N--; | |
if (N <= 0) return; | |
} | |
} | |
} | |
} | |
var newMap = []; | |
for (var i = 0; i < x; i++) { | |
newMap.push([]); | |
for (var j = 0; j < y; j++) { | |
newMap[i].push(1); | |
} | |
} | |
generateRooms(newMap, rooms, x, y); | |
return getImmutable(newMap); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment