Compare commits
No commits in common. "8ee142586b191cf29be5b64c230ad964db26b107" and "f5d257e82634dd3687c3d32f4292037f6072551f" have entirely different histories.
8ee142586b
...
f5d257e826
|
@ -3,6 +3,7 @@ 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);
|
||||
|
|
|
@ -2,6 +2,7 @@ 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
|
||||
|
|
|
@ -84,8 +84,8 @@ impl LevelGenerator {
|
|||
match directions_to_try.pop().unwrap() {
|
||||
Direction::Horizontal => {
|
||||
let mut free_cols: Vec<usize> = vec![];
|
||||
for (col, room_col) in rooms.iter().enumerate().take(ROOMS_HORIZONTAL) {
|
||||
if room_col[room_row].kind == RoomType::EmptyRoom {
|
||||
for col in 0..ROOMS_HORIZONTAL {
|
||||
if rooms[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, 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 {
|
||||
for col in 0..ROOMS_HORIZONTAL {
|
||||
for row in 0..ROOMS_VERTICAL {
|
||||
if rooms[col][row].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, 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 {
|
||||
for col in 0..ROOMS_HORIZONTAL {
|
||||
for row in 0..ROOMS_VERTICAL {
|
||||
if rooms[col][row].kind != RoomType::EmptyRoom {
|
||||
graph.add_node((col, row));
|
||||
}
|
||||
}
|
||||
|
@ -151,13 +151,8 @@ impl LevelGenerator {
|
|||
continue;
|
||||
}
|
||||
if let Some(src_index) = graph.node_indices().find(|i| graph[*i] == (col, row)) {
|
||||
for (col_1, tgt_col) in rooms
|
||||
.iter()
|
||||
.enumerate()
|
||||
.take(ROOMS_HORIZONTAL)
|
||||
.skip(col + 1)
|
||||
{
|
||||
if tgt_col[row].kind != RoomType::EmptyRoom {
|
||||
for col_1 in col + 1..ROOMS_HORIZONTAL {
|
||||
if rooms[col_1][row].kind != RoomType::EmptyRoom {
|
||||
if let Some(tgt_index) =
|
||||
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];
|
||||
|
||||
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 =
|
||||
|
@ -208,9 +202,8 @@ impl LevelGenerator {
|
|||
end_pos: (end_col, end_row),
|
||||
});
|
||||
} else {
|
||||
// Down
|
||||
let start_col =
|
||||
1 + (src_node_col * ROOM_WIDTH) + src_room.offset_x + src_room.width;
|
||||
// println!("Right");
|
||||
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;
|
||||
let end_col = 1 + tgt_node_col * ROOM_WIDTH - 1 + tgt_room.offset_x;
|
||||
|
@ -227,12 +220,40 @@ 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 }
|
||||
}
|
||||
|
|
|
@ -17,11 +17,7 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ create_monster!(
|
|||
name:"rat".to_string(),
|
||||
life: 2,
|
||||
symbol: String::from("r"),
|
||||
color: Color::White,
|
||||
color: Color::Black,
|
||||
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::Gray,
|
||||
color: Color::DarkGray,
|
||||
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::White,
|
||||
color: Color::Black,
|
||||
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::Gray,
|
||||
color: Color::DarkGray,
|
||||
experience_gain: 20,
|
||||
ticks_between_steps: 10,
|
||||
damage_range: 1..=5,
|
||||
|
|
94
src/room.rs
94
src/room.rs
|
@ -1,4 +1,7 @@
|
|||
use std::cmp::{max, min};
|
||||
use std::{
|
||||
cmp::{max, min},
|
||||
ops::Range,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
constants::{LEVEL_HEIGHT, LEVEL_WIDTH, ROOM_HEIGHT, ROOM_WIDTH},
|
||||
|
@ -28,72 +31,21 @@ pub struct Connection {
|
|||
impl Connection {
|
||||
pub fn render(&self, tgt: &mut [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH]) {
|
||||
// the tuples are (col, row)
|
||||
let abs_d_col =
|
||||
max(self.start_pos.0, self.end_pos.0) - min(self.start_pos.0, self.end_pos.0);
|
||||
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_row =
|
||||
max(self.start_pos.1, self.end_pos.1) - min(self.start_pos.1, self.end_pos.1);
|
||||
let d_row = max(self.start_pos.1, self.end_pos.1) - min(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 {
|
||||
if d_col == 0 {
|
||||
for row in self.start_pos.1..=self.end_pos.1 {
|
||||
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 {
|
||||
self.start_pos.0..=self.end_pos.0
|
||||
} else {
|
||||
self.end_pos.0..=self.start_pos.0
|
||||
};
|
||||
for col in range {
|
||||
}
|
||||
if d_row == 0 {
|
||||
for col in self.start_pos.0..=self.end_pos.0 {
|
||||
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,10 +64,10 @@ pub struct Room {
|
|||
|
||||
impl Room {
|
||||
pub fn new(rng: &mut ThreadRng) -> Self {
|
||||
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 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);
|
||||
|
||||
|
@ -131,13 +83,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,
|
||||
|
|
Loading…
Reference in New Issue