map.rs 8.9 KB

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