map.rs 7.4 KB

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