map.rs 6.9 KB

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