-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
186 lines (171 loc) · 5.75 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* main.c
* MazeCubeGen: maze cube generator
*
* Copyright (c) 2020-2024 Bryan Franklin. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "maze.h"
#include "maze-export.h"
static char *configStr = NULL;
static char *inputFile = NULL;
static char *outputFile = NULL;
static char *stlFile = NULL;
static char *stlFileFlat = NULL;
static char *stlSolFile = NULL;
static char *gvFile = NULL;
static void free_buffers() {
if(configStr!=NULL) free(configStr);
if(inputFile!=NULL) free(inputFile);
if(outputFile!=NULL) free(outputFile);
if(stlFile!=NULL) free(stlFile);
if(stlFileFlat!=NULL) free(stlFileFlat);
if(stlSolFile!=NULL) free(stlSolFile);
if(gvFile!=NULL) free(gvFile);
}
static void show_help(int argc, char **argv) {
free_buffers();
printf("%s {-d l,w,h[:o] | -i file.txt} [-h] [-s] [-r seed] [-l length] {-m file.stl | -o file.txt} [-f flat.stl] [-k maxSegments] [-p solution.stl] [-g graph.gv] [-e edgeWidth] [-x scale]\n\n",
argv[0]);
exit(0);
}
int main(int argc, char **argv) {
maze_t maze;
int genMaze = 0;
int doSolve = 0;
int minSolutionLen = -1;
int seed = 0;
int maxSegments = -1;
double scale = 1.0;
double edgeWidth = 0.4;
/* set defaults */
genMaze = 0;
char ch='\0';
while( (ch=getopt(argc, argv, "d:e:f:g:hi:k:l:m:o:p:r:sx:"))!=-1 ) {
switch(ch) {
case 'd':
configStr = strdup(optarg);
genMaze = 1;
break;
case 'e':
edgeWidth = atof(optarg);
break;
case 'f':
/* output flat STL model to file given as argument */
stlFileFlat = strdup(optarg);
break;
case 'g':
/* output graphviz gv to file given as argument */
gvFile = strdup(optarg);
break;
case 'h':
case '?':
show_help(argc,argv);
break;
case 'i':
/* load maze from file given as argument */
inputFile = strdup(optarg);
genMaze = 0;
break;
case 'l':
/* minimum solution length */
minSolutionLen = atoi(optarg);
break;
case 'k':
maxSegments = atoi(optarg);
break;
case 'm':
/* output STL model to file given as argument */
stlFile = strdup(optarg);
break;
case 'o':
/* output maze to file given as argument */
outputFile = strdup(optarg);
break;
case 'p':
/* output solution STL model to file given as argument */
stlSolFile = strdup(optarg);
break;
case 'r':
seed = atoi(optarg);
srand(seed);
break;
case 's':
/* generate a solution */
doSolve = 1;
break;
case 'x':
scale = atof(optarg);
break;
}
}
/* check that sufficient settings exist */
if( inputFile==NULL && !genMaze ) {
fprintf(stderr,"\n\nNo maze source given, use -i to specify an input filename or -g to generate a random maze.\n\n");
show_help(argc,argv);
}
if( outputFile==NULL && stlFile==NULL && stlSolFile==NULL && stlFileFlat==NULL && gvFile==NULL ) {
fprintf(stderr,"\n\nNo output specified, use -o and/or -m to specify an output filename.\n\n");
show_help(argc,argv);
}
/* generate/load maze and solve if requested/needed */
if( genMaze ) {
/* create a new maze */
if( configStr )
maze_init_str(&maze, configStr);
else
maze_init_str(&maze, "11x11x11");
if( maxSegments > 0 )
maze_set_segments(&maze, maxSegments);
if( minSolutionLen > 0 )
maze_set_path_length(&maze, minSolutionLen);
printf("Generating maze.\n");
int segments = maze_generate(&maze);
if( maxSegments > 0 )
printf("%i maze segment%s.\n", segments, (segments==1)?"":"s");
}
else if( inputFile ) {
/* load a maze from an input file */
printf("Loading maze from '%s'.\n", inputFile);
if( maze_load(&maze,inputFile) < 0 ) {
fprintf(stderr, "Failed to load maze from '%s'\n", inputFile);
free_buffers();
exit(1);
}
}
/* solve the maze, if requested/needed */
if( doSolve || minSolutionLen>0 ) {
maze_solve(&maze);
if( minSolutionLen>0 )
printf("solution length is %i.\n", maze.solution.num);
}
/* write requested output */
if( outputFile ) {
printf("Writing %iD maze to `%s`.\n", maze.numDimensions, outputFile);
maze_write(&maze, outputFile);
}
if( stlFile ) {
printf("Exporting %iD maze to STL file `%s`.\n", maze.numDimensions, stlFile);
maze_export_stl(&maze, stlFile);
}
if( stlSolFile ) {
printf("Exporting %iD maze solution to STL file `%s`.\n", maze.numDimensions, stlFile);
maze_export_stl_solution(&maze, stlSolFile);
}
if( stlFileFlat ) {
printf("Exporting %iD maze to flat-packed STL file `%s`.\n", maze.numDimensions, stlFileFlat);
maze_export_stl_flat(&maze, stlFileFlat, edgeWidth, scale);
}
if( gvFile ) {
printf("Exporting %iD maze to graphviz gv file `%s`.\n", maze.numDimensions, gvFile);
maze_export_gv(&maze, gvFile);
}
maze_metrics(&maze);
/* free memory */
maze_free(&maze);
free_buffers();
return 0;
}