The Battle for Wesnoth  1.15.12+dev
vision.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
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 as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Various functions implementing vision (through fog of war and shroud).
18  */
19 
20 #pragma once
21 
22 #include "movetype.hpp"
23 #include "game_events/fwd.hpp"
24 
25 struct map_location;
26 class team;
27 class unit;
28 
29 #include <cstring>
30 #include <map>
31 #include <set>
32 #include <vector>
33 
34 
35 namespace actions {
36  class move_unit_spectator;
37 
38 /**
39  * Helper function that creates the map of enemy anti-vision that's needed when
40  * creating a pathfinding::vision_path.
41  *
42  * Sets @a jamming to the (newly calculated) "jamming" map that reduces @a view_team's vision.
43  */
44 void create_jamming_map(std::map<map_location, int> & jamming,
45  const team & view_team);
46 
47 /**
48  * Class that stores the part of a unit's data that is needed for fog clearing.
49  * (Used by the undo stack as that cannot rely on a unit sticking around, and
50  * we do not really need to copy the entire unit.)
51  */
52 struct clearer_info {
53  std::size_t underlying_id;
55  bool slowed;
56  /** costs is always non-null, all of the constructors initialize it */
57  std::unique_ptr<movetype::terrain_costs> costs;
58 
59  clearer_info(const unit & viewer);
60  clearer_info(const config & cfg);
61 
62  void write(config & cfg) const;
63 };
64 
65 /**
66  * Class to encapsulate fog/shroud clearing and the resultant sighted events.
67  * Note: This class uses teams as parameters (instead of sides) since a
68  * function using this should first check to see if fog/shroud is in use (to
69  * save processing when it is not), which implies the team is readily available.
70  */
72 public:
73  shroud_clearer(const shroud_clearer&) = delete;
74  shroud_clearer& operator=(const shroud_clearer&) = delete;
75 
77  ~shroud_clearer();
78 
79  /**
80  * Function to be called if units have moved or otherwise changed.
81  * It can also be called if it is desirable to calculate the cache
82  * in advance of fog clearing.
83  * @param[in] new_team The team whose vision will be used. If left as
84  * nullptr, the cache will be just be cleared (to be
85  * recalculated later as needed).
86  */
87  void cache_units(const team * new_team=nullptr) { calculate_jamming(new_team); }
88  // cache_units() is currently a near-synonym for calculate_jamming(). The
89  // reason for the two names is so the private function says what it does,
90  // while the public one says why it might be invoked.
91 
92  /**
93  * Clears shroud (and fog) around the provided location for @a view_team
94  * based on @a sight_range, @a costs, and @a slowed.
95  */
96  bool clear_unit(const map_location &view_loc, team &view_team,
97  std::size_t viewer_id, int sight_range, bool slowed,
99  const map_location & real_loc,
100  const std::set<map_location>* known_units = nullptr,
101  std::size_t * enemy_count = nullptr, std::size_t * friend_count = nullptr,
102  move_unit_spectator * spectator = nullptr, bool instant = true);
103  /**
104  * Clears shroud (and fog) around the provided location for @a view_team
105  * as if @a viewer was standing there.
106  */
107  bool clear_unit(const map_location &view_loc,
108  const unit &viewer, team &view_team,
109  const std::set<map_location>* known_units = nullptr,
110  std::size_t * enemy_count = nullptr, std::size_t * friend_count = nullptr,
111  move_unit_spectator * spectator = nullptr, bool instant = true);
112  /**
113  * Clears shroud (and fog) around the provided location for @a view_team
114  * as if @a viewer was standing there. Setting @a instant to false
115  * allows some drawing delays that are used to make movement look better.
116  */
117  bool clear_unit(const map_location &view_loc, const unit &viewer,
118  team &view_team, bool instant)
119  { return clear_unit(view_loc, viewer, view_team, nullptr, nullptr, nullptr, nullptr, instant); }
120  /**
121  * Clears shroud (and fog) around the provided location for @a view_team
122  * as if @a viewer was standing there.
123  */
124  bool clear_unit(const map_location &view_loc, team &view_team,
125  const clearer_info &viewer, bool instant);
126  /**
127  * Clears shroud (and fog) around the provided location as if @a viewer
128  * was standing there.
129  */
130  bool clear_unit(const map_location &view_loc, const unit &viewer,
131  bool can_delay = false, bool invalidate = true,
132  bool instant = true);
133 
134  /** Clears shroud (and fog) at the provided location and its immediate neighbors. */
135  bool clear_dest(const map_location &dest, const unit &viewer);
136 
137  /** Erases the record of sighted events from earlier fog/shroud clearing. */
138  void drop_events();
139 
140  /** Fires the sighted events that were earlier recorded by fog/shroud clearing. */
141  game_events::pump_result_t fire_events();
142 
143  /** The invalidations that should occur after invoking clear_unit(). */
144  void invalidate_after_clear();
145 
146 private:
147  /** A record of a sighting event. */
148  struct sight_data;
149 
150  /** Causes this object's "jamming" map to be recalculated. */
151  void calculate_jamming(const team * new_team);
152 
153  /** Clears shroud from a single location. */
154  bool clear_loc(team &tm, const map_location &loc, const map_location &view_loc,
155  const map_location &event_non_loc, std::size_t viewer_id,
156  bool check_units, std::size_t &enemy_count, std::size_t &friend_count,
157  move_unit_spectator * spectator = nullptr);
158 
159  /** Convenience wrapper for adding sighting data to the sightings_ vector. */
160  inline void record_sighting(const unit & seen, const map_location & seen_loc,
161  std::size_t sighter_id, const map_location & sighter_loc);
162 
163 private: // data
164  std::map<map_location, int> jamming_;
165  std::vector<sight_data> sightings_;
166  /** Keeps track of the team associated with jamming_. */
167  const team * view_team_;
168 };
169 
170 
171 /** Returns the sides that cannot currently see @a target. */
172 std::vector<int> get_sides_not_seeing(const unit & target);
173 /** Fires sighted events for the sides that can see @a target. */
174 game_events::pump_result_t actor_sighted(const unit & target, const std::vector<int> * cache = nullptr);
175 
176 
177 /** Function that recalculates the fog of war. */
178 void recalculate_fog(int side);
179 
180 /** Function that will clear shroud (and fog) based on current unit positions. */
181 bool clear_shroud(int side, bool reset_fog = false, bool fire_events = true);
182 
183 
184 }//namespace actions
Class that stores the part of a unit&#39;s data that is needed for fog clearing.
Definition: vision.hpp:52
A const-only interface for how many (movement, vision, or "jamming") points a unit needs for each hex...
Definition: movetype.hpp:52
std::vector< sight_data > sightings_
Definition: vision.hpp:165
This class represents a single unit of a specific type.
Definition: unit.hpp:120
A record of a sighting event.
Definition: vision.cpp:138
bool clear_unit(const map_location &view_loc, const unit &viewer, team &view_team, bool instant)
Clears shroud (and fog) around the provided location for view_team as if viewer was standing there...
Definition: vision.hpp:117
std::size_t underlying_id
Definition: vision.hpp:53
void create_jamming_map(std::map< map_location, int > &jamming, const team &view_team)
Helper function that creates the map of enemy anti-vision that&#39;s needed when creating a pathfinding::...
Definition: vision.cpp:48
clearer_info(const unit &viewer)
Constructor from a unit.
Definition: vision.cpp:99
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
void recalculate_fog(int side)
Function that recalculates the fog of war.
Definition: vision.cpp:711
Encapsulates the map of the game.
Definition: location.hpp:37
static tcache cache
Definition: minimap.cpp:123
void write(config &cfg) const
Writes to a config.
Definition: vision.cpp:121
bool clear_shroud(int side, bool reset_fog, bool fire_events)
Function that will clear shroud (and fog) based on current unit positions.
Definition: vision.cpp:760
std::unique_ptr< movetype::terrain_costs > costs
costs is always non-null, all of the constructors initialize it
Definition: vision.hpp:57
const team * view_team_
Keeps track of the team associated with jamming_.
Definition: vision.hpp:167
std::map< map_location, int > jamming_
Definition: vision.hpp:164
void cache_units(const team *new_team=nullptr)
Function to be called if units have moved or otherwise changed.
Definition: vision.hpp:87
Class to encapsulate fog/shroud clearing and the resultant sighted events.
Definition: vision.hpp:71
std::tuple< bool, bool > pump_result_t
Definition: fwd.hpp:28
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
std::vector< int > get_sides_not_seeing(const unit &target)
Returns the sides that cannot currently see target.
Definition: vision.cpp:601
game_events::pump_result_t actor_sighted(const unit &target, const std::vector< int > *cache)
Fires sighted events for the sides that can see target.
Definition: vision.cpp:628