… | |
… | |
145 | int count = 0; /* # elements in dirlist */ |
145 | int count = 0; /* # elements in dirlist */ |
146 | |
146 | |
147 | /* look up */ |
147 | /* look up */ |
148 | if(yc < ysize-2 && xc > 2 && xc < xsize-2) /* it is valid to look up */ |
148 | if(yc < ysize-2 && xc > 2 && xc < xsize-2) /* it is valid to look up */ |
149 | { |
149 | { |
150 | int cleartest = (int) maze[xc][yc+1] + (int)maze[xc-1][yc+1] |
150 | int cleartest = (int) maze[xc][yc+1] + (int)maze[xc-1][yc+1] |
151 | + (int) maze[xc+1][yc+1]; |
151 | + (int) maze[xc+1][yc+1]; |
152 | cleartest += (int) maze[xc][yc+2] + (int)maze[xc-1][yc+2] |
152 | cleartest += (int) maze[xc][yc+2] + (int)maze[xc-1][yc+2] |
153 | + (int) maze[xc+1][yc+2]; |
153 | + (int) maze[xc+1][yc+2]; |
154 | |
154 | |
155 | if(cleartest == 0) { |
155 | if(cleartest == 0) { |
156 | dirlist[count] = 1; |
156 | dirlist[count] = 1; |
157 | count++; |
157 | count++; |
158 | } |
158 | } |
159 | } |
159 | } |
160 | |
160 | |
161 | |
161 | |
162 | /* look down */ |
162 | /* look down */ |
163 | if(yc > 2 && xc > 2 && xc < xsize-2) /* it is valid to look down */ |
163 | if(yc > 2 && xc > 2 && xc < xsize-2) /* it is valid to look down */ |
164 | { |
164 | { |
165 | int cleartest = (int) maze[xc][yc-1] + (int)maze[xc-1][yc-1] |
165 | int cleartest = (int) maze[xc][yc-1] + (int)maze[xc-1][yc-1] |
166 | + (int) maze[xc+1][yc-1]; |
166 | + (int) maze[xc+1][yc-1]; |
167 | cleartest += (int) maze[xc][yc-2] + (int)maze[xc-1][yc-2] |
167 | cleartest += (int) maze[xc][yc-2] + (int)maze[xc-1][yc-2] |
168 | + (int) maze[xc+1][yc-2]; |
168 | + (int) maze[xc+1][yc-2]; |
169 | |
169 | |
170 | if(cleartest == 0) { |
170 | if(cleartest == 0) { |
171 | dirlist[count] = 2; |
171 | dirlist[count] = 2; |
172 | count++; |
172 | count++; |
173 | } |
173 | } |
174 | } |
174 | } |
175 | |
175 | |
176 | |
176 | |
177 | /* look right */ |
177 | /* look right */ |
178 | if(xc < xsize- 2 && yc > 2 && yc < ysize-2) /* it is valid to look left */ |
178 | if(xc < xsize- 2 && yc > 2 && yc < ysize-2) /* it is valid to look left */ |
179 | { |
179 | { |
180 | int cleartest = (int) maze[xc+1][yc] + (int)maze[xc+1][yc-1] |
180 | int cleartest = (int) maze[xc+1][yc] + (int)maze[xc+1][yc-1] |
181 | + (int) maze[xc+1][yc+1]; |
181 | + (int) maze[xc+1][yc+1]; |
182 | cleartest += (int) maze[xc+2][yc] + (int)maze[xc+2][yc-1] |
182 | cleartest += (int) maze[xc+2][yc] + (int)maze[xc+2][yc-1] |
183 | + (int) maze[xc+2][yc+1]; |
183 | + (int) maze[xc+2][yc+1]; |
184 | |
184 | |
185 | if(cleartest == 0) { |
185 | if(cleartest == 0) { |
186 | dirlist[count] = 3; |
186 | dirlist[count] = 3; |
187 | count++; |
187 | count++; |
188 | } |
188 | } |
189 | } |
189 | } |
190 | |
190 | |
191 | |
191 | |
192 | /* look left */ |
192 | /* look left */ |
193 | if(xc > 2 && yc > 2 && yc < ysize-2) /* it is valid to look down */ |
193 | if(xc > 2 && yc > 2 && yc < ysize-2) /* it is valid to look down */ |
194 | { |
194 | { |
195 | int cleartest = (int) maze[xc-1][yc] + (int)maze[xc-1][yc-1] |
195 | int cleartest = (int) maze[xc-1][yc] + (int)maze[xc-1][yc-1] |
196 | + (int) maze[xc-1][yc+1]; |
196 | + (int) maze[xc-1][yc+1]; |
197 | cleartest += (int) maze[xc-2][yc] + (int)maze[xc-2][yc-1] |
197 | cleartest += (int) maze[xc-2][yc] + (int)maze[xc-2][yc-1] |
198 | + (int) maze[xc-2][yc+1]; |
198 | + (int) maze[xc-2][yc+1]; |
199 | |
199 | |
200 | if(cleartest == 0) { |
200 | if(cleartest == 0) { |
201 | dirlist[count] = 4; |
201 | dirlist[count] = 4; |
202 | count++; |
202 | count++; |
203 | } |
203 | } |
204 | } |
204 | } |
205 | |
205 | |
206 | if(count==0) return -1; /* failed to find any clear points */ |
206 | if(count==0) return -1; /* failed to find any clear points */ |
207 | |
207 | |
208 | /* choose a random direction */ |
208 | /* choose a random direction */ |
209 | if(count > 1) count = RANDOM() % count; |
209 | if(count > 1) count = RANDOM() % count; |
210 | else count=0; |
210 | else count=0; |
211 | switch(dirlist[count]) { |
211 | switch(dirlist[count]) { |
212 | case 1: /* up */ |
212 | case 1: /* up */ |
213 | { |
213 | { |
214 | *y = yc +1; |
214 | *y = yc +1; |
215 | *x = xc; |
215 | *x = xc; |
216 | break; |
216 | break; |
217 | }; |
217 | }; |
218 | case 2: /* down */ |
218 | case 2: /* down */ |
219 | { |
219 | { |
220 | *y = yc-1; |
220 | *y = yc-1; |
221 | *x = xc; |
221 | *x = xc; |
222 | break; |
222 | break; |
223 | }; |
223 | }; |
224 | case 3: /* right */ |
224 | case 3: /* right */ |
225 | { |
225 | { |
226 | *y = yc; |
226 | *y = yc; |
227 | *x = xc+1; |
227 | *x = xc+1; |
228 | break; |
228 | break; |
229 | } |
229 | } |
230 | case 4: /* left */ |
230 | case 4: /* left */ |
231 | { |
231 | { |
232 | *x = xc-1; |
232 | *x = xc-1; |
233 | *y = yc; |
233 | *y = yc; |
234 | break; |
234 | break; |
235 | } |
235 | } |
236 | default: /* ??? */ |
236 | default: /* ??? */ |
237 | { |
237 | { |
238 | return -1; |
238 | return -1; |
239 | } |
239 | } |
240 | } |
240 | } |
241 | return 1; |
241 | return 1; |
242 | } |
242 | } |
243 | |
243 | |
244 | /* recursive routine which will fill every available space in the maze |
244 | /* recursive routine which will fill every available space in the maze |
245 | with walls*/ |
245 | with walls*/ |
246 | |
246 | |
247 | void fill_maze_full(char **maze, int x, int y, int xsize, int ysize ) { |
247 | void fill_maze_full(char **maze, int x, int y, int xsize, int ysize ) { |
248 | int xc,yc; |
248 | int xc,yc; |
249 | |
249 | |
250 | /* write a wall here */ |
250 | /* write a wall here */ |
… | |
… | |
262 | } |
262 | } |
263 | } |
263 | } |
264 | |
264 | |
265 | |
265 | |
266 | /* recursive routine which will fill much of the maze, but will leave |
266 | /* recursive routine which will fill much of the maze, but will leave |
267 | some free spots (possibly large) toward the center.*/ |
267 | some free spots (possibly large) toward the center.*/ |
268 | |
268 | |
269 | void fill_maze_sparse(char **maze, int x, int y, int xsize, int ysize ) { |
269 | void fill_maze_sparse(char **maze, int x, int y, int xsize, int ysize ) { |
270 | int xc,yc; |
270 | int xc,yc; |
271 | |
271 | |
272 | /* write a wall here */ |
272 | /* write a wall here */ |