Compare commits

...

4 Commits

Author SHA1 Message Date
Joachim Lusiardi 8ee142586b make monsters visible 2024-10-31 22:39:11 +01:00
Joachim Lusiardi 1878959e69 Clippy 2024-10-31 22:36:23 +01:00
Joachim Lusiardi 8bae1c7668 formatted 2024-10-31 22:34:35 +01:00
Joachim Lusiardi 3096386ad1 improve paths 2024-10-31 21:52:04 +01:00
6 changed files with 103 additions and 74 deletions

View File

@ -3,7 +3,6 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
#[proc_macro_derive(CreateMonsters)]
pub fn create_monsters(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::File);

View File

@ -2,7 +2,6 @@ use std::{collections::HashMap, ops::RangeInclusive};
use crate::{monster::MonsterTypes, room::RoomType};
/// the number of rooms in vertical direction
pub const ROOMS_HORIZONTAL: usize = 8;
/// the number of rooms in horizontal direction

View File

@ -84,8 +84,8 @@ impl LevelGenerator {
match directions_to_try.pop().unwrap() {
Direction::Horizontal => {
let mut free_cols: Vec<usize> = vec![];
for col in 0..ROOMS_HORIZONTAL {
if rooms[col][room_row].kind == RoomType::EmptyRoom {
for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
if room_col[room_row].kind == RoomType::EmptyRoom {
free_cols.push(col);
}
}
@ -120,9 +120,9 @@ impl LevelGenerator {
// all fields in the row/column was full so we can place it at any empty position
if !placed {
let mut free_pos: Vec<(usize, usize)> = vec![];
for col in 0..ROOMS_HORIZONTAL {
for row in 0..ROOMS_VERTICAL {
if rooms[col][row].kind == RoomType::EmptyRoom {
for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
for (row, room) in room_col.iter().enumerate().take(ROOMS_VERTICAL) {
if room.kind == RoomType::EmptyRoom {
free_pos.push((col, row));
}
}
@ -138,9 +138,9 @@ impl LevelGenerator {
rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
) -> Graph<(usize, usize), u16, petgraph::Undirected> {
let mut graph = UnGraph::<(usize, usize), u16>::default();
for col in 0..ROOMS_HORIZONTAL {
for row in 0..ROOMS_VERTICAL {
if rooms[col][row].kind != RoomType::EmptyRoom {
for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) {
if room.kind != RoomType::EmptyRoom {
graph.add_node((col, row));
}
}
@ -151,8 +151,13 @@ impl LevelGenerator {
continue;
}
if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) {
for col_1 in col + 1..ROOMS_HORIZONTAL {
if rooms[col_1][row].kind != RoomType::EmptyRoom {
for (col_1, tgt_col) in rooms
.iter()
.enumerate()
.take(ROOMS_HORIZONTAL)
.skip(col + 1)
{
if tgt_col[row].kind != RoomType::EmptyRoom {
if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col_1, row))
{
@ -190,6 +195,7 @@ impl LevelGenerator {
let tgt_room = rooms[tgt_node_col][tgt_node_row];
if src_node_col == tgt_node_col {
// Right
let start_col =
src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2;
let start_row =
@ -202,13 +208,14 @@ impl LevelGenerator {
end_pos: (end_col, end_row),
});
} else {
// println!("Right");
let start_col = 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
// Down
let start_col =
1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
let start_row =
1 + src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height-1) / 2;
1 + src_node_row * ROOM_HEIGHT + src_room.offset_y + (src_room.height - 1) / 2;
let end_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x;
let end_row =
1 + tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height-1) / 2;
1 + tgt_node_row * ROOM_HEIGHT + tgt_room.offset_y + (tgt_room.height - 1) / 2;
rooms[src_node_col][src_node_row].connection_right = Some(Connection {
start_pos: (start_col, start_row),
end_pos: (end_col, end_row),
@ -220,40 +227,12 @@ impl LevelGenerator {
pub fn generate(level: usize, first: bool, last: bool) -> Self {
let mut rng = rand::thread_rng();
/*
Fill grid with unconnected rooms
*/
let mut rooms_to_place: Vec<Room> =
LevelGenerator::generate_rooms_to_place(&mut rng, level, first, last);
let mut rooms: [[Room; 7]; 8] = LevelGenerator::place_rooms(&mut rng, &mut rooms_to_place);
// debug print a text view of the dungeon
println!(" 0 1 2 3 4 5 6 7");
for r in 0..ROOMS_VERTICAL {
print!("{} ", r);
for c in 0..ROOMS_HORIZONTAL {
match rooms[c][r].kind {
RoomType::Start => print!("S "),
RoomType::End => print!("E "),
RoomType::StairUp => print!("< "),
RoomType::StairDown => print!("> "),
RoomType::BasicRoom => print!("_ "),
RoomType::ArtifactRoom => print!("A "),
RoomType::MonsterRoom => print!("M "),
RoomType::EmptyRoom => print!(" "),
};
}
println!();
}
/*
Construct a graph from the unconnected rooms and make a minum spanning tree of it
*/
let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
LevelGenerator::create_mst(&rooms);
LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng }
}

View File

@ -17,7 +17,11 @@ impl LevelWidget {
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg);
}
fn set_bold_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg).set_style(Style::new().add_modifier(Modifier::BOLD));
buf[(x, y)]
.set_symbol(symbol)
.set_bg(bg)
.set_fg(fg)
.set_style(Style::new().add_modifier(Modifier::BOLD));
}
}

View File

@ -45,7 +45,7 @@ create_monster!(
name:"rat".to_string(),
life: 2,
symbol: String::from("r"),
color: Color::Black,
color: Color::White,
experience_gain: 5,
ticks_between_steps: 5,
damage_range: 1..=2,
@ -67,7 +67,7 @@ create_monster!(
name: "orc".to_string(),
life: 4,
symbol: String::from("O"),
color: Color::DarkGray,
color: Color::Gray,
experience_gain: 10,
ticks_between_steps: 10,
damage_range: 2..=3,
@ -78,7 +78,7 @@ create_monster!(
name: "snake".to_string(),
life: 3,
symbol: String::from("s"),
color: Color::Black,
color: Color::White,
experience_gain: 10,
ticks_between_steps: 20,
damage_range: 1..=4,
@ -89,7 +89,7 @@ create_monster!(
name: "skeleton".to_string(),
life: 3,
symbol: String::from("S"),
color: Color::DarkGray,
color: Color::Gray,
experience_gain: 20,
ticks_between_steps: 10,
damage_range: 1..=5,

View File

@ -1,7 +1,4 @@
use std::{
cmp::{max, min},
ops::Range,
};
use std::cmp::{max, min};
use crate::{
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
@ -31,21 +28,72 @@ pub struct Connection {
impl Connection {
pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
// the tuples are (col, row)
println!("{:?} -> {:?}",self.start_pos, self.end_pos);
let d_col = max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
let abs_d_col =
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
let d_row = max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
let abs_d_row =
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
if d_col == 0 {
for row in self.start_pos.1..=self.end_pos.1 {
if abs_d_col == 0 {
let range = if 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;
}
}
if d_row == 0 {
for col in self.start_pos.0..=self.end_pos.0 {
} else if abs_d_row == 0 {
let range = if 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;
}
} else {
let d_row = self.end_pos.1 as i32 - self.start_pos.1 as i32;
if d_row > 0 {
// more up/down
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) {
tgt[self.start_pos.0][r] = StructureElement::Floor;
}
let range = if 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 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 {
tgt[self.end_pos.0][r] = StructureElement::Floor;
}
} else {
// more left/right
let d_col = self.end_pos.0 - self.start_pos.0;
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;
}
let range = if 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;
}
for tgt_col in tgt
.iter_mut()
.take(self.end_pos.0 + 1)
.skip(self.start_pos.0 + d_col / 2)
{
tgt_col[self.end_pos.1] = StructureElement::Floor;
}
}
}
}
}
@ -64,12 +112,12 @@ pub struct Room {
impl Room {
pub fn new(rng: &mut ThreadRng) -> Self {
let width = ROOM_WIDTH-1; //rng.gen_range(3..ROOM_WIDTH);
let height = ROOM_HEIGHT-1; //rng.gen_range(3..ROOM_HEIGHT);
let offset_x = 0; //rng.gen_range(0..(ROOM_WIDTH - width));
let offset_y = 0; //rng.gen_range(0..(ROOM_HEIGHT - height));
let sx = offset_x + rng.gen_range(1..width-1);
let sy = offset_y + rng.gen_range(1..height-1);
let width = rng.gen_range(3..ROOM_WIDTH);
let height = rng.gen_range(3..ROOM_HEIGHT);
let offset_x = rng.gen_range(0..(ROOM_WIDTH - width));
let offset_y = rng.gen_range(0..(ROOM_HEIGHT - height));
let sx = offset_x + rng.gen_range(1..width - 1);
let sy = offset_y + rng.gen_range(1..height - 1);
Self {
kind: RoomType::EmptyRoom,
@ -83,13 +131,13 @@ impl Room {
}
}
pub fn get_x_range(&self) -> Range<usize> {
self.offset_x..self.offset_x + self.width
}
// pub fn get_x_range(&self) -> Range<usize> {
// self.offset_x..self.offset_x + self.width
// }
pub fn get_y_range(&self) -> Range<usize> {
self.offset_y..self.offset_y + self.height
}
// pub fn get_y_range(&self) -> Range<usize> {
// self.offset_y..self.offset_y + self.height
// }
pub fn render(
&self,