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