map.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. use crate::rect::Rect;
  2. use std::cmp::{max, min};
  3. use rltk::{Algorithm2D, BaseMap, Point, RandomNumberGenerator};
  4. use specs::prelude::*;
  5. pub const MAPWIDTH: usize = 80;
  6. pub const MAPHEIGHT: usize = 43;
  7. pub const MAPCOUNT: usize = MAPHEIGHT * MAPWIDTH;
  8. #[derive(PartialEq, Copy, Clone)]
  9. pub enum TileType {
  10. Wall,
  11. Floor,
  12. }
  13. impl TileType {
  14. pub fn glyph(&self) -> u8 {
  15. match self {
  16. TileType::Wall => rltk::to_cp437('#'),
  17. TileType::Floor => rltk::to_cp437('.'),
  18. }
  19. }
  20. pub fn color(&self) -> rltk::RGB {
  21. match self {
  22. TileType::Wall => rltk::RGB::from_f32(0., 1.0, 0.),
  23. TileType::Floor => rltk::RGB::from_f32(0.0, 0.5, 0.5),
  24. }
  25. }
  26. }
  27. #[derive(Default)]
  28. pub struct Map {
  29. pub tiles: Vec<TileType>,
  30. pub rooms: Vec<Rect>,
  31. pub width: i32,
  32. pub height: i32,
  33. pub revealed_tiles: Vec<bool>,
  34. pub visible_tiles: Vec<bool>,
  35. pub blocked: Vec<bool>,
  36. pub tile_content: Vec<Vec<Entity>>,
  37. }
  38. impl Map {
  39. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  40. (y as usize * self.width as usize) + x as usize
  41. }
  42. fn apply_room_to_map(&mut self, room: &Rect) {
  43. for y in room.y1 + 1..=room.y2 {
  44. for x in room.x1 + 1..=room.x2 {
  45. let idx = self.xy_idx(x, y);
  46. self.tiles[idx] = TileType::Floor;
  47. }
  48. }
  49. }
  50. fn apply_horizontal_tunnel(&mut self, x1: i32, x2: i32, y: i32) {
  51. for x in min(x1, x2)..=max(x1, x2) {
  52. let idx = self.xy_idx(x, y);
  53. if idx > 0 && idx < self.width as usize * self.height as usize {
  54. self.tiles[idx as usize] = TileType::Floor;
  55. }
  56. }
  57. }
  58. fn apply_vertical_tunnel(&mut self, y1: i32, y2: i32, x: i32) {
  59. for y in min(y1, y2)..=max(y1, y2) {
  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 is_exit_valid(&self, x: i32, y: i32) -> bool {
  67. if x < 1 || x > self.width - 1 || y < 1 || y > self.height - 1 {
  68. return false;
  69. }
  70. let idx = (y * self.width) + x;
  71. !self.blocked[idx as usize]
  72. }
  73. pub fn populate_blocked(&mut self) {
  74. for (i, tile) in self.tiles.iter_mut().enumerate() {
  75. self.blocked[i] = *tile == TileType::Wall;
  76. }
  77. }
  78. pub fn clear_content_index(&mut self) {
  79. for content in self.tile_content.iter_mut() {
  80. content.clear();
  81. }
  82. }
  83. /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
  84. /// This gives a handful of random rooms and corridors joining them together.
  85. pub fn new_map_rooms_and_corridors() -> 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. tile_content: vec![Vec::new(); MAPCOUNT],
  95. };
  96. const MAX_ROOMS: i32 = 30;
  97. const MIN_SIZE: i32 = 6;
  98. const MAX_SIZE: i32 = 10;
  99. let mut rng = RandomNumberGenerator::new();
  100. for _i in 0..MAX_ROOMS {
  101. let w = rng.range(MIN_SIZE, MAX_SIZE);
  102. let h = rng.range(MIN_SIZE, MAX_SIZE);
  103. let x = rng.roll_dice(1, map.width - w - 1) - 1;
  104. let y = rng.roll_dice(1, map.height - h - 1) - 1;
  105. let new_room = Rect::new(x, y, w, h);
  106. let mut ok = true;
  107. for other_room in map.rooms.iter() {
  108. if new_room.intersect(other_room) {
  109. ok = false
  110. }
  111. }
  112. if ok {
  113. map.apply_room_to_map(&new_room);
  114. if !map.rooms.is_empty() {
  115. let (new_x, new_y) = new_room.center();
  116. let (prev_x, prev_y) = map.rooms[map.rooms.len() - 1].center();
  117. if rng.range(0, 1) == 1 {
  118. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  119. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  120. } else {
  121. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  122. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  123. }
  124. }
  125. map.rooms.push(new_room);
  126. }
  127. }
  128. map
  129. }
  130. pub fn get_attackable<'a>(
  131. &self,
  132. target: usize,
  133. stats: &ReadStorage<'a, crate::components::CombatStats>,
  134. ) -> Option<Entity> {
  135. for potential in self.tile_content[target].iter() {
  136. if stats.contains(*potential) {
  137. return Some(*potential);
  138. }
  139. }
  140. None
  141. }
  142. }
  143. impl BaseMap for Map {
  144. fn is_opaque(&self, idx: i32) -> bool {
  145. self.tiles[idx as usize] == TileType::Wall
  146. }
  147. fn get_available_exits(&self, idx: i32) -> Vec<(i32, f32)> {
  148. let mut exits: Vec<(i32, f32)> = Vec::new();
  149. let x = idx % self.width;
  150. let y = idx / self.width;
  151. // Cardinal directions
  152. if self.is_exit_valid(x - 1, y) {
  153. exits.push((idx - 1, 1.0))
  154. };
  155. if self.is_exit_valid(x + 1, y) {
  156. exits.push((idx + 1, 1.0))
  157. };
  158. if self.is_exit_valid(x, y - 1) {
  159. exits.push((idx - self.width, 1.0))
  160. };
  161. if self.is_exit_valid(x, y + 1) {
  162. exits.push((idx + self.width, 1.0))
  163. };
  164. // Diagonals
  165. if self.is_exit_valid(x - 1, y - 1) {
  166. exits.push(((idx - self.width) - 1, 1.45));
  167. }
  168. if self.is_exit_valid(x + 1, y - 1) {
  169. exits.push(((idx - self.width) + 1, 1.45));
  170. }
  171. if self.is_exit_valid(x - 1, y + 1) {
  172. exits.push(((idx + self.width) - 1, 1.45));
  173. }
  174. if self.is_exit_valid(x + 1, y + 1) {
  175. exits.push(((idx + self.width) + 1, 1.45));
  176. }
  177. exits
  178. }
  179. fn get_pathing_distance(&self, idx1: i32, idx2: i32) -> f32 {
  180. let p1 = Point::new(idx1 % self.width, idx1 / self.width);
  181. let p2 = Point::new(idx2 % self.width, idx2 / self.width);
  182. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  183. }
  184. }
  185. impl Algorithm2D for Map {
  186. fn point2d_to_index(&self, pt: Point) -> i32 {
  187. (pt.y * self.width) + pt.x
  188. }
  189. fn index_to_point2d(&self, idx: i32) -> Point {
  190. Point {
  191. x: idx % self.width,
  192. y: idx / self.width,
  193. }
  194. }
  195. }