Pyrogenesis  trunk
Los.h
Go to the documentation of this file.
1 /* Copyright (C) 2020 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_LOS
19 #define INCLUDED_LOS
20 
21 // It doesn't seem worth moving the implementation to c++ and early-declaring Grid
22 // since files must include "Los.h" explicitly, and that's only done in .cpp files.
23 #include "Grid.h"
24 
25 /**
26  * Computing LOS data at a very high resolution is not necessary and quite slow.
27  * This is the size, in meters, separating each LOS vertex.
28  * (Note that this also means it is the minimal meaningful resolution of any vision range change).
29  */
30 static constexpr i32 LOS_TILE_SIZE = 4;
31 
32 enum class LosState : u8
33 {
34  UNEXPLORED = 0,
35  EXPLORED = 1,
36  VISIBLE = 2,
37  MASK = 3
38 };
39 
40 /**
41  * Object providing efficient abstracted access to the LOS state.
42  * This depends on some implementation details of CCmpRangeManager.
43  *
44  * This *ignores* the GetLosRevealAll flag - callers should check that explicitly.
45  */
47 {
48 private:
49  friend class CCmpRangeManager;
50  friend class TestLOSTexture;
51 
52  CLosQuerier(u32 playerMask, const Grid<u32>& data, ssize_t verticesPerSide) :
53  m_Data(data), m_PlayerMask(playerMask), m_VerticesPerSide(verticesPerSide)
54  {
55  }
56 
57  const CLosQuerier& operator=(const CLosQuerier&); // not implemented
58 
59 public:
60  /**
61  * Returns whether the given vertex is visible (i.e. is within a unit's LOS).
62  */
63  inline bool IsVisible(ssize_t i, ssize_t j) const
64  {
65  if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
66  return false;
67 
68  // Check high bit of each bit-pair
69  if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
70  return true;
71  else
72  return false;
73  }
74 
75  /**
76  * Returns whether the given vertex is explored (i.e. was (or still is) within a unit's LOS).
77  */
78  inline bool IsExplored(ssize_t i, ssize_t j) const
79  {
80  if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
81  return false;
82 
83  // Check low bit of each bit-pair
84  if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
85  return true;
86  else
87  return false;
88  }
89 
90  /**
91  * Returns whether the given vertex is visible (i.e. is within a unit's LOS).
92  * i and j must be in the range [0, verticesPerSide), else behaviour is undefined.
93  */
94  inline bool IsVisible_UncheckedRange(ssize_t i, ssize_t j) const
95  {
96 #ifndef NDEBUG
97  ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
98 #endif
99  // Check high bit of each bit-pair
100  if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
101  return true;
102  else
103  return false;
104  }
105 
106  /**
107  * Returns whether the given vertex is explored (i.e. was (or still is) within a unit's LOS).
108  * i and j must be in the range [0, verticesPerSide), else behaviour is undefined.
109  */
110  inline bool IsExplored_UncheckedRange(ssize_t i, ssize_t j) const
111  {
112 #ifndef NDEBUG
113  ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
114 #endif
115  // Check low bit of each bit-pair
116  if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
117  return true;
118  else
119  return false;
120  }
121 
122 private:
126 };
127 
128 #endif // INCLUDED_LOS
static constexpr i32 LOS_TILE_SIZE
Computing LOS data at a very high resolution is not necessary and quite slow.
Definition: Los.h:30
u32 m_PlayerMask
Definition: Los.h:123
uint8_t u8
Definition: types.h:37
Range manager implementation.
Definition: CCmpRangeManager.cpp:352
const Grid< u32 > & m_Data
Definition: Los.h:124
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:290
uint32_t u32
Definition: types.h:39
ssize_t m_VerticesPerSide
Definition: Los.h:125
bool IsExplored(ssize_t i, ssize_t j) const
Returns whether the given vertex is explored (i.e.
Definition: Los.h:78
CLosQuerier(u32 playerMask, const Grid< u32 > &data, ssize_t verticesPerSide)
Definition: Los.h:52
bool IsVisible_UncheckedRange(ssize_t i, ssize_t j) const
Returns whether the given vertex is visible (i.e.
Definition: Los.h:94
int32_t i32
Definition: types.h:34
intptr_t ssize_t
Definition: wposix_types.h:82
bool IsVisible(ssize_t i, ssize_t j) const
Returns whether the given vertex is visible (i.e.
Definition: Los.h:63
LosState
Definition: Los.h:32
Object providing efficient abstracted access to the LOS state.
Definition: Los.h:46
bool IsExplored_UncheckedRange(ssize_t i, ssize_t j) const
Returns whether the given vertex is explored (i.e.
Definition: Los.h:110