pathfind.hpp

Go to the documentation of this file.
00001 /* $Id: pathfind.hpp 26029 2008-04-23 18:17:29Z alink $ */
00002 /*
00003    Copyright (C) 2003 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 
00015 //! @file pathfind.hpp
00016 //!
00017 
00018 #ifndef PATHFIND_H_INCLUDED
00019 #define PATHFIND_H_INCLUDED
00020 
00021 class gamestatus;
00022 class unit;
00023 class unit_map;
00024 
00025 #include "array.hpp"
00026 #include "map.hpp"
00027 #include "pathutils.hpp"
00028 #include "team.hpp"
00029 
00030 #include <map>
00031 #include <list>
00032 #include <set>
00033 #include <vector>
00034 #include <functional>
00035 
00036 // This module contains various pathfinding functions and utilities.
00037 
00038 //! A convenient type for storing a list of tiles adjacent to a certain tile.
00039 typedef util::array<gamemap::location,6> adjacent_tiles_array;
00040 
00041 class xy_pred : public std::unary_function<gamemap::location const&, bool>
00042 {
00043 public:
00044     virtual bool operator()(gamemap::location const&) = 0;
00045 protected:
00046     virtual ~xy_pred() {}
00047 };
00048 
00049 //! Function which, given a location, will find all tiles within 'radius' of that tile
00050 void get_tiles_radius(const gamemap::location& a, size_t radius,
00051                       std::set<gamemap::location>& res);
00052 
00053 //! Function which, given a set of locations, will find all tiles within 'radius' of those tiles
00054 void get_tiles_radius(const gamemap& map, const std::vector<gamemap::location>& locs, size_t radius,
00055                       std::set<gamemap::location>& res, xy_pred *pred=NULL);
00056 
00057 enum VACANT_TILE_TYPE { VACANT_CASTLE, VACANT_ANY };
00058 
00059 //! Function which will find a location on the board that is
00060 //! as near to loc as possible, but which is unoccupied by any units.
00061 //! If terrain is not 0, then the location found must be of the given terrain type,
00062 //! and must have a path of that terrain type to loc.
00063 //! If no valid location can be found, it will return a null location.
00064 gamemap::location find_vacant_tile(const gamemap& map,
00065                                    const unit_map& un,
00066                                    const gamemap::location& loc,
00067                                    VACANT_TILE_TYPE vacancy=VACANT_ANY);
00068 
00069 //! Function which determines if a given location is in an enemy zone of control.
00070 bool enemy_zoc(gamemap const &map,
00071                unit_map const &units,
00072                std::vector<team> const &teams, gamemap::location const &loc,
00073                team const &viewing_team, unsigned int side, bool see_all=false);
00074 
00075 struct cost_calculator
00076 {
00077     virtual double cost(const gamemap::location& src, const gamemap::location& loc, const double so_far) const = 0;
00078     virtual ~cost_calculator() {}
00079     inline double getNoPathValue(void) const { return (42424242.0); }
00080 };
00081 
00082 //! Object which contains all the possible locations a unit can move to,
00083 //! with associated best routes to those locations.
00084 struct paths
00085 {
00086     paths() : routes() {}
00087 
00088     // Construct a list of paths for the unit at loc.
00089     // - force_ignore_zocs: find the path ignoring ZOC entirely,
00090     //                     if false, will use the unit on the loc's ability
00091     // - allow_teleport: indicates whether unit teleports between villages
00092     // - additional_turns: if 0, paths for how far the unit can move this turn will be calculated.
00093     //                     If 1, paths for how far the unit can move by the end of next turn
00094     //                     will be calculated, and so forth.
00095     // viewing_team is usually current team, except for Show Enemy Moves etc.
00096     paths(gamemap const &map,
00097           unit_map const &units,
00098           gamemap::location const &loc, std::vector<team> const &teams,
00099           bool force_ignore_zocs,bool allow_teleport,
00100          const team &viewing_team,int additional_turns = 0,
00101          bool see_all = false, bool ignore_units = false);
00102 
00103     //! Structure which holds a single route between one location and another.
00104     struct route
00105     {
00106         route() : steps(), move_left(0), waypoints() {}
00107         std::vector<gamemap::location> steps;
00108         int move_left; // movement unit will have left at end of the route.
00109         struct waypoint
00110         {
00111             waypoint(int turns_number = 0, bool in_zoc = false,
00112                     bool do_capture = false, bool is_invisible = false)
00113                 : turns(turns_number), zoc(in_zoc),
00114                     capture(do_capture), invisible(is_invisible) {}
00115             int turns;
00116             bool zoc;
00117             bool capture;
00118             bool invisible;
00119         };
00120         std::map<gamemap::location, waypoint> waypoints;
00121     };
00122 
00123     typedef std::map<gamemap::location,route> routes_map;
00124     routes_map routes;
00125 };
00126 
00127 paths::route a_star_search(gamemap::location const &src, gamemap::location const &dst,
00128                            double stop_at, cost_calculator const *costCalculator,
00129                            const size_t parWidth, const size_t parHeight,
00130                            std::set<gamemap::location> const *teleports = NULL);
00131 
00132 //! Function which, given a unit and a route the unit can move on,
00133 //! will return the number of turns it will take the unit to traverse the route.
00134 //! adds "turn waypoints" to rt.turn_waypoints.
00135 //! Note that "end of path" is also added.
00136 int route_turns_to_complete(const unit& u, paths::route& rt, const team &viewing_team,
00137                             const unit_map& units, const std::vector<team>& teams, const gamemap& map);
00138 
00139 struct shortest_path_calculator : cost_calculator
00140 {
00141     shortest_path_calculator(const unit& u, const team& t, const unit_map& units,
00142                              const std::vector<team>& teams, const gamemap& map,
00143                              bool ignore_unit = false, bool ignore_defense_ = false);
00144     virtual double cost(const gamemap::location& src, const gamemap::location& loc, const double so_far) const;
00145 
00146 private:
00147     unit const &unit_;
00148     team const &viewing_team_;
00149     unit_map const &units_;
00150     std::vector<team> const &teams_;
00151     gamemap const &map_;
00152     int const movement_left_;
00153     int const total_movement_;
00154     bool const ignore_unit_;
00155     bool const ignore_defense_;
00156 };
00157 
00158 //! Function which only uses terrain, ignoring shroud, enemies, etc.
00159 //! Required by move_unit_fake if the normal path fails.
00160 struct emergency_path_calculator : cost_calculator
00161 {
00162     emergency_path_calculator(const unit& u, const gamemap& map);
00163     virtual double cost(const gamemap::location& src, const gamemap::location& loc, const double so_far) const;
00164 
00165 private:
00166     unit const &unit_;
00167     gamemap const &map_;
00168 };
00169 
00170 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs