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
|
/*
* ksokoban - a Sokoban game for TDE
* Copyright (C) 1998 Anders Widell <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#include <stdio.h>
#include "PathFinder.h"
#include "LevelMap.h"
#include "Queue.h"
#include "Move.h"
void
PathFinder::BFS (int _x, int _y) {
Queue<int, 10> xq;
Queue<int, 10> yq;
Queue<int, 10> dq;
int x, y, d;
xq.enqueue (_x);
yq.enqueue (_y);
dq.enqueue (1);
while (!xq.empty ()) {
x = xq.dequeue ();
y = yq.dequeue ();
d = dq.dequeue ();
if (x<0 || x>MAX_X || y<0 || y>MAX_Y || dist[y][x]) continue;
dist[y][x] = d;
xq.enqueue (x);
xq.enqueue (x);
xq.enqueue (x-1);
xq.enqueue (x+1);
yq.enqueue (y-1);
yq.enqueue (y+1);
yq.enqueue (y);
yq.enqueue (y);
dq.enqueue (d+1);
dq.enqueue (d+1);
dq.enqueue (d+1);
dq.enqueue (d+1);
}
}
Move *
PathFinder::search (Map *_map, int _x, int _y) {
int xpos=_map->xpos ();
int ypos=_map->ypos ();
if (xpos == _x && ypos == _y) return 0;
for (int y=0; y<=MAX_Y; y++) {
for (int x=0; x<=MAX_X; x++) {
if (_map->empty (x, y)) dist[y][x] = 0;
else dist[y][x] = PATH_WALL;
}
}
BFS (_x, _y);
#if 0
for (int y=0; y<=MAX_Y; y++) {
for (int x=0; x<=MAX_X; x++) {
//if (x==_x && y==_y) {printf ("++ "); continue;}
//if (x==xpos && y==ypos) {printf ("@@ "); continue;}
if (dist[y][x] == PATH_WALL) {printf ("## "); continue;}
printf ("%02d ", dist[y][x]);
}
printf ("\n");
}
#endif
int d;
Move *move=new Move (xpos, ypos);
int oldX, oldY;
for (;;) {
oldX = xpos;
oldY = ypos;
if (xpos == _x && ypos == _y) {
move->finish ();
//printf ("move->finish ()\n");
return move;
}
d = dist[ypos][xpos];
while (ypos-1 >= 0 && dist[ypos-1][xpos] < d) {
ypos--;
d = dist[ypos][xpos];
}
if (oldY != ypos) {
move->step (xpos, ypos);
//printf ("step (%d, %d)\n", xpos, ypos);
continue;
}
while (ypos+1 <= MAX_Y && dist[ypos+1][xpos] < d) {
ypos++;
d = dist[ypos][xpos];
}
if (oldY != ypos) {
move->step (xpos, ypos);
//printf ("step (%d, %d)\n", xpos, ypos);
continue;
}
while (xpos-1 >= 0 && dist[ypos][xpos-1] < d) {
xpos--;
d = dist[ypos][xpos];
}
if (oldX != xpos) {
move->step (xpos, ypos);
//printf ("step (%d, %d)\n", xpos, ypos);
continue;
}
while (xpos+1 <= MAX_X && dist[ypos][xpos+1] < d) {
xpos++;
d = dist[ypos][xpos];
}
if (oldX != xpos) {
move->step (xpos, ypos);
//printf ("step (%d, %d)\n", xpos, ypos);
continue;
}
delete move;
return 0;
}
}
Move*
PathFinder::drag(int /* x1 */, int /* y1 */, int /* x2 */, int /* y2 */) {
return 0;
}
bool
PathFinder::canDrag(int /* x */, int /* y */) const {
return false;
}
bool
PathFinder::canWalkTo(int /* x */, int /* y */) const {
return false;
}
bool
PathFinder::canDragTo(int /* x */, int /* y */) const {
return false;
}
void
PathFinder::updatePossibleMoves() {
}
void
PathFinder::updatePossibleDestinations(int /* x */, int /* y */) {
}
|