text: !function(t,i){"function"==typeof define&&define.amd?define([],i):"object"==typeof exports?module.exports=i():t.ROT=i()}(this,function(){var t={isSupported:function(){return!(!document.createElement("canvas").getContext||!Function.prototype.bind)},DEFAULT_WIDTH:80,DEFAULT_HEIGHT:25,DIRS:{4:[[0,-1],[1,0],[0,1],[-1,0]],8:[[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1]],6:[[-1,-1],[1,-1],[2,0],[1,1],[-1,1],[-2,0]]},VK_CANCEL:3,VK_HELP:6,VK_BACK_SPACE:8,VK_TAB:9,VK_CLEAR:12,VK_RETURN:13,VK_ENTER:14,VK_SHIFT:16,VK_CONTROL:17,VK_ALT:18,VK_PAUSE:19,VK_CAPS_LOCK:20,VK_ESCAPE:27,VK_SPACE:32,VK_PAGE_UP:33,VK_PAGE_DOWN:34,VK_END:35,VK_HOME:36,VK_LEFT:37,VK_UP:38,VK_RIGHT:39,VK_DOWN:40,VK_PRINTSCREEN:44,VK_INSERT:45,VK_DELETE:46,VK_0:48,VK_1:49,VK_2:50,VK_3:51,VK_4:52,VK_5:53,VK_6:54,VK_7:55,VK_8:56,VK_9:57,VK_COLON:58,VK_SEMICOLON:59,VK_LESS_THAN:60,VK_EQUALS:61,VK_GREATER_THAN:62,VK_QUESTION_MARK:63,VK_AT:64,VK_A:65,VK_B:66,VK_C:67,VK_D:68,VK_E:69,VK_F:70,VK_G:71,VK_H:72,VK_I:73,VK_J:74,VK_K:75,VK_L:76,VK_M:77,VK_N:78,VK_O:79,VK_P:80,VK_Q:81,VK_R:82,VK_S:83,VK_T:84,VK_U:85,VK_V:86,VK_W:87,VK_X:88,VK_Y:89,VK_Z:90,VK_CONTEXT_MENU:93,VK_NUMPAD0:96,VK_NUMPAD1:97,VK_NUMPAD2:98,VK_NUMPAD3:99,VK_NUMPAD4:100,VK_NUMPAD5:101,VK_NUMPAD6:102,VK_NUMPAD7:103,VK_NUMPAD8:104,VK_NUMPAD9:105,VK_MULTIPLY:106,VK_ADD:107,VK_SEPARATOR:108,VK_SUBTRACT:109,VK_DECIMAL:110,VK_DIVIDE:111,VK_F1:112,VK_F2:113,VK_F3:114,VK_F4:115,VK_F5:116,VK_F6:117,VK_F7:118,VK_F8:119,VK_F9:120,VK_F10:121,VK_F11:122,VK_F12:123,VK_F13:124,VK_F14:125,VK_F15:126,VK_F16:127,VK_F17:128,VK_F18:129,VK_F19:130,VK_F20:131,VK_F21:132,VK_F22:133,VK_F23:134,VK_F24:135,VK_NUM_LOCK:144,VK_SCROLL_LOCK:145,VK_CIRCUMFLEX:160,VK_EXCLAMATION:161,VK_DOUBLE_QUOTE:162,VK_HASH:163,VK_DOLLAR:164,VK_PERCENT:165,VK_AMPERSAND:166,VK_UNDERSCORE:167,VK_OPEN_PAREN:168,VK_CLOSE_PAREN:169,VK_ASTERISK:170,VK_PLUS:171,VK_PIPE:172,VK_HYPHEN_MINUS:173,VK_OPEN_CURLY_BRACKET:174,VK_CLOSE_CURLY_BRACKET:175,VK_TILDE:176,VK_COMMA:188,VK_PERIOD:190,VK_SLASH:191,VK_BACK_QUOTE:192,VK_OPEN_BRACKET:219,VK_BACK_SLASH:220,VK_CLOSE_BRACKET:221,VK_QUOTE:222,VK_META:224,VK_ALTGR:225,VK_WIN:91,VK_KANA:21,VK_HANGUL:21,VK_EISU:22,VK_JUNJA:23,VK_FINAL:24,VK_HANJA:25,VK_KANJI:25,VK_CONVERT:28,VK_NONCONVERT:29,VK_ACCEPT:30,VK_MODECHANGE:31,VK_SELECT:41,VK_PRINT:42,VK_EXECUTE:43,VK_SLEEP:95};return t.Text={RE_COLORS:/%([bc]){([^}]*)}/g,TYPE_TEXT:0,TYPE_NEWLINE:1,TYPE_FG:2,TYPE_BG:3,measure:function(t,i){for(var e={width:0,height:1},o=this.tokenize(t,i),r=0,n=0;n<o.length;n++){var s=o[n];switch(s.type){case this.TYPE_TEXT:r+=s.value.length;break;case this.TYPE_NEWLINE:e.height++,e.width=Math.max(e.width,r),r=0}}return e.width=Math.max(e.width,r),e},tokenize:function(i,e){var o=[],r=0;i.replace(this.RE_COLORS,function(e,n,s,a){var h=i.substring(r,a);return h.length&&o.push({type:t.Text.TYPE_TEXT,value:h}),o.push({type:"c"==n?t.Text.TYPE_FG:t.Text.TYPE_BG,value:s.trim()}),r=a+e.length,""});var n=i.substring(r);return n.length&&o.push({type:t.Text.TYPE_TEXT,value:n}),this._breakLines(o,e)},_breakLines:function(i,e){e||(e=1/0);for(var o=0,r=0,n=-1;o<i.length;)if((c=i[o]).type==t.Text.TYPE_NEWLINE&&(r=0,n=-1),c.type==t.Text.TYPE_TEXT){for(;0==r&&" "==c.value.charAt(0);)c.value=c.value.substring(1);if(-1!=(s=c.value.indexOf("\n"))){c.value=this._breakInsideToken(i,o,s,!0);for(p=c.value.split("");p.length&&" "==p[p.length-1];)p.pop();c.value=p.join("")}if(c.value.length){if(r+c.value.length>e){for(var s=-1;;){var a=c.value.indexOf(" ",s+1);if(-1==a)break;if(r+a>e)break;s=a}if(-1!=s)c.value=this._breakInsideToken(i,o,s,!0);else if(-1!=n){var h=(c=i[n]).value.lastIndexOf(" ");c.value=this._breakInsideToken(i,n,h,!0),o=n}else c.value=this._breakInsideToken(i,o,e-r,!1)}else r+=c.value.length,-1!=c.value.indexOf(" ")&&(n=o);o++}else i.splice(o,1)}else o++;i.push({type:t.Text.TYPE_NEWLINE});for(var l=null,o=0;o<i.length;o++){var c=i[o];switch(c.type){case t.Text.TYPE_TEXT:l=c;break;case t.Text.TYPE_NEWLINE:if(l){for(var p=l.value.split("");p.length&&" "==p[p.length-1];)p.pop();l.value=p.join("")}l=null}}return i.pop(),i},_breakInsideToken:function(i,e,o,r){var n={type:t.Text.TYPE_NEWLINE},s={type:t.Text.TYPE_TEXT,value:i[e].value.substring(o+(r?1:0))};return i.splice(e+1,0,n,s),i[e].value.substring(0,o)}},Array.prototype.random=Array.prototype.random||function(){return this.length?this[Math.floor(t.RNG.getUniform()*this.length)]:null},Array.prototype.randomize=Array.prototype.randomize||function(){for(var t=[],i=this.slice();i.length;){var e=i.indexOf(i.random());t.push(i.splice(e,1)[0])}return t},Number.prototype.mod=Number.prototype.mod||function(t){return(this%t+t)%t},String.prototype.capitalize=String.prototype.capitalize||function(){return this.charAt(0).toUpperCase()+this.substring(1)},String.prototype.lpad=String.prototype.lpad||function(t,i){for(var e=t||"0",o=i||2,r="";r.length<o-this.length;)r+=e;return(r=r.substring(0,o-this.length))+this},String.prototype.rpad=String.prototype.rpad||function(t,i){for(var e=t||"0",o=i||2,r="";r.length<o-this.length;)r+=e;return r=r.substring(0,o-this.length),this+r},String.format=String.format||function(t){var i=String.format.map,e=Array.prototype.slice.call(arguments,1),o=function(o,r,n,s){if("%"==t.charAt(s-1))return o.substring(1);if(!e.length)return o;var a=e[0],h=(r||n).split(","),l=h.shift(),c=i[l.toLowerCase()];if(!c)return o;var p=(a=e.shift())[c].apply(a,h),_=l.charAt(0);return _!=_.toLowerCase()&&(p=p.capitalize()),p};return t.replace(/%(?:([a-z]+)|(?:{([^}]+)}))/gi,o)},String.format.map=String.format.map||{s:"toString"},String.prototype.format=String.prototype.format||function(){var t=Array.prototype.slice.call(arguments);return t.unshift(this),String.format.apply(String,t)},Object.create||(Object.create=function(t){var i=function(){};return i.prototype=t,new i}),Function.prototype.extend=Function.prototype.extend||function(t){return this.prototype=Object.create(t.prototype),this.prototype.constructor=this,this},"undefined"!=typeof window&&(window.requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return setTimeout(t,1e3/60)},window.cancelAnimationFrame=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||window.oCancelAnimationFrame||window.msCancelAnimationFrame||function(t){return clearTimeout(t)}),t.Display=function(i){var e=document.createElement("canvas");this._context=e.getContext("2d"),this._data={},this._dirty=!1,this._options={},this._backend=null;var o={width:t.DEFAULT_WIDTH,height:t.DEFAULT_HEIGHT,transpose:!1,layout:"rect",fontSize:15,spacing:1,border:0,forceSquareRatio:!1,fontFamily:"monospace",fontStyle:"",fg:"#ccc",bg:"#000",tileWidth:32,tileHeight:32,tileMap:{},tileSet:null,tileColorize:!1,termColor:"xterm"};for(var r in i)o[r]=i[r];this.setOptions(o),this.DEBUG=this.DEBUG.bind(this),this._tick=this._tick.bind(this),requestAnimationFrame(this._tick)},t.Display.prototype.DEBUG=function(t,i,e){var o=[this._options.bg,this._options.fg];this.draw(t,i,null,null,o[e%o.length])},t.Display.prototype.clear=function(){this._data={},this._dirty=!0},t.Display.prototype.setOptions=function(i){for(var e in i)this._options[e]=i[e];if(i.width||i.height||i.fontSize||i.fontFamily||i.spacing||i.layout){i.layout&&(this._backend=new(t.Display[i.layout.capitalize()])(this._context));var o=(this._options.fontStyle?this._options.fontStyle+" ":"")+this._options.fontSize+"px "+this._options.fontFamily;this._context.font=o,this._backend.compute(this._options),this._context.font=o,this._context.textAlign="center",this._context.textBaseline="middle",this._dirty=!0}return this},t.Display.prototype.getOptions=function(){return this._options},t.Display.prototype.getContainer=function(){return this._context.canvas},t.Display.prototype.computeSize=function(t,i){return this._backend.computeSize(t,i,this._options)},t.Display.prototype.computeFontSize=function(t,i){return this._backend.computeFontSize(t,i,this._options)},t.Display.prototype.eventToPosition=function(t){if(t.touches)var i=t.touches[0].clientX,e=t.touches[0].clientY;else var i=t.clientX,e=t.clientY;var o=this._context.canvas.getBoundingClientRect();return i-=o.left,e-=o.top,i*=this._context.canvas.width/this._context.canvas.clientWidth,e*=this._context.canvas.height/this._context.canvas.clientHeight,i<0||e<0||i>=this._context.canvas.width||e>=this._context.canvas.height?[-1,-1]:this._backend.eventToPosition(i,e)},t.Display.prototype.draw=function(t,i,e,o,r){o||(o=this._options.fg),r||(r=this._options.bg),this._data[t+","+i]=[t,i,e,o,r],!0!==this._dirty&&(this._dirty||(this._dirty={}),this._dirty[t+","+i]=!0)},t.Display.prototype.drawText=function(i,e,o,r){var n=null,s=null,a=i,h=e,l=1;r||(r=this._options.width-i);for(var c=t.Text.tokenize(o,r);c.length;){var p=c.shift();switch(p.type){case t.Text.TYPE_TEXT:for(var _=!1,u=!1,f=!1,d=!1,g=0;g<p.value.length;g++){var m=p.value.charCodeAt(g),v=p.value.charAt(g);f=m>65280&&m<65377||m>65500&&m<65512||m>65518,_=32==v.charCodeAt(0)||12288==v.charCodeAt(0),!d||f||_||a++,f&&!u&&a++,this.draw(a++,h,v,n,s),u=_,d=f}break;case t.Text.TYPE_FG:n=p.value||null;break;case t.Text.TYPE_BG:s=p.value||null;break;case t.Text.TYPE_NEWLINE:a=i,h++,l++}}return l},t.Display.prototype._tick=function(){if(requestAnimationFrame(this._tick),this._dirty){if(!0===this._dirty){this._context.fillStyle=this._options.bg,this._context.fillRect(0,0,this._context.canvas.width,this._context.canvas.height);for(var t in this._data)this._draw(t,!1)}else for(var i in this._dirty)this._draw(i,!0);this._dirty=!1}},t.Display.prototype._draw=function(t,i){var e=this._data[t];e[4]!=this._options.bg&&(i=!0),this._backend.draw(e,i)},t.Display.Backend=function(t){this._context=t},t.Display.Backend.prototype.compute=function(t){},t.Display.Backend.prototype.draw=function(t,i){},t.Display.Backend.prototype.computeSize=function(t,i){},t.Display.Backend.prototype.computeFontSize=function(t,i){},t.Display.Backend.prototype.eventToPosition=function(t,i){},t.Display.Rect=function(i){t.Display.Backend.call(this,i),this._spacingX=0,this._spacingY=0,this._canvasCache={},this._options={}},t.Display.Rect.extend(t.Display.Backend),t.Display.Rect.cache=!1,t.Display.Rect.prototype.compute=function(t){this._canvasCache={},this._options=t;var i=Math.ceil(this._context.measureText("W").width);this._spacingX=Math.ceil(t.spacing*i),this._spacingY=Math.ceil(t.spacing*t.fontSize),this._options.forceSquareRatio&&(this._spacingX=this._spacingY=Math.max(this._spacingX,this._spacingY)),this._context.canvas.width=t.width*this._spacingX,this._context.canvas.height=t.height*this._spacingY},t.Display.Rect.prototype.draw=function(t,i){this.constructor.cache?this._drawWithCache(t,i):this._drawNoCache(t,i)},t.Display.Rect.prototype._drawWithCache=function(t,i){var e=t[0],o=t[1],r=t[2],n=t[3],s=t[4],a=""+r+n+s;if(a in this._canvasCache)var h=this._canvasCache[a];else{var l=this._options.border,c=(h=document.createElement("canvas")).getContext("2d");if(h.width=this._spacingX,h.height=this._spacingY,c.fillStyle=s,c.fillRect(l,l,h.width-l,h.height-l),r){c.fillStyle=n,c.font=this._context.font,c.textAlign="center",c.textBaseline="middle";for(var p=[].concat(r),_=0;_<p.length;_++)c.fillText(p[_],this._spacingX/2,Math.ceil(this._spacingY/2))}this._canvasCache[a]=h}this._context.drawImage(h,e*this._spacingX,o*this._spacingY)},t.Display.Rect.prototype._drawNoCache=function(t,i){var e=t[0],o=t[1],r=t[2],n=t[3],s=t[4];if(i){var a=this._options.border;this._context.fillStyle=s,this._context.fillRect(e*this._spacingX+a,o*this._spacingY+a,this._spacingX-a,this._spacingY-a)}if(r){this._context.fillStyle=n;for(var h=[].concat(r),l=0;l<h.length;l++)this._context.fillText(h[l],(e+.5)*this._spacingX,Math.ceil((o+.5)*this._spacingY))}},t.Display.Rect.prototype.computeSize=function(t,i){return[Math.floor(t/this._spacingX),Math.floor(i/this._spacingY)]},t.Display.Rect.prototype.computeFontSize=function(t,i){var e=Math.floor(t/this._options.width),o=Math.floor(i/this._options.height),r=this._context.font;this._context.font="100px "+this._options.fontFamily;var n=Math.ceil(this._context.measureText("W").width);this._context.font=r;var s=n/100*o/e;return s>1&&(o=Math.floor(o/s)),Math.floor(o/this._options.spacing)},t.Display.Rect.prototype.eventToPosition=function(t,i){return[Math.floor(t/this._spacingX),Math.floor(i/this._spacingY)]},t.Display.Hex=function(i){t.Display.Backend.call(this,i),this._spacingX=0,this._spacingY=0,this._hexSize=0,this._options={}},t.Display.Hex.extend(t.Display.Backend),t.Display.Hex.prototype.compute=function(t){this._options=t;var i=Math.ceil(this._context.measureText("W").width);if(this._hexSize=Math.floor(t.spacing*(t.fontSize+i/Math.sqrt(3))/2),this._spacingX=this._hexSize*Math.sqrt(3)/2,this._spacingY=1.5*this._hexSize,t.transpose)var e="height",o="width";else var e="width",o="height";this._context.canvas[e]=Math.ceil((t.width+1)*this._spacingX),this._context.canvas[o]=Math.ceil((t.height-1)*this._spacingY+2*this._hexSize)},t.Display.Hex.prototype.draw=function(t,i){var e=t[0],o=t[1],r=t[2],n=t[3],s=t[4],a=[(e+1)*this._spacingX,o*this._spacingY+this._hexSize];if(this._options.transpose&&a.reverse(),i&&(this._context.fillStyle=s,this._fill(a[0],a[1])),r){this._context.fillStyle=n;for(var h=[].concat(r),l=0;l<h.length;l++)this._context.fillText(h[l],a[0],Math.ceil(a[1]))}},t.Display.Hex.prototype.computeSize=function(t,i){return this._options.transpose&&(t+=i,t-=i=t-i),[Math.floor(t/this._spacingX)-1,Math.floor((i-2*this._hexSize)/this._spacingY+1)]},t.Display.Hex.prototype.computeFontSize=function(t,i){this._options.transpose&&(t+=i,t-=i=t-i);var e=2*t/((this._options.width+1)*Math.sqrt(3))-1,o=i/(2+1.5*(this._options.height-1)),r=Math.min(e,o),n=this._context.font;this._context.font="100px "+this._options.fontFamily;var s=Math.ceil(this._context.measureText("W").width);this._context.font=n;var a=s/100,h=2*(r=Math.floor(r)+1)/(this._options.spacing*(1+a/Math.sqrt(3)));return Math.ceil(h)-1},t.Display.Hex.prototype.eventToPosition=function(t,i){if(this._options.transpose){t+=i,t-=i=t-i;e=this._context.canvas.width}else var e=this._context.canvas.height;var o=e/this._options.height;return i=Math.floor(i/o),i.mod(2)?(t-=this._spacingX,t=1+2*Math.floor(t/(2*this._spacingX))):t=2*Math.floor(t/(2*this._spacingX)),[t,i]},t.Display.Hex.prototype._fill=function(t,i){var e=this._hexSize,o=this._options.border;this._context.beginPath(),this._options.transpose?(this._context.moveTo(t-e+o,i),this._context.lineTo(t-e/2+o,i+this._spacingX-o),this._context.lineTo(t+e/2-o,i+this._spacingX-o),this._context.lineTo(t+e-o,i),this._context.lineTo(t+e/2-o,i-this._spacingX+o),this._context.lineTo(t-e/2+o,i-this._spacingX+o),this._context.lineTo(t-e+o,i)):(this._context.moveTo(t,i-e+o),this._context.lineTo(t+this._spacingX-o,i-e/2+o),this._context.lineTo(t+this._spacingX-o,i+e/2-o),this._context.lineTo(t,i+e-o),this._context.lineTo(t-this._spacingX+o,i+e/2-o),this._context.lineTo(t-this._spacingX+o,i-e/2+o),this._context.lineTo(t,i-e+o)),this._context.fill()},t.Display.Tile=function(i){t.Display.Rect.call(this,i),this._options={},this._colorCanvas=document.createElement("canvas")},t.Display.Tile.extend(t.Display.Rect),t.Display.Tile.prototype.compute=function(t){this._options=t,this._context.canvas.width=t.width*t.tileWidth,this._context.canvas.height=t.height*t.tileHeight,this._colorCanvas.width=t.tileWidth,this._colorCanvas.height=t.tileHeight},t.Display.Tile.prototype.draw=function(t,i){var e=t[0],o=t[1],r=t[2],n=t[3],s=t[4],a=this._options.tileWidth,h=this._options.tileHeight;if(i&&(this._options.tileColorize?this._context.clearRect(e*a,o*h,a,h):(this._context.fillStyle=s,this._context.fillRect(e*a,o*h,a,h))),r)for(var l=[].concat(r),c=0;c<l.length;c++){var p=this._options.tileMap[l[c]];if(!p)throw new Error("Char '"+l[c]+"' not found in tileMap");if(this._options.tileColorize){var _=this._colorCanvas,u=_.getContext("2d");u.clearRect(0,0,a,h),u.drawImage(this._options.tileSet,p[0],p[1],a,h,0,0,a,h),"transparent"!=n&&(u.fillStyle=n,u.globalCompositeOperation="source-atop",u.fillRect(0,0,a,h)),"transparent"!=s&&(u.fillStyle=s,u.globalCompositeOperation="destination-over",u.fillRect(0,0,a,h)),this._context.drawImage(_,e*a,o*h,a,h)}else this._context.drawImage(this._options.tileSet,p[0],p[1],a,h,e*a,o*h,a,h)}},t.Display.Tile.prototype.computeSize=function(t,i){return[Math.floor(t/this._options.tileWidth),Math.floor(i/this._options.tileHeight)]},t.Display.Tile.prototype.computeFontSize=function(t,i){return[Math.floor(t/this._options.width),Math.floor(i/this._options.height)]},t.Display.Tile.prototype.eventToPosition=function(t,i){return[Math.floor(t/this._options.tileWidth),Math.floor(i/this._options.tileHeight)]},t.RNG={getSeed:function(){return this._seed},setSeed:function(t){return t=t<1?1/t:t,this._seed=t,this._s0=(t>>>0)*this._frac,t=69069*t+1>>>0,this._s1=t*this._frac,t=69069*t+1>>>0,this._s2=t*this._frac,this._c=1,this},getUniform:function(){var t=2091639*this._s0+this._c*this._frac;return this._s0=this._s1,this._s1=this._s2,this._c=0|t,this._s2=t-this._c,this._s2},getUniformInt:function(t,i){var e=Math.max(t,i),o=Math.min(t,i);return Math.floor(this.getUniform()*(e-o+1))+o},getNormal:function(t,i){do{var e=2*this.getUniform()-1,o=2*this.getUniform()-1,r=e*e+o*o}while(r>1||0==r);var n=e*Math.sqrt(-2*Math.log(r)/r);return(t||0)+n*(i||1)},getPercentage:function(){return 1+Math.floor(100*this.getUniform())},getWeightedValue:function(t){var i=0;for(var e in t)i+=t[e];var o=this.getUniform()*i,r=0;for(var e in t)if(r+=t[e],o<r)return e;return e},getState:function(){return[this._s0,this._s1,this._s2,this._c]},setState:function(t){return this._s0=t[0],this._s1=t[1],this._s2=t[2],this._c=t[3],this},clone:function(){var t=Object.create(this);return t.setState(this.getState()),t},_s0:0,_s1:0,_s2:0,_c:0,_frac:2.3283064365386963e-10},t.RNG.setSeed(Date.now()),t.StringGenerator=function(t){this._options={words:!1,order:3,prior:.001};for(var i in t)this._options[i]=t[i];this._boundary=String.fromCharCode(0),this._suffix=this._boundary,this._prefix=[];for(var e=0;e<this._options.order;e++)this._prefix.push(this._boundary);this._priorValues={},this._priorValues[this._boundary]=this._options.prior,this._data={}},t.StringGenerator.prototype.clear=function(){this._data={},this._priorValues={}},t.StringGenerator.prototype.generate=function(){for(var t=[this._sample(this._prefix)];t[t.length-1]!=this._boundary;)t.push(this._sample(t));return this._join(t.slice(0,-1))},t.StringGenerator.prototype.observe=function(t){for(var i=this._split(t),e=0;e<i.length;e++)this._priorValues[i[e]]=this._options.prior;i=this._prefix.concat(i).concat(this._suffix);for(e=this._options.order;e<i.length;e++)for(var o=i.slice(e-this._options.order,e),r=i[e],n=0;n<o.length;n++){var s=o.slice(n);this._observeEvent(s,r)}},t.StringGenerator.prototype.getStats=function(){var t=[],i=0;for(var e in this._priorValues)i++;i--,t.push("distinct samples: "+i);var o=0,r=0;for(var e in this._data){o++;for(var n in this._data[e])r++}return t.push("dictionary size (contexts): "+o),t.push("dictionary size (events): "+r),t.join(", ")},t.StringGenerator.prototype._split=function(t){return t.split(this._options.words?/\s+/:"")},t.StringGenerator.prototype._join=function(t){return t.join(this._options.words?" ":"")},t.StringGenerator.prototype._observeEvent=function(t,i){var e=this._join(t);e in this._data||(this._data[e]={});var o=this._data[e];i in o||(o[i]=0),o[i]++},t.StringGenerator.prototype._sample=function(i){i=this._backoff(i);var e=this._join(i),o=this._data[e],r={};if(this._options.prior){for(var n in this._priorValues)r[n]=this._priorValues[n];for(var n in o)r[n]+=o[n]}else r=o;return t.RNG.getWeightedValue(r)},t.StringGenerator.prototype._backoff=function(t){for(t.length>this._options.order?t=t.slice(-this._options.order):t.length<this._options.order&&(t=this._prefix.slice(0,this._options.order-t.length).concat(t));!(this._join(t)in this._data)&&t.length>0;)t=t.slice(1);return t},t.EventQueue=function(){this._time=0,this._events=[],this._eventTimes=[]},t.EventQueue.prototype.getTime=function(){return this._time},t.EventQueue.prototype.clear=function(){return this._events=[],this._eventTimes=[],this},t.EventQueue.prototype.add=function(t,i){for(var e=this._events.length,o=0;o<this._eventTimes.length;o++)if(this._eventTimes[o]>i){e=o;break}this._events.splice(e,0,t),this._eventTimes.splice(e,0,i)},t.EventQueue.prototype.get=function(){if(!this._events.length)return null;var t=this._eventTimes.splice(0,1)[0];if(t>0){this._time+=t;for(var i=0;i<this._eventTimes.length;i++)this._eventTimes[i]-=t}return this._events.splice(0,1)[0]},t.EventQueue.prototype.getEventTime=function(t){var i=this._events.indexOf(t);if(-1!=i)return this._eventTimes[i]},t.EventQueue.prototype.remove=function(t){var i=this._events.indexOf(t);return-1!=i&&(this._remove(i),!0)},t.EventQueue.prototype._remove=function(t){this._events.splice(t,1),this._eventTimes.splice(t,1)},t.Scheduler=function(){this._queue=new t.EventQueue,this._repeat=[],this._current=null},t.Scheduler.prototype.getTime=function(){return this._queue.getTime()},t.Scheduler.prototype.add=function(t,i){return i&&this._repeat.push(t),this},t.Scheduler.prototype.getTimeOf=function(t){return this._queue.getEventTime(t)},t.Scheduler.prototype.clear=function(){return this._queue.clear(),this._repeat=[],this._current=null,this},t.Scheduler.prototype.remove=function(t){var i=this._queue.remove(t),e=this._repeat.indexOf(t);return-1!=e&&this._repeat.splice(e,1),this._current==t&&(this._current=null),i},t.Scheduler.prototype.next=function(){return this._current=this._queue.get(),this._current},t.Scheduler.Simple=function(){t.Scheduler.call(this)},t.Scheduler.Simple.extend(t.Scheduler),t.Scheduler.Simple.prototype.add=function(i,e){return this._queue.add(i,0),t.Scheduler.prototype.add.call(this,i,e)},t.Scheduler.Simple.prototype.next=function(){return this._current&&-1!=this._repeat.indexOf(this._current)&&this._queue.add(this._current,0),t.Scheduler.prototype.next.call(this)},t.Scheduler.Speed=function(){t.Scheduler.call(this)},t.Scheduler.Speed.extend(t.Scheduler),t.Scheduler.Speed.prototype.add=function(i,e,o){return this._queue.add(i,void 0!==o?o:1/i.getSpeed()),t.Scheduler.prototype.add.call(this,i,e)},t.Scheduler.Speed.prototype.next=function(){return this._current&&-1!=this._repeat.indexOf(this._current)&&this._queue.add(this._current,1/this._current.getSpeed()),t.Scheduler.prototype.next.call(this)},t.Scheduler.Action=function(){t.Scheduler.call(this),this._defaultDuration=1,this._duration=this._defaultDuration},t.Scheduler.Action.extend(t.Scheduler),t.Scheduler.Action.prototype.add=function(i,e,o){return this._queue.add(i,o||this._defaultDuration),t.Scheduler.prototype.add.call(this,i,e)},t.Scheduler.Action.prototype.clear=function(){return this._duration=this._defaultDuration,t.Scheduler.prototype.clear.call(this)},t.Scheduler.Action.prototype.remove=function(i){return i==this._current&&(this._duration=this._defaultDuration),t.Scheduler.prototype.remove.call(this,i)},t.Scheduler.Action.prototype.next=function(){return this._current&&-1!=this._repeat.indexOf(this._current)&&(this._queue.add(this._current,this._duration||this._defaultDuration),this._duration=this._defaultDuration),t.Scheduler.prototype.next.call(this)},t.Scheduler.Action.prototype.setDuration=function(t){return this._current&&(this._duration=t),this},t.Engine=function(t){this._scheduler=t,this._lock=1},t.Engine.prototype.start=function(){return this.unlock()},t.Engine.prototype.lock=function(){return this._lock++,this},t.Engine.prototype.unlock=function(){if(!this._lock)throw new Error("Cannot unlock unlocked engine");for(this._lock--;!this._lock;){var t=this._scheduler.next();if(!t)return this.lock();var i=t.act();i&&i.then&&(this.lock(),i.then(this.unlock.bind(this)))}return this},t.Map=function(i,e){this._width=i||t.DEFAULT_WIDTH,this._height=e||t.DEFAULT_HEIGHT},t.Map.prototype.create=function(t){},t.Map.prototype._fillMap=function(t){for(var i=[],e=0;e<this._width;e++){i.push([]);for(var o=0;o<this._height;o++)i[e].push(t)}return i},t.Map.Arena=function(i,e){t.Map.call(this,i,e)},t.Map.Arena.extend(t.Map),t.Map.Arena.prototype.create=function(t){for(var i=this._width-1,e=this._height-1,o=0;o<=i;o++)for(var r=0;r<=e;r++)t(o,r,o&&r&&o<i&&r<e?0:1);return this},t.Map.DividedMaze=function(i,e){t.Map.call(this,i,e),this._stack=[]},t.Map.DividedMaze.extend(t.Map),t.Map.DividedMaze.prototype.create=function(t){var i=this._width,e=this._height;this._map=[];for(r=0;r<i;r++){this._map.push([]);for(n=0;n<e;n++){var o=0==r||0==n||r+1==i||n+1==e;this._map[r].push(o?1:0)}}this._stack=[[1,1,i-2,e-2]],this._process();for(var r=0;r<i;r++)for(var n=0;n<e;n++)t(r,n,this._map[r][n]);return this._map=null,this},t.Map.DividedMaze.prototype._process=function(){for(;this._stack.length;){var t=this._stack.shift();this._partitionRoom(t)}},t.Map.DividedMaze.prototype._partitionRoom=function(t){for(var i=[],e=[],o=t[0]+1;o<t[2];o++){var r=this._map[o][t[1]-1],n=this._map[o][t[3]+1];!r||!n||o%2||i.push(o)}for(_=t[1]+1;_<t[3];_++){var s=this._map[t[0]-1][_],a=this._map[t[2]+1][_];!s||!a||_%2||e.push(_)}if(i.length&&e.length){var h=i.random(),l=e.random();this._map[h][l]=1;var c=[],p=[];c.push(p);for(o=t[0];o<h;o++)this._map[o][l]=1,p.push([o,l]);p=[];c.push(p);for(o=h+1;o<=t[2];o++)this._map[o][l]=1,p.push([o,l]);p=[];c.push(p);for(_=t[1];_<l;_++)this._map[h][_]=1,p.push([h,_]);p=[];c.push(p);for(var _=l+1;_<=t[3];_++)this._map[h][_]=1,p.push([h,_]);for(var u=c.random(),o=0;o<c.length;o++)if((p=c[o])!=u){var f=p.random();this._map[f[0]][f[1]]=0}this._stack.push([t[0],t[1],h-1,l-1]),this._stack.push([h+1,t[1],t[2],l-1]),this._stack.push([t[0],l+1,h-1,t[3]]),this._stack.push([h+1,l+1,t[2],t[3]])}},t.Map.IceyMaze=function(i,e,o){t.Map.call(this,i,e),this._regularity=o||0},t.Map.IceyMaze.extend(t.Map),t.Map.IceyMaze.prototype.create=function(i){var e=this._width,o=this._height,r=this._fillMap(1);e-=e%2?1:2,o-=o%2?1:2;var n=0,s=0,a=0,h=0,l=0,c=!1,p=[[0,0],[0,0],[0,0],[0,0]];do{if(n=1+2*Math.floor(t.RNG.getUniform()*(e-1)/2),s=1+2*Math.floor(t.RNG.getUniform()*(o-1)/2),l||(r[n][s]=0),!r[n][s]){this._randomize(p);do{0==Math.floor(t.RNG.getUniform()*(this._regularity+1))&&this._randomize(p),c=!0;for(_=0;_<4;_++)if(a=n+2*p[_][0],h=s+2*p[_][1],this._isFree(r,a,h,e,o)){r[a][h]=0,r[n+p[_][0]][s+p[_][1]]=0,n=a,s=h,c=!1,l++;break}}while(!c)}}while(l+1<e*o/4);for(var _=0;_<this._width;_++)for(var u=0;u<this._height;u++)i(_,u,r[_][u]);return this._map=null,this},t.Map.IceyMaze.prototype._randomize=function(i){for(var e=0;e<4;e++)i[e][0]=0,i[e][1]=0;switch(Math.floor(4*t.RNG.getUniform())){case 0:i[0][0]=-1,i[1][0]=1,i[2][1]=-1,i[3][1]=1;break;case 1:i[3][0]=-1,i[2][0]=1,i[1][1]=-1,i[0][1]=1;break;case 2:i[2][0]=-1,i[3][0]=1,i[0][1]=-1,i[1][1]=1;break;case 3:i[1][0]=-1,i[0][0]=1,i[3][1]=-1,i[2][1]=1}},t.Map.IceyMaze.prototype._isFree=function(t,i,e,o,r){return!(i<1||e<1||i>=o||e>=r)&&t[i][e]},t.Map.EllerMaze=function(i,e){t.Map.call(this,i,e)},t.Map.EllerMaze.extend(t.Map),t.Map.EllerMaze.prototype.create=function(i){for(var e=this._fillMap(1),o=Math.ceil((this._width-2)/2),r=[],n=[],s=0;s<o;s++)r.push(s),n.push(s);r.push(o-1);for(l=1;l+3<this._height;l+=2)for(s=0;s<o;s++){h=l;e[a=2*s+1][h]=0,s!=r[s+1]&&t.RNG.getUniform()>.375&&(this._addToList(s,r,n),e[a+1][h]=0),s!=r[s]&&t.RNG.getUniform()>.375?this._removeFromList(s,r,n):e[a][h+1]=0}for(s=0;s<o;s++){var a=2*s+1,h=l;e[a][h]=0,s!=r[s+1]&&(s==r[s]||t.RNG.getUniform()>.375)&&(this._addToList(s,r,n),e[a+1][h]=0),this._removeFromList(s,r,n)}for(s=0;s<this._width;s++)for(var l=0;l<this._height;l++)i(s,l,e[s][l]);return this},t.Map.EllerMaze.prototype._removeFromList=function(t,i,e){e[i[t]]=e[t],i[e[t]]=i[t],e[t]=t,i[t]=t},t.Map.EllerMaze.prototype._addToList=function(t,i,e){e[i[t+1]]=e[t],i[e[t]]=i[t+1],e[t]=t+1,i[t+1]=t},t.Map.Cellular=function(i,e,o){t.Map.call(this,i,e),this._options={born:[5,6,7,8],survive:[4,5,6,7,8],topology:8},this.setOptions(o),this._dirs=t.DIRS[this._options.topology],this._map=this._fillMap(0)},t.Map.Cellular.extend(t.Map),t.Map.Cellular.prototype.randomize=function(i){for(var e=0;e<this._width;e++)for(var o=0;o<this._height;o++)this._map[e][o]=t.RNG.getUniform()<i?1:0;return this},t.Map.Cellular.prototype.setOptions=function(t){for(var i in t)this._options[i]=t[i]},t.Map.Cellular.prototype.set=function(t,i,e){this._map[t][i]=e},t.Map.Cellular.prototype.create=function(t){for(var i=this._fillMap(0),e=this._options.born,o=this._options.survive,r=0;r<this._height;r++){var n=1,s=0;6==this._options.topology&&(n=2,s=r%2);for(var a=s;a<this._width;a+=n){var h=this._map[a][r],l=this._getNeighbors(a,r);h&&-1!=o.indexOf(l)?i[a][r]=1:h||-1==e.indexOf(l)||(i[a][r]=1)}}this._map=i,this.serviceCallback(t)},t.Map.Cellular.prototype.serviceCallback=function(t){if(t)for(var i=0;i<this._height;i++){var e=1,o=0;6==this._options.topology&&(e=2,o=i%2);for(var r=o;r<this._width;r+=e)t(r,i,this._map[r][i])}},t.Map.Cellular.prototype._getNeighbors=function(t,i){for(var e=0,o=0;o<this._dirs.length;o++){var r=this._dirs[o],n=t+r[0],s=i+r[1];n<0||n>=this._width||s<0||s>=this._width||(e+=1==this._map[n][s]?1:0)}return e},t.Map.Cellular.prototype.connect=function(i,e,o){e||(e=0);for(var r=[],n={},s=0;s<this._width;s++)for(var a=0;a<this._height;a++)if(this._freeSpace(s,a,e)){var h=[s,a];n[this._pointKey(h)]=h,r.push([s,a])}var l=r[t.RNG.getUniformInt(0,r.length-1)],c=this._pointKey(l),p={};for(p[c]=l,delete n[c],this._findConnected(p,n,[l],!1,e);Object.keys(n).length>0;){var _=(h=this._getFromTo(p,n))[0],u=h[1],f={};f[this._pointKey(_)]=_,this._findConnected(f,n,[_],!0,e),this._tunnelToConnected(u,_,p,n,e,o);for(var d in f){var g=f[d];this._map[g[0]][g[1]]=e,p[d]=g,delete n[d]}}this.serviceCallback(i)},t.Map.Cellular.prototype._getFromTo=function(i,e){for(var o,r,n=Object.keys(i),s=Object.keys(e),a=0;a<5;a++){if(n.length<s.length)r=i[(h=n)[t.RNG.getUniformInt(0,h.length-1)]],o=this._getClosest(r,e);else{var h=s;o=e[h[t.RNG.getUniformInt(0,h.length-1)]],r=this._getClosest(o,i)}if((o[0]-r[0])*(o[0]-r[0])+(o[1]-r[1])*(o[1]-r[1])<64)break}return[o,r]},t.Map.Cellular.prototype._getClosest=function(t,i){var e=null,o=null;for(k in i){var r=i[k],n=(r[0]-t[0])*(r[0]-t[0])+(r[1]-t[1])*(r[1]-t[1]);(null==o||n<o)&&(o=n,e=r)}return e},t.Map.Cellular.prototype._findConnected=function(t,i,e,o,r){for(;e.length>0;)for(var n=e.splice(0,1)[0],s=[[n[0]+1,n[1]],[n[0]-1,n[1]],[n[0],n[1]+1],[n[0],n[1]-1]],a=0;a<s.length;a++){var h=this._pointKey(s[a]);null==t[h]&&this._freeSpace(s[a][0],s[a][1],r)&&(t[h]=s[a],o||delete i[h],e.push(s[a]))}},t.Map.Cellular.prototype._tunnelToConnected=function(t,i,e,o,r,n){var s,a;this._pointKey(i);i[0]<t[0]?(s=i,a=t):(s=t,a=i);for(var h=s[0];h<=a[0];h++){this._map[h][s[1]]=r;p=[h,s[1]];e[_=this._pointKey(p)]=p,delete o[_]}n&&s[0]<a[0]&&n(s,[a[0],s[1]]);var l=a[0];i[1]<t[1]?(s=i,a=t):(s=t,a=i);for(var c=s[1];c<a[1];c++){this._map[l][c]=r;var p=[l,c],_=this._pointKey(p);e[_]=p,delete o[_]}n&&s[1]<a[1]&&n([a[0],s[1]],[a[0],a[1]])},t.Map.Cellular.prototype._freeSpace=function(t,i,e){return t>=0&&t<this._width&&i>=0&&i<this._height&&this._map[t][i]==e},t.Map.Cellular.prototype._pointKey=function(t){return t[0]+"."+t[1]},t.Map.Dungeon=function(i,e){t.Map.call(this,i,e),this._rooms=[],this._corridors=[]},t.Map.Dungeon.extend(t.Map),t.Map.Dungeon.prototype.getRooms=function(){return this._rooms},t.Map.Dungeon.prototype.getCorridors=function(){return this._corridors},t.Map.Digger=function(i,e,o){t.Map.Dungeon.call(this,i,e),this._options={roomWidth:[3,9],roomHeight:[3,5],corridorLength:[3,10],dugPercentage:.2,timeLimit:1e3};for(var r in o)this._options[r]=o[r];this._features={Room:4,Corridor:4},this._featureAttempts=20,this._walls={},this._digCallback=this._digCallback.bind(this),this._canBeDugCallback=this._canBeDugCallback.bind(this),this._isWallCallback=this._isWallCallback.bind(this),this._priorityWallCallback=this._priorityWallCallback.bind(this)},t.Map.Digger.extend(t.Map.Dungeon),t.Map.Digger.prototype.create=function(t){this._rooms=[],this._corridors=[],this._map=this._fillMap(1),this._walls={},this._dug=0;var i=(this._width-2)*(this._height-2);this._firstRoom();var e=Date.now();do{if(Date.now()-e>this._options.timeLimit)break;var o=this._findWall();if(!o)break;var r=o.split(","),n=parseInt(r[0]),s=parseInt(r[1]),a=this._getDiggingDirection(n,s);if(a){var h=0;do{if(h++,this._tryFeature(n,s,a[0],a[1])){this._removeSurroundingWalls(n,s),this._removeSurroundingWalls(n-a[0],s-a[1]);break}}while(h<this._featureAttempts);var l=0;for(var c in this._walls)this._walls[c]>1&&l++}}while(this._dug/i<this._options.dugPercentage||l);if(this._addDoors(),t)for(var p=0;p<this._width;p++)for(var _=0;_<this._height;_++)t(p,_,this._map[p][_]);return this._walls={},this._map=null,this},t.Map.Digger.prototype._digCallback=function(t,i,e){0==e||2==e?(this._map[t][i]=0,this._dug++):this._walls[t+","+i]=1},t.Map.Digger.prototype._isWallCallback=function(t,i){return!(t<0||i<0||t>=this._width||i>=this._height)&&1==this._map[t][i]},t.Map.Digger.prototype._canBeDugCallback=function(t,i){return!(t<1||i<1||t+1>=this._width||i+1>=this._height)&&1==this._map[t][i]},t.Map.Digger.prototype._priorityWallCallback=function(t,i){this._walls[t+","+i]=2},t.Map.Digger.prototype._firstRoom=function(){var i=Math.floor(this._width/2),e=Math.floor(this._height/2),o=t.Map.Feature.Room.createRandomCenter(i,e,this._options);this._rooms.push(o),o.create(this._digCallback)},t.Map.Digger.prototype._findWall=function(){var t=[],i=[];for(var e in this._walls)2==this._walls[e]?i.push(e):t.push(e);var o=i.length?i:t;if(!o.length)return null;e=o.random();return delete this._walls[e],e},t.Map.Digger.prototype._tryFeature=function(i,e,o,r){var n=t.RNG.getWeightedValue(this._features);return!!(n=t.Map.Feature[n].createRandomAt(i,e,o,r,this._options)).isValid(this._isWallCallback,this._canBeDugCallback)&&(n.create(this._digCallback),n instanceof t.Map.Feature.Room&&this._rooms.push(n),n instanceof t.Map.Feature.Corridor&&(n.createPriorityWalls(this._priorityWallCallback),this._corridors.push(n)),!0)},t.Map.Digger.prototype._removeSurroundingWalls=function(i,e){for(var o=t.DIRS[4],r=0;r<o.length;r++){var n=o[r],s=i+n[0],a=e+n[1];delete this._walls[s+","+a];var s=i+2*n[0],a=e+2*n[1];delete this._walls[s+","+a]}},t.Map.Digger.prototype._getDiggingDirection=function(i,e){if(i<=0||e<=0||i>=this._width-1||e>=this._height-1)return null;for(var o=null,r=t.DIRS[4],n=0;n<r.length;n++){var s=r[n],a=i+s[0],h=e+s[1];if(!this._map[a][h]){if(o)return null;o=s}}return o?[-o[0],-o[1]]:null},t.Map.Digger.prototype._addDoors=function(){for(var t=this._map,i=function(i,e){return 1==t[i][e]},e=0;e<this._rooms.length;e++){var o=this._rooms[e];o.clearDoors(),o.addDoors(i)}},t.Map.Uniform=function(i,e,o){t.Map.Dungeon.call(this,i,e),this._options={roomWidth:[3,9],roomHeight:[3,5],roomDugPercentage:.1,timeLimit:1e3};for(var r in o)this._options[r]=o[r];this._roomAttempts=20,this._corridorAttempts=20,this._connected=[],this._unconnected=[],this._digCallback=this._digCallback.bind(this),this._canBeDugCallback=this._canBeDugCallback.bind(this),this._isWallCallback=this._isWallCallback.bind(this)},t.Map.Uniform.extend(t.Map.Dungeon),t.Map.Uniform.prototype.create=function(t){for(var i=Date.now();;){if(Date.now()-i>this._options.timeLimit)return null;if(this._map=this._fillMap(1),this._dug=0,this._rooms=[],this._unconnected=[],this._generateRooms(),!(this._rooms.length<2)&&this._generateCorridors())break}if(t)for(var e=0;e<this._width;e++)for(var o=0;o<this._height;o++)t(e,o,this._map[e][o]);return this},t.Map.Uniform.prototype._generateRooms=function(){var t=this._width-2,i=this._height-2;do{var e=this._generateRoom();if(this._dug/(t*i)>this._options.roomDugPercentage)break}while(e)},t.Map.Uniform.prototype._generateRoom=function(){for(var i=0;i<this._roomAttempts;){i++;var e=t.Map.Feature.Room.createRandom(this._width,this._height,this._options);if(e.isValid(this._isWallCallback,this._canBeDugCallback))return e.create(this._digCallback),this._rooms.push(e),e}return null},t.Map.Uniform.prototype._generateCorridors=function(){for(var t=0;t<this._corridorAttempts;){t++,this._corridors=[],this._map=this._fillMap(1);for(var i=0;i<this._rooms.length;i++){var e=this._rooms[i];e.clearDoors(),e.create(this._digCallback)}for(this._unconnected=this._rooms.slice().randomize(),this._connected=[],this._unconnected.length&&this._connected.push(this._unconnected.pop());;){var o=this._connected.random(),r=this._closestRoom(this._unconnected,o),n=this._closestRoom(this._connected,r);if(!this._connectRooms(r,n))break;if(!this._unconnected.length)return!0}}return!1},t.Map.Uniform.prototype._closestRoom=function(t,i){for(var e=1/0,o=i.getCenter(),r=null,n=0;n<t.length;n++){var s=t[n],a=s.getCenter(),h=a[0]-o[0],l=a[1]-o[1],c=h*h+l*l;c<e&&(e=c,r=s)}return r},t.Map.Uniform.prototype._connectRooms=function(t,i){var e=t.getCenter(),o=i.getCenter(),r=o[0]-e[0],n=o[1]-e[1];if(Math.abs(r)<Math.abs(n))var s=((c=n>0?2:0)+2)%4,a=i.getLeft(),h=i.getRight(),l=0;else var c=r>0?1:3,s=(c+2)%4,a=i.getTop(),h=i.getBottom(),l=1;var p=this._placeInWall(t,c);if(!p)return!1;if(p[l]>=a&&p[l]<=h){var _=p.slice(),u=null;switch(s){case 0:u=i.getTop()-1;break;case 1:u=i.getRight()+1;break;case 2:u=i.getBottom()+1;break;case 3:u=i.getLeft()-1}_[(l+1)%2]=u,this._digLine([p,_])}else if(p[l]<a-1||p[l]>h+1){var f=p[l]-o[l];switch(s){case 0:case 1:d=f<0?3:1;break;case 2:case 3:var d=f<0?1:3}if(s=(s+d)%4,!(_=this._placeInWall(i,s)))return!1;(m=[0,0])[l]=p[l],m[g=(l+1)%2]=_[g],this._digLine([p,m,_])}else{var g=(l+1)%2;if(!(_=this._placeInWall(i,s)))return!1;var m=Math.round((_[g]+p[g])/2),v=[0,0],y=[0,0];v[l]=p[l],v[g]=m,y[l]=_[l],y[g]=m,this._digLine([p,v,y,_])}return t.addDoor(p[0],p[1]),i.addDoor(_[0],_[1]),-1!=(l=this._unconnected.indexOf(t))&&(this._unconnected.splice(l,1),this._connected.push(t)),-1!=(l=this._unconnected.indexOf(i))&&(this._unconnected.splice(l,1),this._connected.push(i)),!0},t.Map.Uniform.prototype._placeInWall=function(t,i){var e=[0,0],o=[0,0],r=0;switch(i){case 0:o=[1,0],e=[t.getLeft(),t.getTop()-1],r=t.getRight()-t.getLeft()+1;break;case 1:o=[0,1],e=[t.getRight()+1,t.getTop()],r=t.getBottom()-t.getTop()+1;break;case 2:o=[1,0],e=[t.getLeft(),t.getBottom()+1],r=t.getRight()-t.getLeft()+1;break;case 3:o=[0,1],e=[t.getLeft()-1,t.getTop()],r=t.getBottom()-t.getTop()+1}for(var n=[],s=-2,a=0;a<r;a++){var h=e[0]+a*o[0],l=e[1]+a*o[1];n.push(null),1==this._map[h][l]?s!=a-1&&(n[a]=[h,l]):(s=a,a&&(n[a-1]=null))}for(a=n.length-1;a>=0;a--)n[a]||n.splice(a,1);return n.length?n.random():null},t.Map.Uniform.prototype._digLine=function(i){for(var e=1;e<i.length;e++){var o=i[e-1],r=i[e],n=new t.Map.Feature.Corridor(o[0],o[1],r[0],r[1]);n.create(this._digCallback),this._corridors.push(n)}},t.Map.Uniform.prototype._digCallback=function(t,i,e){this._map[t][i]=e,0==e&&this._dug++},t.Map.Uniform.prototype._isWallCallback=function(t,i){return!(t<0||i<0||t>=this._width||i>=this._height)&&1==this._map[t][i]},t.Map.Uniform.prototype._canBeDugCallback=function(t,i){return!(t<1||i<1||t+1>=this._width||i+1>=this._height)&&1==this._map[t][i]},t.Map.Rogue=function(i,e,o){t.Map.call(this,i,e),this._options={cellWidth:3,cellHeight:3};for(var r in o)this._options[r]=o[r];this._options.hasOwnProperty("roomWidth")||(this._options.roomWidth=this._calculateRoomSize(this._width,this._options.cellWidth)),this._options.hasOwnProperty("roomHeight")||(this._options.roomHeight=this._calculateRoomSize(this._height,this._options.cellHeight))},t.Map.Rogue.extend(t.Map),t.Map.Rogue.prototype.create=function(t){if(this.map=this._fillMap(1),this.rooms=[],this.connectedCells=[],this._initRooms(),this._connectRooms(),this._connectUnconnectedRooms(),this._createRandomRoomConnections(),this._createRooms(),this._createCorridors(),t)for(var i=0;i<this._width;i++)for(var e=0;e<this._height;e++)t(i,e,this.map[i][e]);return this},t.Map.Rogue.prototype._calculateRoomSize=function(t,i){var e=Math.floor(t/i*.8),o=Math.floor(t/i*.25);return o<2&&(o=2),e<2&&(e=2),[o,e]},t.Map.Rogue.prototype._initRooms=function(){for(var t=0;t<this._options.cellWidth;t++){this.rooms.push([]);for(var i=0;i<this._options.cellHeight;i++)this.rooms[t].push({x:0,y:0,width:0,height:0,connections:[],cellx:t,celly:i})}},t.Map.Rogue.prototype._connectRooms=function(){var i,e,o,r,n,s=t.RNG.getUniformInt(0,this._options.cellWidth-1),a=t.RNG.getUniformInt(0,this._options.cellHeight-1),h=!1;do{var l=[0,2,4,6];l=l.randomize();do{if(h=!1,i=l.pop(),e=s+t.DIRS[8][i][0],o=a+t.DIRS[8][i][1],!(e<0||e>=this._options.cellWidth||o<0||o>=this._options.cellHeight)){if((r=this.rooms[s][a]).connections.length>0&&r.connections[0][0]==e&&r.connections[0][1]==o)break;0==(n=this.rooms[e][o]).connections.length&&(n.connections.push([s,a]),this.connectedCells.push([e,o]),s=e,a=o,h=!0)}}while(l.length>0&&0==h)}while(l.length>0)},t.Map.Rogue.prototype._connectUnconnectedRooms=function(){var i=this._options.cellWidth,e=this._options.cellHeight;this.connectedCells=this.connectedCells.randomize();for(var o,r,n,s=0;s<this._options.cellWidth;s++)for(var a=0;a<this._options.cellHeight;a++)if(0==(o=this.rooms[s][a]).connections.length){var h=[0,2,4,6];h=h.randomize(),n=!1;do{var l=h.pop(),c=s+t.DIRS[8][l][0],p=a+t.DIRS[8][l][1];if(!(c<0||c>=i||p<0||p>=e)){if(r=this.rooms[c][p],n=!0,0==r.connections.length)break;for(var _=0;_<r.connections.length;_++)if(r.connections[_][0]==s&&r.connections[_][1]==a){n=!1;break}if(n)break}}while(h.length);n?o.connections.push([r.cellx,r.celly]):console.log("-- Unable to connect room.")}},t.Map.Rogue.prototype._createRandomRoomConnections=function(t){},t.Map.Rogue.prototype._createRooms=function(){for(var i,e,o,r,n,s=this._width,a=this._height,h=this._options.cellWidth,l=this._options.cellHeight,c=Math.floor(this._width/h),p=Math.floor(this._height/l),_=this._options.roomWidth,u=this._options.roomHeight,f=0;f<h;f++)for(var d=0;d<l;d++){if(o=c*f,r=p*d,0==o&&(o=1),0==r&&(r=1),i=t.RNG.getUniformInt(_[0],_[1]),e=t.RNG.getUniformInt(u[0],u[1]),d>0)for(n=this.rooms[f][d-1];r-(n.y+n.height)<3;)r++;if(f>0)for(n=this.rooms[f-1][d];o-(n.x+n.width)<3;)o++;for(var g=Math.round(t.RNG.getUniformInt(0,c-i)/2),m=Math.round(t.RNG.getUniformInt(0,p-e)/2);o+g+i>=s;)g?g--:i--;for(;r+m+e>=a;)m?m--:e--;o+=g,r+=m,this.rooms[f][d].x=o,this.rooms[f][d].y=r,this.rooms[f][d].width=i,this.rooms[f][d].height=e;for(var v=o;v<o+i;v++)for(var y=r;y<r+e;y++)this.map[v][y]=0}},t.Map.Rogue.prototype._getWallPosition=function(i,e){var o,r,n;return 1==e||3==e?(o=t.RNG.getUniformInt(i.x+1,i.x+i.width-2),n=1==e?(r=i.y-2)+1:(r=i.y+i.height+1)-1,this.map[o][n]=0):2!=e&&4!=e||(r=t.RNG.getUniformInt(i.y+1,i.y+i.height-2),n=2==e?(o=i.x+i.width+1)-1:(o=i.x-2)+1,this.map[n][r]=0),[o,r]},t.Map.Rogue.prototype._drawCorridor=function(i,e){var o,r,n,s,a=e[0]-i[0],h=e[1]-i[1],l=i[0],c=i[1],p=[],_=Math.abs(a),u=Math.abs(h),f=t.RNG.getUniform(),d=f,g=1-f;for(r=a>0?2:6,n=h>0?4:0,_<u?(o=Math.ceil(u*d),p.push([n,o]),p.push([r,_]),o=Math.floor(u*g),p.push([n,o])):(o=Math.ceil(_*d),p.push([r,o]),p.push([n,u]),o=Math.floor(_*g),p.push([r,o])),this.map[l][c]=0;p.length>0;)for(s=p.pop();s[1]>0;)l+=t.DIRS[8][s[0]][0],c+=t.DIRS[8][s[0]][1],this.map[l][c]=0,s[1]=s[1]-1},t.Map.Rogue.prototype._createCorridors=function(){for(var t,i,e,o,r,n=this._options.cellWidth,s=this._options.cellHeight,a=0;a<n;a++)for(var h=0;h<s;h++){t=this.rooms[a][h];for(var l=0;l<t.connections.length;l++)i=t.connections[l],(e=this.rooms[i[0]][i[1]]).cellx>t.cellx?(o=2,r=4):e.cellx<t.cellx?(o=4,r=2):e.celly>t.celly?(o=3,r=1):e.celly<t.celly&&(o=1,r=3),this._drawCorridor(this._getWallPosition(t,o),this._getWallPosition(e,r))}},t.Map.Feature=function(){},t.Map.Feature.prototype.isValid=function(t){},t.Map.Feature.prototype.create=function(t){},t.Map.Feature.prototype.debug=function(){},t.Map.Feature.createRandomAt=function(t,i,e,o,r){},t.Map.Feature.Room=function(t,i,e,o,r,n){this._x1=t,this._y1=i,this._x2=e,this._y2=o,this._doors={},arguments.length>4&&this.addDoor(r,n)},t.Map.Feature.Room.extend(t.Map.Feature),t.Map.Feature.Room.createRandomAt=function(i,e,o,r,n){var s=n.roomWidth[0],a=n.roomWidth[1],h=t.RNG.getUniformInt(s,a),s=n.roomHeight[0],a=n.roomHeight[1],l=t.RNG.getUniformInt(s,a);if(1==o)return new this(i+1,c=e-Math.floor(t.RNG.getUniform()*l),i+h,c+l-1,i,e);if(-1==o){var c=e-Math.floor(t.RNG.getUniform()*l);return new this(i-h,c,i-1,c+l-1,i,e)}if(1==r)return new this(p=i-Math.floor(t.RNG.getUniform()*h),e+1,p+h-1,e+l,i,e);if(-1==r){var p=i-Math.floor(t.RNG.getUniform()*h);return new this(p,e-l,p+h-1,e-1,i,e)}throw new Error("dx or dy must be 1 or -1")},t.Map.Feature.Room.createRandomCenter=function(i,e,o){var r=o.roomWidth[0],n=o.roomWidth[1],s=t.RNG.getUniformInt(r,n),r=o.roomHeight[0],n=o.roomHeight[1],a=t.RNG.getUniformInt(r,n),h=i-Math.floor(t.RNG.getUniform()*s),l=e-Math.floor(t.RNG.getUniform()*a);return new this(h,l,h+s-1,l+a-1)},t.Map.Feature.Room.createRandom=function(i,e,o){var r=o.roomWidth[0],n=o.roomWidth[1],s=t.RNG.getUniformInt(r,n),r=o.roomHeight[0],n=o.roomHeight[1],a=t.RNG.getUniformInt(r,n),h=i-s-1,l=e-a-1,c=1+Math.floor(t.RNG.getUniform()*h),p=1+Math.floor(t.RNG.getUniform()*l);return new this(c,p,c+s-1,p+a-1)},t.Map.Feature.Room.prototype.addDoor=function(t,i){return this._doors[t+","+i]=1,this},t.Map.Feature.Room.prototype.getDoors=function(t){for(var i in this._doors){var e=i.split(",");t(parseInt(e[0]),parseInt(e[1]))}return this},t.Map.Feature.Room.prototype.clearDoors=function(){return this._doors={},this},t.Map.Feature.Room.prototype.addDoors=function(t){for(var i=this._x1-1,e=this._x2+1,o=this._y1-1,r=this._y2+1,n=i;n<=e;n++)for(var s=o;s<=r;s++)n!=i&&n!=e&&s!=o&&s!=r||t(n,s)||this.addDoor(n,s);return this},t.Map.Feature.Room.prototype.debug=function(){console.log("room",this._x1,this._y1,this._x2,this._y2)},t.Map.Feature.Room.prototype.isValid=function(t,i){for(var e=this._x1-1,o=this._x2+1,r=this._y1-1,n=this._y2+1,s=e;s<=o;s++)for(var a=r;a<=n;a++)if(s==e||s==o||a==r||a==n){if(!t(s,a))return!1}else if(!i(s,a))return!1;return!0},t.Map.Feature.Room.prototype.create=function(t){for(var i=this._x1-1,e=this._x2+1,o=this._y1-1,r=this._y2+1,n=i;n<=e;n++)for(var s=o;s<=r;s++)t(n,s,n+","+s in this._doors?2:n==i||n==e||s==o||s==r?1:0)},t.Map.Feature.Room.prototype.getCenter=function(){return[Math.round((this._x1+this._x2)/2),Math.round((this._y1+this._y2)/2)]},t.Map.Feature.Room.prototype.getLeft=function(){return this._x1},t.Map.Feature.Room.prototype.getRight=function(){return this._x2},t.Map.Feature.Room.prototype.getTop=function(){return this._y1},t.Map.Feature.Room.prototype.getBottom=function(){return this._y2},t.Map.Feature.Corridor=function(t,i,e,o){this._startX=t,this._startY=i,this._endX=e,this._endY=o,this._endsWithAWall=!0},t.Map.Feature.Corridor.extend(t.Map.Feature),t.Map.Feature.Corridor.createRandomAt=function(i,e,o,r,n){var s=n.corridorLength[0],a=n.corridorLength[1],h=t.RNG.getUniformInt(s,a);return new this(i,e,i+o*h,e+r*h)},t.Map.Feature.Corridor.prototype.debug=function(){console.log("corridor",this._startX,this._startY,this._endX,this._endY)},t.Map.Feature.Corridor.prototype.isValid=function(t,i){var e=this._startX,o=this._startY,r=this._endX-e,n=this._endY-o,s=1+Math.max(Math.abs(r),Math.abs(n));r&&(r/=Math.abs(r)),n&&(n/=Math.abs(n));for(var a=n,h=-r,l=!0,c=0;c<s;c++){var p=e+c*r,_=o+c*n;if(i(p,_)||(l=!1),t(p+a,_+h)||(l=!1),t(p-a,_-h)||(l=!1),!l){s=c,this._endX=p-r,this._endY=_-n;break}}if(0==s)return!1;if(1==s&&t(this._endX+r,this._endY+n))return!1;var u=!t(this._endX+r+a,this._endY+n+h),f=!t(this._endX+r-a,this._endY+n-h);return this._endsWithAWall=t(this._endX+r,this._endY+n),!u&&!f||!this._endsWithAWall},t.Map.Feature.Corridor.prototype.create=function(t){var i=this._startX,e=this._startY,o=this._endX-i,r=this._endY-e,n=1+Math.max(Math.abs(o),Math.abs(r));o&&(o/=Math.abs(o)),r&&(r/=Math.abs(r));for(var s=0;s<n;s++)t(i+s*o,e+s*r,0);return!0},t.Map.Feature.Corridor.prototype.createPriorityWalls=function(t){if(this._endsWithAWall){var i=this._startX,e=this._startY,o=this._endX-i,r=this._endY-e;o&&(o/=Math.abs(o)),r&&(r/=Math.abs(r));var n=r,s=-o;t(this._endX+o,this._endY+r),t(this._endX+n,this._endY+s),t(this._endX-n,this._endY-s)}},t.Noise=function(){},t.Noise.prototype.get=function(t,i){},t.Noise.Simplex=function(i){t.Noise.call(this),this._F2=.5*(Math.sqrt(3)-1),this._G2=(3-Math.sqrt(3))/6,this._gradients=[[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1]];for(var e=[],o=i||256,r=0;r<o;r++)e.push(r);e=e.randomize(),this._perms=[],this._indexes=[];for(r=0;r<2*o;r++)this._perms.push(e[r%o]),this._indexes.push(this._perms[r]%this._gradients.length)},t.Noise.Simplex.extend(t.Noise),t.Noise.Simplex.prototype.get=function(t,i){var e,o,r,n=this._perms,s=this._indexes,a=n.length/2,h=this._G2,l=0,c=0,p=0,_=(t+i)*this._F2,u=Math.floor(t+_),f=Math.floor(i+_),d=(u+f)*h,g=t-(u-d),m=i-(f-d);g>m?(o=1,r=0):(o=0,r=1);var v=g-o+h,y=m-r+h,M=g-1+2*h,S=m-1+2*h,b=u.mod(a),x=f.mod(a),w=.5-g*g-m*m;w>=0&&(w*=w,e=s[b+n[x]],l=w*w*((C=this._gradients[e])[0]*g+C[1]*m));var R=.5-v*v-y*y;R>=0&&(R*=R,e=s[b+o+n[x+r]],c=R*R*((C=this._gradients[e])[0]*v+C[1]*y));var V=.5-M*M-S*S;if(V>=0){V*=V,e=s[b+1+n[x+1]];var C=this._gradients[e];p=V*V*(C[0]*M+C[1]*S)}return 70*(l+c+p)},t.FOV=function(t,i){this._lightPasses=t,this._options={topology:8};for(var e in i)this._options[e]=i[e]},t.FOV.prototype.compute=function(t,i,e,o){},t.FOV.prototype._getCircle=function(i,e,o){var r,n,s,a=[];switch(this._options.topology){case 4:n=1,s=[0,1],r=[t.DIRS[8][7],t.DIRS[8][1],t.DIRS[8][3],t.DIRS[8][5]];break;case 6:r=t.DIRS[6],n=1,s=[-1,1];break;case 8:r=t.DIRS[4],n=2,s=[-1,1]}for(var h=i+s[0]*o,l=e+s[1]*o,c=0;c<r.length;c++)for(var p=0;p<o*n;p++)a.push([h,l]),h+=r[c][0],l+=r[c][1];return a},t.FOV.DiscreteShadowcasting=function(i,e){t.FOV.call(this,i,e)},t.FOV.DiscreteShadowcasting.extend(t.FOV),t.FOV.DiscreteShadowcasting.prototype.compute=function(t,i,e,o){this._coords,this._map;if(o(t,i,0,1),this._lightPasses(t,i))for(var r,n,s,a,h,l=[],c=1;c<=e;c++)for(var p=this._getCircle(t,i,c),_=360/p.length,u=0;u<p.length;u++)if(s=p[u][0],a=p[u][1],r=_*(u-.5),n=r+_,h=!this._lightPasses(s,a),this._visibleCoords(Math.floor(r),Math.ceil(n),h,l)&&o(s,a,c,1),2==l.length&&0==l[0]&&360==l[1])return},t.FOV.DiscreteShadowcasting.prototype._visibleCoords=function(t,i,e,o){if(t<0){var r=arguments.callee(0,i,e,o),n=arguments.callee(360+t,360,e,o);return r||n}for(var s=0;s<o.length&&o[s]<t;)s++;if(s==o.length)return e&&o.push(t,i),!0;var a=0;if(s%2){for(;s<o.length&&o[s]<i;)s++,a++;return 0!=a&&(e&&(a%2?o.splice(s-a,a,i):o.splice(s-a,a)),!0)}for(;s<o.length&&o[s]<i;)s++,a++;return(t!=o[s-a]||1!=a)&&(e&&(a%2?o.splice(s-a,a,t):o.splice(s-a,a,t,i)),!0)},t.FOV.PreciseShadowcasting=function(i,e){t.FOV.call(this,i,e)},t.FOV.PreciseShadowcasting.extend(t.FOV),t.FOV.PreciseShadowcasting.prototype.compute=function(t,i,e,o){if(o(t,i,0,1),this._lightPasses(t,i))for(var r,n,s,a,h,l,c=[],p=1;p<=e;p++)for(var _=this._getCircle(t,i,p),u=_.length,f=0;f<u;f++)if(r=_[f][0],n=_[f][1],a=[f?2*f-1:2*u-1,2*u],h=[2*f+1,2*u],s=!this._lightPasses(r,n),(l=this._checkVisibility(a,h,s,c))&&o(r,n,p,l),2==c.length&&0==c[0][0]&&c[1][0]==c[1][1])return},t.FOV.PreciseShadowcasting.prototype._checkVisibility=function(t,i,e,o){if(t[0]>i[0])return(this._checkVisibility(t,[t[1],t[1]],e,o)+this._checkVisibility([0,1],i,e,o))/2;for(var r=0,n=!1;r<o.length;){if((l=(h=o[r])[0]*t[1]-t[0]*h[1])>=0){0!=l||r%2||(n=!0);break}r++}for(var s=o.length,a=!1;s--;){var h=o[s],l=i[0]*h[1]-h[0]*i[1];if(l>=0){0==l&&s%2&&(a=!0);break}}var c=!0;if(r==s&&(n||a)?c=!1:n&&a&&r+1==s&&s%2?c=!1:r>s&&r%2&&(c=!1),!c)return 0;var p,_=s-r+1;if(_%2)if(r%2){var u=o[r];p=(i[0]*u[1]-u[0]*i[1])/(u[1]*i[1]),e&&o.splice(r,_,i)}else p=((u=o[s])[0]*t[1]-t[0]*u[1])/(t[1]*u[1]),e&&o.splice(r,_,t);else{if(!(r%2))return e&&o.splice(r,_,t,i),1;var f=o[r],d=o[s];p=(d[0]*f[1]-f[0]*d[1])/(f[1]*d[1]),e&&o.splice(r,_)}return p/((i[0]*t[1]-t[0]*i[1])/(t[1]*i[1]))},t.FOV.RecursiveShadowcasting=function(i,e){t.FOV.call(this,i,e)},t.FOV.RecursiveShadowcasting.extend(t.FOV),t.FOV.RecursiveShadowcasting.OCTANTS=[[-1,0,0,1],[0,-1,1,0],[0,-1,-1,0],[-1,0,0,-1],[1,0,0,-1],[0,1,-1,0],[0,1,1,0],[1,0,0,1]],t.FOV.RecursiveShadowcasting.prototype.compute=function(i,e,o,r){r(i,e,0,1);for(var n=0;n<t.FOV.RecursiveShadowcasting.OCTANTS.length;n++)this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[n],o,r)},t.FOV.RecursiveShadowcasting.prototype.compute180=function(i,e,o,r,n){n(i,e,0,1);var s=(r-1+8)%8,a=(r-2+8)%8,h=(r+1+8)%8;this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[a],o,n),this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[s],o,n),this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[r],o,n),this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[h],o,n)},t.FOV.RecursiveShadowcasting.prototype.compute90=function(i,e,o,r,n){n(i,e,0,1);var s=(r-1+8)%8;this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[r],o,n),this._renderOctant(i,e,t.FOV.RecursiveShadowcasting.OCTANTS[s],o,n)},t.FOV.RecursiveShadowcasting.prototype._renderOctant=function(t,i,e,o,r){this._castVisibility(t,i,1,1,0,o+1,e[0],e[1],e[2],e[3],r)},t.FOV.RecursiveShadowcasting.prototype._castVisibility=function(t,i,e,o,r,n,s,a,h,l,c){if(!(o<r))for(var p=e;p<=n;p++){for(var _=-p-1,u=-p,f=!1,d=0;_<=0;){var g=t+(_+=1)*s+u*a,m=i+_*h+u*l,v=(_-.5)/(u+.5),y=(_+.5)/(u-.5);if(!(y>o)){if(v<r)break;if(_*_+u*u<n*n&&c(g,m,p,1),f){if(!this._lightPasses(g,m)){d=y;continue}f=!1,o=d}else!this._lightPasses(g,m)&&p<n&&(f=!0,this._castVisibility(t,i,p+1,o,v,n,s,a,h,l,c),d=y)}}if(f)break}},t.Color={fromString:function(t){var i,e;if(t in this._cache)i=this._cache[t];else{if("#"==t.charAt(0)){var o=t.match(/[0-9a-f]/gi).map(function(t){return parseInt(t,16)});if(3==o.length)i=o.map(function(t){return 17*t});else{for(var r=0;r<3;r++)o[r+1]+=16*o[r],o.splice(r,1);i=o}}else i=(e=t.match(/rgb\(([0-9, ]+)\)/i))?e[1].split(/\s*,\s*/).map(function(t){return parseInt(t)}):[0,0,0];this._cache[t]=i}return i.slice()},add:function(t,i){for(var e=t.slice(),o=0;o<3;o++)for(var r=1;r<arguments.length;r++)e[o]+=arguments[r][o];return e},add_:function(t,i){for(var e=0;e<3;e++)for(var o=1;o<arguments.length;o++)t[e]+=arguments[o][e];return t},multiply:function(t,i){for(var e=t.slice(),o=0;o<3;o++){for(var r=1;r<arguments.length;r++)e[o]*=arguments[r][o]/255;e[o]=Math.round(e[o])}return e},multiply_:function(t,i){for(var e=0;e<3;e++){for(var o=1;o<arguments.length;o++)t[e]*=arguments[o][e]/255;t[e]=Math.round(t[e])}return t},interpolate:function(t,i,e){arguments.length<3&&(e=.5);for(var o=t.slice(),r=0;r<3;r++)o[r]=Math.round(o[r]+e*(i[r]-t[r]));return o},interpolateHSL:function(t,i,e){arguments.length<3&&(e=.5);for(var o=this.rgb2hsl(t),r=this.rgb2hsl(i),n=0;n<3;n++)o[n]+=e*(r[n]-o[n]);return this.hsl2rgb(o)},randomize:function(i,e){e instanceof Array||(e=Math.round(t.RNG.getNormal(0,e)));for(var o=i.slice(),r=0;r<3;r++)o[r]+=e instanceof Array?Math.round(t.RNG.getNormal(0,e[r])):e;return o},rgb2hsl:function(t){var i,e,o=t[0]/255,r=t[1]/255,n=t[2]/255,s=Math.max(o,r,n),a=Math.min(o,r,n),h=(s+a)/2;if(s==a)i=e=0;else{var l=s-a;switch(e=h>.5?l/(2-s-a):l/(s+a),s){case o:i=(r-n)/l+(r<n?6:0);break;case r:i=(n-o)/l+2;break;case n:i=(o-r)/l+4}i/=6}return[i,e,h]},hsl2rgb:function(t){var i=t[2];if(0==t[1])return i=Math.round(255*i),[i,i,i];var e=function(t,i,e){return e<0&&(e+=1),e>1&&(e-=1),e<1/6?t+6*(i-t)*e:e<.5?i:e<2/3?t+(i-t)*(2/3-e)*6:t},o=t[1],r=i<.5?i*(1+o):i+o-i*o,n=2*i-r,s=e(n,r,t[0]+1/3),a=e(n,r,t[0]),h=e(n,r,t[0]-1/3);return[Math.round(255*s),Math.round(255*a),Math.round(255*h)]},toRGB:function(t){return"rgb("+this._clamp(t[0])+","+this._clamp(t[1])+","+this._clamp(t[2])+")"},toHex:function(t){for(var i=[],e=0;e<3;e++)i.push(this._clamp(t[e]).toString(16).lpad("0",2));return"#"+i.join("")},_clamp:function(t){return t<0?0:t>255?255:t},_cache:{black:[0,0,0],navy:[0,0,128],darkblue:[0,0,139],mediumblue:[0,0,205],blue:[0,0,255],darkgreen:[0,100,0],green:[0,128,0],teal:[0,128,128],darkcyan:[0,139,139],deepskyblue:[0,191,255],darkturquoise:[0,206,209],mediumspringgreen:[0,250,154],lime:[0,255,0],springgreen:[0,255,127],aqua:[0,255,255],cyan:[0,255,255],midnightblue:[25,25,112],dodgerblue:[30,144,255],forestgreen:[34,139,34],seagreen:[46,139,87],darkslategray:[47,79,79],darkslategrey:[47,79,79],limegreen:[50,205,50],mediumseagreen:[60,179,113],turquoise:[64,224,208],royalblue:[65,105,225],steelblue:[70,130,180],darkslateblue:[72,61,139],mediumturquoise:[72,209,204],indigo:[75,0,130],darkolivegreen:[85,107,47],cadetblue:[95,158,160],cornflowerblue:[100,149,237],mediumaquamarine:[102,205,170],dimgray:[105,105,105],dimgrey:[105,105,105],slateblue:[106,90,205],olivedrab:[107,142,35],slategray:[112,128,144],slategrey:[112,128,144],lightslategray:[119,136,153],lightslategrey:[119,136,153],mediumslateblue:[123,104,238],lawngreen:[124,252,0],chartreuse:[127,255,0],aquamarine:[127,255,212],maroon:[128,0,0],purple:[128,0,128],olive:[128,128,0],gray:[128,128,128],grey:[128,128,128],skyblue:[135,206,235],lightskyblue:[135,206,250],blueviolet:[138,43,226],darkred:[139,0,0],darkmagenta:[139,0,139],saddlebrown:[139,69,19],darkseagreen:[143,188,143],lightgreen:[144,238,144],mediumpurple:[147,112,216],darkviolet:[148,0,211],palegreen:[152,251,152],darkorchid:[153,50,204],yellowgreen:[154,205,50],sienna:[160,82,45],brown:[165,42,42],darkgray:[169,169,169],darkgrey:[169,169,169],lightblue:[173,216,230],greenyellow:[173,255,47],paleturquoise:[175,238,238],lightsteelblue:[176,196,222],powderblue:[176,224,230],firebrick:[178,34,34],darkgoldenrod:[184,134,11],mediumorchid:[186,85,211],rosybrown:[188,143,143],darkkhaki:[189,183,107],silver:[192,192,192],mediumvioletred:[199,21,133],indianred:[205,92,92],peru:[205,133,63],chocolate:[210,105,30],tan:[210,180,140],lightgray:[211,211,211],lightgrey:[211,211,211],palevioletred:[216,112,147],thistle:[216,191,216],orchid:[218,112,214],goldenrod:[218,165,32],crimson:[220,20,60],gainsboro:[220,220,220],plum:[221,160,221],burlywood:[222,184,135],lightcyan:[224,255,255],lavender:[230,230,250],darksalmon:[233,150,122],violet:[238,130,238],palegoldenrod:[238,232,170],lightcoral:[240,128,128],khaki:[240,230,140],aliceblue:[240,248,255],honeydew:[240,255,240],azure:[240,255,255],sandybrown:[244,164,96],wheat:[245,222,179],beige:[245,245,220],whitesmoke:[245,245,245],mintcream:[245,255,250],ghostwhite:[248,248,255],salmon:[250,128,114],antiquewhite:[250,235,215],linen:[250,240,230],lightgoldenrodyellow:[250,250,210],oldlace:[253,245,230],red:[255,0,0],fuchsia:[255,0,255],magenta:[255,0,255],deeppink:[255,20,147],orangered:[255,69,0],tomato:[255,99,71],hotpink:[255,105,180],coral:[255,127,80],darkorange:[255,140,0],lightsalmon:[255,160,122],orange:[255,165,0],lightpink:[255,182,193],pink:[255,192,203],gold:[255,215,0],peachpuff:[255,218,185],navajowhite:[255,222,173],moccasin:[255,228,181],bisque:[255,228,196],mistyrose:[255,228,225],blanchedalmond:[255,235,205],papayawhip:[255,239,213],lavenderblush:[255,240,245],seashell:[255,245,238],cornsilk:[255,248,220],lemonchiffon:[255,250,205],floralwhite:[255,250,240],snow:[255,250,250],yellow:[255,255,0],lightyellow:[255,255,224],ivory:[255,255,240],white:[255,255,255]}},t.Lighting=function(t,i){this._reflectivityCallback=t,this._options={passes:1,emissionThreshold:100,range:10},this._fov=null,this._lights={},this._reflectivityCache={},this._fovCache={},this.setOptions(i)},t.Lighting.prototype.setOptions=function(t){for(var i in t)this._options[i]=t[i];return t&&t.range&&this.reset(),this},t.Lighting.prototype.setFOV=function(t){return this._fov=t,this._fovCache={},this},t.Lighting.prototype.setLight=function(i,e,o){var r=i+","+e;return o?this._lights[r]="string"==typeof o?t.Color.fromString(o):o:delete this._lights[r],this},t.Lighting.prototype.clearLights=function(){this._lights={}},t.Lighting.prototype.reset=function(){return this._reflectivityCache={},this._fovCache={},this},t.Lighting.prototype.compute=function(i){var e={},o={},r={};for(var n in this._lights){var s=this._lights[n];o[n]=[0,0,0],t.Color.add_(o[n],s)}for(var a=0;a<this._options.passes;a++)this._emitLight(o,r,e),a+1!=this._options.passes&&(o=this._computeEmitters(r,e));for(var h in r){var l=h.split(",");i(parseInt(l[0]),parseInt(l[1]),r[h])}return this},t.Lighting.prototype._emitLight=function(t,i,e){for(var o in t){var r=o.split(","),n=parseInt(r[0]),s=parseInt(r[1]);this._emitLightFromCell(n,s,t[o],i),e[o]=1}return this},t.Lighting.prototype._computeEmitters=function(t,i){var e={};for(var o in t)if(!(o in i)){var r=t[o];if(o in this._reflectivityCache)h=this._reflectivityCache[o];else{var n=o.split(","),s=parseInt(n[0]),a=parseInt(n[1]),h=this._reflectivityCallback(s,a);this._reflectivityCache[o]=h}if(0!=h){for(var l=[],c=0,p=0;p<3;p++){var _=Math.round(r[p]*h);l[p]=_,c+=_}c>this._options.emissionThreshold&&(e[o]=l)}}return e},t.Lighting.prototype._emitLightFromCell=function(t,i,e,o){var r=t+","+i;if(r in this._fovCache)n=this._fovCache[r];else var n=this._updateFOV(t,i);for(var s in n){var a=n[s];if(s in o)h=o[s];else{var h=[0,0,0];o[s]=h}for(var l=0;l<3;l++)h[l]+=Math.round(e[l]*a)}return this},t.Lighting.prototype._updateFOV=function(t,i){var e=t+","+i,o={};this._fovCache[e]=o;var r=this._options.range,n=function(t,i,e,n){var s=t+","+i,a=n*(1-e/r);0!=a&&(o[s]=a)};return this._fov.compute(t,i,r,n.bind(this)),o},t.Path=function(i,e,o,r){this._toX=i,this._toY=e,this._fromX=null,this._fromY=null,this._passableCallback=o,this._options={topology:8};for(var n in r)this._options[n]=r[n];this._dirs=t.DIRS[this._options.topology],8==this._options.topology&&(this._dirs=[this._dirs[0],this._dirs[2],this._dirs[4],this._dirs[6],this._dirs[1],this._dirs[3],this._dirs[5],this._dirs[7]])},t.Path.prototype.compute=function(t,i,e){},t.Path.prototype._getNeighbors=function(t,i){for(var e=[],o=0;o<this._dirs.length;o++){var r=this._dirs[o],n=t+r[0],s=i+r[1];this._passableCallback(n,s)&&e.push([n,s])}return e},t.Path.Dijkstra=function(i,e,o,r){t.Path.call(this,i,e,o,r),this._computed={},this._todo=[],this._add(i,e,null)},t.Path.Dijkstra.extend(t.Path),t.Path.Dijkstra.prototype.compute=function(t,i,e){var o=t+","+i;if(o in this._computed||this._compute(t,i),o in this._computed)for(var r=this._computed[o];r;)e(r.x,r.y),r=r.prev},t.Path.Dijkstra.prototype._compute=function(t,i){for(;this._todo.length;){var e=this._todo.shift();if(e.x==t&&e.y==i)return;for(var o=this._getNeighbors(e.x,e.y),r=0;r<o.length;r++){var n=o[r],s=n[0],a=n[1];s+","+a in this._computed||this._add(s,a,e)}}},t.Path.Dijkstra.prototype._add=function(t,i,e){var o={x:t,y:i,prev:e};this._computed[t+","+i]=o,this._todo.push(o)},t.Path.AStar=function(i,e,o,r){t.Path.call(this,i,e,o,r),this._todo=[],this._done={},this._fromX=null,this._fromY=null},t.Path.AStar.extend(t.Path),t.Path.AStar.prototype.compute=function(t,i,e){for(this._todo=[],this._done={},this._fromX=t,this._fromY=i,this._add(this._toX,this._toY,null);this._todo.length&&((h=this._todo.shift()).x!=t||h.y!=i);)for(var o=this._getNeighbors(h.x,h.y),r=0;r<o.length;r++){var n=o[r],s=n[0],a=n[1];s+","+a in this._done||this._add(s,a,h)}var h=this._done[t+","+i];if(h)for(;h;)e(h.x,h.y),h=h.prev},t.Path.AStar.prototype._add=function(t,i,e){var o=this._distance(t,i),r={x:t,y:i,prev:e,g:e?e.g+1:0,h:o};this._done[t+","+i]=r;for(var n=r.g+r.h,s=0;s<this._todo.length;s++){var a=this._todo[s],h=a.g+a.h;if(n<h||n==h&&o<a.h)return void this._todo.splice(s,0,r)}this._todo.push(r)},t.Path.AStar.prototype._distance=function(t,i){switch(this._options.topology){case 4:return Math.abs(t-this._fromX)+Math.abs(i-this._fromY);case 6:var e=Math.abs(t-this._fromX),o=Math.abs(i-this._fromY);return o+Math.max(0,(e-o)/2);case 8:return Math.max(Math.abs(t-this._fromX),Math.abs(i-this._fromY))}throw new Error("Illegal topology")},t});