dungen4
by path411
HTML
<ul>
<li>Percentage of initial cells to make live:</li>
<li>Min: <input id="mincells" type="range" min="0" max="1" step="0.01" /><span id="mincells_v"></span></li>
<li>Max: <input id="maxcells" type="range" min="0" max="1" step="0.01" /><span id="maxcells_v"></span></li>
<li>Current Seed: <input id="seed" /><input id="genseed" type="button" value="Generate Random Seed" /></li>
<li><input id="recalc" type="button" value="Re-Calculate Dungeon" /><input id="defaultsettings" type="button" value="Default Settings" /></li>
<li>Itterations Completed: <span id="count"></span></li>
</ul>
<canvas id="canvas"></canvas>
CSS
canvas {
image-rendering: crisp-edges;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
}
JavaScript
// I can't get jsfiddle to let me import an external script (yeah I fail), anyway, this is david bau's drop in seed algorithim for Math.random(). http://davidbau.com/encode/seedrandom.js
(function(a,b,c,d,e,f){function k(a){var b,c=a.length,e=this,f=0,g=e.i=e.j=0,h=e.S=[];for(c||(a=[c++]);d>f;)h[f]=f++;for(f=0;d>f;f++)h[f]=h[g=j&g+a[f%c]+(b=h[f])],h[g]=b;(e.g=function(a){for(var b,c=0,f=e.i,g=e.j,h=e.S;a--;)b=h[f=j&f+1],c=c*d+h[j&(h[f]=h[g=j&g+b])+(h[g]=b)];return e.i=f,e.j=g,c})(d)}function l(a,b){var e,c=[],d=(typeof a)[0];if(b&&"o"==d)for(e in a)if(a.hasOwnProperty(e))try{c.push(l(a[e],b-1))}catch(f){}return c.length?c:"s"==d?a:a+"\0"}function m(a,b){for(var d,c=a+"",e=0;c.length>e;)b[j&e]=j&(d^=19*b[j&e])+c.charCodeAt(e++);return o(b)}function n(c){try{return a.crypto.getRandomValues(c=new Uint8Array(d)),o(c)}catch(e){return[+new Date,a.document,a.history,a.navigator,a.screen,o(b)]}}function o(a){return String.fromCharCode.apply(0,a)}var g=c.pow(d,e),h=c.pow(2,f),i=2*h,j=d-1;c.seedrandom=function(a,f){var j=[],p=m(l(f?[a,o(b)]:0 in arguments?a:n(),3),j),q=new k(j);return m(o(q.S),b),c.random=function(){for(var a=q.g(e),b=g,c=0;h>a;)a=(a+c)*d,b*=d,c=q.g(1);for(;a>=i;)a/=2,b/=2,c>>>=1;return(a+c)/b},p},m(c.random(),b)})(this,[],Math,256,6,52);
// Implementation inspired by: http://jeremykun.com/2012/07/29/the-cellular-automaton-method-for-cave-generation/
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function Random(min, max) {
return (Math.random() * ((max - min) + 1)) + min | 0;
}
function DistanceBetween(a, b) {
return Math.sqrt(a.X - b.X + a.Y - b.Y);
}
function DrawRect(x, y, w, h) {
ctx.strokeRect(x, y, w, h);
}
function FillRect(x, y, w, h) {
ctx.fillRect(x, y, w, h);
}
function Room(x, y, w, h, parent) {
this.X = x;
this.Y = y;
this.Width = w;
this.Height = h;
this.Parent = parent || null;
}
function Cell(x, y, w, h,...