map.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  157. let map = ecs.fetch::<Map>();
  158. let mut y = 0;
  159. let mut x = 0;
  160. for (idx,tile) in map.tiles.iter().enumerate() {
  161. // Render a tile depending upon the tile type
  162. if map.revealed_tiles[idx] {
  163. let glyph;
  164. let mut fg;
  165. match tile {
  166. TileType::Floor => {
  167. glyph = rltk::to_cp437('.');
  168. fg = RGB::from_f32(0.0, 0.5, 0.5);
  169. }
  170. TileType::Wall => {
  171. glyph = rltk::to_cp437('#');
  172. fg = RGB::from_f32(0., 1.0, 0.);
  173. }
  174. TileType::DownStairs => {
  175. glyph = rltk::to_cp437('>');
  176. fg = RGB::from_f32(0., 1.0, 1.0);
  177. }
  178. }
  179. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  180. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  181. }
  182. // Move the coordinates
  183. x += 1;
  184. if x > MAPWIDTH as i32-1 {
  185. x = 0;
  186. y += 1;
  187. }
  188. }
  189. }