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: 12 25 48.0 %
Date: 2023-01-19 00:18:29 Functions: 3 5 60.0 %

          Line data    Source code
       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             :  */
      46             : class CLosQuerier
      47             : {
      48             : private:
      49             :     friend class CCmpRangeManager;
      50             :     friend class TestLOSTexture;
      51             : 
      52           1 :     CLosQuerier(u32 playerMask, const Grid<u32>& data, ssize_t verticesPerSide) :
      53           1 :     m_Data(data), m_PlayerMask(playerMask), m_VerticesPerSide(verticesPerSide)
      54             :     {
      55           1 :     }
      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           0 :     inline bool IsVisible(ssize_t i, ssize_t j) const
      64             :     {
      65           0 :         if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
      66           0 :             return false;
      67             : 
      68             :         // Check high bit of each bit-pair
      69           0 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
      70           0 :             return true;
      71             :         else
      72           0 :             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           0 :     inline bool IsExplored(ssize_t i, ssize_t j) const
      79             :     {
      80           0 :         if (!(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide))
      81           0 :             return false;
      82             : 
      83             :         // Check low bit of each bit-pair
      84           0 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
      85           0 :             return true;
      86             :         else
      87           0 :             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          64 :     inline bool IsVisible_UncheckedRange(ssize_t i, ssize_t j) const
      95             :     {
      96             : #ifndef NDEBUG
      97          64 :         ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
      98             : #endif
      99             :         // Check high bit of each bit-pair
     100          64 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0xAAAAAAAAu)
     101          10 :             return true;
     102             :         else
     103          54 :             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          54 :     inline bool IsExplored_UncheckedRange(ssize_t i, ssize_t j) const
     111             :     {
     112             : #ifndef NDEBUG
     113          54 :         ENSURE(i >= 0 && j >= 0 && i < m_VerticesPerSide && j < m_VerticesPerSide);
     114             : #endif
     115             :         // Check low bit of each bit-pair
     116          54 :         if ((m_Data.get(i, j) & m_PlayerMask) & 0x55555555u)
     117           0 :             return true;
     118             :         else
     119          54 :             return false;
     120             :     }
     121             : 
     122             : private:
     123             :     u32 m_PlayerMask;
     124             :     const Grid<u32>& m_Data;
     125             :     ssize_t m_VerticesPerSide;
     126             : };
     127             : 
     128             : #endif // INCLUDED_LOS

Generated by: LCOV version 1.13