1 use rltk::{RandomNumberGenerator, RGB, Rltk, Algorithm2D, Point, BaseMap};
2 use super::{Rect};
3 use std::cmp::{max, min};
4 use specs::prelude::*;
5 use serde::{Serialize, Deserialize};
6 use std::collections::HashSet;
7
8 pub const MAPWIDTH : usize = 80;
9 pub const MAPHEIGHT : usize = 38;
10 pub const MAPCOUNT : usize = MAPHEIGHT * MAPWIDTH;
11
12 #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
13 pub enum TileType {
14 Wall,
15 Floor,
16 DownStairs,
17 }
18
19 #[derive(Default, Serialize, Deserialize, Clone)]
20 pub struct Map {
21 pub tiles: Vec<TileType>,
22 pub rooms: Vec<Rect>,
23 pub width: i32,
24 pub height: i32,
25 pub revealed_tiles : Vec<bool>,
26 pub visible_tiles: Vec<bool>,
27 pub blocked : Vec<bool>,
28 pub depth: i32,
29 pub bloodstains : HashSet<usize>,
30
31 #[serde(skip_serializing)]
32 #[serde(skip_deserializing)]
33 pub tile_content : Vec<Vec<Entity>>
34 }
35 impl Map {
36 pub fn clear_content_index(&mut self) {
37 for content in self.tile_content.iter_mut() {
38 content.clear();
39 }
40 }
41
42 pub fn populate_blocked(&mut self) {
43 for (i,tile) in self.tiles.iter_mut().enumerate() {
44 self.blocked[i] = *tile == TileType::Wall;
45 }
46 }
47
48 fn is_exit_valid(&self, x:i32, y:i32) -> bool {
49 if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
50 let idx = self.xy_idx(x, y);
51 !self.blocked[idx]
52 }
53
54 pub fn xy_idx(&self, x: i32, y: i32) -> usize {
55 (y as usize * self.width as usize) + x as usize
56 }
57
58 fn apply_room_to_map(&mut self, room: &Rect) {
59 for y in room.y1 +1 ..= room.y2 {
60 for x in room.x1 + 1 ..= room.x2 {
61 let idx = self.xy_idx(x,y);
62 self.tiles[idx] = TileType::Floor;
63 }
64 }
65 }
66
67 fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
68 for x in min(x1,x2) ..= max(x1,x2) {
69 let idx = self.xy_idx(x, y);
70 if idx > 0 && idx < self.width as usize * self.height as usize {
71 self.tiles[idx as usize] = TileType::Floor;
72 }
73 }
74 }
75
76 fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
77 for y in min(y1,y2) ..= max(y1,y2) {
78 let idx = self.xy_idx(x, y);
79 if idx > 0 && idx < self.width as usize * self.height as usize {
80 self.tiles[idx as usize] = TileType::Floor;
81 }
82 }
83 }
84
85 pub fn new_map_rooms_and_corridors(new_depth: i32) -> Map {
86 let mut map = Map{
87 tiles : vec![TileType::Wall; MAPCOUNT],
88 rooms : Vec::new(),
89 width : MAPWIDTH as i32,
90 height: MAPHEIGHT as i32,
91 revealed_tiles : vec![false; MAPCOUNT],
92 visible_tiles : vec![false; MAPCOUNT],
93 blocked : vec![false; MAPCOUNT],
94 depth: new_depth,
95 bloodstains: HashSet::new(),
96 tile_content : vec![Vec::new(); MAPCOUNT]
97 };
98
99 const MAX_ROOMS : i32 = 30;
100 const MIN_SIZE : i32 = 6;
101 const MAX_SIZE : i32 = 10;
102
103 let mut rng = RandomNumberGenerator::new();
104
105 for _ in 0..MAX_ROOMS {
106 let w = rng.range(MIN_SIZE, MAX_SIZE);
107 let h = rng.range(MIN_SIZE, MAX_SIZE);
108 let x = rng.roll_dice(1, map.width - w - 1) - 1;
109 let y = rng.roll_dice(1, map.height - h - 1) - 1;
110 let new_room = Rect::new(x, y, w, h);
111 let mut ok = true;
112 for other_room in map.rooms.iter() {
113 if new_room.intersect(other_room) { ok = false }
114 }
115 if ok {
116 map.apply_room_to_map(&new_room);
117
118 if !map.rooms.is_empty() {
119 let (new_x, new_y) = new_room.center();
120 let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
121 if rng.range(0,2) == 1 {
122 map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
123 map.apply_vertical_tunnel(prev_y, new_y, new_x);
124 } else {
125 map.apply_vertical_tunnel(prev_y, new_y, prev_x);
126 map.apply_horizontal_tunnel(prev_x, new_x, new_y);
127 }
128 }
129
130 map.rooms.push(new_room);
131 }
132 }
133
134 let stairs_position = map.rooms[map.rooms.len()-1].center();
135 let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1);
136 map.tiles[stairs_idx] = TileType::DownStairs;
137
138 map
139 }
140
141
142
143 }
144 impl Algorithm2D for Map {
145 fn dimensions(&self) -> Point {
146 Point::new(self.width, self.height)
147 }
148 }
149 impl BaseMap for Map {
150 fn is_opaque(&self, idx:usize) -> bool {
151 self.tiles[idx as usize] == TileType::Wall
152 }
153
154 fn get_available_exits(&self, idx:usize) -> rltk::SmallVec<[(usize, f32); 10]> {
155 let mut exits = rltk::SmallVec::new();
156 let x = idx as i32 % self.width;
157 let y = idx as i32 / self.width;
158 let w = self.width as usize;
159
160 // Cardinal directions
161 if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
162 if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
163 if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
164 if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };
165
166 // Diagonals
167 if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
168 if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
169 if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
170 if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }
171
172 exits
173 }
174
175 fn get_pathing_distance(&self, idx1:usize, idx2:usize) -> f32 {
176 let w = self.width as usize;
177 let p1 = Point::new(idx1 % w, idx1 / w);
178 let p2 = Point::new(idx2 % w, idx2 / w);
179 rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
180 }
181 }
182
183 // /// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't
184 // /// look awful.
185 // pub fn new_map_test() -> Vec<TileType> {
186 // let mut map = vec![TileType::Floor; MAPCOUNT];
187
188 // // Make the boundaries walls
189 // for x in 0..80 {
190 // map[xy_idx(x, 0)] = TileType::Wall;
191 // map[xy_idx(x, 49)] = TileType::Wall;
192 // }
193 // for y in 0..50 {
194 // map[xy_idx(0, y)] = TileType::Wall;
195 // map[xy_idx(79, y)] = TileType::Wall;
196 // }
197
198 // // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
199 // // First, obtain the thread-local RNG:
200 // let mut rng = rltk::RandomNumberGenerator::new();
201
202 // for _i in 0..400 {
203 // let x = rng.roll_dice(1, 79);
204 // let y = rng.roll_dice(1, 49);
205 // let idx = xy_idx(x, y);
206 // if idx != xy_idx(40, 25) {
207 // map[idx] = TileType::Wall;
208 // }
209 // }
210
211 // return map
212 // }
213
214 fn is_revealed_and_wall(map: &Map, x: i32, y: i32) -> bool {
215 let idx = map.xy_idx(x, y);
216 map.tiles[idx] == TileType::Wall && map.revealed_tiles[idx]
217 }
218
219 fn wall_glyph(map : &Map, x: i32, y:i32) -> rltk::FontCharType {
220 if x < 1 || x > map.width-2 || y < 1 || y > map.height-2 as i32 { return 35; }
221 let mut mask : u8 = 0;
222
223 if is_revealed_and_wall(map, x, y - 1) { mask +=1; }
224 if is_revealed_and_wall(map, x, y + 1) { mask +=2; }
225 if is_revealed_and_wall(map, x - 1, y) { mask +=4; }
226 if is_revealed_and_wall(map, x + 1, y) { mask +=8; }
227
228 match mask {
229 0 => { 9 } // Pillar because we can't see neighbors
230 1 => { 186 } // Wall only to the north
231 2 => { 186 } // Wall only to the south
232 3 => { 186 } // Wall to the north and south
233 4 => { 205 } // Wall only to the west
234 5 => { 188 } // Wall to the north and west
235 6 => { 187 } // Wall to the south and west
236 7 => { 185 } // Wall to the north, south and west
237 8 => { 205 } // Wall only to the east
238 9 => { 200 } // Wall to the north and east
239 10 => { 201 } // Wall to the south and east
240 11 => { 204 } // Wall to the north, south and east
241 12 => { 205 } // Wall to the east and west
242 13 => { 202 } // Wall to the east, west, and south
243 14 => { 203 } // Wall to the east, west, and north
244 15 => { 206 } // ╬ Wall on all sides
245 _ => { 35 } // We missed one?
246 }
247 }
248
249 pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
250 let map = ecs.fetch::<Map>();
251
252 let mut y = 0;
253 let mut x = 0;
254
255 for (idx,tile) in map.tiles.iter().enumerate() {
256 // Render a tile depending upon the tile type
257 if map.revealed_tiles[idx] {
258 let glyph;
259 let mut fg;
260 let mut bg = RGB::from_f32(0., 0., 0.);
261
262 match tile {
263 TileType::Floor => {
264 glyph = rltk::to_cp437('.');
265 fg = RGB::from_f32(1.0, 0.5, 0.7);
266 }
267 TileType::Wall => {
268 glyph = wall_glyph(&*map, x, y);
269 fg = RGB::from_f32(1.0, 0.6, 0.);
270 }
271 TileType::DownStairs => {
272 glyph = rltk::to_cp437('>');
273 fg = RGB::from_f32(0.0,1.0,1.0);
274 }
275 }
276 // Render bloodstains
277 if map.bloodstains.contains(&idx) {
278 bg = RGB::from_f32(0.75, 0., 0.);
279 }
280
281 // Fog of war
282 if !map.visible_tiles[idx] {
283 fg = fg.to_greyscale();
284 bg = RGB::from_f32(0., 0., 0.); // Don't show blood out of visual range
285 }
286
287
288 ctx.set(x, y, fg, bg, glyph);
289 }
290
291 // Move the coordinates
292 x += 1;
293 if x > 79 {
294 x = 0;
295 y += 1;
296 }
297 }
298 }