Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a13edeecde | |||
| 26db262281 | |||
| bc177fe5ce | |||
| a4158b4445 | |||
| f10d4061bd | |||
| 08d49fbd26 | |||
| 5169b4223a | |||
| 2ab8740228 | |||
| 05d5cf3d79 | |||
| 0272fd726d | |||
| 476aa4bce6 | |||
| 632746d933 | |||
| 5c0eddb2db |
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.gitattributes export-ignore
|
||||
.gitignore export-ignore
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
.cache
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CPackConfig.cmake
|
||||
@@ -5,6 +6,7 @@ CPackSourceConfig.cmake
|
||||
Makefile
|
||||
_CPack_Packages
|
||||
cmake_install.cmake
|
||||
compile_commands.json
|
||||
config.h
|
||||
install_manifest.txt
|
||||
mazemaker
|
||||
@@ -16,3 +18,6 @@ mazemaker
|
||||
*.so
|
||||
*.so.*
|
||||
logs
|
||||
.ninja_deps
|
||||
.ninja_log
|
||||
build.ninja
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
cmake_policy(VERSION 3.0)
|
||||
project (mazemaker VERSION 1.5)
|
||||
project (mazemaker VERSION 2.1)
|
||||
|
||||
SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE)
|
||||
SET(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include CACHE PATH "Installation prefix for C header files" FORCE)
|
||||
SET(LIB_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib CACHE PATH "Installation prefix for object code libraries" FORCE)
|
||||
|
||||
set(CMAKE_MACOSX_RPATH 1)
|
||||
set(CMAKE_INSTALL_RPATH "${LIB_INSTALL_DIR}")
|
||||
|
||||
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc)
|
||||
|
||||
link_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/lib)
|
||||
|
||||
@@ -22,18 +22,37 @@ typedef enum {
|
||||
EDGE_LEFT
|
||||
} mazeedge_dir_t;
|
||||
|
||||
typedef struct {
|
||||
size_t x, y;
|
||||
} maze_point_t;
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
maze_point_t* nodes;
|
||||
} maze_path_t;
|
||||
|
||||
typedef struct mazeoptions mazeoptions_t;
|
||||
|
||||
void mazemaker_generate_maze(int width, int height, mazegrid_t* result);
|
||||
void mazemaker_generate_maze_opt(int width, int height, mazegrid_t* result, mazeoptions_t const*);
|
||||
void mazemaker_free_maze(mazegrid_t* maze);
|
||||
int mazemaker_maze_to_png(mazegrid_t const* maze, char const* filename);
|
||||
int mazemaker_maze_to_png_opt(mazegrid_t const* maze, char const* filename, mazeoptions_t const*);
|
||||
int mazemaker_path_to_png(maze_path_t const* path, char const* filename);
|
||||
int mazemaker_path_to_png_opt(maze_path_t const* path, char const* filename, mazeoptions_t const*);
|
||||
int mazemaker_maze_to_png_mem(mazegrid_t const* maze, size_t* len, uint8_t** buf);
|
||||
int mazemaker_maze_to_png_mem_opt(mazegrid_t const* maze, size_t* len, uint8_t** buf, mazeoptions_t const*);
|
||||
int mazemaker_path_to_png_mem(maze_path_t const* maze, size_t* len, uint8_t** buf);
|
||||
int mazemaker_path_to_png_mem_opt(maze_path_t const* maze, size_t* len, uint8_t** buf, mazeoptions_t const*);
|
||||
|
||||
mazeoptions_t* mazemaker_options_new();
|
||||
void mazemaker_options_free(mazeoptions_t*);
|
||||
int mazemaker_options_set_wall_color(mazeoptions_t*, char const* color_desc);
|
||||
int mazemaker_options_set_background_color(mazeoptions_t*, char const* color_desc);
|
||||
int mazemaker_options_set_path_color(mazeoptions_t*, char const* color_desc);
|
||||
void mazemaker_options_set_seed(mazeoptions_t*, unsigned int seed);
|
||||
|
||||
int mazemaker_solve(mazegrid_t const* maze, maze_path_t* path);
|
||||
void mazemaker_path_free(maze_path_t* path);
|
||||
|
||||
#endif // !def(_MAZEMAKER_H)
|
||||
|
||||
@@ -15,6 +15,7 @@ find_package(PkgConfig REQUIRED)
|
||||
pkg_search_module(PNG REQUIRED libpng)
|
||||
|
||||
check_symbol_exists(arc4random_uniform "stdlib.h" HAVE_ARC4RANDOM)
|
||||
check_symbol_exists(srand_deterministic "stdlib.h" HAVE_SRAND_DETERMINISTIC)
|
||||
|
||||
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/config.h.in"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/config.h" )
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
#cmakedefine HAVE_ARC4RANDOM "@HAVE_ARC4RANDOM@"
|
||||
#cmakedefine HAVE_ARC4RANDOM @HAVE_ARC4RANDOM@
|
||||
#cmakedefine HAVE_SRAND_DETERMINISTIC @HAVE_SRAND_DETERMINISTIC@
|
||||
|
||||
25
lib/grid.c
25
lib/grid.c
@@ -1,5 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include "grid.h"
|
||||
#include "options.h"
|
||||
#include "config.h"
|
||||
|
||||
#define MAX_EDGE_WEIGHT 100
|
||||
@@ -19,14 +20,25 @@
|
||||
#define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8"
|
||||
#define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba"
|
||||
|
||||
static edgeweight_t random_edgeweight() {
|
||||
static edgeweight_t random_edgeweight(mazeoptions_t const* options) {
|
||||
if (!options->seed_set) {
|
||||
#ifdef HAVE_ARC4RANDOM
|
||||
return arc4random_uniform(MAX_EDGE_WEIGHT);
|
||||
#else // HAVE_ARC4RANDOM
|
||||
return random()%MAX_EDGE_WEIGHT;
|
||||
#endif // HAVE_ARC4RANDOM
|
||||
} else {
|
||||
// use deterministic random number generator
|
||||
return rand()%MAX_EDGE_WEIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_SRAND_DETERMINISTIC
|
||||
#define SRAND(x) (srand_deterministic(x))
|
||||
#else
|
||||
#define SRAND(x) (srand(x))
|
||||
#endif // defined(HAVE_SRAND_DETERMINISTIC)
|
||||
|
||||
mazegrid_t mazegrid_new(size_t width, size_t height) {
|
||||
mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*));
|
||||
for (size_t i = 0; i < height; i++) {
|
||||
@@ -72,11 +84,16 @@ edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t x, size_t y, mazeedge
|
||||
else return g->grid[y][x].up;
|
||||
}
|
||||
|
||||
void mazegrid_randomize(mazegrid_t* g) {
|
||||
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options) {
|
||||
// initialize random system
|
||||
if (options->seed_set) {
|
||||
SRAND(options->seed);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < g->height; i++) {
|
||||
for (size_t j = 0; j < g->width; j++) {
|
||||
if (i < g->height - 1) g->grid[i][j].up = random_edgeweight();
|
||||
if (j < g->width - 1) g->grid[i][j].right = random_edgeweight();
|
||||
if (i < g->height - 1) g->grid[i][j].up = random_edgeweight(options);
|
||||
if (j < g->width - 1) g->grid[i][j].right = random_edgeweight(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ void mazegrid_free(mazegrid_t* g);
|
||||
int mazegrid_set_edge(mazegrid_t* g, size_t x, size_t y, mazeedge_dir_t dir, edgeweight_t wt);
|
||||
edgeweight_t mazegrid_get_edge(mazegrid_t const* g, size_t, size_t y, mazeedge_dir_t dir);
|
||||
|
||||
void mazegrid_randomize(mazegrid_t* g);
|
||||
void mazegrid_randomize(mazegrid_t* g, mazeoptions_t const* options);
|
||||
void mazegrid_uniform(mazegrid_t* g);
|
||||
|
||||
void mazegrid_print(mazegrid_t const* g, FILE * f);
|
||||
|
||||
276
lib/image.c
276
lib/image.c
@@ -37,6 +37,94 @@ static void setBox(img_data_t* img, int x, int y, int w, int h, char v) {
|
||||
}
|
||||
}
|
||||
|
||||
static void setBoxCoords(img_data_t* img, int x1, int y1, int x2, int y2, char v) {
|
||||
if (x1 > x2) setBoxCoords(img, x2, x1, y1, y2, v);
|
||||
else if (y1 > y2) setBoxCoords(img, x1, x2, y2, y1, v);
|
||||
else setBox(img, x1, y1, (x2 - x1 + 1), (y2 - y1 + 1), v);
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void printImageData(img_data_t const* img) {
|
||||
FILE *ptr = fopen("output.txt", "wt");
|
||||
for (int i = 0; i < img->h; i++) {
|
||||
int flip_i = img->h - 1 - i;
|
||||
for (int j = 0; j < img->w; j++) {
|
||||
char v = img->data[img->w * flip_i + j];
|
||||
if (v)
|
||||
fprintf(ptr, "#");
|
||||
else
|
||||
fprintf(ptr, ".");
|
||||
}
|
||||
fprintf(ptr, "\n");
|
||||
}
|
||||
fclose(ptr);
|
||||
}
|
||||
#endif // 0
|
||||
|
||||
static int _box_x(int x) {
|
||||
return MARGIN + LINE_THICKNESS + BLOCK_SIZE * x;
|
||||
}
|
||||
|
||||
static int _box_y(int y, int height) {
|
||||
return height - MARGIN - LINE_THICKNESS - BLOCK_SIZE * y;
|
||||
}
|
||||
|
||||
void drawPathSegment(size_t x, size_t y, size_t new_x, size_t new_y, img_data_t* result) {
|
||||
size_t img_height = result->h;
|
||||
if ((x > new_x) || (y > new_y)) {
|
||||
drawPathSegment(new_x, new_y, x, y, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// note: it is ALWAYS the case that either x == new_x OR y == new_y
|
||||
// so the above should not result in infinite recursion
|
||||
|
||||
if (y == new_y) {
|
||||
// horizontal line (right)
|
||||
setBox(result, _box_x(x) + BLOCK_SIZE / 2 - LINE_THICKNESS / 2,
|
||||
_box_y(y, img_height) - BLOCK_SIZE / 2 - LINE_THICKNESS / 2,
|
||||
BLOCK_SIZE + LINE_THICKNESS,
|
||||
LINE_THICKNESS,
|
||||
1);
|
||||
} else {
|
||||
// vertical line (up)
|
||||
setBox(result, _box_x(x) + BLOCK_SIZE / 2 - LINE_THICKNESS / 2,
|
||||
_box_y(new_y, img_height) - BLOCK_SIZE / 2 - LINE_THICKNESS / 2,
|
||||
LINE_THICKNESS,
|
||||
BLOCK_SIZE + LINE_THICKNESS,
|
||||
1);
|
||||
}
|
||||
}
|
||||
|
||||
static img_data_t* pathToImageData(maze_path_t const* path) {
|
||||
size_t width = path->nodes[path->length - 1].x + 1,
|
||||
height = path->nodes[path->length - 1].y + 1,
|
||||
x, y;
|
||||
img_data_t* result = malloc(sizeof(img_data_t));
|
||||
result->w = LINE_THICKNESS + width * BLOCK_SIZE + 2 * MARGIN;
|
||||
result->h = LINE_THICKNESS + height * BLOCK_SIZE + 2 * MARGIN;
|
||||
result->data = malloc(result->w * result->h);
|
||||
//fprintf(stderr, "#########\n");
|
||||
|
||||
// set background
|
||||
memset(result->data, 0, result->w * result->h);
|
||||
|
||||
// set start and end points
|
||||
x = y = 0;
|
||||
setBox(result, MARGIN, _box_y(0, result->h) - BLOCK_SIZE / 2 - LINE_THICKNESS / 2, LINE_THICKNESS + BLOCK_SIZE / 2, LINE_THICKNESS, 1);
|
||||
setBox(result, _box_x(width - 1) + BLOCK_SIZE / 2 - LINE_THICKNESS / 2, _box_y(height - 1, result->h) - BLOCK_SIZE / 2 - LINE_THICKNESS / 2, BLOCK_SIZE / 2 + LINE_THICKNESS, LINE_THICKNESS, 1);
|
||||
|
||||
for (int i = 1; i < path->length; i++) {
|
||||
size_t new_x = path->nodes[i].x, new_y = path->nodes[i].y;
|
||||
drawPathSegment(x, y, new_x, new_y, result);
|
||||
x = new_x;
|
||||
y = new_y;
|
||||
}
|
||||
|
||||
//printImageData(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static img_data_t* gridToImageData(mazegrid_t const* g) {
|
||||
img_data_t* result = malloc(sizeof(img_data_t));
|
||||
result->w = LINE_THICKNESS + g->width * BLOCK_SIZE + 2 * MARGIN;
|
||||
@@ -92,11 +180,103 @@ static void writeImageData(img_data_t const* img, png_structp png_ptr, mazeoptio
|
||||
}
|
||||
}
|
||||
|
||||
static void writePathImageData(img_data_t const* img, png_structp png_ptr, mazeoptions_t const* options) {
|
||||
for (int i = 0; i < img->h; i++) {
|
||||
png_byte row[4 * img->w];
|
||||
for (int j = 0; j < img->w; j++) {
|
||||
if (img->data[i * img->w + j]) {
|
||||
// wall color
|
||||
row[4 * j] = options->path_color[0];
|
||||
row[4 * j + 1] = options->path_color[1];
|
||||
row[4 * j + 2] = options->path_color[2];
|
||||
row[4 * j + 3] = 0xff;
|
||||
} else memset(row + 4 * j, 0, 4);
|
||||
}
|
||||
png_write_row(png_ptr, row);
|
||||
}
|
||||
}
|
||||
|
||||
static void freeImageData(img_data_t* img) {
|
||||
free(img->data);
|
||||
free(img);
|
||||
}
|
||||
|
||||
static int
|
||||
prepare_png(png_structp* png_ptr, png_infop* info_ptr) {
|
||||
*png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (*png_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG write struct.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
*info_ptr = png_create_info_struct(*png_ptr);
|
||||
if (info_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG info struct.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(*png_ptr))) {
|
||||
fprintf(stderr, "Error during PNG creation.\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
cleanup_png(png_structp png_ptr, png_infop info_ptr) {
|
||||
if (info_ptr != NULL) {
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_info_struct(png_ptr, &info_ptr);
|
||||
}
|
||||
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
}
|
||||
|
||||
int mazemaker_path_to_png_opt(maze_path_t const* path, char const* filename, mazeoptions_t const* options) {
|
||||
int code = 1;
|
||||
img_data_t* img = pathToImageData(path);
|
||||
FILE *f = NULL;
|
||||
png_structp png_ptr = NULL;
|
||||
png_infop info_ptr = NULL;
|
||||
|
||||
if (strcmp(filename, "-") == 0) {
|
||||
f = stdout;
|
||||
} else {
|
||||
f = fopen(filename, "wb");
|
||||
if (NULL == f) {
|
||||
fprintf(stderr, "Could not open %s for writing.\n", filename);
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (prepare_png(&png_ptr, &info_ptr) == 0) {
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
png_init_io(png_ptr, f);
|
||||
|
||||
// Write header (8 bit color depth)
|
||||
png_set_IHDR(png_ptr, info_ptr, img->w, img->h,
|
||||
8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
writePathImageData(img, png_ptr, options);
|
||||
|
||||
png_write_end(png_ptr, NULL);
|
||||
|
||||
exit:
|
||||
if (strcmp(filename, "-") != 0) {
|
||||
if (f != NULL) fclose(f);
|
||||
}
|
||||
cleanup_png(png_ptr, info_ptr);
|
||||
if (img != NULL) freeImageData(img);
|
||||
return code;
|
||||
}
|
||||
|
||||
int mazemaker_maze_to_png_opt(mazegrid_t const* g, char const* filename, mazeoptions_t const* options) {
|
||||
int code = 1;
|
||||
img_data_t* img = gridToImageData(g);
|
||||
@@ -115,25 +295,11 @@ int mazemaker_maze_to_png_opt(mazegrid_t const* g, char const* filename, mazeopt
|
||||
}
|
||||
}
|
||||
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (png_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG write struct.\n");
|
||||
if (prepare_png(&png_ptr, &info_ptr) == 0) {
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG info struct.\n");
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
fprintf(stderr, "Error during PNG creation.\n");
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
png_init_io(png_ptr, f);
|
||||
|
||||
@@ -152,11 +318,7 @@ exit:
|
||||
if (strcmp(filename, "-") != 0) {
|
||||
if (f != NULL) fclose(f);
|
||||
}
|
||||
if (info_ptr != NULL) {
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_info_struct(png_ptr, &info_ptr);
|
||||
}
|
||||
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
cleanup_png(png_ptr, info_ptr);
|
||||
if (img != NULL) freeImageData(img);
|
||||
return code;
|
||||
}
|
||||
@@ -168,6 +330,13 @@ int mazemaker_maze_to_png(mazegrid_t const* g, char const* filename) {
|
||||
return result;
|
||||
}
|
||||
|
||||
int mazemaker_path_to_png(maze_path_t const* path, char const* filename) {
|
||||
mazeoptions_t* options = mazemaker_options_new(); // use default options
|
||||
int result = mazemaker_path_to_png_opt(path, filename, options);
|
||||
mazemaker_options_free(options);
|
||||
return result;
|
||||
}
|
||||
|
||||
static void append_png_data(png_structp png_ptr, png_bytep data, png_size_t length) {
|
||||
struct mem_encode* p = (struct mem_encode*)png_get_io_ptr(png_ptr);
|
||||
size_t nsize = p->size + length;
|
||||
@@ -192,22 +361,7 @@ int mazemaker_maze_to_png_mem_opt(mazegrid_t const* g, size_t* len, uint8_t** bu
|
||||
png_infop info_ptr = NULL;
|
||||
struct mem_encode menc = { .buffer = NULL, .size = 0 };
|
||||
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (png_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG write struct.\n");
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == NULL) {
|
||||
fprintf(stderr, "Could not allocate PNG info struct.\n");
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
fprintf(stderr, "Error during PNG creation.\n");
|
||||
if (prepare_png(&png_ptr, &info_ptr) == 0) {
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
@@ -232,11 +386,7 @@ exit:
|
||||
if (!code && menc.buffer) {
|
||||
free(menc.buffer);
|
||||
}
|
||||
if (info_ptr != NULL) {
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_info_struct(png_ptr, &info_ptr);
|
||||
}
|
||||
if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
cleanup_png(png_ptr, info_ptr);
|
||||
if (img != NULL) freeImageData(img);
|
||||
return code;
|
||||
}
|
||||
@@ -247,3 +397,47 @@ int mazemaker_maze_to_png_mem(mazegrid_t const* g, size_t* len, uint8_t** buf) {
|
||||
mazemaker_options_free(options);
|
||||
return result;
|
||||
}
|
||||
|
||||
int mazemaker_path_to_png_mem_opt(maze_path_t const* path, size_t* len, uint8_t** buf, mazeoptions_t const* options) {
|
||||
int code = 1;
|
||||
img_data_t* img = pathToImageData(path);
|
||||
png_structp png_ptr = NULL;
|
||||
png_infop info_ptr = NULL;
|
||||
struct mem_encode menc = { .buffer = NULL, .size = 0 };
|
||||
|
||||
if (prepare_png(&png_ptr, &info_ptr) == 0) {
|
||||
code = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
png_set_write_fn(png_ptr, &menc, append_png_data, png_no_flush);
|
||||
|
||||
// Write header (8 bit color depth)
|
||||
png_set_IHDR(png_ptr, info_ptr, img->w, img->h,
|
||||
8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
|
||||
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
|
||||
writePathImageData(img, png_ptr, options);
|
||||
|
||||
png_write_end(png_ptr, NULL);
|
||||
|
||||
*len = menc.size;
|
||||
*buf = menc.buffer;
|
||||
|
||||
exit:
|
||||
if (!code && menc.buffer) {
|
||||
free(menc.buffer);
|
||||
}
|
||||
cleanup_png(png_ptr, info_ptr);
|
||||
if (img != NULL) freeImageData(img);
|
||||
return code;
|
||||
}
|
||||
|
||||
int mazemaker_path_to_png_mem(maze_path_t const* path, size_t* len, uint8_t** buf) {
|
||||
mazeoptions_t* options = mazemaker_options_new(); // use default options
|
||||
int result = mazemaker_path_to_png_mem_opt(path, len, buf, options);
|
||||
mazemaker_options_free(options);
|
||||
return result;
|
||||
}
|
||||
|
||||
12
lib/maze.c
12
lib/maze.c
@@ -1,13 +1,21 @@
|
||||
#include <mazemaker.h>
|
||||
#include <stdlib.h>
|
||||
#include "grid.h"
|
||||
#include "prim.h"
|
||||
|
||||
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
|
||||
void mazemaker_generate_maze_opt(int width, int height, mazegrid_t* result, mazeoptions_t const* options) {
|
||||
mazegrid_t g = mazegrid_new(width, height);
|
||||
mazegrid_randomize(&g);
|
||||
mazegrid_randomize(&g, options);
|
||||
prim(&g, result);
|
||||
mazegrid_free(&g);
|
||||
}
|
||||
|
||||
void mazemaker_generate_maze(int width, int height, mazegrid_t* result) {
|
||||
mazeoptions_t* options = mazemaker_options_new(); // use defaults
|
||||
mazemaker_generate_maze_opt(width, height, result, options);
|
||||
mazemaker_options_free(options);
|
||||
}
|
||||
|
||||
void mazemaker_free_maze(mazegrid_t* maze) {
|
||||
mazegrid_free(maze);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,10 @@ mazeoptions_t* mazemaker_options_new() {
|
||||
// set defaults
|
||||
memset(result->wall_color, 0, 3);
|
||||
memset(result->background_color, 0xff, 3);
|
||||
result->path_color[0] = 0xff;
|
||||
result->path_color[1] = result->path_color[2] = 0;
|
||||
result->seed = 0;
|
||||
result->seed_set = false;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -78,3 +82,11 @@ int mazemaker_options_set_background_color(mazeoptions_t* options, char const* c
|
||||
return stringToColor(color_desc, options->background_color);
|
||||
}
|
||||
|
||||
int mazemaker_options_set_path_color(mazeoptions_t* options, char const* color_desc) {
|
||||
return stringToColor(color_desc, options->path_color);
|
||||
}
|
||||
|
||||
void mazemaker_options_set_seed(mazeoptions_t* options, rand_seed_t seed) {
|
||||
options->seed = seed;
|
||||
options->seed_set = true;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,17 @@
|
||||
#define _OPTIONS_H 1
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef uint8_t rgb_color_t[3];
|
||||
typedef unsigned int rand_seed_t;
|
||||
|
||||
typedef struct mazeoptions {
|
||||
rgb_color_t wall_color;
|
||||
rgb_color_t background_color;
|
||||
rgb_color_t path_color;
|
||||
rand_seed_t seed;
|
||||
bool seed_set;
|
||||
} mazeoptions_t;
|
||||
|
||||
#endif // !def(_OPTIONS_H)
|
||||
|
||||
118
lib/path.c
Normal file
118
lib/path.c
Normal file
@@ -0,0 +1,118 @@
|
||||
#include <mazemaker.h>
|
||||
#include <string.h>
|
||||
#include "pq.h"
|
||||
#include "set.h"
|
||||
|
||||
DEFINE_PQ(maze_path_t, PathPQ, unsigned int);
|
||||
|
||||
static void path_copy(maze_path_t const* src, maze_path_t* dst) {
|
||||
dst->length = src->length;
|
||||
dst->nodes = calloc(dst->length, sizeof(maze_point_t));
|
||||
memcpy(dst->nodes, src->nodes, src->length * sizeof(maze_point_t));
|
||||
}
|
||||
|
||||
static void path_copy_append(maze_path_t const* src, maze_path_t* dst, maze_point_t pt) {
|
||||
dst->length = src->length + 1;
|
||||
dst->nodes = calloc(dst->length, sizeof(maze_point_t));
|
||||
memcpy(dst->nodes, src->nodes, src->length * sizeof(maze_point_t));
|
||||
dst->nodes[dst->length - 1] = pt;
|
||||
}
|
||||
|
||||
int mazemaker_solve(mazegrid_t const* maze, maze_path_t* path) {
|
||||
int code = 0;
|
||||
struct node_set* set = node_set_new(maze->width, maze->height);
|
||||
NEW_PQ(PathPQ, pq);
|
||||
|
||||
// put starting point in the queue
|
||||
maze_path_t start;
|
||||
start.length = 1;
|
||||
start.nodes = malloc(sizeof(maze_point_t));
|
||||
start.nodes[0].x = 0;
|
||||
start.nodes[0].y = 0;
|
||||
|
||||
PUSH_PQ(pq, PathPQ, start, 0);
|
||||
while (!EMPTY_PQ(pq)) {
|
||||
maze_path_t top;
|
||||
unsigned int dist;
|
||||
size_t x, y;
|
||||
POP_PQ(pq, PathPQ, top, dist);
|
||||
|
||||
x = top.nodes[top.length - 1].x;
|
||||
y = top.nodes[top.length - 1].y;
|
||||
node_set_add(set, x, y);
|
||||
|
||||
// check to see if we've reached the end
|
||||
if ((x == maze->width - 1) && (y == maze->height - 1)) {
|
||||
path_copy(&top, path);
|
||||
mazemaker_path_free(&top);
|
||||
code = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
// otherwise check for unexplored paths in all directions...
|
||||
|
||||
// ... up
|
||||
if ((y < maze->height) && (mazegrid_get_edge(maze, x, y, EDGE_UP) != 0)) {
|
||||
if (!node_set_contains(set, x, y + 1)) {
|
||||
maze_path_t newpath;
|
||||
maze_point_t pt = { .x = x, .y = y + 1 };
|
||||
path_copy_append(&top, &newpath, pt);
|
||||
PUSH_PQ(pq, PathPQ, newpath, dist + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// ... right
|
||||
if ((x < maze->width) && (mazegrid_get_edge(maze, x, y, EDGE_RIGHT) != 0)) {
|
||||
if (!node_set_contains(set, x + 1, y)) {
|
||||
maze_path_t newpath;
|
||||
maze_point_t pt = { .x = x + 1, .y = y };
|
||||
path_copy_append(&top, &newpath, pt);
|
||||
PUSH_PQ(pq, PathPQ, newpath, dist + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// ... down
|
||||
if ((y > 0) && (mazegrid_get_edge(maze, x, y, EDGE_DOWN) != 0)) {
|
||||
if (!node_set_contains(set, x, y - 1)) {
|
||||
maze_path_t newpath;
|
||||
maze_point_t pt = { .x = x, .y = y - 1};
|
||||
path_copy_append(&top, &newpath, pt);
|
||||
PUSH_PQ(pq, PathPQ, newpath, dist + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// ... left
|
||||
if ((x > 0) && (mazegrid_get_edge(maze, x, y, EDGE_LEFT) != 0)) {
|
||||
if (!node_set_contains(set, x - 1, y)) {
|
||||
maze_path_t newpath;
|
||||
maze_point_t pt = { .x = x - 1, .y = y };
|
||||
path_copy_append(&top, &newpath, pt);
|
||||
PUSH_PQ(pq, PathPQ, newpath, dist + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// delete old path
|
||||
mazemaker_path_free(&top);
|
||||
}
|
||||
|
||||
// If no path is found, we will run out of new nodes to explore, the queue
|
||||
// will go empty, and the loop will terminate. The default return code of 0
|
||||
// indicates that no solution has been put in the path parameter.
|
||||
|
||||
if (!EMPTY_PQ(pq)) {
|
||||
maze_path_t top;
|
||||
unsigned int dist;
|
||||
FOREACH_PQ(pq, top, dist) {
|
||||
free(top.nodes);
|
||||
}
|
||||
FOREACH_PQ_END
|
||||
}
|
||||
FREE_PQ(pq);
|
||||
node_set_free(set);
|
||||
return code;
|
||||
}
|
||||
|
||||
void mazemaker_path_free(maze_path_t* path) {
|
||||
path->length = 0;
|
||||
free(path->nodes);
|
||||
}
|
||||
18
lib/path.h
18
lib/path.h
@@ -1,18 +0,0 @@
|
||||
#ifndef _PATH_H
|
||||
#define _PATH_H 1
|
||||
|
||||
#include "grid.h"
|
||||
|
||||
typedef struct {
|
||||
unsigned row, col;
|
||||
} block_t;
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
block_t *nodes;
|
||||
} path_t;
|
||||
|
||||
int shortest_path(mazegrid_t const* maze, path_t* result);
|
||||
void free_path(path_t* p);
|
||||
|
||||
#endif // !def(_PATH_H)
|
||||
110
lib/pq.c
110
lib/pq.c
@@ -1,110 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include "pq.h"
|
||||
|
||||
typedef struct {
|
||||
unsigned int priority;
|
||||
size_t x, y;
|
||||
mazeedge_dir_t dir;
|
||||
} _point_t;
|
||||
|
||||
struct pq {
|
||||
size_t heap_size, next;
|
||||
_point_t* heap;
|
||||
};
|
||||
|
||||
struct pq* pq_new(size_t heap_size) {
|
||||
struct pq* result = malloc(sizeof(struct pq));
|
||||
result->heap_size = heap_size;
|
||||
result->next = 0;
|
||||
result->heap = calloc(heap_size, sizeof(_point_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
static void _swap(struct pq* q, size_t idx1, size_t idx2) {
|
||||
unsigned int priority = q->heap[idx1].priority;
|
||||
size_t x = q->heap[idx1].x;
|
||||
size_t y = q->heap[idx1].y;
|
||||
mazeedge_dir_t dir = q->heap[idx1].dir;
|
||||
|
||||
q->heap[idx1].priority = q->heap[idx2].priority;
|
||||
q->heap[idx1].x = q->heap[idx2].x;
|
||||
q->heap[idx1].y = q->heap[idx2].y;
|
||||
q->heap[idx1].dir = q->heap[idx2].dir;
|
||||
|
||||
q->heap[idx2].priority = priority;
|
||||
q->heap[idx2].x = x;
|
||||
q->heap[idx2].y = y;
|
||||
q->heap[idx2].dir = dir;
|
||||
}
|
||||
|
||||
static void _heapify_up(struct pq* q) {
|
||||
size_t i = q->next - 1;
|
||||
while (i > 0) {
|
||||
size_t parent = i / 2;
|
||||
if (q->heap[parent].priority > q->heap[i].priority) {
|
||||
_swap(q, parent, i);
|
||||
i = parent;
|
||||
} else {
|
||||
// done
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pq_add(struct pq* q, unsigned int priority, size_t x, size_t y, mazeedge_dir_t dir) {
|
||||
q->heap[q->next].x = x;
|
||||
q->heap[q->next].y = y;
|
||||
q->heap[q->next].priority = priority;
|
||||
q->heap[q->next].dir = dir;
|
||||
q->next++;
|
||||
_heapify_up(q);
|
||||
}
|
||||
|
||||
bool pq_empty(struct pq const* q) {
|
||||
return q->next == 0;
|
||||
}
|
||||
|
||||
static void _heapify_down(struct pq* q) {
|
||||
size_t i = 0;
|
||||
while (i < q->next) {
|
||||
size_t ch1 = i * 2, ch2 = i * 2 + 1;
|
||||
if (ch1 >= q->next) return;
|
||||
if (ch2 >= q->next) {
|
||||
if (q->heap[ch1].priority < q->heap[i].priority) {
|
||||
_swap(q, i, ch1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// three-way compare
|
||||
if ((q->heap[i].priority < q->heap[ch1].priority) &&
|
||||
(q->heap[i].priority < q->heap[ch2].priority)) {
|
||||
return; // all done - partial order ensured
|
||||
}
|
||||
if (q->heap[ch1].priority < q->heap[ch2].priority) {
|
||||
_swap(q, i, ch1);
|
||||
i = ch1;
|
||||
} else {
|
||||
_swap(q, i, ch2);
|
||||
i = ch2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int pq_pop(struct pq* q, size_t* x, size_t* y, mazeedge_dir_t* dir) {
|
||||
if (pq_empty(q)) return 0;
|
||||
*x = q->heap[0].x;
|
||||
*y = q->heap[0].y;
|
||||
*dir = q->heap[0].dir;
|
||||
_swap(q, 0, q->next-1);
|
||||
q->next--;
|
||||
_heapify_down(q);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void pq_free(struct pq* q) {
|
||||
free(q->heap); q->heap = NULL;
|
||||
q->heap_size = 0;
|
||||
q->next = 0;
|
||||
free(q);
|
||||
}
|
||||
102
lib/pq.h
102
lib/pq.h
@@ -1,15 +1,101 @@
|
||||
#ifndef _PQ_H
|
||||
#define _PQ_H 1
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "grid.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct pq;
|
||||
#define DEFAULT_HEAP_SIZE 16
|
||||
|
||||
struct pq* pq_new(size_t heap_size);
|
||||
void pq_add(struct pq* q, unsigned int priority, size_t x, size_t y, mazeedge_dir_t dir);
|
||||
int pq_pop(struct pq* q, size_t* x, size_t* y, mazeedge_dir_t *dir);
|
||||
bool pq_empty(struct pq const* q);
|
||||
void pq_free(struct pq* q);
|
||||
#define DEFINE_PQ(T, N, P) \
|
||||
struct _##N##HeapNode { \
|
||||
P priority; \
|
||||
T data; \
|
||||
}; \
|
||||
typedef struct { \
|
||||
size_t heap_size, next; \
|
||||
struct _##N##HeapNode* heap; \
|
||||
} N; \
|
||||
static void _##N##_swap(N* pq, size_t i, size_t j) { \
|
||||
struct _##N##HeapNode tmp = pq->heap[i]; \
|
||||
pq->heap[i] = pq->heap[j]; \
|
||||
pq->heap[j] = tmp; \
|
||||
} \
|
||||
static void _##N##_heapify_up(N* pq) { \
|
||||
size_t i = pq->next - 1; \
|
||||
while (i > 0) { \
|
||||
size_t parent = i / 2; \
|
||||
if (pq->heap[parent].priority > pq->heap[i].priority) { \
|
||||
_##N##_swap(pq, parent, i); \
|
||||
i = parent; \
|
||||
} else return; \
|
||||
} \
|
||||
} \
|
||||
static void _##N##_heapify_down(N* pq) { \
|
||||
size_t i = 0; \
|
||||
while (i < pq->next) { \
|
||||
size_t ch1 = i * 2, ch2 = i * 2 + 1; \
|
||||
if (ch1 >= pq->next) return; \
|
||||
if (ch2 >= pq->next) { \
|
||||
if (pq->heap[ch1].priority < pq->heap[i].priority) { \
|
||||
_##N##_swap(pq, i, ch1); \
|
||||
} \
|
||||
return; \
|
||||
} \
|
||||
if ((pq->heap[i].priority < pq->heap[ch1].priority) && \
|
||||
(pq->heap[i].priority < pq->heap[ch2].priority)) { \
|
||||
return; \
|
||||
} \
|
||||
if (pq->heap[ch1].priority < pq->heap[ch2].priority) { \
|
||||
_##N##_swap(pq, i, ch1); \
|
||||
i = ch1; \
|
||||
} else { \
|
||||
_##N##_swap(pq, i, ch2); \
|
||||
i = ch2; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
#define NEW_PQ(T, N) \
|
||||
T N = (T) { .heap_size = DEFAULT_HEAP_SIZE, .next = 0, .heap = calloc(DEFAULT_HEAP_SIZE, sizeof(struct _##T##HeapNode)) }
|
||||
|
||||
// WARNING: S cannot be an expression that has side effects!
|
||||
#define NEW_PQ_SZ(T, N, S) \
|
||||
T N = (T) { .heap_size = (S), .next = 0, .heap = calloc((S), sizeof(struct _##T##HeapNode)) }
|
||||
|
||||
#define PUSH_PQ(Q, N, I, P) { \
|
||||
if ((Q).next >= (Q).heap_size) { \
|
||||
(Q).heap = realloc((Q).heap, (Q).heap_size * 2 * sizeof(struct _##N##HeapNode)); \
|
||||
(Q).heap_size *= 2; \
|
||||
} \
|
||||
(Q).heap[(Q).next].data = (I); \
|
||||
(Q).heap[(Q).next].priority = (P); \
|
||||
(Q).next++; \
|
||||
_##N##_heapify_up(&(Q)); \
|
||||
}
|
||||
|
||||
#define EMPTY_PQ(Q) ((Q).next == 0)
|
||||
|
||||
// check for emptiness first!
|
||||
#define POP_PQ(Q, N, DST_I, DST_P) { \
|
||||
DST_I = (Q).heap[0].data; \
|
||||
DST_P = (Q).heap[0].priority; \
|
||||
_##N##_swap(&(Q), 0, (Q).next-1); \
|
||||
(Q).next--; \
|
||||
_##N##_heapify_down(&(Q)); \
|
||||
}
|
||||
|
||||
#define FOREACH_PQ(Q, DST_DATA, DST_P) \
|
||||
for (size_t idx = 0; idx < (Q).next; idx++) { \
|
||||
DST_DATA = (Q).heap[idx].data; \
|
||||
DST_P = (Q).heap[idx].priority;
|
||||
|
||||
#define FOREACH_PQ_END }
|
||||
|
||||
// warning: be sure to clean up your data to avoid leaks!
|
||||
#define FREE_PQ(Q) { \
|
||||
free((Q).heap); \
|
||||
(Q).heap_size = 0; \
|
||||
(Q).next = 0; \
|
||||
}
|
||||
|
||||
#endif // !def(_PQ_H)
|
||||
|
||||
57
lib/prim.c
57
lib/prim.c
@@ -2,42 +2,57 @@
|
||||
#include "prim.h"
|
||||
#include "set.h"
|
||||
|
||||
typedef struct {
|
||||
size_t x, y;
|
||||
mazeedge_dir_t dir;
|
||||
} _point_t;
|
||||
|
||||
DEFINE_PQ(_point_t, PointPQ, unsigned int);
|
||||
|
||||
void prim(mazegrid_t const* g, mazegrid_t* result) {
|
||||
struct pq* q = pq_new(g->width*g->height*4);
|
||||
NEW_PQ_SZ(PointPQ, q, g->width*g->height*4);
|
||||
struct node_set* s = node_set_new(g->width, g->height);
|
||||
*result = mazegrid_new(g->width, g->height);
|
||||
|
||||
node_set_add(s, 0, 0);
|
||||
if (g->height > 1) pq_add(q, mazegrid_get_edge(g, 0, 0, EDGE_UP), 0, 1, EDGE_DOWN);
|
||||
if (g->width > 1) pq_add(q, mazegrid_get_edge(g, 0, 0, EDGE_RIGHT), 1, 0, EDGE_LEFT);
|
||||
|
||||
while (!pq_empty(q) && (node_set_size(s) < g->width * g->height)) {
|
||||
size_t x, y;
|
||||
mazeedge_dir_t dir;
|
||||
pq_pop(q, &x, &y, &dir);
|
||||
|
||||
if (node_set_contains(s, x, y)) continue;
|
||||
if (g->height > 1) {
|
||||
_point_t e = { 0, 1, EDGE_DOWN };
|
||||
PUSH_PQ(q, PointPQ, e, mazegrid_get_edge(g, 0, 0, EDGE_UP));
|
||||
}
|
||||
if (g->width > 1) {
|
||||
_point_t e = { 1, 0, EDGE_LEFT };
|
||||
PUSH_PQ(q, PointPQ, e, mazegrid_get_edge(g, 0, 0, EDGE_RIGHT));
|
||||
}
|
||||
while (!EMPTY_PQ(q) && (node_set_size(s) < g->width * g->height)) {
|
||||
_point_t e;
|
||||
unsigned int p;
|
||||
POP_PQ(q, PointPQ, e, p);
|
||||
if (node_set_contains(s, e.x, e.y)) continue;
|
||||
|
||||
// add edge to tree
|
||||
mazegrid_set_edge(result, x, y, dir, 1);
|
||||
mazegrid_set_edge(result, e.x, e.y, e.dir, 1);
|
||||
|
||||
// add neighbors
|
||||
if ((x > 0) && !node_set_contains(s, x - 1, y)) {
|
||||
pq_add(q, mazegrid_get_edge(g, x, y, EDGE_LEFT), x - 1, y, EDGE_RIGHT);
|
||||
if ((e.x > 0) && !node_set_contains(s, e.x - 1, e.y)) {
|
||||
_point_t pt = { e.x - 1, e.y, EDGE_RIGHT };
|
||||
PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_LEFT));
|
||||
}
|
||||
if ((x < g->width - 1) && !node_set_contains(s, x + 1, y)) {
|
||||
pq_add(q, mazegrid_get_edge(g, x, y, EDGE_RIGHT), x + 1, y, EDGE_LEFT);
|
||||
if ((e.x < g->width - 1) && !node_set_contains(s, e.x + 1, e.y)) {
|
||||
_point_t pt = { e.x + 1, e.y, EDGE_LEFT };
|
||||
PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_RIGHT));
|
||||
}
|
||||
if ((y > 0) && !node_set_contains(s, x, y - 1)) {
|
||||
pq_add(q, mazegrid_get_edge(g, x, y, EDGE_DOWN), x, y - 1, EDGE_UP);
|
||||
if ((e.y > 0) && !node_set_contains(s, e.x, e.y - 1)) {
|
||||
_point_t pt = { e.x, e.y - 1, EDGE_UP };
|
||||
PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_DOWN));
|
||||
}
|
||||
if ((y < g->height - 1) && !node_set_contains(s, x, y + 1)) {
|
||||
pq_add(q, mazegrid_get_edge(g, x, y, EDGE_UP), x, y + 1, EDGE_DOWN);
|
||||
if ((e.y < g->height - 1) && !node_set_contains(s, e.x, e.y + 1)) {
|
||||
_point_t pt = { e.x, e.y + 1, EDGE_DOWN };
|
||||
PUSH_PQ(q, PointPQ, pt, mazegrid_get_edge(g, e.x, e.y, EDGE_UP));
|
||||
}
|
||||
|
||||
node_set_add(s, x, y);
|
||||
node_set_add(s, e.x, e.y);
|
||||
}
|
||||
|
||||
node_set_free(s);
|
||||
pq_free(q);
|
||||
FREE_PQ(q);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
int main(int argc, char* argv[]) {
|
||||
char c;
|
||||
int width = 0, height = 0;
|
||||
char const *filename = NULL, *fg_color = NULL, *bg_color = NULL;
|
||||
unsigned int seed = 0;
|
||||
char const *filename = NULL, *fg_color = NULL, *bg_color = NULL, *path_color = NULL, *seed_str = NULL, *solution_fn = NULL;
|
||||
struct poptOption options_table[] = {
|
||||
{ "width", 'w', POPT_ARG_INT, &width, 0,
|
||||
"Width of the maze", "BLOCKS" },
|
||||
@@ -16,6 +17,12 @@ int main(int argc, char* argv[]) {
|
||||
"Foreground (wall) color", "#rrggbb" },
|
||||
{ "background", 'b', POPT_ARG_STRING, &bg_color, 0,
|
||||
"Background color", "#rrggbb" },
|
||||
{ "path", 'p', POPT_ARG_STRING, &path_color, 0,
|
||||
"Path (solution) color", "#rrggbb" },
|
||||
{ "seed", 's', POPT_ARG_STRING, &seed_str, 0,
|
||||
"Random seed", "SEED" },
|
||||
{ "solution", 'u', POPT_ARG_STRING, &solution_fn, 0,
|
||||
"Output solution", "FILENAME" },
|
||||
POPT_AUTOHELP
|
||||
{ NULL, 0, 0, NULL, 0 }
|
||||
};
|
||||
@@ -53,8 +60,27 @@ int main(int argc, char* argv[]) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
mazemaker_generate_maze(width, height, &maze);
|
||||
if (path_color != NULL) {
|
||||
if (0 > mazemaker_options_set_path_color(options, path_color)) {
|
||||
fprintf(stderr, "Unknown color: \"%s\"\n", path_color);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (seed_str != NULL) {
|
||||
seed = (unsigned int)atol(seed_str);
|
||||
mazemaker_options_set_seed(options, seed);
|
||||
}
|
||||
mazemaker_generate_maze_opt(width, height, &maze, options);
|
||||
mazemaker_maze_to_png_opt(&maze, filename, options);
|
||||
if (solution_fn != NULL) {
|
||||
maze_path_t solution;
|
||||
if (mazemaker_solve(&maze, &solution) == 0) {
|
||||
fprintf(stderr, "Error: no maze solution!\n");
|
||||
} else {
|
||||
mazemaker_path_to_png_opt(&solution, solution_fn, options);
|
||||
mazemaker_path_free(&solution);
|
||||
}
|
||||
}
|
||||
mazemaker_free_maze(&maze);
|
||||
mazemaker_options_free(options);
|
||||
poptFreeContext(ctx);
|
||||
|
||||
Reference in New Issue
Block a user