BVB Source Codes

CRYENGINE Show IslandConnections.cpp Source code

Return Download CRYENGINE: download IslandConnections.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 "IslandConnections.h"
  5. #include <CryAISystem/IPathfinder.h>
  6. #include "../NavigationSystem/NavigationSystem.h"
  7. #include "OffGridLinks.h"
  8. #ifdef CRYAISYSTEM_DEBUG
  9.         #include "DebugDrawContext.h"
  10. #endif
  11.  
  12. void MNM::IslandConnections::SetOneWayConnectionBetweenIsland(const MNM::GlobalIslandID fromIsland, const Link& link)
  13. {
  14.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_AI);
  15.  
  16. #if DEBUG_MNM_LOG_OFFMESH_LINK_OPERATIONS
  17.         AILogCommentID("<MNM:OffMeshLink>", "IslandConnections::SetOneWayConnectionBetweenIsland from %u to %u, linkId %u", fromIsland, link.toIsland, link.offMeshLinkID);
  18. #endif
  19.  
  20.         TLinksVector& links = m_islandConnections[fromIsland];
  21.         stl::push_back_unique(links, link);
  22. }
  23.  
  24. void MNM::IslandConnections::RemoveOneWayConnectionBetweenIsland(const MNM::GlobalIslandID fromIsland, const Link& link)
  25. {
  26.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_AI);
  27.  
  28. #if DEBUG_MNM_LOG_OFFMESH_LINK_OPERATIONS
  29.         AILogCommentID("<MNM:OffMeshLink>", "remove link %u of object %x", link.offMeshLinkID, link.objectIDThatCreatesTheConnection);
  30. #endif
  31.  
  32.         TIslandConnectionsMap::iterator linksIt = m_islandConnections.find(fromIsland);
  33.         if (linksIt != m_islandConnections.end())
  34.         {
  35.                 TLinksVector& links = linksIt->second;
  36.                 if (!links.empty())
  37.                 {
  38. #if DEBUG_MNM_LOG_OFFMESH_LINK_OPERATIONS
  39.                         for (const Link& l : links)
  40.                         {
  41.                                 if (l == link)
  42.                                 {
  43.                                         AILogCommentID("<MNM:OffMeshLink>", "object %x : from %u to %u, linkId %u", l.objectIDThatCreatesTheConnection, linksIt->first.GetStaticIslandID(), l.toIsland, l.offMeshLinkID);
  44.                                 }
  45.                         }
  46. #endif
  47.  
  48.                         links.erase(std::remove(links.begin(), links.end(), link), links.end());
  49.                 }
  50.  
  51.                 if (links.empty())
  52.                 {
  53.                         m_islandConnections.erase(linksIt);
  54.                 }
  55.         }
  56. }
  57.  
  58. struct IsLinkAssociatedWithObjectPredicate
  59. {
  60.         IsLinkAssociatedWithObjectPredicate(const uint32 objectId)
  61.                 : m_objectId(objectId)
  62.         {}
  63.  
  64.         bool operator()(const MNM::IslandConnections::Link& linkToEvaluate)
  65.         {
  66.                 return linkToEvaluate.objectIDThatCreatesTheConnection == m_objectId;
  67.         }
  68.  
  69.         uint32 m_objectId;
  70. };
  71.  
  72. void MNM::IslandConnections::RemoveAllIslandConnectionsForObject(const NavigationMeshID& meshID, const uint32 objectId)
  73. {
  74.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_AI);
  75.  
  76. #if DEBUG_MNM_LOG_OFFMESH_LINK_OPERATIONS
  77.         AILogCommentID("<MNM:OffMeshLink>", "remove all links of object %x", objectId);
  78. #endif
  79.  
  80.         TIslandConnectionsMap::iterator linksIt = m_islandConnections.begin();
  81.         TIslandConnectionsMap::iterator linksEnd = m_islandConnections.end();
  82.         for (; linksIt != linksEnd; )
  83.         {
  84.                 if (NavigationMeshID(linksIt->first.GetNavigationMeshIDAsUint32()) == meshID)
  85.                 {
  86.                         TLinksVector& links = linksIt->second;
  87.  
  88. #if DEBUG_MNM_LOG_OFFMESH_LINK_OPERATIONS
  89.                         for (const Link& l : links)
  90.                         {
  91.                                 if (IsLinkAssociatedWithObjectPredicate(objectId)(l))
  92.                                 {
  93.                                         AILogCommentID("<MNM:OffMeshLink>", "object %x : from %u to %u, linkId %u", l.objectIDThatCreatesTheConnection, linksIt->first.GetStaticIslandID(), l.toIsland, l.offMeshLinkID);
  94.                                 }
  95.                         }
  96. #endif
  97.  
  98.                         links.erase(std::remove_if(links.begin(), links.end(), IsLinkAssociatedWithObjectPredicate(objectId)), links.end());
  99.                         if (links.empty())
  100.                         {
  101.                                 linksIt = m_islandConnections.erase(linksIt);
  102.                                 continue;
  103.                         }
  104.                 }
  105.  
  106.                 ++linksIt;
  107.         }
  108. }
  109.  
  110. bool MNM::IslandConnections::CanNavigateBetweenIslands(const IEntity* pEntityToTestOffGridLinks, const MNM::GlobalIslandID fromIsland, const MNM::GlobalIslandID toIsland, TIslandsWay& way) const
  111. {
  112.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_AI);
  113.  
  114.         const static MNM::GlobalIslandID invalidID(MNM::Constants::eGlobalIsland_InvalidIslandID);
  115.         IF_UNLIKELY (fromIsland == invalidID || toIsland == invalidID)
  116.                 return false;
  117.  
  118.         if (fromIsland == toIsland)
  119.                 return true;
  120.  
  121.         const OffMeshNavigationManager* offMeshNavigationManager = gAIEnv.pNavigationSystem->GetOffMeshNavigationManager();
  122.         assert(offMeshNavigationManager);
  123.  
  124.         const size_t maxConnectedIsland = m_islandConnections.size();
  125.  
  126.         TIslandClosedSet closedSet;
  127.         closedSet.reserve(maxConnectedIsland);
  128.         IslandOpenList openList(maxConnectedIsland);
  129.         openList.InsertElement(IslandNode(fromIsland, 0));
  130.         TCameFromMap cameFrom;
  131.  
  132.         while (!openList.IsEmpty())
  133.         {
  134.                 IslandNode currentItem(openList.PopBestElement());
  135.  
  136.                 if (currentItem.id == toIsland)
  137.                 {
  138.                         ReconstructWay(cameFrom, fromIsland, toIsland, way);
  139.                         return true;
  140.                 }
  141.  
  142.                 closedSet.push_back(currentItem.id);
  143.  
  144.                 TIslandConnectionsMap::const_iterator islandConnectionsIt = m_islandConnections.find(currentItem.id);
  145.                 TIslandConnectionsMap::const_iterator islandConnectionsEnd = m_islandConnections.end();
  146.  
  147.                 if (islandConnectionsIt != islandConnectionsEnd)
  148.                 {
  149.                         const TLinksVector& links = islandConnectionsIt->second;
  150.                         TLinksVector::const_iterator linksIt = links.begin();
  151.                         TLinksVector::const_iterator linksEnd = links.end();
  152.                         for (; linksIt != linksEnd; ++linksIt)
  153.                         {
  154.                                 if (std::find(closedSet.begin(), closedSet.end(), linksIt->toIsland) != closedSet.end())
  155.                                         continue;
  156.  
  157.                                 const OffMeshLink* offmeshLink = offMeshNavigationManager->GetOffMeshLink(linksIt->offMeshLinkID);
  158.                                 const bool canUseLink = pEntityToTestOffGridLinks ? (offmeshLink && offmeshLink->CanUse(const_cast<IEntity*>(pEntityToTestOffGridLinks), NULL)) : true;
  159.  
  160.                                 if (canUseLink)
  161.                                 {
  162.                                         IslandNode nextIslandNode(linksIt->toIsland, currentItem.cost + 1.0f);
  163.  
  164.                                         // At this point, if we have multiple connections to the same neighbour island,
  165.                                         // we cannot detect which one is the one that allows us to have the actual shortest path
  166.                                         // so to keep the code simple we will keep the first one
  167.  
  168.                                         if (cameFrom.find(nextIslandNode) != cameFrom.end())
  169.                                                 continue;
  170.  
  171.                                         cameFrom[nextIslandNode] = currentItem;
  172.                                         openList.InsertElement(nextIslandNode);
  173.                                 }
  174.                         }
  175.                 }
  176.         }
  177.  
  178.         way.clear();
  179.  
  180.         return false;
  181. }
  182.  
  183. void MNM::IslandConnections::Reset()
  184. {
  185.         m_islandConnections.clear();
  186. }
  187.  
  188. void MNM::IslandConnections::ReconstructWay(const TCameFromMap& cameFromMap, const MNM::GlobalIslandID fromIsland, const MNM::GlobalIslandID toIsland, TIslandsWay& way) const
  189. {
  190.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_AI);
  191.  
  192.         IslandNode currentIsland(toIsland, .0f);
  193.         way.push_front(currentIsland.id);
  194.  
  195.         TCameFromMap::const_iterator element = cameFromMap.find(currentIsland);
  196.         while (element != cameFromMap.end())
  197.         {
  198.                 currentIsland = element->second;
  199.                 way.push_front(currentIsland.id);
  200.                 element = cameFromMap.find(currentIsland);
  201.         }
  202. }
  203.  
  204. #ifdef CRYAISYSTEM_DEBUG
  205.  
  206. void MNM::IslandConnections::DebugDraw() const
  207. {
  208.         const size_t totalGlobalIslandsRegisterd = m_islandConnections.size();
  209.         const size_t memoryUsedByALink = sizeof(Link);
  210.         size_t totalUsedMemory, totalEffectiveMemory;
  211.         totalUsedMemory = totalEffectiveMemory = 0;
  212.         TIslandConnectionsMap::const_iterator linksIt = m_islandConnections.begin();
  213.         TIslandConnectionsMap::const_iterator linksEnd = m_islandConnections.end();
  214.         for (; linksIt != linksEnd; ++linksIt)
  215.         {
  216.                 const TLinksVector& links = linksIt->second;
  217.                 totalUsedMemory += links.size() * memoryUsedByALink;
  218.                 totalEffectiveMemory += links.capacity() * memoryUsedByALink;
  219.         }
  220.  
  221.         const float conversionValue = 1024.0f;
  222.         CDebugDrawContext dc;
  223.         dc->Draw2dLabel(10.0f, 5.0f, 1.6f, Col_White, false, "Amount of global islands registered into the system %d", (int)totalGlobalIslandsRegisterd);
  224.         dc->Draw2dLabel(10.0f, 25.0f, 1.6f, Col_White, false, "MNM::IslandConnections total used memory %.2fKB", totalUsedMemory / conversionValue);
  225.         dc->Draw2dLabel(10.0f, 45.0f, 1.6f, Col_White, false, "MNM::IslandConnections total effective memory %.2fKB", totalEffectiveMemory / conversionValue);
  226. }
  227.  
  228. #endif
  229.  
downloadIslandConnections.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