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.