Debian Codegraph @

terps/jacl/findroute.c

raw symbols SCIP
1 /* findroute.c --- Finds a route from one location to another
2 * (C) 2008 David Disher, distribute and use
3 * according to GNU GPL, see file COPYING for details.
4 */
5
6 #include "jacl.h"
7 #include "language.h"
8 #include "types.h"
9 #include "prototypes.h"
10
11 /**************************************/
12 /* Queue functions */
13 /**************************************/
14
15 typedef struct QueueNode
16 {
17 int val;
18 int val2;
19 struct QueueNode *next;
20 } QueueNode;
21
22 typedef struct
23 {
24 QueueNode *head;
25 QueueNode *tail;
26 } Queue;
27
28 static void
29 qInit(Queue *q)
30 {
31 q->head = q->tail = NULL;
32 }
33
34 static void
35 qDelete(Queue *q)
36 {
37 QueueNode *node, *next;
38
39 for (node = q->head;node != NULL;node = next)
40 {
41 next = node->next;
42 free(node);
43 }
44
45 q->head = q->tail = NULL;
46 }
47
48 static int
49 qIsEmpty(Queue *q)
50 {
51 return (q->head == NULL);
52 }
53
54 static void
55 qDebug(Queue *q)
56 {
57 printf("Queue:");
58
59 if (q->head == NULL)
60 {
61 printf(" empty");
62 }
63 else
64 {
65 QueueNode *node;
66 for (node = q->head;node != NULL;node = node->next)
67 {
68 printf(" %d (%d)", node->val, node->val2);
69 }
70 }
71
72 putchar('\n');
73 }
74
75 static void
76 qAppend(Queue *q, int val, int val2)
77 {
78 QueueNode *node = (QueueNode*) malloc(sizeof(QueueNode));
79 node->val = val;
80 node->val2 = val2;
81 node->next = NULL;
82
83 if (q->head == NULL)
84 {
85 q->head = q->tail = node;
86 }
87 else
88 {
89 q->tail->next = node;
90 q->tail = node;
91 }
92 }
93
94 static void
95 qPop(Queue *q, int *val, int *val2)
96 {
97 //assert(q->head != NULL);
98
99 *val = q->head->val;
100 *val2 = q->head->val2;
101
102 if (q->head == q->tail)
103 {
104 q->head = q->tail = NULL;
105 }
106 else
107 {
108 q->head = q->head->next;
109 }
110 }
111
112 static void
113 qTest(void)
114 {
115 int val, val2;
116 Queue q;
117
118 qInit(&q);
119 qDebug(&q);
120
121 printf("\nAdd 3, 0\n");
122 qAppend(&q, 3, 0);
123 qDebug(&q);
124
125 printf("\nAdd 4, 2\n");
126 qAppend(&q, 4, 2);
127 qDebug(&q);
128
129 printf("\nAdd 5, 1\n");
130 qAppend(&q, 5, 1);
131 qDebug(&q);
132
133 qPop(&q, &val, &val2);
134 printf("\nPop %d, %d\n", val, val2);
135 qDebug(&q);
136
137 qPop(&q, &val, &val2);
138 printf("\nPop %d, %d\n", val, val2);
139 qDebug(&q);
140
141 printf("\nAdd 6, 3\n");
142 qAppend(&q, 6, 3);
143 qDebug(&q);
144
145 printf("\nDelete all\n");
146 qDelete(&q);
147 qDebug(&q);
148 }
149
150 /**************************************/
151 /* Set functions */
152 /**************************************/
153
154 /* linked list for hash table */
155 typedef struct SetNode
156 {
157 int val;
158 struct SetNode *next;
159 } SetNode;
160
161 #define SET_HASHSIZE 101
162
163 typedef struct
164 {
165 SetNode *node[SET_HASHSIZE];
166 } Set;
167
168 static void
169 setInit(Set *set)
170 {
171 int n;
172
173 for (n = 0;n < SET_HASHSIZE;n++)
174 {
175 set->node[n] = NULL;
176 }
177 }
178
179 static void
180 setDelete(Set *set)
181 {
182 int n;
183
184 for (n = 0;n < SET_HASHSIZE;n++)
185 {
186 SetNode *node, *next;
187
188 for (node = set->node[n];node != NULL;node = next)
189 {
190 next = node->next;
191 free(node);
192 }
193
194 set->node[n] = NULL;
195 }
196 }
197
198 static void
199 setDebug(Set *set)
200 {
201 int n;
202
203 printf("Set:");
204
205 for (n = 0;n < SET_HASHSIZE;n++)
206 {
207 SetNode *node;
208
209 for (node = set->node[n];node != NULL;node = node->next)
210 {
211 printf(" %d", node->val);
212 }
213 }
214
215 putchar('\n');
216 }
217
218 static int
219 setHash(int val)
220 {
221 return abs(val) % SET_HASHSIZE;
222 }
223
224 static void
225 setAdd(Set *set, int val)
226 {
227 SetNode *node;
228 int n = setHash(val);
229
230 /* check if val is already in the set */
231
232 for (node = set->node[n];node != NULL;node = node->next)
233 {
234 if (node->val == val) { return; }
235 }
236
237 node = (SetNode*) malloc(sizeof(SetNode));
238 node->val = val;
239 node->next = set->node[n];
240 set->node[n] = node;
241 }
242
243 /* returns 1 if the set contains val, otherwise returns 0 */
244
245 static int
246 setContains(Set *set, int val)
247 {
248 SetNode *node;
249 int n = setHash(val);
250
251 for (node = set->node[n];node != NULL;node = node->next)
252 {
253 if (node->val == val) { return 1; }
254 }
255
256 return 0;
257 }
258
259 static void setTest(void)
260 {
261 Set s;
262
263 setInit(&s);
264 setDebug(&s);
265
266 printf("\nAdd 34\n");
267 setAdd(&s, 34);
268 setDebug(&s);
269
270 printf("\nAdd 56\n");
271 setAdd(&s, 56);
272 setDebug(&s);
273
274 printf("\nAdd 34 again\n");
275 setAdd(&s, 34);
276 setDebug(&s);
277
278 printf("\nAdd %d\n", 34 + SET_HASHSIZE);
279 setAdd(&s, 34 + SET_HASHSIZE);
280 setDebug(&s);
281
282 printf("\nAdd 78\n");
283 setAdd(&s, 78);
284 setDebug(&s);
285
286 printf("\nDelete all\n");
287 setDelete(&s);
288 setDebug(&s);
289 }
290
291 /**************************************/
292
293 #define DIR_NONE -1
294
295 int
296 find_route(int fromRoom, int toRoom, int known)
297 {
298 // KNOWN INDICATES WHETHER THE LOCATION MUST HAVE BEEN
299 // VISITED PREVIOUSLY. THIS IS NOT REQUIRED FOR NPCS.
300
301 // CREATE AND INITIALISE THE QUEUE OF LOCATION TO PROCESS
302 Queue q;
303 qInit(&q);
304
305 // CREATE AND INITIALISE THE SET OF VISITED LOCATIONS
306 Set visited;
307 setInit(&visited);
308
309 int firstTime = TRUE;
310
311 int result = DIR_NONE;
312
313 // ADD THE STARTING LOCATION TO THE QUEUE FOR PROCESSING
314 qAppend(&q, fromRoom, DIR_NONE);
315
316 // ADD THE STARTING LOCATION TO THE SET OF VISITED LOCATIONS
317 setAdd(&visited, fromRoom);
318
319 // KEEP PROCESSING WHILE THERE ARE LOCATIONS LEFT IN THE QUEUE
320 while (!qIsEmpty(&q))
321 {
322 int n, dir, firstDir;
323
324 // POP THE LOCATION AT THE HEAD OF THE QUEUE (FIRST IN FIRST OUT)
325 // n IS THE LOCATION, firstDir IS THE EXIT THAT WAS PUSHED IN
326 // THE QUEUE.
327 qPop(&q, &n, &firstDir);
328
329 if (n == toRoom)
330 {
331 // HAVE ARRIVED AT THE DESTINATION, RETURN THE FIRST
332 // DIRECTION WALKED IN TO GET TO THE DESTINATION.
333 result = firstDir;
334 break;
335 }
336
337 // LOOP THROUGH ALL THE EXITS OF THE CURRENT LOCATION
338 for (dir = 0;dir < 12 ;dir++)
339 {
340 // SET THE DESTINATION TO THE LOCATION THIS DIRECTION
341 // LEADS TO.
342 int dest = object[n]->integer[dir];
343
344 if (dest < 1 || dest > objects) continue;
345
346 if (object[dest] == NULL) continue;
347
348 if (dest != NOWHERE && !setContains(&visited, dest))
349 {
350 if (!known || (object[dest]->attributes & KNOWN)) {
351 // PUT THE DESTINATION LOCATION INTO THE QUEUE
352 // (IF THIS IS THE STARTING LOCATION)
353 // AND STORE THE DIRECTION THAT LEAD TO IT
354 // firstTime IS CHECKED AS THE LOCATIONS REACHED
355 // FROM THE STARTING LOCATION ARE THE ONLY
356 // ONES THAT REALLY MATTER AS THIS FUNCTION
357 // ONLY RETURNS THE FIRST STEP, NOT THE FULL
358 // PATH. firstDir IS THE ORIGINAL DIRECTION
359 // SET OUT IN FROM THE STARTING LOCATION
360 qAppend(&q, dest, (firstTime ? dir : firstDir));
361
362 // ADD THE LOCATION TO THE SET OF LOCATIONS VISITED
363 setAdd(&visited, dest);
364 }
365 }
366 }
367
368 firstTime = FALSE;
369 }
370
371 setDelete(&visited);
372 qDelete(&q);
373
374 return result;
375 }