pathfind/pathfind.hpp

Go to the documentation of this file.
00001 /* $Id: pathfind.hpp 54122 2012-05-08 00:05:33Z fendrin $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * This module contains various pathfinding functions and utilities.
00019  */
00020 
00021 #ifndef PATHFIND_H_INCLUDED
00022 #define PATHFIND_H_INCLUDED
00023 
00024 class gamemap;
00025 class team;
00026 class unit;
00027 class unit_map;
00028 class unit_movement_type;
00029 
00030 #include "map_location.hpp"
00031 
00032 #include <map>
00033 #include <list>
00034 #include <functional>
00035 
00036 namespace pathfind {
00037 
00038 class teleport_map;
00039 
00040 enum VACANT_TILE_TYPE { VACANT_CASTLE, VACANT_ANY };
00041 enum PATH_TYPE { MOVE, VISION, JAMMING };
00042 
00043 /**
00044  * Function which will find a location on the board that is
00045  * as near to loc as possible, but which is unoccupied by any units.
00046  * If no valid location can be found, it will return a null location.
00047  */
00048 map_location find_vacant_tile(const gamemap& map,
00049                                    const unit_map& un,
00050                                    const map_location& loc,
00051                        VACANT_TILE_TYPE vacancy=VACANT_ANY,
00052                                    const unit* pass_check=NULL);
00053 
00054 /** Determines if a given location is in an enemy zone of control. */
00055 bool enemy_zoc(team const &current_team, map_location const &loc,
00056                team const &viewing_team, bool see_all=false);
00057 
00058 
00059 struct cost_calculator
00060 {
00061     cost_calculator() {}
00062 
00063     virtual double cost(const map_location& loc, const double so_far) const = 0;
00064     virtual ~cost_calculator() {}
00065 
00066     static double getNoPathValue() { return (42424242.0); }
00067 };
00068 
00069 /**
00070  * Object which contains all the possible locations a unit can move to,
00071  * with associated best routes to those locations.
00072  */
00073 struct paths
00074 {
00075     paths()
00076         : destinations()
00077     {
00078     }
00079 
00080     /// Construct a list of paths for the specified unit.
00081     paths(gamemap const &map,
00082           unit_map const &/*units*/, // Not used
00083           const unit& u, std::vector<team> const &teams,
00084           bool force_ignore_zocs, bool allow_teleport,
00085           const team &viewing_team, int additional_turns = 0,
00086           bool see_all = false, bool ignore_units = false);
00087     /// Virtual destructor (default processing).
00088     virtual ~paths();
00089 
00090     struct step
00091     {
00092         map_location curr, prev;
00093         int move_left;
00094     };
00095 
00096     /** Ordered vector of possible destinations. */
00097     struct dest_vect : std::vector<step>
00098     {
00099         const_iterator find(const map_location &) const;
00100         bool contains(const map_location &) const;
00101         void insert(const map_location &);
00102         std::vector<map_location> get_path(const const_iterator &) const;
00103     };
00104     dest_vect destinations;
00105 };
00106 
00107 /**
00108  * A refinement of paths for use when calculating vision.
00109  */
00110 struct vision_path : public paths
00111 {
00112     /// Construct a list of seen hexes for a unit.
00113     vision_path(gamemap const &map, const unit& viewer,
00114                 map_location const &loc, const std::map<map_location, int>& jamming_map);
00115     virtual ~vision_path();
00116 
00117     /// The edges are the non-destination hexes bordering the destinations.
00118     std::set<map_location> edges;
00119 };
00120 
00121 /**
00122  * A refinement of paths for use when calculating jamming.
00123  */
00124 struct jamming_path : public paths
00125 {
00126     /// Construct a list of jammed hexes for a unit.
00127     jamming_path(gamemap const &map, const unit& jammer,
00128                 map_location const &loc);
00129     virtual ~jamming_path();
00130 
00131     /// The edges are the non-destination hexes bordering the destinations.
00132     //std::set<map_location> edges;
00133 };
00134 
00135 
00136 /** Structure which holds a single route between one location and another. */
00137 struct plain_route
00138 {
00139     plain_route() : steps(), move_cost(0) {}
00140     std::vector<map_location> steps;
00141     /** Movement cost for reaching the end of the route. */
00142     int move_cost;
00143 };
00144 
00145 /** Structure which holds a single route and marks for special events. */
00146 struct marked_route
00147 {
00148     marked_route()
00149         : route()
00150         , steps(route.steps)
00151         , move_cost(route.move_cost)
00152         , marks()
00153     {
00154     }
00155 
00156     marked_route(const marked_route& rhs)
00157         : route(rhs.route)
00158         , steps(route.steps)
00159         , move_cost(route.move_cost)
00160         , marks(rhs.marks)
00161     {
00162     }
00163 
00164     marked_route& operator=(const marked_route& rhs)
00165     {
00166         this->route = rhs.route;
00167         this->steps = this->route.steps;
00168         this->move_cost = this->route.move_cost;
00169         this->marks = rhs.marks;
00170         return *this;
00171     }
00172 
00173     struct mark
00174     {
00175         mark(int turns_number = 0, bool in_zoc = false,
00176                 bool do_capture = false, bool is_invisible = false)
00177             : turns(turns_number), zoc(in_zoc),
00178               capture(do_capture), invisible(is_invisible) {}
00179         int turns;
00180         bool zoc;
00181         bool capture;
00182         bool invisible;
00183 
00184         bool operator==(const mark& m) const {
00185             return turns == m.turns && zoc == m.zoc && capture == m.capture && invisible == m.invisible;
00186         }
00187     };
00188     typedef std::map<map_location, mark> mark_map;
00189     plain_route route;
00190 
00191     //make steps and move_cost of the underlying plain_route directly accessible
00192     std::vector<map_location>& steps;
00193     int& move_cost;
00194 
00195     mark_map marks;
00196 };
00197 
00198 plain_route a_star_search(map_location const &src, map_location const &dst,
00199         double stop_at, const cost_calculator* costCalculator,
00200         const size_t parWidth, const size_t parHeight,
00201         const teleport_map* teleports = NULL);
00202 
00203 /**
00204  * Add marks on a route @a rt assuming that the unit located at the first hex of
00205  * rt travels along it.
00206  */
00207 marked_route mark_route(const plain_route &rt);
00208 
00209 struct shortest_path_calculator : cost_calculator
00210 {
00211     shortest_path_calculator(const unit& u, const team& t, const unit_map& units,
00212         const std::vector<team> &teams, const gamemap &map,
00213         bool ignore_unit = false, bool ignore_defense_ = false,
00214         bool see_all = false);
00215     virtual double cost(const map_location& loc, const double so_far) const;
00216 
00217 private:
00218     unit const &unit_;
00219     team const &viewing_team_;
00220     unit_map const &units_;
00221     std::vector<team> const &teams_;
00222     gamemap const &map_;
00223     int const movement_left_;
00224     int const total_movement_;
00225     bool const ignore_unit_;
00226     bool const ignore_defense_;
00227     bool see_all_;
00228 };
00229 
00230 struct move_type_path_calculator : cost_calculator
00231 {
00232     move_type_path_calculator(const unit_movement_type& mt, int movement_left, int total_movement, const team& t, const gamemap& map);
00233     virtual double cost(const map_location& loc, const double so_far) const;
00234 
00235 private:
00236     const unit_movement_type &movement_type_;
00237     const int movement_left_;
00238     const int total_movement_;
00239     team const &viewing_team_;
00240     gamemap const &map_;
00241 };
00242 
00243 /**
00244  * Function which only uses terrain, ignoring shroud, enemies, etc.
00245  * Required by move_unit_fake if the normal path fails.
00246  */
00247 struct emergency_path_calculator : cost_calculator
00248 {
00249     emergency_path_calculator(const unit& u, const gamemap& map);
00250     virtual double cost(const map_location& loc, const double so_far) const;
00251 
00252 private:
00253     unit const &unit_;
00254     gamemap const &map_;
00255 };
00256 
00257 /**
00258  * Function which doesn't take anything into account. Used by
00259  * move_unit_fake for the last-chance case.
00260  */
00261 struct dummy_path_calculator : cost_calculator
00262 {
00263     dummy_path_calculator(const unit& u, const gamemap& map);
00264     virtual double cost(const map_location& loc, const double so_far) const;
00265 };
00266 
00267 }
00268 
00269 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:07 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs