map.rs 6.2 KB

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