make connections more variable

This commit is contained in:
Joachim Lusiardi 2024-11-01 12:02:11 +01:00
parent d42d8a12b4
commit 789a41cb3f
2 changed files with 36 additions and 45 deletions

View File

@ -310,11 +310,11 @@ impl LevelGenerator {
for row in 0..ROOMS_VERTICAL { for row in 0..ROOMS_VERTICAL {
if let Some(connection) = self.rooms[col][row].connection_down { if let Some(connection) = self.rooms[col][row].connection_down {
// println!("down"); // println!("down");
connection.render(&mut structure); connection.render(&mut self.rng, &mut structure);
} }
if let Some(connection) = self.rooms[col][row].connection_right { if let Some(connection) = self.rooms[col][row].connection_right {
// println!("right"); // println!("right");
connection.render(&mut structure); connection.render(&mut self.rng,&mut structure);
} }
} }
} }
@ -331,20 +331,22 @@ impl LevelGenerator {
} }
} }
// #[test] #[test]
// fn test_level_gen() { fn test_level_gen() {
// for _ in 0..1000 { let mut rng = rand::thread_rng();
// LevelGenerator::generate(0, true, false).render(); for _ in 0..1000 {
// } LevelGenerator::generate(0, true, false).render();
// } }
}
// #[test] #[test]
// fn test_level_gen_respects_level() { fn test_level_gen_respects_level() {
// let level = LevelGenerator::generate(0, true, false).render(); let mut rng = rand::thread_rng();
// assert_eq!(0, level.level); let level = LevelGenerator::generate(0, true, false).render();
// let level = LevelGenerator::generate(1, true, false).render(); assert_eq!(0, level.level);
// assert_eq!(1, level.level); let level = LevelGenerator::generate(1, true, false).render();
// } assert_eq!(1, level.level);
}
#[cfg(test)] #[cfg(test)]
fn find_room_types(rooms: &Vec<Room>) -> (bool, bool, bool, bool) { fn find_room_types(rooms: &Vec<Room>) -> (bool, bool, bool, bool) {

View File

@ -1,4 +1,4 @@
use std::cmp::{max, min}; use std::{cmp::{max, min}, ops::RangeInclusive};
use crate::{ use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH}, constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
@ -26,7 +26,14 @@ pub struct Connection {
} }
impl Connection { impl Connection {
pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) { pub fn render(&self, rng: &mut ThreadRng, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
fn calc_range(a: usize, b:usize) -> RangeInclusive<usize>{
if a > b {
b..=a
} else {
a..=b
}
}
// the tuples are (col, row) // the tuples are (col, row)
let abs_d_col = let abs_d_col =
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0); max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
@ -35,22 +42,11 @@ impl Connection {
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1); max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if abs_d_col == 0 { if abs_d_col == 0 {
let range = if self.end_pos.1 > self.start_pos.1 { for row in calc_range(self.end_pos.1, self.start_pos.1 ) {
self.start_pos.1..=self.end_pos.1
} else {
self.end_pos.1..=self.start_pos.1
};
for row in range {
tgt[self.end_pos.0][row] = StructureElement::Floor; tgt[self.end_pos.0][row] = StructureElement::Floor;
} }
} else if abs_d_row == 0 { } else if abs_d_row == 0 {
let range = if self.end_pos.0 > self.start_pos.0 { for col in calc_range(self.end_pos.0, self.start_pos.0) {
self.start_pos.0..=self.end_pos.0
} else {
self.end_pos.0..=self.start_pos.0
};
for col in range {
tgt[col][self.end_pos.1] = StructureElement::Floor; tgt[col][self.end_pos.1] = StructureElement::Floor;
} }
} else { } else {
@ -58,18 +54,16 @@ impl Connection {
if d_row > 0 { if d_row > 0 {
// more up/down // more up/down
let d_row = self.end_pos.1 - self.start_pos.1; let d_row = self.end_pos.1 - self.start_pos.1;
for r in self.start_pos.1..=(self.start_pos.1 + d_row / 2) { let vert_offset = if d_row > 3 {
rng.gen_range(1..=d_row-1)
} else {d_row/2};
for r in self.start_pos.1..=(self.start_pos.1 + vert_offset) {
tgt[self.start_pos.0][r] = StructureElement::Floor; tgt[self.start_pos.0][r] = StructureElement::Floor;
} }
let range = if self.end_pos.0 > self.start_pos.0 { for c in calc_range(self.end_pos.0, self.start_pos.0) {
self.start_pos.0..=self.end_pos.0 tgt[c][self.start_pos.1 + vert_offset] = StructureElement::Floor;
} else {
self.end_pos.0..=self.start_pos.0
};
for c in range {
tgt[c][self.start_pos.1 + d_row / 2] = StructureElement::Floor;
} }
for r in (self.start_pos.1 + d_row / 2)..=self.end_pos.1 { for r in (self.start_pos.1 + vert_offset)..=self.end_pos.1 {
tgt[self.end_pos.0][r] = StructureElement::Floor; tgt[self.end_pos.0][r] = StructureElement::Floor;
} }
} else { } else {
@ -78,12 +72,7 @@ impl Connection {
for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) { for tgt_col in tgt.iter_mut().skip(self.start_pos.0).take(d_col / 2 + 1) {
tgt_col[self.start_pos.1] = StructureElement::Floor; tgt_col[self.start_pos.1] = StructureElement::Floor;
} }
let range = if self.end_pos.1 > self.start_pos.1 { for r in calc_range(self.end_pos.1, self.start_pos.1) {
self.start_pos.1..=self.end_pos.1
} else {
self.end_pos.1..=self.start_pos.1
};
for r in range {
tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor; tgt[self.start_pos.0 + d_col / 2][r] = StructureElement::Floor;
} }
for tgt_col in tgt for tgt_col in tgt