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
31 * Basic A* search function. "adp" should have been initialized by
32 * as_init (any previously allocated data will be freed by as_reset here),
33 * and adp->from and adp->to should be set accordingly. On success,
34 * returns 0, with adp->path set to a linked list of coordinates to target.
35 * If we can't find target, return -1; if malloc fails, return -2.
38 as_search(struct as_data *adp)
41 struct as_queue *head;
52 * Jump start the queue by making first element the zero-cost
53 * node where we start.
55 AS_NEW_MALLOC(head, struct as_queue, -2);
57 head->next = head->prev = NULL;
58 AS_NEW(np, struct as_node, -2);
61 as_setcinq(adp, head->np->c, adp->head);
65 /* see if we're done, one way or another */
70 fprintf(stderr, "Iteration %d, head at %d, %d\n", iter,
71 head->np->c.x, head->np->c.y);
74 /* Add it to the cache */
75 as_add_cachepath(adp);
77 if (head->np->c.x == adp->to.x && head->np->c.y == adp->to.y)
80 /* extend queue by neighbors */
82 fprintf(stderr, "\tExtending queue\n");
84 adp->head = head = as_extend(adp);
85 /* FIXME leaks when as_extend() returns NULL without adding to tried */
88 fprintf(stderr, "queue:\n");
90 for (qp = head; qp; qp = qp->next) {
91 fprintf(stderr, "\t%d, %d so far %f lb %f sec %f\n",
92 qp->np->c.x, qp->np->c.y,
93 qp->np->knowncost, qp->np->lbcost, qp->np->seccost);
96 fprintf(stderr, "\tqueue len %d\n", i);
103 fprintf(stderr, "Failed\n");
111 fprintf(stderr, "Succeeded, iter %d, cost %f!\n", iter,
112 head->np->knowncost);
113 fprintf(stderr, "Path:\n");
114 for (pp = adp->path; pp; pp = pp->next) {
115 fprintf(stderr, "\t%d, %d\n", pp->c.x, pp->c.y);
117 fprintf(stderr, "Tried queue:\n");
118 for (qp = adp->tried; qp; qp = qp->next) {
119 fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
121 fprintf(stderr, "Subsumed queue:\n");
122 for (qp = adp->subsumed; qp; qp = qp->next) {
123 fprintf(stderr, "\t%d, %d\n", qp->np->c.x, qp->np->c.y);
131 * Work backwards through the list of nodes (starting at head)
135 as_makepath(struct as_data *adp)
140 for (np = adp->head->np; np; np = np->back) {
141 pp = malloc(sizeof(struct as_path));
143 pp->next = adp->path;