Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //requires std.zh
- //these 3 arrays are the generated dungeon
- //each 25 indexes in the array are one floor, rooms numbered from top left to bottom right
- //"layout" is room type
- //"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
- //"content" is a rooms content, TypeA and TypeB can mean anything
- int FloorsRoomLayout[75]; //1-13 = main path, 14 = side path, 15 = filler
- int FloorsRoomDoors[75];
- int FloorsRoomContent[75];
- //floor room content
- const int FRContent_Start = 1;
- const int FRContent_Boss = 2;
- const int FRContent_TypeA = 3;
- const int FRContent_TypeB = 4;
- void ResetDungeon(){
- for (int i = 0; i < 75; i++) {
- FloorsRoomLayout[i] = 0;
- FloorsRoomDoors[i] = 0;
- FloorsRoomContent[i] = 0;
- }
- }
- void GenerateDungeon(){
- ResetDungeon();
- int floor; int i; int j; int k; int l; int var; int mainpath[14]; int otherpath[5]; int pathingvisited[25]; int rngstuff[25];
- //begin generating all 3 floors
- for (floor = 1; floor <= 3; floor++) {
- //place filler blocks
- var = Rand(4);
- if ( var == 0 )
- FloorsRoomLayout[FR(0, floor)] = 15;
- else if ( var == 1 )
- FloorsRoomLayout[FR(1, floor)] = 15;
- else if ( var == 2 )
- FloorsRoomLayout[FR(5, floor)] = 15;
- else if ( var == 3 )
- FloorsRoomLayout[FR(6, floor)] = 15;
- var = Rand(4);
- if ( var == 0 )
- FloorsRoomLayout[FR(3, floor)] = 15;
- else if ( var == 1 )
- FloorsRoomLayout[FR(4, floor)] = 15;
- else if ( var == 2 )
- FloorsRoomLayout[FR(8, floor)] = 15;
- else if ( var == 3 )
- FloorsRoomLayout[FR(9, floor)] = 15;
- var = Rand(4);
- if ( var == 0 )
- FloorsRoomLayout[FR(15, floor)] = 15;
- else if ( var == 1 )
- FloorsRoomLayout[FR(16, floor)] = 15;
- else if ( var == 2 )
- FloorsRoomLayout[FR(20, floor)] = 15;
- else if ( var == 3 )
- FloorsRoomLayout[FR(21, floor)] = 15;
- var = Rand(4);
- if ( var == 0 )
- FloorsRoomLayout[FR(18, floor)] = 15;
- else if ( var == 1 )
- FloorsRoomLayout[FR(19, floor)] = 15;
- else if ( var == 2 )
- FloorsRoomLayout[FR(23, floor)] = 15;
- else if ( var == 3 )
- FloorsRoomLayout[FR(24, floor)] = 15;
- //place start room
- if ( floor == 1 )
- var = Rand(0, 24);
- else {
- for (i = 0; i <= 24; i++) {
- if ( FloorsRoomContent[FR(i, floor-1)] == FRContent_Boss )
- var = i;
- }
- }
- FloorsRoomLayout[FR(var, floor)] = 1;
- FloorsRoomContent[FR(var, floor)] = FRContent_Start;
- //find main path
- for (i = 0; i < 14; i++)
- mainpath[i] = -1;
- for (i = 0; i < 25; i++)
- pathingvisited[i] = 0;
- mainpath[1] = var; //the starting room
- pathingvisited[var] = 1;
- i = 1;
- var = 0;
- while(var == 0){
- mainpath[i+1] = ChoosePath(mainpath[i], floor, pathingvisited, false);
- if ( mainpath[i+1] != -1 ) { //path found, so go there
- i ++;
- pathingvisited[mainpath[i]] = 1;
- }
- else if ( i == 12 ) //no path found but be satisfied with 12
- var = 12;
- else { //no path found, so backtrack
- if ( i > 1 )
- mainpath[i] = -1;
- i --;
- }
- if ( i >= 13 ) //end at 13
- var = 13;
- if ( i <= 0 ) {
- //pathing is dumb, retry
- for (i = 2; i < 14; i++)
- mainpath[i] = -1;
- for (i = 0; i < 25; i++)
- pathingvisited[i] = 0;
- pathingvisited[mainpath[1]] = 1;
- i = 1;
- }
- }
- for (i = 0; i < 25; i++)
- pathingvisited[i] = 0;
- //create main path from stack
- for (i = 2; i <= var; i++) {
- FloorsRoomLayout[FR(mainpath[i], floor)] = i;
- if ( mainpath[i-1] == mainpath[i]-5 ) { //room before is north
- FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0100b;
- FloorsRoomDoors[FR(mainpath[i], floor)] |= 1000b;
- }
- else if ( mainpath[i-1] == mainpath[i]+5 ) { //room before is south
- FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 1000b;
- FloorsRoomDoors[FR(mainpath[i], floor)] |= 0100b;
- }
- else if ( mainpath[i-1] == mainpath[i]-1 ) { //room before is west
- FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0001b;
- FloorsRoomDoors[FR(mainpath[i], floor)] |= 0010b;
- }
- else if ( mainpath[i-1] == mainpath[i]+1 ) { //room before is east
- FloorsRoomDoors[FR(mainpath[i-1], floor)] |= 0010b;
- FloorsRoomDoors[FR(mainpath[i], floor)] |= 0001b;
- }
- if ( i == var )
- FloorsRoomContent[FR(mainpath[i], floor)] = FRContent_Boss;
- }
- //remove filler blocks
- for (i = 0; i <= 24; i++) {
- if ( FloorsRoomLayout[FR(i, floor)] == 15 )
- FloorsRoomLayout[FR(i, floor)] = 0;
- }
- //find "TypeA" paths
- for (i = 0; i < 2; i++) {
- var = 0;
- if ( floor == 1 ) j = 50;
- if ( floor == 2 ) j = 20;
- if ( floor == 3 ) j = 0;
- if ( Rand(100) >= j ) { //chance to skip 3 tiles try
- for (j = 0; j < 20; j++) { //3 tiles try
- k = Rand(1, 12);
- if ( FloorsRoomContent[FR(mainpath[k], floor)] != FRContent_Boss ) {
- for (l = 0; l < 5; l++)
- otherpath[l] = -1;
- for (l = 0; l < 25; l++)
- pathingvisited[l] = 0;
- otherpath[1] = mainpath[k];
- l = 1;
- while(l > 0){
- otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
- if ( otherpath[l+1] != -1 ) {
- l ++;
- pathingvisited[otherpath[l]] = 1;
- }
- else
- l = 0; //exit loop
- if ( l == 4 ) {
- var = 4;
- j = 20; //exit loop
- l = 0; //exit loop
- }
- }
- }
- }
- }
- if ( var == 0 ) {
- for (j = 0; j < 50; j++) { //2 tiles try
- k = Rand(1, 12);
- if ( FloorsRoomContent[FR(mainpath[k], floor)] != FRContent_Boss ) {
- for (l = 0; l < 5; l++)
- otherpath[l] = -1;
- for (l = 0; l < 25; l++)
- pathingvisited[l] = 0;
- otherpath[1] = mainpath[k];
- l = 1;
- while(l > 0){
- otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
- if ( otherpath[l+1] != -1 ) {
- l ++;
- pathingvisited[otherpath[l]] = 1;
- }
- else
- l = 0; //exit loop
- if ( l == 3 ) {
- var = 3;
- j = 50; //exit loop
- l = 0; //exit loop
- }
- }
- }
- }
- }
- if ( var == 0 ) {
- for (j = 0; j < 5; j++)
- otherpath[j] = -1;
- for (j = 0; j < 25; j++)
- pathingvisited[j] = 0;
- while(var == 0){ //1 tile bruteforce
- j = Rand(1, 12);
- otherpath[1] = mainpath[j];
- otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
- if ( otherpath[2] != -1 )
- var = 2;
- WaitNoAction();
- }
- }
- //create "TypeA" path from stack
- for (j = 2; j <= var; j++) {
- FloorsRoomLayout[FR(otherpath[j], floor)] = 14;
- if ( otherpath[j-1] == otherpath[j]-5 ) { //room before is north
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0100b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 1000b;
- }
- else if ( otherpath[j-1] == otherpath[j]+5 ) { //room before is south
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 1000b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0100b;
- }
- else if ( otherpath[j-1] == otherpath[j]-1 ) { //room before is west
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0001b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0010b;
- }
- else if ( otherpath[j-1] == otherpath[j]+1 ) { //room before is east
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0010b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0001b;
- }
- if ( j == var )
- FloorsRoomContent[FR(otherpath[j], floor)] = FRContent_TypeA;
- }
- }
- //find "TypeB" path
- var = 0;
- for (j = 0; j < 20; j++) { //2 tiles try
- k = Rand(25);
- while(FloorsRoomContent[FR(k, floor)] == FRContent_Boss || FloorsRoomContent[FR(k, floor)] == FRContent_TypeA
- || FloorsRoomLayout[FR(k, floor)] == 0)
- k = Rand(25);
- for (l = 0; l < 5; l++)
- otherpath[l] = -1;
- for (l = 0; l < 25; l++)
- pathingvisited[l] = 0;
- otherpath[1] = k;
- l = 1;
- while(l > 0){
- otherpath[l+1] = ChoosePath(otherpath[l], floor, pathingvisited, false);
- if ( otherpath[l+1] != -1 ) {
- l ++;
- pathingvisited[otherpath[l]] = 1;
- }
- else
- l = 0; //exit loop
- if ( l == 3 ) {
- var = 3;
- j = 20; //exit loop
- l = 0; //exit loop
- }
- }
- }
- if ( var == 0 ) {
- for (j = 0; j < 5; j++)
- otherpath[j] = -1;
- for (j = 0; j < 25; j++)
- pathingvisited[j] = 0;
- while(var == 0){ //1 tile bruteforce
- j = Rand(25);
- while(FloorsRoomContent[FR(j, floor)] == FRContent_Boss || FloorsRoomContent[FR(j, floor)] == FRContent_TypeA
- || FloorsRoomLayout[FR(j, floor)] == 0)
- j = Rand(25);
- otherpath[1] = j;
- otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
- if ( otherpath[2] != -1 )
- var = 2;
- }
- }
- //create "TypeB" path from stack
- for (j = 2; j <= var; j++) {
- FloorsRoomLayout[FR(otherpath[j], floor)] = 14;
- if ( otherpath[j-1] == otherpath[j]-5 ) { //room before is north
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0100b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 1000b;
- }
- else if ( otherpath[j-1] == otherpath[j]+5 ) { //room before is south
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 1000b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0100b;
- }
- else if ( otherpath[j-1] == otherpath[j]-1 ) { //room before is west
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0001b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0010b;
- }
- else if ( otherpath[j-1] == otherpath[j]+1 ) { //room before is east
- FloorsRoomDoors[FR(otherpath[j-1], floor)] |= 0010b;
- FloorsRoomDoors[FR(otherpath[j], floor)] |= 0001b;
- }
- if ( j == var )
- FloorsRoomContent[FR(otherpath[j], floor)] = FRContent_TypeB;
- }
- //find random rooms
- for (i = 0; i < 50; i++) {
- k = Rand(25);
- while(FloorsRoomContent[FR(k, floor)] == FRContent_Boss || FloorsRoomContent[FR(k, floor)] == FRContent_TypeA
- || FloorsRoomLayout[FR(k, floor)] == 0)
- k = Rand(25);
- for (l = 0; l < 5; l++)
- otherpath[l] = -1;
- for (l = 0; l < 25; l++)
- pathingvisited[l] = 0;
- otherpath[1] = k;
- otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, false);
- if ( otherpath[2] != -1 ) {
- //create room
- FloorsRoomLayout[FR(otherpath[2], floor)] = 14;
- if ( otherpath[1] == otherpath[2]-5 ) { //room before is north
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0100b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 1000b;
- }
- else if ( otherpath[1] == otherpath[2]+5 ) { //room before is south
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 1000b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0100b;
- }
- else if ( otherpath[1] == otherpath[2]-1 ) { //room before is west
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0001b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0010b;
- }
- else if ( otherpath[1] == otherpath[2]+1 ) { //room before is east
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0010b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0001b;
- }
- }
- }
- //create final rooms
- for (i = 0; i < 25; i++)
- rngstuff[i] = -1;
- var = 0;
- for (i = 0; i < 25; i++) {
- if ( FloorsRoomLayout[FR(i, floor)] == 0 ) {
- rngstuff[i] = Rand(250);
- var ++;
- }
- }
- while(var > 0){
- for (i = 0; i < 250; i++) {
- for (j = 0; j < 25; j++) {
- if ( i == rngstuff[j] ) {
- for (k = 0; k < 5; k++)
- otherpath[k] = -1;
- otherpath[1] = j;
- otherpath[2] = ChoosePath(otherpath[1], floor, pathingvisited, true);
- if ( otherpath[2] != -1 ) {
- FloorsRoomLayout[FR(otherpath[1], floor)] = 14;
- if ( otherpath[1] == otherpath[2]-5 ) { //room before is north
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0100b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 1000b;
- }
- else if ( otherpath[1] == otherpath[2]+5 ) { //room before is south
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 1000b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0100b;
- }
- else if ( otherpath[1] == otherpath[2]-1 ) { //room before is west
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0001b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0010b;
- }
- else if ( otherpath[1] == otherpath[2]+1 ) { //room before is east
- FloorsRoomDoors[FR(otherpath[1], floor)] |= 0010b;
- FloorsRoomDoors[FR(otherpath[2], floor)] |= 0001b;
- }
- var --; //exit loop when floor is filled
- }
- }
- }
- }
- }
- }
- }
- int FR(int room, int floor){
- return room + ((floor-1) * 25);
- }
- int NorthRoomIndex(int i){
- if ( i < 5 )
- return -1;
- return i-5;
- }
- int SouthRoomIndex(int i){
- if ( i > 19 )
- return -1;
- return i+5;
- }
- int WestRoomIndex(int i){
- if ( i == 0 || i == 5 || i == 10 || i == 15 || i == 20 )
- return -1;
- return i-1;
- }
- int EastRoomIndex(int i){
- if ( i == 4 || i == 9 || i == 14 || i == 19 || i == 24 )
- return -1;
- return i+1;
- }
- int ChoosePath(int room, int floor, int pathingvisited, bool finalrooms){
- int pathvars[11]; //pathoptions up/down/left/right, iteration, index, numoptions, pathoptions a/b/c/d
- pathvars[0] = -1; pathvars[1] = -1; pathvars[2] = -1; pathvars[3] = -1; pathvars[6] = 0;
- for (pathvars[4] = 0; pathvars[4] < 4; pathvars[4]++) { //check all directions for options
- if ( pathvars[4] == 0 )
- pathvars[5] = NorthRoomIndex(room);
- else if ( pathvars[4] == 1 )
- pathvars[5] = SouthRoomIndex(room);
- else if ( pathvars[4] == 2 )
- pathvars[5] = WestRoomIndex(room);
- else
- pathvars[5] = EastRoomIndex(room);
- if ( pathvars[5] != -1 ) {
- if ( !finalrooms ) {
- if ( FloorsRoomLayout[FR(pathvars[5], floor)] == 0 ) {
- if ( pathingvisited[pathvars[5]] == 0 ) {
- pathvars[pathvars[4]] = pathvars[5];
- pathvars[6] ++;
- }
- }
- }
- else {
- if ( FloorsRoomLayout[FR(pathvars[5], floor)] > 0 ) {
- if ( FloorsRoomContent[FR(pathvars[5], floor)] != FRContent_Boss ) {
- pathvars[pathvars[4]] = pathvars[5];
- pathvars[6] ++;
- }
- }
- }
- }
- }
- if ( pathvars[6] == 0 )
- return -1;
- for (pathvars[4] = 0; pathvars[4] < pathvars[6]; pathvars[4]++) { //put options into a/b/c/d
- if ( pathvars[0] != -1 ) {
- pathvars[7+pathvars[4]] = pathvars[0];
- pathvars[0] = -1;
- }
- else if ( pathvars[1] != -1 ) {
- pathvars[7+pathvars[4]] = pathvars[1];
- pathvars[1] = -1;
- }
- else if ( pathvars[2] != -1 ) {
- pathvars[7+pathvars[4]] = pathvars[2];
- pathvars[2] = -1;
- }
- else if ( pathvars[3] != -1 ) {
- pathvars[7+pathvars[4]] = pathvars[3];
- pathvars[3] = -1;
- }
- }
- pathvars[5] = Rand(pathvars[6]);
- return pathvars[7+pathvars[5]];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement