Level generator bug

I have been working on a level generation script. Ive gotten almost all of the bugs out with alot of help from the community. Ive got one last bug, the level isn’t generating… Somewhere in the red code my checks are returning false but i cant find it.

//setup the variables.
var rows = 100;
var cols = 100;
var dungeon = new int[cols,rows];
var oldcol:int;
var oldrow:int;
var armlength:int;
var roomheight;
var roomwidth;
var newcol:int;
var newrow:int;

// set up the constants
var maxroomheight:int=7;
var minroomheight:int=3;
var maxroomwidth:int=7;
var minroomwidth:int=3;
var hallway : Transform;
var wall : Transform;


// Different variables in the dungeon array mean different things.
//  0 is occupied by a wall,  1 is open space and 2 is a node for the creation of new hallways.

function Start()
{
	//Clear the dungeon. 
	for(d=0; d<cols; d++)
	{
		for(e=0; e<rows; e++)
		{
			dungeon[d,e]=0;
		}
	}
	
	
	//pick the seed tile for the dungeon and remember it as the start position;
	oldcol=Random.Range(1,cols);
	oldrow=Random.Range(1,rows);
	newcol=oldcol;
	newrow=oldrow;
	dungeon[oldcol,oldrow]=2;

	//place the player at the start position. 
	transform.Translate(oldcol*5,1,oldrow*5);
	
	//Find a tile thats been flagged as a node.
[COLOR="red"]	for(d=0; d<cols; d++)
	{
		for(e=0; e<rows; e++)
		{
			if (dungeon[d,e]==2)
			{
				oldcol=d;
				oldrow=e;
				buildarm(oldcol,oldrow);
			}
		}
	}[/COLOR]
	

	
	//build the dungeon from cubes and hallway objects. 
	for (x=0; x<cols; x++)
	{
		for(z=0; z<rows; z++)
		{
			if (dungeon[x,z]>0)
			{
				Instantiate (hallway, Vector3(x*5, 0, z*5), Quaternion.identity);
			}
			else
			{
				Instantiate (wall, Vector3(x*5,0, z*5), Quaternion.identity);
			}
		}
	}



}



