The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vision.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef ACTIONS_VISION_H_INCLUDED
21 #define ACTIONS_VISION_H_INCLUDED
22 
23 #include "movetype.hpp"
24 
25 struct map_location;
26 class team;
27 class unit;
28 
29 #include <boost/noncopyable.hpp>
30 #include <cstring>
31 #include <map>
32 #include <set>
33 #include <vector>
34 
35 
36 namespace actions {
37  class move_unit_spectator;
38 
39 /// Class that stores the part of a unit's data that is needed for fog clearing.
40 /// (Used by the undo stack as that cannot rely on a unit sticking around, and
41 /// we do not really need to copy the entire unit.)
42 struct clearer_info {
43  size_t underlying_id;
45  bool slowed;
47 
48  clearer_info(const unit & viewer);
49  clearer_info(const config & cfg);
50 
51  void write(config & cfg) const;
52 };
53 
54 /// Class to encapsulate fog/shroud clearing and the resultant sighted events.
55 /// Note: This class uses teams as parameters (instead of sides) since a
56 /// function using this should first check to see if fog/shroud is in use (to
57 /// save processing when it is not), which implies the team is readily available.
58 class shroud_clearer : public boost::noncopyable {
59 public:
62 
63  /// Function to be called if units have moved or otherwise changed.
64  /// It can also be called if it is desirable to calculate the cache
65  /// in advance of fog clearing.
66  /// @param[in] new_team The team whose vision will be used. If left as
67  /// nullptr, the cache will be just be cleared (to be
68  /// recalculated later as needed).
69  void cache_units(const team * new_team=nullptr) { calculate_jamming(new_team); }
70  // cache_units() is currently a near-synonym for calculate_jamming(). The
71  // reason for the two names is so the private function says what it does,
72  // while the public one says why it might be invoked.
73 
74  /// Clears shroud (and fog) around the provided location for @a view_team
75  /// based on @a sight_range, @a costs, and @a slowed.
76  bool clear_unit(const map_location &view_loc, team &view_team,
77  size_t viewer_id, int sight_range, bool slowed,
78  const movetype::terrain_costs & costs,
79  const map_location & real_loc,
80  const std::set<map_location>* known_units = nullptr,
81  size_t * enemy_count = nullptr, size_t * friend_count = nullptr,
82  move_unit_spectator * spectator = nullptr, bool instant = true);
83  /// Clears shroud (and fog) around the provided location for @a view_team
84  /// as if @a viewer was standing there.
85  bool clear_unit(const map_location &view_loc,
86  const unit &viewer, team &view_team,
87  const std::set<map_location>* known_units = nullptr,
88  size_t * enemy_count = nullptr, size_t * friend_count = nullptr,
89  move_unit_spectator * spectator = nullptr, bool instant = true);
90  /// Clears shroud (and fog) around the provided location for @a view_team
91  /// as if @a viewer was standing there. Setting @a instant to false
92  /// allows some drawing delays that are used to make movement look better.
93  bool clear_unit(const map_location &view_loc, const unit &viewer,
94  team &view_team, bool instant)
95  { return clear_unit(view_loc, viewer, view_team, nullptr, nullptr, nullptr, nullptr, instant); }
96  /// Clears shroud (and fog) around the provided location for @a view_team
97  /// as if @a viewer was standing there.
98  bool clear_unit(const map_location &view_loc, team &view_team,
99  const clearer_info &viewer, bool instant);
100  /// Clears shroud (and fog) around the provided location as if @a viewer
101  /// was standing there.
102  bool clear_unit(const map_location &view_loc, const unit &viewer,
103  bool can_delay = false, bool invalidate = true,
104  bool instant = true);
105 
106  /// Clears shroud (and fog) at the provided location and its immediate neighbors.
107  bool clear_dest(const map_location &dest, const unit &viewer);
108 
109  /// Erases the record of sighted events from earlier fog/shroud clearing.
110  void drop_events();
111 
112  /// Fires the sighted events that were earlier recorded by fog/shroud clearing.
113  bool fire_events();
114 
115  /// The invalidations that should occur after invoking clear_unit().
116  void invalidate_after_clear();
117 
118 private:
119  /// A record of a sighting event.
120  struct sight_data;
121 
122  /// Causes this object's "jamming" map to be recalculated.
123  void calculate_jamming(const team * new_team);
124 
125  /// Clears shroud from a single location.
126  bool clear_loc(team &tm, const map_location &loc, const map_location &view_loc,
127  const map_location &event_non_loc, size_t viewer_id,
128  bool check_units, size_t &enemy_count, size_t &friend_count,
129  move_unit_spectator * spectator = nullptr);
130 
131  /// Convenience wrapper for adding sighting data to the sightings_ vector.
132  inline void record_sighting(const unit & seen, const map_location & seen_loc,
133  size_t sighter_id, const map_location & sighter_loc);
134 
135 private: // data
136  std::map<map_location, int> jamming_;
137  std::vector<sight_data> sightings_;
138  /// Keeps track of the team associated with jamming_.
139  const team * view_team_;
140 };
141 
142 
143 /// Returns the sides that cannot currently see @a target.
144 std::vector<int> get_sides_not_seeing(const unit & target);
145 /// Fires sighted events for the sides that can see @a target.
146 bool actor_sighted(const unit & target, const std::vector<int> * cache = nullptr);
147 
148 
149 /// Function that recalculates the fog of war.
150 void recalculate_fog(int side);
151 
152 /// Function that will clear shroud (and fog) based on current unit positions.
153 bool clear_shroud(int side, bool reset_fog = false, bool fire_events = true);
154 
155 
156 }//namespace actions
157 
158 #endif
Class that stores the part of a unit's data that is needed for fog clearing.
Definition: vision.hpp:42
Stores a set of terrain costs (for movement, vision, or "jamming").
Definition: movetype.hpp:88
std::vector< sight_data > sightings_
Definition: vision.hpp:137
Definition: unit.hpp:95
bool clear_unit(const map_location &view_loc, team &view_team, 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, size_t *enemy_count=nullptr, 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, costs, and slowed.
Definition: vision.cpp:331
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:93
bool actor_sighted(const unit &target, const std::vector< int > *cache)
Fires sighted events for the sides that can see target.
Definition: vision.cpp:623
~shroud_clearer()
Destructor.
Definition: vision.cpp:179
void record_sighting(const unit &seen, const map_location &seen_loc, size_t sighter_id, const map_location &sighter_loc)
Convenience wrapper for adding sighting data to the sightings_ vector.
Definition: vision.cpp:158
clearer_info(const unit &viewer)
Constructor from a unit.
Definition: vision.cpp:102
void calculate_jamming(const team *new_team)
Causes this object's "jamming" map to be recalculated.
Definition: vision.cpp:193
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:495
bool clear_loc(team &tm, const map_location &loc, const map_location &view_loc, const map_location &event_non_loc, size_t viewer_id, bool check_units, size_t &enemy_count, size_t &friend_count, move_unit_spectator *spectator=nullptr)
Clears shroud from a single location.
Definition: vision.cpp:227
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
void recalculate_fog(int side)
Function that recalculates the fog of war.
Definition: vision.cpp:705
Encapsulates the map of the game.
Definition: location.hpp:38
void drop_events()
Erases the record of sighted events from earlier fog/shroud clearing.
Definition: vision.cpp:534
static tcache cache
Definition: minimap.cpp:139
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:754
bool fire_events()
Fires the sighted events that were earlier recorded by fog/shroud clearing.
Definition: vision.cpp:547
const team * view_team_
Keeps track of the team associated with jamming_.
Definition: vision.hpp:139
void write(config &cfg) const
Writes to a config.
Definition: vision.cpp:124
std::map< map_location, int > jamming_
Definition: vision.hpp:136
void cache_units(const team *new_team=nullptr)
Function to be called if units have moved or otherwise changed.
Definition: vision.hpp:69
Class to encapsulate fog/shroud clearing and the resultant sighted events.
Definition: vision.hpp:58
movetype::terrain_costs costs
Definition: vision.hpp:46
void invalidate_after_clear()
The invalidations that should occur after invoking clear_unit().
Definition: vision.cpp:582
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
shroud_clearer()
Default constructor.
Definition: vision.cpp:170
std::vector< int > get_sides_not_seeing(const unit &target)
Returns the sides that cannot currently see target.
Definition: vision.cpp:596
GLenum target
Definition: glew.h:5190