map.rs 8.5 KB

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