Add visibility simulation

This commit is contained in:
Alex Bethel 2022-01-15 20:15:17 -06:00
parent 9a2b24e006
commit daafe3b023
4 changed files with 125 additions and 8 deletions

View file

@ -101,12 +101,18 @@ impl DungeonLevel {
level.exits
}
/// Draws a level on the display window.
pub fn draw(&self, win: &Window) {
/// Draws a level on the display window. Draws only the cells for
/// which `filter` returns true; use `|_| true` to draw the whole
/// level.
pub fn draw(&self, win: &Window, filter: impl Fn((i32, i32)) -> bool) {
for y in 0..LEVEL_SIZE.1 {
win.mv(y as _, 0);
for x in 0..LEVEL_SIZE.0 {
win.addch(self.render_tile(x, y));
win.addch(if filter((x as _, y as _)) {
self.render_tile(x, y)
} else {
' '
});
}
}
}

View file

@ -15,6 +15,7 @@ mod player;
mod rooms;
mod systems;
mod util;
mod visibility;
fn main() {
let mut world = World::new();

View file

@ -7,6 +7,7 @@ use crate::{
components::{CharRender, MobAction, Mobile, Player, Position},
level::DungeonLevel,
quit,
visibility::{visible, CellVisibility, Lighting},
};
/// Runs a player turn on the ECS, using the given `screen` for input
@ -85,9 +86,32 @@ fn possible(ecs: &World, action: &MobAction) -> bool {
/// Renders the state of the world onto the screen.
fn render_screen(ecs: &mut World, screen: &mut Window) {
// Calculate the player's position.
let plrs = ecs.read_storage::<Player>();
let pos = ecs.read_storage::<Position>();
let (_plr, player_pos) = (&plrs, &pos)
.join()
.next()
.expect("Player must have a position");
// Draw the base level.
let level = ecs.fetch::<DungeonLevel>();
level.draw(screen);
level.draw(screen, |cell| {
visible(
(player_pos.x, player_pos.y),
cell,
Some(10),
|(x, y)| {
if level.tile(x, y).is_navigable() {
CellVisibility::Transparent
} else {
CellVisibility::Blocking
}
},
// Level is fully lit for now.
|(_x, _y)| Lighting::Lit,
)
});
// Draw all renderable entities.
let renderables = ecs.read_storage::<CharRender>();
@ -97,10 +121,7 @@ fn render_screen(ecs: &mut World, screen: &mut Window) {
}
// Leave the cursor on the player's position.
let plrs = ecs.read_storage::<Player>();
let pos = ecs.read_storage::<Position>();
let (_plr, pos) = (&plrs, &pos).join().next().unwrap();
screen.mv(pos.y, pos.x);
screen.mv(player_pos.y, player_pos.x);
screen.refresh();
}

89
src/visibility.rs Normal file
View file

@ -0,0 +1,89 @@
//! Code for determining which cells the player and monsters can see.
/// The light transmission properties of a cell in the world.
#[derive(Debug, PartialEq)]
pub enum CellVisibility {
/// This cell allows light to pass through: monsters can see
/// through this cell as if it is air.
Transparent,
/// This cell blocks all light.
Blocking,
}
/// How well-lit a cell is.
#[derive(Debug, PartialEq)]
pub enum Lighting {
/// Monsters can only see in this cell if the cell is immediately
/// adjacent to the monster.
Dark,
/// Monsters can see in this cell from far away.
Lit,
}
/// Calculates whether a monster standing at `origin` can see the
/// contents of cell `cell`. We assume the monster can see `radius`
/// cells away at best (None for unlimited range), that `cell_map`
/// represents whether a cell transmits light, and that `light_map`
/// represents how well-lit a cell is.
pub fn visible(
origin: (i32, i32),
cell: (i32, i32),
radius: Option<i32>,
cell_map: impl Fn((i32, i32)) -> CellVisibility,
light_map: impl Fn((i32, i32)) -> Lighting,
) -> bool {
let dx = cell.0 - origin.0;
let dy = cell.1 - origin.1;
radius
.map(|radius| dx * dx + dy * dy < radius * radius)
.unwrap_or(true)
&& (light_map(cell) == Lighting::Lit)
&& (line(origin, cell).all(|tile| cell_map(tile) == CellVisibility::Transparent))
}
/// Constructs an iterator over the cells in a straight line from
/// `start` to `end`. The line will include `start`, but not `end`.
fn line(start: (i32, i32), end: (i32, i32)) -> Box<dyn Iterator<Item = (i32, i32)>> {
// We could use a dedicated iterator type here eventually and
// avoid the `Box` allocations, but I'm gonna assume it's not a
// significant problem until proven otherwise.
let dx = end.0 - start.0;
let dy = end.1 - start.1;
// Transform the world so we're working from left to right, with
// slope magnitude less than 1.
if dx.abs() < dy.abs() {
Box::new(line((start.1, start.0), (end.1, end.0)).map(|(x, y)| (y, x)))
} else if dx < 0 {
Box::new(line((-start.0, start.1), (-end.0, end.1)).map(|(x, y)| (-x, y)))
} else {
// Move the destination over by 0.5 cells on each axis, to
// navigate to the corner rather than the center of the target
// cell. It's weird but it makes things work way better.
let dx = dx as f64 - 0.5;
let dy = if dy > 0 {
dy as f64 - 0.5
} else if dy < 0 {
dy as f64 + 0.5
} else {
dy as f64
};
// Now use float math to step along the line, one cell at a
// time.
let slope = dy as f64 / dx as f64;
Box::new(
std::iter::successors(Some((start.0, start.1 as f64)), move |&(x, y)| {
Some((x + 1, y + slope))
})
// Add 0.5 here to round to nearest rather than rounding
// towards zero (eliminates some bias).
.map(|(x, y)| (x, (y + 0.5) as i32))
.take_while(move |(x, _y)| x < &end.0),
)
}
}