• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TylerHum

Random Level Builder Help

6 posts in this topic

Ok I'm trying to make a Level builder that'll build rooms and halls but its become rather hit and miss and I'm stuck on it. For one the checker that makes sure it wont build over itself only fires when it wants it seems and two it can go from putting completely random garbage all over to getting stuck in infinite loops.

Build Level:
[code]#include "BL.h"

bool CollisionCheck(Tile LevelCurrent[100][100],int x,int y,int width,int height)
{
int check=0;
bool fin;
for(y; y<height; y++)
{
for(x; x<width; x++)
{
if(LevelCurrent[x][y].walkable==1)
check++;
}
}
if(check>0)
fin=false;
else if(check==0)
fin=true;
return fin;
}

void BuildLevel(Tile LevelCurrent[100][100],int stair_x,int stair_y)
{
bool finish=false,cont=false;
int x,y,x2,y2,type,length,roll,counter;
type=4;

for(y=0; y<100; y++)
{
for(x=0; x<100; x++)
{
LevelCurrent[x][y].walkable=0;
LevelCurrent[x][y].flag_build=0;
LevelCurrent[x][y].tries=0;
}
}

LevelCurrent[stair_x][stair_y].flag_build=1;
LevelCurrent[stair_x][stair_y].tries=5;


while(!finish)
{
/*
TYPES:
Halls= 0-3
Standard Rooms= 4-24
Uncommon Rooms= 25-45
Rare Rooms= 46-66
*/
type=rand()%5;
for(y=0; y<100; y++)
{
for(x=0; x<100; x++)
{

//===========HALLS============================================================================================//
//===UpHall============//
if((LevelCurrent[x][y].flag_build==1||LevelCurrent[x][y].flag_build==2)&&type==0)
{
length=rand()%20;
while(!CollisionCheck(LevelCurrent,x,y-length,1,length)&&y-length>4)
{
if(length<=4)
break;
length--;
}
if(length>4)
{
for(int i=0; i<length; i++)
{
LevelCurrent[x][y-i].walkable=1;
}
LevelCurrent[x][y].flag_build=0;
LevelCurrent[x][y-length].flag_build=1;
}
else{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries++;
else
LevelCurrent[x][y].flag_build=0;
}
}
//=====================//

//===RightHall=========//
if((LevelCurrent[x][y].flag_build==2)&&type==1)
{
length=rand()%20;
while(!CollisionCheck(LevelCurrent,x,y,length,1)&&x+length<92)
{
if(length<=4)
break;
length--;
}
if(length>4)
{
for(int i=0; i<length; i++)
{
LevelCurrent[x+i][y].walkable=1;
}
LevelCurrent[x][y].flag_build=0;
LevelCurrent[x+length][y].flag_build=1;
}
else{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries++;
else
LevelCurrent[x][y].flag_build=0;
}
}
//=====================//

//===DownHall==========//
if((LevelCurrent[x][y].flag_build==2)&&type==2)
{
length=rand()%20;
while(!CollisionCheck(LevelCurrent,x,y,1,length)&&y+length<92)
{
if(length<=4)
break;
length--;
}
if(length>4)
{
for(int i=0; i<length; i++)
{
LevelCurrent[x][y+i].walkable=1;
}
LevelCurrent[x][y].flag_build==0;
LevelCurrent[x][y+length].flag_build=1;
}
else{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries++;
else
LevelCurrent[x][y].flag_build=0;

}
}
//=====================//

//===LeftHall==========//
if((LevelCurrent[x][y].flag_build==2)&&type==3)
{
length=rand()%20;
while(!CollisionCheck(LevelCurrent,x-length,y,length,1)&&x-length>4)
{
if(length<=4)
break;
length--;
}
if(length>4)
{
for(int i=0; i<length; i++)
{
LevelCurrent[x-1][y].walkable=1;
}
LevelCurrent[x][y].flag_build=0;
LevelCurrent[x-length][y].flag_build=1;
}
else{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries++;
else
LevelCurrent[x][y].flag_build=0;
}
}
//=====================//
//============================================================================================================//

//===========COMMON_ROOMS=====================================================================================//

//Diamond - Medium - Type 4
if(LevelCurrent[x][y].flag_build==1&&type==4)
{
if(CollisionCheck(LevelCurrent,x-3,y-3,7,7))
{
//=Y-3========================//
LevelCurrent[x][y-3].walkable=1;
//============================//

//=Y-2========================//
LevelCurrent[x-1][y-2].walkable=1;
LevelCurrent[x][y-2].walkable=1;
LevelCurrent[x+1][y-2].walkable=1;
//============================//

//=Y-1========================//
LevelCurrent[x-2][y-1].walkable=1;
LevelCurrent[x-1][y-1].walkable=1;
LevelCurrent[x][y-1].walkable=1;
LevelCurrent[x+1][y-1].walkable=1;
LevelCurrent[x+2][y-1].walkable=1;
//============================//

//=Y+0========================//
LevelCurrent[x-3][y].walkable=1;
LevelCurrent[x-2][y].walkable=1;
LevelCurrent[x-1][y].walkable=1;
LevelCurrent[x][y].walkable=1;
LevelCurrent[x+1][y].walkable=1;
LevelCurrent[x+2][y].walkable=1;
LevelCurrent[x+3][y].walkable=1;
//============================//

//=Y+1========================//
LevelCurrent[x-2][y+1].walkable=1;
LevelCurrent[x-1][y+1].walkable=1;
LevelCurrent[x][y+1].walkable=1;
LevelCurrent[x+1][y+1].walkable=1;
LevelCurrent[x+2][y+1].walkable=1;
//============================//

//=Y+2========================//
LevelCurrent[x-1][y+2].walkable=1;
LevelCurrent[x][y+2].walkable=1;
LevelCurrent[x+1][y+2].walkable=1;
//============================//

//=Y+3========================//
LevelCurrent[x][y+3].walkable=1;
//============================//

//=Flags======================//
if(LevelCurrent[x][y+4].walkable==0&&y+4<92) LevelCurrent[x][y+3].flag_build=2;
if(LevelCurrent[x][y-4].walkable==0&&y-4>4) LevelCurrent[x][y-3].flag_build=2;
if(LevelCurrent[x+4][y].walkable==0&&x+4<92) LevelCurrent[x+3][y].flag_build=2;
if(LevelCurrent[x-4][y].walkable==0&&x-4>4) LevelCurrent[x-3][y].flag_build=2;
//============================//
//=Tries======================//
if(LevelCurrent[x][y+4].walkable==0&&y+4<92) LevelCurrent[x][y+3].tries=5;
if(LevelCurrent[x][y-4].walkable==0&&y-4>4) LevelCurrent[x][y-3].tries=5;
if(LevelCurrent[x+4][y].walkable==0&&x+4<92) LevelCurrent[x+3][y].tries=5;
if(LevelCurrent[x-4][y].walkable==0&&x-4>4) LevelCurrent[x-3][y].tries=5;
//============================//
LevelCurrent[x][y].tries=0;
}
/*else{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries++;
else
LevelCurrent[x][y].flag_build=0;
}*/
}

//============================================================================================================//

//===========RARE_ROOMS=======================================================================================//

//Hall Of Heroes - Type 46
if(type==46)
{

}
//============================================================================================================//
}
}
counter=0;
for(y2=0; y2<100; y2++)
{
for(x2=0; x2<100; x2++)
{
if(LevelCurrent[x2][y2].flag_build!=0)
counter++;
}
}
if(counter==0)
finish=true;
counter=0;
}
return;
}

