7 Commits
v1.4 ... v1.6

Author SHA1 Message Date
0272fd726d Add option to set random seed for deterministic behavior
Bump version to 1.6
2022-01-15 14:40:21 -05:00
476aa4bce6 Remove useless quotation marks 2021-11-29 19:40:54 -05:00
632746d933 Ignore ninja artifacts 2021-11-29 19:38:59 -05:00
5c0eddb2db MacOS compatibility 2021-11-29 19:18:58 -05:00
776def6948 Use arc4random if available 2021-11-23 21:47:15 -05:00
00db3de911 Add dir for popt library 2021-10-05 23:30:46 -04:00
49b62ee7bd MacOS compatibility 2021-10-05 23:30:32 -04:00
12 changed files with 81 additions and 9 deletions

4
.gitignore vendored
View File

@@ -8,6 +8,7 @@ cmake_install.cmake
config.h config.h
install_manifest.txt install_manifest.txt
mazemaker mazemaker
*.dylib
*.tar.gz *.tar.gz
*.png *.png
*.pc *.pc
@@ -15,3 +16,6 @@ mazemaker
*.so *.so
*.so.* *.so.*
logs logs
.ninja_deps
.ninja_log
build.ninja

View File

@@ -1,11 +1,14 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.0)
cmake_policy(VERSION 3.0) cmake_policy(VERSION 3.0)
project (mazemaker VERSION 1.4) project (mazemaker VERSION 1.6)
SET(EXEC_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE PATH "Installation prefix for executables and object code libraries" FORCE) 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(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(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) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/mazemaker.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/mazemaker.pc)
link_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/lib) link_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/lib)

View File

@@ -25,6 +25,7 @@ typedef enum {
typedef struct mazeoptions mazeoptions_t; typedef struct mazeoptions mazeoptions_t;
void mazemaker_generate_maze(int width, int height, mazegrid_t* result); 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); void mazemaker_free_maze(mazegrid_t* maze);
int mazemaker_maze_to_png(mazegrid_t const* maze, char const* filename); 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_maze_to_png_opt(mazegrid_t const* maze, char const* filename, mazeoptions_t const*);
@@ -35,5 +36,6 @@ mazeoptions_t* mazemaker_options_new();
void mazemaker_options_free(mazeoptions_t*); void mazemaker_options_free(mazeoptions_t*);
int mazemaker_options_set_wall_color(mazeoptions_t*, char const* color_desc); 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_background_color(mazeoptions_t*, char const* color_desc);
void mazemaker_options_set_seed(mazeoptions_t*, unsigned int seed);
#endif // !def(_MAZEMAKER_H) #endif // !def(_MAZEMAKER_H)

View File

@@ -1,5 +1,8 @@
project(mazemaker_lib) project(mazemaker_lib)
cmake_minimum_required (VERSION 2.8.12) cmake_minimum_required (VERSION 2.8.12)
include(CheckSymbolExists)
set (mazemaker_SOVERSION_CURRENT 0) set (mazemaker_SOVERSION_CURRENT 0)
set (mazemaker_SOVERSION_REVISION 2) set (mazemaker_SOVERSION_REVISION 2)
set (mazemaker_SOVERSION_AGE 0) set (mazemaker_SOVERSION_AGE 0)
@@ -11,6 +14,12 @@ set (mazemaker_SOVERSION ${mazemaker_SOVERSION_MAJOR}.${mazemaker_SOVERSION_MINO
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_search_module(PNG REQUIRED libpng) 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" )
file(GLOB SOURCES *.c *.h) file(GLOB SOURCES *.c *.h)
add_library(mazemaker_shared SHARED ${SOURCES}) add_library(mazemaker_shared SHARED ${SOURCES})

2
lib/config.h.in Normal file
View File

@@ -0,0 +1,2 @@
#cmakedefine HAVE_ARC4RANDOM @HAVE_ARC4RANDOM@
#cmakedefine HAVE_SRAND_DETERMINISTIC @HAVE_SRAND_DETERMINISTIC@

View File

@@ -1,5 +1,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "grid.h" #include "grid.h"
#include "options.h"
#include "config.h"
#define MAX_EDGE_WEIGHT 100 #define MAX_EDGE_WEIGHT 100
#define TOP_LEFT_CORNER "\xe2\x94\x8f" #define TOP_LEFT_CORNER "\xe2\x94\x8f"
@@ -18,6 +20,25 @@
#define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8" #define HORIZONTAL_HALF_LEFT "\xe2\x95\xb8"
#define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba" #define HORIZONTAL_HALF_RIGHT "\xe2\x95\xba"
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) { mazegrid_t mazegrid_new(size_t width, size_t height) {
mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*)); mazeedges_t** grid = calloc(height, sizeof(mazeedges_t*));
for (size_t i = 0; i < height; i++) { for (size_t i = 0; i < height; i++) {
@@ -63,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; 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 i = 0; i < g->height; i++) {
for (size_t j = 0; j < g->width; j++) { for (size_t j = 0; j < g->width; j++) {
if (i < g->height - 1) g->grid[i][j].up = (edgeweight_t)(random()%MAX_EDGE_WEIGHT); if (i < g->height - 1) g->grid[i][j].up = random_edgeweight(options);
if (j < g->width - 1) g->grid[i][j].right = (edgeweight_t)(random()%MAX_EDGE_WEIGHT); if (j < g->width - 1) g->grid[i][j].right = random_edgeweight(options);
} }
} }
} }

