diff --git a/Cargo.toml b/Cargo.toml index ff44a43..98116ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mapgen" -version = "0.2.1" +version = "0.3.0" authors = ["Krzysztof Langner "] description = "Map generator for games (dungeons, worlds etc.)" keywords = ["game", "map", "map-generator"] diff --git a/README.md b/README.md index a3ee855..0320b6e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Generate procedural maps for games. [Try it in the browser](https://klangner.git ### Dungeons * Map generators - * [ ] BSP Interior + * [x] BSP Interior * [x] BSP Rooms * [x] Cellular automata * [ ] Diffusion-Limited Aggregation (DLA) diff --git a/demo/src/lib.rs b/demo/src/lib.rs index 3e25a78..d3cc4d8 100644 --- a/demo/src/lib.rs +++ b/demo/src/lib.rs @@ -6,7 +6,7 @@ use mapgen::dungeon::{ map::TileType, cellular_automata::CellularAutomataGen, simple_rooms::SimpleRoomsGen, - bsp_rooms::BspRoomsGen, + bsp_interior::BspInteriorGen, starting_point::{AreaStartingPosition, XStart, YStart}, cull_unreachable::CullUnreachable, distant_exit::DistantExit, @@ -64,11 +64,10 @@ impl World { tiles } } - pub fn new_bsp_rooms(width: u32, height: u32, seed: u32) -> World { - World::print_map_info(format!("BSP Rooms with the seed: {}", seed)); + pub fn new_bsp_interior(width: u32, height: u32, seed: u32) -> World { + World::print_map_info(format!("BSP Interior with the seed: {}", seed)); let mut rng = StdRng::seed_from_u64(seed as u64); - let map = MapBuilder::new(BspRoomsGen::new()) - .with(NearestCorridors::new()) + let map = MapBuilder::new(BspInteriorGen::new()) .build_map_with_rng(width as usize, height as usize, &mut rng); let tiles = (0..map.tiles.len()) .map(|i| if map.tiles[i] == TileType::Floor {Cell::Floor} else {Cell::Wall}) @@ -87,7 +86,7 @@ impl World { } else if px < 0.6666 { World::new_simple_rooms(width, height, seed) } else { - World::new_bsp_rooms(width, height, seed) + World::new_bsp_interior(width, height, seed) } } diff --git a/demo/www/index.html b/demo/www/index.html index 0b9200a..2426dee 100644 --- a/demo/www/index.html +++ b/demo/www/index.html @@ -37,7 +37,7 @@ diff --git a/demo/www/index.js b/demo/www/index.js index d1ff6f2..5b973b9 100644 --- a/demo/www/index.js +++ b/demo/www/index.js @@ -32,9 +32,9 @@ function newSimpleRooms() { requestAnimationFrame(renderLoop); } -function newBspRooms() { +function newBspInterior() { var seed = Date.now(); - world = World.new_bsp_rooms(width, height, seed); + world = World.new_bsp_interior(width, height, seed); requestAnimationFrame(renderLoop); } @@ -107,5 +107,5 @@ newRandomGen(); // Connect UI element document.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata); document.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms); -document.getElementById('bsp-rooms-option').addEventListener('click', newBspRooms); +document.getElementById('bsp-interior-option').addEventListener('click', newBspInterior); document.getElementById('random-option').addEventListener('click', newRandomGen); diff --git a/docs/0.bootstrap.js b/docs/0.bootstrap.js index 18d39ea..3024424 100644 --- a/docs/0.bootstrap.js +++ b/docs/0.bootstrap.js @@ -4,11 +4,11 @@ /*!*****************************!*\ !*** ../pkg/mapgen_demo.js ***! \*****************************/ -/*! exports provided: Cell, World, __wbindgen_throw */ +/*! exports provided: Cell, World, __wbindgen_object_drop_ref, __wbg_getRandomValues_3ac1b33c90b52596, __wbg_randomFillSync_6f956029658662ec, __wbg_self_1c83eb4471d9eb9b, __wbg_static_accessor_MODULE_abf5ae284bffdf45, __wbg_require_5b2b5b594d809d9f, __wbg_crypto_c12f14e810edcaa2, __wbg_msCrypto_679be765111ba775, __wbindgen_is_undefined, __wbg_getRandomValues_05a60bf171bfc2be, __wbg_instanceof_Window_adf3196bdc02b386, __wbg_document_6cc8d0b87c0a99b9, __wbg_getElementById_0cb6ad9511b1efc0, __wbg_setinnerHTML_4ff235db1a3cb4d8, __wbg_call_8e95613cc6524977, __wbindgen_object_clone_ref, __wbg_newnoargs_f3b8a801d5d4b079, __wbg_self_07b2f89e82ceb76d, __wbg_window_ba85d88572adc0dc, __wbg_globalThis_b9277fc37e201fe5, __wbg_global_e16303fe83e1d57f, __wbindgen_throw */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mapgen_demo_bg.wasm */ \"../pkg/mapgen_demo_bg.wasm\");\n/* harmony import */ var _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./mapgen_demo_bg.js */ \"../pkg/mapgen_demo_bg.js\");\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"Cell\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"Cell\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"World\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"World\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_throw\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbindgen_throw\"]; });\n\n\n\n\n//# sourceURL=webpack:///../pkg/mapgen_demo.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mapgen_demo_bg.wasm */ \"../pkg/mapgen_demo_bg.wasm\");\n/* harmony import */ var _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./mapgen_demo_bg.js */ \"../pkg/mapgen_demo_bg.js\");\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"Cell\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"Cell\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"World\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"World\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_object_drop_ref\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbindgen_object_drop_ref\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getRandomValues_3ac1b33c90b52596\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_getRandomValues_3ac1b33c90b52596\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_randomFillSync_6f956029658662ec\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_randomFillSync_6f956029658662ec\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_self_1c83eb4471d9eb9b\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_self_1c83eb4471d9eb9b\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_static_accessor_MODULE_abf5ae284bffdf45\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_static_accessor_MODULE_abf5ae284bffdf45\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_require_5b2b5b594d809d9f\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_require_5b2b5b594d809d9f\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_crypto_c12f14e810edcaa2\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_crypto_c12f14e810edcaa2\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_msCrypto_679be765111ba775\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_msCrypto_679be765111ba775\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_is_undefined\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbindgen_is_undefined\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getRandomValues_05a60bf171bfc2be\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_getRandomValues_05a60bf171bfc2be\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_instanceof_Window_adf3196bdc02b386\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_instanceof_Window_adf3196bdc02b386\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_document_6cc8d0b87c0a99b9\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_document_6cc8d0b87c0a99b9\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getElementById_0cb6ad9511b1efc0\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_getElementById_0cb6ad9511b1efc0\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_setinnerHTML_4ff235db1a3cb4d8\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_setinnerHTML_4ff235db1a3cb4d8\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_call_8e95613cc6524977\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_call_8e95613cc6524977\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_object_clone_ref\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbindgen_object_clone_ref\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_newnoargs_f3b8a801d5d4b079\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_newnoargs_f3b8a801d5d4b079\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_self_07b2f89e82ceb76d\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_self_07b2f89e82ceb76d\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_window_ba85d88572adc0dc\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_window_ba85d88572adc0dc\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_globalThis_b9277fc37e201fe5\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_globalThis_b9277fc37e201fe5\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbg_global_e16303fe83e1d57f\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbg_global_e16303fe83e1d57f\"]; });\n\n/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_throw\", function() { return _mapgen_demo_bg_js__WEBPACK_IMPORTED_MODULE_1__[\"__wbindgen_throw\"]; });\n\n\n\n\n//# sourceURL=webpack:///../pkg/mapgen_demo.js?"); /***/ }), @@ -16,11 +16,11 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _map /*!********************************!*\ !*** ../pkg/mapgen_demo_bg.js ***! \********************************/ -/*! exports provided: Cell, World, __wbindgen_throw */ +/*! exports provided: Cell, World, __wbindgen_object_drop_ref, __wbg_getRandomValues_3ac1b33c90b52596, __wbg_randomFillSync_6f956029658662ec, __wbg_self_1c83eb4471d9eb9b, __wbg_static_accessor_MODULE_abf5ae284bffdf45, __wbg_require_5b2b5b594d809d9f, __wbg_crypto_c12f14e810edcaa2, __wbg_msCrypto_679be765111ba775, __wbindgen_is_undefined, __wbg_getRandomValues_05a60bf171bfc2be, __wbg_instanceof_Window_adf3196bdc02b386, __wbg_document_6cc8d0b87c0a99b9, __wbg_getElementById_0cb6ad9511b1efc0, __wbg_setinnerHTML_4ff235db1a3cb4d8, __wbg_call_8e95613cc6524977, __wbindgen_object_clone_ref, __wbg_newnoargs_f3b8a801d5d4b079, __wbg_self_07b2f89e82ceb76d, __wbg_window_ba85d88572adc0dc, __wbg_globalThis_b9277fc37e201fe5, __wbg_global_e16303fe83e1d57f, __wbindgen_throw */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* WEBPACK VAR INJECTION */(function(module) {/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"Cell\", function() { return Cell; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"World\", function() { return World; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_throw\", function() { return __wbindgen_throw; });\n/* harmony import */ var _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mapgen_demo_bg.wasm */ \"../pkg/mapgen_demo_bg.wasm\");\n\n\nconst lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;\n\nlet cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });\n\ncachedTextDecoder.decode();\n\nlet cachegetUint8Memory0 = null;\nfunction getUint8Memory0() {\n if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"memory\"].buffer) {\n cachegetUint8Memory0 = new Uint8Array(_mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"memory\"].buffer);\n }\n return cachegetUint8Memory0;\n}\n\nfunction getStringFromWasm0(ptr, len) {\n return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));\n}\n/**\n*/\nconst Cell = Object.freeze({ Floor:0,\"0\":\"Floor\",Wall:1,\"1\":\"Wall\", });\n/**\n*/\nclass World {\n\n static __wrap(ptr) {\n const obj = Object.create(World.prototype);\n obj.ptr = ptr;\n\n return obj;\n }\n\n free() {\n const ptr = this.ptr;\n this.ptr = 0;\n\n _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"__wbg_world_free\"](ptr);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_cellular_automata(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_cellular_automata\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_random_rooms(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_random_rooms\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @returns {number}\n */\n width() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_width\"](this.ptr);\n return ret >>> 0;\n }\n /**\n * @returns {number}\n */\n height() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_height\"](this.ptr);\n return ret >>> 0;\n }\n /**\n * @returns {number}\n */\n tiles() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_tiles\"](this.ptr);\n return ret;\n }\n}\n\nconst __wbindgen_throw = function(arg0, arg1) {\n throw new Error(getStringFromWasm0(arg0, arg1));\n};\n\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../www/node_modules/webpack/buildin/harmony-module.js */ \"./node_modules/webpack/buildin/harmony-module.js\")(module)))\n\n//# sourceURL=webpack:///../pkg/mapgen_demo_bg.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* WEBPACK VAR INJECTION */(function(module, global) {/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"Cell\", function() { return Cell; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"World\", function() { return World; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_object_drop_ref\", function() { return __wbindgen_object_drop_ref; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getRandomValues_3ac1b33c90b52596\", function() { return __wbg_getRandomValues_3ac1b33c90b52596; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_randomFillSync_6f956029658662ec\", function() { return __wbg_randomFillSync_6f956029658662ec; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_self_1c83eb4471d9eb9b\", function() { return __wbg_self_1c83eb4471d9eb9b; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_static_accessor_MODULE_abf5ae284bffdf45\", function() { return __wbg_static_accessor_MODULE_abf5ae284bffdf45; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_require_5b2b5b594d809d9f\", function() { return __wbg_require_5b2b5b594d809d9f; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_crypto_c12f14e810edcaa2\", function() { return __wbg_crypto_c12f14e810edcaa2; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_msCrypto_679be765111ba775\", function() { return __wbg_msCrypto_679be765111ba775; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_is_undefined\", function() { return __wbindgen_is_undefined; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getRandomValues_05a60bf171bfc2be\", function() { return __wbg_getRandomValues_05a60bf171bfc2be; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_instanceof_Window_adf3196bdc02b386\", function() { return __wbg_instanceof_Window_adf3196bdc02b386; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_document_6cc8d0b87c0a99b9\", function() { return __wbg_document_6cc8d0b87c0a99b9; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_getElementById_0cb6ad9511b1efc0\", function() { return __wbg_getElementById_0cb6ad9511b1efc0; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_setinnerHTML_4ff235db1a3cb4d8\", function() { return __wbg_setinnerHTML_4ff235db1a3cb4d8; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_call_8e95613cc6524977\", function() { return __wbg_call_8e95613cc6524977; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_object_clone_ref\", function() { return __wbindgen_object_clone_ref; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_newnoargs_f3b8a801d5d4b079\", function() { return __wbg_newnoargs_f3b8a801d5d4b079; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_self_07b2f89e82ceb76d\", function() { return __wbg_self_07b2f89e82ceb76d; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_window_ba85d88572adc0dc\", function() { return __wbg_window_ba85d88572adc0dc; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_globalThis_b9277fc37e201fe5\", function() { return __wbg_globalThis_b9277fc37e201fe5; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbg_global_e16303fe83e1d57f\", function() { return __wbg_global_e16303fe83e1d57f; });\n/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, \"__wbindgen_throw\", function() { return __wbindgen_throw; });\n/* harmony import */ var _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./mapgen_demo_bg.wasm */ \"../pkg/mapgen_demo_bg.wasm\");\n\n\nconst heap = new Array(32).fill(undefined);\n\nheap.push(undefined, null, true, false);\n\nfunction getObject(idx) { return heap[idx]; }\n\nlet heap_next = heap.length;\n\nfunction dropObject(idx) {\n if (idx < 36) return;\n heap[idx] = heap_next;\n heap_next = idx;\n}\n\nfunction takeObject(idx) {\n const ret = getObject(idx);\n dropObject(idx);\n return ret;\n}\n\nfunction addHeapObject(obj) {\n if (heap_next === heap.length) heap.push(heap.length + 1);\n const idx = heap_next;\n heap_next = heap[idx];\n\n heap[idx] = obj;\n return idx;\n}\n\nconst lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;\n\nlet cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });\n\ncachedTextDecoder.decode();\n\nlet cachegetUint8Memory0 = null;\nfunction getUint8Memory0() {\n if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"memory\"].buffer) {\n cachegetUint8Memory0 = new Uint8Array(_mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"memory\"].buffer);\n }\n return cachegetUint8Memory0;\n}\n\nfunction getStringFromWasm0(ptr, len) {\n return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));\n}\n\nfunction handleError(f) {\n return function () {\n try {\n return f.apply(this, arguments);\n\n } catch (e) {\n _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"__wbindgen_exn_store\"](addHeapObject(e));\n }\n };\n}\n\nfunction getArrayU8FromWasm0(ptr, len) {\n return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);\n}\n\nfunction isLikeNone(x) {\n return x === undefined || x === null;\n}\n/**\n*/\nconst Cell = Object.freeze({ Floor:0,\"0\":\"Floor\",Wall:1,\"1\":\"Wall\", });\n/**\n*/\nclass World {\n\n static __wrap(ptr) {\n const obj = Object.create(World.prototype);\n obj.ptr = ptr;\n\n return obj;\n }\n\n free() {\n const ptr = this.ptr;\n this.ptr = 0;\n\n _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"__wbg_world_free\"](ptr);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_cellular_automata(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_cellular_automata\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_simple_rooms(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_simple_rooms\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_bsp_interior(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_bsp_interior\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @param {number} width\n * @param {number} height\n * @param {number} seed\n * @returns {World}\n */\n static new_random(width, height, seed) {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_new_random\"](width, height, seed);\n return World.__wrap(ret);\n }\n /**\n * @returns {number}\n */\n width() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_width\"](this.ptr);\n return ret >>> 0;\n }\n /**\n * @returns {number}\n */\n height() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_height\"](this.ptr);\n return ret >>> 0;\n }\n /**\n * @returns {number}\n */\n tiles() {\n var ret = _mapgen_demo_bg_wasm__WEBPACK_IMPORTED_MODULE_0__[\"world_tiles\"](this.ptr);\n return ret;\n }\n}\n\nconst __wbindgen_object_drop_ref = function(arg0) {\n takeObject(arg0);\n};\n\nconst __wbg_getRandomValues_3ac1b33c90b52596 = function(arg0, arg1, arg2) {\n getObject(arg0).getRandomValues(getArrayU8FromWasm0(arg1, arg2));\n};\n\nconst __wbg_randomFillSync_6f956029658662ec = function(arg0, arg1, arg2) {\n getObject(arg0).randomFillSync(getArrayU8FromWasm0(arg1, arg2));\n};\n\nconst __wbg_self_1c83eb4471d9eb9b = handleError(function() {\n var ret = self.self;\n return addHeapObject(ret);\n});\n\nconst __wbg_static_accessor_MODULE_abf5ae284bffdf45 = function() {\n var ret = module;\n return addHeapObject(ret);\n};\n\nconst __wbg_require_5b2b5b594d809d9f = function(arg0, arg1, arg2) {\n var ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));\n return addHeapObject(ret);\n};\n\nconst __wbg_crypto_c12f14e810edcaa2 = function(arg0) {\n var ret = getObject(arg0).crypto;\n return addHeapObject(ret);\n};\n\nconst __wbg_msCrypto_679be765111ba775 = function(arg0) {\n var ret = getObject(arg0).msCrypto;\n return addHeapObject(ret);\n};\n\nconst __wbindgen_is_undefined = function(arg0) {\n var ret = getObject(arg0) === undefined;\n return ret;\n};\n\nconst __wbg_getRandomValues_05a60bf171bfc2be = function(arg0) {\n var ret = getObject(arg0).getRandomValues;\n return addHeapObject(ret);\n};\n\nconst __wbg_instanceof_Window_adf3196bdc02b386 = function(arg0) {\n var ret = getObject(arg0) instanceof Window;\n return ret;\n};\n\nconst __wbg_document_6cc8d0b87c0a99b9 = function(arg0) {\n var ret = getObject(arg0).document;\n return isLikeNone(ret) ? 0 : addHeapObject(ret);\n};\n\nconst __wbg_getElementById_0cb6ad9511b1efc0 = function(arg0, arg1, arg2) {\n var ret = getObject(arg0).getElementById(getStringFromWasm0(arg1, arg2));\n return isLikeNone(ret) ? 0 : addHeapObject(ret);\n};\n\nconst __wbg_setinnerHTML_4ff235db1a3cb4d8 = function(arg0, arg1, arg2) {\n getObject(arg0).innerHTML = getStringFromWasm0(arg1, arg2);\n};\n\nconst __wbg_call_8e95613cc6524977 = handleError(function(arg0, arg1) {\n var ret = getObject(arg0).call(getObject(arg1));\n return addHeapObject(ret);\n});\n\nconst __wbindgen_object_clone_ref = function(arg0) {\n var ret = getObject(arg0);\n return addHeapObject(ret);\n};\n\nconst __wbg_newnoargs_f3b8a801d5d4b079 = function(arg0, arg1) {\n var ret = new Function(getStringFromWasm0(arg0, arg1));\n return addHeapObject(ret);\n};\n\nconst __wbg_self_07b2f89e82ceb76d = handleError(function() {\n var ret = self.self;\n return addHeapObject(ret);\n});\n\nconst __wbg_window_ba85d88572adc0dc = handleError(function() {\n var ret = window.window;\n return addHeapObject(ret);\n});\n\nconst __wbg_globalThis_b9277fc37e201fe5 = handleError(function() {\n var ret = globalThis.globalThis;\n return addHeapObject(ret);\n});\n\nconst __wbg_global_e16303fe83e1d57f = handleError(function() {\n var ret = global.global;\n return addHeapObject(ret);\n});\n\nconst __wbindgen_throw = function(arg0, arg1) {\n throw new Error(getStringFromWasm0(arg0, arg1));\n};\n\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../www/node_modules/webpack/buildin/harmony-module.js */ \"./node_modules/webpack/buildin/harmony-module.js\")(module), __webpack_require__(/*! ./../www/node_modules/webpack/buildin/global.js */ \"./node_modules/webpack/buildin/global.js\")))\n\n//# sourceURL=webpack:///../pkg/mapgen_demo_bg.js?"); /***/ }), @@ -28,7 +28,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* WEBPACK VAR INJECTION */(f /*!**********************************!*\ !*** ../pkg/mapgen_demo_bg.wasm ***! \**********************************/ -/*! exports provided: memory, __wbg_world_free, world_new_cellular_automata, world_new_random_rooms, world_width, world_height, world_tiles */ +/*! exports provided: memory, __wbg_world_free, world_new_cellular_automata, world_new_simple_rooms, world_new_bsp_interior, world_new_random, world_width, world_height, world_tiles, __wbindgen_exn_store */ /***/ (function(module, exports, __webpack_require__) { eval("\"use strict\";\n// Instantiate WebAssembly module\nvar wasmExports = __webpack_require__.w[module.i];\n__webpack_require__.r(exports);\n// export exports from WebAssembly module\nfor(var name in wasmExports) if(name != \"__webpack_init__\") exports[name] = wasmExports[name];\n// exec imports from WebAssembly module (for esm order)\n/* harmony import */ var m0 = __webpack_require__(/*! ./mapgen_demo_bg.js */ \"../pkg/mapgen_demo_bg.js\");\n\n\n// exec wasm module\nwasmExports[\"__webpack_init__\"]()\n\n//# sourceURL=webpack:///../pkg/mapgen_demo_bg.wasm?"); @@ -43,7 +43,18 @@ eval("\"use strict\";\n// Instantiate WebAssembly module\nvar wasmExports = __we /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var mapgen_demo__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! mapgen-demo */ \"../pkg/mapgen_demo.js\");\n/* harmony import */ var mapgen_demo_mapgen_demo_bg__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! mapgen-demo/mapgen_demo_bg */ \"../pkg/mapgen_demo_bg.wasm\");\n\n\n\nconst CELL_SIZE = 12;\nconst GRID_COLOR = \"#CCCCCC\";\nconst DEAD_COLOR = \"#FFFFFF\";\nconst ALIVE_COLOR = \"#000000\";\n\nvar world = null;\nconst width = 80;\nconst height = 50;\n\nconst infoDiv = document.getElementById('map-info');\n// Give the canvas room for all of our cells and a 1px border\n// around each of them.\nconst canvas = document.getElementById(\"mapgen-canvas\");\ncanvas.height = (CELL_SIZE + 1) * height + 1;\ncanvas.width = (CELL_SIZE + 1) * width + 1;\n\nconst ctx = canvas.getContext('2d');\n\n// Map generators\nfunction newCellularAutomata() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_cellular_automata(width, height, seed);\n requestAnimationFrame(renderLoop);\n infoDiv.textContent = \"Cellular Automata with the seed: \" + seed;\n}\n\nfunction newSimpleRooms() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_random_rooms(width, height, seed);\n requestAnimationFrame(renderLoop);\n infoDiv.textContent = \"Random Rooms with the seed: \" + seed;\n}\n\nconst renderLoop = () => {\n // universe.tick();\n\n drawGrid();\n drawCells();\n\n requestAnimationFrame(renderLoop);\n};\n\nconst drawGrid = () => {\n ctx.beginPath();\n ctx.strokeStyle = GRID_COLOR;\n\n // Vertical lines.\n for (let i = 0; i <= width; i++) {\n ctx.moveTo(i * (CELL_SIZE + 1) + 1, 0);\n ctx.lineTo(i * (CELL_SIZE + 1) + 1, (CELL_SIZE + 1) * height + 1);\n }\n\n // Horizontal lines.\n for (let j = 0; j <= height; j++) {\n ctx.moveTo(0, j * (CELL_SIZE + 1) + 1);\n ctx.lineTo((CELL_SIZE + 1) * width + 1, j * (CELL_SIZE + 1) + 1);\n }\n\n ctx.stroke();\n};\n\nconst getIndex = (row, column) => {\n return row * width + column;\n};\n\nconst drawCells = () => {\n const tilesPtr = world.tiles();\n const tiles = new Uint8Array(mapgen_demo_mapgen_demo_bg__WEBPACK_IMPORTED_MODULE_1__[\"memory\"].buffer, tilesPtr, width * height);\n\n ctx.beginPath();\n\n for (let row = 0; row < height; row++) {\n for (let col = 0; col < width; col++) {\n const idx = getIndex(row, col);\n\n ctx.fillStyle = tiles[idx] == mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"Cell\"].Floor\n ? DEAD_COLOR\n : ALIVE_COLOR;\n\n ctx.fillRect(\n col * (CELL_SIZE + 1) + 1,\n row * (CELL_SIZE + 1) + 1,\n CELL_SIZE,\n CELL_SIZE\n );\n }\n }\n\n ctx.stroke();\n};\n\nnewCellularAutomata();\n\n// Connect UI element\ndocument.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata);\ndocument.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms);\n\n\n//# sourceURL=webpack:///./index.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var mapgen_demo__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! mapgen-demo */ \"../pkg/mapgen_demo.js\");\n/* harmony import */ var mapgen_demo_mapgen_demo_bg__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! mapgen-demo/mapgen_demo_bg */ \"../pkg/mapgen_demo_bg.wasm\");\n\n\n\nconst CELL_SIZE = 12;\nconst GRID_COLOR = \"#CCCCCC\";\nconst DEAD_COLOR = \"#FFFFFF\";\nconst ALIVE_COLOR = \"#000000\";\n\nvar world = null;\nconst width = 80;\nconst height = 50;\n\nconst infoDiv = document.getElementById('map-info');\n// Give the canvas room for all of our cells and a 1px border\n// around each of them.\nconst canvas = document.getElementById(\"mapgen-canvas\");\ncanvas.height = (CELL_SIZE + 1) * height + 1;\ncanvas.width = (CELL_SIZE + 1) * width + 1;\n\nconst ctx = canvas.getContext('2d');\n\n// Map generators\nfunction newCellularAutomata() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_cellular_automata(width, height, seed);\n requestAnimationFrame(renderLoop);\n}\n\nfunction newSimpleRooms() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_simple_rooms(width, height, seed);\n requestAnimationFrame(renderLoop);\n}\n\nfunction newBspInterior() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_bsp_interior(width, height, seed);\n requestAnimationFrame(renderLoop);\n}\n\nfunction newRandomGen() {\n var seed = Date.now();\n world = mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"World\"].new_random(width, height, seed);\n requestAnimationFrame(renderLoop);\n}\n\nconst renderLoop = () => {\n // universe.tick();\n\n drawGrid();\n drawCells();\n\n requestAnimationFrame(renderLoop);\n};\n\nconst drawGrid = () => {\n ctx.beginPath();\n ctx.strokeStyle = GRID_COLOR;\n\n // Vertical lines.\n for (let i = 0; i <= width; i++) {\n ctx.moveTo(i * (CELL_SIZE + 1) + 1, 0);\n ctx.lineTo(i * (CELL_SIZE + 1) + 1, (CELL_SIZE + 1) * height + 1);\n }\n\n // Horizontal lines.\n for (let j = 0; j <= height; j++) {\n ctx.moveTo(0, j * (CELL_SIZE + 1) + 1);\n ctx.lineTo((CELL_SIZE + 1) * width + 1, j * (CELL_SIZE + 1) + 1);\n }\n\n ctx.stroke();\n};\n\nconst getIndex = (row, column) => {\n return row * width + column;\n};\n\nconst drawCells = () => {\n const tilesPtr = world.tiles();\n const tiles = new Uint8Array(mapgen_demo_mapgen_demo_bg__WEBPACK_IMPORTED_MODULE_1__[\"memory\"].buffer, tilesPtr, width * height);\n\n ctx.beginPath();\n\n for (let row = 0; row < height; row++) {\n for (let col = 0; col < width; col++) {\n const idx = getIndex(row, col);\n\n ctx.fillStyle = tiles[idx] == mapgen_demo__WEBPACK_IMPORTED_MODULE_0__[\"Cell\"].Floor\n ? DEAD_COLOR\n : ALIVE_COLOR;\n\n ctx.fillRect(\n col * (CELL_SIZE + 1) + 1,\n row * (CELL_SIZE + 1) + 1,\n CELL_SIZE,\n CELL_SIZE\n );\n }\n }\n\n ctx.stroke();\n};\n\nnewRandomGen();\n\n// Connect UI element\ndocument.getElementById('cellular-automata-option').addEventListener('click', newCellularAutomata);\ndocument.getElementById('simple-rooms-option').addEventListener('click', newSimpleRooms);\ndocument.getElementById('bsp-interior-option').addEventListener('click', newBspInterior);\ndocument.getElementById('random-option').addEventListener('click', newRandomGen);\n\n\n//# sourceURL=webpack:///./index.js?"); + +/***/ }), + +/***/ "./node_modules/webpack/buildin/global.js": +/*!***********************************!*\ + !*** (webpack)/buildin/global.js ***! + \***********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("var g;\n\n// This works in non-strict mode\ng = (function() {\n\treturn this;\n})();\n\ntry {\n\t// This works if eval is allowed (see CSP)\n\tg = g || new Function(\"return this\")();\n} catch (e) {\n\t// This works if the window reference is available\n\tif (typeof window === \"object\") g = window;\n}\n\n// g can still be undefined, but nothing to do about it...\n// We return undefined, instead of nothing here, so it's\n// easier to handle this case. if(!global) { ...}\n\nmodule.exports = g;\n\n\n//# sourceURL=webpack:///(webpack)/buildin/global.js?"); /***/ }), diff --git a/docs/bootstrap.js b/docs/bootstrap.js index cbda51c..6cf36c6 100644 --- a/docs/bootstrap.js +++ b/docs/bootstrap.js @@ -55,6 +55,69 @@ /******/ "../pkg/mapgen_demo_bg.wasm": function() { /******/ return { /******/ "./mapgen_demo_bg.js": { +/******/ "__wbindgen_object_drop_ref": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbindgen_object_drop_ref"](p0i32); +/******/ }, +/******/ "__wbg_getRandomValues_3ac1b33c90b52596": function(p0i32,p1i32,p2i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_getRandomValues_3ac1b33c90b52596"](p0i32,p1i32,p2i32); +/******/ }, +/******/ "__wbg_randomFillSync_6f956029658662ec": function(p0i32,p1i32,p2i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_randomFillSync_6f956029658662ec"](p0i32,p1i32,p2i32); +/******/ }, +/******/ "__wbg_self_1c83eb4471d9eb9b": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_self_1c83eb4471d9eb9b"](); +/******/ }, +/******/ "__wbg_static_accessor_MODULE_abf5ae284bffdf45": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_static_accessor_MODULE_abf5ae284bffdf45"](); +/******/ }, +/******/ "__wbg_require_5b2b5b594d809d9f": function(p0i32,p1i32,p2i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_require_5b2b5b594d809d9f"](p0i32,p1i32,p2i32); +/******/ }, +/******/ "__wbg_crypto_c12f14e810edcaa2": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_crypto_c12f14e810edcaa2"](p0i32); +/******/ }, +/******/ "__wbg_msCrypto_679be765111ba775": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_msCrypto_679be765111ba775"](p0i32); +/******/ }, +/******/ "__wbindgen_is_undefined": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbindgen_is_undefined"](p0i32); +/******/ }, +/******/ "__wbg_getRandomValues_05a60bf171bfc2be": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_getRandomValues_05a60bf171bfc2be"](p0i32); +/******/ }, +/******/ "__wbg_instanceof_Window_adf3196bdc02b386": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_instanceof_Window_adf3196bdc02b386"](p0i32); +/******/ }, +/******/ "__wbg_document_6cc8d0b87c0a99b9": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_document_6cc8d0b87c0a99b9"](p0i32); +/******/ }, +/******/ "__wbg_getElementById_0cb6ad9511b1efc0": function(p0i32,p1i32,p2i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_getElementById_0cb6ad9511b1efc0"](p0i32,p1i32,p2i32); +/******/ }, +/******/ "__wbg_setinnerHTML_4ff235db1a3cb4d8": function(p0i32,p1i32,p2i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_setinnerHTML_4ff235db1a3cb4d8"](p0i32,p1i32,p2i32); +/******/ }, +/******/ "__wbg_call_8e95613cc6524977": function(p0i32,p1i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_call_8e95613cc6524977"](p0i32,p1i32); +/******/ }, +/******/ "__wbindgen_object_clone_ref": function(p0i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbindgen_object_clone_ref"](p0i32); +/******/ }, +/******/ "__wbg_newnoargs_f3b8a801d5d4b079": function(p0i32,p1i32) { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_newnoargs_f3b8a801d5d4b079"](p0i32,p1i32); +/******/ }, +/******/ "__wbg_self_07b2f89e82ceb76d": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_self_07b2f89e82ceb76d"](); +/******/ }, +/******/ "__wbg_window_ba85d88572adc0dc": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_window_ba85d88572adc0dc"](); +/******/ }, +/******/ "__wbg_globalThis_b9277fc37e201fe5": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_globalThis_b9277fc37e201fe5"](); +/******/ }, +/******/ "__wbg_global_e16303fe83e1d57f": function() { +/******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbg_global_e16303fe83e1d57f"](); +/******/ }, /******/ "__wbindgen_throw": function(p0i32,p1i32) { /******/ return installedModules["../pkg/mapgen_demo_bg.js"].exports["__wbindgen_throw"](p0i32,p1i32); /******/ } @@ -159,7 +222,7 @@ /******/ promises.push(installedWasmModuleData); /******/ else { /******/ var importObject = wasmImportObjects[wasmModuleId](); -/******/ var req = fetch(__webpack_require__.p + "" + {"../pkg/mapgen_demo_bg.wasm":"bbf7fe2631d06e11148e"}[wasmModuleId] + ".module.wasm"); +/******/ var req = fetch(__webpack_require__.p + "" + {"../pkg/mapgen_demo_bg.wasm":"cbb67d3121ca19dbc9af"}[wasmModuleId] + ".module.wasm"); /******/ var promise; /******/ if(importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') { /******/ promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) { diff --git a/docs/cbb67d3121ca19dbc9af.module.wasm b/docs/cbb67d3121ca19dbc9af.module.wasm new file mode 100644 index 0000000..a0d00bd Binary files /dev/null and b/docs/cbb67d3121ca19dbc9af.module.wasm differ diff --git a/docs/index.html b/docs/index.html index 07d6fec..2426dee 100644 --- a/docs/index.html +++ b/docs/index.html @@ -35,8 +35,10 @@ Select generator diff --git a/src/common/geometry.rs b/src/common/geometry.rs index 57d8715..86295b2 100644 --- a/src/common/geometry.rs +++ b/src/common/geometry.rs @@ -30,24 +30,44 @@ impl Point { /// Rectangle region on the map #[derive(PartialEq, Copy, Clone)] pub struct Rect { - pub x1 : i32, - pub x2 : i32, - pub y1 : i32, - pub y2 : i32 + pub x1 : usize, + pub x2 : usize, + pub y1 : usize, + pub y2 : usize } impl Rect { - pub fn new(x:i32, y: i32, width:i32, height:i32) -> Rect { + pub fn new(x: usize, y: usize, width: usize, height: usize) -> Rect { Rect{x1:x, y1:y, x2:x+width, y2:y+height} } + pub fn new_i32(x:i32, y: i32, width:i32, height:i32) -> Rect { + Rect::new(x as usize, y as usize, width as usize, height as usize) + } + /// Returns true if this overlaps with other pub fn intersect(&self, other:&Rect) -> bool { self.x1 <= other.x2 && self.x2 >= other.x1 && self.y1 <= other.y2 && self.y2 >= other.y1 } pub fn center(&self) -> Point { - Point::new_i32((self.x1 + self.x2)/2, (self.y1 + self.y2)/2) + Point::new((self.x1 + self.x2)/2, (self.y1 + self.y2)/2) + } + + pub fn width(&self) -> usize { + if self.x2 >= self.x1 { + self.x2 - self.x1 + } else { + self.x1 - self.x2 + } + } + + pub fn height(&self) -> usize { + if self.y2 >= self.y1 { + self.y2 - self.y1 + } else { + self.y1 - self.y2 + } } } @@ -72,4 +92,12 @@ mod tests { let rect2 = Rect::new(30, 30, 60, 60); assert!(rect1.intersect(&rect2)); } + + #[test] + fn test_size() { + let rect1 = Rect::new(10, 10, 40, 30); + assert_eq!(rect1.width(), 40); + assert_eq!(rect1.height(), 30); + } + } \ No newline at end of file diff --git a/src/common/random.rs b/src/common/random.rs index 047d01f..9ebd257 100644 --- a/src/common/random.rs +++ b/src/common/random.rs @@ -2,14 +2,25 @@ //! use rand::prelude::*; +pub trait Rng { + /// Generate random number between start and end (bot inclusive). + fn roll_dice(&mut self, min: usize, max: usize) -> usize; + /// Generate random number between start (inclusive) and end (exclusive). + fn random_range(&mut self, start: usize, end: usize) -> usize; +} -/// Generate random number between start (inclusive) and end (exclusive). -pub fn random_range(rng: &mut StdRng, start: usize, end: usize) -> usize { - let max = (end - start) as u32; - if max == 0 { - start - } else { - ((rng.next_u32() % max) + start as u32) as usize +impl Rng for StdRng { + fn roll_dice(&mut self, min: usize, max: usize) -> usize { + self.random_range(min, max+1) + } + + fn random_range(&mut self, start: usize, end: usize) -> usize { + let max = (end - start) as u32; + if max == 0 { + start + } else { + ((self.next_u32() % max) + start as u32) as usize + } } } @@ -18,16 +29,40 @@ pub fn random_range(rng: &mut StdRng, start: usize, end: usize) -> usize { /// ------------------------------------------------------------------------------------------------ #[cfg(test)] mod tests { - use std::time::{SystemTime, UNIX_EPOCH}; use rand::prelude::*; - use super::*; + use super::Rng; #[test] fn test_range() { - let system_time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Can't access system time"); - let mut rng = StdRng::seed_from_u64(system_time.as_millis() as u64); - let x = random_range(&mut rng, 5, 8); + let mut rng = StdRng::seed_from_u64(100); + let x = rng.random_range(5, 8); assert!(x >= 5 && x < 8); } + #[test] + fn test_range_average() { + let num_op = 1000; + let mut rng = StdRng::seed_from_u64(1000); + let xs: Vec = (0..num_op).map(|_| rng.random_range(5, 10)).collect(); + let mean = xs.iter().sum::() / num_op; + let min = *xs.iter().min().expect("no min"); + let max = *xs.iter().max().expect("no max"); + assert_eq!(mean, 7); + assert_eq!(min, 5); + assert_eq!(max, 9); + } + + #[test] + fn test_roll_dice() { + let num_op = 1000; + let mut rng = StdRng::seed_from_u64(2000); + let xs: Vec = (0..num_op).map(|_| rng.roll_dice(1, 7)).collect(); + let mean = xs.iter().sum::() as f32 / num_op as f32 + 0.5; + let min = *xs.iter().min().expect("no min"); + let max = *xs.iter().max().expect("no max"); + assert_eq!(mean as usize, 4); + assert_eq!(min, 1); + assert_eq!(max, 7); + } + } \ No newline at end of file diff --git a/src/dungeon/bsp_interior.rs b/src/dungeon/bsp_interior.rs new file mode 100644 index 0000000..e4b2574 --- /dev/null +++ b/src/dungeon/bsp_interior.rs @@ -0,0 +1,108 @@ +//! Random rooms map generator. +//! +//! Try to generate rooms of different size to fill the map area. +//! Rooms will not overlap. +//! +//! Example generator usage: +//! ``` +//! use rand::prelude::*; +//! use mapgen::dungeon::{ +//! MapGenerator, +//! bsp_interior::BspInteriorGen +//! }; +//! +//! let mut rng = StdRng::seed_from_u64(100); +//! let gen = BspInteriorGen::new(); +//! let map = gen.generate_map(80, 50, &mut rng); +//! +//! assert_eq!(map.width, 80); +//! assert_eq!(map.height, 50); +//! ``` +//! + +use rand::prelude::*; +use super::MapGenerator; +use crate::common::geometry::{Point, Rect}; +use crate::common::random::Rng; +use super::map::Map; + + +pub struct BspInteriorGen { + min_room_size: usize, +} + +impl MapGenerator for BspInteriorGen { + fn generate_map(&self, width: usize, height: usize, rng : &mut StdRng) -> Map { + self.build(rng, width, height) + } +} + +impl BspInteriorGen { + + pub fn new() -> Box { + Box::new(BspInteriorGen{ + min_room_size: 8, + }) + } + + fn build(&self, rng: &mut StdRng, width: usize, height: usize) -> Map { + let mut map = Map::new(width, height); + let mut rects: Vec = Vec::new(); + rects.push( Rect::new(1, 1, map.width-2, map.height-2) ); + let first_room = rects[0]; + // Divide the first room + self.add_subrects(first_room, rng, &mut rects); + + let rooms_copy = rects.clone(); + for r in rooms_copy.iter() { + let room = *r; + map.add_room(room); + } + + // Now we want corridors + for i in 0..map.rooms.len()-1 { + let room = map.rooms[i]; + let next_room = map.rooms[i+1]; + let start_x = room.x1 + rng.random_range(1, room.width()); + let start_y = room.y1 + rng.random_range(1, room.height()); + let end_x = next_room.x1 + (rng.random_range(1, next_room.width())); + let end_y = next_room.y1 + (rng.random_range(1, next_room.width())); + map.add_corridor(Point::new(start_x, start_y), Point::new(end_x, end_y)); + } + + map + } + + fn add_subrects(&self, rect: Rect, rng: &mut StdRng, rects: &mut Vec) { + // Remove the last rect from the list + if !rects.is_empty() { + rects.remove(rects.len() - 1); + } + + // Calculate boundaries + let width = rect.x2 - rect.x1; + let height = rect.y2 - rect.y1; + let half_width = width / 2; + let half_height = height / 2; + + let split = rng.roll_dice(1, 4); + + if split <= 2 { + // Horizontal split + let h1 = Rect::new( rect.x1, rect.y1, half_width-1, height ); + rects.push( h1 ); + if half_width > self.min_room_size { self.add_subrects(h1, rng, rects); } + let h2 = Rect::new( rect.x1 + half_width, rect.y1, half_width, height ); + rects.push( h2 ); + if half_width > self.min_room_size { self.add_subrects(h2, rng, rects); } + } else { + // Vertical split + let v1 = Rect::new( rect.x1, rect.y1, width, half_height-1 ); + rects.push(v1); + if half_height > self.min_room_size { self.add_subrects(v1, rng, rects); } + let v2 = Rect::new( rect.x1, rect.y1 + half_height, width, half_height ); + rects.push(v2); + if half_height > self.min_room_size { self.add_subrects(v2, rng, rects); } + } + } +} \ No newline at end of file diff --git a/src/dungeon/bsp_rooms.rs b/src/dungeon/bsp_rooms.rs index 042ced9..e9b6618 100644 --- a/src/dungeon/bsp_rooms.rs +++ b/src/dungeon/bsp_rooms.rs @@ -23,7 +23,7 @@ use rand::prelude::*; use super::MapGenerator; use crate::common::geometry::Rect; -use crate::common::random; +use crate::common::random::Rng; use super::map::{Map, TileType}; @@ -48,7 +48,7 @@ impl BspRoomsGen { let mut map = Map::new(width, height); let mut rects: Vec = Vec::new(); // Start with a single map-sized rectangle - rects.push( Rect::new(2, 2, (width-5) as i32, (height-5) as i32) ); + rects.push( Rect::new(2, 2, width-5, height-5) ); let first_room = rects[0]; rects.append(&mut self.split_into_subrects(first_room)); // Divide the first room @@ -71,10 +71,10 @@ impl BspRoomsGen { fn split_into_subrects(&self, rect: Rect) -> Vec { let mut rects: Vec = Vec::new(); - let width = i32::abs(rect.x1 - rect.x2); - let height = i32::abs(rect.y1 - rect.y2); - let half_width = i32::max(width / 2, 1); - let half_height = i32::max(height / 2, 1); + let width = rect.width(); + let height = rect.height(); + let half_width = usize::max(width / 2, 1); + let half_height = usize::max(height / 2, 1); rects.push(Rect::new( rect.x1, rect.y1, half_width, half_height )); rects.push(Rect::new( rect.x1, rect.y1 + half_height, half_width, half_height )); @@ -86,22 +86,22 @@ impl BspRoomsGen { fn get_random_rect(&self, rng : &mut StdRng, rects: &Vec) -> Rect { if rects.len() == 1 { return rects[0]; } - let idx = random::random_range(rng, 0, rects.len()); + let idx = rng.random_range(0, rects.len()); rects[idx] } fn get_random_sub_rect(&self, rect: Rect, rng: &mut StdRng) -> Rect { let mut result = rect; - let rect_width = i32::abs(rect.x1 - rect.x2); - let rect_height = i32::abs(rect.y1 - rect.y2); + let rect_width = rect.width(); + let rect_height = rect.height(); - let w = usize::max(3, random::random_range(rng, 1, usize::min(rect_width as usize, 20))) + 1; - let h = usize::max(3, random::random_range(rng, 1, usize::min(rect_height as usize, 20))) + 1; + let w = usize::max(3, rng.random_range(1, usize::min(rect_width as usize, 20))) + 1; + let h = usize::max(3, rng.random_range(1, usize::min(rect_height as usize, 20))) + 1; - result.x1 += random::random_range(rng, 0, 6) as i32; - result.y1 += random::random_range(rng, 0, 6) as i32; - result.x2 = result.x1 + w as i32; - result.y2 = result.y1 + h as i32; + result.x1 += rng.random_range(0, 6); + result.y1 += rng.random_range(0, 6); + result.x2 = result.x1 + w; + result.y2 = result.y1 + h; result } @@ -121,8 +121,8 @@ impl BspRoomsGen { for y in expanded.y1 ..= expanded.y2 { for x in expanded.x1 ..= expanded.x2 { - if x > map.width as i32 -2 { can_build = false; } - if y > map.height as i32 -2 { can_build = false; } + if x > map.width - 2 { can_build = false; } + if y > map.height - 2 { can_build = false; } if x < 1 { can_build = false; } if y < 1 { can_build = false; } if can_build { diff --git a/src/dungeon/mod.rs b/src/dungeon/mod.rs index 1ccd797..d1ef8de 100644 --- a/src/dungeon/mod.rs +++ b/src/dungeon/mod.rs @@ -29,6 +29,7 @@ pub mod map; pub mod cellular_automata; pub mod cull_unreachable; +pub mod bsp_interior; pub mod bsp_rooms; pub mod distant_exit; pub mod simple_rooms; diff --git a/src/dungeon/simple_rooms.rs b/src/dungeon/simple_rooms.rs index 7792fee..84d245a 100644 --- a/src/dungeon/simple_rooms.rs +++ b/src/dungeon/simple_rooms.rs @@ -23,7 +23,7 @@ use rand::prelude::*; use super::MapGenerator; use crate::common::geometry::Rect; -use crate::common::random; +use crate::common::random::Rng; use super::map::{Map}; @@ -54,11 +54,11 @@ impl SimpleRoomsGen { // Create room dimensions for _ in 0..self.max_rooms { - let w = random::random_range(rng, self.min_room_size, self.max_room_size); - let h = random::random_range(rng, self.min_room_size, self.max_room_size); - let x = random::random_range(rng, 1, width - w); - let y = random::random_range(rng, 1, height - h); - let new_room = Rect::new(x as i32, y as i32, w as i32, h as i32); + let w = rng.random_range(self.min_room_size, self.max_room_size); + let h = rng.random_range(self.min_room_size, self.max_room_size); + let x = rng.random_range(1, width - w); + let y = rng.random_range(1, height - h); + let new_room = Rect::new(x, y, w, h); let intersects = map.rooms.iter().any(|r| new_room.intersect(r)); if !intersects { map.add_room(new_room);