[/code]

I'm at a complete lose at this point so any help would be greatly appreciated. Thanks.
0

Share this post


Link to post
Share on other sites
[quote name='PrestoChung' timestamp='1309492483' post='4829876']
Have you tried debugging it step-by-step?

Maybe adding some asserts would help pinpoint the problem(s).
[/quote]

Yea I've debugged if for a few hours now and I still cant find the issue.
0

Share this post


Link to post
Share on other sites
CollisionCheck() reports true if any walkable cell is in the portion of the map with coordinates between (x,y) and (width,height): it doesn't seem to match the call in BuildLevel(); parameter values are not checked; it assumes x<=width and y<=height; the parameters aren't, obviously, a width and height; you shouldn't iterate using x and y.
[source lang="cpp"]
bool CollisionCheck(const Tile** LevelCurrent, const int x1,const int y1,const int x2,const int y2,const int width,const int height)
{
int xmin,xmax,ymin,ymax;//iteration limits
if(x1>x2)
{
xmin=x2;
xmax=x1;
}
else
{
xmin=x1;
xmax=x2;
}
if(y1>y2)
{
ymin=y2;
ymax=y1;
}
else
{
ymin=y1;
ymax=y2;
}

if (xmin<0) xmin=0;
if (ymin<0) ymin=0;
if (xmax>width) xmax=width; if (ymax>height) ymax=height;
for(int y=ymin; y<ymax; y++)
{
for(int x=xmin; x<xmax; x++)
{
if(LevelCurrent[x][y].walkable==1)//should probably be a bool, not an integer
//if(LevelCurrent[x][y].walkable)

return false;
}
}
return true;
}

