initial commit, lordsawar source, slightly modified
[lordsawar] / src / PathCalculator.h
1 // Copyright (C) 2009 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17 #ifndef PATH_CALCULATOR_H
18 #define PATH_CALCULATOR_H
19
20 #include <gtkmm.h>
21 #include "vector.h"
22
23 class Stack;
24 class Path;
25 class City;
26 class Player;
27 class ArmyProdBase;
28
29 //! An object that calculates shortest paths on a weighted grid.
30 /** 
31  */
32 class PathCalculator
33 {
34 public:
35
36     //! Default constructor.
37     PathCalculator(const Stack *s, bool zigzag = true, int enemy_city_avoidance = -1, int enemy_stack_avoidance = -1);
38
39     //! Alternate constructor.  calculate with a copy of the stack.
40     PathCalculator(const Stack &s, bool zigzag = true, int enemy_city_avoidance = -1, int enemy_stack_avoidance = -1);
41
42     //! Alternate constructor.  calculate with a new stack of one army.
43     PathCalculator(Player *p, Vector<int> src, const ArmyProdBase *prodbase = NULL, bool zigzag = true, int enemy_city_avoidance = -1, int enemy_stack_avoidance = -1);
44
45     //! Copy constructor.
46     PathCalculator(const PathCalculator&);
47
48     //! Destructor.
49     ~PathCalculator();
50
51     bool isReachable(Vector<int> pos);
52
53     Path* calculate(Vector<int> dest, guint32 &moves, guint32 &turns, bool zigzag = true);
54
55     Path* calculateToCity (City *c, guint32 &moves, guint32 &turns, bool zigzag = true);
56     int calculate(Vector<int> dest, bool zigzag = true);
57
58     static bool isBlocked(const Stack *s, Vector<int> pos, bool enemy_cities_block, bool enemy_stacks_block);
59
60     //! Return the positions on the map that are reachable in MP or less.
61     std::list<Vector<int> > getReachablePositions(int mp = 0);
62 private:
63     struct node
64       {
65         int moves;
66         int turns;
67         int moves_left;
68       };
69     struct node *nodes;
70     const Stack *stack;
71     bool flying;
72     guint32 d_bonus;
73     int land_reset_moves;
74     int boat_reset_moves;
75     bool zigzag;
76     bool on_ship;
77     int enemy_city_avoidance;
78     int enemy_stack_avoidance;
79
80     /** 
81      * Checks how many movement points are needed to cross a tile from
82      * an adjacent tile.
83      * 
84      * @param pos    This is the origin point that the Stack is moving from.
85      *
86      * @param dest   This is the destination point that we're checking if the 
87      *               Stack can move to.
88      *
89      * @return The number of movement points required to traverse the
90      *         destination tile, or -1 if movement not possible.
91      */
92     //! Calculates movement points to traverse an adjacent tile.
93     int pointsToMoveTo(Vector<int> pos, Vector<int> next) const;
94
95     bool load_or_unload(Vector<int> src, Vector<int> dest, bool &on_ship);
96
97     /**
98      * The purpose of this method is to verify if the Stack can move 
99      * onto the Tile of the given destination point.
100      *
101      * @note This method does not calculate a path and it does not 
102      * consider the amount of movement points that the Stack has. 
103      *
104      * This method uses two shortcuts to check if it is impossible for 
105      * the given stack to travel to the given destination on the GameMap.  
106      * Firstly it checks to see if the destination terrain Tile::Type is 
107      * of a kind that the Stack can't travel on at all (e.g. Mountains, 
108      * and the Stack can't fly).  Secondly it checks to see if the tile 
109      * is both adjacent and blocked from that direction.
110      *
111      * This method is primarily used to assist in mouse cursor display.
112      *
113      * @param dest    The position on the map to see if the Stack can 
114      *                move to.
115      *
116      * @return True if the Stack can move to the given position on the 
117      *         GameMap.  Otherwise the return value is false.
118      */
119     //! Calculates if a Stack move to a position on the GameMap.
120     bool canMoveThere(Vector<int> dest);
121
122     std::list<Vector<int> > calcMoves(Vector<int> pos);
123     bool calcMoves(Vector<int> pos, Vector<int> next);
124     bool calcFinalMoves(Vector<int> pos);
125     bool calcFinalMoves(Vector<int> pos, Vector<int> next);
126
127     void populateNodeMap();
128     /** 
129      * Checks if the way to a given tile is blocked
130      * 
131      * This function returns whether a unit can pass over a tile from
132      * another tile.  The idea here is that the "sides" of certain tiles
133      * are blocked from entry.  E.g. when trying to go from water to
134      * land, without going through a city.
135      *
136      * @param pos    This is the origin point that the Stack is moving from.
137      *
138      * @param dest   This is the destination point that we're checking if 
139      *           the Stack can move to.
140      *
141      * @note The movement capabilities of a Stack are not taken into 
142      *       account in this method.  This method should only be called 
143      *       for Stack objects that are not flying.
144      *
145      * @return False if a non-flying stack may pass, true otherwise.  
146      *         False if the two points are not adjacent.
147      */
148     //! Checks if the way between adjacent tiles is blocked.
149     bool isBlockedDir(Vector<int> pos, Vector<int> next);
150
151     //this method involves checking for enemy stacks, cities in the way.
152     bool isBlocked(Vector<int> pos);
153
154     void dumpNodeMap(Vector<int> dest);
155     bool compareNodeMaps(void *map);
156     bool delete_stack;
157
158 };
159
160 #endif