Hi, I'm in the process of writing a roguelike (ex Nethack). It's not big, but should still be playable. I've gotten to level generation, and am basically just 'carving' the rooms out of a blank slate.
The entire floor starts off as an array of rock (0x00), with passable rooms/hallways being open space (0x01). Basically, mapgen::createM ap() generates a room and hallway MAXROOMS times. It makes sure that nothing overlaps or goes out of the bounds of the array (which is MAXX by MAXY). It compiles fine, but as soon as I call createMap, it spouts the error "Segmentati on fault (core dumped)". Does anybody have any ideas as to what is going wrong?
I'm compiling using g++ 1.4.2 (Ubuntu).
The entire floor starts off as an array of rock (0x00), with passable rooms/hallways being open space (0x01). Basically, mapgen::createM ap() generates a room and hallway MAXROOMS times. It makes sure that nothing overlaps or goes out of the bounds of the array (which is MAXX by MAXY). It compiles fine, but as soon as I call createMap, it spouts the error "Segmentati on fault (core dumped)". Does anybody have any ideas as to what is going wrong?
I'm compiling using g++ 1.4.2 (Ubuntu).
Code:
#ifndef MAPGEN_H #define MAPGEN_H #include "dice.h" // Has the header with rand() in it... just the library version namespace mapgen { const int MAXX = 80; // Change to change map sizes const int MAXY = 22; // " " const int MAXROOMS = 6; const int MAXROOMX = 5; const int MAXROOMY = 5; const int MAXHALLLENGTH = 10; int map[MAXX][MAXY]; // Initialize map int makeHall(int atx, int aty, int dir) { // Directions: // 1 // 2 0 // 3 int length = (rand() % MAXHALLLENGTH) + 1; bool clean = true; // Checks for size if ((length + atx) > MAXX || (atx - length) < 0 || (aty - length) < 0 || (length + aty) > MAXY) { clean = false; } if (clean) { if (dir == 0) { // East for (int i = atx; i <= length; i++) { map[i][aty] = 0x01; } } else if (dir == 1) { // North for (int i = aty; i <= length; i--) { map[atx][i] = 0x01; } } else if (dir == 2) { // West for (int i = atx; i <= length; i--) { map[i][aty] = 0x01; } } else { // South for (int i = aty; i <= length; i++) { map[atx][i] = 0x01; } } } return 0; } void makeRoom() { int xa, xb, ya, yb; // Rectangle Coords: /* (xa, ya) +---------+ * | | * | | * +---------+ (xb, yb) */ int height, width; // For calcs... height = (rand() % MAXROOMY) + 1; width = (rand() % MAXROOMX) + 1; xa = rand() % MAXX; ya = rand() % MAXY; xb = xa + width; yb = ya + height; bool clean = true; // Check to make sure area of room is clear if (map[xa][ya] != 0x00 || map[xb][yb] != 0x00) { clean = false; } // Check to make sure it's in the grid... if (xb > MAXX || yb > MAXY) { clean = false; } if (clean) { for (int i = xa; i <= xb; i++) { for (int j = ya; j <= yb; j++) { map[i][j] = 0x01; // Floor ('.') } } int hx; int hy; int wall = (rand() % 4); if (wall == 0) { // East hx = xb; hy = (ya + (rand() % (yb - 1)) + 1); } else if (wall == 1) { // North hx = (xa + (rand() % (xb - 1)) + 1); hy = ya; } else if (wall == 2) { // West hx = xa; hy = (ya + (rand() % (yb - 1)) + 1); } else { // South hx = (xa + (rand() % (xb - 1)) + 1); hy = yb; } makeHall(hx,hy,wall); } } void createMap() { for (int i = 0; i < MAXX; i++) { for (int j = 0; j < MAXY; i++) { map[i][j] = 0x00; } } for (int i = 0; i <= MAXROOMS; i++) { makeRoom(); } } } #endif
Comment