1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
//! Turn-based dungeons: the map, field of view, derelict-ship generation,
//! and the rules of a crawl. Drawing lives in `scenes::derelict`.
pub mod armour;
pub mod arms;
pub mod crawl;
pub mod fov;
pub mod ship;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Pos {
pub x: i32,
pub y: i32,
}
impl Pos {
pub const fn new(x: i32, y: i32) -> Pos {
Pos { x, y }
}
pub const fn offset(self, dx: i32, dy: i32) -> Pos {
Pos::new(self.x + dx, self.y + dy)
}
/// Steps from here to `other` when diagonal moves are allowed.
pub fn steps_to(self, other: Pos) -> i32 {
(self.x - other.x).abs().max((self.y - other.y).abs())
}
/// The eight cells around this one.
pub fn neighbours(self) -> impl Iterator<Item = Pos> {
const DIRS: [(i32, i32); 8] =
[(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)];
DIRS.into_iter().map(move |(dx, dy)| self.offset(dx, dy))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tile {
/// Outside the ship.
Void,
/// The outer skin of the ship.
Hull,
/// An inside wall.
Wall,
Floor,
/// Floor strewn with wreckage. Walkable; it just looks different.
Debris,
Door {
open: bool,
},
/// The way out: the airlock your own ship is docked to.
Airlock,
}
impl Tile {
/// Whether you can step onto it. Closed doors open when bumped instead,
/// and stepping into the airlock leaves the ship.
pub fn walkable(self) -> bool {
matches!(self, Tile::Floor | Tile::Debris | Tile::Door { open: true })
}
pub fn blocks_sight(self) -> bool {
matches!(
self,
Tile::Void | Tile::Hull | Tile::Wall | Tile::Door { open: false } | Tile::Airlock
)
}
}
/// A named area of the map, shown to the player while they stand in it.
pub struct Room {
pub name: &'static str,
}
pub struct Map {
pub width: i32,
pub height: i32,
tiles: Vec<Tile>,
room_of: Vec<Option<u16>>,
pub rooms: Vec<Room>,
}
impl Map {
pub fn new(width: i32, height: i32, fill: Tile) -> Map {
let cells = (width * height) as usize;
Map {
width,
height,
tiles: vec![fill; cells],
room_of: vec![None; cells],
rooms: Vec::new(),
}
}
pub fn contains(&self, p: Pos) -> bool {
p.x >= 0 && p.y >= 0 && p.x < self.width && p.y < self.height
}
/// Index into a per-cell `Vec` laid out like the map. Panics off the map.
pub fn index(&self, p: Pos) -> usize {
assert!(self.contains(p), "{p:?} is off the map");
(p.y * self.width + p.x) as usize
}
/// The tile at `p`; everything off the map is `Void`.
pub fn tile(&self, p: Pos) -> Tile {
if self.contains(p) { self.tiles[self.index(p)] } else { Tile::Void }
}
pub fn set(&mut self, p: Pos, tile: Tile) {
let i = self.index(p);
self.tiles[i] = tile;
}
/// Which room `p` is in, to tell when you've gone into another.
pub fn room_index(&self, p: Pos) -> Option<u16> {
if !self.contains(p) {
return None;
}
self.room_of[self.index(p)]
}
pub fn room_at(&self, p: Pos) -> Option<&Room> {
if !self.contains(p) {
return None;
}
self.room_of[self.index(p)].map(|r| &self.rooms[usize::from(r)])
}
pub fn add_room(&mut self, name: &'static str) -> u16 {
self.rooms.push(Room { name });
(self.rooms.len() - 1) as u16
}
pub fn set_room(&mut self, p: Pos, room: Option<u16>) {
let i = self.index(p);
self.room_of[i] = room;
}
pub fn positions(&self) -> impl Iterator<Item = Pos> + use<> {
let (w, h) = (self.width, self.height);
(0..h).flat_map(move |y| (0..w).map(move |x| Pos::new(x, y)))
}
/// Steps from `from` to every cell reachable through walkable tiles and
/// closed doors, moving diagonally too; `None` where unreachable.
pub fn distances_from(&self, from: Pos) -> Vec<Option<u32>> {
let mut dist = vec![None; self.tiles.len()];
let mut queue = std::collections::VecDeque::from([from]);
dist[self.index(from)] = Some(0);
while let Some(p) = queue.pop_front() {
let d = dist[self.index(p)].unwrap();
for n in p.neighbours() {
let tile = self.tile(n);
if (tile.walkable() || tile == (Tile::Door { open: false }))
&& dist[self.index(n)].is_none()
{
dist[self.index(n)] = Some(d + 1);
queue.push_back(n);
}
}
}
dist
}
}
|