00001 /* 00002 * Copyright (C) 2000-2001 The Exult Team 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #ifdef HAVE_CONFIG_H 00020 # include <config.h> 00021 #endif 00022 00023 00024 #include "Astar.h" 00025 00026 /* 00027 * Find path from source to destination. 00028 * 00029 * Output: 1 if successful, else 0. 00030 */ 00031 int Astar::NewPath(Tile_coord s, Tile_coord d, Pathfinder_client *client) 00032 { 00033 extern Tile_coord *Find_path(Tile_coord, Tile_coord, 00034 Pathfinder_client *client, int& plen); 00035 src = s; // Store start, destination. 00036 dest = d; 00037 path.clear(); // Clear out old path, if there. 00038 Tile_coord *t = Find_path(s, d, client, pathlen); 00039 bool success = (t != 0); 00040 for(int i=0;i<pathlen;i++) 00041 path.push_back(t[i]); 00042 delete [] t; // Discard temporary storage 00043 next_index = 0; 00044 dir = 1; 00045 stop = pathlen; 00046 return (success); 00047 } 00048 00049 /* 00050 * Get next point on path to go to (in tile coords). 00051 * 00052 * Output: 0 if all done. 00053 */ 00054 int Astar::GetNextStep(Tile_coord& n, bool& done) 00055 { 00056 if (next_index == stop) 00057 { 00058 done = true; 00059 return (0); 00060 } 00061 n = path[next_index]; 00062 next_index += dir; 00063 done = (next_index == stop); 00064 return 1; 00065 } 00066 00067 /* 00068 * Set to traverse backwards. 00069 * 00070 * Output: 1 always (we succeeded). 00071 */ 00072 int Astar::set_backwards 00073 ( 00074 ) 00075 { 00076 dir = -1; 00077 stop = -1; 00078 next_index = pathlen - 1; 00079 return 1; 00080 } 00081 00082 /* 00083 * Get # steps left. 00084 */ 00085 int Astar::get_num_steps 00086 ( 00087 ) 00088 { 00089 return (stop - next_index)*dir; 00090 } 00091 00092 /* 00093 * Delete. 00094 */ 00095 Astar::~Astar 00096 ( 00097 ) 00098 { 00099 } 00100