[COLOR="red"]
function buildarm(oldcol,oldrow)
{

	if((oldcol+1)<cols)
	{
		if((dungeon[(oldcol+1),oldrow])==2)
		{
			if ((Random.Range(0,1))==1)
			{
				armlength=(Random.Range(5,15));
				for(d=0; d<armlength; d++)
				{
					if ((oldcol+d)<=cols)
					{
						if ((oldcol+d)>0)
						{
							dungeon[(oldcol+d),oldrow]=1;
				
							if (d==armlength)
							{
								//generate a room
								if ((Random.Range(0,1))==1)
								{
									//Turns the room height/width into odd integers. 
									roomheight=(Mathf.Round(((Random.Range(minroomheight,maxroomheight))+0.5))-1);  
									roomwidth=(Mathf.Round(((Random.Range(minroomwidth,maxroomwidth))+0.5))-1);
									for(f=0; f<roomwidth; f++)
									{
										for (g=0; g<roomheight; g++)
										{
											dungeon[(oldcol-((roomwidth/2)-0.5))+f,(oldrow-((roomheight/2)-0.5))+g]=1;
										}
									}	
								}
								else // or not
								{
									dungeon[(oldcol+d),oldrow]=2;
								}
							}
						}
					}
				}
			}	
		}
	}

	
	

		if ((oldrow+1)<rows)
		{
			if((dungeon[oldcol,(oldrow+1)])==2)
			{
				if ((Random.Range(0,1))==1)
				{
					armlength=(Random.Range(5,15));
					for(d=0; d<armlength; d++)
					{
						if ((oldrow+d)<=rows)
						{
						if ((oldrow+d)>0)
						{
							dungeon[oldcol,(oldrow+d)]=1;
				
							if (d==armlength)
							{
								//generate a room
								if ((Random.Range(0,1))==1)
								{
									//Turns the room height/width into odd integers. 
									roomheight=(Mathf.Round(((Random.Range(minroomheight,maxroomheight))+0.5))-1);  
									roomwidth=(Mathf.Round(((Random.Range(minroomwidth,maxroomwidth))+0.5))-1);
									for(f=0; f<roomwidth; f++)
									{
										for (g=0; g<roomheight; g++)
										{
											dungeon[(oldcol-((roomwidth/2)-0.5))+f,(oldrow-((roomheight/2)-0.5))+g]=1;
										}
									}
								}
									else // or not
								{
									dungeon[oldcol,(oldrow+d)]=2;
								}
									
								}
							}
						}
					}
				}
			}
		}
	
	
	if ((oldcol-1)>=0)
	{
		if((dungeon[(oldcol-1),oldrow])==2)
		{
			if ((Random.Range(0,1))==1)
			{
				armlength=(Random.Range(5,15));
				for(d=0; d<armlength; d++)
				{
					if ((oldcol-d )>=0)
					{
						if ((oldcol-d)>0)
						{
							dungeon[(oldcol-d),oldrow]=1;
				
							if (d==armlength)
							{
								//generate a room
								if ((Random.Range(0,1))==1)
								{
									//Turns the room height/width into odd integers. 
									roomheight=(Mathf.Round(((Random.Range(minroomheight,maxroomheight))+0.5))-1);  
									roomwidth=(Mathf.Round(((Random.Range(minroomwidth,maxroomwidth))+0.5))-1);
									for(f=0; f<roomwidth; f++)
									{
										for (g=0; g<roomheight; g++)
										{
										dungeon[(oldcol-((roomwidth/2)-0.5))+f,(oldrow-((roomheight/2)-0.5))+g]=1;
										}
									}
								}
								else // or not
								{
									dungeon[(oldcol-d),oldrow]=2;
								}
							}
						}
					}
				}
			}
		}
	}
	
	
	if((oldrow-1)>0)
	{
		if((dungeon[oldcol,(oldrow-1)])==2)
		{
			if ((Random.Range(0,1))==1)
			{
				armlength=(Random.Range(5,15));
				for(d=0; d<armlength; d++)
				{
					if ((oldrow-1)>0)
					{
						dungeon[oldcol,(oldrow-1)]=1;
				
						if (d==armlength)
						{
							//generate a room
							if ((Random.Range(0,1))==1)
							{
								//Turns the room height/width into odd integers. 
								roomheight=(Mathf.Round(((Random.Range(minroomheight,maxroomheight))+0.5))-1);  
								roomwidth=(Mathf.Round(((Random.Range(minroomwidth,maxroomwidth))+0.5))-1);
								for(f=0; f<roomwidth; f++)
								{
									for (g=0; g<roomheight; g++)
									{
										dungeon[(oldcol-((roomwidth/2)-0.5))+f,(oldrow-((roomheight/2)-0.5))+g]=1;
									}
								}
							}
							else // or not
							{
								dungeon[oldcol,(oldrow-d)]=2;
							}
						}
					}
				}
			}
		}
	}
	
}[/COLOR]



//    How to build the procedurally generated terrain.
//    #1 pick a seed tile.
//    #2 Check each direction and randomly decide if your going to go out from there. If so mark it as a node tile.
//    #3  pick a node tile and generate a random length hallway off of it then unflag the node tile so it isnt picked again.
//    #4  Randomly decide if you want a room at the end of the hallway, if so set the center of the room as a node tile. (The room would have to be an odd number in each direction. Decide from that node tile if you want to move in any direction. 
//    #5 repeat with reducing chance untill all node tiles have been used or the map is big enough.

Oh! And feel free to use the level generator for anything you want. If your going to use it you need to plug in a wall/hallway prefab.

So I am looking at it… and putting in some checks as I see it could be… and I run accross one line that spells disaster for me…

if ((Random.Range(0,1))==1)

Ordinarily, you would think, ok, it is either 0 or 1, but such is never the case. Random.Range produces a number between start and end, and of the same type as the greatest type of what you are using. So if you use 2 ints, it returns an int. One float, would return a float.

So to test this, I wrote a simple script to give me values…

for(i=0; i<100; i++){
	Debug.Log(Random.Range(0,1));
}

Low and behold, it always returns 0

So, my suggestion is to change your code…

if ((Random.Range(0.0,1.0))>0.5)

Yes. From the docs about the version of Random.Range that takes integers:

Noe that the second parameter is exclusive. So indeed Random.Range(0, 1) always returns 0. If you want to get either 0 or 1, use Random.Range(0, 2). This returns an integer number between 0 and 2, including 0 and excluding 2.

Thanks! I never would have found that! Let me change that quick then re-upload the code!

Edit: Still having the bug, time to comb through the checks again and find the problem!

Could be simpler: if (Random.value > 0.5)

–Eric

Fixed it! Once again thanks everyone! The problem code was “if((dungeon[oldcol,(oldrow+1)])==2)” i was checking for a node when i was branching off (In other words carving out new areas where there were walls before). I love that Aha! moment when you fix a bug caused by a stupid little error on my part :smile:

Ill upload the finished level generator once i finish fleshing it out and making it look good.