Need some help getting my Dungeon generator to work right
Hi All,
i am working on a Dungeon Generator for a roguelike that i am programming and i am having trouble with it because the rooms that should be carved out of the walls are spawning over each other.
I have been trying to fix this now for 2 days and haven't gotten anyware close of solving my Problem.
Maybe someone here has an idea how to help me :-)
To make things a bit easier i will first explain in word what it should do:
1. firstly i get me a random position X and Y
2. i generate a random length and width for the room size
3. i add the length and width to the random X and y to get the maximal X and Y position in the map
4. Now since i have these four positions i can make a square in my map with the corners X/Y , max X/Y, X/ max Y, max X/ Max Y.
5. I check once around this square to see that all is walls and if this is the case the room should be generated
So and now here my code:
Code:
#include <iostream>
#include <fstream>
#include "cstdlib"
#include <ctime>
#include <stdio.h>
#include <termios.h>
#include <string>
using namespace std;
int main()
{
const int mapx =22;
const int mapy =82;//69;
const int mapz =10;
char map[mapx][mapy]=
{
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################"
};
srand(time(NULL));
// maximum of rooms
const unsigned int maxRooms=8;
//Variable die hochgezählt wird bis maximale zimmmer erreicht sind
unsigned int roomCounter=0;
char floor='a';
char door='+';
char wall='#';
//length of room
int length2;
//the random position x and y
int randomPosy;
int randomPosx;
//the width of the room
int width;
//the maximum position of x and y in the map for the room
int maxrandomPosy;
int maxrandomPosx;
//help variables for checking around the room
int checker1=0;
bool check1=false;
int checker2=0;
int check2=false;
int checker3=0;
bool check3=false;
int checker4=0;
bool check4=false;
while( roomCounter < maxRooms )
{
// this is only to help show how sometimes the rooms overlap
if(roomCounter == 0)
{
floor = '1';
}
if( roomCounter == 1)
{
floor = '2';
}
if(roomCounter == 2)
{
floor = '3';
}
if( roomCounter == 3)
{
floor = '4' ;
}
if( roomCounter == 4 )
{
floor = '5' ;
}
if( roomCounter == 5 )
{
floor = '6' ;
}
if( roomCounter == 6 )
{
floor = '7' ;
}
if( roomCounter == 7 )
{
floor = '8' ;
}
do
{
length2 = rand()% 5 + 5;
randomPosy = 1+rand()% 65;
randomPosx = 1+rand()% 14;
width = rand()%3+4;
maxrandomPosy = randomPosy+length2 ;
maxrandomPosx=randomPosx+width;
}
while(map[randomPosx][randomPosy]!=wall && map[randomPosx][maxrandomPosy]!=wall && map[maxrandomPosx][randomPosy]!=wall && map[maxrandomPosx][maxrandomPosy]!=wall);
//now i check around where the room should then be
//this is the check for above the room
for(int a = randomPosy ; a < maxrandomPosy + 1 ; a++)
{
if(map[ randomPosx-1 ][a]==wall)
{
//when theres a wall then it should count up
++checker1;
}
}
// Wenn nun der Zähler die gleiche Anzahl an Stellen mit Mauer hat wie die width (ja hätte da die width überprüfen können anstatt das so kompliziert zu machen)
// dann wird der boolsche checker zu true gesetzt
if(checker1==maxrandomPosy-randomPosy+1)
{
check1=true;
}
// das gleiche Schema wie oben nur hier die rechte Seite des 4-Ecks
for(int b=randomPosx ; b < maxrandomPosx + 1 ; b++ )
{
if(map[b][ maxrandomPosy + 1 ]==wall)
{
++checker2;
}
}
if(checker2 == maxrandomPosx-randomPosx+1 )
{
check2 = true;
}
for(int c = randomPosy ; c < maxrandomPosy + 1 ; c++ )
{
if(map[ maxrandomPosx + 1][c] == wall )
{
++checker3;
}
}
if(checker3 == maxrandomPosy-randomPosy+1 )
{
check3=true;
}
for(int d=randomPosx ; d < maxrandomPosx + 1; d++ )
{
if(map[d][randomPosy-1] == wall )
{
++checker4;
}
}
if(checker4 == maxrandomPosx - randomPosx + 1 )
{
check4=true;
}
// Nun wenn um das 4-Eck alles Mauern sind und die Ecken des 4-ecks etc. Mauern sind wird der Raum ausgehöhlt
if( check1 == true && check2 == true && check3 == true && check4 == true )
{
if(map[randomPosx][randomPosy]==wall && map[maxrandomPosx][randomPosy]==wall && map[randomPosx][maxrandomPosy]==wall && map[maxrandomPosx][maxrandomPosy]==wall)
{
if(map[randomPosx-1][randomPosy]==wall && map[maxrandomPosx+1][randomPosy]==wall && map[randomPosx-1][maxrandomPosy]==wall && map[maxrandomPosx+1][maxrandomPosy]==wall)
{
if(map[randomPosx][randomPosy-1]==wall && map[maxrandomPosx][randomPosy-1]==wall && map[randomPosx][maxrandomPosy+1]==wall && map[maxrandomPosx][maxrandomPosy+1]==wall)
{
//Nun wird der Raum ausgehöhlt
while(randomPosy<maxrandomPosy )
{
for(int c=randomPosx; c< maxrandomPosx; c++)
{
map[c][randomPosy]=floor;
}
randomPosy++;
}
//wenn ein raum ausgehöhlt wurde die zähler variable um 1 addieren
++roomCounter;
//print map
for(int m=0; m<mapx; m++)
{
cout <<map[m]<<endl;
}
cout<<endl;
}
}
}
}
}
return 0;
}
This shows my Problem very nice how rooms get spawned over each other:
Code:
################################################################################
################################################################################
#################################################################11111##########
#################################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444####################################################################
########################################55555###################################
#############################66666######55555###################################
#############################66666######55555######3333333######################
#############################66666######55555######3333333######################
########222222###############66666######55555######3333333######################
########222222###############66666######55555######3333333######################
########222222###############66666##############################################
########222222##################################################################
########222222##################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
#################################################################11111##########
#################################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444#####################################################11111##########
####44444444####################################################################
###########################7777777######55555###################################
###########################7777777######55555###################################
###########################7777777######55555######3333333######################
###########################7777777######55555######3333333######################
########222222#############7777777######55555######3333333######################
########222222###############66666######55555######3333333######################
########222222###############66666##############################################
########222222##################################################################
########222222##################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
################################################################################
###############################################888888############11111##########
###############################################888888############11111##########
####44444444###################################888888############11111##########
####44444444###################################888888############11111##########
####44444444###################################888888############11111##########
####44444444#####################################################11111##########
####44444444####################################################################
###########################7777777######55555###################################
###########################7777777######55555###################################
###########################7777777######55555######3333333######################
###########################7777777######55555######3333333######################
########222222#############7777777######55555######3333333######################
########222222###############66666######55555######3333333######################
########222222###############66666##############################################
########222222##################################################################
########222222##################################################################
################################################################################
################################################################################
################################################################################
################################################################################
Sorry for my bad English :-) I hope someone understands what i am trying to do and can help me with this