src/lib/as/as_extend.c

Go to the documentation of this file.
00001 /*
00002  *  A* Search - A search library used in Empire to determine paths between
00003  *              objects.
00004  *  Copyright (C) 1990-1998 Phil Lapsley
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 #include <config.h>
00022 
00023 #include <stdlib.h>
00024 #include "as.h"
00025 
00026 /*
00027  * Extend the queue by neighbors.  This entails getting the
00028  * coordinates of all the neighbors, figuring out their lower bound
00029  * costs, throwing away ones that are more expensive than ones we
00030  * already have, sorting, tand then merging into the queue.
00031  */
00032 struct as_queue *
00033 as_extend(struct as_data *adp)
00034 {
00035     struct as_queue *qp;
00036     int i;
00037     struct as_queue *head;
00038 
00039     head = adp->head;
00040 
00041     /* Find the neighboring coordinates. */
00042     i = adp->neighbor(head->np->c, adp->neighbor_coords, adp->userdata);
00043     if (i == 0)
00044         return NULL;
00045     /*
00046      * Get rid of neighbors that are more costly than ones we already have,
00047      * and sort the rest into an array of as_nodes.
00048      */
00049     i = as_winnow(adp, adp->neighbor_coords, i);
00050     if (i < 0)
00051         return NULL;
00052     if (i > 1)
00053         qsort(adp->neighbor_nodes, i,
00054               sizeof(*adp->neighbor_nodes), as_costcomp);
00055 
00056     /* remove old coord from head of queue and add to list of tried */
00057     qp = head;
00058     head = head->next;
00059     if (head)
00060         head->prev = NULL;
00061     if (adp->tried) {
00062         adp->tried->prev = qp;
00063         qp->next = adp->tried;
00064         adp->tried = qp;
00065     } else
00066         adp->tried = qp;
00067     adp->tried->np->flags |= AS_TRIED;
00068 
00069     head = as_merge(adp, head, adp->neighbor_nodes);
00070     return head;
00071 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2