The Battle for Wesnoth  1.15.1+dev
teleport.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License version 2
7  or at your option any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "config.hpp"
17 #include "map/location.hpp"
18 
19 class team;
20 class unit;
21 class vconfig;
22 
23 
24 namespace pathfind {
25 
26 typedef std::pair<std::set<map_location>, std::set<map_location>>
28 
29 /*
30  * Represents the tunnel wml tag.
31  */
33 {
34 public:
35  /*
36  * Constructs the object from a saved file.
37  * @param cfg the contents of a [tunnel] tag
38  */
39  teleport_group(const config& cfg);
40 
41  /*
42  * Constructs the object from a config file.
43  * @param cfg the contents of a [tunnel] tag
44  * @param way_back inverts the direction of the teleport
45  */
46  teleport_group(const vconfig& cfg, bool way_back = false);
47 
48  /*
49  * Fills the argument loc_pair if the unit u matches the groups filter.
50  * @param loc_pair returned teleport_pair if the unit matches
51  * @param u this unit must match the group's filter
52  * @param ignore_units don't consider zoc and blocking when calculating the shorted path between
53  */
54  void get_teleport_pair(
55  teleport_pair& loc_pair
56  , const unit& u
57  , const bool ignore_units) const;
58 
59  /*
60  * Can be set by the id attribute or is randomly chosen.
61  * @return unique id of the teleport group
62  */
63  const std::string& get_teleport_id() const;
64 
65  /*
66  * Returns whether the group should always be visible,
67  * even for enemy movement under shroud.
68  * @return visibility of the teleport group
69  */
70  bool always_visible() const;
71 
72  /*
73  * Returns whether allied units on the exit hex can be passed.
74  */
75  bool pass_allied_units() const;
76 
77  /*
78  * Returns whether vision through tunnels is possible.
79  */
80  bool allow_vision() const;
81 
82  /** Inherited from savegame_config. */
83  config to_config() const;
84 
85 private:
86 
87  config cfg_; // unexpanded contents of a [tunnel] tag
88  bool reversed_; // Whether the tunnel's direction is reversed
89  std::string id_; // unique id of the group
90 };
91 
92 
93 class teleport_map {
94 public:
95  /*
96  * @param teleport_groups
97  * @param u
98  * @param viewing_team
99  * @param see_all
100  * @param ignore_units
101  * @param check_vision
102  */
103  teleport_map(
104  const std::vector<teleport_group>& teleport_groups
105  , const unit& u
106  , const team &viewing_team
107  , const bool see_all
108  , const bool ignore_units
109  , const bool check_vision);
110 
111  /*
112  * Constructs an empty teleport map.
113  */
115  teleport_map_(), sources_(), targets_() {}
116 
117  /*
118  * @param adjacents used to return the adjacent hexes
119  * @param loc the map location for which we want to know the adjacent hexes
120  */
121  void get_adjacents(std::set<map_location>& adjacents, map_location loc) const;
122  /*
123  * @param sources used to return the locations that are an entrance of the tunnel
124  */
125  void get_sources(std::set<map_location>& sources) const;
126  /*
127  * @param targets used to return the locations that are an exit of the tunnel
128  */
129  void get_targets(std::set<map_location>& targets) const;
130 
131  /*
132  * @returns whether the teleport_map does contain any defined tunnel
133  */
134  bool empty() const {
135  return sources_.empty();
136  }
137 
138 private:
139  std::map<map_location, std::set<std::string>> teleport_map_;
140  std::map<std::string, std::set<map_location>> sources_;
141  std::map<std::string, std::set<map_location>> targets_;
142 };
143 
144 /*
145  * @param u The unit that is processed by pathfinding
146  * @param viewing_team The team the player belongs to
147  * @param see_all Whether the teleport can be seen below shroud
148  * @param ignore_units Whether to ignore zoc and blocking by units
149  * @param check_vision Whether to check vision as opposed to movement range
150  * @returns a teleport_map
151  */
152 const teleport_map get_teleport_locations(const unit &u, const team &viewing_team,
153  bool see_all = false, bool ignore_units = false, bool check_vision = false);
154 
155 class manager
156 {
157 public:
158  manager(const config &cfg);
159 
160  /*
161  * @param group teleport_group to be added
162  */
163  void add(const teleport_group &group);
164 
165  /*
166  * @param id id of the teleport_group that is to be removed by the method
167  */
168  void remove(const std::string &id);
169 
170  /*
171  * @return all registered teleport groups on the game field
172  */
173  const std::vector<teleport_group>& get() const;
174 
175  /** Inherited from savegame_config. */
176  config to_config() const;
177 
178  /*
179  * @returns the next free unique id for a teleport group
180  */
181  std::string next_unique_id();
182 private:
183  std::vector<teleport_group> tunnels_;
184  int id_;
185 };
186 
187 }
std::map< std::string, std::set< map_location > > sources_
Definition: teleport.hpp:140
std::pair< std::set< map_location >, std::set< map_location > > teleport_pair
Definition: teleport.hpp:27
This class represents a single unit of a specific type.
Definition: unit.hpp:99
bool pass_allied_units() const
Definition: teleport.cpp:145
bool allow_vision() const
Definition: teleport.cpp:149
bool empty() const
Definition: teleport.hpp:134
config to_config() const
Inherited from savegame_config.
Definition: teleport.cpp:153
Definitions for the interface to Wesnoth Markup Language (WML).
teleport_group(const config &cfg)
Definition: teleport.cpp:39
const std::string & get_teleport_id() const
Definition: teleport.cpp:137
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
void get_teleport_pair(teleport_pair &loc_pair, const unit &u, const bool ignore_units) const
Definition: teleport.cpp:108
std::map< std::string, std::set< map_location > > targets_
Definition: teleport.hpp:141
Encapsulates the map of the game.
Definition: location.hpp:42
std::map< map_location, std::set< std::string > > teleport_map_
Definition: teleport.hpp:139
std::vector< teleport_group > tunnels_
Definition: teleport.hpp:183
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
const teleport_map get_teleport_locations(const unit &u, const team &viewing_team, bool see_all, bool ignore_units, bool check_vision)
Definition: teleport.cpp:260
bool always_visible() const
Definition: teleport.cpp:141
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92