0

I am trying to make a dungeon generator for a roguelike, using a BSP like the one described here: Basic BSP

I am a bit new to tree structures, and I have been working on this for a while now and really can't seem to figure out where I am going wrong. For now I have it so it will just print out all "#"s to represent the room dimensions of each leaf node so I can see if it is working. I keep getting segmentation faults and I am really not sure where I am going wrong at this point. If anyone could take a look at my code and give me some advice I would greatly appreciate it. Thank you!

#include <ncurses.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define MIN_ROOM_HEIGHT 5
#define MIN_ROOM_WIDTH 10


struct tree 
{
    int max_x, min_x;
    int max_y, min_y;

    struct tree *Right_Down, *Left_Up;

}; 

typedef struct tree room;


room create_rooms(room **map, WINDOW *board, int max_x, int min_x, int    max_y, int min_y)
{
    room *temp = NULL;

    if(!(*map))
    {
        temp = (room *)malloc(sizeof(room));
        temp->Left_Up = temp->Right_Down = NULL;
        (temp)->max_x = max_x;
        (temp)->min_x = min_x;
        (temp)->max_y = max_y;
        (temp)->min_y = min_y;
        *map = temp;
    }
    /* room structures for smaller rooms this room will be
     * split into, Left/Up or Right/Down, depending on whether
     * horizontal or vertical split was chosen. 
     */
    // room newLeft_Up, newRight_Down;

    /* Position to split at for horizontal and vertical */
    int split_horiz, split_vert;

    /* Divide room into 2 smaller rooms, unless doing so 
     * would make them below minimum room size
     */
    int split_type = rand() % 2;

    int stop = 0;

    /* 1 is Horizontal Split */
    if(split_type == 1)
    {
        if((*map)->max_y > MIN_ROOM_HEIGHT+4)
        {

            while(stop == 0)
            {
                if((split_horiz = rand() % ((*map)->max_y-2)) < MIN_ROOM_HEIGHT+4);
                {
                    stop = 1;
                }
            }

                create_rooms(&(*map)->Left_Up, board, (*map)->max_x, (*map)->min_x, split_horiz-1, (*map)->min_y);

                create_rooms(&(*map)->Right_Down, board, (*map)->max_x, (*map)->min_x, (*map)->max_y, split_horiz+1);
        }

    }
    /* 2 is Vertical Split */
    else
    {

        if((*map)->max_x > MIN_ROOM_WIDTH+4)
        {

            while(stop == 0)
            {
                if((split_vert = rand() % ((*map)->max_x-2)) < MIN_ROOM_WIDTH+4);
                {
                    stop = 1;
                }
            }

                create_rooms(&(*map)->Left_Up, board, split_vert-1, (*map)->min_x, (*map)->max_y, (*map)->min_y);


                create_rooms(&(*map)->Right_Down, board, (*map)->max_x, split_vert+1, (*map)->max_y, (*map)->min_y);



        }

    }

    char tiles[(*map)->max_y-1] [(*map)->max_x-1];
    int i, j;

    /* Print room tiles if this is a leaf node */
    if((!((*map)->Left_Up)) && (!((*map)->Right_Down)))
    {
        for(i=(*map)->min_y; i < (*map)->max_y-1; i++)
        {
            for(j=(*map)->min_x; j < (*map)->max_x-1; j++)
            {
                tiles[i][j] = '#';
                wmove(board, i, j);
                wprintw(board, "#");
            }
        }
    }
    wrefresh(board);


}

int main()
{
    /* ncurses initializations */
    initscr();
    start_color();
    keypad(stdscr, TRUE);
    refresh();
    noecho();


    /* counters */
    int i, j;


    /* seed srand for later use */
    srand(time(NULL));

    /* Initialize Game Board */
    WINDOW *board;

    board = newwin(LINES, COLS, 0, 0);
    box(board, 0, 0);


    room *map;

    map = NULL;

    create_rooms(&map, board, COLS-1, 1, LINES-1, 1);


    wrefresh(board);

    /* sleep for 5 secs */
    sleep(5);

    /* close ncurses */
    endwin();

    return 0;
}
econsteve
  • 55
  • 1
  • 1
  • 4

0 Answers0