map.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. use rltk::{RandomNumberGenerator, RGB, Rltk, Algorithm2D, Point, BaseMap};
  2. use super::{Rect};
  3. use std::cmp::{max, min};
  4. use specs::prelude::*;
  5. use serde::{Serialize, Deserialize};
  6. pub const MAPWIDTH : usize = 80;
  7. pub const MAPHEIGHT : usize = 38;
  8. pub const MAPCOUNT : usize = MAPHEIGHT * MAPWIDTH;
  9. #[derive(PartialEq, Copy, Clone, Serialize, Deserialize)]
  10. pub enum TileType {
  11. Wall,
  12. Floor,
  13. DownStairs,
  14. }
  15. #[derive(Default, Serialize, Deserialize, Clone)]
  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 depth: i32,
  25. #[serde(skip_serializing)]
  26. #[serde(skip_deserializing)]
  27. pub tile_content : Vec<Vec<Entity>>
  28. }
  29. impl Map {
  30. pub fn clear_content_index(&mut self) {
  31. for content in self.tile_content.iter_mut() {
  32. content.clear();
  33. }
  34. }
  35. pub fn populate_blocked(&mut self) {
  36. for (i,tile) in self.tiles.iter_mut().enumerate() {
  37. self.blocked[i] = *tile == TileType::Wall;
  38. }
  39. }
  40. fn is_exit_valid(&self, x:i32, y:i32) -> bool {
  41. if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
  42. let idx = self.xy_idx(x, y);
  43. !self.blocked[idx]
  44. }
  45. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  46. (y as usize * self.width as usize) + x as usize
  47. }
  48. fn apply_room_to_map(&mut self, room: &Rect) {
  49. for y in room.y1 +1 ..= room.y2 {
  50. for x in room.x1 + 1 ..= room.x2 {
  51. let idx = self.xy_idx(x,y);
  52. self.tiles[idx] = TileType::Floor;
  53. }
  54. }
  55. }
  56. fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
  57. for x in min(x1,x2) ..= max(x1,x2) {
  58. let idx = self.xy_idx(x, y);
  59. if idx > 0 && idx < self.width as usize * self.height as usize {
  60. self.tiles[idx as usize] = TileType::Floor;
  61. }
  62. }
  63. }
  64. fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
  65. for y in min(y1,y2) ..= max(y1,y2) {
  66. let idx = self.xy_idx(x, y);
  67. if idx > 0 && idx < self.width as usize * self.height as usize {
  68. self.tiles[idx as usize] = TileType::Floor;
  69. }
  70. }
  71. }
  72. pub fn new_map_rooms_and_corridors(new_depth: i32) -> Map {
  73. let mut map = Map{
  74. tiles : vec![TileType::Wall; MAPCOUNT],
  75. rooms : Vec::new(),
  76. width : MAPWIDTH as i32,
  77. height: MAPHEIGHT as i32,
  78. revealed_tiles : vec![false; MAPCOUNT],
  79. visible_tiles : vec![false; MAPCOUNT],
  80. blocked : vec![false; MAPCOUNT],
  81. depth: new_depth,
  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 _ 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) { ok = false }
  97. }
  98. if ok {
  99. map.apply_room_to_map(&new_room);
  100. if !map.rooms.is_empty() {
  101. let (new_x, new_y) = new_room.center();
  102. let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
  103. if rng.range(0,2) == 1 {
  104. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  105. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  106. } else {
  107. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  108. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  109. }
  110. }
  111. map.rooms.push(new_room);
  112. }
  113. }
  114. let stairs_position = map.rooms[map.rooms.len()-1].center();
  115. let stairs_idx = map.xy_idx(stairs_position.0, stairs_position.1);
  116. map.tiles[stairs_idx] = TileType::DownStairs;
  117. map
  118. }
  119. }
  120. impl Algorithm2D for Map {
  121. fn dimensions(&self) -> Point {
  122. Point::new(self.width, self.height)
  123. }
  124. }
  125. impl BaseMap for Map {
  126. fn is_opaque(&self, idx:usize) -> bool {
  127. self.tiles[idx as usize] == TileType::Wall
  128. }
  129. fn get_available_exits(&self, idx:usize) -> rltk::SmallVec<[(usize, f32); 10]> {
  130. let mut exits = rltk::SmallVec::new();
  131. let x = idx as i32 % self.width;
  132. let y = idx as i32 / self.width;
  133. let w = self.width as usize;
  134. // Cardinal directions
  135. if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
  136. if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
  137. if self.is_exit_valid(x, y-1) { exits.push((idx-w, 1.0)) };
  138. if self.is_exit_valid(x, y+1) { exits.push((idx+w, 1.0)) };
  139. // Diagonals
  140. if self.is_exit_valid(x-1, y-1) { exits.push(((idx-w)-1, 1.45)); }
  141. if self.is_exit_valid(x+1, y-1) { exits.push(((idx-w)+1, 1.45)); }
  142. if self.is_exit_valid(x-1, y+1) { exits.push(((idx+w)-1, 1.45)); }
  143. if self.is_exit_valid(x+1, y+1) { exits.push(((idx+w)+1, 1.45)); }
  144. exits
  145. }
  146. fn get_pathing_distance(&self, idx1:usize, idx2:usize) -> f32 {
  147. let w = self.width as usize;
  148. let p1 = Point::new(idx1 % w, idx1 / w);
  149. let p2 = Point::new(idx2 % w, idx2 / w);
  150. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  151. }
  152. }
  153. // /// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't
  154. // /// look awful.
  155. // pub fn new_map_test() -> Vec<TileType> {
  156. // let mut map = vec![TileType::Floor; MAPCOUNT];
  157. // // Make the boundaries walls
  158. // for x in 0..80 {
  159. // map[xy_idx(x, 0)] = TileType::Wall;
  160. // map[xy_idx(x, 49)] = TileType::Wall;
  161. // }
  162. // for y in 0..50 {
  163. // map[xy_idx(0, y)] = TileType::Wall;
  164. // map[xy_idx(79, y)] = TileType::Wall;
  165. // }
  166. // // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
  167. // // First, obtain the thread-local RNG:
  168. // let mut rng = rltk::RandomNumberGenerator::new();
  169. // for _i in 0..400 {
  170. // let x = rng.roll_dice(1, 79);
  171. // let y = rng.roll_dice(1, 49);
  172. // let idx = xy_idx(x, y);
  173. // if idx != xy_idx(40, 25) {
  174. // map[idx] = TileType::Wall;
  175. // }
  176. // }
  177. // return map
  178. // }
  179. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  180. let map = ecs.fetch::<Map>();
  181. let mut y = 0;
  182. let mut x = 0;
  183. for (idx,tile) in map.tiles.iter().enumerate() {
  184. // Render a tile depending upon the tile type
  185. if map.revealed_tiles[idx] {
  186. let glyph;
  187. let mut fg;
  188. match tile {
  189. TileType::Floor => {
  190. glyph = rltk::to_cp437('.');
  191. fg = RGB::from_f32(1.0, 0.5, 0.7);
  192. }
  193. TileType::Wall => {
  194. glyph = rltk::to_cp437('#');
  195. fg = RGB::from_f32(1.0, 0.6, 0.);
  196. }
  197. TileType::DownStairs => {
  198. glyph = rltk::to_cp437('>');
  199. fg = RGB::from_f32(0.0,1.0,1.0);
  200. }
  201. }
  202. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  203. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  204. }
  205. // Move the coordinates
  206. x += 1;
  207. if x > 79 {
  208. x = 0;
  209. y += 1;
  210. }
  211. }
  212. }