map.rs 9.7 KB

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