-
-
Save StonedXander/973262 to your computer and use it in GitHub Desktop.
Just a simple maze generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <vector> | |
typedef struct { | |
unsigned int x; | |
unsigned int y; | |
unsigned char direction; | |
} Coordinate; | |
class Dungeon { | |
unsigned int _width; | |
unsigned int _height; | |
unsigned char *_data; | |
public: | |
void generate(unsigned int, unsigned int); | |
void write(const char*); | |
}; | |
#define QUEUE_SIZE 1024 | |
// Cell has been marked => Can't be a candidate anymore. | |
#define CELL_MARKED 1 | |
#define CELL_FLOOR 128 | |
void Dungeon::generate(unsigned int w, unsigned int h) { | |
int oX[4] = { -1, 0, 1, 0 }; | |
int oY[4] = { 0, -1, 0, 1 }; | |
Coordinate *queue = new Coordinate[w*h]; | |
unsigned int load = 0; | |
unsigned char *d = new unsigned char[w*h]; | |
memset(d, 0, sizeof(char)*w*h); | |
load = 4; | |
queue[0].x = rand() % (w / 2) * 2; | |
queue[0].y = rand() % (h / 2) * 2; | |
queue[0].direction = 0; | |
queue[1].x = queue[0].x; | |
queue[1].y = queue[0].y; | |
queue[1].direction = 1; | |
queue[2].x = queue[0].x; | |
queue[2].y = queue[0].y; | |
queue[2].direction = 2; | |
queue[3].x = queue[0].x; | |
queue[3].y = queue[0].y; | |
queue[3].direction = 3; | |
// As long as there's a wall in the list. | |
while(load != 0) { | |
unsigned int candidate = rand()%load; | |
Coordinate *c = queue + candidate; | |
unsigned int x = c->x; | |
unsigned int y = c->y; | |
unsigned char dir = c->direction; | |
unsigned int pos = (y * w) + x; | |
// First, remove from the queue. | |
--load; | |
memcpy(c, queue + load, sizeof(Coordinate)); | |
*(d + pos) = CELL_FLOOR; | |
// Get the direction. | |
unsigned int tx = x + oX[dir]*2; | |
unsigned int ty = y + oY[dir]*2; | |
unsigned int tpos = (ty * w) + (tx); | |
if(!((tx < 0) || (tx >= w) || (ty < 0) || (ty >= h))) { | |
unsigned char *dest = d + tpos; | |
if(((*dest) & CELL_FLOOR) == 0) { | |
// It's ok ! Let's propagate by creating candidates. | |
// We don't care about loop back as this cell is marked. | |
d[(x + oX[dir]) + ((y + oY[dir]) * w)] = CELL_FLOOR; | |
d[(x + (oX[dir]*2)) + ((y + (oY[dir]*2)) * w)] = CELL_FLOOR; | |
Coordinate *insert = queue + load; | |
for(int i = 0; i < 4; ++i) { | |
insert[i].x = tx; | |
insert[i].y = ty; | |
insert[i].direction = i; | |
} | |
load += 4; | |
} | |
} | |
} | |
delete []queue; | |
_width = w; | |
_height = h; | |
_data = d; | |
} | |
void Dungeon::write(const char* filename) | |
{ | |
FILE *out; | |
printf( "Open !\n" ); | |
out = fopen(filename, "wb"); | |
printf("Prepare !\n"); | |
fprintf(out, "P5\n#\n%d %d\n255\n", _width, _height); | |
printf("Write\n"); | |
fwrite(_data, 1, _width*_height, out); | |
printf("Close\n"); | |
fclose(out); | |
printf("Done\n"); | |
} | |
int main() | |
{ | |
Dungeon donj; | |
donj.generate(32, 32); | |
donj.write("maze.pgm"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm gonna puke. This is awful ...