]> git.pond.sub.org Git - empserver/blob - src/lib/as/as_extend.c
4c9e7e3a6650e847863f8588704cec371acb0b32
[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
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include "as.h"
25
26 /*
27  * Extend the queue by neighbors.  This entails getting the
28  * coordinates of all the neighbors, figuring out their lower bound
29  * costs, throwing away ones that are more expensive than ones we
30  * already have, sorting, tand then merging into the queue.
31  */
32 struct as_queue *
33 as_extend(struct as_data *adp)
34 {
35     struct as_queue *qp;
36     int i;
37     struct as_queue *head;
38
39     head = adp->head;
40
41     /* Find the neighboring coordinates. */
42     i = adp->neighbor(head->np->c, adp->neighbor_coords, adp->userdata);
43     if (i == 0)
44         return NULL;
45     /*
46      * Get rid of neighbors that are more costly than ones we already have,
47      * and sort the rest into an array of as_nodes.
48      */
49     i = as_winnow(adp, adp->neighbor_coords, i);
50     if (i < 0)
51         return NULL;
52     if (i > 1)
53         qsort(adp->neighbor_nodes, i,
54               sizeof(*adp->neighbor_nodes), as_costcomp);
55
56     /* remove old coord from head of queue and add to list of tried */
57     qp = head;
58     head = head->next;
59     if (head)
60         head->prev = NULL;
61     if (adp->tried) {
62         adp->tried->prev = qp;
63         qp->next = adp->tried;
64         adp->tried = qp;
65     } else
66         adp->tried = qp;
67     adp->tried->np->flags |= AS_TRIED;
68
69     head = as_merge(adp, head, adp->neighbor_nodes);
70     return head;
71 }