summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs152
1 files changed, 79 insertions, 73 deletions
diff --git a/src/main.rs b/src/main.rs
index ad75eaa..b1e65fb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -165,6 +165,82 @@ 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> {
+
+ let mut from = from;
+ let diff: (i64, i64) = (from.0 as i64 - to.0 as i64, from.1 as i64 - to.1 as i64);
+ let abs_diff = (i64::abs(diff.0), i64::abs(diff.1));
+ println!("{:?} {:?}", diff, abs_diff);
+ println!("{:?} {:?}", from, to);
+ let mut direction = if abs_diff.1 < abs_diff.0 {
+ if diff.0 < 0 { Direction::Right } else { Direction::Left }
+ } else {
+ if diff.1 < 0 { Direction::Down } else { Direction::Up }
+ };
+ println!("{:?}", direction);
+
+ /*TODO: Pick an random number of steps*/
+ let mut steps = match direction {
+ Direction::Right | Direction::Left => {
+ rng.gen_range(0, abs_diff.0) + 1
+ },
+ Direction::Up | Direction::Down => {
+ rng.gen_range(0, abs_diff.1) + 1
+ },
+ Direction::None => unreachable!("direction is somehow None")
+ };
+
+ /*Start tunneling*/
+ let mut connected = false;
+ while !connected {
+ while steps > 0 {
+ /*TODO: Locate possible link neighbours*/
+ match direction {
+ Direction::Up => from.1 = from.1 - 1,
+ Direction::Right => from.0 = from.0 + 1,
+ Direction::Down => from.1 = from.1 + 1,
+ Direction::Left => from.0 = from.0 - 1,
+ _ => {},
+ }
+ let tile = level.region.get(from.0, from.1)?;
+ match tile {
+ Some(tile) => {
+ if tile.room != from_id {
+ connected = true;
+ break;
+ }
+ },
+ None => {
+ println!("SET -> {} {}", from.0, from.1);
+ level.region.set(from.0, from.1, Some(Tile { id: 1, room: from_id}));
+ }
+ }
+ steps = steps - 1;
+ }
+
+ let diff: (i64, i64) = (from.0 as i64 - to.0 as i64, from.1 as i64 - to.1 as i64);
+ let abs_diff = (i64::abs(diff.0), i64::abs(diff.1));
+
+ direction = if abs_diff.1 < abs_diff.0 {
+ if -1 < diff.0 { Direction::Left } else { Direction::Right }
+ } else {
+ if -1 < diff.1 { Direction::Up } else { Direction::Down }
+ };
+ println!("{:?}", direction);
+
+ steps = match direction {
+ Direction::Right | Direction::Left => {
+ if abs_diff.0 == 0 { 1 } else { rng.gen_range(0, abs_diff.0) + 1 }
+ },
+ Direction::Up | Direction::Down => {
+ if abs_diff.1 == 0 { 1 } else { rng.gen_range(0, abs_diff.1) + 1 }
+ },
+ _ => 0
+ };
+ }
+ Ok(())
+}
+
fn connect_rooms<R: Rng + ?Sized>(level: &mut Level, from_id: RoomId, to_id: RoomId, rng: &mut R) -> Result<(), u32> {
let mut candidates = Vec::new();
@@ -235,77 +311,7 @@ fn connect_rooms<R: Rng + ?Sized>(level: &mut Level, from_id: RoomId, to_id: Roo
/*TODO: Pick a random point*/
let mut pos = candidates[rng.gen_range(0, candidates.len())];
- let mut direction = if abs_diff.1 < abs_diff.0 {
- if diff.0 < 0 { Direction::Right } else { Direction::Left }
- } else {
- if diff.1 < 0 { Direction::Down } else { Direction::Up }
- };
- println!("{:?}", direction);
-
- /*TODO: Pick an random number of steps*/
- let mut steps = match direction {
- Direction::Right | Direction::Left => {
- rng.gen_range(0, abs_diff.0 - (from.width / 2) as i32) + 1
- },
- Direction::Up | Direction::Down => {
- rng.gen_range(0, abs_diff.1 - (from.height / 2) as i32) + 1
- },
- Direction::None => unreachable!("direction is somehow None")
- };
-
- /*Start tunneling*/
- let mut connected = false;
- while !connected {
- while steps > 0 {
- /*TODO: Locate possible link neighbours*/
- match direction {
- Direction::Up => pos.1 = pos.1 - 1,
- Direction::Right => pos.0 = pos.0 + 1,
- Direction::Down => pos.1 = pos.1 + 1,
- Direction::Left => pos.0 = pos.0 - 1,
- _ => {},
- }
- let tile = level.region.get(pos.0, pos.1)?;
- match tile {
- Some(tile) => {
- if tile.room != from_id {
- connected = true;
- break;
- }
- },
- None => {
- println!("SET -> {} {}", pos.0, pos.1);
- level.region.set(pos.0, pos.1, Some(Tile { id: 1, room: from_id}));
- }
- }
- steps = steps - 1;
- }
-
- let diff: (i32, i32) = (pos.0 as i32 - to_center.0 as i32, pos.1 as i32 - to_center.1 as i32);
- let abs_diff = (i32::abs(diff.0), i32::abs(diff.1));
-
- direction = if abs_diff.1 < abs_diff.0 {
- if -1 < diff.0 { Direction::Left } else { Direction::Right }
- } else {
- if -1 < diff.1 { Direction::Up } else { Direction::Down }
- };
- println!("{:?}", direction);
-
- steps = match direction {
- Direction::Right | Direction::Left => {
- let diff = i32::abs(abs_diff.0 - (from.width / 2) as i32);
- if diff == 0 { 1 } else { rng.gen_range(0, diff) + 1 }
- },
- Direction::Up | Direction::Down => {
- let diff = i32::abs(abs_diff.1 - (from.height / 2) as i32);
- if diff == 0 { 1 } else { rng.gen_range(0, diff) + 1 }
- },
- _ => 0
- };
- }
-
-
- Ok(())
+ tunnel(level, pos, to_center, from_id, rng)
}
fn create_deadend<R: Rng + ?Sized>(level: &mut Level, rng: &mut R) -> Result<(), u32> {
@@ -500,8 +506,8 @@ fn main() {
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, 3, 2, &mut rng);
- create_deadend(&mut level, &mut rng);
- create_deadend(&mut level, &mut rng);
+ //create_deadend(&mut level, &mut rng);
+ //create_deadend(&mut level, &mut rng);
println!("{:?}", res);
print_level(&level);
//println!("{:?}", level);