Compare commits

..

No commits in common. "8ee142586b191cf29be5b64c230ad964db26b107" and "f5d257e82634dd3687c3d32f4292037f6072551f" have entirely different histories.

6 changed files with 74 additions and 103 deletions

View File

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

View File

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

View File

@ -84,8 +84,8 @@ impl LevelGenerator {
match directions_to_try.pop().unwrap() { match directions_to_try.pop().unwrap() {
Direction::Horizontal => { Direction::Horizontal => {
let mut free_cols: Vec<usize> = vec![]; let mut free_cols: Vec<usize> = vec![];
for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { for col in 0..ROOMS_HORIZONTAL {
if room_col[room_row].kind == RoomType::EmptyRoom { if rooms[col][room_row].kind == RoomType::EmptyRoom {
free_cols.push(col); 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 // all fields in the row/column was full so we can place it at any empty position
if !placed { if !placed {
let mut free_pos: Vec<(usize, usize)> = vec![]; let mut free_pos: Vec<(usize, usize)> = vec![];
for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { for col in 0..ROOMS_HORIZONTAL {
for (row, room) in room_col.iter().enumerate().take(ROOMS_VERTICAL) { for row in 0..ROOMS_VERTICAL {
if room.kind == RoomType::EmptyRoom { if rooms[col][row].kind == RoomType::EmptyRoom {
free_pos.push((col, row)); free_pos.push((col, row));
} }
} }
@ -138,9 +138,9 @@ impl LevelGenerator {
rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL], rooms: &[[Room; ROOMS_VERTICAL]; ROOMS_HORIZONTAL],
) -> Graph<(usize, usize), u16, petgraph::Undirected> { ) -> Graph<(usize, usize), u16, petgraph::Undirected> {
let mut graph = UnGraph::<(usize, usize), u16>::default(); let mut graph = UnGraph::<(usize, usize), u16>::default();
for (col, tgt_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) { for col in 0..ROOMS_HORIZONTAL {
for (row, room) in tgt_col.iter().enumerate().take(ROOMS_VERTICAL) { for row in 0..ROOMS_VERTICAL {
if room.kind != RoomType::EmptyRoom { if rooms[col][row].kind != RoomType::EmptyRoom {
graph.add_node((col, row)); graph.add_node((col, row));
} }
} }
@ -151,13 +151,8 @@ impl LevelGenerator {
continue; continue;
} }
if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) { if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) {
for (col_1, tgt_col) in rooms for col_1 in col + 1..ROOMS_HORIZONTAL {
.iter() if rooms[col_1][row].kind != RoomType::EmptyRoom {
.enumerate()
.take(ROOMS_HORIZONTAL)
.skip(col + 1)
{
if tgt_col[row].kind != RoomType::EmptyRoom {
if let Some(tgt_index) = if let Some(tgt_index) =
graph.node_indices().find(|i| graph[*i] == (col_1, row)) graph.node_indices().find(|i| graph[*i] == (col_1, row))
{ {
@ -195,7 +190,6 @@ impl LevelGenerator {
let tgt_room = rooms[tgt_node_col][tgt_node_row]; let tgt_room = rooms[tgt_node_col][tgt_node_row];
if src_node_col == tgt_node_col { if src_node_col == tgt_node_col {
// Right
let start_col = let start_col =
src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2; src_node_col * ROOM_WIDTH + src_room.offset_x + (src_room.width + 1) / 2;
let start_row = let start_row =
@ -208,14 +202,13 @@ impl LevelGenerator {
end_pos: (end_col, end_row), end_pos: (end_col, end_row),
}); });
} else { } else {
// Down // println!("Right");
let start_col = let start_col = 1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
let start_row = 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_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x;
let end_row = 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 { rooms[src_node_col][src_node_row].connection_right = Some(Connection {
start_pos: (start_col, start_row), start_pos: (start_col, start_row),
end_pos: (end_col, end_row), end_pos: (end_col, end_row),
@ -227,12 +220,40 @@ impl LevelGenerator {
pub fn generate(level: usize, first: bool, last: bool) -> Self { pub fn generate(level: usize, first: bool, last: bool) -> Self {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
/*
Fill grid with unconnected rooms
*/
let mut rooms_to_place: Vec<Room> = let mut rooms_to_place: Vec<Room> =
LevelGenerator::generate_rooms_to_place(&mut rng, level, first, last); 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); 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> = let mst: Graph<(usize, usize), u16, petgraph::Undirected> =
LevelGenerator::create_mst(&rooms); LevelGenerator::create_mst(&rooms);
LevelGenerator::create_connections(&mut rooms, &mst); LevelGenerator::create_connections(&mut rooms, &mst);
LevelGenerator { level, rooms, rng } LevelGenerator { level, rooms, rng }
} }

View File

@ -17,11 +17,7 @@ impl LevelWidget {
buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg); 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) { fn set_bold_cell(&self, buf: &mut Buffer, x: u16, y: u16, symbol: &str, fg: Color, bg: Color) {
buf[(x, y)] buf[(x, y)].set_symbol(symbol).set_bg(bg).set_fg(fg).set_style(Style::new().add_modifier(Modifier::BOLD));
.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(), name:"rat".to_string(),
life: 2, life: 2,
symbol: String::from("r"), symbol: String::from("r"),
color: Color::White, color: Color::Black,
experience_gain: 5, experience_gain: 5,
ticks_between_steps: 5, ticks_between_steps: 5,
damage_range: 1..=2, damage_range: 1..=2,
@ -67,7 +67,7 @@ create_monster!(
name: "orc".to_string(), name: "orc".to_string(),
life: 4, life: 4,
symbol: String::from("O"), symbol: String::from("O"),
color: Color::Gray, color: Color::DarkGray,
experience_gain: 10, experience_gain: 10,
ticks_between_steps: 10, ticks_between_steps: 10,
damage_range: 2..=3, damage_range: 2..=3,
@ -78,7 +78,7 @@ create_monster!(
name: "snake".to_string(), name: "snake".to_string(),
life: 3, life: 3,
symbol: String::from("s"), symbol: String::from("s"),
color: Color::White, color: Color::Black,
experience_gain: 10, experience_gain: 10,
ticks_between_steps: 20, ticks_between_steps: 20,
damage_range: 1..=4, damage_range: 1..=4,
@ -89,7 +89,7 @@ create_monster!(
name: "skeleton".to_string(), name: "skeleton".to_string(),
life: 3, life: 3,
symbol: String::from("S"), symbol: String::from("S"),
color: Color::Gray, color: Color::DarkGray,
experience_gain: 20, experience_gain: 20,
ticks_between_steps: 10, ticks_between_steps: 10,
damage_range: 1..=5, damage_range: 1..=5,

View File

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