initial commit, lordsawar source, slightly modified
[lordsawar] / src / path.h
1 // Copyright (C) 2000, 2001, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2004, 2005 Andrea Paternesi
4 // Copyright (C) 2004 John Farrell
5 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
6 // Copyright (C) 2008 Ole Laursen
7 //
8 //  This program is free software; you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation; either version 3 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU Library General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
21 //  02110-1301, USA.
22
23 #ifndef PATH_H
24 #define PATH_H
25
26 #include <gtkmm.h>
27 #include <list>
28 #include "vector.h"
29
30 class Stack;
31 class XML_Helper;
32 class City;
33
34 //! A list of waypoint coordinates (Vector<int>) on the GameMap.
35 /** 
36  * The path class is used to store movement paths, determine new movement 
37  * paths, and to query existing movement paths.
38  *
39  */
40 class Path : public std::list<Vector<int> >
41 {
42     public:
43         //! The xml tag of this object in a saved-game file.
44         static std::string d_tag; 
45
46         //! Default constructor.
47         Path();
48
49         //! Copy constructor.
50         Path(const Path& p);
51
52         //! Make a new path by loading it from an opened saved-game file.
53         Path(XML_Helper* helper);
54         //! Destructor.
55         ~Path();
56
57         //! Save the path to an opened saved-game file.
58         bool save(XML_Helper* helper) const;
59
60         /** 
61          * Check if the path is blocked for some reason, and recalculate it
62          * if necessary.
63          *
64          * @param stack        The Stack whose path we validate.
65          * @param enemy_city_avoidance Return false if a path's point is on an
66          * enemy city, or not if this value is non-negative.
67          * @param enemy_stack_avoidance Return false if a path's point is on an
68          * enemy stack, or not if this value is non-negative.
69          *
70          * @return True if path is valid, False if path is blocked and could
71          *         not be recalculated, True if the path was invalid but was
72          *         recalculated succesfully.
73          */
74         //! Validate an existing path.
75         bool checkPath(Stack* stack, int enemy_city_avoidance = -1, int enemy_stack_avoidance = -1);
76
77         /** 
78          * Calculates the path from the stack's position to a destination.
79          *
80          * The calculated path is stored within the instance (remember: each
81          * stack has a path instance). During calculation, bonuses and other
82          * specialities are taken into account.
83          *
84          * @param stack        The stack whose path we calculate.
85          * @param dest         The destination point to calculate for.
86          * @param zigzag       Whether we're using the normal way to
87          *                     calculate paths or not.  False means we never 
88          *                     go diagonally.  True means we do.
89          * @param turns        This variable gets filled up with the number of
90          *                     turns it takes to get to the destination.  If
91          *                     the destination can be reached in this turn,
92          *                     the value returned is 0.
93          *
94          * @return The number of movement points needed to destination or 0
95          *         if no path is possible.
96          */
97         //! Calculate a Stack object's Path to a destination on the GameMap.
98         guint32 calculate(Stack* stack, Vector<int> dest, guint32 &turns, bool zigzag = true);
99         guint32 calculate(Stack* stack, Vector<int> dest, bool zigzag = true);
100
101         //! Recalculate a Stack object's Path.
102         void recalculate (Stack* s);
103
104         //! Return the number of points the stack can move along it's path.
105         guint32 getMovesExhaustedAtPoint() {return d_moves_exhausted_at_point;}
106
107         /**
108          * Set the point at which the stack can't move along it's path.
109          * If the first point in the stack's path cannot be moved to,
110          * this method should return 0.  If the second point can't be moved 
111          * to, then this method should return 1, etc.
112          * 
113          * The purpose of this method is to assist in drawing the waypoint
114          * graphics.
115          *
116          * @param index   The index of the point in the stack's path that
117          *                cannot be moved to.
118          */
119         //! Set the number of points the stack can move along it's path.
120         void setMovesExhaustedAtPoint(guint32 index) 
121           {d_moves_exhausted_at_point = index;}
122
123         void eraseFirstPoint();
124         
125         //! find which tile in the city is quickest to move to.
126         guint32 calculateToCity (Stack *s, City *c, bool zigzag = true);
127         void dump();
128     private:
129
130         int pointsToMoveTo(const Stack *s, int x, int y, int destx, int desty) const;
131
132         void calculate (Stack* s, Vector<int> dest, guint32 &mp, guint32 &turns, bool zigzag);
133         bool load_or_unload(Stack *s, Vector<int> src, Vector<int> dest, bool &on_ship);
134
135         // Data
136
137         //! A cached copy of a Stack object's movement bonus.
138         guint32 d_bonus;
139
140         //! The point in the path that can't be reached.
141         guint32 d_moves_exhausted_at_point;
142
143 };
144
145 #endif // PATH_H