The Battle for Wesnoth  1.15.12+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 loc the map location for which we want to know the adjacent hexes
119  * @todo what does this function actually have to do with adjacent hexes?
120  */
121  std::set<map_location> get_adjacents(map_location loc) const;
122 
123  /** Returns the locations that are an entrance of the tunnel. */
124  std::set<map_location> get_sources() const;
125 
126  /** Returns the locations that are an exit of the tunnel. */
127  std::set<map_location> get_targets() const;
128 
129  /*
130  * @returns whether the teleport_map does contain any defined tunnel
131  */
132  bool empty() const {
133  return sources_.empty();
134  }
135 
136 private:
137  std::map<map_location, std::set<std::string>> teleport_map_;
138  std::map<std::string, std::set<map_location>> sources_;
139  std::map<std::string, std::set<map_location>> targets_;
140 };
141 
142 /*
143  * @param u The unit that is processed by pathfinding
144  * @param viewing_team The team the player belongs to
145  * @param see_all Whether the teleport can be seen below shroud
146  * @param ignore_units Whether to ignore zoc and blocking by units
147  * @param check_vision Whether to check vision as opposed to movement range
148  * @returns a teleport_map
149  */
150 const teleport_map get_teleport_locations(const unit &u, const team &viewing_team,
151  bool see_all = false, bool ignore_units = false, bool check_vision = false);
152 
153 class manager
154 {
155 public:
156  manager(const config &cfg);
157 
158  /*
159  * @param group teleport_group to be added
160  */
161  void add(const teleport_group &group);
162 
163  /*
164  * @param id id of the teleport_group that is to be removed by the method
165  */
166  void remove(const std::string &id);
167 
168  /*
169  * @return all registered teleport groups on the game field
170  */
171  const std::vector<teleport_group>& get() const;
172 
173  /** Inherited from savegame_config. */
174  config to_config() const;
175 
176  /*
177  * @returns the next free unique id for a teleport group
178  */
179  std::string next_unique_id();
180 private:
181  std::vector<teleport_group> tunnels_;
182  int id_;
183 };
184 
185 }
std::map< std::string, std::set< map_location > > sources_
Definition: teleport.hpp:138
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:120
bool pass_allied_units() const
Definition: teleport.cpp:145
bool allow_vision() const
Definition: teleport.cpp:149
bool empty() const
Definition: teleport.hpp:132
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:139
Encapsulates the map of the game.
Definition: location.hpp:37
std::map< map_location, std::set< std::string > > teleport_map_
Definition: teleport.hpp:137
std::vector< teleport_group > tunnels_
Definition: teleport.hpp:181
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
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:265
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:59