… | |
… | |
30 | |
30 | |
31 | */ |
31 | */ |
32 | #include <global.h> |
32 | #include <global.h> |
33 | #include <random_map.h> |
33 | #include <random_map.h> |
34 | |
34 | |
35 | #define RANDOM_OPTIONS 0 /* Pick random options below */ |
35 | #define RANDOM_OPTIONS 0 /* Pick random options below */ |
36 | #define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly*/ |
36 | #define REGULAR_SPIRAL 1 /* Regular spiral--distance increases constantly */ |
37 | #define FINE_SPIRAL 2 /* uses the min. separation: most coiling */ |
37 | #define FINE_SPIRAL 2 /* uses the min. separation: most coiling */ |
38 | #define FIT_SPIRAL 4 /* scale to a rectangular region, not square */ |
38 | #define FIT_SPIRAL 4 /* scale to a rectangular region, not square */ |
39 | #define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */ |
39 | #define MAX_SPIRAL_OPT 8 /* this should be 2x the last real option */ |
40 | #include <math.h> |
40 | #include <math.h> |
41 | |
41 | |
42 | #ifndef MIN |
42 | #ifndef MIN |
43 | #define MIN(x,y) (((x)<(y))? (x):(y)) |
43 | # define MIN(x,y) (((x)<(y))? (x):(y)) |
44 | #endif |
44 | #endif |
45 | #ifndef MAX |
45 | #ifndef MAX |
46 | #define MAX(x,y) (((x)<(y))? (y):(x)) |
46 | # define MAX(x,y) (((x)<(y))? (y):(x)) |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | #define MINDIST 3 |
49 | #define MINDIST 3 |
50 | |
50 | |
51 | #define MAX_FINE .454545 |
51 | #define MAX_FINE .454545 |
52 | |
52 | |
53 | extern int surround_check(char **maze,int i, int j, int xsize, int ysize); |
53 | extern int surround_check (char **maze, int i, int j, int xsize, int ysize); |
54 | |
54 | |
|
|
55 | char ** |
55 | char **map_gen_spiral(int xsize, int ysize, int option) { |
56 | map_gen_spiral (int xsize, int ysize, int option) |
|
|
57 | { |
56 | int i,j; |
58 | int i, j; |
57 | float parm=0; |
59 | float parm = 0; |
58 | float x=0,y=0; |
60 | float x = 0, y = 0; |
59 | int ic,jc; |
61 | int ic, jc; |
60 | float SizeX,SizeY; |
62 | float SizeX, SizeY; |
61 | float xscale,yscale; |
63 | float xscale, yscale; |
62 | |
64 | |
63 | /* allocate that array, set it up */ |
65 | /* allocate that array, set it up */ |
64 | char **maze = (char **)calloc(sizeof(char*),xsize); |
66 | char **maze = (char **) calloc (sizeof (char *), xsize); |
|
|
67 | |
65 | for(i=0;i<xsize;i++) { |
68 | for (i = 0; i < xsize; i++) |
|
|
69 | { |
66 | maze[i] = (char *) calloc(sizeof(char),ysize); |
70 | maze[i] = (char *) calloc (sizeof (char), ysize); |
67 | } |
71 | } |
68 | |
72 | |
69 | /* slightly easier to fill and then cut */ |
73 | /* slightly easier to fill and then cut */ |
70 | for(i=0;i<xsize;i++) for(j=0;j<ysize;j++) maze[i][j] = '#'; |
74 | for (i = 0; i < xsize; i++) |
|
|
75 | for (j = 0; j < ysize; j++) |
|
|
76 | maze[i][j] = '#'; |
71 | |
77 | |
72 | ic = xsize/2; |
78 | ic = xsize / 2; |
73 | jc = ysize/2; |
79 | jc = ysize / 2; |
74 | SizeX = xsize/2 - 2; |
80 | SizeX = xsize / 2 - 2; |
75 | SizeY = ysize/2 -2; |
81 | SizeY = ysize / 2 - 2; |
76 | |
82 | |
77 | /* select random options if necessary */ |
83 | /* select random options if necessary */ |
78 | if(option==0) { |
84 | if (option == 0) |
|
|
85 | { |
79 | option=RANDOM()%MAX_SPIRAL_OPT; |
86 | option = RANDOM () % MAX_SPIRAL_OPT; |
80 | } |
87 | } |
81 | |
88 | |
82 | /* the order in which these are evaluated matters*/ |
89 | /* the order in which these are evaluated matters */ |
83 | |
90 | |
84 | /* the following two are mutually exclusive. |
91 | /* the following two are mutually exclusive. |
85 | pick one if they're both set. */ |
92 | pick one if they're both set. */ |
86 | if((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL)) |
93 | if ((option & REGULAR_SPIRAL) && (option & FIT_SPIRAL)) |
87 | { |
94 | { |
88 | /* unset REGULAR_SPIRAL half the time */ |
95 | /* unset REGULAR_SPIRAL half the time */ |
89 | if(RANDOM()%2 && (option & REGULAR_SPIRAL)) |
96 | if (RANDOM () % 2 && (option & REGULAR_SPIRAL)) |
90 | option -= REGULAR_SPIRAL; |
97 | option -= REGULAR_SPIRAL; |
91 | else |
98 | else |
92 | option -= FIT_SPIRAL; |
99 | option -= FIT_SPIRAL; |
93 | } |
100 | } |
94 | |
101 | |
95 | xscale=yscale=MAX_FINE; /* fine spiral */ |
102 | xscale = yscale = MAX_FINE; /* fine spiral */ |
96 | |
103 | |
97 | /* choose the spiral pitch */ |
104 | /* choose the spiral pitch */ |
98 | if(! (option & FINE_SPIRAL) ) { |
105 | if (!(option & FINE_SPIRAL)) |
|
|
106 | { |
99 | float pitch = (RANDOM() %5)/10. + 10./22.; |
107 | float pitch = (RANDOM () % 5) / 10. + 10. / 22.; |
|
|
108 | |
100 | xscale=yscale=pitch; |
109 | xscale = yscale = pitch; |
101 | } |
110 | } |
102 | |
111 | |
103 | if((option & FIT_SPIRAL) && (xsize!=ysize) ) { |
112 | if ((option & FIT_SPIRAL) && (xsize != ysize)) |
|
|
113 | { |
|
|
114 | if (xsize > ysize) |
104 | if(xsize > ysize) xscale *= (float)xsize/(float)ysize; |
115 | xscale *= (float) xsize / (float) ysize; |
|
|
116 | else |
105 | else yscale *= (float)ysize/(float)xsize; |
117 | yscale *= (float) ysize / (float) xsize; |
106 | } |
118 | } |
107 | |
119 | |
108 | if(option & REGULAR_SPIRAL) { |
120 | if (option & REGULAR_SPIRAL) |
|
|
121 | { |
109 | float scale = MIN(xscale,yscale); |
122 | float scale = MIN (xscale, yscale); |
|
|
123 | |
110 | xscale=yscale=scale; |
124 | xscale = yscale = scale; |
111 | } |
125 | } |
112 | |
126 | |
113 | /* cut out the spiral */ |
127 | /* cut out the spiral */ |
114 | while( (fabs(x) < SizeX) && (fabs(y) < SizeY) ) |
128 | while ((fabs (x) < SizeX) && (fabs (y) < SizeY)) |
115 | { |
129 | { |
116 | x = parm * cos(parm)*xscale; |
130 | x = parm * cos (parm) * xscale; |
117 | y = parm * sin(parm)*yscale; |
131 | y = parm * sin (parm) * yscale; |
118 | maze[(int)(ic + x )][(int)(jc + y )]='\0'; |
132 | maze[(int) (ic + x)][(int) (jc + y)] = '\0'; |
119 | parm+=0.01; |
133 | parm += 0.01; |
120 | }; |
134 | }; |
121 | |
135 | |
122 | maze[(int)(ic + x+0.5)][(int)(jc + y+0.5)]='<'; |
136 | maze[(int) (ic + x + 0.5)][(int) (jc + y + 0.5)] = '<'; |
123 | |
137 | |
124 | |
138 | |
125 | /* cut out the center in a 2x2 and place the center and downexit */ |
139 | /* cut out the center in a 2x2 and place the center and downexit */ |
126 | maze[ic][jc+1]='>'; |
140 | maze[ic][jc + 1] = '>'; |
127 | maze[ic][jc]='C'; |
141 | maze[ic][jc] = 'C'; |
128 | |
142 | |
129 | |
143 | |
130 | return maze; |
144 | return maze; |
131 | } |
145 | } |
132 | |
146 | |
133 | /* the following function connects disjoint spirals which may |
147 | /* the following function connects disjoint spirals which may |
134 | result from the symmetrization process. */ |
148 | result from the symmetrization process. */ |
|
|
149 | void |
135 | void connect_spirals(int xsize,int ysize,int sym, char **layout) { |
150 | connect_spirals (int xsize, int ysize, int sym, char **layout) |
|
|
151 | { |
136 | |
152 | |
137 | int i,j,ic=xsize/2,jc=ysize/2; |
153 | int i, j, ic = xsize / 2, jc = ysize / 2; |
138 | |
154 | |
139 | if(sym==X_SYM) { |
155 | if (sym == X_SYM) |
|
|
156 | { |
140 | layout[ic][jc] = 0; |
157 | layout[ic][jc] = 0; |
141 | /* go left from map center */ |
158 | /* go left from map center */ |
142 | for(i=ic-1,j=jc; i>0 && layout[i][j]=='#' ;i--) |
159 | for (i = ic - 1, j = jc; i > 0 && layout[i][j] == '#'; i--) |
143 | layout[i][j]=0; |
160 | layout[i][j] = 0; |
144 | /* go right */ |
161 | /* go right */ |
145 | for(i=ic+1,j=jc; i<xsize-1 && layout[i][j]=='#' ;i++) |
162 | for (i = ic + 1, j = jc; i < xsize - 1 && layout[i][j] == '#'; i++) |
146 | layout[i][j]=0; |
163 | layout[i][j] = 0; |
147 | } |
164 | } |
148 | |
165 | |
149 | if(sym==Y_SYM) { |
166 | if (sym == Y_SYM) |
|
|
167 | { |
150 | |
168 | |
151 | layout[ic][jc] = 0; |
169 | layout[ic][jc] = 0; |
152 | /* go up */ |
170 | /* go up */ |
153 | for(i=ic,j=jc-1; j>0 && layout[i][j]=='#' ;j--) |
171 | for (i = ic, j = jc - 1; j > 0 && layout[i][j] == '#'; j--) |
154 | layout[i][j]=0; |
172 | layout[i][j] = 0; |
155 | /* go down */ |
173 | /* go down */ |
156 | for(i=ic,j=jc+1; j<ysize-1 && layout[i][j]=='#' ;j++) |
174 | for (i = ic, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++) |
157 | layout[i][j]=0; |
175 | layout[i][j] = 0; |
158 | } |
176 | } |
159 | |
177 | |
160 | if(sym==XY_SYM) { |
178 | if (sym == XY_SYM) |
|
|
179 | { |
161 | /* go left from map center */ |
180 | /* go left from map center */ |
162 | layout[ic][jc/2]=0; |
181 | layout[ic][jc / 2] = 0; |
163 | layout[ic/2][jc]=0; |
182 | layout[ic / 2][jc] = 0; |
164 | layout[ic][jc/2+jc]=0; |
183 | layout[ic][jc / 2 + jc] = 0; |
165 | layout[ic/2+ic][jc]=0; |
184 | layout[ic / 2 + ic][jc] = 0; |
166 | for(i=ic-1,j=jc/2; i>0 && layout[i][j]=='#' ;i--) { |
185 | for (i = ic - 1, j = jc / 2; i > 0 && layout[i][j] == '#'; i--) |
|
|
186 | { |
167 | layout[i][j + jc]=0; |
187 | layout[i][j + jc] = 0; |
168 | layout[i][j]=0; |
188 | layout[i][j] = 0; |
169 | } |
189 | } |
170 | /* go right */ |
190 | /* go right */ |
171 | for(i=ic+1,j=jc/2; i<xsize-1 && layout[i][j]=='#' ;i++) { |
191 | for (i = ic + 1, j = jc / 2; i < xsize - 1 && layout[i][j] == '#'; i++) |
|
|
192 | { |
172 | layout[i][j+jc]=0; |
193 | layout[i][j + jc] = 0; |
173 | layout[i][j]=0; |
194 | layout[i][j] = 0; |
174 | } |
195 | } |
175 | /* go up */ |
196 | /* go up */ |
176 | for(i=ic/2,j=jc-1; j>0 && layout[i][j]=='#' ;j--) { |
197 | for (i = ic / 2, j = jc - 1; j > 0 && layout[i][j] == '#'; j--) |
|
|
198 | { |
177 | layout[i][j]=0; |
199 | layout[i][j] = 0; |
178 | layout[i+ic][j]=0; |
200 | layout[i + ic][j] = 0; |
179 | } |
201 | } |
180 | /* go down */ |
202 | /* go down */ |
181 | for(i=ic/2,j=jc+1; j<ysize-1 && layout[i][j]=='#' ;j++) { |
203 | for (i = ic / 2, j = jc + 1; j < ysize - 1 && layout[i][j] == '#'; j++) |
|
|
204 | { |
182 | layout[i][j]=0; |
205 | layout[i][j] = 0; |
183 | layout[i+ic][j]=0; |
206 | layout[i + ic][j] = 0; |
184 | } |
207 | } |
185 | |
208 | |
186 | } |
209 | } |
187 | |
210 | |
188 | /* get rid of bad doors. */ |
211 | /* get rid of bad doors. */ |
189 | for(i=0;i<xsize;i++) |
212 | for (i = 0; i < xsize; i++) |
190 | for(j=0;j<ysize;j++) { |
213 | for (j = 0; j < ysize; j++) |
191 | if(layout[i][j]=='D') { /* remove bad door. */ |
214 | { |
|
|
215 | if (layout[i][j] == 'D') |
|
|
216 | { /* remove bad door. */ |
192 | int si = surround_check(layout,i,j,xsize,ysize); |
217 | int si = surround_check (layout, i, j, xsize, ysize); |
|
|
218 | |
193 | if(si!=3 && si!=12) { |
219 | if (si != 3 && si != 12) |
|
|
220 | { |
194 | layout[i][j]=0; |
221 | layout[i][j] = 0; |
195 | /* back up and recheck any nearby doors */ |
222 | /* back up and recheck any nearby doors */ |
196 | i=0;j=0; |
223 | i = 0; |
|
|
224 | j = 0; |
|
|
225 | } |
197 | } |
226 | } |
198 | } |
227 | } |
199 | } |
228 | |
200 | |
|
|
201 | |
229 | |
202 | |
230 | |
203 | } |
231 | } |
204 | |
|
|