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