LCOV - code coverage report
Current view: top level - source/simulation2/helpers - Los.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 16 17 94.1 %
Date: 2026-12-03 01:19:39 Functions: 2 2 100.0 %

          Line data    Source code
       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             : 
      21             : #include "lib/posix/posix_types.h"
      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.
      24             : #include "simulation2/helpers/Grid.h"
      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             :  */
      31             : static constexpr std::int32_t LOS_TILE_SIZE = 4;
      32             : 
      33             : enum 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             :  */
      47             : class CLosQuerier
      48             : {
      49             : private:
      50             :     friend class CCmpRangeManager;
      51             :     friend class TestLOSTexture;
      52             : 
      53       28263 :     CLosQuerier(std::uint32_t playerMask, const Grid<std::uint32_t>& data, ssize_t verticesPerSide) :
      54       28263 :     m_Data(data), m_PlayerMask(playerMask), m_VerticesPerSide(verticesPerSide)
      55             :     {
      56           0 :     }
      57             : 
      58             :     const CLosQuerier& operator=(const CLosQuerier&); // not implemented
      59             : 
      60             : public:
      61             :     /**
      62             :      * Returns whether the given vertex is visible (i.e. is within a unit's LOS).
      63             :      */
      64       28262 :     inline bool IsVisible(ssize_t i, ssize_t j) const
      65             :     {
      66       28262 :         if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
      67             :             return false;
      68             : 
      69             :         // Check high bit of each bit-pair
      70       28262 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
      71             :             return true;
      72             :         else
      73       24692 :             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       24692 :     inline bool IsExplored(ssize_t i, ssize_t j) const
      80             :     {
      81       24692 :         if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
      82             :             return false;
      83             : 
      84             :         // Check low bit of each bit-pair
      85       24692 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
      86             :             return true;
      87             :         else
      88       24692 :             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             :      */
      95          64 :     inline bool IsVisible_UncheckedRange(ssize_t i, ssize_t j) const
      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          64 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
     102             :             return true;
     103             :         else
     104          54 :             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             :      */
     111          54 :     inline bool IsExplored_UncheckedRange(ssize_t i, ssize_t j) const
     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          54 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
     118             :             return true;
     119             :         else
     120          54 :             return false;
     121             :     }
     122             : 
     123             : private:
     124             :     std::uint32_t m_PlayerMask;
     125             :     const Grid<std::uint32_t>& m_Data;
     126             :     ssize_t m_VerticesPerSide;
     127             : };
     128             : 
     129             : #endif // INCLUDED_LOS

Generated by: LCOV version 1.16