[/source]
2

Share this post


Link to post
Share on other sites
Ok I got the issues with the checking function (Thanks Lorenzo) and they now work fine with building the rooms but the hallways still build over everything as if nothing is there.


Here's how it does hallways moving up. All the other hall builders are basically the same as this.
[code]if((LevelCurrent[x][y].flag_build==1||LevelCurrent[x][y].flag_build==2)&&type==0)
{
length=rand()%10;
while(!cont||y-length<4||!CollisionCheck(LevelCurrent,x,y-length,x,y))
{
length--;
if(length<=4)
cont=true;
}
if(length>4&&y>4&&LevelCurrent[x][y].direction!=2)
{
for(int i=0; i<length; i++)
{
LevelCurrent[x][y-i].walkable=1;
}
LevelCurrent[x][y].flag_build=0;
LevelCurrent[x][y-length].flag_build=1;
LevelCurrent[x][y-length].tries=5;
LevelCurrent[x][y-length].direction=0;
}
else
{
if(LevelCurrent[x][y].tries>0)
LevelCurrent[x][y].tries--;
else if(LevelCurrent[x][y].tries==0)
LevelCurrent[x][y].flag_build=0;
}
}[/code]

I'm starting to think I'm doing halls all wrong to be honest. Any input would be great.

Edit: Also if I set the compiler to Release instead of Debug (MS VC++ 2010) and set a break in the collision check function it claims it will never be hit.
0

Share this post


Link to post
Share on other sites
Don't debug Release builds unless you're trying to reproduce an error that only happens in Release. Debugging Release builds is a tedious process.

Have you considered putting some output into this function? For some processes, debugging step-by-step is too fine grained to figure out what is going wrong. Instead, if you printed a representation of the level at the start of each loop (possibly pausing the generation in the process), you'd be clearly able to see what was happening. Once you figure out where it is going wrong, you can use the debugger to find out why.

Your function is very big. One thing that might help narrow it down is to split it into several smaller functions.

A different idea is to build the rooms first and then create the tiled level from it. So you might build a set of objects which represent the topology of the level in terms of rooms, corridors, etc. Then use a second pass to turn that into a tile map.
1

Share this post


Link to post
Share on other sites
[s] Ah I've discovered the source of the issue! My only issue now is that I don't know how to actually fix it.

CollisionCheck snippet:
[code]
for(int y=x1; y<y2; y++)
{
for(int x=x1; x<x2; x++)
{
if(LevelCurrent[x][y].walkable==1)
return false;
}
}
return true;
[/code]

The issue is that since the halls are 1 tile in width whenever it goes through y<y2 or x<x2 its equal to it so it just passes by without being checked.

[code]
y=y1;
x=x1;

while(y<=y2)
{
while(x<=x2)
{
if(LevelCurrent[x][y].walkable==1)
return false;
x++;
}
y++;
}
return true;
[/code]
^ I tried doing this so it wouldnt just pass on and the only thing it would do after that was build halls straight up for some odd reason.

[/s]Ya know what disregard all of that I completely forgot about do while loops and it's now working perfectly. Thank you all for your help.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0