BVB Source Codes

CRYENGINE Show WorldMonitor.cpp Source code

Return Download CRYENGINE: download WorldMonitor.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 "WorldMonitor.h"
  5. #include "Navigation/NavigationSystem/NavigationSystem.h"
  6. #include "DebugDrawContext.h"
  7.  
  8. //#pragma optimize("", off)
  9. //#pragma inline_depth(0)
  10.  
  11. WorldMonitor::WorldMonitor()
  12.         : m_callback(0)
  13.         , m_enabled(gEnv->IsEditor())
  14. {
  15. }
  16.  
  17. WorldMonitor::WorldMonitor(const Callback& callback)
  18.         : m_callback(callback)
  19.         , m_enabled(gEnv->IsEditor())
  20. {
  21. }
  22.  
  23. void WorldMonitor::Start()
  24. {
  25.         AILogComment("Navigation System WorldMonitor::Start");
  26.  
  27.         if (IsEnabled())
  28.         {
  29.                 if (m_callback)
  30.                 {
  31.                         gEnv->pPhysicalWorld->AddEventClient(EventPhysStateChange::id, StateChangeHandler, 1, 1.0f);
  32.                         gEnv->pPhysicalWorld->AddEventClient(EventPhysEntityDeleted::id, EntityRemovedHandler, 1, 1.0f);
  33.                         gEnv->pPhysicalWorld->AddEventClient(EventPhysEntityDeleted::id, EntityRemovedHandlerAsync, 0, 1.0f);
  34.                 }
  35.         }
  36. }
  37.  
  38. void WorldMonitor::Stop()
  39. {
  40.         AILogComment("Navigation System WorldMonitor::Stop");
  41.  
  42.         if (IsEnabled())
  43.         {
  44.                 if (m_callback)
  45.                 {
  46.                         gEnv->pPhysicalWorld->RemoveEventClient(EventPhysStateChange::id, StateChangeHandler, 1);
  47.                         gEnv->pPhysicalWorld->RemoveEventClient(EventPhysEntityDeleted::id, EntityRemovedHandler, 1);
  48.                         gEnv->pPhysicalWorld->RemoveEventClient(EventPhysEntityDeleted::id, EntityRemovedHandlerAsync, 0);
  49.                 }
  50.         }
  51. }
  52.  
  53. bool WorldMonitor::IsEnabled() const
  54. {
  55.         return m_enabled;
  56. }
  57.  
  58. int WorldMonitor::StateChangeHandler(const EventPhys* pPhysEvent)
  59. {
  60.         WorldMonitor* pthis = gAIEnv.pNavigationSystem->GetWorldMonitor();
  61.  
  62.         assert(pthis != NULL);
  63.         assert(pthis->IsEnabled());
  64.  
  65.         {
  66.                 const EventPhysStateChange* event = (EventPhysStateChange*)pPhysEvent;
  67.  
  68.                 bool consider = false;
  69.  
  70.                 if (event->iSimClass[1] == SC_STATIC)
  71.                         consider = true;
  72.                 else
  73.                 {
  74.                         IPhysicalEntity* physEnt = event->pEntity;
  75.  
  76.                         if ((event->iSimClass[1] == SC_SLEEPING_RIGID) ||
  77.                             (event->iSimClass[1] == SC_ACTIVE_RIGID) ||
  78.                             (physEnt->GetType() == PE_RIGID))
  79.                         {
  80.                                 consider = NavigationSystemUtils::IsDynamicObjectPartOfTheMNMGenerationProcess(physEnt);
  81.                         }
  82.                 }
  83.  
  84.                 if (consider)
  85.                 {
  86.                         const AABB aabbOld(event->BBoxOld[0], event->BBoxOld[1]);
  87.                         const AABB aabbNew(event->BBoxNew[0], event->BBoxNew[1]);
  88.  
  89.                         if (((aabbOld.min - aabbNew.min).len2() + (aabbOld.max - aabbNew.max).len2()) > 0.0f)
  90.                         {
  91.                                 pthis->m_callback(aabbOld);
  92.                                 pthis->m_callback(aabbNew);
  93.  
  94.                                 if (gAIEnv.CVars.DebugDrawNavigationWorldMonitor)
  95.                                 {
  96.                                         CDebugDrawContext dc;
  97.  
  98.                                         dc->DrawAABB(aabbOld, IDENTITY, false, Col_DarkGray, eBBD_Faceted);
  99.                                         dc->DrawAABB(aabbNew, IDENTITY, false, Col_White, eBBD_Faceted);
  100.                                 }
  101.                         }
  102.                 }
  103.         }
  104.  
  105.         return 1;
  106. }
  107.  
  108. int WorldMonitor::EntityRemovedHandler(const EventPhys* pPhysEvent)
  109. {
  110.         WorldMonitor* pthis = gAIEnv.pNavigationSystem->GetWorldMonitor();
  111.  
  112.         assert(pthis != NULL);
  113.         assert(pthis->IsEnabled());
  114.  
  115.         AABB aabb;
  116.  
  117.         if (ShallEventPhysEntityDeletedBeHandled(pPhysEvent, aabb))
  118.         {
  119.                 pthis->m_callback(aabb);
  120.  
  121.                 if (gAIEnv.CVars.DebugDrawNavigationWorldMonitor)
  122.                 {
  123.                         CDebugDrawContext dc;
  124.  
  125.                         dc->DrawAABB(aabb, IDENTITY, false, Col_White, eBBD_Faceted);
  126.                 }
  127.         }
  128.  
  129.         return 1;
  130. }
  131.  
  132. int WorldMonitor::EntityRemovedHandlerAsync(const EventPhys* pPhysEvent)
  133. {
  134.         WorldMonitor* pthis = gAIEnv.pNavigationSystem->GetWorldMonitor();
  135.  
  136.         assert(pthis != NULL);
  137.         assert(pthis->IsEnabled());
  138.  
  139.         AABB aabb;
  140.  
  141.         if (ShallEventPhysEntityDeletedBeHandled(pPhysEvent, aabb))
  142.         {
  143.                 pthis->m_queuedAABBChanges.push_back(aabb);
  144.         }
  145.  
  146.         return 1;
  147. }
  148.  
  149. bool WorldMonitor::ShallEventPhysEntityDeletedBeHandled(const EventPhys* pPhysEvent, AABB& outAabb)
  150. {
  151.         assert(pPhysEvent->idval == EventPhysEntityDeleted::id);
  152.  
  153.         const EventPhysEntityDeleted* event = static_cast<const EventPhysEntityDeleted*>(pPhysEvent);
  154.  
  155.         bool consider = false;
  156.  
  157.         IPhysicalEntity* physEnt = event->pEntity;
  158.         pe_type type = physEnt->GetType();
  159.  
  160.         if (type == PE_STATIC)
  161.         {
  162.                 consider = true;
  163.         }
  164.         else if (type == PE_RIGID)
  165.         {
  166.                 consider = NavigationSystemUtils::IsDynamicObjectPartOfTheMNMGenerationProcess(physEnt);
  167.         }
  168.  
  169.         if (consider)
  170.         {
  171.                 pe_status_pos sp;
  172.  
  173.                 if (physEnt->GetStatus(&sp))
  174.                 {
  175.                         // Careful: the AABB we just received is relative to the entity's position. This is specific to the EventPhysEntityDeleted we're currently handling.
  176.                         outAabb.min = sp.BBox[0] + sp.pos;
  177.                         outAabb.max = sp.BBox[1] + sp.pos;
  178.                         return true;
  179.                 }
  180.         }
  181.  
  182.         return false;
  183. }
  184.  
  185. void WorldMonitor::FlushPendingAABBChanges()
  186. {
  187.         std::vector<AABB> changesInTheWorld;
  188.         m_queuedAABBChanges.swap(changesInTheWorld);
  189.  
  190.         if (!changesInTheWorld.empty())
  191.         {
  192.                 for (const AABB& aabb : changesInTheWorld)
  193.                 {
  194.                         m_callback(aabb);
  195.  
  196.                         if (gAIEnv.CVars.DebugDrawNavigationWorldMonitor)
  197.                         {
  198.                                 CDebugDrawContext dc;
  199.  
  200.                                 dc->DrawAABB(aabb, IDENTITY, false, Col_White, eBBD_Faceted);
  201.                         }
  202.                 }
  203.         }
  204. }
  205.  
downloadWorldMonitor.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