map.rs 6.7 KB

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