BVB Source Codes

CRYENGINE Show MovementBlock_UseSmartObject.cpp Source code

Return Download CRYENGINE: download MovementBlock_UseSmartObject.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 "MovementBlock_UseSmartObject.h"
  5. #include "MovementActor.h"
  6. #include <CryAISystem/MovementUpdateContext.h>
  7. #include "MovementHelpers.h"
  8. #include "Navigation/NavigationSystem/NavigationSystem.h"
  9. #include "Navigation/MNM/OffGridLinks.h"
  10. #include "PipeUser.h"
  11. #include "AIBubblesSystem/AIBubblesSystem.h"
  12.  
  13. namespace Movement
  14. {
  15. namespace MovementBlocks
  16. {
  17. UseSmartObject::UseSmartObject(
  18.   const CNavPath& path,
  19.   const PathPointDescriptor::OffMeshLinkData& mnmData,
  20.   const MovementStyle& style)
  21.         : Base(path, style)
  22.         , m_smartObjectMNMData(mnmData)
  23.         , m_timeSpentWaitingForSmartObjectToBecomeFree(0.0f)
  24. {
  25.  
  26. }
  27.  
  28. void UseSmartObject::Begin(IMovementActor& actor)
  29. {
  30.         Base::Begin(actor);
  31.         m_timeSpentWaitingForSmartObjectToBecomeFree = 0.0f;
  32. }
  33.  
  34. Movement::Block::Status UseSmartObject::Update(const MovementUpdateContext& context)
  35. {
  36.         const Status baseStatus = Base::Update(context);
  37.  
  38.         if (m_state == Traverse)
  39.         {
  40.                 PathFollowResult result;
  41.                 Movement::Helpers::UpdatePathFollowing(result, context, m_upcomingStyle);
  42.         }
  43.  
  44.         return baseStatus;
  45. }
  46.  
  47. void UseSmartObject::SetUpcomingPath(const CNavPath& upcomingPath)
  48. {
  49.         m_upcomingPath = upcomingPath;
  50. }
  51.  
  52. void UseSmartObject::SetUpcomingStyle(const MovementStyle& upcomingStyle)
  53. {
  54.         m_upcomingStyle = upcomingStyle;
  55. }
  56.  
  57. void UseSmartObject::OnTraverseStarted(const MovementUpdateContext& context)
  58. {
  59.         Movement::Helpers::BeginPathFollowing(context.actor, m_upcomingStyle, m_upcomingPath);
  60. }
  61.  
  62. UseExactPositioningBase::TryRequestingExactPositioningResult UseSmartObject::TryRequestingExactPositioning(const MovementUpdateContext& context)
  63. {
  64.         MNM::OffMeshLink* pOffMeshLink = gAIEnv.pNavigationSystem->GetOffMeshNavigationManager()->GetOffMeshLink(m_smartObjectMNMData.offMeshLinkID);
  65.         OffMeshLink_SmartObject* pSOLink = pOffMeshLink ? pOffMeshLink->CastTo<OffMeshLink_SmartObject>() : NULL;
  66.  
  67.         if (pSOLink)
  68.         {
  69.                 CSmartObjectManager* pSmartObjectManager = gAIEnv.pSmartObjectManager;
  70.                 CSmartObject* pSmartObject = pSOLink->m_pSmartObject;
  71.  
  72.                 if (pSmartObjectManager->IsSmartObjectBusy(pSmartObject))
  73.                 {
  74.                         // Smart object is busy so stop movement and wait for your turn
  75.                         context.actor.GetAdapter().ClearMovementState();
  76.  
  77.                         m_timeSpentWaitingForSmartObjectToBecomeFree += gEnv->pTimer->GetFrameTime();
  78.                         if (m_timeSpentWaitingForSmartObjectToBecomeFree > 10.0f)
  79.                         {
  80.                                 const EntityId actorEntityId = context.actor.GetEntityId();
  81.                                 if (IEntity* entity = gEnv->pEntitySystem->GetEntity(actorEntityId))
  82.                                 {
  83.                                         const Vec3 teleportDestination = GetSmartObjectEndPosition();
  84.  
  85.                                         // Report
  86.                                         stack_string message;
  87.                                         message.Format("Agent spent too long waiting for a smart object to become free. UseSmartObject spent too long waiting. Teleported to end of smart object (%f, %f, %f)", teleportDestination.x, teleportDestination.y, teleportDestination.z);
  88.                                         AIQueueBubbleMessage("WaitForSmartObjectToBecomeFree", actorEntityId, message.c_str(), eBNS_Balloon | eBNS_LogWarning);
  89.  
  90.                                         // Teleport
  91.                                         Matrix34 transform = entity->GetWorldTM();
  92.                                         transform.SetTranslation(teleportDestination);
  93.                                         entity->SetWorldTM(transform);
  94.  
  95.                                         return RequestFailed_FinishImmediately;
  96.                                 }
  97.                         }
  98.  
  99.                         return RequestDelayed_SkipPathFollowing;
  100.                 }
  101.                 else if (context.actor.GetAdapter().IsClosestToUseTheSmartObject(*pSOLink))
  102.                 {
  103.                         // Fill in the actor target request, and figure out the navSO method.
  104.                         if (context.actor.GetAdapter().PrepareNavigateSmartObject(pSmartObject, pSOLink))
  105.                         {
  106.                                 return RequestSucceeded;
  107.                         }
  108.                         else
  109.                         {
  110.                                 // Attention!
  111.                                 // We could not navigate via the selected smart object.
  112.                                 //
  113.                                 // One reason why this happens is because the entrance
  114.                                 // or exit area is blocked by some dynamic obstacle.
  115.                                 //
  116.                                 // Todo:
  117.                                 // 1. This should be handled before the path is constructed!
  118.                                 // 2. The smart object should have a physics phantom that
  119.                                 //    report back to listeners when blocked/unblocked
  120.                                 //    so that the smart object and paths currently including
  121.                                 //    it in a planned path can be correctly invalidated.
  122.  
  123.                                 context.actor.GetAdapter().ResetActorTargetRequest();
  124.                                 context.actor.GetAdapter().InvalidateSmartObjectLink(pSmartObject, pSOLink);
  125.  
  126.                                 assert(false);
  127.  
  128.                                 // TODO: Handle this failure.
  129.  
  130.                                 return RequestFailed_FinishImmediately;
  131.                         }
  132.                 }
  133.                 else
  134.                 {
  135.                         // We are not the closest candidate user
  136.                         // Currently simply keeps moving towards the target
  137.                         return RequestDelayed_ContinuePathFollowing;
  138.                 }
  139.         }
  140.         else
  141.         {
  142.                 // TODO: listen to links unregistration and try to handle invalid movement block sooner than here
  143.  
  144.                 // No SOLink - link was unregistered (blocked, deleted...) and cannot be traversed anymore.
  145.                 return RequestFailed_CancelImmediately;
  146.         }
  147. }
  148.  
  149. void UseSmartObject::HandleExactPositioningError(const MovementUpdateContext& context)
  150. {
  151.         const EntityId actorEntityId = context.actor.GetEntityId();
  152.         if (IEntity* entity = gEnv->pEntitySystem->GetEntity(actorEntityId))
  153.         {
  154.                 {
  155.                         const Vec3 teleportDestination = GetSmartObjectEndPosition();
  156.  
  157.                         // Report
  158.                         stack_string message;
  159.                         message.Format("Exact positioning failed to get me to the start of the smart object. Teleported to end of smart object (%f, %f, %f)", teleportDestination.x, teleportDestination.y, teleportDestination.z);
  160.                         AIQueueBubbleMessage("PrepareForSmartObjectError", actorEntityId, message.c_str(), eBNS_Balloon | eBNS_LogWarning);
  161.  
  162.                         // Teleport
  163.                         Matrix34 transform = entity->GetWorldTM();
  164.                         transform.SetTranslation(teleportDestination);
  165.                         entity->SetWorldTM(transform);
  166.  
  167.                         // Cancel exact positioning
  168.                         context.actor.GetAdapter().ResetActorTargetRequest();
  169.                 }
  170.         }
  171. }
  172.  
  173. Vec3 UseSmartObject::GetSmartObjectEndPosition() const
  174. {
  175.         MNM::OffMeshLink* pOffMeshLink = gAIEnv.pNavigationSystem->GetOffMeshNavigationManager()->GetOffMeshLink(m_smartObjectMNMData.offMeshLinkID);
  176.         OffMeshLink_SmartObject* pSOLink = pOffMeshLink ? pOffMeshLink->CastTo<OffMeshLink_SmartObject>() : NULL;
  177.  
  178.         assert(pSOLink);
  179.         if (pSOLink)
  180.         {
  181.                 CSmartObject* pSmartObject = pSOLink->m_pSmartObject;
  182.                 const Vec3 endPosition = pSmartObject->GetHelperPos(pSOLink->m_pToHelper);
  183.                 return endPosition;
  184.         }
  185.  
  186.         return Vec3Constants<float>::fVec3_Zero;
  187. }
  188. }
  189. }
  190.  
downloadMovementBlock_UseSmartObject.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