map.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. use rltk::{RandomNumberGenerator, RGB, Rltk, Algorithm2D, Point, BaseMap};
  2. use super::{Rect};
  3. use std::cmp::{max, min};
  4. use specs::prelude::*;
  5. pub const MAPWIDTH : usize = 80;
  6. pub const MAPHEIGHT : usize = 38;
  7. pub const MAPCOUNT : usize = MAPHEIGHT * MAPWIDTH;
  8. #[derive(PartialEq, Copy, Clone)]
  9. pub enum TileType {
  10. Wall,
  11. Floor
  12. }
  13. pub struct Map {
  14. pub tiles: Vec<TileType>,
  15. pub rooms: Vec<Rect>,
  16. pub width: i32,
  17. pub height: i32,
  18. pub revealed_tiles : Vec<bool>,
  19. pub visible_tiles: Vec<bool>,
  20. pub blocked : Vec<bool>,
  21. pub tile_content : Vec<Vec<Entity>>
  22. }
  23. impl Map {
  24. pub fn clear_content_index(&mut self) {
  25. for content in self.tile_content.iter_mut() {
  26. content.clear();
  27. }
  28. }
  29. pub fn populate_blocked(&mut self) {
  30. for (i,tile) in self.tiles.iter_mut().enumerate() {
  31. self.blocked[i] = *tile == TileType::Wall;
  32. }
  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 = self.xy_idx(x, y);
  37. !self.blocked[idx]
  38. }
  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. pub fn new_map_rooms_and_corridors() -> Map {
  67. let mut map = Map{
  68. tiles : vec![TileType::Wall; MAPCOUNT],
  69. rooms : Vec::new(),
  70. width : MAPWIDTH as i32,
  71. height: MAPHEIGHT as i32,
  72. revealed_tiles : vec![false; MAPCOUNT],
  73. visible_tiles : vec![false; MAPCOUNT],
  74. blocked : vec![false; MAPCOUNT],
  75. tile_content : vec![Vec::new(); MAPCOUNT]
  76. };
  77. const MAX_ROOMS : i32 = 30;
  78. const MIN_SIZE : i32 = 6;
  79. const MAX_SIZE : i32 = 10;
  80. let mut rng = RandomNumberGenerator::new();
  81. for _ in 0..MAX_ROOMS {
  82. let w = rng.range(MIN_SIZE, MAX_SIZE);
  83. let h = rng.range(MIN_SIZE, MAX_SIZE);
  84. let x = rng.roll_dice(1, map.width - w - 1) - 1;
  85. let y = rng.roll_dice(1, map.height - h - 1) - 1;
  86. let new_room = Rect::new(x, y, w, h);
  87. let mut ok = true;
  88. for other_room in map.rooms.iter() {
  89. if new_room.intersect(other_room) { ok = false }
  90. }
  91. if ok {
  92. map.apply_room_to_map(&new_room);
  93. if !map.rooms.is_empty() {
  94. let (new_x, new_y) = new_room.center();
  95. let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
  96. if rng.range(0,2) == 1 {
  97. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  98. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  99. } else {
  100. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  101. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  102. }
  103. }
  104. map.rooms.push(new_room);
  105. }
  106. }
  107. map
  108. }
  109. }
  110. impl Algorithm2D for Map {
  111. fn dimensions(&self) -> Point {
  112. Point::new(self.width, self.height)
  113. }
  114. }
  115. impl BaseMap for Map {
  116. fn is_opaque(&self, idx:usize) -> bool {
  117. self.tiles[idx as usize] == TileType::Wall
  118. }
  119. fn get_available_exits(&self, idx:usize) -> rltk::SmallVec<[(usize, f32); 10]> {
  120. let mut exits = rltk::SmallVec::new();
  121. let x = idx as i32 % self.width;
  122. let y = idx as i32 / self.width;
  123. let w = self.width as usize;
  124. // Cardinal directions
  125. if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
  126. if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
  127. if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
  128. if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };
  129. // Diagonals
  130. if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
  131. if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
  132. if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
  133. if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }
  134. exits
  135. }
  136. fn get_pathing_distance(&self, idx1:usize, idx2:usize) -> f32 {
  137. let w = self.width as usize;
  138. let p1 = Point::new(idx1 % w, idx1 / w);
  139. let p2 = Point::new(idx2 % w, idx2 / w);
  140. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  141. }
  142. }
  143. // /// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't
  144. // /// look awful.
  145. // pub fn new_map_test() -> Vec<TileType> {
  146. // let mut map = vec![TileType::Floor; MAPCOUNT];
  147. // // Make the boundaries walls
  148. // for x in 0..80 {
  149. // map[xy_idx(x, 0)] = TileType::Wall;
  150. // map[xy_idx(x, 49)] = TileType::Wall;
  151. // }
  152. // for y in 0..50 {
  153. // map[xy_idx(0, y)] = TileType::Wall;
  154. // map[xy_idx(79, y)] = TileType::Wall;
  155. // }
  156. // // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
  157. // // First, obtain the thread-local RNG:
  158. // let mut rng = rltk::RandomNumberGenerator::new();
  159. // for _i in 0..400 {
  160. // let x = rng.roll_dice(1, 79);
  161. // let y = rng.roll_dice(1, 49);
  162. // let idx = xy_idx(x, y);
  163. // if idx != xy_idx(40, 25) {
  164. // map[idx] = TileType::Wall;
  165. // }
  166. // }
  167. // return map
  168. // }
  169. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  170. let map = ecs.fetch::<Map>();
  171. let mut y = 0;
  172. let mut x = 0;
  173. for (idx,tile) in map.tiles.iter().enumerate() {
  174. // Render a tile depending upon the tile type
  175. if map.revealed_tiles[idx] {
  176. let glyph;
  177. let mut fg;
  178. match tile {
  179. TileType::Floor => {
  180. glyph = rltk::to_cp437('.');
  181. fg = RGB::from_f32(1.0, 0.5, 0.7);
  182. }
  183. TileType::Wall => {
  184. glyph = rltk::to_cp437('#');
  185. fg = RGB::from_f32(1.0, 0.6, 0.);
  186. }
  187. }
  188. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  189. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  190. }
  191. // Move the coordinates
  192. x += 1;
  193. if x > 79 {
  194. x = 0;
  195. y += 1;
  196. }
  197. }
  198. }