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