BVB Source Codes

CRYENGINE Show CoverUser.cpp Source code

Return Download CRYENGINE: download CoverUser.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "CoverUser.h"
  5. #include "CoverSurface.h"
  6. #include "CoverSystem.h"
  7.  
  8. #include "DebugDrawContext.h"
  9.  
  10. CoverUser::CoverUser()
  11.         : m_coverID(0)
  12.         , m_nextCoverID(0)
  13.         , m_locationEffectiveHeight(FLT_MAX)
  14.         , m_location(ZERO)
  15.         , m_normal(ZERO)
  16.         , m_compromised(false)
  17.         , m_farFromCoverLocation(false)
  18. {
  19. }
  20.  
  21. void CoverUser::Reset()
  22. {
  23.         if (m_coverID)
  24.                 gAIEnv.pCoverSystem->SetCoverOccupied(m_coverID, false, m_params.userID);
  25.  
  26.         m_coverID = CoverID();
  27.  
  28.         ResetState();
  29. }
  30.  
  31. void CoverUser::ResetState()
  32. {
  33.         m_compromised = false;
  34.         m_farFromCoverLocation = false;
  35.         m_locationEffectiveHeight = FLT_MAX;
  36.         m_location.zero();
  37.         m_normal.zero();
  38. }
  39.  
  40. void CoverUser::SetCoverID(const CoverID& coverID)
  41. {
  42.         if (m_coverID != coverID)
  43.         {
  44.                 if (m_coverID)
  45.                         gAIEnv.pCoverSystem->SetCoverOccupied(m_coverID, false, m_params.userID);
  46.  
  47.                 ResetState();
  48.  
  49.                 m_coverID = coverID;
  50.  
  51.                 if (m_coverID)
  52.                         gAIEnv.pCoverSystem->SetCoverOccupied(m_coverID, true, m_params.userID);
  53.         }
  54. }
  55.  
  56. const CoverID& CoverUser::GetCoverID() const
  57. {
  58.         return m_coverID;
  59. }
  60.  
  61. void CoverUser::SetParams(const Params& params)
  62. {
  63.         m_params = params;
  64. }
  65.  
  66. const CoverUser::Params& CoverUser::GetParams() const
  67. {
  68.         return m_params;
  69. }
  70.  
  71. void CoverUser::Update(float updateTime, const Vec3& pos, const Vec3* eyes, uint32 eyeCount, float minEffectiveCoverHeight /*= 0.001f*/)
  72. {
  73.         if (m_coverID)
  74.         {
  75.                 FRAME_PROFILER("CoverUser::Update 2", gEnv->pSystem, PROFILE_AI);
  76.  
  77.                 Vec3 coverNormal;
  78.                 Vec3 coverLocation = gAIEnv.pCoverSystem->GetCoverLocation(m_coverID, m_params.distanceToCover, 0, &coverNormal);
  79.  
  80.                 const float maxAllowedDistanceFromCoverLocationSq = sqr(1.0f);
  81.                 m_farFromCoverLocation = (pos - coverLocation).len2() > maxAllowedDistanceFromCoverLocationSq;
  82.  
  83.                 if (!m_compromised)
  84.                         m_compromised = !IsInCover(coverLocation, m_params.inCoverRadius, eyes, eyeCount);
  85.  
  86.                 if (!m_compromised)
  87.                 {
  88.                         if (coverNormal.dot(eyes[0] - coverLocation) >= 0.0001f)
  89.                                 m_compromised = true;
  90.                 }
  91.  
  92.                 if (!m_compromised)
  93.                 {
  94.                         m_locationEffectiveHeight = CalculateEffectiveHeightAt(coverLocation, eyes, eyeCount);
  95.  
  96.                         if (m_locationEffectiveHeight < minEffectiveCoverHeight)
  97.                                 m_compromised = true;
  98.  
  99.                         m_location = pos;
  100.                 }
  101.         }
  102. }
  103.  
  104. void CoverUser::UpdateWhileMoving(float updateTime, const Vec3& pos, const Vec3* eyes, uint32 eyeCount)
  105. {
  106.         if (m_coverID)
  107.         {
  108.                 FRAME_PROFILER("CoverUser::Update 2", gEnv->pSystem, PROFILE_AI);
  109.  
  110.                 if (!m_compromised)
  111.                 {
  112.                         Vec3 coverLocation = gAIEnv.pCoverSystem->GetCoverLocation(m_coverID, m_params.distanceToCover);
  113.                         m_locationEffectiveHeight = CalculateEffectiveHeightAt(coverLocation, eyes, eyeCount);
  114.  
  115.                         if (m_locationEffectiveHeight <= 0.001f)
  116.                                 m_compromised = true;
  117.  
  118.                         m_location = pos;
  119.                 }
  120.         }
  121. }
  122.  
  123. void CoverUser::UpdateNormal(const Vec3& pos)
  124. {
  125.         m_normal.zero();
  126.  
  127.         if (CoverSurfaceID surfaceID = gAIEnv.pCoverSystem->GetSurfaceID(m_coverID))
  128.         {
  129.                 const CoverPath& path = gAIEnv.pCoverSystem->GetCoverPath(surfaceID, m_params.distanceToCover);
  130.                 m_normal = -path.GetNormalAt(pos);
  131.         }
  132. }
  133.  
  134. bool CoverUser::IsCompromised() const
  135. {
  136.         return m_compromised;
  137. }
  138.  
  139. bool CoverUser::IsFarFromCoverLocation() const
  140. {
  141.         return m_farFromCoverLocation;
  142. }
  143.  
  144. float CoverUser::GetLocationEffectiveHeight() const
  145. {
  146.         return m_locationEffectiveHeight;
  147. }
  148.  
  149. void CoverUser::DebugDraw() const
  150. {
  151.         CDebugDrawContext dc;
  152.  
  153.         if (m_locationEffectiveHeight > 0.0f && m_locationEffectiveHeight < FLT_MAX)
  154.                 dc->DrawLine(m_location, Col_LimeGreen, m_location + CoverUp * m_locationEffectiveHeight, Col_LimeGreen, 25.0f);
  155. }
  156.  
  157. bool CoverUser::IsInCover(const Vec3& pos, float radius, const Vec3* eyes, uint32 eyeCount) const
  158. {
  159.         const CoverSurface& surface = gAIEnv.pCoverSystem->GetCoverSurface(m_coverID);
  160.  
  161.         if (radius < 0.0001f)
  162.         {
  163.                 for (uint32 i = 0; i < eyeCount; ++i)
  164.                 {
  165.                         if (!surface.IsPointInCover(eyes[i], pos))
  166.                                 return false;
  167.                 }
  168.         }
  169.         else
  170.         {
  171.                 for (uint32 i = 0; i < eyeCount; ++i)
  172.                 {
  173.                         if (!surface.IsCircleInCover(eyes[i], pos, radius))
  174.                                 return false;
  175.                 }
  176.         }
  177.  
  178.         return true;
  179. }
  180.  
  181. float CoverUser::CalculateEffectiveHeightAt(const Vec3& pos, const Vec3* eyes, uint32 eyeCount) const
  182. {
  183.         float lowestHeightSq = FLT_MAX;
  184.  
  185.         const CoverSurface& surface = gAIEnv.pCoverSystem->GetCoverSurface(m_coverID);
  186.  
  187.         for (uint32 i = 0; i < eyeCount; ++i)
  188.         {
  189.                 float heightSq;
  190.                 if (!surface.GetCoverOcclusionAt(eyes[i], pos, &heightSq))
  191.                         return -1.0f;
  192.  
  193.                 if (heightSq <= lowestHeightSq)
  194.                         lowestHeightSq = heightSq;
  195.         }
  196.  
  197.         return sqrt_tpl(lowestHeightSq);
  198. }
  199.  
  200. const Vec3& CoverUser::GetCoverNormal() const
  201. {
  202.         return m_normal;
  203. }
  204.  
  205. Vec3 CoverUser::GetCoverLocation() const
  206. {
  207.         return gAIEnv.pCoverSystem->GetCoverLocation(m_coverID, m_params.distanceToCover);
  208. }
  209.  
downloadCoverUser.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top