el_diablo/src/level.rs

376 lines
12 KiB
Rust
Raw Normal View History

2023-12-03 09:25:03 +01:00
use std::cmp::{max, min};
2023-12-18 16:11:19 +01:00
2023-12-30 16:31:27 +01:00
use rand::rngs::ThreadRng;
2024-10-20 14:59:20 +02:00
use rand::Rng;
2023-12-30 16:31:27 +01:00
2024-10-20 14:59:20 +02:00
use crate::artifacts::Artifact;
2023-12-09 22:43:06 +01:00
#[cfg(test)]
use crate::artifacts::{Chest, Potion};
2024-10-20 14:59:20 +02:00
use crate::monster::Monster;
2023-12-18 16:11:19 +01:00
#[cfg(test)]
2023-12-25 13:26:45 +01:00
use crate::monster::{Orc, Rat};
2023-12-26 11:38:23 +01:00
use crate::player::Player;
2023-12-25 13:47:15 +01:00
use crate::position::HasPosition;
2023-11-27 19:21:10 +01:00
use crate::position::Position;
pub const LEVEL_WIDTH: usize = 50;
pub const LEVEL_HEIGHT: usize = 25;
2023-11-08 06:52:20 +01:00
#[derive(Copy, Clone, Debug, PartialEq)]
2023-11-27 21:24:43 +01:00
pub enum StructureElement {
2023-12-04 18:41:21 +01:00
Start,
End,
2023-11-08 06:52:20 +01:00
Wall,
Floor,
StairDown,
StairUp,
2023-11-27 19:21:10 +01:00
Unknown,
2023-11-08 06:52:20 +01:00
}
2024-10-20 14:59:20 +02:00
type PossibleArtifact<'a> = Option<&'a mut Box<(dyn Artifact + 'static)>>;
type PossibleMonster<'a> = Option<&'a mut Box<(dyn Monster + 'static)>>;
2023-11-08 06:52:20 +01:00
pub struct Level {
2023-12-05 17:13:39 +01:00
pub(crate) level: usize,
pub(crate) structure: [[StructureElement; LEVEL_HEIGHT]; LEVEL_WIDTH],
pub(crate) discovered: [[bool; LEVEL_HEIGHT]; LEVEL_WIDTH],
2023-12-18 16:11:19 +01:00
pub(crate) monsters: Vec<Box<dyn Monster>>,
2023-12-05 17:13:39 +01:00
pub(crate) artifacts: Vec<Box<dyn Artifact>>,
2023-12-06 20:59:03 +01:00
/// the position of the start in the level (either stair up or start point)
2023-12-05 17:13:39 +01:00
pub(crate) start: (usize, usize),
2023-12-06 20:59:03 +01:00
/// the position of the end in the level (either stair down or end point)
2023-12-05 17:13:39 +01:00
pub(crate) end: (usize, usize),
2023-12-26 19:11:41 +01:00
pub(crate) rng: ThreadRng,
2023-11-08 06:52:20 +01:00
}
impl Level {
2024-10-20 14:59:20 +02:00
pub fn get_element(
&mut self,
x: i16,
y: i16,
) -> (
Option<StructureElement>,
PossibleMonster,
PossibleArtifact
) {
2023-11-27 19:21:10 +01:00
if x < 0 || y < 0 {
2023-12-03 13:44:29 +01:00
return (None, None, None);
2023-11-27 19:21:10 +01:00
}
let x = x as usize;
let y = y as usize;
if x >= LEVEL_WIDTH || y >= LEVEL_HEIGHT {
2023-12-03 13:44:29 +01:00
return (None, None, None);
2023-11-08 06:52:20 +01:00
}
2023-11-27 19:21:10 +01:00
if !self.discovered[x][y] {
2023-12-03 13:44:29 +01:00
return (Some(StructureElement::Unknown), None, None);
2023-11-27 19:21:10 +01:00
}
2023-12-03 13:44:29 +01:00
let search_pos = &Position::new(self.level, x, y);
2023-12-18 16:11:19 +01:00
let mut res_m: Option<&mut Box<dyn Monster>> = None;
2023-12-03 09:25:03 +01:00
for m in &mut self.monsters {
2023-12-03 13:44:29 +01:00
if m.get_immutable_position() == search_pos {
2023-12-03 09:25:03 +01:00
res_m = Some(m);
}
}
2023-12-03 23:59:05 +01:00
let mut res_a: Option<&mut Box<dyn Artifact>> = None;
2023-12-03 13:44:29 +01:00
for a in &mut self.artifacts {
if a.get_immutable_position() == search_pos {
res_a = Some(a);
}
}
(Some(self.structure[x][y]), res_m, res_a)
2023-12-03 09:25:03 +01:00
}
2023-11-27 19:21:10 +01:00
/// discover the area with in the level around the given position
pub fn discover(&mut self, pos: &Position) {
let x = pos.get_x();
let y = pos.get_y();
// uncover fields directly next to the player
for x_r in max(x - 1, 0)..=min(x + 1, LEVEL_WIDTH - 1) {
for y_r in max(y - 1, 0)..=min(y + 1, LEVEL_HEIGHT - 1) {
self.discovered[x_r][y_r] = true;
}
}
// uncover fields up to 2 fields right of the player
for x_r in x..=min(x + 2, LEVEL_WIDTH - 1) {
self.discovered[x_r][y] = true;
if self.structure[x_r][y] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields below of the player
for y_r in y..=min(y + 2, LEVEL_HEIGHT - 1) {
self.discovered[x][y_r] = true;
if self.structure[x][y_r] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields left of the player
for x_r in (max(x, 2)..x + 2).rev() {
self.discovered[x_r - 2][y] = true;
if self.structure[x_r - 2][y] == StructureElement::Wall {
break;
}
}
// uncover fields up to 2 fields above of the player
for y_r in (max(y, 2)..y + 2).rev() {
self.discovered[x][y_r - 2] = true;
if self.structure[x][y_r - 2] == StructureElement::Wall {
break;
}
}
}
2023-12-31 15:59:52 +01:00
pub fn update(&mut self, ticks: u128, player: &mut Player, messages: &mut Vec<String>) {
2023-12-17 16:14:17 +01:00
for (index, a) in &mut self.artifacts.iter().enumerate() {
if a.was_collected() {
self.artifacts.remove(index);
break;
}
}
2023-12-26 11:38:23 +01:00
for index in 0..self.monsters.len() {
2023-12-30 16:31:27 +01:00
if self.monsters[index].is_dead() {
continue;
}
2023-12-26 19:11:41 +01:00
if ticks % self.monsters[index].get_ticks_between_steps() != 0 {
continue;
}
loop {
// calculate the direction the monster will try to walk
let (dx, dy) = match self.rng.gen_range(0..5) {
2024-10-20 14:59:20 +02:00
1 => (1, 0),
2 => (-1, 0),
3 => (0, 1),
4 => (0, -1),
_ => (0, 0),
2023-12-26 19:11:41 +01:00
};
if self.can_monster_move(self.monsters[index].as_ref(), dx, dy) {
let (new_x, new_y) = self.monsters[index].get_position().change(dx, dy);
2024-10-20 14:59:20 +02:00
if player.get_immutable_position().get_x() == new_x
&& player.get_immutable_position().get_y() == new_y
{
2023-12-26 19:11:41 +01:00
self.monsters[index].decrease_life(1);
player.change_life(-1);
2024-10-20 14:59:20 +02:00
messages.insert(
0,
format!("{} hits you.", self.monsters[index].get_name()).to_string(),
);
messages.insert(
0,
format!("you hit {}.", self.monsters[index].get_name()).to_string(),
);
2023-12-30 16:31:27 +01:00
// if the attack did not kill the opponent, back down
if !player.is_dead() {
self.monsters[index].get_position().change(-dx, -dy);
}
2023-12-26 19:11:41 +01:00
}
break;
}
2023-12-26 11:38:23 +01:00
}
}
2023-12-30 16:31:27 +01:00
for (index, m) in &mut self.monsters.iter().enumerate() {
if m.is_dead() {
self.monsters.remove(index);
break;
}
}
2023-12-26 11:38:23 +01:00
}
2023-12-26 19:11:41 +01:00
2023-12-26 11:38:23 +01:00
pub fn can_monster_move(&self, agent: &dyn Monster, dx: i16, dy: i16) -> bool {
let agent_pos = agent.get_immutable_position();
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
2023-12-26 19:11:41 +01:00
2024-01-03 09:12:00 +01:00
// verify we cannot leave the playing field
2023-12-30 16:31:27 +01:00
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
2023-12-26 19:11:41 +01:00
return false;
}
for index in 0..self.monsters.len() {
let pos = self.monsters[index].get_immutable_position();
2024-10-20 14:59:20 +02:00
if pos.get_x() == new_x && pos.get_y() == new_y {
return false;
}
2023-12-26 19:11:41 +01:00
}
2023-12-26 11:38:23 +01:00
self.structure[new_x][new_y] != StructureElement::Wall
}
pub fn can_player_move(&self, agent: &Player, dx: i16, dy: i16) -> bool {
let agent_pos = agent.get_immutable_position();
let new_x: usize = (agent_pos.get_x() as i16 + dx) as usize;
let new_y: usize = (agent_pos.get_y() as i16 + dy) as usize;
2024-01-03 09:12:00 +01:00
// verify we cannot leave the playing field
if new_x >= LEVEL_WIDTH || new_y >= LEVEL_HEIGHT {
return false;
}
2023-12-26 11:38:23 +01:00
self.structure[new_x][new_y] != StructureElement::Wall
}
#[cfg(test)]
pub fn new(level: usize) -> Level {
let mut s = [[StructureElement::Wall; LEVEL_HEIGHT]; LEVEL_WIDTH];
for x in 2..LEVEL_WIDTH - 2 {
for y in 2..LEVEL_HEIGHT - 2 {
s[x][y] = StructureElement::Floor;
}
}
Level {
level,
structure: s,
discovered: [[false; LEVEL_HEIGHT]; LEVEL_WIDTH],
monsters: Vec::with_capacity(10),
artifacts: Vec::with_capacity(10),
start: (0, 0),
end: (0, 0),
2023-12-26 19:11:41 +01:00
rng: rand::thread_rng(),
2023-12-26 11:38:23 +01:00
}
}
#[cfg(test)]
pub fn add_monster(&mut self, mut monster: impl Monster + 'static) -> Result<(), String> {
if self.level != monster.get_position().get_level() {
return Err("Wrong Level".to_string());
}
for m in &mut self.monsters {
if m.get_position() == monster.get_position() {
return Err("Position already used".to_string());
}
}
self.monsters.push(Box::new(monster));
Ok(())
}
#[cfg(test)]
pub fn add_artifact(&mut self, artifact: impl Artifact + 'static) -> Result<(), String> {
if self.level != artifact.get_immutable_position().get_level() {
return Err("Wrong Level".to_string());
}
for a in &mut self.artifacts {
if a.get_immutable_position() == artifact.get_immutable_position() {
return Err("Position already used".to_string());
}
}
self.artifacts.push(Box::new(artifact));
Ok(())
2023-12-17 16:14:17 +01:00
}
2023-11-08 06:52:20 +01:00
}
#[test]
fn test_get_element() {
2023-12-03 09:25:03 +01:00
let mut l = Level::new(0);
assert_eq!(l.get_element(-1, -1).0, None);
assert_eq!(l.get_element(0, 0).0.unwrap(), StructureElement::Unknown);
2024-10-20 14:59:20 +02:00
assert_eq!(
l.get_element(LEVEL_WIDTH as i16 - 1, LEVEL_HEIGHT as i16 - 1)
.0
.unwrap(),
StructureElement::Unknown
);
assert_eq!(
l.get_element(LEVEL_WIDTH as i16, LEVEL_HEIGHT as i16).0,
None
);
2023-11-27 19:21:10 +01:00
}
#[test]
fn test_discover_get_element() {
2023-12-03 09:25:03 +01:00
let mut l = Level::new(0);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Unknown);
2023-11-27 19:21:10 +01:00
let p = Position::new(0, 10, 10);
l.discover(&p);
2023-12-03 09:25:03 +01:00
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(9, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 9).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(11, 10).0.unwrap(), StructureElement::Floor);
assert_eq!(l.get_element(10, 11).0.unwrap(), StructureElement::Floor);
}
#[test]
fn test_discover_can_add_monster() {
let mut l = Level::new(0);
2023-12-25 13:26:45 +01:00
let m = Rat::new_with_position(Position::new(1, 2, 3));
2023-12-03 09:25:03 +01:00
assert_eq!(l.add_monster(m), Err("Wrong Level".to_string()));
2023-12-25 13:26:45 +01:00
let mut m = Orc::new_with_position(Position::new(0, 2, 3));
2023-12-03 09:25:03 +01:00
m.get_position().set(0, 2, 3);
assert_eq!(l.add_monster(m), Ok(()));
2023-12-25 13:26:45 +01:00
let m = Rat::new_with_position(Position::new(0, 2, 3));
2023-12-03 09:25:03 +01:00
assert_eq!(l.add_monster(m), Err("Position already used".to_string()));
2023-12-25 13:26:45 +01:00
let m = Rat::new_with_position(Position::new(0, 2, 4));
2023-12-03 09:25:03 +01:00
assert_eq!(l.add_monster(m), Ok(()));
}
2023-12-03 13:44:29 +01:00
#[test]
fn test_discover_can_add_artifact() {
let mut l = Level::new(0);
2023-12-03 23:59:05 +01:00
let a = Chest::new(Position::new(1, 2, 3));
2023-12-03 13:44:29 +01:00
assert_eq!(l.add_artifact(a), Err("Wrong Level".to_string()));
2023-12-03 23:59:05 +01:00
let a = Chest::new(Position::new(0, 2, 3));
2023-12-03 13:44:29 +01:00
assert_eq!(l.add_artifact(a), Ok(()));
2023-12-03 23:59:05 +01:00
let a = Potion::new(Position::new(0, 2, 3));
2023-12-03 13:44:29 +01:00
assert_eq!(l.add_artifact(a), Err("Position already used".to_string()));
2023-12-03 23:59:05 +01:00
let a = Chest::new(Position::new(0, 1, 3));
2023-12-03 13:44:29 +01:00
assert_eq!(l.add_artifact(a), Ok(()));
}
2023-12-03 09:25:03 +01:00
#[test]
fn test_discover_get_monster() {
let mut l = Level::new(0);
let p = Position::new(0, 10, 10);
l.discover(&p);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10).1.is_none());
2023-12-25 13:26:45 +01:00
let m = Rat::new_with_position(Position::new(0, 10, 10));
2023-12-03 09:25:03 +01:00
assert_eq!(l.add_monster(m), Ok(()));
2023-12-03 09:36:58 +01:00
let elem = l.get_element(10, 10);
2023-12-03 09:25:03 +01:00
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.1.is_some());
let m = elem.1.unwrap();
2023-12-25 13:26:45 +01:00
assert_eq!(m.get_life(), 2);
2023-12-03 09:25:03 +01:00
m.decrease_life(2);
2023-12-25 13:26:45 +01:00
assert_eq!(l.get_element(10, 10).1.unwrap().get_life(), 0);
2023-11-08 06:52:20 +01:00
}
2023-12-03 09:36:58 +01:00
2023-12-03 13:44:29 +01:00
#[test]
fn test_discover_get_artifact() {
let mut l = Level::new(0);
let p = Position::new(0, 10, 10);
l.discover(&p);
assert_eq!(l.get_element(10, 10).0.unwrap(), StructureElement::Floor);
assert!(l.get_element(10, 10).2.is_none());
2023-12-03 23:59:05 +01:00
let a = Chest::new(Position::new(0, 10, 10));
2023-12-03 13:44:29 +01:00
assert_eq!(l.add_artifact(a), Ok(()));
let elem = l.get_element(10, 10);
assert_eq!(elem.0.unwrap(), StructureElement::Floor);
assert!(elem.2.is_some());
}
2023-12-03 09:36:58 +01:00
#[test]
fn test_discover_get_monster_can_move() {
let mut l = Level::new(0);
let p = Position::new(0, 10, 10);
l.discover(&p);
2023-12-25 13:26:45 +01:00
let m = Rat::new_with_position(Position::new(0, 10, 10));
2023-12-03 09:36:58 +01:00
l.add_monster(m).expect("Panic because of");
let m = l.get_element(10, 10).1.unwrap();
m.get_position().change(1, 1);
let m = l.get_element(10, 10).1;
assert!(m.is_none());
let m = l.get_element(11, 11).1;
assert!(m.is_some());
2023-12-25 13:26:45 +01:00
assert_eq!(m.unwrap().get_life(), 2);
2023-12-03 09:36:58 +01:00
}