Added maze Fixed #28
This commit is contained in:
parent
322eca52cf
commit
569d751031
|
@ -21,7 +21,7 @@ This library consists of different map filters which can be combined to create c
|
|||
* [x] Cull unreachable areas
|
||||
* [ ] Diffusion-Limited Aggregation (DLA)
|
||||
* [x] Drunkard's walk
|
||||
* [ ] Maze
|
||||
* [x] Maze
|
||||
* [x] Noise generator
|
||||
* [ ] Prefabs
|
||||
* [x] Room corridors nearest
|
||||
|
|
|
@ -99,19 +99,32 @@ impl World {
|
|||
World::new(width, height, map)
|
||||
}
|
||||
|
||||
pub fn new_maze(width: u32, height: u32, seed: u32) -> World {
|
||||
World::print_map_info(format!("Maze with the seed: {}", seed));
|
||||
let mut rng = StdRng::seed_from_u64(seed as u64);
|
||||
let map = MapBuilder::new(width as usize, height as usize)
|
||||
.with(MazeBuilder::new())
|
||||
.with(AreaStartingPosition::new(XStart::LEFT, YStart::TOP))
|
||||
.with(DistantExit::new())
|
||||
.build_with_rng(&mut rng);
|
||||
World::new(width, height, map)
|
||||
}
|
||||
|
||||
pub fn new_random(width: u32, height: u32, seed: u32) -> World {
|
||||
let mut rng = rand::thread_rng();
|
||||
let px = rng.gen::<f32>();
|
||||
if px < 0.2 {
|
||||
if px < 1.0/6.0 {
|
||||
World::new_cellular_automata(width, height, seed)
|
||||
} else if px < 0.4 {
|
||||
} else if px < 2.0/6.0 {
|
||||
World::new_simple_rooms(width, height, seed)
|
||||
} else if px < 0.6 {
|
||||
} else if px < 3.0/6.0 {
|
||||
World::new_drunkard(width, height, seed)
|
||||
} else if px < 0.8 {
|
||||
} else if px < 4.0/6.0 {
|
||||
World::new_bsp_rooms(width, height, seed)
|
||||
} else if px < 5.0/6.0 {
|
||||
World::new_bsp_rooms(width, height, seed)
|
||||
} else {
|
||||
World::new_bsp_interior(width, height, seed)
|
||||
World::new_maze(width, height, seed)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@
|
|||
<a class="dropdown-item" id="bsp-rooms-option">BSP Rooms</a>
|
||||
<a class="dropdown-item" id="bsp-interior-option">BSP Interior</a>
|
||||
<a class="dropdown-item" id="drunkard-option">Drunkard Walk</a>
|
||||
<a class="dropdown-item" id="maze-option">Maze</a>
|
||||
<a class="dropdown-item" id="random-option">Random Generator</a>
|
||||
</div>
|
||||
</li>
|
||||
|
|
|
@ -60,6 +60,12 @@ function newBspRooms() {
|
|||
requestAnimationFrame(renderLoop);
|
||||
}
|
||||
|
||||
function newMaze() {
|
||||
var seed = Date.now();
|
||||
world = World.new_maze(GRID_COLS, GRID_ROWS, get_seed());
|
||||
requestAnimationFrame(renderLoop);
|
||||
}
|
||||
|
||||
function newRandomGen() {
|
||||
var seed = Date.now();
|
||||
world = World.new_random(GRID_COLS, GRID_ROWS, get_seed());
|
||||
|
@ -156,4 +162,5 @@ document.getElementById('simple-rooms-option').addEventListener('click', newSimp
|
|||
document.getElementById('bsp-rooms-option').addEventListener('click', newBspRooms);
|
||||
document.getElementById('drunkard-option').addEventListener('click', newDrunkard);
|
||||
document.getElementById('bsp-interior-option').addEventListener('click', newBspInterior);
|
||||
document.getElementById('maze-option').addEventListener('click', newMaze);
|
||||
document.getElementById('random-option').addEventListener('click', newRandomGen);
|
||||
|
|
File diff suppressed because one or more lines are too long
2
docs/bootstrap.js
vendored
2
docs/bootstrap.js
vendored
|
@ -222,7 +222,7 @@
|
|||
/******/ promises.push(installedWasmModuleData);
|
||||
/******/ else {
|
||||
/******/ var importObject = wasmImportObjects[wasmModuleId]();
|
||||
/******/ var req = fetch(__webpack_require__.p + "" + {"../pkg/mapgen_demo_bg.wasm":"3bda724f23e860df0b9c"}[wasmModuleId] + ".module.wasm");
|
||||
/******/ var req = fetch(__webpack_require__.p + "" + {"../pkg/mapgen_demo_bg.wasm":"fa6c9d72e1d9a518ed31"}[wasmModuleId] + ".module.wasm");
|
||||
/******/ var promise;
|
||||
/******/ if(importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') {
|
||||
/******/ promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {
|
||||
|
|
BIN
docs/fa6c9d72e1d9a518ed31.module.wasm
Normal file
BIN
docs/fa6c9d72e1d9a518ed31.module.wasm
Normal file
Binary file not shown.
|
@ -41,6 +41,7 @@
|
|||
<a class="dropdown-item" id="bsp-rooms-option">BSP Rooms</a>
|
||||
<a class="dropdown-item" id="bsp-interior-option">BSP Interior</a>
|
||||
<a class="dropdown-item" id="drunkard-option">Drunkard Walk</a>
|
||||
<a class="dropdown-item" id="maze-option">Maze</a>
|
||||
<a class="dropdown-item" id="random-option">Random Generator</a>
|
||||
</div>
|
||||
</li>
|
||||
|
|
217
src/filter/maze.rs
Normal file
217
src/filter/maze.rs
Normal file
|
@ -0,0 +1,217 @@
|
|||
//! Example generator usage:
|
||||
//! ```
|
||||
//! use rand::prelude::*;
|
||||
//! use mapgen::{Map, MapFilter};
|
||||
//! use mapgen::filter::MazeBuilder;
|
||||
//!
|
||||
//! let mut rng = StdRng::seed_from_u64(100);
|
||||
//! let gen = MazeBuilder::new();
|
||||
//! let map = gen.modify_map(&mut rng, &Map::new(80, 50));
|
||||
//!
|
||||
//! assert_eq!(map.width, 80);
|
||||
//! assert_eq!(map.height, 50);
|
||||
//! ```
|
||||
//!
|
||||
|
||||
use rand::prelude::*;
|
||||
use crate::MapFilter;
|
||||
use crate::{
|
||||
map::{Map, TileType},
|
||||
random::Rng
|
||||
};
|
||||
|
||||
|
||||
pub struct MazeBuilder {}
|
||||
|
||||
impl MapFilter for MazeBuilder {
|
||||
fn modify_map(&self, rng: &mut StdRng, map: &Map) -> Map {
|
||||
self.build(rng, map)
|
||||
}
|
||||
}
|
||||
|
||||
impl MazeBuilder {
|
||||
pub fn new() -> Box<MazeBuilder> {
|
||||
Box::new(MazeBuilder{})
|
||||
}
|
||||
|
||||
#[allow(clippy::map_entry)]
|
||||
fn build(&self, rng: &mut StdRng, map: &Map) -> Map {
|
||||
let mut new_map = map.clone();
|
||||
let mut maze = Grid::new((map.width as i32/ 2)-2, (map.height as i32/ 2)-2, rng);
|
||||
maze.generate_maze(&mut new_map);
|
||||
new_map
|
||||
}
|
||||
}
|
||||
|
||||
/* Maze code taken under MIT from https://github.com/cyucelen/mazeGenerator/ */
|
||||
|
||||
const TOP : usize = 0;
|
||||
const RIGHT : usize = 1;
|
||||
const BOTTOM : usize = 2;
|
||||
const LEFT : usize = 3;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
struct Cell {
|
||||
row: i32,
|
||||
column: i32,
|
||||
walls: [bool; 4],
|
||||
visited: bool,
|
||||
}
|
||||
|
||||
impl Cell {
|
||||
fn new(row: i32, column: i32) -> Cell {
|
||||
Cell{
|
||||
row,
|
||||
column,
|
||||
walls: [true, true, true, true],
|
||||
visited: false
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_walls(&mut self, next : &mut Cell) {
|
||||
let x = self.column - next.column;
|
||||
let y = self.row - next.row;
|
||||
|
||||
if x == 1 {
|
||||
self.walls[LEFT] = false;
|
||||
next.walls[RIGHT] = false;
|
||||
}
|
||||
else if x == -1 {
|
||||
self.walls[RIGHT] = false;
|
||||
next.walls[LEFT] = false;
|
||||
}
|
||||
else if y == 1 {
|
||||
self.walls[TOP] = false;
|
||||
next.walls[BOTTOM] = false;
|
||||
}
|
||||
else if y == -1 {
|
||||
self.walls[BOTTOM] = false;
|
||||
next.walls[TOP] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Grid<'a> {
|
||||
width: i32,
|
||||
height: i32,
|
||||
cells: Vec<Cell>,
|
||||
backtrace: Vec<usize>,
|
||||
current: usize,
|
||||
rng : &'a mut StdRng
|
||||
}
|
||||
|
||||
impl<'a> Grid<'a> {
|
||||
fn new(width: i32, height:i32, rng: &mut StdRng) -> Grid {
|
||||
let mut grid = Grid{
|
||||
width,
|
||||
height,
|
||||
cells: Vec::new(),
|
||||
backtrace: Vec::new(),
|
||||
current: 0,
|
||||
rng
|
||||
};
|
||||
|
||||
for row in 0..height {
|
||||
for column in 0..width {
|
||||
grid.cells.push(Cell::new(row, column));
|
||||
}
|
||||
}
|
||||
|
||||
grid
|
||||
}
|
||||
|
||||
fn calculate_index(&self, row: i32, column: i32) -> i32 {
|
||||
if row < 0 || column < 0 || column > self.width-1 || row > self.height-1 {
|
||||
-1
|
||||
} else {
|
||||
column + (row * self.width)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_available_neighbors(&self) -> Vec<usize> {
|
||||
let mut neighbors : Vec<usize> = Vec::new();
|
||||
|
||||
let current_row = self.cells[self.current].row;
|
||||
let current_column = self.cells[self.current].column;
|
||||
|
||||
let neighbor_indices : [i32; 4] = [
|
||||
self.calculate_index(current_row -1, current_column),
|
||||
self.calculate_index(current_row, current_column + 1),
|
||||
self.calculate_index(current_row + 1, current_column),
|
||||
self.calculate_index(current_row, current_column - 1)
|
||||
];
|
||||
|
||||
for i in neighbor_indices.iter() {
|
||||
if *i != -1 && !self.cells[*i as usize].visited {
|
||||
neighbors.push(*i as usize);
|
||||
}
|
||||
}
|
||||
|
||||
neighbors
|
||||
}
|
||||
|
||||
fn find_next_cell(&mut self) -> Option<usize> {
|
||||
let neighbors = self.get_available_neighbors();
|
||||
if !neighbors.is_empty() {
|
||||
if neighbors.len() == 1 {
|
||||
return Some(neighbors[0]);
|
||||
} else {
|
||||
return Some(neighbors[(self.rng.roll_dice(1, neighbors.len())-1) as usize]);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn generate_maze(&mut self, map: &mut Map) {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
self.cells[self.current].visited = true;
|
||||
let next = self.find_next_cell();
|
||||
|
||||
match next {
|
||||
Some(next) => {
|
||||
self.cells[next].visited = true;
|
||||
self.backtrace.push(self.current);
|
||||
// __lower_part__ __higher_part_
|
||||
// / \ / \
|
||||
// --------cell1------ | cell2-----------
|
||||
let (lower_part, higher_part) =
|
||||
self.cells.split_at_mut(std::cmp::max(self.current, next));
|
||||
let cell1 = &mut lower_part[std::cmp::min(self.current, next)];
|
||||
let cell2 = &mut higher_part[0];
|
||||
cell1.remove_walls(cell2);
|
||||
self.current = next;
|
||||
}
|
||||
None => {
|
||||
if !self.backtrace.is_empty() {
|
||||
self.current = self.backtrace[0];
|
||||
self.backtrace.remove(0);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if i % 50 == 0 {
|
||||
self.copy_to_map(map);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_to_map(&self, map: &mut Map) {
|
||||
// Clear the map
|
||||
for i in map.tiles.iter_mut() { *i = TileType::Wall; }
|
||||
|
||||
for cell in self.cells.iter() {
|
||||
let x = (cell.column as usize + 1) * 2;
|
||||
let y = (cell.row as usize + 1) * 2;
|
||||
|
||||
map.set_tile(x, y, TileType::Floor);
|
||||
if !cell.walls[TOP] { map.set_tile(x, y-1, TileType::Floor) }
|
||||
if !cell.walls[RIGHT] { map.set_tile(x+1, y, TileType::Floor) }
|
||||
if !cell.walls[BOTTOM] { map.set_tile(x, y+1, TileType::Floor) }
|
||||
if !cell.walls[LEFT] { map.set_tile(x-1, y, TileType::Floor) }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ pub mod cellular_automata;
|
|||
pub mod cull_unreachable;
|
||||
pub mod distant_exit;
|
||||
pub mod drunkard;
|
||||
pub mod maze;
|
||||
pub mod noise_generator;
|
||||
pub mod simple_rooms;
|
||||
pub mod rooms_corridors_nearest;
|
||||
|
@ -18,6 +19,7 @@ pub use cellular_automata::CellularAutomata;
|
|||
pub use cull_unreachable::CullUnreachable;
|
||||
pub use distant_exit::DistantExit;
|
||||
pub use drunkard::DrunkardsWalk;
|
||||
pub use maze::MazeBuilder;
|
||||
pub use noise_generator::NoiseGenerator;
|
||||
pub use simple_rooms::SimpleRooms;
|
||||
pub use rooms_corridors_nearest::NearestCorridors;
|
||||
|
|
Loading…
Reference in New Issue
Block a user