summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs81
1 files changed, 35 insertions, 46 deletions
diff --git a/src/main.rs b/src/main.rs
index 7bba3d8..f285976 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,7 +20,6 @@ enum LevelBuilderError {
mod Direction {
- pub const NONE: u8 = 0;
pub const UP: u8 = 1 << 0;
pub const RIGHT: u8 = 1 << 1;
pub const DOWN: u8 = 1 << 2;
@@ -131,24 +130,27 @@ const TILE_IS_PATH: u8 = 0x4;
#[derive(Clone, Debug)]
struct Tile {
- id: u16,
- room: RoomId,
+ owner: Owner,
flags: u8,
connections: u8,
}
#[derive(Clone)]
struct TileTemplate {
- id: u16,
connections: u8
}
+#[derive(Eq, PartialEq, Clone, Debug, Copy)]
+enum Owner {
+ Room(RoomId),
+ Tunnel(u32),
+}
+
impl TileTemplate {
fn into_tile(&self, room: RoomId) -> Tile {
Tile {
connections: self.connections,
- id: self.id,
- room: room,
+ owner: Owner::Room(room),
flags: 0,
}
}
@@ -167,6 +169,7 @@ struct Room {
struct Level {
region: Box<dyn Region<Option<Tile>>>,
rooms: Vec<Room>,
+ tunnels: u32,
}
fn place_template(level: &mut Level, template: &RoomTemplate, x: u32, y: u32) -> Result<(RoomId), (u32)> {
@@ -202,7 +205,7 @@ fn place_template(level: &mut Level, template: &RoomTemplate, x: u32, y: u32) ->
Ok(room_id)
}
-fn tunnel<R: Rng + ?Sized>(level: &mut Level, from: (u32, u32), to: (u32, u32), from_id: RoomId, rng: &mut R) -> Result<(), u32> {
+fn tunnel<R: Rng + ?Sized>(level: &mut Level, from: (u32, u32), to: (u32, u32), owner: Owner, rng: &mut R) -> Result<(), u32> {
let mut from = from;
let diff: (i64, i64) = (from.0 as i64 - to.0 as i64, from.1 as i64 - to.1 as i64);
@@ -246,7 +249,7 @@ fn tunnel<R: Rng + ?Sized>(level: &mut Level, from: (u32, u32), to: (u32, u32),
let tile = level.region.get_mut(from.0, from.1)?;
match tile {
Some(tile) => {
- if tile.room != from_id {
+ if tile.owner != owner {
tile.connections = tile.connections | opposite;
connected = true;
break;
@@ -254,7 +257,7 @@ fn tunnel<R: Rng + ?Sized>(level: &mut Level, from: (u32, u32), to: (u32, u32),
},
None => {
println!("SET -> {} {}", from.0, from.1);
- level.region.set(from.0, from.1, Some(Tile { id: 1, room: from_id, flags: TILE_IS_PATH, connections: opposite}));
+ level.region.set(from.0, from.1, Some(Tile { owner: owner, flags: TILE_IS_PATH, connections: opposite}));
}
}
steps = steps - 1;
@@ -352,7 +355,7 @@ fn connect_rooms<R: Rng + ?Sized>(level: &mut Level, from_id: RoomId, to_id: Roo
}
let pos = candidates[rng.gen_range(0, candidates.len())];
- tunnel(level, pos, to_center, from_id, rng)
+ tunnel(level, pos, to_center, Owner::Room(from_id), rng)
}
fn create_deadend<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u32> {
@@ -378,7 +381,9 @@ fn create_deadend<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(),
let rnd_index = rng.gen_range(0, level.rooms.len());
let room = &mut level.rooms[rnd_index];
let room_center: (u32, u32) = ((room.x + (room.width/2)), (room.y + (room.height/2)));
- return tunnel(level, (x,y), room_center, 9, rng);
+ let res = tunnel(level, (x,y), room_center, Owner::Tunnel(level.tunnels), rng);
+ level.tunnels += 1;
+ return res;
}
}
@@ -416,6 +421,9 @@ fn random_walk<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u3
let mut options = [0; 4];
let mut x = 0;
let mut y = 0;
+
+ let owner = Owner::Tunnel(level.tunnels);
+ level.tunnels += 1;
while neighbours <= 0 && iterations < max_iterations {
neighbours = 0;
@@ -425,27 +433,6 @@ fn random_walk<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u3
if let Some(tile) = level.region.get(x, y)? {
if tile.flags & TILE_IS_PATH != 0 {
empty_neighbours(level, x, y, &mut options, &mut neighbours);
- /*
- if region.get(x, y - 1)?.is_none() {
- options[neighbours] = Direction::UP;
- neighbours += 1;
- }
-
- if region.get(x + 1, y)?.is_none() {
- options[neighbours] = Direction::RIGHT;
- neighbours = neighbours + 1;
- }
-
- if region.get(x, y + 1)?.is_none() {
- options[neighbours] = Direction::DOWN;
- neighbours = neighbours + 1;
- }
-
- if region.get(x - 1, y)?.is_none() {
- options[neighbours] = Direction::LEFT;
- neighbours = neighbours + 1;
- }
- */
}
}
iterations = iterations + 1;
@@ -465,7 +452,7 @@ fn random_walk<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u3
let mut pos = (x, y);
pos = Direction::step_towards(direction, pos).unwrap();
- level.region.set(pos.0, pos.1, Some(Tile { id: 1, room: 8, flags: TILE_IS_PATH, connections: Direction::opposite(direction)}));
+ level.region.set(pos.0, pos.1, Some(Tile { owner: owner, flags: TILE_IS_PATH, connections: Direction::opposite(direction)}));
let mut iterations = rng.gen_range(0, 2);
while iterations >= 0 {
@@ -491,7 +478,7 @@ fn random_walk<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u3
match level.region.get(pos.0, pos.1) {
Ok(tile) => {
if tile.is_none() {
- level.region.set(pos.0, pos.1, Some(Tile { id: 1, room: 8, flags: TILE_IS_PATH, connections: Direction::opposite(direction)}));
+ level.region.set(pos.0, pos.1, Some(Tile { owner: owner, flags: TILE_IS_PATH, connections: Direction::opposite(direction)}));
if let Some(tile) = level.region.get_mut(old_pos.0, old_pos.1)? {
tile.connections |= direction;
}
@@ -516,7 +503,7 @@ fn print_level(level: &Level) {
for x in 0..region.width() {
let tile = region.get(x, y).unwrap();
match tile {
- Some(tile) => print!("{:?}", tile.room),
+ Some(tile) => print!("{:?}", tile.owner),
None => print!("+"),
}
}
@@ -596,28 +583,29 @@ fn main() {
println!("Hello, world!");
let mut thread_rng = thread_rng();
//18
- let mut rng = SmallRng::from_seed([18;16]);
+ let mut rng = SmallRng::from_seed([33;16]);
let mut template:VecRegion<Option<TileTemplate>> = Region::new_with(3, 3);
use Direction::{UP, DOWN, LEFT, RIGHT};
- template.set(0, 0, Some(TileTemplate { id: 1, connections: RIGHT | DOWN}));
- template.set(1, 0, Some(TileTemplate { id: 1, connections: RIGHT | DOWN | LEFT}));
- template.set(2, 0, Some(TileTemplate { id: 1, connections: DOWN | LEFT}));
+ template.set(0, 0, Some(TileTemplate { connections: RIGHT | DOWN}));
+ template.set(1, 0, Some(TileTemplate { connections: RIGHT | DOWN | LEFT}));
+ template.set(2, 0, Some(TileTemplate { connections: DOWN | LEFT}));
- template.set(0, 1, Some(TileTemplate { id: 1, connections: UP | RIGHT | DOWN}));
- template.set(1, 1, Some(TileTemplate { id: 1, connections: UP | RIGHT | DOWN | LEFT}));
- template.set(2, 1, Some(TileTemplate { id: 1, connections: UP | DOWN | LEFT}));
+ template.set(0, 1, Some(TileTemplate { connections: UP | RIGHT | DOWN}));
+ template.set(1, 1, Some(TileTemplate { connections: UP | RIGHT | DOWN | LEFT}));
+ template.set(2, 1, Some(TileTemplate { connections: UP | DOWN | LEFT}));
- template.set(0, 2, Some(TileTemplate { id: 1, connections: UP | RIGHT}));
- template.set(1, 2, Some(TileTemplate { id: 1, connections: UP | RIGHT | LEFT}));
- template.set(2, 2, Some(TileTemplate { id: 1, connections: UP | LEFT}));
+ template.set(0, 2, Some(TileTemplate { connections: UP | RIGHT}));
+ template.set(1, 2, Some(TileTemplate { connections: UP | RIGHT | LEFT}));
+ template.set(2, 2, Some(TileTemplate { connections: UP | LEFT}));
let mut level: VecRegion<Option<Tile>> = Region::new_with(15, 11);
let mut level = Level {
rooms: Vec::new(),
region: Box::new(level),
+ tunnels: 0,
};
let res = place_template(&mut level, &mut template, 0, 0);
@@ -626,8 +614,9 @@ fn main() {
let res = place_template(&mut level, &mut template, 8, 7);
print_level(&level);
let res = connect_rooms(&mut level, 0, 1, &mut rng);
- let res = connect_rooms(&mut level, 1, 2, &mut rng);
+ let res = connect_rooms(&mut level, 2, 1, &mut rng);
let res = connect_rooms(&mut level, 3, 2, &mut rng);
+ //let res = connect_rooms(&mut level, 1, 3, &mut rng);
create_deadend(&mut level, &mut rng);
random_walk(&mut level, &mut rng);
random_walk(&mut level, &mut rng);