fork download
  1. #include "Dungeon.h"
  2.  
  3. Dungeon::Dungeon()
  4. {
  5. //ctor
  6. }
  7.  
  8.  
  9. bool Dungeon::check(char** dungeon, char dir)
  10. {
  11. //Check if it is possible to make room in the direction(%dir) that was passed {{{
  12. bool is_available;
  13. switch(dir){
  14. case 's':
  15. is_available = y_pos + height <= D_HEIGHT && x_pos + width <= D_WIDTH;
  16. if(is_available){
  17. for (int y = y_pos; y < y_pos + height; y++){
  18. for(int x = x_pos; x < x_pos + width; x++){
  19. if (y == y_pos || y == y_pos + (height-1) || x == x_pos || x == x_pos + (width-1)) continue;
  20. if (dungeon[y][x] != NOTHING) is_available = false;
  21. }
  22. }
  23. }
  24. break;
  25. case 'S':
  26. is_available = y_pos + height <= D_HEIGHT && x_pos - width >= 0;
  27. if(is_available){
  28. for (int y = y_pos; y < y_pos + height; y++){
  29. for(int x = x_pos; x > x_pos - width; x--){
  30. if (y == y_pos || y == y_pos + (height-1) || x == x_pos || x == x_pos - (width-1)) continue;
  31. if (dungeon[y][x] != NOTHING) is_available = false;
  32. }
  33. }
  34. }
  35. break;
  36. case 'n':
  37. is_available = y_pos - height >= 0 && x_pos + width <= D_WIDTH;
  38. if(is_available){
  39. for (int y = y_pos; y > y_pos - height; y--){
  40. for(int x = x_pos; x < x_pos + width; x++){
  41. if (y == y_pos || y == y_pos - (height-1) || x == x_pos || x == x_pos + (width-1)) continue;
  42. if (dungeon[y][x] != NOTHING) is_available = false;
  43. }
  44. }
  45. }
  46. break;
  47. case 'N':
  48. is_available = y_pos - height >= 0 && x_pos - width >= 0;
  49. if(is_available){
  50. for (int y = y_pos; y > y_pos - height; y--){
  51. for(int x = x_pos; x > x_pos - width; x--){
  52. if (y == y_pos || y == y_pos - (height-1) || x == x_pos || x == x_pos - (width-1)) continue;
  53. if (dungeon[y][x] != NOTHING) is_available = false;
  54. }
  55. }
  56. }
  57. break;
  58. }
  59. return is_available;
  60. //}}}
  61. }
  62.  
  63.  
  64. void Dungeon::genDungeon(char** &dungeon)
  65. {
  66. for (int y = 0; y<D_HEIGHT; y++){
  67. for (int x = 0; x<D_WIDTH; x++){
  68. dungeon[y][x] = NOTHING;
  69. }
  70. }
  71.  
  72. //Starting point
  73. y_pos = rand() % D_HEIGHT;
  74. x_pos = rand() % D_WIDTH;
  75.  
  76. do{
  77. genRoom(dungeon);
  78. }while(!dirs.empty());
  79. }
  80.  
  81.  
  82. void Dungeon::genRoom(char** &dungeon)
  83. {
  84. dirs.clear();
  85.  
  86. //S - south west; s - south east; N - north west; n - north east;
  87. char s_w = 'S'; char s_e = 's'; char n_w = 'N'; char n_e = 'n';
  88. char dir;
  89.  
  90. //Room width and height {{{
  91. constexpr int MIN_WIDTH = D_WIDTH / 20;
  92. constexpr int MIN_HEIGHT = D_HEIGHT / 20;
  93. constexpr int MAX_WIDTH = D_WIDTH / 4;
  94. constexpr int MAX_HEIGHT = D_HEIGHT / 4;
  95. width = rand() % MAX_WIDTH + MIN_WIDTH;
  96. height = rand() % MAX_HEIGHT + MIN_HEIGHT;
  97. //}}}
  98.  
  99. //Store possible directions in %dirs vector {{{
  100. if (check(dungeon, s_e)){
  101. dirs.push_back(s_e);
  102. }
  103. if (check(dungeon, s_w)){
  104. dirs.push_back(s_w);
  105. }
  106. if (check(dungeon, n_e)){
  107. dirs.push_back(n_e);
  108. }
  109. if (check(dungeon, n_w)){
  110. dirs.push_back(n_w);
  111. }
  112. //Break if there is no possible directions
  113. if(dirs.empty()) return;
  114. //}}}
  115.  
  116.  
  117. //Make room in the randomly selected direction {{{
  118. dir = dirs[rand()%dirs.size()];
  119. switch (dir){
  120. case 's':
  121. for (int y = y_pos; y < y_pos + height; y++){
  122. for (int x = x_pos; x < x_pos + width; x++){ //HOW THIS WORKS: It just draws WALL if current y||x pos is the first||last, i.e. y||x == starting_point or y||x == ending_point
  123. if (y == y_pos || y == y_pos + (height-1) || x == x_pos || x == x_pos + (width-1)){
  124. dungeon[y][x] = WALL;
  125. }else{
  126. dungeon[y][x] = FLOOR;
  127. }
  128. }
  129. }
  130. y_pos += (height - 1);
  131. x_pos += (width - 1);
  132. break;
  133. case 'S':
  134. for (int y = y_pos; y < y_pos + height; y++){
  135. for (int x = x_pos; x > x_pos - width; x--){
  136. if (y == y_pos || y == y_pos + (height-1) || x == x_pos || x == x_pos - (width-1)){
  137. dungeon[y][x] = WALL;
  138. }else{
  139. dungeon[y][x] = FLOOR;
  140. }
  141. }
  142. }
  143. y_pos += (height - 1);
  144. x_pos -= (width - 1);
  145. break;
  146. case 'n':
  147. for (int y = y_pos; y > y_pos - height; y--){
  148. for (int x = x_pos; x < x_pos + width; x++){
  149. if (y == y_pos || y == y_pos - (height-1) || x == x_pos || x == x_pos + (width-1)){
  150. dungeon[y][x] = WALL;
  151. }else{
  152. dungeon[y][x] = FLOOR;
  153. }
  154. }
  155. }
  156. y_pos -= (height - 1);
  157. x_pos += (width - 1);
  158. break;
  159. case 'N':
  160. for (int y = y_pos; y > y_pos - height; y--){
  161. for (int x = x_pos; x > x_pos - width; x--){
  162. if (y == y_pos || y == y_pos - (height-1) || x == x_pos || x == x_pos - (width-1)){
  163. dungeon[y][x] = WALL;
  164. }else{
  165. dungeon[y][x] = FLOOR;
  166. }
  167. }
  168. }
  169. y_pos -= (height - 1);
  170. x_pos -= (width - 1);
  171. break;
  172. }
  173. //}}}
  174. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: Dungeon.h: No such file or directory
 #include "Dungeon.h"
                     ^
compilation terminated.
stdout
Standard output is empty