├── down.png ├── left.png ├── up.png ├── index.html ├── right.png ├── snake.rar ├── .gitattributes ├── style.css ├── script.js └── rot.js /down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/down.png -------------------------------------------------------------------------------- /left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/left.png -------------------------------------------------------------------------------- /up.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/up.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/index.html -------------------------------------------------------------------------------- /right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/right.png -------------------------------------------------------------------------------- /snake.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ran4erep/snake/master/snake.rar -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | #snake canvas { 2 | border: 5px outset black; 3 | } 4 | 5 | .d-pad { 6 | position: relative; 7 | width: 130px; 8 | height: 130px; 9 | top: -200px; 10 | right: -100px; 11 | } 12 | 13 | #buttonUp, #buttonDown { 14 | position: absolute; 15 | left: 50%; 16 | transform: translateX(-50%); 17 | } 18 | 19 | #buttonUp { 20 | top: 0; 21 | } 22 | 23 | #buttonDown { 24 | bottom: 0; 25 | } 26 | 27 | #buttonLeft, #buttonRight { 28 | position: absolute; 29 | top: 50%; 30 | transform: translateY(-50%); 31 | } 32 | 33 | #buttonLeft { 34 | left: 0; 35 | } 36 | 37 | #buttonRight { 38 | right: 0; 39 | } -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | function SHOW(node) { 2 | var mountNodeId = 'snake'; 3 | document.getElementById(mountNodeId).appendChild(node); 4 | } 5 | //функция святого рандомчика 6 | function rand(min, max) { 7 | return Math.floor(Math.random() * (max - min + 1)) + min; 8 | } 9 | 10 | //функция очистки для ROT 11 | function cls() { 12 | for (var i=0; i<=width; i++) { 13 | for (var j=0; j<=height; j++) { 14 | switch(optionsBG) { 15 | case 1 : 16 | display.draw(i, j, ".", "#000000"); 17 | break; 18 | 19 | case 0 : 20 | display.draw(i, j, " ", "#000000"); 21 | break; 22 | } 23 | } 24 | } 25 | } 26 | 27 | 28 | //управление игрой 29 | function input() { 30 | //управление с клавиатуры 31 | document.addEventListener('keydown', function(e) { 32 | switch (e.keyCode) { 33 | case 68 : 34 | case 39 : 35 | direction = 1; 36 | break; 37 | 38 | case 65 : 39 | case 37 : 40 | direction = 2; 41 | break; 42 | 43 | case 87 : 44 | case 38 : 45 | direction = 3; 46 | break; 47 | 48 | case 83 : 49 | case 40 : 50 | direction = 4; 51 | break; 52 | 53 | } 54 | }) 55 | //управление при помощи тачскрина/мыши 56 | buttonRight.addEventListener('click', function(e) { 57 | direction=1; 58 | }); 59 | buttonLeft.addEventListener('click', function(e) { 60 | direction=2; 61 | }); 62 | buttonUp.addEventListener('click', function(e) { 63 | direction=3; 64 | }); 65 | buttonDown.addEventListener('click', function(e) { 66 | direction=4; 67 | }); 68 | } 69 | 70 | //логика 71 | function logic() { 72 | var prevX = tailX[0]; 73 | var prevY = tailY[0]; 74 | var prev2X, prev2Y; 75 | tailX[0]=x; 76 | tailY[0]=y; 77 | for (var i=1; i19) 135 | y=0; 136 | if (y<0) 137 | y=19; 138 | if (x>19) 139 | x=0; 140 | if (x<0) 141 | x=19; 142 | 143 | //что делать когда игра закончилась (змея укусила свой хвост) 144 | for (var i=0; i { 185 | // console.log(e.target.value); 186 | //}); 187 | var gameOver; 188 | var moveState = null; 189 | var gameSpeed; 190 | var width = 20, height =20; 191 | var x, y, fruitX, fruitY, score, tail; 192 | var direction = 0; 193 | var timeStart = 0; 194 | var tail = 0; 195 | var tailX = [], tailY = []; 196 | var tailDraw; 197 | var R, G, B; 198 | 199 | //рендеринг консоли 200 | var display = new ROT.Display({width:width, height:height, spacing: 0.8, forceSquareRatio:true, bg:"#D8D8D3"}); 201 | SHOW(display.getContainer()); 202 | 203 | //первоначальная настройка 204 | gameOver = false; 205 | gameSpeed = 300; 206 | x = width/2; 207 | y = height/2; 208 | fruitX = rand(9, 11); 209 | fruitY = rand(9, 11); 210 | score = 0; 211 | optionsBG = 0; 212 | 213 | 214 | //главная функция 215 | function update(timestamp){ 216 | if (timestamp - timeStart >= gameSpeed) { 217 | //cls(); 218 | //отрисовываем фрукт 219 | //display.draw(fruitX, fruitY, "@", "#000000"); 220 | //отрисовываем змейку 221 | //display.draw(x, y, "O", "#000000"); 222 | //выводим очки в HTML документ 223 | for (var i=0; i>> 0) * FRAC; 33 | seed = seed * 69069 + 1 >>> 0; 34 | this._s1 = seed * FRAC; 35 | seed = seed * 69069 + 1 >>> 0; 36 | this._s2 = seed * FRAC; 37 | this._c = 1; 38 | return this; 39 | }; 40 | 41 | _proto.getUniform = function getUniform() { 42 | var t = 2091639 * this._s0 + this._c * FRAC; 43 | this._s0 = this._s1; 44 | this._s1 = this._s2; 45 | this._c = t | 0; 46 | this._s2 = t - this._c; 47 | return this._s2; 48 | }; 49 | 50 | _proto.getUniformInt = function getUniformInt(lowerBound, upperBound) { 51 | var max = Math.max(lowerBound, upperBound); 52 | var min = Math.min(lowerBound, upperBound); 53 | return Math.floor(this.getUniform() * (max - min + 1)) + min; 54 | }; 55 | 56 | _proto.getNormal = function getNormal(mean, stddev) { 57 | if (mean === void 0) { 58 | mean = 0; 59 | } 60 | 61 | if (stddev === void 0) { 62 | stddev = 1; 63 | } 64 | 65 | var u, v, r; 66 | 67 | do { 68 | u = 2 * this.getUniform() - 1; 69 | v = 2 * this.getUniform() - 1; 70 | r = u * u + v * v; 71 | } while (r > 1 || r == 0); 72 | 73 | var gauss = u * Math.sqrt(-2 * Math.log(r) / r); 74 | return mean + gauss * stddev; 75 | }; 76 | 77 | _proto.getPercentage = function getPercentage() { 78 | return 1 + Math.floor(this.getUniform() * 100); 79 | }; 80 | 81 | _proto.getItem = function getItem(array) { 82 | if (!array.length) { 83 | return null; 84 | } 85 | 86 | return array[Math.floor(this.getUniform() * array.length)]; 87 | }; 88 | 89 | _proto.shuffle = function shuffle(array) { 90 | var result = []; 91 | var clone = array.slice(); 92 | 93 | while (clone.length) { 94 | var _index = clone.indexOf(this.getItem(clone)); 95 | 96 | result.push(clone.splice(_index, 1)[0]); 97 | } 98 | 99 | return result; 100 | }; 101 | 102 | _proto.getWeightedValue = function getWeightedValue(data) { 103 | var total = 0; 104 | 105 | for (var _id in data) { 106 | total += data[_id]; 107 | } 108 | 109 | var random = this.getUniform() * total; 110 | var id, 111 | part = 0; 112 | 113 | for (id in data) { 114 | part += data[id]; 115 | 116 | if (random < part) { 117 | return id; 118 | } 119 | } 120 | 121 | return id; 122 | }; 123 | 124 | _proto.getState = function getState() { 125 | return [this._s0, this._s1, this._s2, this._c]; 126 | }; 127 | 128 | _proto.setState = function setState(state) { 129 | this._s0 = state[0]; 130 | this._s1 = state[1]; 131 | this._s2 = state[2]; 132 | this._c = state[3]; 133 | return this; 134 | }; 135 | 136 | _proto.clone = function clone() { 137 | var clone = new RNG(); 138 | return clone.setState(this.getState()); 139 | }; 140 | 141 | return RNG; 142 | }(); 143 | 144 | var RNG$1 = new RNG().setSeed(Date.now()); 145 | 146 | var Backend = 147 | /*#__PURE__*/ 148 | function () { 149 | function Backend() {} 150 | 151 | var _proto2 = Backend.prototype; 152 | 153 | _proto2.getContainer = function getContainer() { 154 | return null; 155 | }; 156 | 157 | _proto2.setOptions = function setOptions(options) { 158 | this._options = options; 159 | }; 160 | 161 | return Backend; 162 | }(); 163 | 164 | var Canvas = 165 | /*#__PURE__*/ 166 | function (_Backend) { 167 | _inheritsLoose(Canvas, _Backend); 168 | 169 | function Canvas() { 170 | var _this; 171 | 172 | _this = _Backend.call(this) || this; 173 | _this._ctx = document.createElement("canvas").getContext("2d"); 174 | return _this; 175 | } 176 | 177 | var _proto3 = Canvas.prototype; 178 | 179 | _proto3.schedule = function schedule(cb) { 180 | requestAnimationFrame(cb); 181 | }; 182 | 183 | _proto3.getContainer = function getContainer() { 184 | return this._ctx.canvas; 185 | }; 186 | 187 | _proto3.setOptions = function setOptions(opts) { 188 | _Backend.prototype.setOptions.call(this, opts); 189 | 190 | var style = opts.fontStyle ? opts.fontStyle + " " : ""; 191 | var font = style + " " + opts.fontSize + "px " + opts.fontFamily; 192 | this._ctx.font = font; 193 | 194 | this._updateSize(); 195 | 196 | this._ctx.font = font; 197 | this._ctx.textAlign = "center"; 198 | this._ctx.textBaseline = "middle"; 199 | }; 200 | 201 | _proto3.clear = function clear() { 202 | this._ctx.fillStyle = this._options.bg; 203 | 204 | this._ctx.fillRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height); 205 | }; 206 | 207 | _proto3.eventToPosition = function eventToPosition(x, y) { 208 | var canvas = this._ctx.canvas; 209 | var rect = canvas.getBoundingClientRect(); 210 | x -= rect.left; 211 | y -= rect.top; 212 | x *= canvas.width / rect.width; 213 | y *= canvas.height / rect.height; 214 | 215 | if (x < 0 || y < 0 || x >= canvas.width || y >= canvas.height) { 216 | return [-1, -1]; 217 | } 218 | 219 | return this._normalizedEventToPosition(x, y); 220 | }; 221 | 222 | return Canvas; 223 | }(Backend); 224 | 225 | function mod(x, n) { 226 | return (x % n + n) % n; 227 | } 228 | 229 | function clamp(val, min, max) { 230 | if (min === void 0) { 231 | min = 0; 232 | } 233 | 234 | if (max === void 0) { 235 | max = 1; 236 | } 237 | 238 | if (val < min) return min; 239 | if (val > max) return max; 240 | return val; 241 | } 242 | 243 | function capitalize(string) { 244 | return string.charAt(0).toUpperCase() + string.substring(1); 245 | } 246 | 247 | function format(template) { 248 | for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { 249 | args[_key - 1] = arguments[_key]; 250 | } 251 | 252 | var map = format.map; 253 | 254 | var replacer = function replacer(match, group1, group2, index) { 255 | if (template.charAt(index - 1) == "%") { 256 | return match.substring(1); 257 | } 258 | 259 | if (!args.length) { 260 | return match; 261 | } 262 | 263 | var obj = args[0]; 264 | var group = group1 || group2; 265 | var parts = group.split(","); 266 | var name = parts.shift() || ""; 267 | var method = map[name.toLowerCase()]; 268 | 269 | if (!method) { 270 | return match; 271 | } 272 | 273 | obj = args.shift(); 274 | var replaced = obj[method].apply(obj, parts); 275 | var first = name.charAt(0); 276 | 277 | if (first != first.toLowerCase()) { 278 | replaced = capitalize(replaced); 279 | } 280 | 281 | return replaced; 282 | }; 283 | 284 | return template.replace(/%(?:([a-z]+)|(?:{([^}]+)}))/gi, replacer); 285 | } 286 | 287 | format.map = { 288 | "s": "toString" 289 | }; 290 | var util = 291 | /*#__PURE__*/ 292 | Object.freeze({ 293 | mod: mod, 294 | clamp: clamp, 295 | capitalize: capitalize, 296 | format: format 297 | }); 298 | 299 | var Hex = 300 | /*#__PURE__*/ 301 | function (_Canvas) { 302 | _inheritsLoose(Hex, _Canvas); 303 | 304 | function Hex() { 305 | var _this2; 306 | 307 | _this2 = _Canvas.call(this) || this; 308 | _this2._spacingX = 0; 309 | _this2._spacingY = 0; 310 | _this2._hexSize = 0; 311 | return _this2; 312 | } 313 | 314 | var _proto4 = Hex.prototype; 315 | 316 | _proto4.draw = function draw(data, clearBefore) { 317 | var x = data[0], 318 | y = data[1], 319 | ch = data[2], 320 | fg = data[3], 321 | bg = data[4]; 322 | var px = [(x + 1) * this._spacingX, y * this._spacingY + this._hexSize]; 323 | 324 | if (this._options.transpose) { 325 | px.reverse(); 326 | } 327 | 328 | if (clearBefore) { 329 | this._ctx.fillStyle = bg; 330 | 331 | this._fill(px[0], px[1]); 332 | } 333 | 334 | if (!ch) { 335 | return; 336 | } 337 | 338 | this._ctx.fillStyle = fg; 339 | var chars = [].concat(ch); 340 | 341 | for (var i = 0; i < chars.length; i++) { 342 | this._ctx.fillText(chars[i], px[0], Math.ceil(px[1])); 343 | } 344 | }; 345 | 346 | _proto4.computeSize = function computeSize(availWidth, availHeight) { 347 | if (this._options.transpose) { 348 | availWidth += availHeight; 349 | availHeight = availWidth - availHeight; 350 | availWidth -= availHeight; 351 | } 352 | 353 | var width = Math.floor(availWidth / this._spacingX) - 1; 354 | var height = Math.floor((availHeight - 2 * this._hexSize) / this._spacingY + 1); 355 | return [width, height]; 356 | }; 357 | 358 | _proto4.computeFontSize = function computeFontSize(availWidth, availHeight) { 359 | if (this._options.transpose) { 360 | availWidth += availHeight; 361 | availHeight = availWidth - availHeight; 362 | availWidth -= availHeight; 363 | } 364 | 365 | var hexSizeWidth = 2 * availWidth / ((this._options.width + 1) * Math.sqrt(3)) - 1; 366 | var hexSizeHeight = availHeight / (2 + 1.5 * (this._options.height - 1)); 367 | var hexSize = Math.min(hexSizeWidth, hexSizeHeight); 368 | var oldFont = this._ctx.font; 369 | this._ctx.font = "100px " + this._options.fontFamily; 370 | var width = Math.ceil(this._ctx.measureText("W").width); 371 | this._ctx.font = oldFont; 372 | var ratio = width / 100; 373 | hexSize = Math.floor(hexSize) + 1; 374 | var fontSize = 2 * hexSize / (this._options.spacing * (1 + ratio / Math.sqrt(3))); 375 | return Math.ceil(fontSize) - 1; 376 | }; 377 | 378 | _proto4._normalizedEventToPosition = function _normalizedEventToPosition(x, y) { 379 | var nodeSize; 380 | 381 | if (this._options.transpose) { 382 | x += y; 383 | y = x - y; 384 | x -= y; 385 | nodeSize = this._ctx.canvas.width; 386 | } else { 387 | nodeSize = this._ctx.canvas.height; 388 | } 389 | 390 | var size = nodeSize / this._options.height; 391 | y = Math.floor(y / size); 392 | 393 | if (mod(y, 2)) { 394 | x -= this._spacingX; 395 | x = 1 + 2 * Math.floor(x / (2 * this._spacingX)); 396 | } else { 397 | x = 2 * Math.floor(x / (2 * this._spacingX)); 398 | } 399 | 400 | return [x, y]; 401 | }; 402 | 403 | _proto4._fill = function _fill(cx, cy) { 404 | var a = this._hexSize; 405 | var b = this._options.border; 406 | var ctx = this._ctx; 407 | ctx.beginPath(); 408 | 409 | if (this._options.transpose) { 410 | ctx.moveTo(cx - a + b, cy); 411 | ctx.lineTo(cx - a / 2 + b, cy + this._spacingX - b); 412 | ctx.lineTo(cx + a / 2 - b, cy + this._spacingX - b); 413 | ctx.lineTo(cx + a - b, cy); 414 | ctx.lineTo(cx + a / 2 - b, cy - this._spacingX + b); 415 | ctx.lineTo(cx - a / 2 + b, cy - this._spacingX + b); 416 | ctx.lineTo(cx - a + b, cy); 417 | } else { 418 | ctx.moveTo(cx, cy - a + b); 419 | ctx.lineTo(cx + this._spacingX - b, cy - a / 2 + b); 420 | ctx.lineTo(cx + this._spacingX - b, cy + a / 2 - b); 421 | ctx.lineTo(cx, cy + a - b); 422 | ctx.lineTo(cx - this._spacingX + b, cy + a / 2 - b); 423 | ctx.lineTo(cx - this._spacingX + b, cy - a / 2 + b); 424 | ctx.lineTo(cx, cy - a + b); 425 | } 426 | 427 | ctx.fill(); 428 | }; 429 | 430 | _proto4._updateSize = function _updateSize() { 431 | var opts = this._options; 432 | var charWidth = Math.ceil(this._ctx.measureText("W").width); 433 | this._hexSize = Math.floor(opts.spacing * (opts.fontSize + charWidth / Math.sqrt(3)) / 2); 434 | this._spacingX = this._hexSize * Math.sqrt(3) / 2; 435 | this._spacingY = this._hexSize * 1.5; 436 | var xprop; 437 | var yprop; 438 | 439 | if (opts.transpose) { 440 | xprop = "height"; 441 | yprop = "width"; 442 | } else { 443 | xprop = "width"; 444 | yprop = "height"; 445 | } 446 | 447 | this._ctx.canvas[xprop] = Math.ceil((opts.width + 1) * this._spacingX); 448 | this._ctx.canvas[yprop] = Math.ceil((opts.height - 1) * this._spacingY + 2 * this._hexSize); 449 | }; 450 | 451 | return Hex; 452 | }(Canvas); 453 | 454 | var Rect = 455 | /*#__PURE__*/ 456 | function (_Canvas2) { 457 | _inheritsLoose(Rect, _Canvas2); 458 | 459 | function Rect() { 460 | var _this3; 461 | 462 | _this3 = _Canvas2.call(this) || this; 463 | _this3._spacingX = 0; 464 | _this3._spacingY = 0; 465 | _this3._canvasCache = {}; 466 | return _this3; 467 | } 468 | 469 | var _proto5 = Rect.prototype; 470 | 471 | _proto5.setOptions = function setOptions(options) { 472 | _Canvas2.prototype.setOptions.call(this, options); 473 | 474 | this._canvasCache = {}; 475 | }; 476 | 477 | _proto5.draw = function draw(data, clearBefore) { 478 | if (Rect.cache) { 479 | this._drawWithCache(data); 480 | } else { 481 | this._drawNoCache(data, clearBefore); 482 | } 483 | }; 484 | 485 | _proto5._drawWithCache = function _drawWithCache(data) { 486 | var x = data[0], 487 | y = data[1], 488 | ch = data[2], 489 | fg = data[3], 490 | bg = data[4]; 491 | var hash = "" + ch + fg + bg; 492 | var canvas; 493 | 494 | if (hash in this._canvasCache) { 495 | canvas = this._canvasCache[hash]; 496 | } else { 497 | var b = this._options.border; 498 | canvas = document.createElement("canvas"); 499 | var ctx = canvas.getContext("2d"); 500 | canvas.width = this._spacingX; 501 | canvas.height = this._spacingY; 502 | ctx.fillStyle = bg; 503 | ctx.fillRect(b, b, canvas.width - b, canvas.height - b); 504 | 505 | if (ch) { 506 | ctx.fillStyle = fg; 507 | ctx.font = this._ctx.font; 508 | ctx.textAlign = "center"; 509 | ctx.textBaseline = "middle"; 510 | var chars = [].concat(ch); 511 | 512 | for (var i = 0; i < chars.length; i++) { 513 | ctx.fillText(chars[i], this._spacingX / 2, Math.ceil(this._spacingY / 2)); 514 | } 515 | } 516 | 517 | this._canvasCache[hash] = canvas; 518 | } 519 | 520 | this._ctx.drawImage(canvas, x * this._spacingX, y * this._spacingY); 521 | }; 522 | 523 | _proto5._drawNoCache = function _drawNoCache(data, clearBefore) { 524 | var x = data[0], 525 | y = data[1], 526 | ch = data[2], 527 | fg = data[3], 528 | bg = data[4]; 529 | 530 | if (clearBefore) { 531 | var b = this._options.border; 532 | this._ctx.fillStyle = bg; 533 | 534 | this._ctx.fillRect(x * this._spacingX + b, y * this._spacingY + b, this._spacingX - b, this._spacingY - b); 535 | } 536 | 537 | if (!ch) { 538 | return; 539 | } 540 | 541 | this._ctx.fillStyle = fg; 542 | var chars = [].concat(ch); 543 | 544 | for (var i = 0; i < chars.length; i++) { 545 | this._ctx.fillText(chars[i], (x + 0.5) * this._spacingX, Math.ceil((y + 0.5) * this._spacingY)); 546 | } 547 | }; 548 | 549 | _proto5.computeSize = function computeSize(availWidth, availHeight) { 550 | var width = Math.floor(availWidth / this._spacingX); 551 | var height = Math.floor(availHeight / this._spacingY); 552 | return [width, height]; 553 | }; 554 | 555 | _proto5.computeFontSize = function computeFontSize(availWidth, availHeight) { 556 | var boxWidth = Math.floor(availWidth / this._options.width); 557 | var boxHeight = Math.floor(availHeight / this._options.height); 558 | var oldFont = this._ctx.font; 559 | this._ctx.font = "100px " + this._options.fontFamily; 560 | var width = Math.ceil(this._ctx.measureText("W").width); 561 | this._ctx.font = oldFont; 562 | var ratio = width / 100; 563 | var widthFraction = ratio * boxHeight / boxWidth; 564 | 565 | if (widthFraction > 1) { 566 | boxHeight = Math.floor(boxHeight / widthFraction); 567 | } 568 | 569 | return Math.floor(boxHeight / this._options.spacing); 570 | }; 571 | 572 | _proto5._normalizedEventToPosition = function _normalizedEventToPosition(x, y) { 573 | return [Math.floor(x / this._spacingX), Math.floor(y / this._spacingY)]; 574 | }; 575 | 576 | _proto5._updateSize = function _updateSize() { 577 | var opts = this._options; 578 | var charWidth = Math.ceil(this._ctx.measureText("W").width); 579 | this._spacingX = Math.ceil(opts.spacing * charWidth); 580 | this._spacingY = Math.ceil(opts.spacing * opts.fontSize); 581 | 582 | if (opts.forceSquareRatio) { 583 | this._spacingX = this._spacingY = Math.max(this._spacingX, this._spacingY); 584 | } 585 | 586 | this._ctx.canvas.width = opts.width * this._spacingX; 587 | this._ctx.canvas.height = opts.height * this._spacingY; 588 | }; 589 | 590 | return Rect; 591 | }(Canvas); 592 | 593 | Rect.cache = false; 594 | 595 | var Tile = 596 | /*#__PURE__*/ 597 | function (_Canvas3) { 598 | _inheritsLoose(Tile, _Canvas3); 599 | 600 | function Tile() { 601 | var _this4; 602 | 603 | _this4 = _Canvas3.call(this) || this; 604 | _this4._colorCanvas = document.createElement("canvas"); 605 | return _this4; 606 | } 607 | 608 | var _proto6 = Tile.prototype; 609 | 610 | _proto6.draw = function draw(data, clearBefore) { 611 | var x = data[0], 612 | y = data[1], 613 | ch = data[2], 614 | fg = data[3], 615 | bg = data[4]; 616 | var tileWidth = this._options.tileWidth; 617 | var tileHeight = this._options.tileHeight; 618 | 619 | if (clearBefore) { 620 | if (this._options.tileColorize) { 621 | this._ctx.clearRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight); 622 | } else { 623 | this._ctx.fillStyle = bg; 624 | 625 | this._ctx.fillRect(x * tileWidth, y * tileHeight, tileWidth, tileHeight); 626 | } 627 | } 628 | 629 | if (!ch) { 630 | return; 631 | } 632 | 633 | var chars = [].concat(ch); 634 | var fgs = [].concat(fg); 635 | var bgs = [].concat(bg); 636 | 637 | for (var i = 0; i < chars.length; i++) { 638 | var tile = this._options.tileMap[chars[i]]; 639 | 640 | if (!tile) { 641 | throw new Error("Char \"" + chars[i] + "\" not found in tileMap"); 642 | } 643 | 644 | if (this._options.tileColorize) { 645 | var canvas = this._colorCanvas; 646 | var context = canvas.getContext("2d"); 647 | context.globalCompositeOperation = "source-over"; 648 | context.clearRect(0, 0, tileWidth, tileHeight); 649 | var _fg = fgs[i]; 650 | var _bg = bgs[i]; 651 | context.drawImage(this._options.tileSet, tile[0], tile[1], tileWidth, tileHeight, 0, 0, tileWidth, tileHeight); 652 | 653 | if (_fg != "transparent") { 654 | context.fillStyle = _fg; 655 | context.globalCompositeOperation = "source-atop"; 656 | context.fillRect(0, 0, tileWidth, tileHeight); 657 | } 658 | 659 | if (_bg != "transparent") { 660 | context.fillStyle = _bg; 661 | context.globalCompositeOperation = "destination-over"; 662 | context.fillRect(0, 0, tileWidth, tileHeight); 663 | } 664 | 665 | this._ctx.drawImage(canvas, x * tileWidth, y * tileHeight, tileWidth, tileHeight); 666 | } else { 667 | this._ctx.drawImage(this._options.tileSet, tile[0], tile[1], tileWidth, tileHeight, x * tileWidth, y * tileHeight, tileWidth, tileHeight); 668 | } 669 | } 670 | }; 671 | 672 | _proto6.computeSize = function computeSize(availWidth, availHeight) { 673 | var width = Math.floor(availWidth / this._options.tileWidth); 674 | var height = Math.floor(availHeight / this._options.tileHeight); 675 | return [width, height]; 676 | }; 677 | 678 | _proto6.computeFontSize = function computeFontSize() { 679 | throw new Error("Tile backend does not understand font size"); 680 | }; 681 | 682 | _proto6._normalizedEventToPosition = function _normalizedEventToPosition(x, y) { 683 | return [Math.floor(x / this._options.tileWidth), Math.floor(y / this._options.tileHeight)]; 684 | }; 685 | 686 | _proto6._updateSize = function _updateSize() { 687 | var opts = this._options; 688 | this._ctx.canvas.width = opts.width * opts.tileWidth; 689 | this._ctx.canvas.height = opts.height * opts.tileHeight; 690 | this._colorCanvas.width = opts.tileWidth; 691 | this._colorCanvas.height = opts.tileHeight; 692 | }; 693 | 694 | return Tile; 695 | }(Canvas); 696 | 697 | function fromString(str) { 698 | var cached, r; 699 | 700 | if (str in CACHE) { 701 | cached = CACHE[str]; 702 | } else { 703 | if (str.charAt(0) == "#") { 704 | var matched = str.match(/[0-9a-f]/gi) || []; 705 | var values = matched.map(function (x) { 706 | return parseInt(x, 16); 707 | }); 708 | 709 | if (values.length == 3) { 710 | cached = values.map(function (x) { 711 | return x * 17; 712 | }); 713 | } else { 714 | for (var i = 0; i < 3; i++) { 715 | values[i + 1] += 16 * values[i]; 716 | values.splice(i, 1); 717 | } 718 | 719 | cached = values; 720 | } 721 | } else if (r = str.match(/rgb\(([0-9, ]+)\)/i)) { 722 | cached = r[1].split(/\s*,\s*/).map(function (x) { 723 | return parseInt(x); 724 | }); 725 | } else { 726 | cached = [0, 0, 0]; 727 | } 728 | 729 | CACHE[str] = cached; 730 | } 731 | 732 | return cached.slice(); 733 | } 734 | 735 | function add(color1) { 736 | var result = color1.slice(); 737 | 738 | for (var _len2 = arguments.length, colors = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { 739 | colors[_key2 - 1] = arguments[_key2]; 740 | } 741 | 742 | for (var i = 0; i < 3; i++) { 743 | for (var j = 0; j < colors.length; j++) { 744 | result[i] += colors[j][i]; 745 | } 746 | } 747 | 748 | return result; 749 | } 750 | 751 | function add_(color1) { 752 | for (var _len3 = arguments.length, colors = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { 753 | colors[_key3 - 1] = arguments[_key3]; 754 | } 755 | 756 | for (var i = 0; i < 3; i++) { 757 | for (var j = 0; j < colors.length; j++) { 758 | color1[i] += colors[j][i]; 759 | } 760 | } 761 | 762 | return color1; 763 | } 764 | 765 | function multiply(color1) { 766 | var result = color1.slice(); 767 | 768 | for (var _len4 = arguments.length, colors = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { 769 | colors[_key4 - 1] = arguments[_key4]; 770 | } 771 | 772 | for (var i = 0; i < 3; i++) { 773 | for (var j = 0; j < colors.length; j++) { 774 | result[i] *= colors[j][i] / 255; 775 | } 776 | 777 | result[i] = Math.round(result[i]); 778 | } 779 | 780 | return result; 781 | } 782 | 783 | function multiply_(color1) { 784 | for (var _len5 = arguments.length, colors = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) { 785 | colors[_key5 - 1] = arguments[_key5]; 786 | } 787 | 788 | for (var i = 0; i < 3; i++) { 789 | for (var j = 0; j < colors.length; j++) { 790 | color1[i] *= colors[j][i] / 255; 791 | } 792 | 793 | color1[i] = Math.round(color1[i]); 794 | } 795 | 796 | return color1; 797 | } 798 | 799 | function interpolate(color1, color2, factor) { 800 | if (factor === void 0) { 801 | factor = 0.5; 802 | } 803 | 804 | var result = color1.slice(); 805 | 806 | for (var i = 0; i < 3; i++) { 807 | result[i] = Math.round(result[i] + factor * (color2[i] - color1[i])); 808 | } 809 | 810 | return result; 811 | } 812 | 813 | var lerp = interpolate; 814 | 815 | function interpolateHSL(color1, color2, factor) { 816 | if (factor === void 0) { 817 | factor = 0.5; 818 | } 819 | 820 | var hsl1 = rgb2hsl(color1); 821 | var hsl2 = rgb2hsl(color2); 822 | 823 | for (var i = 0; i < 3; i++) { 824 | hsl1[i] += factor * (hsl2[i] - hsl1[i]); 825 | } 826 | 827 | return hsl2rgb(hsl1); 828 | } 829 | 830 | var lerpHSL = interpolateHSL; 831 | 832 | function randomize(color, diff) { 833 | if (!(diff instanceof Array)) { 834 | diff = Math.round(RNG$1.getNormal(0, diff)); 835 | } 836 | 837 | var result = color.slice(); 838 | 839 | for (var i = 0; i < 3; i++) { 840 | result[i] += diff instanceof Array ? Math.round(RNG$1.getNormal(0, diff[i])) : diff; 841 | } 842 | 843 | return result; 844 | } 845 | 846 | function rgb2hsl(color) { 847 | var r = color[0] / 255; 848 | var g = color[1] / 255; 849 | var b = color[2] / 255; 850 | var max = Math.max(r, g, b), 851 | min = Math.min(r, g, b); 852 | var h = 0, 853 | s, 854 | l = (max + min) / 2; 855 | 856 | if (max == min) { 857 | s = 0; 858 | } else { 859 | var d = max - min; 860 | s = l > 0.5 ? d / (2 - max - min) : d / (max + min); 861 | 862 | switch (max) { 863 | case r: 864 | h = (g - b) / d + (g < b ? 6 : 0); 865 | break; 866 | 867 | case g: 868 | h = (b - r) / d + 2; 869 | break; 870 | 871 | case b: 872 | h = (r - g) / d + 4; 873 | break; 874 | } 875 | 876 | h /= 6; 877 | } 878 | 879 | return [h, s, l]; 880 | } 881 | 882 | function hue2rgb(p, q, t) { 883 | if (t < 0) t += 1; 884 | if (t > 1) t -= 1; 885 | if (t < 1 / 6) return p + (q - p) * 6 * t; 886 | if (t < 1 / 2) return q; 887 | if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; 888 | return p; 889 | } 890 | 891 | function hsl2rgb(color) { 892 | var l = color[2]; 893 | 894 | if (color[1] == 0) { 895 | l = Math.round(l * 255); 896 | return [l, l, l]; 897 | } else { 898 | var s = color[1]; 899 | var q = l < 0.5 ? l * (1 + s) : l + s - l * s; 900 | var p = 2 * l - q; 901 | var r = hue2rgb(p, q, color[0] + 1 / 3); 902 | var g = hue2rgb(p, q, color[0]); 903 | var b = hue2rgb(p, q, color[0] - 1 / 3); 904 | return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]; 905 | } 906 | } 907 | 908 | function toRGB(color) { 909 | var clamped = color.map(function (x) { 910 | return clamp(x, 0, 255); 911 | }); 912 | return "rgb(" + clamped.join(",") + ")"; 913 | } 914 | 915 | function toHex(color) { 916 | var clamped = color.map(function (x) { 917 | return clamp(x, 0, 255).toString(16).padStart(2, "0"); 918 | }); 919 | return "#" + clamped.join(""); 920 | } 921 | 922 | var CACHE = { 923 | "black": [0, 0, 0], 924 | "navy": [0, 0, 128], 925 | "darkblue": [0, 0, 139], 926 | "mediumblue": [0, 0, 205], 927 | "blue": [0, 0, 255], 928 | "darkgreen": [0, 100, 0], 929 | "green": [0, 128, 0], 930 | "teal": [0, 128, 128], 931 | "darkcyan": [0, 139, 139], 932 | "deepskyblue": [0, 191, 255], 933 | "darkturquoise": [0, 206, 209], 934 | "mediumspringgreen": [0, 250, 154], 935 | "lime": [0, 255, 0], 936 | "springgreen": [0, 255, 127], 937 | "aqua": [0, 255, 255], 938 | "cyan": [0, 255, 255], 939 | "midnightblue": [25, 25, 112], 940 | "dodgerblue": [30, 144, 255], 941 | "forestgreen": [34, 139, 34], 942 | "seagreen": [46, 139, 87], 943 | "darkslategray": [47, 79, 79], 944 | "darkslategrey": [47, 79, 79], 945 | "limegreen": [50, 205, 50], 946 | "mediumseagreen": [60, 179, 113], 947 | "turquoise": [64, 224, 208], 948 | "royalblue": [65, 105, 225], 949 | "steelblue": [70, 130, 180], 950 | "darkslateblue": [72, 61, 139], 951 | "mediumturquoise": [72, 209, 204], 952 | "indigo": [75, 0, 130], 953 | "darkolivegreen": [85, 107, 47], 954 | "cadetblue": [95, 158, 160], 955 | "cornflowerblue": [100, 149, 237], 956 | "mediumaquamarine": [102, 205, 170], 957 | "dimgray": [105, 105, 105], 958 | "dimgrey": [105, 105, 105], 959 | "slateblue": [106, 90, 205], 960 | "olivedrab": [107, 142, 35], 961 | "slategray": [112, 128, 144], 962 | "slategrey": [112, 128, 144], 963 | "lightslategray": [119, 136, 153], 964 | "lightslategrey": [119, 136, 153], 965 | "mediumslateblue": [123, 104, 238], 966 | "lawngreen": [124, 252, 0], 967 | "chartreuse": [127, 255, 0], 968 | "aquamarine": [127, 255, 212], 969 | "maroon": [128, 0, 0], 970 | "purple": [128, 0, 128], 971 | "olive": [128, 128, 0], 972 | "gray": [128, 128, 128], 973 | "grey": [128, 128, 128], 974 | "skyblue": [135, 206, 235], 975 | "lightskyblue": [135, 206, 250], 976 | "blueviolet": [138, 43, 226], 977 | "darkred": [139, 0, 0], 978 | "darkmagenta": [139, 0, 139], 979 | "saddlebrown": [139, 69, 19], 980 | "darkseagreen": [143, 188, 143], 981 | "lightgreen": [144, 238, 144], 982 | "mediumpurple": [147, 112, 216], 983 | "darkviolet": [148, 0, 211], 984 | "palegreen": [152, 251, 152], 985 | "darkorchid": [153, 50, 204], 986 | "yellowgreen": [154, 205, 50], 987 | "sienna": [160, 82, 45], 988 | "brown": [165, 42, 42], 989 | "darkgray": [169, 169, 169], 990 | "darkgrey": [169, 169, 169], 991 | "lightblue": [173, 216, 230], 992 | "greenyellow": [173, 255, 47], 993 | "paleturquoise": [175, 238, 238], 994 | "lightsteelblue": [176, 196, 222], 995 | "powderblue": [176, 224, 230], 996 | "firebrick": [178, 34, 34], 997 | "darkgoldenrod": [184, 134, 11], 998 | "mediumorchid": [186, 85, 211], 999 | "rosybrown": [188, 143, 143], 1000 | "darkkhaki": [189, 183, 107], 1001 | "silver": [192, 192, 192], 1002 | "mediumvioletred": [199, 21, 133], 1003 | "indianred": [205, 92, 92], 1004 | "peru": [205, 133, 63], 1005 | "chocolate": [210, 105, 30], 1006 | "tan": [210, 180, 140], 1007 | "lightgray": [211, 211, 211], 1008 | "lightgrey": [211, 211, 211], 1009 | "palevioletred": [216, 112, 147], 1010 | "thistle": [216, 191, 216], 1011 | "orchid": [218, 112, 214], 1012 | "goldenrod": [218, 165, 32], 1013 | "crimson": [220, 20, 60], 1014 | "gainsboro": [220, 220, 220], 1015 | "plum": [221, 160, 221], 1016 | "burlywood": [222, 184, 135], 1017 | "lightcyan": [224, 255, 255], 1018 | "lavender": [230, 230, 250], 1019 | "darksalmon": [233, 150, 122], 1020 | "violet": [238, 130, 238], 1021 | "palegoldenrod": [238, 232, 170], 1022 | "lightcoral": [240, 128, 128], 1023 | "khaki": [240, 230, 140], 1024 | "aliceblue": [240, 248, 255], 1025 | "honeydew": [240, 255, 240], 1026 | "azure": [240, 255, 255], 1027 | "sandybrown": [244, 164, 96], 1028 | "wheat": [245, 222, 179], 1029 | "beige": [245, 245, 220], 1030 | "whitesmoke": [245, 245, 245], 1031 | "mintcream": [245, 255, 250], 1032 | "ghostwhite": [248, 248, 255], 1033 | "salmon": [250, 128, 114], 1034 | "antiquewhite": [250, 235, 215], 1035 | "linen": [250, 240, 230], 1036 | "lightgoldenrodyellow": [250, 250, 210], 1037 | "oldlace": [253, 245, 230], 1038 | "red": [255, 0, 0], 1039 | "fuchsia": [255, 0, 255], 1040 | "magenta": [255, 0, 255], 1041 | "deeppink": [255, 20, 147], 1042 | "orangered": [255, 69, 0], 1043 | "tomato": [255, 99, 71], 1044 | "hotpink": [255, 105, 180], 1045 | "coral": [255, 127, 80], 1046 | "darkorange": [255, 140, 0], 1047 | "lightsalmon": [255, 160, 122], 1048 | "orange": [255, 165, 0], 1049 | "lightpink": [255, 182, 193], 1050 | "pink": [255, 192, 203], 1051 | "gold": [255, 215, 0], 1052 | "peachpuff": [255, 218, 185], 1053 | "navajowhite": [255, 222, 173], 1054 | "moccasin": [255, 228, 181], 1055 | "bisque": [255, 228, 196], 1056 | "mistyrose": [255, 228, 225], 1057 | "blanchedalmond": [255, 235, 205], 1058 | "papayawhip": [255, 239, 213], 1059 | "lavenderblush": [255, 240, 245], 1060 | "seashell": [255, 245, 238], 1061 | "cornsilk": [255, 248, 220], 1062 | "lemonchiffon": [255, 250, 205], 1063 | "floralwhite": [255, 250, 240], 1064 | "snow": [255, 250, 250], 1065 | "yellow": [255, 255, 0], 1066 | "lightyellow": [255, 255, 224], 1067 | "ivory": [255, 255, 240], 1068 | "white": [255, 255, 255] 1069 | }; 1070 | var color = 1071 | /*#__PURE__*/ 1072 | Object.freeze({ 1073 | fromString: fromString, 1074 | add: add, 1075 | add_: add_, 1076 | multiply: multiply, 1077 | multiply_: multiply_, 1078 | interpolate: interpolate, 1079 | lerp: lerp, 1080 | interpolateHSL: interpolateHSL, 1081 | lerpHSL: lerpHSL, 1082 | randomize: randomize, 1083 | rgb2hsl: rgb2hsl, 1084 | hsl2rgb: hsl2rgb, 1085 | toRGB: toRGB, 1086 | toHex: toHex 1087 | }); 1088 | 1089 | function clearToAnsi(bg) { 1090 | return "\x1B[0;48;5;" + termcolor(bg) + "m\x1B[2J"; 1091 | } 1092 | 1093 | function colorToAnsi(fg, bg) { 1094 | return "\x1B[0;38;5;" + termcolor(fg) + ";48;5;" + termcolor(bg) + "m"; 1095 | } 1096 | 1097 | function positionToAnsi(x, y) { 1098 | return "\x1B[" + (y + 1) + ";" + (x + 1) + "H"; 1099 | } 1100 | 1101 | function termcolor(color) { 1102 | var SRC_COLORS = 256.0; 1103 | var DST_COLORS = 6.0; 1104 | var COLOR_RATIO = DST_COLORS / SRC_COLORS; 1105 | var rgb = fromString(color); 1106 | var r = Math.floor(rgb[0] * COLOR_RATIO); 1107 | var g = Math.floor(rgb[1] * COLOR_RATIO); 1108 | var b = Math.floor(rgb[2] * COLOR_RATIO); 1109 | return r * 36 + g * 6 + b * 1 + 16; 1110 | } 1111 | 1112 | var Term = 1113 | /*#__PURE__*/ 1114 | function (_Backend2) { 1115 | _inheritsLoose(Term, _Backend2); 1116 | 1117 | function Term() { 1118 | var _this5; 1119 | 1120 | _this5 = _Backend2.call(this) || this; 1121 | _this5._offset = [0, 0]; 1122 | _this5._cursor = [-1, -1]; 1123 | _this5._lastColor = ""; 1124 | return _this5; 1125 | } 1126 | 1127 | var _proto7 = Term.prototype; 1128 | 1129 | _proto7.schedule = function schedule(cb) { 1130 | setTimeout(cb, 1000 / 60); 1131 | }; 1132 | 1133 | _proto7.setOptions = function setOptions(options) { 1134 | _Backend2.prototype.setOptions.call(this, options); 1135 | 1136 | var size = [options.width, options.height]; 1137 | var avail = this.computeSize(); 1138 | this._offset = avail.map(function (val, index) { 1139 | return Math.floor((val - size[index]) / 2); 1140 | }); 1141 | }; 1142 | 1143 | _proto7.clear = function clear() { 1144 | process.stdout.write(clearToAnsi(this._options.bg)); 1145 | }; 1146 | 1147 | _proto7.draw = function draw(data, clearBefore) { 1148 | var x = data[0], 1149 | y = data[1], 1150 | ch = data[2], 1151 | fg = data[3], 1152 | bg = data[4]; 1153 | var dx = this._offset[0] + x; 1154 | var dy = this._offset[1] + y; 1155 | var size = this.computeSize(); 1156 | 1157 | if (dx < 0 || dx >= size[0]) { 1158 | return; 1159 | } 1160 | 1161 | if (dy < 0 || dy >= size[1]) { 1162 | return; 1163 | } 1164 | 1165 | if (dx !== this._cursor[0] || dy !== this._cursor[1]) { 1166 | process.stdout.write(positionToAnsi(dx, dy)); 1167 | this._cursor[0] = dx; 1168 | this._cursor[1] = dy; 1169 | } 1170 | 1171 | if (clearBefore) { 1172 | if (!ch) { 1173 | ch = " "; 1174 | } 1175 | } 1176 | 1177 | if (!ch) { 1178 | return; 1179 | } 1180 | 1181 | var newColor = colorToAnsi(fg, bg); 1182 | 1183 | if (newColor !== this._lastColor) { 1184 | process.stdout.write(newColor); 1185 | this._lastColor = newColor; 1186 | } 1187 | 1188 | var chars = [].concat(ch); 1189 | process.stdout.write(chars[0]); 1190 | this._cursor[0]++; 1191 | 1192 | if (this._cursor[0] >= size[0]) { 1193 | this._cursor[0] = 0; 1194 | this._cursor[1]++; 1195 | } 1196 | }; 1197 | 1198 | _proto7.computeFontSize = function computeFontSize() { 1199 | throw new Error("Terminal backend has no notion of font size"); 1200 | }; 1201 | 1202 | _proto7.eventToPosition = function eventToPosition(x, y) { 1203 | return [x, y]; 1204 | }; 1205 | 1206 | _proto7.computeSize = function computeSize() { 1207 | return [process.stdout.columns, process.stdout.rows]; 1208 | }; 1209 | 1210 | return Term; 1211 | }(Backend); 1212 | 1213 | var RE_COLORS = /%([bc]){([^}]*)}/g; 1214 | var TYPE_TEXT = 0; 1215 | var TYPE_NEWLINE = 1; 1216 | var TYPE_FG = 2; 1217 | var TYPE_BG = 3; 1218 | 1219 | function measure(str, maxWidth) { 1220 | var result = { 1221 | width: 0, 1222 | height: 1 1223 | }; 1224 | var tokens = tokenize(str, maxWidth); 1225 | var lineWidth = 0; 1226 | 1227 | for (var i = 0; i < tokens.length; i++) { 1228 | var token = tokens[i]; 1229 | 1230 | switch (token.type) { 1231 | case TYPE_TEXT: 1232 | lineWidth += token.value.length; 1233 | break; 1234 | 1235 | case TYPE_NEWLINE: 1236 | result.height++; 1237 | result.width = Math.max(result.width, lineWidth); 1238 | lineWidth = 0; 1239 | break; 1240 | } 1241 | } 1242 | 1243 | result.width = Math.max(result.width, lineWidth); 1244 | return result; 1245 | } 1246 | 1247 | function tokenize(str, maxWidth) { 1248 | var result = []; 1249 | var offset = 0; 1250 | str.replace(RE_COLORS, function (match, type, name, index) { 1251 | var part = str.substring(offset, index); 1252 | 1253 | if (part.length) { 1254 | result.push({ 1255 | type: TYPE_TEXT, 1256 | value: part 1257 | }); 1258 | } 1259 | 1260 | result.push({ 1261 | type: type == "c" ? TYPE_FG : TYPE_BG, 1262 | value: name.trim() 1263 | }); 1264 | offset = index + match.length; 1265 | return ""; 1266 | }); 1267 | var part = str.substring(offset); 1268 | 1269 | if (part.length) { 1270 | result.push({ 1271 | type: TYPE_TEXT, 1272 | value: part 1273 | }); 1274 | } 1275 | 1276 | return breakLines(result, maxWidth); 1277 | } 1278 | 1279 | function breakLines(tokens, maxWidth) { 1280 | if (!maxWidth) { 1281 | maxWidth = Infinity; 1282 | } 1283 | 1284 | var i = 0; 1285 | var lineLength = 0; 1286 | var lastTokenWithSpace = -1; 1287 | 1288 | while (i < tokens.length) { 1289 | var token = tokens[i]; 1290 | 1291 | if (token.type == TYPE_NEWLINE) { 1292 | lineLength = 0; 1293 | lastTokenWithSpace = -1; 1294 | } 1295 | 1296 | if (token.type != TYPE_TEXT) { 1297 | i++; 1298 | continue; 1299 | } 1300 | 1301 | while (lineLength == 0 && token.value.charAt(0) == " ") { 1302 | token.value = token.value.substring(1); 1303 | } 1304 | 1305 | var _index2 = token.value.indexOf("\n"); 1306 | 1307 | if (_index2 != -1) { 1308 | token.value = breakInsideToken(tokens, i, _index2, true); 1309 | var arr = token.value.split(""); 1310 | 1311 | while (arr.length && arr[arr.length - 1] == " ") { 1312 | arr.pop(); 1313 | } 1314 | 1315 | token.value = arr.join(""); 1316 | } 1317 | 1318 | if (!token.value.length) { 1319 | tokens.splice(i, 1); 1320 | continue; 1321 | } 1322 | 1323 | if (lineLength + token.value.length > maxWidth) { 1324 | var _index3 = -1; 1325 | 1326 | while (1) { 1327 | var nextIndex = token.value.indexOf(" ", _index3 + 1); 1328 | 1329 | if (nextIndex == -1) { 1330 | break; 1331 | } 1332 | 1333 | if (lineLength + nextIndex > maxWidth) { 1334 | break; 1335 | } 1336 | 1337 | _index3 = nextIndex; 1338 | } 1339 | 1340 | if (_index3 != -1) { 1341 | token.value = breakInsideToken(tokens, i, _index3, true); 1342 | } else if (lastTokenWithSpace != -1) { 1343 | var _token = tokens[lastTokenWithSpace]; 1344 | 1345 | var breakIndex = _token.value.lastIndexOf(" "); 1346 | 1347 | _token.value = breakInsideToken(tokens, lastTokenWithSpace, breakIndex, true); 1348 | i = lastTokenWithSpace; 1349 | } else { 1350 | token.value = breakInsideToken(tokens, i, maxWidth - lineLength, false); 1351 | } 1352 | } else { 1353 | lineLength += token.value.length; 1354 | 1355 | if (token.value.indexOf(" ") != -1) { 1356 | lastTokenWithSpace = i; 1357 | } 1358 | } 1359 | 1360 | i++; 1361 | } 1362 | 1363 | tokens.push({ 1364 | type: TYPE_NEWLINE 1365 | }); 1366 | var lastTextToken = null; 1367 | 1368 | for (var _i = 0; _i < tokens.length; _i++) { 1369 | var _token2 = tokens[_i]; 1370 | 1371 | switch (_token2.type) { 1372 | case TYPE_TEXT: 1373 | lastTextToken = _token2; 1374 | break; 1375 | 1376 | case TYPE_NEWLINE: 1377 | if (lastTextToken) { 1378 | var _arr = lastTextToken.value.split(""); 1379 | 1380 | while (_arr.length && _arr[_arr.length - 1] == " ") { 1381 | _arr.pop(); 1382 | } 1383 | 1384 | lastTextToken.value = _arr.join(""); 1385 | } 1386 | 1387 | lastTextToken = null; 1388 | break; 1389 | } 1390 | } 1391 | 1392 | tokens.pop(); 1393 | return tokens; 1394 | } 1395 | 1396 | function breakInsideToken(tokens, tokenIndex, breakIndex, removeBreakChar) { 1397 | var newBreakToken = { 1398 | type: TYPE_NEWLINE 1399 | }; 1400 | var newTextToken = { 1401 | type: TYPE_TEXT, 1402 | value: tokens[tokenIndex].value.substring(breakIndex + (removeBreakChar ? 1 : 0)) 1403 | }; 1404 | tokens.splice(tokenIndex + 1, 0, newBreakToken, newTextToken); 1405 | return tokens[tokenIndex].value.substring(0, breakIndex); 1406 | } 1407 | 1408 | var text = 1409 | /*#__PURE__*/ 1410 | Object.freeze({ 1411 | TYPE_TEXT: TYPE_TEXT, 1412 | TYPE_NEWLINE: TYPE_NEWLINE, 1413 | TYPE_FG: TYPE_FG, 1414 | TYPE_BG: TYPE_BG, 1415 | measure: measure, 1416 | tokenize: tokenize 1417 | }); 1418 | var DEFAULT_WIDTH = 80; 1419 | var DEFAULT_HEIGHT = 25; 1420 | var DIRS = { 1421 | 4: [[0, -1], [1, 0], [0, 1], [-1, 0]], 1422 | 8: [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]], 1423 | 6: [[-1, -1], [1, -1], [2, 0], [1, 1], [-1, 1], [-2, 0]] 1424 | }; 1425 | var KEYS = { 1426 | VK_CANCEL: 3, 1427 | VK_HELP: 6, 1428 | VK_BACK_SPACE: 8, 1429 | VK_TAB: 9, 1430 | VK_CLEAR: 12, 1431 | VK_RETURN: 13, 1432 | VK_ENTER: 14, 1433 | VK_SHIFT: 16, 1434 | VK_CONTROL: 17, 1435 | VK_ALT: 18, 1436 | VK_PAUSE: 19, 1437 | VK_CAPS_LOCK: 20, 1438 | VK_ESCAPE: 27, 1439 | VK_SPACE: 32, 1440 | VK_PAGE_UP: 33, 1441 | VK_PAGE_DOWN: 34, 1442 | VK_END: 35, 1443 | VK_HOME: 36, 1444 | VK_LEFT: 37, 1445 | VK_UP: 38, 1446 | VK_RIGHT: 39, 1447 | VK_DOWN: 40, 1448 | VK_PRINTSCREEN: 44, 1449 | VK_INSERT: 45, 1450 | VK_DELETE: 46, 1451 | VK_0: 48, 1452 | VK_1: 49, 1453 | VK_2: 50, 1454 | VK_3: 51, 1455 | VK_4: 52, 1456 | VK_5: 53, 1457 | VK_6: 54, 1458 | VK_7: 55, 1459 | VK_8: 56, 1460 | VK_9: 57, 1461 | VK_COLON: 58, 1462 | VK_SEMICOLON: 59, 1463 | VK_LESS_THAN: 60, 1464 | VK_EQUALS: 61, 1465 | VK_GREATER_THAN: 62, 1466 | VK_QUESTION_MARK: 63, 1467 | VK_AT: 64, 1468 | VK_A: 65, 1469 | VK_B: 66, 1470 | VK_C: 67, 1471 | VK_D: 68, 1472 | VK_E: 69, 1473 | VK_F: 70, 1474 | VK_G: 71, 1475 | VK_H: 72, 1476 | VK_I: 73, 1477 | VK_J: 74, 1478 | VK_K: 75, 1479 | VK_L: 76, 1480 | VK_M: 77, 1481 | VK_N: 78, 1482 | VK_O: 79, 1483 | VK_P: 80, 1484 | VK_Q: 81, 1485 | VK_R: 82, 1486 | VK_S: 83, 1487 | VK_T: 84, 1488 | VK_U: 85, 1489 | VK_V: 86, 1490 | VK_W: 87, 1491 | VK_X: 88, 1492 | VK_Y: 89, 1493 | VK_Z: 90, 1494 | VK_CONTEXT_MENU: 93, 1495 | VK_NUMPAD0: 96, 1496 | VK_NUMPAD1: 97, 1497 | VK_NUMPAD2: 98, 1498 | VK_NUMPAD3: 99, 1499 | VK_NUMPAD4: 100, 1500 | VK_NUMPAD5: 101, 1501 | VK_NUMPAD6: 102, 1502 | VK_NUMPAD7: 103, 1503 | VK_NUMPAD8: 104, 1504 | VK_NUMPAD9: 105, 1505 | VK_MULTIPLY: 106, 1506 | VK_ADD: 107, 1507 | VK_SEPARATOR: 108, 1508 | VK_SUBTRACT: 109, 1509 | VK_DECIMAL: 110, 1510 | VK_DIVIDE: 111, 1511 | VK_F1: 112, 1512 | VK_F2: 113, 1513 | VK_F3: 114, 1514 | VK_F4: 115, 1515 | VK_F5: 116, 1516 | VK_F6: 117, 1517 | VK_F7: 118, 1518 | VK_F8: 119, 1519 | VK_F9: 120, 1520 | VK_F10: 121, 1521 | VK_F11: 122, 1522 | VK_F12: 123, 1523 | VK_F13: 124, 1524 | VK_F14: 125, 1525 | VK_F15: 126, 1526 | VK_F16: 127, 1527 | VK_F17: 128, 1528 | VK_F18: 129, 1529 | VK_F19: 130, 1530 | VK_F20: 131, 1531 | VK_F21: 132, 1532 | VK_F22: 133, 1533 | VK_F23: 134, 1534 | VK_F24: 135, 1535 | VK_NUM_LOCK: 144, 1536 | VK_SCROLL_LOCK: 145, 1537 | VK_CIRCUMFLEX: 160, 1538 | VK_EXCLAMATION: 161, 1539 | VK_DOUBLE_QUOTE: 162, 1540 | VK_HASH: 163, 1541 | VK_DOLLAR: 164, 1542 | VK_PERCENT: 165, 1543 | VK_AMPERSAND: 166, 1544 | VK_UNDERSCORE: 167, 1545 | VK_OPEN_PAREN: 168, 1546 | VK_CLOSE_PAREN: 169, 1547 | VK_ASTERISK: 170, 1548 | VK_PLUS: 171, 1549 | VK_PIPE: 172, 1550 | VK_HYPHEN_MINUS: 173, 1551 | VK_OPEN_CURLY_BRACKET: 174, 1552 | VK_CLOSE_CURLY_BRACKET: 175, 1553 | VK_TILDE: 176, 1554 | VK_COMMA: 188, 1555 | VK_PERIOD: 190, 1556 | VK_SLASH: 191, 1557 | VK_BACK_QUOTE: 192, 1558 | VK_OPEN_BRACKET: 219, 1559 | VK_BACK_SLASH: 220, 1560 | VK_CLOSE_BRACKET: 221, 1561 | VK_QUOTE: 222, 1562 | VK_META: 224, 1563 | VK_ALTGR: 225, 1564 | VK_WIN: 91, 1565 | VK_KANA: 21, 1566 | VK_HANGUL: 21, 1567 | VK_EISU: 22, 1568 | VK_JUNJA: 23, 1569 | VK_FINAL: 24, 1570 | VK_HANJA: 25, 1571 | VK_KANJI: 25, 1572 | VK_CONVERT: 28, 1573 | VK_NONCONVERT: 29, 1574 | VK_ACCEPT: 30, 1575 | VK_MODECHANGE: 31, 1576 | VK_SELECT: 41, 1577 | VK_PRINT: 42, 1578 | VK_EXECUTE: 43, 1579 | VK_SLEEP: 95 1580 | }; 1581 | var BACKENDS = { 1582 | "hex": Hex, 1583 | "rect": Rect, 1584 | "tile": Tile, 1585 | "term": Term 1586 | }; 1587 | var DEFAULT_OPTIONS = { 1588 | width: DEFAULT_WIDTH, 1589 | height: DEFAULT_HEIGHT, 1590 | transpose: false, 1591 | layout: "rect", 1592 | fontSize: 15, 1593 | spacing: 1, 1594 | border: 0, 1595 | forceSquareRatio: false, 1596 | fontFamily: "monospace", 1597 | fontStyle: "", 1598 | fg: "#ccc", 1599 | bg: "#000", 1600 | tileWidth: 32, 1601 | tileHeight: 32, 1602 | tileMap: {}, 1603 | tileSet: null, 1604 | tileColorize: false 1605 | }; 1606 | 1607 | var Display = 1608 | /*#__PURE__*/ 1609 | function () { 1610 | function Display(options) { 1611 | if (options === void 0) { 1612 | options = {}; 1613 | } 1614 | 1615 | this._data = {}; 1616 | this._dirty = false; 1617 | this._options = {}; 1618 | options = Object.assign({}, DEFAULT_OPTIONS, options); 1619 | this.setOptions(options); 1620 | this.DEBUG = this.DEBUG.bind(this); 1621 | this._tick = this._tick.bind(this); 1622 | 1623 | this._backend.schedule(this._tick); 1624 | } 1625 | 1626 | var _proto8 = Display.prototype; 1627 | 1628 | _proto8.DEBUG = function DEBUG(x, y, what) { 1629 | var colors = [this._options.bg, this._options.fg]; 1630 | this.draw(x, y, null, null, colors[what % colors.length]); 1631 | }; 1632 | 1633 | _proto8.clear = function clear() { 1634 | this._data = {}; 1635 | this._dirty = true; 1636 | }; 1637 | 1638 | _proto8.setOptions = function setOptions(options) { 1639 | Object.assign(this._options, options); 1640 | 1641 | if (options.width || options.height || options.fontSize || options.fontFamily || options.spacing || options.layout) { 1642 | if (options.layout) { 1643 | var ctor = BACKENDS[options.layout]; 1644 | this._backend = new ctor(); 1645 | } 1646 | 1647 | this._backend.setOptions(this._options); 1648 | 1649 | this._dirty = true; 1650 | } 1651 | 1652 | return this; 1653 | }; 1654 | 1655 | _proto8.getOptions = function getOptions() { 1656 | return this._options; 1657 | }; 1658 | 1659 | _proto8.getContainer = function getContainer() { 1660 | return this._backend.getContainer(); 1661 | }; 1662 | 1663 | _proto8.computeSize = function computeSize(availWidth, availHeight) { 1664 | return this._backend.computeSize(availWidth, availHeight); 1665 | }; 1666 | 1667 | _proto8.computeFontSize = function computeFontSize(availWidth, availHeight) { 1668 | return this._backend.computeFontSize(availWidth, availHeight); 1669 | }; 1670 | 1671 | _proto8.computeTileSize = function computeTileSize(availWidth, availHeight) { 1672 | var width = Math.floor(availWidth / this._options.width); 1673 | var height = Math.floor(availHeight / this._options.height); 1674 | return [width, height]; 1675 | }; 1676 | 1677 | _proto8.eventToPosition = function eventToPosition(e) { 1678 | var x, y; 1679 | 1680 | if ("touches" in e) { 1681 | x = e.touches[0].clientX; 1682 | y = e.touches[0].clientY; 1683 | } else { 1684 | x = e.clientX; 1685 | y = e.clientY; 1686 | } 1687 | 1688 | return this._backend.eventToPosition(x, y); 1689 | }; 1690 | 1691 | _proto8.draw = function draw(x, y, ch, fg, bg) { 1692 | if (!fg) { 1693 | fg = this._options.fg; 1694 | } 1695 | 1696 | if (!bg) { 1697 | bg = this._options.bg; 1698 | } 1699 | 1700 | var key = x + "," + y; 1701 | this._data[key] = [x, y, ch, fg, bg]; 1702 | 1703 | if (this._dirty === true) { 1704 | return; 1705 | } 1706 | 1707 | if (!this._dirty) { 1708 | this._dirty = {}; 1709 | } 1710 | 1711 | this._dirty[key] = true; 1712 | }; 1713 | 1714 | _proto8.drawText = function drawText(x, y, text, maxWidth) { 1715 | var fg = null; 1716 | var bg = null; 1717 | var cx = x; 1718 | var cy = y; 1719 | var lines = 1; 1720 | 1721 | if (!maxWidth) { 1722 | maxWidth = this._options.width - x; 1723 | } 1724 | 1725 | var tokens = tokenize(text, maxWidth); 1726 | 1727 | while (tokens.length) { 1728 | var token = tokens.shift(); 1729 | 1730 | switch (token.type) { 1731 | case TYPE_TEXT: 1732 | var isSpace = false, 1733 | isPrevSpace = false, 1734 | isFullWidth = false, 1735 | isPrevFullWidth = false; 1736 | 1737 | for (var i = 0; i < token.value.length; i++) { 1738 | var cc = token.value.charCodeAt(i); 1739 | var c = token.value.charAt(i); 1740 | isFullWidth = cc > 0xff00 && cc < 0xff61 || cc > 0xffdc && cc < 0xffe8 || cc > 0xffee; 1741 | isSpace = c.charCodeAt(0) == 0x20 || c.charCodeAt(0) == 0x3000; 1742 | 1743 | if (isPrevFullWidth && !isFullWidth && !isSpace) { 1744 | cx++; 1745 | } 1746 | 1747 | if (isFullWidth && !isPrevSpace) { 1748 | cx++; 1749 | } 1750 | 1751 | this.draw(cx++, cy, c, fg, bg); 1752 | isPrevSpace = isSpace; 1753 | isPrevFullWidth = isFullWidth; 1754 | } 1755 | 1756 | break; 1757 | 1758 | case TYPE_FG: 1759 | fg = token.value || null; 1760 | break; 1761 | 1762 | case TYPE_BG: 1763 | bg = token.value || null; 1764 | break; 1765 | 1766 | case TYPE_NEWLINE: 1767 | cx = x; 1768 | cy++; 1769 | lines++; 1770 | break; 1771 | } 1772 | } 1773 | 1774 | return lines; 1775 | }; 1776 | 1777 | _proto8._tick = function _tick() { 1778 | this._backend.schedule(this._tick); 1779 | 1780 | if (!this._dirty) { 1781 | return; 1782 | } 1783 | 1784 | if (this._dirty === true) { 1785 | this._backend.clear(); 1786 | 1787 | for (var id in this._data) { 1788 | this._draw(id, false); 1789 | } 1790 | } else { 1791 | for (var key in this._dirty) { 1792 | this._draw(key, true); 1793 | } 1794 | } 1795 | 1796 | this._dirty = false; 1797 | }; 1798 | 1799 | _proto8._draw = function _draw(key, clearBefore) { 1800 | var data = this._data[key]; 1801 | 1802 | if (data[4] != this._options.bg) { 1803 | clearBefore = true; 1804 | } 1805 | 1806 | this._backend.draw(data, clearBefore); 1807 | }; 1808 | 1809 | return Display; 1810 | }(); 1811 | 1812 | Display.Rect = Rect; 1813 | Display.Hex = Hex; 1814 | Display.Tile = Tile; 1815 | Display.Term = Term; 1816 | 1817 | var StringGenerator = 1818 | /*#__PURE__*/ 1819 | function () { 1820 | function StringGenerator(options) { 1821 | this._options = { 1822 | words: false, 1823 | order: 3, 1824 | prior: 0.001 1825 | }; 1826 | Object.assign(this._options, options); 1827 | this._boundary = String.fromCharCode(0); 1828 | this._suffix = this._boundary; 1829 | this._prefix = []; 1830 | 1831 | for (var i = 0; i < this._options.order; i++) { 1832 | this._prefix.push(this._boundary); 1833 | } 1834 | 1835 | this._priorValues = {}; 1836 | this._priorValues[this._boundary] = this._options.prior; 1837 | this._data = {}; 1838 | } 1839 | 1840 | var _proto9 = StringGenerator.prototype; 1841 | 1842 | _proto9.clear = function clear() { 1843 | this._data = {}; 1844 | this._priorValues = {}; 1845 | }; 1846 | 1847 | _proto9.generate = function generate() { 1848 | var result = [this._sample(this._prefix)]; 1849 | 1850 | while (result[result.length - 1] != this._boundary) { 1851 | result.push(this._sample(result)); 1852 | } 1853 | 1854 | return this._join(result.slice(0, -1)); 1855 | }; 1856 | 1857 | _proto9.observe = function observe(string) { 1858 | var tokens = this._split(string); 1859 | 1860 | for (var i = 0; i < tokens.length; i++) { 1861 | this._priorValues[tokens[i]] = this._options.prior; 1862 | } 1863 | 1864 | tokens = this._prefix.concat(tokens).concat(this._suffix); 1865 | 1866 | for (var _i2 = this._options.order; _i2 < tokens.length; _i2++) { 1867 | var context = tokens.slice(_i2 - this._options.order, _i2); 1868 | var event = tokens[_i2]; 1869 | 1870 | for (var j = 0; j < context.length; j++) { 1871 | var subcontext = context.slice(j); 1872 | 1873 | this._observeEvent(subcontext, event); 1874 | } 1875 | } 1876 | }; 1877 | 1878 | _proto9.getStats = function getStats() { 1879 | var parts = []; 1880 | var priorCount = Object.keys(this._priorValues).length; 1881 | priorCount--; 1882 | parts.push("distinct samples: " + priorCount); 1883 | var dataCount = Object.keys(this._data).length; 1884 | var eventCount = 0; 1885 | 1886 | for (var p in this._data) { 1887 | eventCount += Object.keys(this._data[p]).length; 1888 | } 1889 | 1890 | parts.push("dictionary size (contexts): " + dataCount); 1891 | parts.push("dictionary size (events): " + eventCount); 1892 | return parts.join(", "); 1893 | }; 1894 | 1895 | _proto9._split = function _split(str) { 1896 | return str.split(this._options.words ? /\s+/ : ""); 1897 | }; 1898 | 1899 | _proto9._join = function _join(arr) { 1900 | return arr.join(this._options.words ? " " : ""); 1901 | }; 1902 | 1903 | _proto9._observeEvent = function _observeEvent(context, event) { 1904 | var key = this._join(context); 1905 | 1906 | if (!(key in this._data)) { 1907 | this._data[key] = {}; 1908 | } 1909 | 1910 | var data = this._data[key]; 1911 | 1912 | if (!(event in data)) { 1913 | data[event] = 0; 1914 | } 1915 | 1916 | data[event]++; 1917 | }; 1918 | 1919 | _proto9._sample = function _sample(context) { 1920 | context = this._backoff(context); 1921 | 1922 | var key = this._join(context); 1923 | 1924 | var data = this._data[key]; 1925 | var available = {}; 1926 | 1927 | if (this._options.prior) { 1928 | for (var event in this._priorValues) { 1929 | available[event] = this._priorValues[event]; 1930 | } 1931 | 1932 | for (var _event in data) { 1933 | available[_event] += data[_event]; 1934 | } 1935 | } else { 1936 | available = data; 1937 | } 1938 | 1939 | return RNG$1.getWeightedValue(available); 1940 | }; 1941 | 1942 | _proto9._backoff = function _backoff(context) { 1943 | if (context.length > this._options.order) { 1944 | context = context.slice(-this._options.order); 1945 | } else if (context.length < this._options.order) { 1946 | context = this._prefix.slice(0, this._options.order - context.length).concat(context); 1947 | } 1948 | 1949 | while (!(this._join(context) in this._data) && context.length > 0) { 1950 | context = context.slice(1); 1951 | } 1952 | 1953 | return context; 1954 | }; 1955 | 1956 | return StringGenerator; 1957 | }(); 1958 | 1959 | var EventQueue = 1960 | /*#__PURE__*/ 1961 | function () { 1962 | function EventQueue() { 1963 | this._time = 0; 1964 | this._events = []; 1965 | this._eventTimes = []; 1966 | } 1967 | 1968 | var _proto10 = EventQueue.prototype; 1969 | 1970 | _proto10.getTime = function getTime() { 1971 | return this._time; 1972 | }; 1973 | 1974 | _proto10.clear = function clear() { 1975 | this._events = []; 1976 | this._eventTimes = []; 1977 | return this; 1978 | }; 1979 | 1980 | _proto10.add = function add(event, time) { 1981 | var index = this._events.length; 1982 | 1983 | for (var i = 0; i < this._eventTimes.length; i++) { 1984 | if (this._eventTimes[i] > time) { 1985 | index = i; 1986 | break; 1987 | } 1988 | } 1989 | 1990 | this._events.splice(index, 0, event); 1991 | 1992 | this._eventTimes.splice(index, 0, time); 1993 | }; 1994 | 1995 | _proto10.get = function get() { 1996 | if (!this._events.length) { 1997 | return null; 1998 | } 1999 | 2000 | var time = this._eventTimes.splice(0, 1)[0]; 2001 | 2002 | if (time > 0) { 2003 | this._time += time; 2004 | 2005 | for (var i = 0; i < this._eventTimes.length; i++) { 2006 | this._eventTimes[i] -= time; 2007 | } 2008 | } 2009 | 2010 | return this._events.splice(0, 1)[0]; 2011 | }; 2012 | 2013 | _proto10.getEventTime = function getEventTime(event) { 2014 | var index = this._events.indexOf(event); 2015 | 2016 | if (index == -1) { 2017 | return undefined; 2018 | } 2019 | 2020 | return this._eventTimes[index]; 2021 | }; 2022 | 2023 | _proto10.remove = function remove(event) { 2024 | var index = this._events.indexOf(event); 2025 | 2026 | if (index == -1) { 2027 | return false; 2028 | } 2029 | 2030 | this._remove(index); 2031 | 2032 | return true; 2033 | }; 2034 | 2035 | _proto10._remove = function _remove(index) { 2036 | this._events.splice(index, 1); 2037 | 2038 | this._eventTimes.splice(index, 1); 2039 | }; 2040 | 2041 | return EventQueue; 2042 | }(); 2043 | 2044 | var Scheduler = 2045 | /*#__PURE__*/ 2046 | function () { 2047 | function Scheduler() { 2048 | this._queue = new EventQueue(); 2049 | this._repeat = []; 2050 | this._current = null; 2051 | } 2052 | 2053 | var _proto11 = Scheduler.prototype; 2054 | 2055 | _proto11.getTime = function getTime() { 2056 | return this._queue.getTime(); 2057 | }; 2058 | 2059 | _proto11.add = function add(item, repeat) { 2060 | if (repeat) { 2061 | this._repeat.push(item); 2062 | } 2063 | 2064 | return this; 2065 | }; 2066 | 2067 | _proto11.getTimeOf = function getTimeOf(item) { 2068 | return this._queue.getEventTime(item); 2069 | }; 2070 | 2071 | _proto11.clear = function clear() { 2072 | this._queue.clear(); 2073 | 2074 | this._repeat = []; 2075 | this._current = null; 2076 | return this; 2077 | }; 2078 | 2079 | _proto11.remove = function remove(item) { 2080 | var result = this._queue.remove(item); 2081 | 2082 | var index = this._repeat.indexOf(item); 2083 | 2084 | if (index != -1) { 2085 | this._repeat.splice(index, 1); 2086 | } 2087 | 2088 | if (this._current == item) { 2089 | this._current = null; 2090 | } 2091 | 2092 | return result; 2093 | }; 2094 | 2095 | _proto11.next = function next() { 2096 | this._current = this._queue.get(); 2097 | return this._current; 2098 | }; 2099 | 2100 | return Scheduler; 2101 | }(); 2102 | 2103 | var Simple = 2104 | /*#__PURE__*/ 2105 | function (_Scheduler) { 2106 | _inheritsLoose(Simple, _Scheduler); 2107 | 2108 | function Simple() { 2109 | return _Scheduler.apply(this, arguments) || this; 2110 | } 2111 | 2112 | var _proto12 = Simple.prototype; 2113 | 2114 | _proto12.add = function add(item, repeat) { 2115 | this._queue.add(item, 0); 2116 | 2117 | return _Scheduler.prototype.add.call(this, item, repeat); 2118 | }; 2119 | 2120 | _proto12.next = function next() { 2121 | if (this._current && this._repeat.indexOf(this._current) != -1) { 2122 | this._queue.add(this._current, 0); 2123 | } 2124 | 2125 | return _Scheduler.prototype.next.call(this); 2126 | }; 2127 | 2128 | return Simple; 2129 | }(Scheduler); 2130 | 2131 | var Speed = 2132 | /*#__PURE__*/ 2133 | function (_Scheduler2) { 2134 | _inheritsLoose(Speed, _Scheduler2); 2135 | 2136 | function Speed() { 2137 | return _Scheduler2.apply(this, arguments) || this; 2138 | } 2139 | 2140 | var _proto13 = Speed.prototype; 2141 | 2142 | _proto13.add = function add(item, repeat, time) { 2143 | this._queue.add(item, time !== undefined ? time : 1 / item.getSpeed()); 2144 | 2145 | return _Scheduler2.prototype.add.call(this, item, repeat); 2146 | }; 2147 | 2148 | _proto13.next = function next() { 2149 | if (this._current && this._repeat.indexOf(this._current) != -1) { 2150 | this._queue.add(this._current, 1 / this._current.getSpeed()); 2151 | } 2152 | 2153 | return _Scheduler2.prototype.next.call(this); 2154 | }; 2155 | 2156 | return Speed; 2157 | }(Scheduler); 2158 | 2159 | var Action = 2160 | /*#__PURE__*/ 2161 | function (_Scheduler3) { 2162 | _inheritsLoose(Action, _Scheduler3); 2163 | 2164 | function Action() { 2165 | var _this6; 2166 | 2167 | _this6 = _Scheduler3.call(this) || this; 2168 | _this6._defaultDuration = 1; 2169 | _this6._duration = _this6._defaultDuration; 2170 | return _this6; 2171 | } 2172 | 2173 | var _proto14 = Action.prototype; 2174 | 2175 | _proto14.add = function add(item, repeat, time) { 2176 | this._queue.add(item, time || this._defaultDuration); 2177 | 2178 | return _Scheduler3.prototype.add.call(this, item, repeat); 2179 | }; 2180 | 2181 | _proto14.clear = function clear() { 2182 | this._duration = this._defaultDuration; 2183 | return _Scheduler3.prototype.clear.call(this); 2184 | }; 2185 | 2186 | _proto14.remove = function remove(item) { 2187 | if (item == this._current) { 2188 | this._duration = this._defaultDuration; 2189 | } 2190 | 2191 | return _Scheduler3.prototype.remove.call(this, item); 2192 | }; 2193 | 2194 | _proto14.next = function next() { 2195 | if (this._current && this._repeat.indexOf(this._current) != -1) { 2196 | this._queue.add(this._current, this._duration || this._defaultDuration); 2197 | 2198 | this._duration = this._defaultDuration; 2199 | } 2200 | 2201 | return _Scheduler3.prototype.next.call(this); 2202 | }; 2203 | 2204 | _proto14.setDuration = function setDuration(time) { 2205 | if (this._current) { 2206 | this._duration = time; 2207 | } 2208 | 2209 | return this; 2210 | }; 2211 | 2212 | return Action; 2213 | }(Scheduler); 2214 | 2215 | var index = { 2216 | Simple: Simple, 2217 | Speed: Speed, 2218 | Action: Action 2219 | }; 2220 | 2221 | var FOV = 2222 | /*#__PURE__*/ 2223 | function () { 2224 | function FOV(lightPassesCallback, options) { 2225 | if (options === void 0) { 2226 | options = {}; 2227 | } 2228 | 2229 | this._lightPasses = lightPassesCallback; 2230 | this._options = Object.assign({ 2231 | topology: 8 2232 | }, options); 2233 | } 2234 | 2235 | var _proto15 = FOV.prototype; 2236 | 2237 | _proto15._getCircle = function _getCircle(cx, cy, r) { 2238 | var result = []; 2239 | var dirs, countFactor, startOffset; 2240 | 2241 | switch (this._options.topology) { 2242 | case 4: 2243 | countFactor = 1; 2244 | startOffset = [0, 1]; 2245 | dirs = [DIRS[8][7], DIRS[8][1], DIRS[8][3], DIRS[8][5]]; 2246 | break; 2247 | 2248 | case 6: 2249 | dirs = DIRS[6]; 2250 | countFactor = 1; 2251 | startOffset = [-1, 1]; 2252 | break; 2253 | 2254 | case 8: 2255 | dirs = DIRS[4]; 2256 | countFactor = 2; 2257 | startOffset = [-1, 1]; 2258 | break; 2259 | 2260 | default: 2261 | throw new Error("Incorrect topology for FOV computation"); 2262 | break; 2263 | } 2264 | 2265 | var x = cx + startOffset[0] * r; 2266 | var y = cy + startOffset[1] * r; 2267 | 2268 | for (var i = 0; i < dirs.length; i++) { 2269 | for (var j = 0; j < r * countFactor; j++) { 2270 | result.push([x, y]); 2271 | x += dirs[i][0]; 2272 | y += dirs[i][1]; 2273 | } 2274 | } 2275 | 2276 | return result; 2277 | }; 2278 | 2279 | return FOV; 2280 | }(); 2281 | 2282 | var DiscreteShadowcasting = 2283 | /*#__PURE__*/ 2284 | function (_FOV) { 2285 | _inheritsLoose(DiscreteShadowcasting, _FOV); 2286 | 2287 | function DiscreteShadowcasting() { 2288 | return _FOV.apply(this, arguments) || this; 2289 | } 2290 | 2291 | var _proto16 = DiscreteShadowcasting.prototype; 2292 | 2293 | _proto16.compute = function compute(x, y, R, callback) { 2294 | callback(x, y, 0, 1); 2295 | 2296 | if (!this._lightPasses(x, y)) { 2297 | return; 2298 | } 2299 | 2300 | var DATA = []; 2301 | var A, B, cx, cy, blocks; 2302 | 2303 | for (var r = 1; r <= R; r++) { 2304 | var neighbors = this._getCircle(x, y, r); 2305 | 2306 | var angle = 360 / neighbors.length; 2307 | 2308 | for (var i = 0; i < neighbors.length; i++) { 2309 | cx = neighbors[i][0]; 2310 | cy = neighbors[i][1]; 2311 | A = angle * (i - 0.5); 2312 | B = A + angle; 2313 | blocks = !this._lightPasses(cx, cy); 2314 | 2315 | if (this._visibleCoords(Math.floor(A), Math.ceil(B), blocks, DATA)) { 2316 | callback(cx, cy, r, 1); 2317 | } 2318 | 2319 | if (DATA.length == 2 && DATA[0] == 0 && DATA[1] == 360) { 2320 | return; 2321 | } 2322 | } 2323 | } 2324 | }; 2325 | 2326 | _proto16._visibleCoords = function _visibleCoords(A, B, blocks, DATA) { 2327 | if (A < 0) { 2328 | var v1 = this._visibleCoords(0, B, blocks, DATA); 2329 | 2330 | var v2 = this._visibleCoords(360 + A, 360, blocks, DATA); 2331 | 2332 | return v1 || v2; 2333 | } 2334 | 2335 | var index = 0; 2336 | 2337 | while (index < DATA.length && DATA[index] < A) { 2338 | index++; 2339 | } 2340 | 2341 | if (index == DATA.length) { 2342 | if (blocks) { 2343 | DATA.push(A, B); 2344 | } 2345 | 2346 | return true; 2347 | } 2348 | 2349 | var count = 0; 2350 | 2351 | if (index % 2) { 2352 | while (index < DATA.length && DATA[index] < B) { 2353 | index++; 2354 | count++; 2355 | } 2356 | 2357 | if (count == 0) { 2358 | return false; 2359 | } 2360 | 2361 | if (blocks) { 2362 | if (count % 2) { 2363 | DATA.splice(index - count, count, B); 2364 | } else { 2365 | DATA.splice(index - count, count); 2366 | } 2367 | } 2368 | 2369 | return true; 2370 | } else { 2371 | while (index < DATA.length && DATA[index] < B) { 2372 | index++; 2373 | count++; 2374 | } 2375 | 2376 | if (A == DATA[index - count] && count == 1) { 2377 | return false; 2378 | } 2379 | 2380 | if (blocks) { 2381 | if (count % 2) { 2382 | DATA.splice(index - count, count, A); 2383 | } else { 2384 | DATA.splice(index - count, count, A, B); 2385 | } 2386 | } 2387 | 2388 | return true; 2389 | } 2390 | }; 2391 | 2392 | return DiscreteShadowcasting; 2393 | }(FOV); 2394 | 2395 | var PreciseShadowcasting = 2396 | /*#__PURE__*/ 2397 | function (_FOV2) { 2398 | _inheritsLoose(PreciseShadowcasting, _FOV2); 2399 | 2400 | function PreciseShadowcasting() { 2401 | return _FOV2.apply(this, arguments) || this; 2402 | } 2403 | 2404 | var _proto17 = PreciseShadowcasting.prototype; 2405 | 2406 | _proto17.compute = function compute(x, y, R, callback) { 2407 | callback(x, y, 0, 1); 2408 | 2409 | if (!this._lightPasses(x, y)) { 2410 | return; 2411 | } 2412 | 2413 | var SHADOWS = []; 2414 | var cx, cy, blocks, A1, A2, visibility; 2415 | 2416 | for (var r = 1; r <= R; r++) { 2417 | var neighbors = this._getCircle(x, y, r); 2418 | 2419 | var neighborCount = neighbors.length; 2420 | 2421 | for (var i = 0; i < neighborCount; i++) { 2422 | cx = neighbors[i][0]; 2423 | cy = neighbors[i][1]; 2424 | A1 = [i ? 2 * i - 1 : 2 * neighborCount - 1, 2 * neighborCount]; 2425 | A2 = [2 * i + 1, 2 * neighborCount]; 2426 | blocks = !this._lightPasses(cx, cy); 2427 | visibility = this._checkVisibility(A1, A2, blocks, SHADOWS); 2428 | 2429 | if (visibility) { 2430 | callback(cx, cy, r, visibility); 2431 | } 2432 | 2433 | if (SHADOWS.length == 2 && SHADOWS[0][0] == 0 && SHADOWS[1][0] == SHADOWS[1][1]) { 2434 | return; 2435 | } 2436 | } 2437 | } 2438 | }; 2439 | 2440 | _proto17._checkVisibility = function _checkVisibility(A1, A2, blocks, SHADOWS) { 2441 | if (A1[0] > A2[0]) { 2442 | var v1 = this._checkVisibility(A1, [A1[1], A1[1]], blocks, SHADOWS); 2443 | 2444 | var v2 = this._checkVisibility([0, 1], A2, blocks, SHADOWS); 2445 | 2446 | return (v1 + v2) / 2; 2447 | } 2448 | 2449 | var index1 = 0, 2450 | edge1 = false; 2451 | 2452 | while (index1 < SHADOWS.length) { 2453 | var old = SHADOWS[index1]; 2454 | var diff = old[0] * A1[1] - A1[0] * old[1]; 2455 | 2456 | if (diff >= 0) { 2457 | if (diff == 0 && !(index1 % 2)) { 2458 | edge1 = true; 2459 | } 2460 | 2461 | break; 2462 | } 2463 | 2464 | index1++; 2465 | } 2466 | 2467 | var index2 = SHADOWS.length, 2468 | edge2 = false; 2469 | 2470 | while (index2--) { 2471 | var _old = SHADOWS[index2]; 2472 | 2473 | var _diff = A2[0] * _old[1] - _old[0] * A2[1]; 2474 | 2475 | if (_diff >= 0) { 2476 | if (_diff == 0 && index2 % 2) { 2477 | edge2 = true; 2478 | } 2479 | 2480 | break; 2481 | } 2482 | } 2483 | 2484 | var visible = true; 2485 | 2486 | if (index1 == index2 && (edge1 || edge2)) { 2487 | visible = false; 2488 | } else if (edge1 && edge2 && index1 + 1 == index2 && index2 % 2) { 2489 | visible = false; 2490 | } else if (index1 > index2 && index1 % 2) { 2491 | visible = false; 2492 | } 2493 | 2494 | if (!visible) { 2495 | return 0; 2496 | } 2497 | 2498 | var visibleLength; 2499 | var remove = index2 - index1 + 1; 2500 | 2501 | if (remove % 2) { 2502 | if (index1 % 2) { 2503 | var P = SHADOWS[index1]; 2504 | visibleLength = (A2[0] * P[1] - P[0] * A2[1]) / (P[1] * A2[1]); 2505 | 2506 | if (blocks) { 2507 | SHADOWS.splice(index1, remove, A2); 2508 | } 2509 | } else { 2510 | var _P = SHADOWS[index2]; 2511 | visibleLength = (_P[0] * A1[1] - A1[0] * _P[1]) / (A1[1] * _P[1]); 2512 | 2513 | if (blocks) { 2514 | SHADOWS.splice(index1, remove, A1); 2515 | } 2516 | } 2517 | } else { 2518 | if (index1 % 2) { 2519 | var P1 = SHADOWS[index1]; 2520 | var P2 = SHADOWS[index2]; 2521 | visibleLength = (P2[0] * P1[1] - P1[0] * P2[1]) / (P1[1] * P2[1]); 2522 | 2523 | if (blocks) { 2524 | SHADOWS.splice(index1, remove); 2525 | } 2526 | } else { 2527 | if (blocks) { 2528 | SHADOWS.splice(index1, remove, A1, A2); 2529 | } 2530 | 2531 | return 1; 2532 | } 2533 | } 2534 | 2535 | var arcLength = (A2[0] * A1[1] - A1[0] * A2[1]) / (A1[1] * A2[1]); 2536 | return visibleLength / arcLength; 2537 | }; 2538 | 2539 | return PreciseShadowcasting; 2540 | }(FOV); 2541 | 2542 | var 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]]; 2543 | 2544 | var RecursiveShadowcasting = 2545 | /*#__PURE__*/ 2546 | function (_FOV3) { 2547 | _inheritsLoose(RecursiveShadowcasting, _FOV3); 2548 | 2549 | function RecursiveShadowcasting() { 2550 | return _FOV3.apply(this, arguments) || this; 2551 | } 2552 | 2553 | var _proto18 = RecursiveShadowcasting.prototype; 2554 | 2555 | _proto18.compute = function compute(x, y, R, callback) { 2556 | callback(x, y, 0, 1); 2557 | 2558 | for (var i = 0; i < OCTANTS.length; i++) { 2559 | this._renderOctant(x, y, OCTANTS[i], R, callback); 2560 | } 2561 | }; 2562 | 2563 | _proto18.compute180 = function compute180(x, y, R, dir, callback) { 2564 | callback(x, y, 0, 1); 2565 | var previousOctant = (dir - 1 + 8) % 8; 2566 | var nextPreviousOctant = (dir - 2 + 8) % 8; 2567 | var nextOctant = (dir + 1 + 8) % 8; 2568 | 2569 | this._renderOctant(x, y, OCTANTS[nextPreviousOctant], R, callback); 2570 | 2571 | this._renderOctant(x, y, OCTANTS[previousOctant], R, callback); 2572 | 2573 | this._renderOctant(x, y, OCTANTS[dir], R, callback); 2574 | 2575 | this._renderOctant(x, y, OCTANTS[nextOctant], R, callback); 2576 | }; 2577 | 2578 | _proto18.compute90 = function compute90(x, y, R, dir, callback) { 2579 | callback(x, y, 0, 1); 2580 | var previousOctant = (dir - 1 + 8) % 8; 2581 | 2582 | this._renderOctant(x, y, OCTANTS[dir], R, callback); 2583 | 2584 | this._renderOctant(x, y, OCTANTS[previousOctant], R, callback); 2585 | }; 2586 | 2587 | _proto18._renderOctant = function _renderOctant(x, y, octant, R, callback) { 2588 | this._castVisibility(x, y, 1, 1.0, 0.0, R + 1, octant[0], octant[1], octant[2], octant[3], callback); 2589 | }; 2590 | 2591 | _proto18._castVisibility = function _castVisibility(startX, startY, row, visSlopeStart, visSlopeEnd, radius, xx, xy, yx, yy, callback) { 2592 | if (visSlopeStart < visSlopeEnd) { 2593 | return; 2594 | } 2595 | 2596 | for (var i = row; i <= radius; i++) { 2597 | var dx = -i - 1; 2598 | var dy = -i; 2599 | var blocked = false; 2600 | var newStart = 0; 2601 | 2602 | while (dx <= 0) { 2603 | dx += 1; 2604 | var mapX = startX + dx * xx + dy * xy; 2605 | var mapY = startY + dx * yx + dy * yy; 2606 | var slopeStart = (dx - 0.5) / (dy + 0.5); 2607 | var slopeEnd = (dx + 0.5) / (dy - 0.5); 2608 | 2609 | if (slopeEnd > visSlopeStart) { 2610 | continue; 2611 | } 2612 | 2613 | if (slopeStart < visSlopeEnd) { 2614 | break; 2615 | } 2616 | 2617 | if (dx * dx + dy * dy < radius * radius) { 2618 | callback(mapX, mapY, i, 1); 2619 | } 2620 | 2621 | if (!blocked) { 2622 | if (!this._lightPasses(mapX, mapY) && i < radius) { 2623 | blocked = true; 2624 | 2625 | this._castVisibility(startX, startY, i + 1, visSlopeStart, slopeStart, radius, xx, xy, yx, yy, callback); 2626 | 2627 | newStart = slopeEnd; 2628 | } 2629 | } else { 2630 | if (!this._lightPasses(mapX, mapY)) { 2631 | newStart = slopeEnd; 2632 | continue; 2633 | } 2634 | 2635 | blocked = false; 2636 | visSlopeStart = newStart; 2637 | } 2638 | } 2639 | 2640 | if (blocked) { 2641 | break; 2642 | } 2643 | } 2644 | }; 2645 | 2646 | return RecursiveShadowcasting; 2647 | }(FOV); 2648 | 2649 | var index$1 = { 2650 | DiscreteShadowcasting: DiscreteShadowcasting, 2651 | PreciseShadowcasting: PreciseShadowcasting, 2652 | RecursiveShadowcasting: RecursiveShadowcasting 2653 | }; 2654 | 2655 | var Map = 2656 | /*#__PURE__*/ 2657 | function () { 2658 | function Map(width, height) { 2659 | if (width === void 0) { 2660 | width = DEFAULT_WIDTH; 2661 | } 2662 | 2663 | if (height === void 0) { 2664 | height = DEFAULT_HEIGHT; 2665 | } 2666 | 2667 | this._width = width; 2668 | this._height = height; 2669 | } 2670 | 2671 | var _proto19 = Map.prototype; 2672 | 2673 | _proto19._fillMap = function _fillMap(value) { 2674 | var map = []; 2675 | 2676 | for (var i = 0; i < this._width; i++) { 2677 | map.push([]); 2678 | 2679 | for (var j = 0; j < this._height; j++) { 2680 | map[i].push(value); 2681 | } 2682 | } 2683 | 2684 | return map; 2685 | }; 2686 | 2687 | return Map; 2688 | }(); 2689 | 2690 | var Arena = 2691 | /*#__PURE__*/ 2692 | function (_Map) { 2693 | _inheritsLoose(Arena, _Map); 2694 | 2695 | function Arena() { 2696 | return _Map.apply(this, arguments) || this; 2697 | } 2698 | 2699 | var _proto20 = Arena.prototype; 2700 | 2701 | _proto20.create = function create(callback) { 2702 | var w = this._width - 1; 2703 | var h = this._height - 1; 2704 | 2705 | for (var i = 0; i <= w; i++) { 2706 | for (var j = 0; j <= h; j++) { 2707 | var empty = i && j && i < w && j < h; 2708 | callback(i, j, empty ? 0 : 1); 2709 | } 2710 | } 2711 | 2712 | return this; 2713 | }; 2714 | 2715 | return Arena; 2716 | }(Map); 2717 | 2718 | var Dungeon = 2719 | /*#__PURE__*/ 2720 | function (_Map2) { 2721 | _inheritsLoose(Dungeon, _Map2); 2722 | 2723 | function Dungeon(width, height) { 2724 | var _this7; 2725 | 2726 | _this7 = _Map2.call(this, width, height) || this; 2727 | _this7._rooms = []; 2728 | _this7._corridors = []; 2729 | return _this7; 2730 | } 2731 | 2732 | var _proto21 = Dungeon.prototype; 2733 | 2734 | _proto21.getRooms = function getRooms() { 2735 | return this._rooms; 2736 | }; 2737 | 2738 | _proto21.getCorridors = function getCorridors() { 2739 | return this._corridors; 2740 | }; 2741 | 2742 | return Dungeon; 2743 | }(Map); 2744 | 2745 | var Feature = function Feature() {}; 2746 | 2747 | var Room = 2748 | /*#__PURE__*/ 2749 | function (_Feature) { 2750 | _inheritsLoose(Room, _Feature); 2751 | 2752 | function Room(x1, y1, x2, y2, doorX, doorY) { 2753 | var _this8; 2754 | 2755 | _this8 = _Feature.call(this) || this; 2756 | _this8._x1 = x1; 2757 | _this8._y1 = y1; 2758 | _this8._x2 = x2; 2759 | _this8._y2 = y2; 2760 | _this8._doors = {}; 2761 | 2762 | if (doorX !== undefined && doorY !== undefined) { 2763 | _this8.addDoor(doorX, doorY); 2764 | } 2765 | 2766 | return _this8; 2767 | } 2768 | 2769 | Room.createRandomAt = function createRandomAt(x, y, dx, dy, options) { 2770 | var min = options.roomWidth[0]; 2771 | var max = options.roomWidth[1]; 2772 | var width = RNG$1.getUniformInt(min, max); 2773 | min = options.roomHeight[0]; 2774 | max = options.roomHeight[1]; 2775 | var height = RNG$1.getUniformInt(min, max); 2776 | 2777 | if (dx == 1) { 2778 | var y2 = y - Math.floor(RNG$1.getUniform() * height); 2779 | return new this(x + 1, y2, x + width, y2 + height - 1, x, y); 2780 | } 2781 | 2782 | if (dx == -1) { 2783 | var _y = y - Math.floor(RNG$1.getUniform() * height); 2784 | 2785 | return new this(x - width, _y, x - 1, _y + height - 1, x, y); 2786 | } 2787 | 2788 | if (dy == 1) { 2789 | var x2 = x - Math.floor(RNG$1.getUniform() * width); 2790 | return new this(x2, y + 1, x2 + width - 1, y + height, x, y); 2791 | } 2792 | 2793 | if (dy == -1) { 2794 | var _x = x - Math.floor(RNG$1.getUniform() * width); 2795 | 2796 | return new this(_x, y - height, _x + width - 1, y - 1, x, y); 2797 | } 2798 | 2799 | throw new Error("dx or dy must be 1 or -1"); 2800 | }; 2801 | 2802 | Room.createRandomCenter = function createRandomCenter(cx, cy, options) { 2803 | var min = options.roomWidth[0]; 2804 | var max = options.roomWidth[1]; 2805 | var width = RNG$1.getUniformInt(min, max); 2806 | min = options.roomHeight[0]; 2807 | max = options.roomHeight[1]; 2808 | var height = RNG$1.getUniformInt(min, max); 2809 | var x1 = cx - Math.floor(RNG$1.getUniform() * width); 2810 | var y1 = cy - Math.floor(RNG$1.getUniform() * height); 2811 | var x2 = x1 + width - 1; 2812 | var y2 = y1 + height - 1; 2813 | return new this(x1, y1, x2, y2); 2814 | }; 2815 | 2816 | Room.createRandom = function createRandom(availWidth, availHeight, options) { 2817 | var min = options.roomWidth[0]; 2818 | var max = options.roomWidth[1]; 2819 | var width = RNG$1.getUniformInt(min, max); 2820 | min = options.roomHeight[0]; 2821 | max = options.roomHeight[1]; 2822 | var height = RNG$1.getUniformInt(min, max); 2823 | var left = availWidth - width - 1; 2824 | var top = availHeight - height - 1; 2825 | var x1 = 1 + Math.floor(RNG$1.getUniform() * left); 2826 | var y1 = 1 + Math.floor(RNG$1.getUniform() * top); 2827 | var x2 = x1 + width - 1; 2828 | var y2 = y1 + height - 1; 2829 | return new this(x1, y1, x2, y2); 2830 | }; 2831 | 2832 | var _proto22 = Room.prototype; 2833 | 2834 | _proto22.addDoor = function addDoor(x, y) { 2835 | this._doors[x + "," + y] = 1; 2836 | return this; 2837 | }; 2838 | 2839 | _proto22.getDoors = function getDoors(cb) { 2840 | for (var key in this._doors) { 2841 | var parts = key.split(","); 2842 | cb(parseInt(parts[0]), parseInt(parts[1])); 2843 | } 2844 | 2845 | return this; 2846 | }; 2847 | 2848 | _proto22.clearDoors = function clearDoors() { 2849 | this._doors = {}; 2850 | return this; 2851 | }; 2852 | 2853 | _proto22.addDoors = function addDoors(isWallCallback) { 2854 | var left = this._x1 - 1; 2855 | var right = this._x2 + 1; 2856 | var top = this._y1 - 1; 2857 | var bottom = this._y2 + 1; 2858 | 2859 | for (var x = left; x <= right; x++) { 2860 | for (var y = top; y <= bottom; y++) { 2861 | if (x != left && x != right && y != top && y != bottom) { 2862 | continue; 2863 | } 2864 | 2865 | if (isWallCallback(x, y)) { 2866 | continue; 2867 | } 2868 | 2869 | this.addDoor(x, y); 2870 | } 2871 | } 2872 | 2873 | return this; 2874 | }; 2875 | 2876 | _proto22.debug = function debug() { 2877 | console.log("room", this._x1, this._y1, this._x2, this._y2); 2878 | }; 2879 | 2880 | _proto22.isValid = function isValid(isWallCallback, canBeDugCallback) { 2881 | var left = this._x1 - 1; 2882 | var right = this._x2 + 1; 2883 | var top = this._y1 - 1; 2884 | var bottom = this._y2 + 1; 2885 | 2886 | for (var x = left; x <= right; x++) { 2887 | for (var y = top; y <= bottom; y++) { 2888 | if (x == left || x == right || y == top || y == bottom) { 2889 | if (!isWallCallback(x, y)) { 2890 | return false; 2891 | } 2892 | } else { 2893 | if (!canBeDugCallback(x, y)) { 2894 | return false; 2895 | } 2896 | } 2897 | } 2898 | } 2899 | 2900 | return true; 2901 | }; 2902 | 2903 | _proto22.create = function create(digCallback) { 2904 | var left = this._x1 - 1; 2905 | var right = this._x2 + 1; 2906 | var top = this._y1 - 1; 2907 | var bottom = this._y2 + 1; 2908 | var value = 0; 2909 | 2910 | for (var x = left; x <= right; x++) { 2911 | for (var y = top; y <= bottom; y++) { 2912 | if (x + "," + y in this._doors) { 2913 | value = 2; 2914 | } else if (x == left || x == right || y == top || y == bottom) { 2915 | value = 1; 2916 | } else { 2917 | value = 0; 2918 | } 2919 | 2920 | digCallback(x, y, value); 2921 | } 2922 | } 2923 | }; 2924 | 2925 | _proto22.getCenter = function getCenter() { 2926 | return [Math.round((this._x1 + this._x2) / 2), Math.round((this._y1 + this._y2) / 2)]; 2927 | }; 2928 | 2929 | _proto22.getLeft = function getLeft() { 2930 | return this._x1; 2931 | }; 2932 | 2933 | _proto22.getRight = function getRight() { 2934 | return this._x2; 2935 | }; 2936 | 2937 | _proto22.getTop = function getTop() { 2938 | return this._y1; 2939 | }; 2940 | 2941 | _proto22.getBottom = function getBottom() { 2942 | return this._y2; 2943 | }; 2944 | 2945 | return Room; 2946 | }(Feature); 2947 | 2948 | var Corridor = 2949 | /*#__PURE__*/ 2950 | function (_Feature2) { 2951 | _inheritsLoose(Corridor, _Feature2); 2952 | 2953 | function Corridor(startX, startY, endX, endY) { 2954 | var _this9; 2955 | 2956 | _this9 = _Feature2.call(this) || this; 2957 | _this9._startX = startX; 2958 | _this9._startY = startY; 2959 | _this9._endX = endX; 2960 | _this9._endY = endY; 2961 | _this9._endsWithAWall = true; 2962 | return _this9; 2963 | } 2964 | 2965 | Corridor.createRandomAt = function createRandomAt(x, y, dx, dy, options) { 2966 | var min = options.corridorLength[0]; 2967 | var max = options.corridorLength[1]; 2968 | var length = RNG$1.getUniformInt(min, max); 2969 | return new this(x, y, x + dx * length, y + dy * length); 2970 | }; 2971 | 2972 | var _proto23 = Corridor.prototype; 2973 | 2974 | _proto23.debug = function debug() { 2975 | console.log("corridor", this._startX, this._startY, this._endX, this._endY); 2976 | }; 2977 | 2978 | _proto23.isValid = function isValid(isWallCallback, canBeDugCallback) { 2979 | var sx = this._startX; 2980 | var sy = this._startY; 2981 | var dx = this._endX - sx; 2982 | var dy = this._endY - sy; 2983 | var length = 1 + Math.max(Math.abs(dx), Math.abs(dy)); 2984 | 2985 | if (dx) { 2986 | dx = dx / Math.abs(dx); 2987 | } 2988 | 2989 | if (dy) { 2990 | dy = dy / Math.abs(dy); 2991 | } 2992 | 2993 | var nx = dy; 2994 | var ny = -dx; 2995 | var ok = true; 2996 | 2997 | for (var i = 0; i < length; i++) { 2998 | var x = sx + i * dx; 2999 | var y = sy + i * dy; 3000 | 3001 | if (!canBeDugCallback(x, y)) { 3002 | ok = false; 3003 | } 3004 | 3005 | if (!isWallCallback(x + nx, y + ny)) { 3006 | ok = false; 3007 | } 3008 | 3009 | if (!isWallCallback(x - nx, y - ny)) { 3010 | ok = false; 3011 | } 3012 | 3013 | if (!ok) { 3014 | length = i; 3015 | this._endX = x - dx; 3016 | this._endY = y - dy; 3017 | break; 3018 | } 3019 | } 3020 | 3021 | if (length == 0) { 3022 | return false; 3023 | } 3024 | 3025 | if (length == 1 && isWallCallback(this._endX + dx, this._endY + dy)) { 3026 | return false; 3027 | } 3028 | 3029 | var firstCornerBad = !isWallCallback(this._endX + dx + nx, this._endY + dy + ny); 3030 | var secondCornerBad = !isWallCallback(this._endX + dx - nx, this._endY + dy - ny); 3031 | this._endsWithAWall = isWallCallback(this._endX + dx, this._endY + dy); 3032 | 3033 | if ((firstCornerBad || secondCornerBad) && this._endsWithAWall) { 3034 | return false; 3035 | } 3036 | 3037 | return true; 3038 | }; 3039 | 3040 | _proto23.create = function create(digCallback) { 3041 | var sx = this._startX; 3042 | var sy = this._startY; 3043 | var dx = this._endX - sx; 3044 | var dy = this._endY - sy; 3045 | var length = 1 + Math.max(Math.abs(dx), Math.abs(dy)); 3046 | 3047 | if (dx) { 3048 | dx = dx / Math.abs(dx); 3049 | } 3050 | 3051 | if (dy) { 3052 | dy = dy / Math.abs(dy); 3053 | } 3054 | 3055 | for (var i = 0; i < length; i++) { 3056 | var x = sx + i * dx; 3057 | var y = sy + i * dy; 3058 | digCallback(x, y, 0); 3059 | } 3060 | 3061 | return true; 3062 | }; 3063 | 3064 | _proto23.createPriorityWalls = function createPriorityWalls(priorityWallCallback) { 3065 | if (!this._endsWithAWall) { 3066 | return; 3067 | } 3068 | 3069 | var sx = this._startX; 3070 | var sy = this._startY; 3071 | var dx = this._endX - sx; 3072 | var dy = this._endY - sy; 3073 | 3074 | if (dx) { 3075 | dx = dx / Math.abs(dx); 3076 | } 3077 | 3078 | if (dy) { 3079 | dy = dy / Math.abs(dy); 3080 | } 3081 | 3082 | var nx = dy; 3083 | var ny = -dx; 3084 | priorityWallCallback(this._endX + dx, this._endY + dy); 3085 | priorityWallCallback(this._endX + nx, this._endY + ny); 3086 | priorityWallCallback(this._endX - nx, this._endY - ny); 3087 | }; 3088 | 3089 | return Corridor; 3090 | }(Feature); 3091 | 3092 | var Uniform = 3093 | /*#__PURE__*/ 3094 | function (_Dungeon) { 3095 | _inheritsLoose(Uniform, _Dungeon); 3096 | 3097 | function Uniform(width, height, options) { 3098 | var _this10; 3099 | 3100 | _this10 = _Dungeon.call(this, width, height) || this; 3101 | _this10._options = { 3102 | roomWidth: [3, 9], 3103 | roomHeight: [3, 5], 3104 | roomDugPercentage: 0.1, 3105 | timeLimit: 1000 3106 | }; 3107 | Object.assign(_this10._options, options); 3108 | _this10._map = []; 3109 | _this10._dug = 0; 3110 | _this10._roomAttempts = 20; 3111 | _this10._corridorAttempts = 20; 3112 | _this10._connected = []; 3113 | _this10._unconnected = []; 3114 | _this10._digCallback = _this10._digCallback.bind(_assertThisInitialized(_assertThisInitialized(_this10))); 3115 | _this10._canBeDugCallback = _this10._canBeDugCallback.bind(_assertThisInitialized(_assertThisInitialized(_this10))); 3116 | _this10._isWallCallback = _this10._isWallCallback.bind(_assertThisInitialized(_assertThisInitialized(_this10))); 3117 | return _this10; 3118 | } 3119 | 3120 | var _proto24 = Uniform.prototype; 3121 | 3122 | _proto24.create = function create(callback) { 3123 | var t1 = Date.now(); 3124 | 3125 | while (1) { 3126 | var t2 = Date.now(); 3127 | 3128 | if (t2 - t1 > this._options.timeLimit) { 3129 | return null; 3130 | } 3131 | 3132 | this._map = this._fillMap(1); 3133 | this._dug = 0; 3134 | this._rooms = []; 3135 | this._unconnected = []; 3136 | 3137 | this._generateRooms(); 3138 | 3139 | if (this._rooms.length < 2) { 3140 | continue; 3141 | } 3142 | 3143 | if (this._generateCorridors()) { 3144 | break; 3145 | } 3146 | } 3147 | 3148 | if (callback) { 3149 | for (var i = 0; i < this._width; i++) { 3150 | for (var j = 0; j < this._height; j++) { 3151 | callback(i, j, this._map[i][j]); 3152 | } 3153 | } 3154 | } 3155 | 3156 | return this; 3157 | }; 3158 | 3159 | _proto24._generateRooms = function _generateRooms() { 3160 | var w = this._width - 2; 3161 | var h = this._height - 2; 3162 | var room; 3163 | 3164 | do { 3165 | room = this._generateRoom(); 3166 | 3167 | if (this._dug / (w * h) > this._options.roomDugPercentage) { 3168 | break; 3169 | } 3170 | } while (room); 3171 | }; 3172 | 3173 | _proto24._generateRoom = function _generateRoom() { 3174 | var count = 0; 3175 | 3176 | while (count < this._roomAttempts) { 3177 | count++; 3178 | var room = Room.createRandom(this._width, this._height, this._options); 3179 | 3180 | if (!room.isValid(this._isWallCallback, this._canBeDugCallback)) { 3181 | continue; 3182 | } 3183 | 3184 | room.create(this._digCallback); 3185 | 3186 | this._rooms.push(room); 3187 | 3188 | return room; 3189 | } 3190 | 3191 | return null; 3192 | }; 3193 | 3194 | _proto24._generateCorridors = function _generateCorridors() { 3195 | var cnt = 0; 3196 | 3197 | while (cnt < this._corridorAttempts) { 3198 | cnt++; 3199 | this._corridors = []; 3200 | this._map = this._fillMap(1); 3201 | 3202 | for (var i = 0; i < this._rooms.length; i++) { 3203 | var room = this._rooms[i]; 3204 | room.clearDoors(); 3205 | room.create(this._digCallback); 3206 | } 3207 | 3208 | this._unconnected = RNG$1.shuffle(this._rooms.slice()); 3209 | this._connected = []; 3210 | 3211 | if (this._unconnected.length) { 3212 | this._connected.push(this._unconnected.pop()); 3213 | } 3214 | 3215 | while (1) { 3216 | var connected = RNG$1.getItem(this._connected); 3217 | 3218 | if (!connected) { 3219 | break; 3220 | } 3221 | 3222 | var room1 = this._closestRoom(this._unconnected, connected); 3223 | 3224 | if (!room1) { 3225 | break; 3226 | } 3227 | 3228 | var room2 = this._closestRoom(this._connected, room1); 3229 | 3230 | if (!room2) { 3231 | break; 3232 | } 3233 | 3234 | var ok = this._connectRooms(room1, room2); 3235 | 3236 | if (!ok) { 3237 | break; 3238 | } 3239 | 3240 | if (!this._unconnected.length) { 3241 | return true; 3242 | } 3243 | } 3244 | } 3245 | 3246 | return false; 3247 | }; 3248 | 3249 | _proto24._closestRoom = function _closestRoom(rooms, room) { 3250 | var dist = Infinity; 3251 | var center = room.getCenter(); 3252 | var result = null; 3253 | 3254 | for (var i = 0; i < rooms.length; i++) { 3255 | var r = rooms[i]; 3256 | var c = r.getCenter(); 3257 | var dx = c[0] - center[0]; 3258 | var dy = c[1] - center[1]; 3259 | var d = dx * dx + dy * dy; 3260 | 3261 | if (d < dist) { 3262 | dist = d; 3263 | result = r; 3264 | } 3265 | } 3266 | 3267 | return result; 3268 | }; 3269 | 3270 | _proto24._connectRooms = function _connectRooms(room1, room2) { 3271 | var center1 = room1.getCenter(); 3272 | var center2 = room2.getCenter(); 3273 | var diffX = center2[0] - center1[0]; 3274 | var diffY = center2[1] - center1[1]; 3275 | var start; 3276 | var end; 3277 | var dirIndex1, dirIndex2, min, max, index; 3278 | 3279 | if (Math.abs(diffX) < Math.abs(diffY)) { 3280 | dirIndex1 = diffY > 0 ? 2 : 0; 3281 | dirIndex2 = (dirIndex1 + 2) % 4; 3282 | min = room2.getLeft(); 3283 | max = room2.getRight(); 3284 | index = 0; 3285 | } else { 3286 | dirIndex1 = diffX > 0 ? 1 : 3; 3287 | dirIndex2 = (dirIndex1 + 2) % 4; 3288 | min = room2.getTop(); 3289 | max = room2.getBottom(); 3290 | index = 1; 3291 | } 3292 | 3293 | start = this._placeInWall(room1, dirIndex1); 3294 | 3295 | if (!start) { 3296 | return false; 3297 | } 3298 | 3299 | if (start[index] >= min && start[index] <= max) { 3300 | end = start.slice(); 3301 | var value = 0; 3302 | 3303 | switch (dirIndex2) { 3304 | case 0: 3305 | value = room2.getTop() - 1; 3306 | break; 3307 | 3308 | case 1: 3309 | value = room2.getRight() + 1; 3310 | break; 3311 | 3312 | case 2: 3313 | value = room2.getBottom() + 1; 3314 | break; 3315 | 3316 | case 3: 3317 | value = room2.getLeft() - 1; 3318 | break; 3319 | } 3320 | 3321 | end[(index + 1) % 2] = value; 3322 | 3323 | this._digLine([start, end]); 3324 | } else if (start[index] < min - 1 || start[index] > max + 1) { 3325 | var diff = start[index] - center2[index]; 3326 | var rotation = 0; 3327 | 3328 | switch (dirIndex2) { 3329 | case 0: 3330 | case 1: 3331 | rotation = diff < 0 ? 3 : 1; 3332 | break; 3333 | 3334 | case 2: 3335 | case 3: 3336 | rotation = diff < 0 ? 1 : 3; 3337 | break; 3338 | } 3339 | 3340 | dirIndex2 = (dirIndex2 + rotation) % 4; 3341 | end = this._placeInWall(room2, dirIndex2); 3342 | 3343 | if (!end) { 3344 | return false; 3345 | } 3346 | 3347 | var mid = [0, 0]; 3348 | mid[index] = start[index]; 3349 | var index2 = (index + 1) % 2; 3350 | mid[index2] = end[index2]; 3351 | 3352 | this._digLine([start, mid, end]); 3353 | } else { 3354 | var _index4 = (index + 1) % 2; 3355 | 3356 | end = this._placeInWall(room2, dirIndex2); 3357 | 3358 | if (!end) { 3359 | return false; 3360 | } 3361 | 3362 | var _mid = Math.round((end[_index4] + start[_index4]) / 2); 3363 | 3364 | var mid1 = [0, 0]; 3365 | var mid2 = [0, 0]; 3366 | mid1[index] = start[index]; 3367 | mid1[_index4] = _mid; 3368 | mid2[index] = end[index]; 3369 | mid2[_index4] = _mid; 3370 | 3371 | this._digLine([start, mid1, mid2, end]); 3372 | } 3373 | 3374 | room1.addDoor(start[0], start[1]); 3375 | room2.addDoor(end[0], end[1]); 3376 | index = this._unconnected.indexOf(room1); 3377 | 3378 | if (index != -1) { 3379 | this._unconnected.splice(index, 1); 3380 | 3381 | this._connected.push(room1); 3382 | } 3383 | 3384 | index = this._unconnected.indexOf(room2); 3385 | 3386 | if (index != -1) { 3387 | this._unconnected.splice(index, 1); 3388 | 3389 | this._connected.push(room2); 3390 | } 3391 | 3392 | return true; 3393 | }; 3394 | 3395 | _proto24._placeInWall = function _placeInWall(room, dirIndex) { 3396 | var start = [0, 0]; 3397 | var dir = [0, 0]; 3398 | var length = 0; 3399 | 3400 | switch (dirIndex) { 3401 | case 0: 3402 | dir = [1, 0]; 3403 | start = [room.getLeft(), room.getTop() - 1]; 3404 | length = room.getRight() - room.getLeft() + 1; 3405 | break; 3406 | 3407 | case 1: 3408 | dir = [0, 1]; 3409 | start = [room.getRight() + 1, room.getTop()]; 3410 | length = room.getBottom() - room.getTop() + 1; 3411 | break; 3412 | 3413 | case 2: 3414 | dir = [1, 0]; 3415 | start = [room.getLeft(), room.getBottom() + 1]; 3416 | length = room.getRight() - room.getLeft() + 1; 3417 | break; 3418 | 3419 | case 3: 3420 | dir = [0, 1]; 3421 | start = [room.getLeft() - 1, room.getTop()]; 3422 | length = room.getBottom() - room.getTop() + 1; 3423 | break; 3424 | } 3425 | 3426 | var avail = []; 3427 | var lastBadIndex = -2; 3428 | 3429 | for (var i = 0; i < length; i++) { 3430 | var x = start[0] + i * dir[0]; 3431 | var y = start[1] + i * dir[1]; 3432 | avail.push(null); 3433 | var isWall = this._map[x][y] == 1; 3434 | 3435 | if (isWall) { 3436 | if (lastBadIndex != i - 1) { 3437 | avail[i] = [x, y]; 3438 | } 3439 | } else { 3440 | lastBadIndex = i; 3441 | 3442 | if (i) { 3443 | avail[i - 1] = null; 3444 | } 3445 | } 3446 | } 3447 | 3448 | for (var _i3 = avail.length - 1; _i3 >= 0; _i3--) { 3449 | if (!avail[_i3]) { 3450 | avail.splice(_i3, 1); 3451 | } 3452 | } 3453 | 3454 | return avail.length ? RNG$1.getItem(avail) : null; 3455 | }; 3456 | 3457 | _proto24._digLine = function _digLine(points) { 3458 | for (var i = 1; i < points.length; i++) { 3459 | var start = points[i - 1]; 3460 | var end = points[i]; 3461 | var corridor = new Corridor(start[0], start[1], end[0], end[1]); 3462 | corridor.create(this._digCallback); 3463 | 3464 | this._corridors.push(corridor); 3465 | } 3466 | }; 3467 | 3468 | _proto24._digCallback = function _digCallback(x, y, value) { 3469 | this._map[x][y] = value; 3470 | 3471 | if (value == 0) { 3472 | this._dug++; 3473 | } 3474 | }; 3475 | 3476 | _proto24._isWallCallback = function _isWallCallback(x, y) { 3477 | if (x < 0 || y < 0 || x >= this._width || y >= this._height) { 3478 | return false; 3479 | } 3480 | 3481 | return this._map[x][y] == 1; 3482 | }; 3483 | 3484 | _proto24._canBeDugCallback = function _canBeDugCallback(x, y) { 3485 | if (x < 1 || y < 1 || x + 1 >= this._width || y + 1 >= this._height) { 3486 | return false; 3487 | } 3488 | 3489 | return this._map[x][y] == 1; 3490 | }; 3491 | 3492 | return Uniform; 3493 | }(Dungeon); 3494 | 3495 | var Cellular = 3496 | /*#__PURE__*/ 3497 | function (_Map3) { 3498 | _inheritsLoose(Cellular, _Map3); 3499 | 3500 | function Cellular(width, height, options) { 3501 | var _this11; 3502 | 3503 | if (options === void 0) { 3504 | options = {}; 3505 | } 3506 | 3507 | _this11 = _Map3.call(this, width, height) || this; 3508 | _this11._options = { 3509 | born: [5, 6, 7, 8], 3510 | survive: [4, 5, 6, 7, 8], 3511 | topology: 8 3512 | }; 3513 | 3514 | _this11.setOptions(options); 3515 | 3516 | _this11._dirs = DIRS[_this11._options.topology]; 3517 | _this11._map = _this11._fillMap(0); 3518 | return _this11; 3519 | } 3520 | 3521 | var _proto25 = Cellular.prototype; 3522 | 3523 | _proto25.randomize = function randomize(probability) { 3524 | for (var i = 0; i < this._width; i++) { 3525 | for (var j = 0; j < this._height; j++) { 3526 | this._map[i][j] = RNG$1.getUniform() < probability ? 1 : 0; 3527 | } 3528 | } 3529 | 3530 | return this; 3531 | }; 3532 | 3533 | _proto25.setOptions = function setOptions(options) { 3534 | Object.assign(this._options, options); 3535 | }; 3536 | 3537 | _proto25.set = function set(x, y, value) { 3538 | this._map[x][y] = value; 3539 | }; 3540 | 3541 | _proto25.create = function create(callback) { 3542 | var newMap = this._fillMap(0); 3543 | 3544 | var born = this._options.born; 3545 | var survive = this._options.survive; 3546 | 3547 | for (var j = 0; j < this._height; j++) { 3548 | var widthStep = 1; 3549 | var widthStart = 0; 3550 | 3551 | if (this._options.topology == 6) { 3552 | widthStep = 2; 3553 | widthStart = j % 2; 3554 | } 3555 | 3556 | for (var i = widthStart; i < this._width; i += widthStep) { 3557 | var cur = this._map[i][j]; 3558 | 3559 | var ncount = this._getNeighbors(i, j); 3560 | 3561 | if (cur && survive.indexOf(ncount) != -1) { 3562 | newMap[i][j] = 1; 3563 | } else if (!cur && born.indexOf(ncount) != -1) { 3564 | newMap[i][j] = 1; 3565 | } 3566 | } 3567 | } 3568 | 3569 | this._map = newMap; 3570 | callback && this._serviceCallback(callback); 3571 | }; 3572 | 3573 | _proto25._serviceCallback = function _serviceCallback(callback) { 3574 | for (var j = 0; j < this._height; j++) { 3575 | var widthStep = 1; 3576 | var widthStart = 0; 3577 | 3578 | if (this._options.topology == 6) { 3579 | widthStep = 2; 3580 | widthStart = j % 2; 3581 | } 3582 | 3583 | for (var i = widthStart; i < this._width; i += widthStep) { 3584 | callback(i, j, this._map[i][j]); 3585 | } 3586 | } 3587 | }; 3588 | 3589 | _proto25._getNeighbors = function _getNeighbors(cx, cy) { 3590 | var result = 0; 3591 | 3592 | for (var i = 0; i < this._dirs.length; i++) { 3593 | var dir = this._dirs[i]; 3594 | var x = cx + dir[0]; 3595 | var y = cy + dir[1]; 3596 | 3597 | if (x < 0 || x >= this._width || y < 0 || y >= this._height) { 3598 | continue; 3599 | } 3600 | 3601 | result += this._map[x][y] == 1 ? 1 : 0; 3602 | } 3603 | 3604 | return result; 3605 | }; 3606 | 3607 | _proto25.connect = function connect(callback, value, connectionCallback) { 3608 | if (!value) value = 0; 3609 | var allFreeSpace = []; 3610 | var notConnected = {}; 3611 | var widthStep = 1; 3612 | var widthStarts = [0, 0]; 3613 | 3614 | if (this._options.topology == 6) { 3615 | widthStep = 2; 3616 | widthStarts = [0, 1]; 3617 | } 3618 | 3619 | for (var y = 0; y < this._height; y++) { 3620 | for (var x = widthStarts[y % 2]; x < this._width; x += widthStep) { 3621 | if (this._freeSpace(x, y, value)) { 3622 | var p = [x, y]; 3623 | notConnected[this._pointKey(p)] = p; 3624 | allFreeSpace.push([x, y]); 3625 | } 3626 | } 3627 | } 3628 | 3629 | var start = allFreeSpace[RNG$1.getUniformInt(0, allFreeSpace.length - 1)]; 3630 | 3631 | var key = this._pointKey(start); 3632 | 3633 | var connected = {}; 3634 | connected[key] = start; 3635 | delete notConnected[key]; 3636 | 3637 | this._findConnected(connected, notConnected, [start], false, value); 3638 | 3639 | while (Object.keys(notConnected).length > 0) { 3640 | var _p = this._getFromTo(connected, notConnected); 3641 | 3642 | var from = _p[0]; 3643 | var to = _p[1]; 3644 | var local = {}; 3645 | local[this._pointKey(from)] = from; 3646 | 3647 | this._findConnected(local, notConnected, [from], true, value); 3648 | 3649 | var tunnelFn = this._options.topology == 6 ? this._tunnelToConnected6 : this._tunnelToConnected; 3650 | tunnelFn.call(this, to, from, connected, notConnected, value, connectionCallback); 3651 | 3652 | for (var k in local) { 3653 | var pp = local[k]; 3654 | this._map[pp[0]][pp[1]] = value; 3655 | connected[k] = pp; 3656 | delete notConnected[k]; 3657 | } 3658 | } 3659 | 3660 | callback && this._serviceCallback(callback); 3661 | }; 3662 | 3663 | _proto25._getFromTo = function _getFromTo(connected, notConnected) { 3664 | var from = [0, 0], 3665 | to = [0, 0], 3666 | d; 3667 | var connectedKeys = Object.keys(connected); 3668 | var notConnectedKeys = Object.keys(notConnected); 3669 | 3670 | for (var i = 0; i < 5; i++) { 3671 | if (connectedKeys.length < notConnectedKeys.length) { 3672 | var keys = connectedKeys; 3673 | to = connected[keys[RNG$1.getUniformInt(0, keys.length - 1)]]; 3674 | from = this._getClosest(to, notConnected); 3675 | } else { 3676 | var _keys = notConnectedKeys; 3677 | from = notConnected[_keys[RNG$1.getUniformInt(0, _keys.length - 1)]]; 3678 | to = this._getClosest(from, connected); 3679 | } 3680 | 3681 | d = (from[0] - to[0]) * (from[0] - to[0]) + (from[1] - to[1]) * (from[1] - to[1]); 3682 | 3683 | if (d < 64) { 3684 | break; 3685 | } 3686 | } 3687 | 3688 | return [from, to]; 3689 | }; 3690 | 3691 | _proto25._getClosest = function _getClosest(point, space) { 3692 | var minPoint = null; 3693 | var minDist = null; 3694 | 3695 | for (var k in space) { 3696 | var p = space[k]; 3697 | var d = (p[0] - point[0]) * (p[0] - point[0]) + (p[1] - point[1]) * (p[1] - point[1]); 3698 | 3699 | if (minDist == null || d < minDist) { 3700 | minDist = d; 3701 | minPoint = p; 3702 | } 3703 | } 3704 | 3705 | return minPoint; 3706 | }; 3707 | 3708 | _proto25._findConnected = function _findConnected(connected, notConnected, stack, keepNotConnected, value) { 3709 | while (stack.length > 0) { 3710 | var p = stack.splice(0, 1)[0]; 3711 | var tests = void 0; 3712 | 3713 | if (this._options.topology == 6) { 3714 | tests = [[p[0] + 2, p[1]], [p[0] + 1, p[1] - 1], [p[0] - 1, p[1] - 1], [p[0] - 2, p[1]], [p[0] - 1, p[1] + 1], [p[0] + 1, p[1] + 1]]; 3715 | } else { 3716 | tests = [[p[0] + 1, p[1]], [p[0] - 1, p[1]], [p[0], p[1] + 1], [p[0], p[1] - 1]]; 3717 | } 3718 | 3719 | for (var i = 0; i < tests.length; i++) { 3720 | var key = this._pointKey(tests[i]); 3721 | 3722 | if (connected[key] == null && this._freeSpace(tests[i][0], tests[i][1], value)) { 3723 | connected[key] = tests[i]; 3724 | 3725 | if (!keepNotConnected) { 3726 | delete notConnected[key]; 3727 | } 3728 | 3729 | stack.push(tests[i]); 3730 | } 3731 | } 3732 | } 3733 | }; 3734 | 3735 | _proto25._tunnelToConnected = function _tunnelToConnected(to, from, connected, notConnected, value, connectionCallback) { 3736 | var a, b; 3737 | 3738 | if (from[0] < to[0]) { 3739 | a = from; 3740 | b = to; 3741 | } else { 3742 | a = to; 3743 | b = from; 3744 | } 3745 | 3746 | for (var xx = a[0]; xx <= b[0]; xx++) { 3747 | this._map[xx][a[1]] = value; 3748 | var p = [xx, a[1]]; 3749 | 3750 | var pkey = this._pointKey(p); 3751 | 3752 | connected[pkey] = p; 3753 | delete notConnected[pkey]; 3754 | } 3755 | 3756 | if (connectionCallback && a[0] < b[0]) { 3757 | connectionCallback(a, [b[0], a[1]]); 3758 | } 3759 | 3760 | var x = b[0]; 3761 | 3762 | if (from[1] < to[1]) { 3763 | a = from; 3764 | b = to; 3765 | } else { 3766 | a = to; 3767 | b = from; 3768 | } 3769 | 3770 | for (var yy = a[1]; yy < b[1]; yy++) { 3771 | this._map[x][yy] = value; 3772 | var _p2 = [x, yy]; 3773 | 3774 | var _pkey = this._pointKey(_p2); 3775 | 3776 | connected[_pkey] = _p2; 3777 | delete notConnected[_pkey]; 3778 | } 3779 | 3780 | if (connectionCallback && a[1] < b[1]) { 3781 | connectionCallback([b[0], a[1]], [b[0], b[1]]); 3782 | } 3783 | }; 3784 | 3785 | _proto25._tunnelToConnected6 = function _tunnelToConnected6(to, from, connected, notConnected, value, connectionCallback) { 3786 | var a, b; 3787 | 3788 | if (from[0] < to[0]) { 3789 | a = from; 3790 | b = to; 3791 | } else { 3792 | a = to; 3793 | b = from; 3794 | } 3795 | 3796 | var xx = a[0]; 3797 | var yy = a[1]; 3798 | 3799 | while (!(xx == b[0] && yy == b[1])) { 3800 | var stepWidth = 2; 3801 | 3802 | if (yy < b[1]) { 3803 | yy++; 3804 | stepWidth = 1; 3805 | } else if (yy > b[1]) { 3806 | yy--; 3807 | stepWidth = 1; 3808 | } 3809 | 3810 | if (xx < b[0]) { 3811 | xx += stepWidth; 3812 | } else if (xx > b[0]) { 3813 | xx -= stepWidth; 3814 | } else if (b[1] % 2) { 3815 | xx -= stepWidth; 3816 | } else { 3817 | xx += stepWidth; 3818 | } 3819 | 3820 | this._map[xx][yy] = value; 3821 | var p = [xx, yy]; 3822 | 3823 | var pkey = this._pointKey(p); 3824 | 3825 | connected[pkey] = p; 3826 | delete notConnected[pkey]; 3827 | } 3828 | 3829 | if (connectionCallback) { 3830 | connectionCallback(from, to); 3831 | } 3832 | }; 3833 | 3834 | _proto25._freeSpace = function _freeSpace(x, y, value) { 3835 | return x >= 0 && x < this._width && y >= 0 && y < this._height && this._map[x][y] == value; 3836 | }; 3837 | 3838 | _proto25._pointKey = function _pointKey(p) { 3839 | return p[0] + "." + p[1]; 3840 | }; 3841 | 3842 | return Cellular; 3843 | }(Map); 3844 | 3845 | var FEATURES = { 3846 | "room": Room, 3847 | "corridor": Corridor 3848 | }; 3849 | 3850 | var Digger = 3851 | /*#__PURE__*/ 3852 | function (_Dungeon2) { 3853 | _inheritsLoose(Digger, _Dungeon2); 3854 | 3855 | function Digger(width, height, options) { 3856 | var _this12; 3857 | 3858 | if (options === void 0) { 3859 | options = {}; 3860 | } 3861 | 3862 | _this12 = _Dungeon2.call(this, width, height) || this; 3863 | _this12._options = Object.assign({ 3864 | roomWidth: [3, 9], 3865 | roomHeight: [3, 5], 3866 | corridorLength: [3, 10], 3867 | dugPercentage: 0.2, 3868 | timeLimit: 1000 3869 | }, options); 3870 | _this12._features = { 3871 | "room": 4, 3872 | "corridor": 4 3873 | }; 3874 | _this12._map = []; 3875 | _this12._featureAttempts = 20; 3876 | _this12._walls = {}; 3877 | _this12._dug = 0; 3878 | _this12._digCallback = _this12._digCallback.bind(_assertThisInitialized(_assertThisInitialized(_this12))); 3879 | _this12._canBeDugCallback = _this12._canBeDugCallback.bind(_assertThisInitialized(_assertThisInitialized(_this12))); 3880 | _this12._isWallCallback = _this12._isWallCallback.bind(_assertThisInitialized(_assertThisInitialized(_this12))); 3881 | _this12._priorityWallCallback = _this12._priorityWallCallback.bind(_assertThisInitialized(_assertThisInitialized(_this12))); 3882 | return _this12; 3883 | } 3884 | 3885 | var _proto26 = Digger.prototype; 3886 | 3887 | _proto26.create = function create(callback) { 3888 | this._rooms = []; 3889 | this._corridors = []; 3890 | this._map = this._fillMap(1); 3891 | this._walls = {}; 3892 | this._dug = 0; 3893 | var area = (this._width - 2) * (this._height - 2); 3894 | 3895 | this._firstRoom(); 3896 | 3897 | var t1 = Date.now(); 3898 | var priorityWalls; 3899 | 3900 | do { 3901 | priorityWalls = 0; 3902 | var t2 = Date.now(); 3903 | 3904 | if (t2 - t1 > this._options.timeLimit) { 3905 | break; 3906 | } 3907 | 3908 | var wall = this._findWall(); 3909 | 3910 | if (!wall) { 3911 | break; 3912 | } 3913 | 3914 | var parts = wall.split(","); 3915 | var x = parseInt(parts[0]); 3916 | var y = parseInt(parts[1]); 3917 | 3918 | var dir = this._getDiggingDirection(x, y); 3919 | 3920 | if (!dir) { 3921 | continue; 3922 | } 3923 | 3924 | var featureAttempts = 0; 3925 | 3926 | do { 3927 | featureAttempts++; 3928 | 3929 | if (this._tryFeature(x, y, dir[0], dir[1])) { 3930 | this._removeSurroundingWalls(x, y); 3931 | 3932 | this._removeSurroundingWalls(x - dir[0], y - dir[1]); 3933 | 3934 | break; 3935 | } 3936 | } while (featureAttempts < this._featureAttempts); 3937 | 3938 | for (var id in this._walls) { 3939 | if (this._walls[id] > 1) { 3940 | priorityWalls++; 3941 | } 3942 | } 3943 | } while (this._dug / area < this._options.dugPercentage || priorityWalls); 3944 | 3945 | this._addDoors(); 3946 | 3947 | if (callback) { 3948 | for (var i = 0; i < this._width; i++) { 3949 | for (var j = 0; j < this._height; j++) { 3950 | callback(i, j, this._map[i][j]); 3951 | } 3952 | } 3953 | } 3954 | 3955 | this._walls = {}; 3956 | this._map = []; 3957 | return this; 3958 | }; 3959 | 3960 | _proto26._digCallback = function _digCallback(x, y, value) { 3961 | if (value == 0 || value == 2) { 3962 | this._map[x][y] = 0; 3963 | this._dug++; 3964 | } else { 3965 | this._walls[x + "," + y] = 1; 3966 | } 3967 | }; 3968 | 3969 | _proto26._isWallCallback = function _isWallCallback(x, y) { 3970 | if (x < 0 || y < 0 || x >= this._width || y >= this._height) { 3971 | return false; 3972 | } 3973 | 3974 | return this._map[x][y] == 1; 3975 | }; 3976 | 3977 | _proto26._canBeDugCallback = function _canBeDugCallback(x, y) { 3978 | if (x < 1 || y < 1 || x + 1 >= this._width || y + 1 >= this._height) { 3979 | return false; 3980 | } 3981 | 3982 | return this._map[x][y] == 1; 3983 | }; 3984 | 3985 | _proto26._priorityWallCallback = function _priorityWallCallback(x, y) { 3986 | this._walls[x + "," + y] = 2; 3987 | }; 3988 | 3989 | _proto26._firstRoom = function _firstRoom() { 3990 | var cx = Math.floor(this._width / 2); 3991 | var cy = Math.floor(this._height / 2); 3992 | var room = Room.createRandomCenter(cx, cy, this._options); 3993 | 3994 | this._rooms.push(room); 3995 | 3996 | room.create(this._digCallback); 3997 | }; 3998 | 3999 | _proto26._findWall = function _findWall() { 4000 | var prio1 = []; 4001 | var prio2 = []; 4002 | 4003 | for (var _id2 in this._walls) { 4004 | var prio = this._walls[_id2]; 4005 | 4006 | if (prio == 2) { 4007 | prio2.push(_id2); 4008 | } else { 4009 | prio1.push(_id2); 4010 | } 4011 | } 4012 | 4013 | var arr = prio2.length ? prio2 : prio1; 4014 | 4015 | if (!arr.length) { 4016 | return null; 4017 | } 4018 | 4019 | var id = RNG$1.getItem(arr.sort()); 4020 | delete this._walls[id]; 4021 | return id; 4022 | }; 4023 | 4024 | _proto26._tryFeature = function _tryFeature(x, y, dx, dy) { 4025 | var featureName = RNG$1.getWeightedValue(this._features); 4026 | var ctor = FEATURES[featureName]; 4027 | var feature = ctor.createRandomAt(x, y, dx, dy, this._options); 4028 | 4029 | if (!feature.isValid(this._isWallCallback, this._canBeDugCallback)) { 4030 | return false; 4031 | } 4032 | 4033 | feature.create(this._digCallback); 4034 | 4035 | if (feature instanceof Room) { 4036 | this._rooms.push(feature); 4037 | } 4038 | 4039 | if (feature instanceof Corridor) { 4040 | feature.createPriorityWalls(this._priorityWallCallback); 4041 | 4042 | this._corridors.push(feature); 4043 | } 4044 | 4045 | return true; 4046 | }; 4047 | 4048 | _proto26._removeSurroundingWalls = function _removeSurroundingWalls(cx, cy) { 4049 | var deltas = DIRS[4]; 4050 | 4051 | for (var i = 0; i < deltas.length; i++) { 4052 | var delta = deltas[i]; 4053 | var x = cx + delta[0]; 4054 | var y = cy + delta[1]; 4055 | delete this._walls[x + "," + y]; 4056 | x = cx + 2 * delta[0]; 4057 | y = cy + 2 * delta[1]; 4058 | delete this._walls[x + "," + y]; 4059 | } 4060 | }; 4061 | 4062 | _proto26._getDiggingDirection = function _getDiggingDirection(cx, cy) { 4063 | if (cx <= 0 || cy <= 0 || cx >= this._width - 1 || cy >= this._height - 1) { 4064 | return null; 4065 | } 4066 | 4067 | var result = null; 4068 | var deltas = DIRS[4]; 4069 | 4070 | for (var i = 0; i < deltas.length; i++) { 4071 | var delta = deltas[i]; 4072 | var x = cx + delta[0]; 4073 | var y = cy + delta[1]; 4074 | 4075 | if (!this._map[x][y]) { 4076 | if (result) { 4077 | return null; 4078 | } 4079 | 4080 | result = delta; 4081 | } 4082 | } 4083 | 4084 | if (!result) { 4085 | return null; 4086 | } 4087 | 4088 | return [-result[0], -result[1]]; 4089 | }; 4090 | 4091 | _proto26._addDoors = function _addDoors() { 4092 | var data = this._map; 4093 | 4094 | function isWallCallback(x, y) { 4095 | return data[x][y] == 1; 4096 | } 4097 | 4098 | for (var i = 0; i < this._rooms.length; i++) { 4099 | var room = this._rooms[i]; 4100 | room.clearDoors(); 4101 | room.addDoors(isWallCallback); 4102 | } 4103 | }; 4104 | 4105 | return Digger; 4106 | }(Dungeon); 4107 | 4108 | function addToList(i, L, R) { 4109 | R[L[i + 1]] = R[i]; 4110 | L[R[i]] = L[i + 1]; 4111 | R[i] = i + 1; 4112 | L[i + 1] = i; 4113 | } 4114 | 4115 | function removeFromList(i, L, R) { 4116 | R[L[i]] = R[i]; 4117 | L[R[i]] = L[i]; 4118 | R[i] = i; 4119 | L[i] = i; 4120 | } 4121 | 4122 | var EllerMaze = 4123 | /*#__PURE__*/ 4124 | function (_Map4) { 4125 | _inheritsLoose(EllerMaze, _Map4); 4126 | 4127 | function EllerMaze() { 4128 | return _Map4.apply(this, arguments) || this; 4129 | } 4130 | 4131 | var _proto27 = EllerMaze.prototype; 4132 | 4133 | _proto27.create = function create(callback) { 4134 | var map = this._fillMap(1); 4135 | 4136 | var w = Math.ceil((this._width - 2) / 2); 4137 | var rand = 9 / 24; 4138 | var L = []; 4139 | var R = []; 4140 | 4141 | for (var i = 0; i < w; i++) { 4142 | L.push(i); 4143 | R.push(i); 4144 | } 4145 | 4146 | L.push(w - 1); 4147 | var j; 4148 | 4149 | for (j = 1; j + 3 < this._height; j += 2) { 4150 | for (var _i4 = 0; _i4 < w; _i4++) { 4151 | var x = 2 * _i4 + 1; 4152 | var y = j; 4153 | map[x][y] = 0; 4154 | 4155 | if (_i4 != L[_i4 + 1] && RNG$1.getUniform() > rand) { 4156 | addToList(_i4, L, R); 4157 | map[x + 1][y] = 0; 4158 | } 4159 | 4160 | if (_i4 != L[_i4] && RNG$1.getUniform() > rand) { 4161 | removeFromList(_i4, L, R); 4162 | } else { 4163 | map[x][y + 1] = 0; 4164 | } 4165 | } 4166 | } 4167 | 4168 | for (var _i5 = 0; _i5 < w; _i5++) { 4169 | var _x2 = 2 * _i5 + 1; 4170 | 4171 | var _y2 = j; 4172 | map[_x2][_y2] = 0; 4173 | 4174 | if (_i5 != L[_i5 + 1] && (_i5 == L[_i5] || RNG$1.getUniform() > rand)) { 4175 | addToList(_i5, L, R); 4176 | map[_x2 + 1][_y2] = 0; 4177 | } 4178 | 4179 | removeFromList(_i5, L, R); 4180 | } 4181 | 4182 | for (var _i6 = 0; _i6 < this._width; _i6++) { 4183 | for (var _j = 0; _j < this._height; _j++) { 4184 | callback(_i6, _j, map[_i6][_j]); 4185 | } 4186 | } 4187 | 4188 | return this; 4189 | }; 4190 | 4191 | return EllerMaze; 4192 | }(Map); 4193 | 4194 | var DividedMaze = 4195 | /*#__PURE__*/ 4196 | function (_Map5) { 4197 | _inheritsLoose(DividedMaze, _Map5); 4198 | 4199 | function DividedMaze() { 4200 | var _this13; 4201 | 4202 | _this13 = _Map5.apply(this, arguments) || this; 4203 | _this13._stack = []; 4204 | _this13._map = []; 4205 | return _this13; 4206 | } 4207 | 4208 | var _proto28 = DividedMaze.prototype; 4209 | 4210 | _proto28.create = function create(callback) { 4211 | var w = this._width; 4212 | var h = this._height; 4213 | this._map = []; 4214 | 4215 | for (var i = 0; i < w; i++) { 4216 | this._map.push([]); 4217 | 4218 | for (var j = 0; j < h; j++) { 4219 | var border = i == 0 || j == 0 || i + 1 == w || j + 1 == h; 4220 | 4221 | this._map[i].push(border ? 1 : 0); 4222 | } 4223 | } 4224 | 4225 | this._stack = [[1, 1, w - 2, h - 2]]; 4226 | 4227 | this._process(); 4228 | 4229 | for (var _i7 = 0; _i7 < w; _i7++) { 4230 | for (var _j2 = 0; _j2 < h; _j2++) { 4231 | callback(_i7, _j2, this._map[_i7][_j2]); 4232 | } 4233 | } 4234 | 4235 | this._map = []; 4236 | return this; 4237 | }; 4238 | 4239 | _proto28._process = function _process() { 4240 | while (this._stack.length) { 4241 | var room = this._stack.shift(); 4242 | 4243 | this._partitionRoom(room); 4244 | } 4245 | }; 4246 | 4247 | _proto28._partitionRoom = function _partitionRoom(room) { 4248 | var availX = []; 4249 | var availY = []; 4250 | 4251 | for (var i = room[0] + 1; i < room[2]; i++) { 4252 | var top = this._map[i][room[1] - 1]; 4253 | var bottom = this._map[i][room[3] + 1]; 4254 | 4255 | if (top && bottom && !(i % 2)) { 4256 | availX.push(i); 4257 | } 4258 | } 4259 | 4260 | for (var j = room[1] + 1; j < room[3]; j++) { 4261 | var left = this._map[room[0] - 1][j]; 4262 | var right = this._map[room[2] + 1][j]; 4263 | 4264 | if (left && right && !(j % 2)) { 4265 | availY.push(j); 4266 | } 4267 | } 4268 | 4269 | if (!availX.length || !availY.length) { 4270 | return; 4271 | } 4272 | 4273 | var x = RNG$1.getItem(availX); 4274 | var y = RNG$1.getItem(availY); 4275 | this._map[x][y] = 1; 4276 | var walls = []; 4277 | var w = []; 4278 | walls.push(w); 4279 | 4280 | for (var _i8 = room[0]; _i8 < x; _i8++) { 4281 | this._map[_i8][y] = 1; 4282 | w.push([_i8, y]); 4283 | } 4284 | 4285 | w = []; 4286 | walls.push(w); 4287 | 4288 | for (var _i9 = x + 1; _i9 <= room[2]; _i9++) { 4289 | this._map[_i9][y] = 1; 4290 | w.push([_i9, y]); 4291 | } 4292 | 4293 | w = []; 4294 | walls.push(w); 4295 | 4296 | for (var _j3 = room[1]; _j3 < y; _j3++) { 4297 | this._map[x][_j3] = 1; 4298 | w.push([x, _j3]); 4299 | } 4300 | 4301 | w = []; 4302 | walls.push(w); 4303 | 4304 | for (var _j4 = y + 1; _j4 <= room[3]; _j4++) { 4305 | this._map[x][_j4] = 1; 4306 | w.push([x, _j4]); 4307 | } 4308 | 4309 | var solid = RNG$1.getItem(walls); 4310 | 4311 | for (var _i10 = 0; _i10 < walls.length; _i10++) { 4312 | var _w = walls[_i10]; 4313 | 4314 | if (_w == solid) { 4315 | continue; 4316 | } 4317 | 4318 | var hole = RNG$1.getItem(_w); 4319 | this._map[hole[0]][hole[1]] = 0; 4320 | } 4321 | 4322 | this._stack.push([room[0], room[1], x - 1, y - 1]); 4323 | 4324 | this._stack.push([x + 1, room[1], room[2], y - 1]); 4325 | 4326 | this._stack.push([room[0], y + 1, x - 1, room[3]]); 4327 | 4328 | this._stack.push([x + 1, y + 1, room[2], room[3]]); 4329 | }; 4330 | 4331 | return DividedMaze; 4332 | }(Map); 4333 | 4334 | var IceyMaze = 4335 | /*#__PURE__*/ 4336 | function (_Map6) { 4337 | _inheritsLoose(IceyMaze, _Map6); 4338 | 4339 | function IceyMaze(width, height, regularity) { 4340 | var _this14; 4341 | 4342 | if (regularity === void 0) { 4343 | regularity = 0; 4344 | } 4345 | 4346 | _this14 = _Map6.call(this, width, height) || this; 4347 | _this14._regularity = regularity; 4348 | _this14._map = []; 4349 | return _this14; 4350 | } 4351 | 4352 | var _proto29 = IceyMaze.prototype; 4353 | 4354 | _proto29.create = function create(callback) { 4355 | var width = this._width; 4356 | var height = this._height; 4357 | 4358 | var map = this._fillMap(1); 4359 | 4360 | width -= width % 2 ? 1 : 2; 4361 | height -= height % 2 ? 1 : 2; 4362 | var cx = 0; 4363 | var cy = 0; 4364 | var nx = 0; 4365 | var ny = 0; 4366 | var done = 0; 4367 | var blocked = false; 4368 | var dirs = [[0, 0], [0, 0], [0, 0], [0, 0]]; 4369 | 4370 | do { 4371 | cx = 1 + 2 * Math.floor(RNG$1.getUniform() * (width - 1) / 2); 4372 | cy = 1 + 2 * Math.floor(RNG$1.getUniform() * (height - 1) / 2); 4373 | 4374 | if (!done) { 4375 | map[cx][cy] = 0; 4376 | } 4377 | 4378 | if (!map[cx][cy]) { 4379 | this._randomize(dirs); 4380 | 4381 | do { 4382 | if (Math.floor(RNG$1.getUniform() * (this._regularity + 1)) == 0) { 4383 | this._randomize(dirs); 4384 | } 4385 | 4386 | blocked = true; 4387 | 4388 | for (var i = 0; i < 4; i++) { 4389 | nx = cx + dirs[i][0] * 2; 4390 | ny = cy + dirs[i][1] * 2; 4391 | 4392 | if (this._isFree(map, nx, ny, width, height)) { 4393 | map[nx][ny] = 0; 4394 | map[cx + dirs[i][0]][cy + dirs[i][1]] = 0; 4395 | cx = nx; 4396 | cy = ny; 4397 | blocked = false; 4398 | done++; 4399 | break; 4400 | } 4401 | } 4402 | } while (!blocked); 4403 | } 4404 | } while (done + 1 < width * height / 4); 4405 | 4406 | for (var _i11 = 0; _i11 < this._width; _i11++) { 4407 | for (var j = 0; j < this._height; j++) { 4408 | callback(_i11, j, map[_i11][j]); 4409 | } 4410 | } 4411 | 4412 | this._map = []; 4413 | return this; 4414 | }; 4415 | 4416 | _proto29._randomize = function _randomize(dirs) { 4417 | for (var i = 0; i < 4; i++) { 4418 | dirs[i][0] = 0; 4419 | dirs[i][1] = 0; 4420 | } 4421 | 4422 | switch (Math.floor(RNG$1.getUniform() * 4)) { 4423 | case 0: 4424 | dirs[0][0] = -1; 4425 | dirs[1][0] = 1; 4426 | dirs[2][1] = -1; 4427 | dirs[3][1] = 1; 4428 | break; 4429 | 4430 | case 1: 4431 | dirs[3][0] = -1; 4432 | dirs[2][0] = 1; 4433 | dirs[1][1] = -1; 4434 | dirs[0][1] = 1; 4435 | break; 4436 | 4437 | case 2: 4438 | dirs[2][0] = -1; 4439 | dirs[3][0] = 1; 4440 | dirs[0][1] = -1; 4441 | dirs[1][1] = 1; 4442 | break; 4443 | 4444 | case 3: 4445 | dirs[1][0] = -1; 4446 | dirs[0][0] = 1; 4447 | dirs[3][1] = -1; 4448 | dirs[2][1] = 1; 4449 | break; 4450 | } 4451 | }; 4452 | 4453 | _proto29._isFree = function _isFree(map, x, y, width, height) { 4454 | if (x < 1 || y < 1 || x >= width || y >= height) { 4455 | return false; 4456 | } 4457 | 4458 | return map[x][y]; 4459 | }; 4460 | 4461 | return IceyMaze; 4462 | }(Map); 4463 | 4464 | var Rogue = 4465 | /*#__PURE__*/ 4466 | function (_Map7) { 4467 | _inheritsLoose(Rogue, _Map7); 4468 | 4469 | function Rogue(width, height, options) { 4470 | var _this15; 4471 | 4472 | _this15 = _Map7.call(this, width, height) || this; 4473 | _this15.map = []; 4474 | _this15.rooms = []; 4475 | _this15.connectedCells = []; 4476 | options = Object.assign({ 4477 | cellWidth: 3, 4478 | cellHeight: 3 4479 | }, options); 4480 | 4481 | if (!options.hasOwnProperty("roomWidth")) { 4482 | options["roomWidth"] = _this15._calculateRoomSize(_this15._width, options["cellWidth"]); 4483 | } 4484 | 4485 | if (!options.hasOwnProperty("roomHeight")) { 4486 | options["roomHeight"] = _this15._calculateRoomSize(_this15._height, options["cellHeight"]); 4487 | } 4488 | 4489 | _this15._options = options; 4490 | return _this15; 4491 | } 4492 | 4493 | var _proto30 = Rogue.prototype; 4494 | 4495 | _proto30.create = function create(callback) { 4496 | this.map = this._fillMap(1); 4497 | this.rooms = []; 4498 | this.connectedCells = []; 4499 | 4500 | this._initRooms(); 4501 | 4502 | this._connectRooms(); 4503 | 4504 | this._connectUnconnectedRooms(); 4505 | 4506 | this._createRandomRoomConnections(); 4507 | 4508 | this._createRooms(); 4509 | 4510 | this._createCorridors(); 4511 | 4512 | if (callback) { 4513 | for (var i = 0; i < this._width; i++) { 4514 | for (var j = 0; j < this._height; j++) { 4515 | callback(i, j, this.map[i][j]); 4516 | } 4517 | } 4518 | } 4519 | 4520 | return this; 4521 | }; 4522 | 4523 | _proto30._calculateRoomSize = function _calculateRoomSize(size, cell) { 4524 | var max = Math.floor(size / cell * 0.8); 4525 | var min = Math.floor(size / cell * 0.25); 4526 | 4527 | if (min < 2) { 4528 | min = 2; 4529 | } 4530 | 4531 | if (max < 2) { 4532 | max = 2; 4533 | } 4534 | 4535 | return [min, max]; 4536 | }; 4537 | 4538 | _proto30._initRooms = function _initRooms() { 4539 | for (var i = 0; i < this._options.cellWidth; i++) { 4540 | this.rooms.push([]); 4541 | 4542 | for (var j = 0; j < this._options.cellHeight; j++) { 4543 | this.rooms[i].push({ 4544 | "x": 0, 4545 | "y": 0, 4546 | "width": 0, 4547 | "height": 0, 4548 | "connections": [], 4549 | "cellx": i, 4550 | "celly": j 4551 | }); 4552 | } 4553 | } 4554 | }; 4555 | 4556 | _proto30._connectRooms = function _connectRooms() { 4557 | var cgx = RNG$1.getUniformInt(0, this._options.cellWidth - 1); 4558 | var cgy = RNG$1.getUniformInt(0, this._options.cellHeight - 1); 4559 | var idx; 4560 | var ncgx; 4561 | var ncgy; 4562 | var found = false; 4563 | var room; 4564 | var otherRoom; 4565 | var dirToCheck; 4566 | 4567 | do { 4568 | dirToCheck = [0, 2, 4, 6]; 4569 | dirToCheck = RNG$1.shuffle(dirToCheck); 4570 | 4571 | do { 4572 | found = false; 4573 | idx = dirToCheck.pop(); 4574 | ncgx = cgx + DIRS[8][idx][0]; 4575 | ncgy = cgy + DIRS[8][idx][1]; 4576 | 4577 | if (ncgx < 0 || ncgx >= this._options.cellWidth) { 4578 | continue; 4579 | } 4580 | 4581 | if (ncgy < 0 || ncgy >= this._options.cellHeight) { 4582 | continue; 4583 | } 4584 | 4585 | room = this.rooms[cgx][cgy]; 4586 | 4587 | if (room["connections"].length > 0) { 4588 | if (room["connections"][0][0] == ncgx && room["connections"][0][1] == ncgy) { 4589 | break; 4590 | } 4591 | } 4592 | 4593 | otherRoom = this.rooms[ncgx][ncgy]; 4594 | 4595 | if (otherRoom["connections"].length == 0) { 4596 | otherRoom["connections"].push([cgx, cgy]); 4597 | this.connectedCells.push([ncgx, ncgy]); 4598 | cgx = ncgx; 4599 | cgy = ncgy; 4600 | found = true; 4601 | } 4602 | } while (dirToCheck.length > 0 && found == false); 4603 | } while (dirToCheck.length > 0); 4604 | }; 4605 | 4606 | _proto30._connectUnconnectedRooms = function _connectUnconnectedRooms() { 4607 | var cw = this._options.cellWidth; 4608 | var ch = this._options.cellHeight; 4609 | this.connectedCells = RNG$1.shuffle(this.connectedCells); 4610 | var room; 4611 | var otherRoom; 4612 | var validRoom; 4613 | 4614 | for (var i = 0; i < this._options.cellWidth; i++) { 4615 | for (var j = 0; j < this._options.cellHeight; j++) { 4616 | room = this.rooms[i][j]; 4617 | 4618 | if (room["connections"].length == 0) { 4619 | var directions = [0, 2, 4, 6]; 4620 | directions = RNG$1.shuffle(directions); 4621 | validRoom = false; 4622 | 4623 | do { 4624 | var dirIdx = directions.pop(); 4625 | var newI = i + DIRS[8][dirIdx][0]; 4626 | var newJ = j + DIRS[8][dirIdx][1]; 4627 | 4628 | if (newI < 0 || newI >= cw || newJ < 0 || newJ >= ch) { 4629 | continue; 4630 | } 4631 | 4632 | otherRoom = this.rooms[newI][newJ]; 4633 | validRoom = true; 4634 | 4635 | if (otherRoom["connections"].length == 0) { 4636 | break; 4637 | } 4638 | 4639 | for (var k = 0; k < otherRoom["connections"].length; k++) { 4640 | if (otherRoom["connections"][k][0] == i && otherRoom["connections"][k][1] == j) { 4641 | validRoom = false; 4642 | break; 4643 | } 4644 | } 4645 | 4646 | if (validRoom) { 4647 | break; 4648 | } 4649 | } while (directions.length); 4650 | 4651 | if (validRoom) { 4652 | room["connections"].push([otherRoom["cellx"], otherRoom["celly"]]); 4653 | } else { 4654 | console.log("-- Unable to connect room."); 4655 | } 4656 | } 4657 | } 4658 | } 4659 | }; 4660 | 4661 | _proto30._createRandomRoomConnections = function _createRandomRoomConnections() {}; 4662 | 4663 | _proto30._createRooms = function _createRooms() { 4664 | var w = this._width; 4665 | var h = this._height; 4666 | var cw = this._options.cellWidth; 4667 | var ch = this._options.cellHeight; 4668 | var cwp = Math.floor(this._width / cw); 4669 | var chp = Math.floor(this._height / ch); 4670 | var roomw; 4671 | var roomh; 4672 | var roomWidth = this._options["roomWidth"]; 4673 | var roomHeight = this._options["roomHeight"]; 4674 | var sx; 4675 | var sy; 4676 | var otherRoom; 4677 | 4678 | for (var i = 0; i < cw; i++) { 4679 | for (var j = 0; j < ch; j++) { 4680 | sx = cwp * i; 4681 | sy = chp * j; 4682 | 4683 | if (sx == 0) { 4684 | sx = 1; 4685 | } 4686 | 4687 | if (sy == 0) { 4688 | sy = 1; 4689 | } 4690 | 4691 | roomw = RNG$1.getUniformInt(roomWidth[0], roomWidth[1]); 4692 | roomh = RNG$1.getUniformInt(roomHeight[0], roomHeight[1]); 4693 | 4694 | if (j > 0) { 4695 | otherRoom = this.rooms[i][j - 1]; 4696 | 4697 | while (sy - (otherRoom["y"] + otherRoom["height"]) < 3) { 4698 | sy++; 4699 | } 4700 | } 4701 | 4702 | if (i > 0) { 4703 | otherRoom = this.rooms[i - 1][j]; 4704 | 4705 | while (sx - (otherRoom["x"] + otherRoom["width"]) < 3) { 4706 | sx++; 4707 | } 4708 | } 4709 | 4710 | var sxOffset = Math.round(RNG$1.getUniformInt(0, cwp - roomw) / 2); 4711 | var syOffset = Math.round(RNG$1.getUniformInt(0, chp - roomh) / 2); 4712 | 4713 | while (sx + sxOffset + roomw >= w) { 4714 | if (sxOffset) { 4715 | sxOffset--; 4716 | } else { 4717 | roomw--; 4718 | } 4719 | } 4720 | 4721 | while (sy + syOffset + roomh >= h) { 4722 | if (syOffset) { 4723 | syOffset--; 4724 | } else { 4725 | roomh--; 4726 | } 4727 | } 4728 | 4729 | sx = sx + sxOffset; 4730 | sy = sy + syOffset; 4731 | this.rooms[i][j]["x"] = sx; 4732 | this.rooms[i][j]["y"] = sy; 4733 | this.rooms[i][j]["width"] = roomw; 4734 | this.rooms[i][j]["height"] = roomh; 4735 | 4736 | for (var ii = sx; ii < sx + roomw; ii++) { 4737 | for (var jj = sy; jj < sy + roomh; jj++) { 4738 | this.map[ii][jj] = 0; 4739 | } 4740 | } 4741 | } 4742 | } 4743 | }; 4744 | 4745 | _proto30._getWallPosition = function _getWallPosition(aRoom, aDirection) { 4746 | var rx; 4747 | var ry; 4748 | var door; 4749 | 4750 | if (aDirection == 1 || aDirection == 3) { 4751 | rx = RNG$1.getUniformInt(aRoom["x"] + 1, aRoom["x"] + aRoom["width"] - 2); 4752 | 4753 | if (aDirection == 1) { 4754 | ry = aRoom["y"] - 2; 4755 | door = ry + 1; 4756 | } else { 4757 | ry = aRoom["y"] + aRoom["height"] + 1; 4758 | door = ry - 1; 4759 | } 4760 | 4761 | this.map[rx][door] = 0; 4762 | } else { 4763 | ry = RNG$1.getUniformInt(aRoom["y"] + 1, aRoom["y"] + aRoom["height"] - 2); 4764 | 4765 | if (aDirection == 2) { 4766 | rx = aRoom["x"] + aRoom["width"] + 1; 4767 | door = rx - 1; 4768 | } else { 4769 | rx = aRoom["x"] - 2; 4770 | door = rx + 1; 4771 | } 4772 | 4773 | this.map[door][ry] = 0; 4774 | } 4775 | 4776 | return [rx, ry]; 4777 | }; 4778 | 4779 | _proto30._drawCorridor = function _drawCorridor(startPosition, endPosition) { 4780 | var xOffset = endPosition[0] - startPosition[0]; 4781 | var yOffset = endPosition[1] - startPosition[1]; 4782 | var xpos = startPosition[0]; 4783 | var ypos = startPosition[1]; 4784 | var tempDist; 4785 | var xDir; 4786 | var yDir; 4787 | var move; 4788 | var moves = []; 4789 | var xAbs = Math.abs(xOffset); 4790 | var yAbs = Math.abs(yOffset); 4791 | var percent = RNG$1.getUniform(); 4792 | var firstHalf = percent; 4793 | var secondHalf = 1 - percent; 4794 | xDir = xOffset > 0 ? 2 : 6; 4795 | yDir = yOffset > 0 ? 4 : 0; 4796 | 4797 | if (xAbs < yAbs) { 4798 | tempDist = Math.ceil(yAbs * firstHalf); 4799 | moves.push([yDir, tempDist]); 4800 | moves.push([xDir, xAbs]); 4801 | tempDist = Math.floor(yAbs * secondHalf); 4802 | moves.push([yDir, tempDist]); 4803 | } else { 4804 | tempDist = Math.ceil(xAbs * firstHalf); 4805 | moves.push([xDir, tempDist]); 4806 | moves.push([yDir, yAbs]); 4807 | tempDist = Math.floor(xAbs * secondHalf); 4808 | moves.push([xDir, tempDist]); 4809 | } 4810 | 4811 | this.map[xpos][ypos] = 0; 4812 | 4813 | while (moves.length > 0) { 4814 | move = moves.pop(); 4815 | 4816 | while (move[1] > 0) { 4817 | xpos += DIRS[8][move[0]][0]; 4818 | ypos += DIRS[8][move[0]][1]; 4819 | this.map[xpos][ypos] = 0; 4820 | move[1] = move[1] - 1; 4821 | } 4822 | } 4823 | }; 4824 | 4825 | _proto30._createCorridors = function _createCorridors() { 4826 | var cw = this._options.cellWidth; 4827 | var ch = this._options.cellHeight; 4828 | var room; 4829 | var connection; 4830 | var otherRoom; 4831 | var wall; 4832 | var otherWall; 4833 | 4834 | for (var i = 0; i < cw; i++) { 4835 | for (var j = 0; j < ch; j++) { 4836 | room = this.rooms[i][j]; 4837 | 4838 | for (var k = 0; k < room["connections"].length; k++) { 4839 | connection = room["connections"][k]; 4840 | otherRoom = this.rooms[connection[0]][connection[1]]; 4841 | 4842 | if (otherRoom["cellx"] > room["cellx"]) { 4843 | wall = 2; 4844 | otherWall = 4; 4845 | } else if (otherRoom["cellx"] < room["cellx"]) { 4846 | wall = 4; 4847 | otherWall = 2; 4848 | } else if (otherRoom["celly"] > room["celly"]) { 4849 | wall = 3; 4850 | otherWall = 1; 4851 | } else { 4852 | wall = 1; 4853 | otherWall = 3; 4854 | } 4855 | 4856 | this._drawCorridor(this._getWallPosition(room, wall), this._getWallPosition(otherRoom, otherWall)); 4857 | } 4858 | } 4859 | } 4860 | }; 4861 | 4862 | return Rogue; 4863 | }(Map); 4864 | 4865 | var index$2 = { 4866 | Arena: Arena, 4867 | Uniform: Uniform, 4868 | Cellular: Cellular, 4869 | Digger: Digger, 4870 | EllerMaze: EllerMaze, 4871 | DividedMaze: DividedMaze, 4872 | IceyMaze: IceyMaze, 4873 | Rogue: Rogue 4874 | }; 4875 | 4876 | var Noise = function Noise() {}; 4877 | 4878 | var F2 = 0.5 * (Math.sqrt(3) - 1); 4879 | var G2 = (3 - Math.sqrt(3)) / 6; 4880 | 4881 | var Simplex = 4882 | /*#__PURE__*/ 4883 | function (_Noise) { 4884 | _inheritsLoose(Simplex, _Noise); 4885 | 4886 | function Simplex(gradients) { 4887 | var _this16; 4888 | 4889 | if (gradients === void 0) { 4890 | gradients = 256; 4891 | } 4892 | 4893 | _this16 = _Noise.call(this) || this; 4894 | _this16._gradients = [[0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1]]; 4895 | var permutations = []; 4896 | 4897 | for (var i = 0; i < gradients; i++) { 4898 | permutations.push(i); 4899 | } 4900 | 4901 | permutations = RNG$1.shuffle(permutations); 4902 | _this16._perms = []; 4903 | _this16._indexes = []; 4904 | 4905 | for (var _i12 = 0; _i12 < 2 * gradients; _i12++) { 4906 | _this16._perms.push(permutations[_i12 % gradients]); 4907 | 4908 | _this16._indexes.push(_this16._perms[_i12] % _this16._gradients.length); 4909 | } 4910 | 4911 | return _this16; 4912 | } 4913 | 4914 | var _proto31 = Simplex.prototype; 4915 | 4916 | _proto31.get = function get(xin, yin) { 4917 | var perms = this._perms; 4918 | var indexes = this._indexes; 4919 | var count = perms.length / 2; 4920 | var n0 = 0, 4921 | n1 = 0, 4922 | n2 = 0, 4923 | gi; 4924 | var s = (xin + yin) * F2; 4925 | var i = Math.floor(xin + s); 4926 | var j = Math.floor(yin + s); 4927 | var t = (i + j) * G2; 4928 | var X0 = i - t; 4929 | var Y0 = j - t; 4930 | var x0 = xin - X0; 4931 | var y0 = yin - Y0; 4932 | var i1, j1; 4933 | 4934 | if (x0 > y0) { 4935 | i1 = 1; 4936 | j1 = 0; 4937 | } else { 4938 | i1 = 0; 4939 | j1 = 1; 4940 | } 4941 | 4942 | var x1 = x0 - i1 + G2; 4943 | var y1 = y0 - j1 + G2; 4944 | var x2 = x0 - 1 + 2 * G2; 4945 | var y2 = y0 - 1 + 2 * G2; 4946 | var ii = mod(i, count); 4947 | var jj = mod(j, count); 4948 | var t0 = 0.5 - x0 * x0 - y0 * y0; 4949 | 4950 | if (t0 >= 0) { 4951 | t0 *= t0; 4952 | gi = indexes[ii + perms[jj]]; 4953 | var grad = this._gradients[gi]; 4954 | n0 = t0 * t0 * (grad[0] * x0 + grad[1] * y0); 4955 | } 4956 | 4957 | var t1 = 0.5 - x1 * x1 - y1 * y1; 4958 | 4959 | if (t1 >= 0) { 4960 | t1 *= t1; 4961 | gi = indexes[ii + i1 + perms[jj + j1]]; 4962 | var _grad = this._gradients[gi]; 4963 | n1 = t1 * t1 * (_grad[0] * x1 + _grad[1] * y1); 4964 | } 4965 | 4966 | var t2 = 0.5 - x2 * x2 - y2 * y2; 4967 | 4968 | if (t2 >= 0) { 4969 | t2 *= t2; 4970 | gi = indexes[ii + 1 + perms[jj + 1]]; 4971 | var _grad2 = this._gradients[gi]; 4972 | n2 = t2 * t2 * (_grad2[0] * x2 + _grad2[1] * y2); 4973 | } 4974 | 4975 | return 70 * (n0 + n1 + n2); 4976 | }; 4977 | 4978 | return Simplex; 4979 | }(Noise); 4980 | 4981 | var index$3 = { 4982 | Simplex: Simplex 4983 | }; 4984 | 4985 | var Path = 4986 | /*#__PURE__*/ 4987 | function () { 4988 | function Path(toX, toY, passableCallback, options) { 4989 | if (options === void 0) { 4990 | options = {}; 4991 | } 4992 | 4993 | this._toX = toX; 4994 | this._toY = toY; 4995 | this._passableCallback = passableCallback; 4996 | this._options = Object.assign({ 4997 | topology: 8 4998 | }, options); 4999 | this._dirs = DIRS[this._options.topology]; 5000 | 5001 | if (this._options.topology == 8) { 5002 | 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]]; 5003 | } 5004 | } 5005 | 5006 | var _proto32 = Path.prototype; 5007 | 5008 | _proto32._getNeighbors = function _getNeighbors(cx, cy) { 5009 | var result = []; 5010 | 5011 | for (var i = 0; i < this._dirs.length; i++) { 5012 | var dir = this._dirs[i]; 5013 | var x = cx + dir[0]; 5014 | var y = cy + dir[1]; 5015 | 5016 | if (!this._passableCallback(x, y)) { 5017 | continue; 5018 | } 5019 | 5020 | result.push([x, y]); 5021 | } 5022 | 5023 | return result; 5024 | }; 5025 | 5026 | return Path; 5027 | }(); 5028 | 5029 | var Dijkstra = 5030 | /*#__PURE__*/ 5031 | function (_Path) { 5032 | _inheritsLoose(Dijkstra, _Path); 5033 | 5034 | function Dijkstra(toX, toY, passableCallback, options) { 5035 | var _this17; 5036 | 5037 | _this17 = _Path.call(this, toX, toY, passableCallback, options) || this; 5038 | _this17._computed = {}; 5039 | _this17._todo = []; 5040 | 5041 | _this17._add(toX, toY, null); 5042 | 5043 | return _this17; 5044 | } 5045 | 5046 | var _proto33 = Dijkstra.prototype; 5047 | 5048 | _proto33.compute = function compute(fromX, fromY, callback) { 5049 | var key = fromX + "," + fromY; 5050 | 5051 | if (!(key in this._computed)) { 5052 | this._compute(fromX, fromY); 5053 | } 5054 | 5055 | if (!(key in this._computed)) { 5056 | return; 5057 | } 5058 | 5059 | var item = this._computed[key]; 5060 | 5061 | while (item) { 5062 | callback(item.x, item.y); 5063 | item = item.prev; 5064 | } 5065 | }; 5066 | 5067 | _proto33._compute = function _compute(fromX, fromY) { 5068 | while (this._todo.length) { 5069 | var item = this._todo.shift(); 5070 | 5071 | if (item.x == fromX && item.y == fromY) { 5072 | return; 5073 | } 5074 | 5075 | var neighbors = this._getNeighbors(item.x, item.y); 5076 | 5077 | for (var i = 0; i < neighbors.length; i++) { 5078 | var neighbor = neighbors[i]; 5079 | var x = neighbor[0]; 5080 | var y = neighbor[1]; 5081 | var id = x + "," + y; 5082 | 5083 | if (id in this._computed) { 5084 | continue; 5085 | } 5086 | 5087 | this._add(x, y, item); 5088 | } 5089 | } 5090 | }; 5091 | 5092 | _proto33._add = function _add(x, y, prev) { 5093 | var obj = { 5094 | x: x, 5095 | y: y, 5096 | prev: prev 5097 | }; 5098 | this._computed[x + "," + y] = obj; 5099 | 5100 | this._todo.push(obj); 5101 | }; 5102 | 5103 | return Dijkstra; 5104 | }(Path); 5105 | 5106 | var AStar = 5107 | /*#__PURE__*/ 5108 | function (_Path2) { 5109 | _inheritsLoose(AStar, _Path2); 5110 | 5111 | function AStar(toX, toY, passableCallback, options) { 5112 | var _this18; 5113 | 5114 | if (options === void 0) { 5115 | options = {}; 5116 | } 5117 | 5118 | _this18 = _Path2.call(this, toX, toY, passableCallback, options) || this; 5119 | _this18._todo = []; 5120 | _this18._done = {}; 5121 | return _this18; 5122 | } 5123 | 5124 | var _proto34 = AStar.prototype; 5125 | 5126 | _proto34.compute = function compute(fromX, fromY, callback) { 5127 | this._todo = []; 5128 | this._done = {}; 5129 | this._fromX = fromX; 5130 | this._fromY = fromY; 5131 | 5132 | this._add(this._toX, this._toY, null); 5133 | 5134 | while (this._todo.length) { 5135 | var _item = this._todo.shift(); 5136 | 5137 | var id = _item.x + "," + _item.y; 5138 | 5139 | if (id in this._done) { 5140 | continue; 5141 | } 5142 | 5143 | this._done[id] = _item; 5144 | 5145 | if (_item.x == fromX && _item.y == fromY) { 5146 | break; 5147 | } 5148 | 5149 | var neighbors = this._getNeighbors(_item.x, _item.y); 5150 | 5151 | for (var i = 0; i < neighbors.length; i++) { 5152 | var neighbor = neighbors[i]; 5153 | var x = neighbor[0]; 5154 | var y = neighbor[1]; 5155 | 5156 | var _id3 = x + "," + y; 5157 | 5158 | if (_id3 in this._done) { 5159 | continue; 5160 | } 5161 | 5162 | this._add(x, y, _item); 5163 | } 5164 | } 5165 | 5166 | var item = this._done[fromX + "," + fromY]; 5167 | 5168 | if (!item) { 5169 | return; 5170 | } 5171 | 5172 | while (item) { 5173 | callback(item.x, item.y); 5174 | item = item.prev; 5175 | } 5176 | }; 5177 | 5178 | _proto34._add = function _add(x, y, prev) { 5179 | var h = this._distance(x, y); 5180 | 5181 | var obj = { 5182 | x: x, 5183 | y: y, 5184 | prev: prev, 5185 | g: prev ? prev.g + 1 : 0, 5186 | h: h 5187 | }; 5188 | var f = obj.g + obj.h; 5189 | 5190 | for (var i = 0; i < this._todo.length; i++) { 5191 | var item = this._todo[i]; 5192 | var itemF = item.g + item.h; 5193 | 5194 | if (f < itemF || f == itemF && h < item.h) { 5195 | this._todo.splice(i, 0, obj); 5196 | 5197 | return; 5198 | } 5199 | } 5200 | 5201 | this._todo.push(obj); 5202 | }; 5203 | 5204 | _proto34._distance = function _distance(x, y) { 5205 | switch (this._options.topology) { 5206 | case 4: 5207 | return Math.abs(x - this._fromX) + Math.abs(y - this._fromY); 5208 | break; 5209 | 5210 | case 6: 5211 | var dx = Math.abs(x - this._fromX); 5212 | var dy = Math.abs(y - this._fromY); 5213 | return dy + Math.max(0, (dx - dy) / 2); 5214 | break; 5215 | 5216 | case 8: 5217 | return Math.max(Math.abs(x - this._fromX), Math.abs(y - this._fromY)); 5218 | break; 5219 | } 5220 | }; 5221 | 5222 | return AStar; 5223 | }(Path); 5224 | 5225 | var index$4 = { 5226 | Dijkstra: Dijkstra, 5227 | AStar: AStar 5228 | }; 5229 | 5230 | var Engine = 5231 | /*#__PURE__*/ 5232 | function () { 5233 | function Engine(scheduler) { 5234 | this._scheduler = scheduler; 5235 | this._lock = 1; 5236 | } 5237 | 5238 | var _proto35 = Engine.prototype; 5239 | 5240 | _proto35.start = function start() { 5241 | return this.unlock(); 5242 | }; 5243 | 5244 | _proto35.lock = function lock() { 5245 | this._lock++; 5246 | return this; 5247 | }; 5248 | 5249 | _proto35.unlock = function unlock() { 5250 | if (!this._lock) { 5251 | throw new Error("Cannot unlock unlocked engine"); 5252 | } 5253 | 5254 | this._lock--; 5255 | 5256 | while (!this._lock) { 5257 | var actor = this._scheduler.next(); 5258 | 5259 | if (!actor) { 5260 | return this.lock(); 5261 | } 5262 | 5263 | var result = actor.act(); 5264 | 5265 | if (result && result.then) { 5266 | this.lock(); 5267 | result.then(this.unlock.bind(this)); 5268 | } 5269 | } 5270 | 5271 | return this; 5272 | }; 5273 | 5274 | return Engine; 5275 | }(); 5276 | 5277 | var Lighting = 5278 | /*#__PURE__*/ 5279 | function () { 5280 | function Lighting(reflectivityCallback, options) { 5281 | if (options === void 0) { 5282 | options = {}; 5283 | } 5284 | 5285 | this._reflectivityCallback = reflectivityCallback; 5286 | this._options = {}; 5287 | options = Object.assign({ 5288 | passes: 1, 5289 | emissionThreshold: 100, 5290 | range: 10 5291 | }, options); 5292 | this._lights = {}; 5293 | this._reflectivityCache = {}; 5294 | this._fovCache = {}; 5295 | this.setOptions(options); 5296 | } 5297 | 5298 | var _proto36 = Lighting.prototype; 5299 | 5300 | _proto36.setOptions = function setOptions(options) { 5301 | Object.assign(this._options, options); 5302 | 5303 | if (options && options.range) { 5304 | this.reset(); 5305 | } 5306 | 5307 | return this; 5308 | }; 5309 | 5310 | _proto36.setFOV = function setFOV(fov) { 5311 | this._fov = fov; 5312 | this._fovCache = {}; 5313 | return this; 5314 | }; 5315 | 5316 | _proto36.setLight = function setLight(x, y, color) { 5317 | var key = x + "," + y; 5318 | 5319 | if (color) { 5320 | this._lights[key] = typeof color == "string" ? fromString(color) : color; 5321 | } else { 5322 | delete this._lights[key]; 5323 | } 5324 | 5325 | return this; 5326 | }; 5327 | 5328 | _proto36.clearLights = function clearLights() { 5329 | this._lights = {}; 5330 | }; 5331 | 5332 | _proto36.reset = function reset() { 5333 | this._reflectivityCache = {}; 5334 | this._fovCache = {}; 5335 | return this; 5336 | }; 5337 | 5338 | _proto36.compute = function compute(lightingCallback) { 5339 | var doneCells = {}; 5340 | var emittingCells = {}; 5341 | var litCells = {}; 5342 | 5343 | for (var key in this._lights) { 5344 | var light = this._lights[key]; 5345 | emittingCells[key] = [0, 0, 0]; 5346 | add_(emittingCells[key], light); 5347 | } 5348 | 5349 | for (var i = 0; i < this._options.passes; i++) { 5350 | this._emitLight(emittingCells, litCells, doneCells); 5351 | 5352 | if (i + 1 == this._options.passes) { 5353 | continue; 5354 | } 5355 | 5356 | emittingCells = this._computeEmitters(litCells, doneCells); 5357 | } 5358 | 5359 | for (var litKey in litCells) { 5360 | var parts = litKey.split(","); 5361 | var x = parseInt(parts[0]); 5362 | var y = parseInt(parts[1]); 5363 | lightingCallback(x, y, litCells[litKey]); 5364 | } 5365 | 5366 | return this; 5367 | }; 5368 | 5369 | _proto36._emitLight = function _emitLight(emittingCells, litCells, doneCells) { 5370 | for (var key in emittingCells) { 5371 | var parts = key.split(","); 5372 | var x = parseInt(parts[0]); 5373 | var y = parseInt(parts[1]); 5374 | 5375 | this._emitLightFromCell(x, y, emittingCells[key], litCells); 5376 | 5377 | doneCells[key] = 1; 5378 | } 5379 | 5380 | return this; 5381 | }; 5382 | 5383 | _proto36._computeEmitters = function _computeEmitters(litCells, doneCells) { 5384 | var result = {}; 5385 | 5386 | for (var key in litCells) { 5387 | if (key in doneCells) { 5388 | continue; 5389 | } 5390 | 5391 | var _color = litCells[key]; 5392 | var reflectivity = void 0; 5393 | 5394 | if (key in this._reflectivityCache) { 5395 | reflectivity = this._reflectivityCache[key]; 5396 | } else { 5397 | var parts = key.split(","); 5398 | var x = parseInt(parts[0]); 5399 | var y = parseInt(parts[1]); 5400 | reflectivity = this._reflectivityCallback(x, y); 5401 | this._reflectivityCache[key] = reflectivity; 5402 | } 5403 | 5404 | if (reflectivity == 0) { 5405 | continue; 5406 | } 5407 | 5408 | var emission = [0, 0, 0]; 5409 | var intensity = 0; 5410 | 5411 | for (var i = 0; i < 3; i++) { 5412 | var part = Math.round(_color[i] * reflectivity); 5413 | emission[i] = part; 5414 | intensity += part; 5415 | } 5416 | 5417 | if (intensity > this._options.emissionThreshold) { 5418 | result[key] = emission; 5419 | } 5420 | } 5421 | 5422 | return result; 5423 | }; 5424 | 5425 | _proto36._emitLightFromCell = function _emitLightFromCell(x, y, color, litCells) { 5426 | var key = x + "," + y; 5427 | var fov; 5428 | 5429 | if (key in this._fovCache) { 5430 | fov = this._fovCache[key]; 5431 | } else { 5432 | fov = this._updateFOV(x, y); 5433 | } 5434 | 5435 | for (var fovKey in fov) { 5436 | var formFactor = fov[fovKey]; 5437 | var result = void 0; 5438 | 5439 | if (fovKey in litCells) { 5440 | result = litCells[fovKey]; 5441 | } else { 5442 | result = [0, 0, 0]; 5443 | litCells[fovKey] = result; 5444 | } 5445 | 5446 | for (var i = 0; i < 3; i++) { 5447 | result[i] += Math.round(color[i] * formFactor); 5448 | } 5449 | } 5450 | 5451 | return this; 5452 | }; 5453 | 5454 | _proto36._updateFOV = function _updateFOV(x, y) { 5455 | var key1 = x + "," + y; 5456 | var cache = {}; 5457 | this._fovCache[key1] = cache; 5458 | var range = this._options.range; 5459 | 5460 | function cb(x, y, r, vis) { 5461 | var key2 = x + "," + y; 5462 | var formFactor = vis * (1 - r / range); 5463 | 5464 | if (formFactor == 0) { 5465 | return; 5466 | } 5467 | 5468 | cache[key2] = formFactor; 5469 | } 5470 | 5471 | this._fov.compute(x, y, range, cb.bind(this)); 5472 | 5473 | return cache; 5474 | }; 5475 | 5476 | return Lighting; 5477 | }(); 5478 | 5479 | var Util = util; 5480 | var Color = color; 5481 | var Text = text; 5482 | exports.Util = Util; 5483 | exports.Color = Color; 5484 | exports.Text = Text; 5485 | exports.RNG = RNG$1; 5486 | exports.Display = Display; 5487 | exports.StringGenerator = StringGenerator; 5488 | exports.EventQueue = EventQueue; 5489 | exports.Scheduler = index; 5490 | exports.FOV = index$1; 5491 | exports.Map = index$2; 5492 | exports.Noise = index$3; 5493 | exports.Path = index$4; 5494 | exports.Engine = Engine; 5495 | exports.Lighting = Lighting; 5496 | exports.DEFAULT_WIDTH = DEFAULT_WIDTH; 5497 | exports.DEFAULT_HEIGHT = DEFAULT_HEIGHT; 5498 | exports.DIRS = DIRS; 5499 | exports.KEYS = KEYS; 5500 | Object.defineProperty(exports, '__esModule', { 5501 | value: true 5502 | }); 5503 | }); 5504 | 5505 | --------------------------------------------------------------------------------