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