Adonthell  0.4
path.h
Go to the documentation of this file.
1 /*
2  $Id: path.h,v 1.7 2003/02/23 23:14:34 ksterker Exp $
3 
4  Copyright (C) 2001 Alexandre Courbot
5  Part of the Adonthell Project http://adonthell.linuxgames.com
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 
16 /**
17  * @file path.h
18  * @author Alexandre Courbot <alexandrecourbot@linuxgames.com>
19  *
20  * @brief Declares the path class.
21  *
22  *
23  */
24 
25 #ifndef PATH_H__
26 #define PATH_H__
27 
28 #include "mapsquare.h"
29 
30 #ifndef SWIG
31 using namespace std;
32 #endif
33 
34 class landmap;
35 
36 /**
37  * A* pathfinding algorithm implementation class.
38  *
39  * This class calculates the shortest way from a begin point
40  * to a goal point on a landmap using the A* algorithm. It
41  * stores a list of directions that when followed lead from
42  * the start to the goal.
43  *
44  * This class is particularly well designed for mapcharacters,
45  * who will often need to walk from one point to another.
46  *
47  */
48 class path
49 {
50 private:
51  struct compare_squarecost
52  {
53  bool operator() (const mapsquare * s1, const mapsquare * s2)
54  {
55  return (s1->f > s2->f);
56  }
57  };
58 
59 public:
60 #ifndef SWIG
61  /**
62  * (x, y) coordinates of a point on a submap.
63  *
64  */
65  struct area_coord
66  {
67  /**
68  * X position.
69  *
70  */
72 
73  /**
74  * Y position.
75  *
76  */
78  };
79 
80  /**
81  * Landmap where the pathfinding will occur.
82  *
83  */
85 
86  /**
87  * Submap where the pathfinding will occur.
88  *
89  */
91 
92  /**
93  * Direction to face once the goal is reached.
94  *
95  */
97 
98  /**
99  * Start point.
100  *
101  */
103 
104  /**
105  * Goal point.
106  *
107  */
109 #endif // SWIG
110 
111  /**
112  * Totally clears the path.
113  *
114  */
115  void clear ()
116  {
117  moves_to_goal.clear ();
118  }
119 
120  /**
121  * Tries to find the shortest path possible between the
122  * \e start point and the \e goal point.
123  *
124  * @return \e true if a path was found, \e false otherwise.
125  */
126  bool calculate ();
127 
128  /**
129  * Returns the number of moves between \e start and \e goal.
130  *
131  *
132  * @return Number of moves between \e start and \e goal.
133  */
135  {
136  return moves_to_goal.size ();
137  }
138 
139  /**
140  * Returns the move to perform when at position \e nbr.
141  *
142  * @param nbr Index of the move to get.
143  *
144  * @return Direction (move) at index \e nbr.
145  */
147  {
148  return moves_to_goal[nbr_moves () - (nbr + 1)];
149  }
150 
151  /**
152  * Restore the path's state from an opened file.
153  *
154  * @param file the opened file from which to load the state.
155  *
156  * @return 0 in case of success, error code otherwise.
157  *
158  * @bug the landmap this path belongs to must be restored manually!
159  */
160  s_int8 get_state (igzstream & file);
161 
162  /**
163  * Saves the path's state into an opened file.
164  *
165  * @param file the opened file where to the state.
166  *
167  * @return 0 in case of success, error code otherwise.
168  *
169  * @bug the landmap this path belongs to can't be saved (as it's a pointer)!
170  */
171  s_int8 put_state (ogzstream & file) const;
172 
173 private:
174  vector <u_int16> moves_to_goal;
175  u_int16 goal_estimate (u_int16 x, u_int16 y);
176 };
177 
178 #endif