-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.c
491 lines (448 loc) · 13.7 KB
/
Maze.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "Maze.h"
static const bool debug = false;
/*---------------------------------------------------------------------------
Maze: Prototype Static Functions
-------------------------------------------------------------------------*/
static void InitWayPoint( WayPoint *point, int y, int x);
static void AddWayPoint( WayPoint *point, WayPoint *addpoint);
static int Lookup( WayPoint *point, WayPoint *goal, int limit, int depth);
static int LookupStartToGoal(WayPoint *stat, WayPoint *goal, const int maxLimit);
/*-----------------------------------------------------------------------------
Maze: Extern Functions
---------------------------------------------------------------------------*/
/* @ret 迷路パスの要素数
* @param sy, sx 1~
* @param ey, ex 0~
* @param height, width 1~
* @param margin 1~
*/
extern unsigned int MazePathElems(int sy, int sx, int ey, int ex, int height, int width, int margin)
{
if (margin <= 0)
margin = 1;
int a = (height - sy - ey + 1) / margin;
int b = (width - sx - ex + 1) / margin;
return a * b * margin;
}
/* MazeBuildPath: 棒倒し法による迷路の内壁パスを作成。
* @ret 迷路パスの要素数
* @param sy, sx 1~
* @param ey, ex 0~
* @param height, width 1~
* @param margin 1~
*/
extern void MazeBuildPath(Position *path, int pathElems, int sy, int sx, int ey, int ex, int height, int width, int margin)
{
int i, j, k, orgY, orgX, pc;
int dir, tmp, flag;
int w = (width - sx - ex + 1);
tmp = 0;
pc = 0;
for (i = sy - 1 + margin - 1; i < height - ey; i += margin)
{
for (j = sx - 1 + margin - 1; j < width - ex; j += margin)
{
if (pc >= pathElems)
return;
orgY = path[pc].y = i;
orgX = path[pc].x = j;
pc++;
// 鉛筆を倒す方向の決定
flag = 0;
do {
if (pc < w) {
dir = CommonGetRandom(1, 4);
} else {
dir = CommonGetRandom(2, 4);
}
if (abs(dir - tmp) == 2) {
flag = 1;
} else {
flag = 0;
}
} while (flag);
tmp = dir;
// 鉛筆を倒す
for (k = 1; k < margin; k++)
{
if (dir == 1) {// 上
path[pc].y = orgY - k;
path[pc].x = orgX;
if (path[pc].y < 0 || path[pc].y >= height)
path[pc].y = orgY;
}
else if (dir == 2) {// 右
path[pc].y = orgY;
path[pc].x = orgX + k;
if (path[pc].x < 0 || path[pc].x >= width)
path[pc].x = orgX;
}
else if (dir == 3) {// 下
path[pc].y = orgY + k;
path[pc].x = orgX;
if (path[pc].y < 0 || path[pc].y >= height)
path[pc].y = orgY;
}
else if (dir == 4) {// 左
path[pc].y = orgY;
path[pc].x = orgX - k;
if (path[pc].x < 0 || path[pc].x >= width)
path[pc].x = orgX;
}
pc++;
}
}
}
}
/*---------------------------------------------------------------------------
Maze WayPoint: ToDo
-------------------------------------------------------------------------*/
/* WayPointData構造体を初期化する。
* @return 作成するウェイポイントの総数。
*
* @param *way WayPointData構造体へのポインタ。
* @param orgy ウェイポイントの作成を始める基点座標Y。
* @param orgx ウェイポイントの作成を始める基点座標X。
* @param height ウェイポイントを作成する範囲:高さ。(0 ~ height) | (orgy ~ height-orgy).
* @param width ウェイポイントを作成する範囲:幅。(0 ~ width) | (orgx ~ width-orgx).
* @param interval ウェイポイントを置いていく間隔。
*/
extern int MazeInitWayPointData( struct WayPointData *way, int orgy, int orgx, int height, int width, int interval)
{
way->orgy = orgy;
way->orgx = orgx;
way->width = width;
way->height = height;
way->interval = interval;
way->points = 0;
way->elems = 0;
struct WayPointRouteData emptyRouteData = {};
way->route = emptyRouteData;
// 要素数を計算
int cnty = 0, cntx = 0;
for (int i= way->orgy; i< way->height; i += way->interval) cnty++;
for (int i= way->orgx; i< way->width ; i += way->interval) cntx++;
way->elems = cnty * cntx;
return way->elems;
}
/* ウェイポイントを初期化。
* @param *point 初期化するウェイポイント。
* @param y ウェイポイントの座標Y。
* @param x ウェイポイントの座標X。
*/
static void InitWayPoint( WayPoint *point, int y, int x)
{
point->pos.y = y;
point->pos.x = x;
point->linksum = 0;
for (int i= 0; i< NELEMS(point->links); ++i)
{
point->links[i] = 0;
}
}
/* WayPoint 配列のメモリを確保。
* @param *way WayPointData構造体のポインタ。
*/
extern void MazeAllocWayPoints( struct WayPointData *way )
{
way->points = (WayPoint *) malloc(sizeof(WayPoint) * way->elems);
if (! way->points) fprintf(stderr, "error: AllocWayPoints: malloc failde."), exit(2);
}
extern void MazeFreeWayPoints( struct WayPointData *way )
{
if (way->points)
{
free(way->points), way->points = 0;
}
}
extern void MazeAllocDeadend( struct WayPointData *way )
{
way->route.deadend = (WayPoint **) malloc(
sizeof(WayPoint *) * way->route.deadendElems );
if (! way->route.deadend) fprintf(stderr,"error: AllocDeadend: malloc failed."), exit(2);
}
extern void MazeFreeDeadend( struct WayPointData *way )
{
if (way->route.deadend)
{
free(way->route.deadend), way->route.deadend = 0;
}
}
/* ウェイポイントを構築。
* @param *way 初期化&メモリ確保済みのウェイポイントデータ構造体。
*/
extern void MazeBuildWayPoints( struct WayPointData *way)
{
int orgy = way->orgy;
int orgx = way->orgx;
int height = way->height;
int width = way->width;
int interval = way->interval;
int pointsElems = way->elems;
WayPoint *points = way->points;
// 初期化
int pc= 0;
for (int y= orgy; y< height; y+= interval)
{
for (int x= orgx; x< width; x+= interval)
{
if (pc >= pointsElems)// エラー
return;
InitWayPoint( &points[pc], y, x);
pc++;
}
}
// ウェイポイントリストの作成
WayPoint *curpoint;
Position environs[4];
bool flags[4];
way->route.deadendElems = 0;// 袋小路の総数を初期化
for (int i= 0; i< pointsElems; ++i)
{
curpoint = &points[i];
// 現在のポイントの、周辺座標を作成
// NORTH
environs[0].y = curpoint->pos.y - interval;
environs[0].x = curpoint->pos.x;
// SOUTH
environs[1].y = curpoint->pos.y + interval;
environs[1].x = curpoint->pos.x;
// EAST
environs[2].y = curpoint->pos.y;
environs[2].x = curpoint->pos.x + interval;
// WEST
environs[3].y = curpoint->pos.y;
environs[3].x = curpoint->pos.x - interval;
// 現在のポイントから、作成した周辺座標とのあいだの
// マップ上に、衝突物がないかを走査
for (int dir= 0; dir< 4; ++dir)
{
Position fluct = {0, 0};// 変動の方向
flags[dir] = true;
// 変動の方向を決定
switch (dir)
{
case 0:/* NORTH */ fluct.y = -1; fluct.x = 0; break;
case 1:/* SOUTH */ fluct.y = 1; fluct.x = 0; break;
case 2:/* EAST */ fluct.y = 0; fluct.x = 1; break;
case 3:/* WEST */ fluct.y = 0; fluct.x = -1; break;
}
// マップ上の衝突物を走査
for (int j= 0; j< interval; ++j)
{
int y = curpoint->pos.y + (j * fluct.y);
int x = curpoint->pos.x + (j * fluct.x);
if ((unsigned int)y >= NUM_MAP_HEIGHT ||
(unsigned int)x >= NUM_MAP_WIDTH)
{
flags[dir] = false;// 有効範囲外
break;
}
int value = MapGetFieldValue(y, x);
if ( MapIsCollisionFieldValue( value ))
{
flags[dir] = false;// 衝突物有り。フラグをバッキバキ。
break;
}
}
}
// 現在のポイントと、有効な周辺座標とをリンクする
for (int j= 0; j< pointsElems; ++j)
{
if (curpoint == &points[j])
continue;// 自身ならスルー
for (int dir= 0; dir< 4; ++dir)
{
if (flags[dir])// その方向の周辺座標が有効で
{
// ポイントリスト内のポイントの座標と一致するなら
if ( points[j].pos.y == environs[dir].y &&
points[j].pos.x == environs[dir].x )
{
// リンクする
AddWayPoint( curpoint, &points[j] );
break;
}
}
}
}
if (curpoint->linksum == 1)
{
way->route.deadendElems++;
}
}
}
/* ウェイポイントにリンクを加える。すでにリンク済みであるポイントはリンクしない。リンクが満杯のときもリンクしない。
* @param *point ウェイポイント。
* @param *addpoint 加えるポイント。
*/
static void AddWayPoint( WayPoint *point, WayPoint *addpoint)
{
for (int i= 0; i< NELEMS(point->links); ++i)
{
if (point->links[i] == addpoint)
{
return;
}
if (point->links[i] == 0)
{
point->links[i] = addpoint;
point->linksum++;
return;
}
}
}
/* ウェイポイントを描画 */
extern void MazePrintWayPoint( WayPoint *point )
{
printf("point(%2d,%2d)\n", point->pos.y, point->pos.x);
for (int i= 0; i< NELEMS(point->links); ++i)
{
if (point->links[i])
{
printf(" -> link point(%2d,%2d)\n",
point->links[i]->pos.y, point->links[i]->pos.x);
}
}
puts("--------------------------------");
}
/* ウェイポイント中の袋小路(dead end) のアドレスをポインタ配列に格納する。
* *deadend[] のメモリが確保されていない場合、警告を出してリターン(あまり意味がない)。
*/
extern void MazeSaveDeadend( struct WayPointData *way )
{
if (! way->route.deadend)
{
fprintf(stderr, "warning: BuildDeadend: route.deadend is invalid.");
return;
}
int j= 0;
for (int i= 0; i< way->elems && j< way->route.deadendElems; ++i)
{
if (way->points[i].linksum == 1)
{
way->route.deadend[j++] = &way->points[i];
}
}
}
/* ウェイポイント中の袋小路において、最長経路を持つ袋小路のペアを求める。
* LookupStartToGoal(), Lookup() を使用。
*
* @return 最長経路を発見できたら、そのペア間の距離(深さ)を返す。
失敗したら -1 を返す。
* @param *way ウェイポイントデータ構造体へのポインタ。
* @param maxLimit 反復深化深さ優先探索における、深化の限界値。
*/
extern int MazeFindMostLongRouteInDeadend( struct WayPointData *way, const int maxLimit )
{
WayPoint *stat,
*goal,
**deadend = way->route.deadend
;
int deadendElems = way->route.deadendElems,
res = -1,
long_index_i = 0,
long_index_j = 0,
cur_depth = -1
;
for (int i= 0; i< deadendElems - 1; ++i)
{
stat = deadend[i];
for (int j= i + 1; j< deadendElems; ++j)
{
goal = deadend[j];
res = LookupStartToGoal( stat, goal, maxLimit );// 探索を開始。
if (res >= 0)// 探索結果を判定。
{
if (res > cur_depth)
{
cur_depth = res;
long_index_i = i;
long_index_j = j;
}
if (debug) puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
}
if (debug)
{
printf("deadend elems: %d\n", deadendElems);
printf("found most long route is: stat(%2d,%2d)-> goal(%2d,%2d)\n",
deadend[long_index_i]->pos.y, deadend[long_index_i]->pos.x,
deadend[long_index_j]->pos.y, deadend[long_index_j]->pos.x );
}
way->route.mostLongPair[0] = *deadend[long_index_i];
way->route.mostLongPair[1] = *deadend[long_index_j];
return cur_depth;
}
/* LookupStartToGoal() の相方。再帰処理を担当する。
* param< *point > から param< *goal > に辿りつくまで再帰する。
* 使用アルゴリズム:反復深化深さ優先探索。
*
* @return goal を見つけた場合、その深さを返す。
* 失敗した場合は -1。
*
* @param *point 探索を始めるウェイポイント。
* @param *goal 目的となるウェイポイント。
* @param limit 再帰の限界値。(default 1~n)
* @param depth 再帰探索を始める深さの初期値。(default 0)
*/
static int Lookup( WayPoint *point, WayPoint *goal, int limit, int depth)
{
if (point)
{
int res = 0;
if (point == goal)
{
if (debug) printf("depth %2d: (%2d,%2d) <- found goal\n", depth, point->pos.y, point->pos.x );
return depth;// found goal.
}
if (depth >= limit)
{
return -1;
}
for (int i= 0; i< NELEMS(point->links); ++i)
{
res = Lookup( point->links[i], goal, limit, depth + 1 );// 再帰
if (res >= 0)
{
if (debug) printf("depth %2d: (%2d,%2d)\n", depth, point->pos.y, point->pos.x );
return res;// found goal
}
}
}
return -1;
}
/* Lookup() の相方。探索を始める関数。
* 1 ~ maxLimit の値で Lookuo() を呼び出す。
* 使用アルゴリズム:反復深化深さ優先探索。
*
* @return goal を見つけた場合、その深さを返す。
* 失敗した場合は -1。
* @param *stat 探索を始めるウェイポイント。
* @param *goal 目的となるウェイポイント。
* @param maxLimit 探索の限界深化値。
*/
static int LookupStartToGoal(WayPoint *stat, WayPoint *goal, const int maxLimit)
{
int res = -1;
for (int i= 1; i< maxLimit; ++i)
{
for (int j= 0; j< NELEMS(stat->links); ++j)
{
res = Lookup( stat->links[j], goal, i, 0 );
if (res >= 0)
{
// found goal.
if (debug)
printf("found route: depth %2d:(%2d,%2d)->(%2d,%2d)\n", res, stat->pos.y, stat->pos.x, goal->pos.y, goal->pos.x );
return res;
}
}
}
return res;
}