]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_extend.c
Indented with src/scripts/indent-emp.
[empserver] / src / lib / as / as_extend.c
1 /*
2  *  A* Search - A search library used in Empire to determine paths between
3  *              objects.
4  *  Copyright (C) 1990-1998 Phil Lapsley
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "as.h"
23
24 #if !defined(lint) && !defined(SABER)
25 static char sccsid[] = "@(#)as_extend.c 1.2     11/13/90";
26 #endif /* not lint */
27
28 /*
29  * Extend the queue by neighbors.  This entails getting the
30  * coordinates of all the neighbors, figuring out their lower bound
31  * costs, throwing away ones that are more expensive than ones we
32  * already have, sorting, tand then merging into the queue.
33  */
34 struct as_queue *
35 as_extend(struct as_data *adp)
36 {
37     struct as_queue *qp;
38     int i;
39     struct as_queue *head;
40
41     head = adp->head;
42
43     /* Find the neighboring coordinates. */
44     i = (*adp->neighbor) (head->np->c, adp->neighbor_coords,
45                           adp->userdata);
46     if (i == 0)
47         return (NULL);
48     /*
49      * Get rid of neighbors that are more costly than ones we already have,
50      * and sort the rest into an array of as_nodes.
51      */
52     i = as_winnow(adp, adp->neighbor_coords, i);
53     if (i < 0)
54         return (NULL);
55     if (i > 1)
56         qsort(adp->neighbor_nodes, i,
57               sizeof(*adp->neighbor_nodes), (qsort_func_t)as_costcomp);
58
59     /* remove old coord from head of queue and add to list of tried */
60     qp = head;
61     head = head->next;
62     if (head)
63         head->prev = NULL;
64     if (adp->tried) {
65         adp->tried->prev = qp;
66         qp->next = adp->tried;
67         adp->tried = qp;
68     } else
69         adp->tried = qp;
70     adp->tried->np->flags |= AS_TRIED;
71
72     head = as_merge(adp, head, adp->neighbor_nodes);
73     return (head);
74 }