map.rs 6.1 KB

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