## Tuesday, 19 March 2013

### Branches!

I'm now on my Easter 'break' which means ill be spending a lot of time on my team project! Incidently this means there is going to be more blogs/longer blogs :)

Anyway back to business! I have a random dungeon path generator that recognises how many rooms are around it and knows where those adjacent rooms are. That's cool but one path? that's pretty... well... linear and often times linear equals boring. So let's add some branches to the path giving the player some options when traversing the dungeon. What those options are can be decided at a later date.

To crate a branch to my dungeon the code is going to be very similar to that of the main path, however the starting location will be a room that already exists along the main path. To do this I generate a random number between 0 and the length of the path which will be represented by roomCount.

Random.Range(0,roomCount);

I will then take the co-ordinates of a room that have this random number as an index within the array. This is our starting room. After the first room is found the code for making a branch is almost identical to that of drawing the main path with some altered variable names.

uhh, maybe there needs to be some other changes... So I introduced some new public and private variables;

public bool branches : branches is used to decide if the path should have branches via an if statement.
private int branchCount : branchCount is used to count the rooms created that are represent branches, if added to roomCount will produce the index of that room in the array.
public int branchNumber : branchNumber adjusts the number of branches attached to the path
public int branchLength : branchLength adjusts the number of rooms in a branch.

The problem in the previous picture is that there was nothing telling the branches when to stop.

Using branchNumber and branchLength I'm defining  a stopping point for branch creation.

So my branches are working somewhat but I can instantly see where the issue is. Branches crossing over each other and branches crossing with the main path cause my adjacent calculations to exceed 4 rooms and currently there is no option if a room has 5 or more adjacent rooms. To fix this I either have to give options for having 5 or more adjacent rooms, remove the unnecessary additional adjacent rooms or just stop looking for rooms if I have 4 already. So naturally I did all of them just to be safe. :D

I changed the 4 room if statement to

if(adjacentCount >= 4) { Instantiate 4 Door Prefab }

That way if there are somehow 5 or more adjacent rooms it will just place 4 doors.

I then introduced some booleans

bool N
bool S
bool E
bool W

If a room has been found at any of the respective positions, the boolean value will change. If one of the boolean values have changed another room at that location cannot increment adjacentCount.

if(Yarr[j] == roomY+1 & Xarr[j] == roomX & E == false) { adjacentCount++; E = true; }

Lastly I want to remove rooms that have the same value as other rooms. To do this I go through the array with one for loop:

for(int i = 0; i <= roomCount+branchCount; i++)

I then go through the array again inside that loop using another for loop:

for(int i = 0; i <= roomCount+branchCount; i++)
{
for(int j = 0; j <= roomCount+branchCount; j++)
}

using the If statement:

if(Xarr[j] == roomX & Yarr[j] == roomY & j != i)

I set the X and Y value of the room being compared to -10. When it comes to drawing rooms if a room has -10 X and or -10 Y, just don't draw it.

for(int i = 0; i <= roomCount+branchCount; i++)
{
roomX = Xarr[i];
roomY = Yarr[i];
for(int j = 0; j <= roomCount+branchCount; j++)
{
if(Xarr[j] == roomX & Yarr[j] == roomY & j != i)
{
Xarr[j] = -10;
Yarr[j] = -10;
}
}
}

The Final Product :

There were some other issues that were down to typos in the code that didn't come out with compiler errors that are now fixed :)