map.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. extern crate rltk;
  2. use rltk::{ RGB, Rltk, Console, BaseMap, Algorithm2D, Point };
  3. use super::{Rect};
  4. extern crate specs;
  5. use specs::prelude::*;
  6. use serde::{Serialize, Deserialize};
  7. use std::collections::HashSet;
  8. pub const MAPWIDTH : usize = 80;
  9. pub const MAPHEIGHT : usize = 43;
  10. pub const MAPCOUNT : usize = MAPHEIGHT * MAPWIDTH;
  11. #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
  12. pub enum TileType {
  13. Wall, Floor, DownStairs
  14. }
  15. #[derive(Default, Serialize, Deserialize, Clone)]
  16. pub struct Map {
  17. pub tiles : Vec<TileType>,
  18. pub rooms : Vec<Rect>,
  19. pub width : i32,
  20. pub height : i32,
  21. pub revealed_tiles : Vec<bool>,
  22. pub visible_tiles : Vec<bool>,
  23. pub blocked : Vec<bool>,
  24. pub depth : i32,
  25. pub bloodstains : HashSet<usize>,
  26. #[serde(skip_serializing)]
  27. #[serde(skip_deserializing)]
  28. pub tile_content : Vec<Vec<Entity>>
  29. }
  30. impl Map {
  31. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  32. (y as usize * self.width as usize) + x as usize
  33. }
  34. fn is_exit_valid(&self, x:i32, y:i32) -> bool {
  35. if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
  36. let idx = (y * self.width) + x;
  37. !self.blocked[idx as usize]
  38. }
  39. pub fn populate_blocked(&mut self) {
  40. for (i,tile) in self.tiles.iter_mut().enumerate() {
  41. self.blocked[i] = *tile == TileType::Wall;
  42. }
  43. }
  44. pub fn clear_content_index(&mut self) {
  45. for content in self.tile_content.iter_mut() {
  46. content.clear();
  47. }
  48. }
  49. /// Generates an empty map, consisting entirely of solid walls
  50. pub fn new(new_depth : i32) -> Map {
  51. Map{
  52. tiles : vec![TileType::Wall; MAPCOUNT],
  53. rooms : Vec::new(),
  54. width : MAPWIDTH as i32,
  55. height: MAPHEIGHT as i32,
  56. revealed_tiles : vec![false; MAPCOUNT],
  57. visible_tiles : vec![false; MAPCOUNT],
  58. blocked : vec![false; MAPCOUNT],
  59. tile_content : vec![Vec::new(); MAPCOUNT],
  60. depth: new_depth,
  61. bloodstains: HashSet::new()
  62. }
  63. }
  64. }
  65. impl BaseMap for Map {
  66. fn is_opaque(&self, idx:i32) -> bool {
  67. self.tiles[idx as usize] == TileType::Wall
  68. }
  69. fn get_available_exits(&self, idx:i32) -> Vec<(i32, f32)> {
  70. let mut exits : Vec<(i32, f32)> = Vec::new();
  71. let x = idx % self.width;
  72. let y = idx / self.width;
  73. // Cardinal directions
  74. if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
  75. if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
  76. if self.is_exit_valid(x, y-1) { exits.push((idx-self.width, 1.0)) };
  77. if self.is_exit_valid(x, y+1) { exits.push((idx+self.width, 1.0)) };
  78. // Diagonals
  79. if self.is_exit_valid(x-1, y-1) { exits.push(((idx-self.width)-1, 1.45)); }
  80. if self.is_exit_valid(x+1, y-1) { exits.push(((idx-self.width)+1, 1.45)); }
  81. if self.is_exit_valid(x-1, y+1) { exits.push(((idx+self.width)-1, 1.45)); }
  82. if self.is_exit_valid(x+1, y+1) { exits.push(((idx+self.width)+1, 1.45)); }
  83. exits
  84. }
  85. fn get_pathing_distance(&self, idx1:i32, idx2:i32) -> f32 {
  86. let p1 = Point::new(idx1 % self.width, idx1 / self.width);
  87. let p2 = Point::new(idx2 % self.width, idx2 / self.width);
  88. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  89. }
  90. }
  91. impl Algorithm2D for Map {
  92. fn point2d_to_index(&self, pt: Point) -> i32 {
  93. (pt.y * self.width) + pt.x
  94. }
  95. fn index_to_point2d(&self, idx:i32) -> Point {
  96. Point{ x: idx % self.width, y: idx / self.width }
  97. }
  98. }
  99. fn is_revealed_and_wall(map: &Map, x: i32, y: i32) -> bool {
  100. let idx = map.xy_idx(x, y);
  101. map.tiles[idx] == TileType::Wall && map.revealed_tiles[idx]
  102. }
  103. fn wall_glyph(map : &Map, x: i32, y:i32) -> u8 {
  104. if x < 1 || x > map.width-2 || y < 1 || y > map.height-2 as i32 { return 35; }
  105. let mut mask : u8 = 0;
  106. if is_revealed_and_wall(map, x, y - 1) { mask +=1; }
  107. if is_revealed_and_wall(map, x, y + 1) { mask +=2; }
  108. if is_revealed_and_wall(map, x - 1, y) { mask +=4; }
  109. if is_revealed_and_wall(map, x + 1, y) { mask +=8; }
  110. match mask {
  111. 0 => { 9 } // Pillar because we can't see neighbors
  112. 1 => { 186 } // Wall only to the north
  113. 2 => { 186 } // Wall only to the south
  114. 3 => { 186 } // Wall to the north and south
  115. 4 => { 205 } // Wall only to the west
  116. 5 => { 188 } // Wall to the north and west
  117. 6 => { 187 } // Wall to the south and west
  118. 7 => { 185 } // Wall to the north, south and west
  119. 8 => { 205 } // Wall only to the east
  120. 9 => { 200 } // Wall to the north and east
  121. 10 => { 201 } // Wall to the south and east
  122. 11 => { 204 } // Wall to the north, south and east
  123. 12 => { 205 } // Wall to the east and west
  124. 13 => { 202 } // Wall to the east, west, and south
  125. 14 => { 203 } // Wall to the east, west, and north
  126. _ => { 35 } // We missed one?
  127. }
  128. }
  129. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  130. let map = ecs.fetch::<Map>();
  131. let mut y = 0;
  132. let mut x = 0;
  133. for (idx,tile) in map.tiles.iter().enumerate() {
  134. // Render a tile depending upon the tile type
  135. if map.revealed_tiles[idx] {
  136. let glyph;
  137. let mut fg;
  138. let mut bg = RGB::from_f32(0., 0., 0.);
  139. match tile {
  140. TileType::Floor => {
  141. glyph = rltk::to_cp437('.');
  142. fg = RGB::from_f32(0.0, 0.5, 0.5);
  143. }
  144. TileType::Wall => {
  145. glyph = wall_glyph(&*map, x, y);
  146. fg = RGB::from_f32(0., 1.0, 0.);
  147. }
  148. TileType::DownStairs => {
  149. glyph = rltk::to_cp437('>');
  150. fg = RGB::from_f32(0., 1.0, 1.0);
  151. }
  152. }
  153. if map.bloodstains.contains(&idx) { bg = RGB::from_f32(0.75, 0., 0.); }
  154. if !map.visible_tiles[idx] {
  155. fg = fg.to_greyscale();
  156. bg = RGB::from_f32(0., 0., 0.); // Don't show stains out of visual range
  157. }
  158. ctx.set(x, y, fg, bg, glyph);
  159. }
  160. // Move the coordinates
  161. x += 1;
  162. if x > MAPWIDTH as i32-1 {
  163. x = 0;
  164. y += 1;
  165. }
  166. }
  167. }