Advertisement
Guest User

Little dungeon generator script header

a guest
Dec 11th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.51 KB | None | 0 0
  1. //requires std.zh
  2.  
  3. //these 3 arrays are the generated dungeon
  4. //each 25 indexes in the array are one floor, rooms numbered from top left to bottom right
  5. //"layout" is room type
  6. //"doors" is a 4 bit variable like 0000b where each bit indicates if there is a room connection (1) or not (0), in order N/S/W/E
  7. //"content" is a rooms content, TypeA and TypeB can mean anything
  8. int FloorsRoomLayout[75]; //1-13 = main path, 14 = side path, 15 = filler
  9. int FloorsRoomDoors[75];
  10. int FloorsRoomContent[75];
  11. //floor room content
  12. const int FRContent_Start = 1;
  13. const int FRContent_Boss = 2;
  14. const int FRContent_TypeA = 3;
  15. const int FRContent_TypeB = 4;
  16.  
  17. void ResetDungeon(){
  18.     for (int i = 0; i < 75; i++) {
  19.         FloorsRoomLayout[i] = 0;
  20.         FloorsRoomDoors[i] = 0;
  21.         FloorsRoomContent[i] = 0;
  22.     }
  23. }
  24.  
  25. void GenerateDungeon(){
  26.     ResetDungeon();
  27.     int floor; int i; int j; int k; int l; int var; int mainpath[14]; int otherpath[5]; int pathingvisited[25]; int rngstuff[25];
  28.     //begin generating all 3 floors
  29.     for (floor = 1; floor <= 3; floor++) {
  30.         //place filler blocks
  31.         var = Rand(4);
  32.         if ( var == 0 )
  33.             FloorsRoomLayout[FR(0, floor)] = 15;
  34.         else if ( var == 1 )
  35.             FloorsRoomLayout[FR(1, floor)] = 15;
  36.         else if ( var == 2 )
  37.             FloorsRoomLayout[FR(5, floor)] = 15;
  38.         else if ( var == 3 )
  39.             FloorsRoomLayout[FR(6, floor)] = 15;
  40.         var = Rand(4);
  41.         if ( var == 0 )
  42.             FloorsRoomLayout[FR(3, floor)] = 15;
  43.         else if ( var == 1 )
  44.             FloorsRoomLayout[FR(4, floor)] = 15;
  45.         else if ( var == 2 )
  46.             FloorsRoomLayout[FR(8, floor)] = 15;
  47.         else if ( var == 3 )
  48.             FloorsRoomLayout[FR(9, floor)] = 15;
  49.         var = Rand(4);
  50.         if ( var == 0 )
  51.             FloorsRoomLayout[FR(15, floor)] = 15;
  52.         else if ( var == 1 )
  53.             FloorsRoomLayout[FR(16, floor)] = 15;
  54.         else if ( var == 2 )
  55.             FloorsRoomLayout[FR(20, floor)] = 15;
  56.         else if ( var == 3 )
  57.             FloorsRoomLayout[FR(21, floor)] = 15;
  58.         var = Rand(4);
  59.         if ( var == 0 )
  60.             FloorsRoomLayout[FR(18, floor)] = 15;
  61.         else if ( var == 1 )
  62.             FloorsRoomLayout[FR(19, floor)] = 15;
  63.         else if ( var == 2 )
  64.             FloorsRoomLayout[FR(23, floor)] = 15;
  65.         else if ( var == 3 )
  66.             FloorsRoomLayout[FR(24, floor)] = 15;
  67.        
  68.         //place start room
  69.         if ( floor == 1 )
  70.             var = Rand(0, 24);
  71.         else {
  72.             for (i = 0; i <= 24; i++) {
  73.                 if ( FloorsRoomContent[FR(i, floor-1)] == FRContent_Boss )
  74.                     var = i;
  75.             }
  76.         }
  77.         FloorsRoomLayout[FR(var, floor)] = 1;
  78.         FloorsRoomContent[FR(var, floor)] = FRContent_Start;
  79.        
  80.         //find main path
  81.         for (i = 0; i < 14; i++)
  82.             mainpath[i] = -1;
  83.         for (i = 0; i < 25; i++)
  84.             pathingvisited[i] = 0;
  85.         mainpath[1] = var; //the starting room
  86.         pathingvisited[var] = 1;
  87.         i = 1;
  88.         var = 0;
  89.         while(var == 0){
  90.             mainpath[i+1] = ChoosePath(mainpath[i], floor, pathingvisited, false);
  91.             if ( mainpath[i+1] != -1 ) { //path found, so go there
  92.                 i ++;
  93.                 pathingvisited[mainpath[i]] = 1;
  94.             }
  95.             else if ( i == 12 ) //no path found but be satisfied with 12
  96.                 var = 12;
  97.             else { //no path found, so backtrack
  98.                 if ( i > 1 )
  99.                     mainpath[i] = -1;
  100.                 i --;
  101.             }
  102.             if ( i >= 13 ) //end at 13
  103.                 var = 13;
  104.             if ( i <= 0 ) {
  105.                 //pathing is dumb, retry
  106.                 for (i = 2; i < 14; i++)
  107.                     mainpath[i] = -1;
  108.                 for (i = 0; i < 25; i++)
  109.                     pathingvisited[i] = 0;
  110.                 pathingvisited[mainpath[1]] = 1;
  111.                 i = 1;
  112.             }
  113.         }
  114.         for (i = 0; i < 25; i++)
  115.             pathingvisited[i] = 0;
  116.         //create main path from stack
  117.         for (i = 2; i <= var; i++) {
  118.             FloorsRoomLayout[FR(mainpath[i], floor)] = i;
  119.             if ( mainpath[i-1] == mainpath[i]-5 ) { //room before is north
  120.                 FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0100b;
  121.                 FloorsRoomDoors[FR(mainpath[i], floor)] |= 1000b;
  122.             }
  123.             else if ( mainpath[i-1] == mainpath[i]+5 ) { //room before is south
  124.                 FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 1000b;
  125.                 FloorsRoomDoors[FR(mainpath[i], floor)] |= 0100b;
  126.             }
  127.             else if ( mainpath[i-1] == mainpath[i]-1 ) { //room before is west
  128.                 FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0001b;
  129.                 FloorsRoomDoors[FR(mainpath[i], floor)] |= 0010b;
  130.             }
  131.             else if ( mainpath[i-1] == mainpath[i]+1 ) { //room before is east
  132.                 FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0010b;
  133.                 FloorsRoomDoors[FR(mainpath[i], floor)] |= 0001b;
  134.             }
  135.             if ( i == var )
  136.                 FloorsRoomContent[FR(mainpath[i], floor)] = FRContent_Boss;
  137.         }
  138.        
  139.         //remove filler blocks
  140.         for (i = 0; i <= 24; i++) {
  141.             if ( FloorsRoomLayout[FR(i, floor)] == 15 )
  142.                 FloorsRoomLayout[FR(i, floor)] = 0;
  143.         }
  144.        
  145.         //find "TypeA" paths
  146.         for (i = 0; i < 2; i++) {
  147.             var = 0;
  148.             if ( floor == 1 ) j = 50;
  149.             if ( floor == 2 ) j = 20;
  150.             if ( floor == 3 ) j = 0;
  151.             if ( Rand(100) >= j ) { //chance to skip 3 tiles try
  152.                 for (j = 0; j < 20; j++) { //3 tiles try
  153.                     k = Rand(1, 12);
  154.                     if ( FloorsRoomContent[FR(mainpath[k], floor)] != FRContent_Boss ) {
  155.                         for (l = 0; l < 5; l++)
  156.                             otherpath[l] = -1;
  157.                         for (l = 0; l < 25; l++)
  158.                             pathingvisited[l] = 0;
  159.                         otherpath[1] = mainpath[k];
  160.                         l = 1;
  161.                         while(l > 0){
  162.                             otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
  163.                             if ( otherpath[l+1] != -1 ) {
  164.                                 l ++;
  165.                                 pathingvisited[otherpath[l]] = 1;
  166.                             }
  167.                             else
  168.                                 l = 0; //exit loop
  169.                             if ( l == 4 ) {
  170.                                 var = 4;
  171.                                 j = 20; //exit loop
  172.                                 l = 0; //exit loop
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.             if ( var == 0 ) {
  179.                 for (j = 0; j < 50; j++) { //2 tiles try
  180.                     k = Rand(1, 12);
  181.                     if ( FloorsRoomContent[FR(mainpath[k], floor)] != FRContent_Boss ) {
  182.                         for (l = 0; l < 5; l++)
  183.                             otherpath[l] = -1;
  184.                         for (l = 0; l < 25; l++)
  185.                             pathingvisited[l] = 0;
  186.                         otherpath[1] = mainpath[k];
  187.                         l = 1;
  188.                         while(l > 0){
  189.                             otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
  190.                             if ( otherpath[l+1] != -1 ) {
  191.                                 l ++;
  192.                                 pathingvisited[otherpath[l]] = 1;
  193.                             }
  194.                             else
  195.                                 l = 0; //exit loop
  196.                             if ( l == 3 ) {
  197.                                 var = 3;
  198.                                 j = 50; //exit loop
  199.                                 l = 0; //exit loop
  200.                             }
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.             if ( var == 0 ) {
  206.                 for (j = 0; j < 5; j++)
  207.                     otherpath[j] = -1;
  208.                 for (j = 0; j < 25; j++)
  209.                     pathingvisited[j] = 0;
  210.                 while(var == 0){ //1 tile bruteforce
  211.                     j = Rand(1, 12);
  212.                     otherpath[1] = mainpath[j];
  213.                     otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
  214.                     if ( otherpath[2] != -1 )
  215.                         var = 2;
  216.                     WaitNoAction();
  217.                 }
  218.             }
  219.             //create "TypeA" path from stack
  220.             for (j = 2; j <= var; j++) {
  221.                 FloorsRoomLayout[FR(otherpath[j], floor)] = 14;
  222.                 if ( otherpath[j-1] == otherpath[j]-5 ) { //room before is north
  223.                     FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0100b;
  224.                     FloorsRoomDoors[FR(otherpath[j], floor)] |= 1000b;
  225.                 }
  226.                 else if ( otherpath[j-1] == otherpath[j]+5 ) { //room before is south
  227.                     FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 1000b;
  228.                     FloorsRoomDoors[FR(otherpath[j], floor)] |= 0100b;
  229.                 }
  230.                 else if ( otherpath[j-1] == otherpath[j]-1 ) { //room before is west
  231.                     FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0001b;
  232.                     FloorsRoomDoors[FR(otherpath[j], floor)] |= 0010b;
  233.                 }
  234.                 else if ( otherpath[j-1] == otherpath[j]+1 ) { //room before is east
  235.                     FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0010b;
  236.                     FloorsRoomDoors[FR(otherpath[j], floor)] |= 0001b;
  237.                 }
  238.                 if ( j == var )
  239.                     FloorsRoomContent[FR(otherpath[j], floor)] = FRContent_TypeA;
  240.             }
  241.         }
  242.        
  243.         //find "TypeB" path
  244.         var = 0;
  245.         for (j = 0; j < 20; j++) { //2 tiles try
  246.             k = Rand(25);
  247.             while(FloorsRoomContent[FR(k, floor)] == FRContent_Boss || FloorsRoomContent[FR(k, floor)] == FRContent_TypeA
  248.             || FloorsRoomLayout[FR(k, floor)] == 0)
  249.                 k = Rand(25);
  250.             for (l = 0; l < 5; l++)
  251.                 otherpath[l] = -1;
  252.             for (l = 0; l < 25; l++)
  253.                 pathingvisited[l] = 0;
  254.             otherpath[1] = k;
  255.             l = 1;
  256.             while(l > 0){
  257.                 otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
  258.                 if ( otherpath[l+1] != -1 ) {
  259.                     l ++;
  260.                     pathingvisited[otherpath[l]] = 1;
  261.                 }
  262.                 else
  263.                     l = 0; //exit loop
  264.                 if ( l == 3 ) {
  265.                     var = 3;
  266.                     j = 20; //exit loop
  267.                     l = 0; //exit loop
  268.                 }
  269.             }
  270.         }
  271.         if ( var == 0 ) {
  272.             for (j = 0; j < 5; j++)
  273.                 otherpath[j] = -1;
  274.             for (j = 0; j < 25; j++)
  275.                 pathingvisited[j] = 0;
  276.             while(var == 0){ //1 tile bruteforce
  277.                 j = Rand(25);
  278.                 while(FloorsRoomContent[FR(j, floor)] == FRContent_Boss || FloorsRoomContent[FR(j, floor)] == FRContent_TypeA
  279.                 || FloorsRoomLayout[FR(j, floor)] == 0)
  280.                     j = Rand(25);
  281.                 otherpath[1] = j;
  282.                 otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
  283.                 if ( otherpath[2] != -1 )
  284.                     var = 2;
  285.             }
  286.         }
  287.         //create "TypeB" path from stack
  288.         for (j = 2; j <= var; j++) {
  289.             FloorsRoomLayout[FR(otherpath[j], floor)] = 14;
  290.             if ( otherpath[j-1] == otherpath[j]-5 ) { //room before is north
  291.                 FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0100b;
  292.                 FloorsRoomDoors[FR(otherpath[j], floor)] |= 1000b;
  293.             }
  294.             else if ( otherpath[j-1] == otherpath[j]+5 ) { //room before is south
  295.                 FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 1000b;
  296.                 FloorsRoomDoors[FR(otherpath[j], floor)] |= 0100b;
  297.             }
  298.             else if ( otherpath[j-1] == otherpath[j]-1 ) { //room before is west
  299.                 FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0001b;
  300.                 FloorsRoomDoors[FR(otherpath[j], floor)] |= 0010b;
  301.             }
  302.             else if ( otherpath[j-1] == otherpath[j]+1 ) { //room before is east
  303.                 FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0010b;
  304.                 FloorsRoomDoors[FR(otherpath[j], floor)] |= 0001b;
  305.             }
  306.             if ( j == var )
  307.                 FloorsRoomContent[FR(otherpath[j], floor)] = FRContent_TypeB;
  308.         }
  309.        
  310.         //find random rooms
  311.         for (i = 0; i < 50; i++) {
  312.             k = Rand(25);
  313.             while(FloorsRoomContent[FR(k, floor)] == FRContent_Boss || FloorsRoomContent[FR(k, floor)] == FRContent_TypeA
  314.             || FloorsRoomLayout[FR(k, floor)] == 0)
  315.                 k = Rand(25);
  316.             for (l = 0; l < 5; l++)
  317.                 otherpath[l] = -1;
  318.             for (l = 0; l < 25; l++)
  319.                 pathingvisited[l] = 0;
  320.             otherpath[1] = k;
  321.             otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
  322.             if ( otherpath[2] != -1 ) {
  323.                 //create room
  324.                 FloorsRoomLayout[FR(otherpath[2], floor)] = 14;
  325.                 if ( otherpath[1] == otherpath[2]-5 ) { //room before is north
  326.                     FloorsRoomDoors[FR(otherpath[1], floor)] |= 0100b;
  327.                     FloorsRoomDoors[FR(otherpath[2], floor)] |= 1000b;
  328.                 }
  329.                 else if ( otherpath[1] == otherpath[2]+5 ) { //room before is south
  330.                     FloorsRoomDoors[FR(otherpath[1], floor)] |= 1000b;
  331.                     FloorsRoomDoors[FR(otherpath[2], floor)] |= 0100b;
  332.                 }
  333.                 else if ( otherpath[1] == otherpath[2]-1 ) { //room before is west
  334.                     FloorsRoomDoors[FR(otherpath[1], floor)] |= 0001b;
  335.                     FloorsRoomDoors[FR(otherpath[2], floor)] |= 0010b;
  336.                 }
  337.                 else if ( otherpath[1] == otherpath[2]+1 ) { //room before is east
  338.                     FloorsRoomDoors[FR(otherpath[1], floor)] |= 0010b;
  339.                     FloorsRoomDoors[FR(otherpath[2], floor)] |= 0001b;
  340.                 }
  341.             }
  342.         }
  343.        
  344.         //create final rooms
  345.         for (i = 0; i < 25; i++)
  346.             rngstuff[i] = -1;
  347.         var = 0;
  348.         for (i = 0; i < 25; i++) {
  349.             if ( FloorsRoomLayout[FR(i, floor)] == 0 ) {
  350.                 rngstuff[i] = Rand(250);
  351.                 var ++;
  352.             }
  353.         }
  354.         while(var > 0){
  355.             for (i = 0; i < 250; i++) {
  356.                 for (j = 0; j < 25; j++) {
  357.                     if ( i == rngstuff[j] ) {
  358.                         for (k = 0; k < 5; k++)
  359.                             otherpath[k] = -1;
  360.                         otherpath[1] = j;
  361.                         otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, true);
  362.                         if ( otherpath[2] != -1 ) {
  363.                             FloorsRoomLayout[FR(otherpath[1], floor)] = 14;
  364.                             if ( otherpath[1] == otherpath[2]-5 ) { //room before is north
  365.                                 FloorsRoomDoors[FR(otherpath[1], floor)] |= 0100b;
  366.                                 FloorsRoomDoors[FR(otherpath[2], floor)] |= 1000b;
  367.                             }
  368.                             else if ( otherpath[1] == otherpath[2]+5 ) { //room before is south
  369.                                 FloorsRoomDoors[FR(otherpath[1], floor)] |= 1000b;
  370.                                 FloorsRoomDoors[FR(otherpath[2], floor)] |= 0100b;
  371.                             }
  372.                             else if ( otherpath[1] == otherpath[2]-1 ) { //room before is west
  373.                                 FloorsRoomDoors[FR(otherpath[1], floor)] |= 0001b;
  374.                                 FloorsRoomDoors[FR(otherpath[2], floor)] |= 0010b;
  375.                             }
  376.                             else if ( otherpath[1] == otherpath[2]+1 ) { //room before is east
  377.                                 FloorsRoomDoors[FR(otherpath[1], floor)] |= 0010b;
  378.                                 FloorsRoomDoors[FR(otherpath[2], floor)] |= 0001b;
  379.                             }
  380.                             var --; //exit loop when floor is filled
  381.                         }
  382.                     }
  383.                 }
  384.             }
  385.         }
  386.     }
  387. }
  388.  
  389. int FR(int room, int floor){
  390.     return room + ((floor-1) * 25);
  391. }
  392. int NorthRoomIndex(int i){
  393.     if ( i < 5 )
  394.         return -1;
  395.     return i-5;
  396. }
  397. int SouthRoomIndex(int i){
  398.     if ( i > 19 )
  399.         return -1;
  400.     return i+5;
  401. }
  402. int WestRoomIndex(int i){
  403.     if ( i == 0 || i == 5 || i == 10 || i == 15 || i == 20 )
  404.         return -1;
  405.     return i-1;
  406. }
  407. int EastRoomIndex(int i){
  408.     if ( i == 4 || i == 9 || i == 14 || i == 19 || i == 24 )
  409.         return -1;
  410.     return i+1;
  411. }
  412. int ChoosePath(int room, int floor, int pathingvisited, bool finalrooms){
  413.     int pathvars[11]; //pathoptions up/down/left/right, iteration, index, numoptions, pathoptions a/b/c/d
  414.     pathvars[0] = -1; pathvars[1] = -1; pathvars[2] = -1; pathvars[3] = -1; pathvars[6] = 0;
  415.     for (pathvars[4] = 0; pathvars[4] < 4; pathvars[4]++) { //check all directions for options
  416.         if ( pathvars[4] == 0 )
  417.             pathvars[5] = NorthRoomIndex(room);
  418.         else if ( pathvars[4] == 1 )
  419.             pathvars[5] = SouthRoomIndex(room);
  420.         else if ( pathvars[4] == 2 )
  421.             pathvars[5] = WestRoomIndex(room);
  422.         else
  423.             pathvars[5] = EastRoomIndex(room);
  424.         if ( pathvars[5] != -1 ) {
  425.             if ( !finalrooms ) {
  426.                 if ( FloorsRoomLayout[FR(pathvars[5], floor)] == 0 ) {
  427.                     if ( pathingvisited[pathvars[5]] == 0 ) {
  428.                         pathvars[pathvars[4]] = pathvars[5];
  429.                         pathvars[6] ++;
  430.                     }
  431.                 }
  432.             }
  433.             else {
  434.                 if ( FloorsRoomLayout[FR(pathvars[5], floor)] > 0 ) {
  435.                     if ( FloorsRoomContent[FR(pathvars[5], floor)] != FRContent_Boss ) {
  436.                         pathvars[pathvars[4]] = pathvars[5];
  437.                         pathvars[6] ++;
  438.                     }
  439.                 }
  440.             }
  441.         }
  442.     }
  443.     if ( pathvars[6] == 0 )
  444.         return -1;
  445.     for (pathvars[4] = 0; pathvars[4] < pathvars[6]; pathvars[4]++) { //put options into a/b/c/d
  446.         if ( pathvars[0] != -1 ) {
  447.             pathvars[7+pathvars[4]] = pathvars[0];
  448.             pathvars[0] = -1;
  449.         }
  450.         else if ( pathvars[1] != -1 ) {
  451.             pathvars[7+pathvars[4]] = pathvars[1];
  452.             pathvars[1] = -1;
  453.         }
  454.         else if ( pathvars[2] != -1 ) {
  455.             pathvars[7+pathvars[4]] = pathvars[2];
  456.             pathvars[2] = -1;
  457.         }
  458.         else if ( pathvars[3] != -1 ) {
  459.             pathvars[7+pathvars[4]] = pathvars[3];
  460.             pathvars[3] = -1;
  461.         }
  462.     }
  463.     pathvars[5] = Rand(pathvars[6]);
  464.     return pathvars[7+pathvars[5]];
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement