2 * A* Search - A search library used in Empire to determine paths between
4 * Copyright (C) 1990-1998 Phil Lapsley
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.
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.
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
21 * 11/09/98 - Steve McClure
22 * Added path list caching structures
29 * Basic A* search function. "adp" should have been initialized by
30 * as_init (any previously allocated data will be freed by as_reset here),
31 * and adp->from and adp->to should be set accordingly. On success,
32 * returns 0, with adp->path set to a linked list of coordinates to target.
33 * If we can't find target, return -1; if malloc fails, return -2.
36 as_search(struct as_data *adp)
39 struct as_queue *head;
50 * Jump start the queue by making first element the zero-cost
51 * node where we start.
53 AS_NEW_MALLOC(head, struct as_queue, -2);
55 head->next = head->prev = NULL;
56 AS_NEW(np, struct as_node, -2);
59 as_setcinq(adp, head->np->c, adp->head);
64 fprintf(stderr, "Iteration %d, head at %d, %d\n", iter,
65 head->np->c.x, head->np->c.y);
68 /* see if we're done, one way or another */
72 /* Add it to the cache */
73 as_add_cachepath(adp);
75 if (head->np->c.x == adp->to.x && head->np->c.y == adp->to.y)
78 /* extend queue by neighbors */
80 fprintf(stderr, "\tExtending queue\n");
82 adp->head = head = as_extend(adp);
85 fprintf(stderr, "queue:\n");
87 for (qp = head; qp; qp = qp->next) {
88 fprintf(stderr, "\t%d, %d so far %f lb %f sec %f\n",
89 qp->np->c.x, qp->np->c.y,
90 qp->np->knowncost, qp->np->lbcost, qp->np->seccost);
93 fprintf(stderr, "\tqueue len %d\n", i);
100 fprintf(stderr, "Failed\n");
108 fprintf(stderr, "Succeeded, iter %d, cost %f!\n", iter,
109 head->np->knowncost);
110 fprintf(stderr, "Path:\n");
111 for (pp = adp->path; pp; pp = pp->next) {
112 fprintf(stderr, "\t%d, %d\n", pp->c.x, pp->c.y);
114 fprintf(stderr, "Tried queue:\n");
115 for (qp = adp->tried; qp; qp = qp->next) {
116 fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
118 fprintf(stderr, "Subsumed queue:\n");
119 for (qp = adp->subsumed; qp; qp = qp->next) {
120 fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
128 * Work backwards through the list of nodes (starting at head)
132 as_makepath(struct as_data *adp)
137 for (np = adp->head->np; np; np = np->back) {
138 pp = (struct as_path *)malloc(sizeof(struct as_path));
140 pp->next = adp->path;