The Battle for Wesnoth  1.17.0-dev
teleport.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 class team;
22 class unit;
23 class vconfig;
24 
25 
26 namespace pathfind {
27 
28 typedef std::pair<std::set<map_location>, std::set<map_location>>
30 
31 /*
32  * Represents the tunnel wml tag.
33  */
35 {
36 public:
37  /*
38  * Constructs the object from a saved file.
39  * @param cfg the contents of a [tunnel] tag
40  */
41  teleport_group(const config& cfg);
42 
43  /*
44  * Constructs the object from a config file.
45  * @param cfg the contents of a [tunnel] tag
46  * @param way_back inverts the direction of the teleport
47  */
48  teleport_group(const vconfig& cfg, bool way_back = false);
49 
50  /*
51  * Fills the argument loc_pair if the unit u matches the groups filter.
52  * @param loc_pair returned teleport_pair if the unit matches
53  * @param u this unit must match the group's filter
54  * @param ignore_units don't consider zoc and blocking when calculating the shorted path between
55  */
56  void get_teleport_pair(
57  teleport_pair& loc_pair
58  , const unit& u
59  , const bool ignore_units) const;
60 
61  /*
62  * Can be set by the id attribute or is randomly chosen.
63  * @return unique id of the teleport group
64  */
65  const std::string& get_teleport_id() const;
66 
67  /*
68  * Returns whether the group should always be visible,
69  * even for enemy movement under shroud.
70  * @return visibility of the teleport group
71  */
72  bool always_visible() const;
73 
74  /*
75  * Returns whether allied units on the exit hex can be passed.
76  */
77  bool pass_allied_units() const;
78 
79  /*
80  * Returns whether vision through tunnels is possible.
81  */
82  bool allow_vision() const;
83 
84  /** Inherited from savegame_config. */
85  config to_config() const;
86 
87 private:
88 
89  config cfg_; // unexpanded contents of a [tunnel] tag
90  bool reversed_; // Whether the tunnel's direction is reversed
91  std::string id_; // unique id of the group
92 };
93 
94 
95 class teleport_map {
96 public:
97  /*
98  * @param teleport_groups
99  * @param u
100  * @param viewing_team
101  * @param see_all
102  * @param ignore_units
103  * @param check_vision
104  */
105  teleport_map(
106  const std::vector<teleport_group>& teleport_groups
107  , const unit& u
108  , const team &viewing_team
109  , const bool see_all
110  , const bool ignore_units
111  , const bool check_vision);
112 
113  /*
114  * Constructs an empty teleport map.
115  */
117  teleport_map_(), sources_(), targets_() {}
118 
119  /**
120  * @param loc the map location for which we want to know the adjacent hexes
121  * @todo what does this function actually have to do with adjacent hexes?
122  */
123  std::set<map_location> get_adjacents(map_location loc) const;
124 
125  /** Returns the locations that are an entrance of the tunnel. */
126  std::set<map_location> get_sources() const;
127 
128  /** Returns the locations that are an exit of the tunnel. */
129  std::set<map_location> get_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:29
This class represents a single unit of a specific type.
Definition: unit.hpp:121
bool pass_allied_units() const
Definition: teleport.cpp:150
bool allow_vision() const
Definition: teleport.cpp:154
bool empty() const
Definition: teleport.hpp:134
config to_config() const
Inherited from savegame_config.
Definition: teleport.cpp:158
Definitions for the interface to Wesnoth Markup Language (WML).
teleport_group(const config &cfg)
Definition: teleport.cpp:44
const std::string & get_teleport_id() const
Definition: teleport.cpp:142
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
void get_teleport_pair(teleport_pair &loc_pair, const unit &u, const bool ignore_units) const
Definition: teleport.cpp:113
std::map< std::string, std::set< map_location > > targets_
Definition: teleport.hpp:141
Encapsulates the map of the game.
Definition: location.hpp:38
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
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: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:270
bool always_visible() const
Definition: teleport.cpp:146
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61