View File

@@ -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); 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); 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_uniform(mazegrid_t* g);
void mazegrid_print(mazegrid_t const* g, FILE * f); void mazegrid_print(mazegrid_t const* g, FILE * f);

View File

@@ -1,13 +1,21 @@
#include <mazemaker.h> #include <mazemaker.h>
#include <stdlib.h>
#include "grid.h" #include "grid.h"
#include "prim.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_t g = mazegrid_new(width, height);
mazegrid_randomize(&g); mazegrid_randomize(&g, options);
prim(&g, result); prim(&g, result);
mazegrid_free(&g); 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) { void mazemaker_free_maze(mazegrid_t* maze) {
mazegrid_free(maze); mazegrid_free(maze);
} }

View File

@@ -10,6 +10,8 @@ mazeoptions_t* mazemaker_options_new() {
// set defaults // set defaults
memset(result->wall_color, 0, 3); memset(result->wall_color, 0, 3);
memset(result->background_color, 0xff, 3); memset(result->background_color, 0xff, 3);
result->seed = 0;
result->seed_set = false;
return result; return result;
} }
@@ -78,3 +80,7 @@ int mazemaker_options_set_background_color(mazeoptions_t* options, char const* c
return stringToColor(color_desc, options->background_color); return stringToColor(color_desc, options->background_color);
} }
void mazemaker_options_set_seed(mazeoptions_t* options, rand_seed_t seed) {
options->seed = seed;
options->seed_set = true;
}

View File

@@ -2,12 +2,16 @@
#define _OPTIONS_H 1 #define _OPTIONS_H 1
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
typedef uint8_t rgb_color_t[3]; typedef uint8_t rgb_color_t[3];
typedef unsigned int rand_seed_t;
typedef struct mazeoptions { typedef struct mazeoptions {
rgb_color_t wall_color; rgb_color_t wall_color;
rgb_color_t background_color; rgb_color_t background_color;
rand_seed_t seed;
bool seed_set;
} mazeoptions_t; } mazeoptions_t;
#endif // !def(_OPTIONS_H) #endif // !def(_OPTIONS_H)

View File

@@ -4,6 +4,7 @@ pkg_search_module(POPT REQUIRED popt)
include_directories("../include" ${POPT_INCLUDE_DIRS}) include_directories("../include" ${POPT_INCLUDE_DIRS})
add_executable(mazemaker mazemaker.c) add_executable(mazemaker mazemaker.c)
target_link_directories(mazemaker PUBLIC ${POPT_LIBRARY_DIRS})
target_link_libraries(mazemaker mazemaker_shared ${POPT_LIBRARIES}) target_link_libraries(mazemaker mazemaker_shared ${POPT_LIBRARIES})
install (TARGETS mazemaker DESTINATION bin) install (TARGETS mazemaker DESTINATION bin)

View File

@@ -6,7 +6,8 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
char c; char c;
int width = 0, height = 0; 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, *seed_str = NULL;
struct poptOption options_table[] = { struct poptOption options_table[] = {
{ "width", 'w', POPT_ARG_INT, &width, 0, { "width", 'w', POPT_ARG_INT, &width, 0,
"Width of the maze", "BLOCKS" }, "Width of the maze", "BLOCKS" },
@@ -16,6 +17,8 @@ int main(int argc, char* argv[]) {
"Foreground (wall) color", "#rrggbb" }, "Foreground (wall) color", "#rrggbb" },
{ "background", 'b', POPT_ARG_STRING, &bg_color, 0, { "background", 'b', POPT_ARG_STRING, &bg_color, 0,
"Background color", "#rrggbb" }, "Background color", "#rrggbb" },
{ "seed", 's', POPT_ARG_STRING, &seed_str, 0,
"Random seed", "SEED" },
POPT_AUTOHELP POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0 } { NULL, 0, 0, NULL, 0 }
}; };
@@ -53,7 +56,11 @@ int main(int argc, char* argv[]) {
exit(1); exit(1);
} }
} }
mazemaker_generate_maze(width, height, &maze); 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); mazemaker_maze_to_png_opt(&maze, filename, options);
mazemaker_free_maze(&maze); mazemaker_free_maze(&maze);
mazemaker_options_free(options); mazemaker_options_free(options);