map.rs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. pub const MAPWIDTH : usize = 80;
  8. pub const MAPHEIGHT : usize = 43;
  9. pub const MAPCOUNT : usize = MAPHEIGHT * MAPWIDTH;
  10. #[derive(PartialEq, Copy, Clone)]
  11. pub enum TileType {
  12. Wall, Floor
  13. }
  14. #[derive(Default)]
  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. pub tile_content : Vec<Vec<Entity>>
  24. }
  25. impl Map {
  26. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  27. (y as usize * self.width as usize) + x as usize
  28. }
  29. fn apply_room_to_map(&mut self, room : &Rect) {
  30. for y in room.y1 +1 ..= room.y2 {
  31. for x in room.x1 + 1 ..= room.x2 {
  32. let idx = self.xy_idx(x, y);
  33. self.tiles[idx] = TileType::Floor;
  34. }
  35. }
  36. }
  37. fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
  38. for x in min(x1,x2) ..= max(x1,x2) {
  39. let idx = self.xy_idx(x, y);
  40. if idx > 0 && idx < self.width as usize * self.height as usize {
  41. self.tiles[idx as usize] = TileType::Floor;
  42. }
  43. }
  44. }
  45. fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
  46. for y in min(y1,y2) ..= max(y1,y2) {
  47. let idx = self.xy_idx(x, y);
  48. if idx > 0 && idx < self.width as usize * self.height as usize {
  49. self.tiles[idx as usize] = TileType::Floor;
  50. }
  51. }
  52. }
  53. fn is_exit_valid(&self, x:i32, y:i32) -> bool {
  54. if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
  55. let idx = (y * self.width) + x;
  56. !self.blocked[idx as usize]
  57. }
  58. pub fn populate_blocked(&mut self) {
  59. for (i,tile) in self.tiles.iter_mut().enumerate() {
  60. self.blocked[i] = *tile == TileType::Wall;
  61. }
  62. }
  63. pub fn clear_content_index(&mut self) {
  64. for content in self.tile_content.iter_mut() {
  65. content.clear();
  66. }
  67. }
  68. /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
  69. /// This gives a handful of random rooms and corridors joining them together.
  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 _i 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,1) == 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 BaseMap for Map {
  115. fn is_opaque(&self, idx:i32) -> bool {
  116. self.tiles[idx as usize] == TileType::Wall
  117. }
  118. fn get_available_exits(&self, idx:i32) -> Vec<(i32, f32)> {
  119. let mut exits : Vec<(i32, f32)> = Vec::new();
  120. let x = idx % self.width;
  121. let y = idx / self.width;
  122. // Cardinal directions
  123. if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
  124. if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
  125. if self.is_exit_valid(x, y-1) { exits.push((idx-self.width, 1.0)) };
  126. if self.is_exit_valid(x, y+1) { exits.push((idx+self.width, 1.0)) };
  127. // Diagonals
  128. if self.is_exit_valid(x-1, y-1) { exits.push(((idx-self.width)-1, 1.45)); }
  129. if self.is_exit_valid(x+1, y-1) { exits.push(((idx-self.width)+1, 1.45)); }
  130. if self.is_exit_valid(x-1, y+1) { exits.push(((idx+self.width)-1, 1.45)); }
  131. if self.is_exit_valid(x+1, y+1) { exits.push(((idx+self.width)+1, 1.45)); }
  132. exits
  133. }
  134. fn get_pathing_distance(&self, idx1:i32, idx2:i32) -> f32 {
  135. let p1 = Point::new(idx1 % self.width, idx1 / self.width);
  136. let p2 = Point::new(idx2 % self.width, idx2 / self.width);
  137. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  138. }
  139. }
  140. impl Algorithm2D for Map {
  141. fn point2d_to_index(&self, pt: Point) -> i32 {
  142. (pt.y * self.width) + pt.x
  143. }
  144. fn index_to_point2d(&self, idx:i32) -> Point {
  145. Point{ x: idx % self.width, y: idx / self.width }
  146. }
  147. }
  148. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  149. let map = ecs.fetch::<Map>();
  150. let mut y = 0;
  151. let mut x = 0;
  152. for (idx,tile) in map.tiles.iter().enumerate() {
  153. // Render a tile depending upon the tile type
  154. if map.revealed_tiles[idx] {
  155. let glyph;
  156. let mut fg;
  157. match tile {
  158. TileType::Floor => {
  159. glyph = rltk::to_cp437('.');
  160. fg = RGB::from_f32(0.0, 0.5, 0.5);
  161. }
  162. TileType::Wall => {
  163. glyph = rltk::to_cp437('#');
  164. fg = RGB::from_f32(0., 1.0, 0.);
  165. }
  166. }
  167. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  168. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  169. }
  170. // Move the coordinates
  171. x += 1;
  172. if x > MAPWIDTH as i32-1 {
  173. x = 0;
  174. y += 1;
  175. }
  176. }
  177. }