map.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #[derive(PartialEq, Copy, Clone)]
  8. pub enum TileType {
  9. Wall, Floor
  10. }
  11. #[derive(Default)]
  12. pub struct Map {
  13. pub tiles : Vec<TileType>,
  14. pub rooms : Vec<Rect>,
  15. pub width : i32,
  16. pub height : i32,
  17. pub revealed_tiles : Vec<bool>,
  18. pub visible_tiles : Vec<bool>,
  19. pub blocked : Vec<bool>,
  20. pub tile_content : Vec<Vec<Entity>>
  21. }
  22. impl Map {
  23. pub fn xy_idx(&self, x: i32, y: i32) -> usize {
  24. (y as usize * self.width as usize) + x as usize
  25. }
  26. fn apply_room_to_map(&mut self, room : &Rect) {
  27. for y in room.y1 +1 ..= room.y2 {
  28. for x in room.x1 + 1 ..= room.x2 {
  29. let idx = self.xy_idx(x, y);
  30. self.tiles[idx] = TileType::Floor;
  31. }
  32. }
  33. }
  34. fn apply_horizontal_tunnel(&mut self, x1:i32, x2:i32, y:i32) {
  35. for x in min(x1,x2) ..= max(x1,x2) {
  36. let idx = self.xy_idx(x, y);
  37. if idx > 0 && idx < self.width as usize * self.height as usize {
  38. self.tiles[idx as usize] = TileType::Floor;
  39. }
  40. }
  41. }
  42. fn apply_vertical_tunnel(&mut self, y1:i32, y2:i32, x:i32) {
  43. for y in min(y1,y2) ..= max(y1,y2) {
  44. let idx = self.xy_idx(x, y);
  45. if idx > 0 && idx < self.width as usize * self.height as usize {
  46. self.tiles[idx as usize] = TileType::Floor;
  47. }
  48. }
  49. }
  50. fn is_exit_valid(&self, x:i32, y:i32) -> bool {
  51. if x < 1 || x > self.width-1 || y < 1 || y > self.height-1 { return false; }
  52. let idx = (y * self.width) + x;
  53. !self.blocked[idx as usize]
  54. }
  55. pub fn populate_blocked(&mut self) {
  56. for (i,tile) in self.tiles.iter_mut().enumerate() {
  57. self.blocked[i] = *tile == TileType::Wall;
  58. }
  59. }
  60. pub fn clear_content_index(&mut self) {
  61. for content in self.tile_content.iter_mut() {
  62. content.clear();
  63. }
  64. }
  65. /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
  66. /// This gives a handful of random rooms and corridors joining them together.
  67. pub fn new_map_rooms_and_corridors() -> Map {
  68. let mut map = Map{
  69. tiles : vec![TileType::Wall; 80*50],
  70. rooms : Vec::new(),
  71. width : 80,
  72. height: 50,
  73. revealed_tiles : vec![false; 80*50],
  74. visible_tiles : vec![false; 80*50],
  75. blocked : vec![false; 80*50],
  76. tile_content : vec![Vec::new(); 80*50]
  77. };
  78. const MAX_ROOMS : i32 = 30;
  79. const MIN_SIZE : i32 = 6;
  80. const MAX_SIZE : i32 = 10;
  81. let mut rng = RandomNumberGenerator::new();
  82. for _i in 0..MAX_ROOMS {
  83. let w = rng.range(MIN_SIZE, MAX_SIZE);
  84. let h = rng.range(MIN_SIZE, MAX_SIZE);
  85. let x = rng.roll_dice(1, map.width - w - 1) - 1;
  86. let y = rng.roll_dice(1, map.height - h - 1) - 1;
  87. let new_room = Rect::new(x, y, w, h);
  88. let mut ok = true;
  89. for other_room in map.rooms.iter() {
  90. if new_room.intersect(other_room) { ok = false }
  91. }
  92. if ok {
  93. map.apply_room_to_map(&new_room);
  94. if !map.rooms.is_empty() {
  95. let (new_x, new_y) = new_room.center();
  96. let (prev_x, prev_y) = map.rooms[map.rooms.len()-1].center();
  97. if rng.range(0,1) == 1 {
  98. map.apply_horizontal_tunnel(prev_x, new_x, prev_y);
  99. map.apply_vertical_tunnel(prev_y, new_y, new_x);
  100. } else {
  101. map.apply_vertical_tunnel(prev_y, new_y, prev_x);
  102. map.apply_horizontal_tunnel(prev_x, new_x, new_y);
  103. }
  104. }
  105. map.rooms.push(new_room);
  106. }
  107. }
  108. map
  109. }
  110. }
  111. impl BaseMap for Map {
  112. fn is_opaque(&self, idx:i32) -> bool {
  113. self.tiles[idx as usize] == TileType::Wall
  114. }
  115. fn get_available_exits(&self, idx:i32) -> Vec<(i32, f32)> {
  116. let mut exits : Vec<(i32, f32)> = Vec::new();
  117. let x = idx % self.width;
  118. let y = idx / self.width;
  119. // Cardinal directions
  120. if self.is_exit_valid(x-1, y) { exits.push((idx-1, 1.0)) };
  121. if self.is_exit_valid(x+1, y) { exits.push((idx+1, 1.0)) };
  122. if self.is_exit_valid(x, y-1) { exits.push((idx-self.width, 1.0)) };
  123. if self.is_exit_valid(x, y+1) { exits.push((idx+self.width, 1.0)) };
  124. // Diagonals
  125. if self.is_exit_valid(x-1, y-1) { exits.push(((idx-self.width)-1, 1.45)); }
  126. if self.is_exit_valid(x+1, y-1) { exits.push(((idx-self.width)+1, 1.45)); }
  127. if self.is_exit_valid(x-1, y+1) { exits.push(((idx+self.width)-1, 1.45)); }
  128. if self.is_exit_valid(x+1, y+1) { exits.push(((idx+self.width)+1, 1.45)); }
  129. exits
  130. }
  131. fn get_pathing_distance(&self, idx1:i32, idx2:i32) -> f32 {
  132. let p1 = Point::new(idx1 % self.width, idx1 / self.width);
  133. let p2 = Point::new(idx2 % self.width, idx2 / self.width);
  134. rltk::DistanceAlg::Pythagoras.distance2d(p1, p2)
  135. }
  136. }
  137. impl Algorithm2D for Map {
  138. fn point2d_to_index(&self, pt: Point) -> i32 {
  139. (pt.y * self.width) + pt.x
  140. }
  141. fn index_to_point2d(&self, idx:i32) -> Point {
  142. Point{ x: idx % self.width, y: idx / self.width }
  143. }
  144. }
  145. pub fn draw_map(ecs: &World, ctx : &mut Rltk) {
  146. let map = ecs.fetch::<Map>();
  147. let mut y = 0;
  148. let mut x = 0;
  149. for (idx,tile) in map.tiles.iter().enumerate() {
  150. // Render a tile depending upon the tile type
  151. if map.revealed_tiles[idx] {
  152. let glyph;
  153. let mut fg;
  154. match tile {
  155. TileType::Floor => {
  156. glyph = rltk::to_cp437('.');
  157. fg = RGB::from_f32(0.0, 0.5, 0.5);
  158. }
  159. TileType::Wall => {
  160. glyph = rltk::to_cp437('#');
  161. fg = RGB::from_f32(0., 1.0, 0.);
  162. }
  163. }
  164. if !map.visible_tiles[idx] { fg = fg.to_greyscale() }
  165. ctx.set(x, y, fg, RGB::from_f32(0., 0., 0.), glyph);
  166. }
  167. // Move the coordinates
  168. x += 1;
  169. if x > 79 {
  170. x = 0;
  171. y += 1;
  172. }
  173. }
  174. }