Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Los.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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
22// It doesn't seem worth moving the implementation to c++ and early-declaring Grid
23// since files must include "Los.h" explicitly, and that's only done in .cpp files.
25
26/**
27 * Computing LOS data at a very high resolution is not necessary and quite slow.
28 * This is the size, in meters, separating each LOS vertex.
29 * (Note that this also means it is the minimal meaningful resolution of any vision range change).
30 */
31static constexpr std::int32_t LOS_TILE_SIZE = 4;
32
33enum class LosState : std::uint8_t
34{
35 UNEXPLORED = 0,
36 EXPLORED = 1,
37 VISIBLE = 2,
38 MASK = 3
39};
40
41/**
42 * Object providing efficient abstracted access to the LOS state.
43 * This depends on some implementation details of CCmpRangeManager.
44 *
45 * This *ignores* the GetLosRevealWholeMap flag - callers should check that explicitly.
46 */
48{
49private:
50 friend class CCmpRangeManager;
51 friend class TestLOSTexture;
52
53 CLosQuerier(std::uint32_t playerMask, const Grid<std::uint32_t>& data, ssize_t verticesPerSide) :
54 m_Data(data), m_PlayerMask(playerMask), m_VerticesPerSide(verticesPerSide)
55 {
56 }
57
58 const CLosQuerier& operator=(const CLosQuerier&); // not implemented
59
60public:
61 /**
62 * Returns whether the given vertex is visible (i.e. is within a unit's LOS).
63 */
64 inline bool IsVisible(ssize_t i, ssize_t j) const
65 {
66 if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
67 return false;
68
69 // Check high bit of each bit-pair
70 if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
71 return true;
72 else
73 return false;
74 }
75
76 /**
77 * Returns whether the given vertex is explored (i.e. was (or still is) within a unit's LOS).
78 */
79 inline bool IsExplored(ssize_t i, ssize_t j) const
80 {
81 if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
82 return false;
83
84 // Check low bit of each bit-pair
85 if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
86 return true;
87 else
88 return false;
89 }
90
91 /**
92 * Returns whether the given vertex is visible (i.e. is within a unit's LOS).
93 * i and j must be in the range [0, verticesPerSide), else behaviour is undefined.
94 */
96 {
97#ifndef NDEBUG
98 ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
99#endif
100 // Check high bit of each bit-pair
101 if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
102 return true;
103 else
104 return false;
105 }
106
107 /**
108 * Returns whether the given vertex is explored (i.e. was (or still is) within a unit's LOS).
109 * i and j must be in the range [0, verticesPerSide), else behaviour is undefined.
110 */
112 {
113#ifndef NDEBUG
114 ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
115#endif
116 // Check low bit of each bit-pair
117 if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
118 return true;
119 else
120 return false;
121 }
122
123private:
124 std::uint32_t m_PlayerMask;
127};
128
129#endif // INCLUDED_LOS
LosState
Definition: Los.h:34
static constexpr std::int32_t LOS_TILE_SIZE
Computing LOS data at a very high resolution is not necessary and quite slow.
Definition: Los.h:31
Range manager implementation.
Definition: CCmpRangeManager.cpp:415
Object providing efficient abstracted access to the LOS state.
Definition: Los.h:48
bool IsExplored(ssize_t i, ssize_t j) const
Returns whether the given vertex is explored (i.e.
Definition: Los.h:79
const Grid< std::uint32_t > & m_Data
Definition: Los.h:125
bool IsVisible_UncheckedRange(ssize_t i, ssize_t j) const
Returns whether the given vertex is visible (i.e.
Definition: Los.h:95
const CLosQuerier & operator=(const CLosQuerier &)
ssize_t m_VerticesPerSide
Definition: Los.h:126
bool IsVisible(ssize_t i, ssize_t j) const
Returns whether the given vertex is visible (i.e.
Definition: Los.h:64
CLosQuerier(std::uint32_t playerMask, const Grid< std::uint32_t > &data, ssize_t verticesPerSide)
Definition: Los.h:53
std::uint32_t m_PlayerMask
Definition: Los.h:124
friend class TestLOSTexture
Definition: Los.h:51
bool IsExplored_UncheckedRange(ssize_t i, ssize_t j) const
Returns whether the given vertex is explored (i.e.
Definition: Los.h:111
T & get(std::pair< std::uint16_t, std::uint16_t > coords)
Definition: Grid.h:215
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
intptr_t ssize_t
Definition: wposix_types.h:36