map.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use rltk::{ RGB, Rltk, Console, RandomNumberGenerator };
  2. use super::{Rect};
  3. use std::cmp::{max, min};
  4. #[derive(PartialEq, Copy, Clone)]
  5. pub enum TileType {
  6. Wall, Floor
  7. }
  8. pub fn xy_idx(x: i32, y: i32) -> usize {
  9. (y as usize * 80) + x as usize
  10. }
  11. /// Makes a map with solid boundaries and 400 randomly placed walls. No guarantees that it won't
  12. /// look awful.
  13. pub fn new_map_test() -> Vec<TileType> {
  14. let mut map = vec![TileType::Floor; 80*50];
  15. // Make the boundaries walls
  16. for x in 0..80 {
  17. map[xy_idx(x, 0)] = TileType::Wall;
  18. map[xy_idx(x, 49)] = TileType::Wall;
  19. }
  20. for y in 0..50 {
  21. map[xy_idx(0, y)] = TileType::Wall;
  22. map[xy_idx(79, y)] = TileType::Wall;
  23. }
  24. // Now we'll randomly splat a bunch of walls. It won't be pretty, but it's a decent illustration.
  25. // First, obtain the thread-local RNG:
  26. let mut rng = rltk::RandomNumberGenerator::new();
  27. for _i in 0..400 {
  28. let x = rng.roll_dice(1, 79);
  29. let y = rng.roll_dice(1, 49);
  30. let idx = xy_idx(x, y);
  31. if idx != xy_idx(40, 25) {
  32. map[idx] = TileType::Wall;
  33. }
  34. }
  35. map
  36. }
  37. fn apply_room_to_map(room : &Rect, map: &mut [TileType]) {
  38. for y in room.y1 +1 ..= room.y2 {
  39. for x in room.x1 + 1 ..= room.x2 {
  40. map[xy_idx(x, y)] = TileType::Floor;
  41. }
  42. }
  43. }
  44. fn apply_horizontal_tunnel(map: &mut [TileType], x1:i32, x2:i32, y:i32) {
  45. for x in min(x1,x2) ..= max(x1,x2) {
  46. let idx = xy_idx(x, y);
  47. if idx > 0 && idx < 80*50 {
  48. map[idx as usize] = TileType::Floor;
  49. }
  50. }
  51. }
  52. fn apply_vertical_tunnel(map: &mut [TileType], y1:i32, y2:i32, x:i32) {
  53. for y in min(y1,y2) ..= max(y1,y2) {
  54. let idx = xy_idx(x, y);
  55. if idx > 0 && idx < 80*50 {
  56. map[idx as usize] = TileType::Floor;
  57. }
  58. }
  59. }
  60. /// Makes a new map using the algorithm from http://rogueliketutorials.com/tutorials/tcod/part-3/
  61. /// This gives a handful of random rooms and corridors joining them together.
  62. pub fn new_map_rooms_and_corridors() -> (Vec<Rect>, Vec<TileType>) {
  63. let mut map = vec![TileType::Wall; 80*50];
  64. let mut rooms : Vec<Rect> = Vec::new();
  65. const MAX_ROOMS : i32 = 30;
  66. const MIN_SIZE : i32 = 6;
  67. const MAX_SIZE : i32 = 10;
  68. let mut rng = RandomNumberGenerator::new();
  69. for _i in 0..MAX_ROOMS {
  70. let w = rng.range(MIN_SIZE, MAX_SIZE);
  71. let h = rng.range(MIN_SIZE, MAX_SIZE);
  72. let x = rng.roll_dice(1, 80 - w - 1) - 1;
  73. let y = rng.roll_dice(1, 50 - h - 1) - 1;
  74. let new_room = Rect::new(x, y, w, h);
  75. let mut ok = true;
  76. for other_room in rooms.iter() {
  77. if new_room.intersect(other_room) { ok = false }
  78. }
  79. if ok {
  80. apply_room_to_map(&new_room, &mut map);
  81. if !rooms.is_empty() {
  82. let (new_x, new_y) = new_room.center();
  83. let (prev_x, prev_y) = rooms[rooms.len()-1].center();
  84. if rng.range(0,1) == 1 {
  85. apply_horizontal_tunnel(&mut map, prev_x, new_x, prev_y);
  86. apply_vertical_tunnel(&mut map, prev_y, new_y, new_x);
  87. } else {
  88. apply_vertical_tunnel(&mut map, prev_y, new_y, prev_x);
  89. apply_horizontal_tunnel(&mut map, prev_x, new_x, new_y);
  90. }
  91. }
  92. rooms.push(new_room);
  93. }
  94. }
  95. (rooms, map)
  96. }
  97. pub fn draw_map(map: &[TileType], ctx : &mut Rltk) {
  98. let mut y = 0;
  99. let mut x = 0;
  100. for tile in map.iter() {
  101. // Render a tile depending upon the tile type
  102. match tile {
  103. TileType::Floor => {
  104. ctx.set(x, y, RGB::from_f32(0.5, 0.5, 0.5), RGB::from_f32(0., 0., 0.), rltk::to_cp437('.'));
  105. }
  106. TileType::Wall => {
  107. ctx.set(x, y, RGB::from_f32(0.0, 1.0, 0.0), RGB::from_f32(0., 0., 0.), rltk::to_cp437('#'));
  108. }
  109. }
  110. // Move the coordinates
  111. x += 1;
  112. if x > 79 {
  113. x = 0;
  114. y += 1;
  115. }
  116. }
  117. }