BVB Source Codes

CRYENGINE Show TacticalPointSystem.h Source code

Return Download CRYENGINE: download TacticalPointSystem.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /********************************************************************
  4.    ---------------------------------------------------------------------
  5.    File name:   TacticalPointSystem.h
  6.    $Id$
  7.    $DateTime$
  8.    Description: System for Tactical Point queries
  9.    (Major extension of the old hidepoint search concept)
  10.    ---------------------------------------------------------------------
  11.    History:
  12.    - 02:07:2007 : Created by Matthew Jack
  13.  
  14.  *********************************************************************/
  15.  
  16. #ifndef __TacticalPointSystem_H__
  17. #define __TacticalPointSystem_H__
  18.  
  19. #if _MSC_VER > 1000
  20.         #pragma once
  21. #endif
  22.  
  23. #include <CryAISystem/ITacticalPointSystem.h>
  24.  
  25. #include "TacticalPointQuery.h"
  26.  
  27. #include <CryMath/Cry_Math.h>
  28. #include "TacticalPointQueryEnum.h"
  29. #include "PipeUser.h"
  30. #include "HideSpot.h"
  31.  
  32. #include "../Cover/CoverSystem.h"
  33. #include "PostureManager.h"
  34.  
  35. // Forward declarations
  36.  
  37. class CPipeUser;
  38. class CTacticalPointQuery;
  39. class COptionCriteria;
  40.  
  41. enum ETacticalPointQuery;
  42. enum ETacticalPointQueryParameter;
  43.  
  44. //----------------------------------------------------------------------------------------------//
  45.  
  46. inline void InitQueryContextFromActor(CAIActor* pAIActor, QueryContext& context)
  47. {
  48.         if (!pAIActor)
  49.         {
  50.                 AIAssert(0);
  51.                 return;
  52.         }
  53.  
  54.         context.pAIActor = pAIActor;
  55.         context.actorPos = pAIActor->GetPos();
  56.         context.actorDir = pAIActor->GetViewDir();
  57.         context.actorNavCaps = pAIActor->m_movementAbility.pathfindingProperties.navCapMask;
  58.         context.actorEntityId = pAIActor->GetEntityID();
  59.  
  60.         context.actorRadius = pAIActor->GetParameters().m_fPassRadius;
  61.         context.distanceToCover = pAIActor->GetParameters().distanceToCover;
  62.         context.inCoverRadius = pAIActor->GetParameters().inCoverRadius;
  63.         context.effectiveCoverHeight = pAIActor->GetParameters().effectiveCoverHeight;
  64.  
  65.         context.realTarget.zero();
  66.         context.realTargetDir.zero();
  67.  
  68.         IAIObject* attentionTarget = pAIActor->GetAttentionTarget();
  69.         if (attentionTarget)
  70.         {
  71.                 context.attentionTarget = attentionTarget->GetPos();
  72.                 context.attentionTargetDir = attentionTarget->GetViewDir();
  73.  
  74.                 if (const IAIObject* liveTarget = CAIActor::GetLiveTarget(static_cast<const CAIObject*>(attentionTarget)))
  75.                 {
  76.                         context.realTarget = liveTarget->GetPos();
  77.                         context.realTargetDir = liveTarget->GetViewDir();
  78.                 }
  79.         }
  80.         else
  81.         {
  82.                 context.attentionTarget.zero();
  83.                 context.attentionTargetDir.zero();
  84.         }
  85.  
  86.         CPipeUser* pPipeUser = pAIActor->CastToCPipeUser();
  87.         IAIObject* refPoint = pPipeUser ? pPipeUser->GetRefPoint() : 0;
  88.  
  89.         if (refPoint)
  90.         {
  91.                 context.referencePoint = refPoint->GetPos();
  92.                 context.referencePointDir = refPoint->GetViewDir();
  93.         }
  94.         else
  95.         {
  96.                 context.referencePoint.zero();
  97.                 context.referencePointDir.zero();
  98.         }
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------------//
  102.  
  103. class CTacticalPoint : public ITacticalPoint
  104. {
  105. public:
  106.         // cppcheck-suppress uninitMemberVar
  107.         CTacticalPoint() : eTPType(eTPT_None) {}
  108.  
  109.         // Need this for the STL containers that we shouldn't be using
  110.         CTacticalPoint(const CTacticalPoint& that)
  111.         {
  112.                 *this = that;
  113.         }
  114.  
  115.         const CTacticalPoint& operator=(const CTacticalPoint& that)
  116.         {
  117.                 eTPType = that.eTPType;
  118.                 memcpy(&vPos, &that.vPos, sizeof(vPos));
  119.  
  120.                 switch (eTPType)
  121.                 {
  122.                 case eTPT_None:
  123.                 case eTPT_Point:
  124.                         break;
  125.  
  126.                 case eTPT_EntityPos:
  127.                         m_entityId = that.m_entityId;
  128.                         break;
  129.  
  130.                 case eTPT_HideSpot:
  131.                         m_Hidespot = that.m_Hidespot;
  132.                         break;
  133.  
  134.                 case eTPT_AIObject:
  135.                         m_refObject = that.m_refObject;
  136.                         break;
  137.  
  138.                 case eTPT_CoverID:
  139.                         coverID = that.coverID;
  140.                         break;
  141.  
  142.                 default:
  143.                         assert(false);
  144.                 }
  145.                 return *this;
  146.         }
  147.  
  148.         bool operator==(const CTacticalPoint& that) const
  149.         {
  150.                 if (eTPType != that.eTPType)
  151.                         return false;
  152.  
  153.                 switch (eTPType)
  154.                 {
  155.                 case eTPT_None:
  156.                         return false;
  157.                 case eTPT_HideSpot:
  158.                         {
  159.                                 if (m_Hidespot.pAnchorObject && (m_Hidespot.pAnchorObject != that.m_Hidespot.pAnchorObject))
  160.                                         return false;
  161.                                 else if (m_Hidespot.info.type != that.m_Hidespot.info.type)
  162.                                         return false;
  163.                                 else
  164.                                         return ((m_Hidespot.info.pos - that.m_Hidespot.info.pos).len2() < 0.5f * 0.5f);
  165.                         }
  166.                 case eTPT_Point:
  167.                         return ((vPos - that.vPos).len2() < 0.5f * 0.5f);
  168.                 case eTPT_EntityPos:
  169.                         return m_entityId == that.m_entityId;
  170.                 case eTPT_AIObject:
  171.                         return m_refObject == that.m_refObject;
  172.                 case eTPT_CoverID:
  173.                         return coverID == that.coverID;
  174.                 default:
  175.                         break;
  176.                 }
  177.                 return false;
  178.         }
  179.  
  180.         bool operator!=(const CTacticalPoint& that) const
  181.         {
  182.                 return !operator==(that);
  183.         }
  184.  
  185.         // cppcheck-suppress uninitMemberVar
  186.         explicit CTacticalPoint(const SHideSpot& hidespot)
  187.                 : eTPType(eTPT_HideSpot)
  188.                 , m_Hidespot(hidespot)
  189.                 , m_entityId(0)
  190.                 , vPos(hidespot.info.pos)
  191.         {
  192.         }
  193.  
  194.         // cppcheck-suppress uninitMemberVar
  195.         explicit CTacticalPoint(const Vec3& vPoint) : eTPType(eTPT_Point)
  196.         {
  197.                 vPos = vPoint;
  198.         }
  199.  
  200.         // cppcheck-suppress uninitMemberVar
  201.         explicit CTacticalPoint(CWeakRef<CAIObject> refObj) : eTPType(eTPT_AIObject)
  202.         {
  203.                 // Ideally, we might not store position in this case, as the reference may become invalid
  204.                 CAIObject* pObj = refObj.GetAIObject();
  205.                 vPos = (pObj ? pObj->GetPos() : ZERO);
  206.                 m_refObject = refObj;
  207.         }
  208.  
  209.         CTacticalPoint(EntityId entityId, const Vec3& vPoint) : eTPType(eTPT_EntityPos)
  210.         {
  211.                 vPos = vPoint;
  212.                 m_entityId = entityId;
  213.         }
  214.  
  215.         // cppcheck-suppress uninitMemberVar
  216.         CTacticalPoint(const CoverID& coverID, const Vec3& pos) : eTPType(eTPT_CoverID)
  217.         {
  218.                 this->vPos = pos;
  219.                 this->coverID = coverID;
  220.         }
  221.  
  222.         ~CTacticalPoint()
  223.         {
  224.                 switch (eTPType)
  225.                 {
  226.                 case eTPT_None:
  227.                 case eTPT_Point:
  228.                 case eTPT_EntityPos:
  229.                 case eTPT_HideSpot:
  230.                 case eTPT_AIObject:
  231.                 case eTPT_CoverID:
  232.                         break;
  233.  
  234.                 default:
  235.                         assert(false);
  236.                 }
  237.                 ;
  238.         }
  239.  
  240.         virtual Vec3                               GetPos() const          { return vPos; }
  241.         virtual void                               SetPos(Vec3 pos)        { vPos = pos; }
  242.         virtual ITacticalPoint::ETacticalPointType GetType() const         { return eTPType; }
  243.         virtual const SHideSpot*                   GetHidespot() const     { return (eTPType == eTPT_HideSpot ? &m_Hidespot : NULL); }
  244.         virtual tAIObjectID                        GetAIObjectId() const   { return (eTPType == eTPT_AIObject ? m_refObject.GetObjectID() : INVALID_AIOBJECTID); }
  245.         virtual bool                               IsValid() const         { return eTPType != eTPT_None; }
  246.  
  247.         const SHideSpotInfo*                       GetHidespotInfo() const { return (eTPType == eTPT_HideSpot ? &(m_Hidespot.info) : NULL); }
  248.  
  249.         inline CoverID                             GetCoverID() const
  250.         {
  251.                 return coverID;
  252.         }
  253.  
  254.         // These methods only available within the AI system
  255.         CWeakRef<CAIObject>       GetAI()             { return (eTPType == eTPT_AIObject ? m_refObject : NILREF); }
  256.         CWeakRef<const CAIObject> GetAI() const       { return (eTPType == eTPT_AIObject ? m_refObject : NILREF); }
  257.  
  258.         EntityId                  GetEntityId() const { return (eTPType == eTPT_EntityPos ? m_entityId : INVALID_ENTITYID); }
  259.  
  260. private:
  261.         // Type specifying the relevant union member if any
  262.         ETacticalPointType  eTPType;
  263.  
  264.         SHideSpot           m_Hidespot;
  265.         EntityId            m_entityId;
  266.         CWeakRef<CAIObject> m_refObject;
  267.  
  268.         CoverID             coverID;
  269.         Vec3                vPos;
  270. };
  271. typedef std::vector<CTacticalPoint> TTacticalPoints;
  272.  
  273. //----------------------------------------------------------------------------------------------//
  274.  
  275. class CTacticalPointGenerateResult : public ITacticalPointGenerateResult
  276. {
  277. public:
  278.         virtual bool AddHideSpot(const SHideSpot& hidespot);
  279.         virtual bool AddPoint(const Vec3& point);
  280.         virtual bool AddEntity(IEntity* pEntity);
  281.         virtual bool AddEntityPoint(IEntity* pEntity, const Vec3& point);
  282.  
  283.         bool         HasPoints() const;
  284.         void         GetPoints(TTacticalPoints& points) const;
  285.  
  286. private:
  287.         TTacticalPoints m_Points;
  288. };
  289.  
  290. // There need only ever be one of these in game, hence a singleton
  291. // Creates on first request, which is lightweight
  292.  
  293. class CTacticalPointSystem : public ITacticalPointSystem
  294. {
  295.         friend class COptionCriteria;
  296.         friend class CCriterion;
  297.  
  298.         friend struct SAIEnvironment;
  299.         friend class CAISystem; //TODO see why these are needed and why this has a private constructor
  300.  
  301. public:
  302.         static void RegisterCVars();
  303.  
  304. public:
  305.         // ---------- ITacticalPointSystem methods ----------
  306.         // Separate this out completely into an adaptor?
  307.  
  308.         void Reset();
  309.  
  310.         // Get a new query ID, to allow us to build a new query
  311.         TPSQueryID  CreateQueryID(const char* psName);
  312.         // Destroy a query ID and release all resources associated with it
  313.         bool        DestroyQueryID(TPSQueryID queryID);
  314.         // Get the Name of a query by ID
  315.         const char* GetQueryName(TPSQueryID queryID);
  316.         // Get the query ID of a query by name
  317.         TPSQueryID  GetQueryID(const char* psName);
  318.         // Returns a pointer to indexed option of a given query
  319.         const char* GetOptionLabel(TPSQueryID queryID, int option);
  320.  
  321.         // Build up a query
  322.         // The "option" parameter allows you to build up fallback options
  323.         bool AddToParameters(TPSQueryID queryID, const char* sSpec, float fValue, int option = 0);
  324.         bool AddToParameters(TPSQueryID queryID, const char* sSpec, bool bValue, int option = 0);
  325.         bool AddToParameters(TPSQueryID queryID, const char* sSpec, const char* sValue, int option = 0);
  326.         bool AddToGeneration(TPSQueryID queryID, const char* sSpec, float fValue, int option = 0);
  327.         bool AddToGeneration(TPSQueryID queryID, const char* sSpec, const char* sValue, int option = 0);
  328.         bool AddToConditions(TPSQueryID queryID, const char* sSpec, float fValue, int option = 0);
  329.         bool AddToConditions(TPSQueryID queryID, const char* sSpec, bool bValue, int option = 0);
  330.         bool AddToWeights(TPSQueryID queryID, const char* sSpec, float fValue, int option = 0);
  331.  
  332.         // Test a given point if it fulfills conditions of a given query.
  333.         int TestConditions(TPSQueryID queryID, const QueryContext& context, Vec3& point, bool& bValid) const;
  334.  
  335.         // Start a new asynchronous query. Returns the id "ticket" for this query instance.
  336.         // Types needed to avoid confusion?
  337.         TPSQueryTicket AsyncQuery(TPSQueryID queryID, const QueryContext& m_context, int flags, int nPoints, ITacticalPointResultsReceiver* pReciever);
  338.  
  339.         void           UnlockResults(TPSQueryTicket queryTicket);
  340.         bool           HasLockedResults(TPSQueryTicket queryTicket) const;
  341.  
  342.         // Cancel an asynchronous query.
  343.         bool CancelAsyncQuery(TPSQueryTicket ticket);
  344.  
  345.         // ---------- ~ End of ITacticalPointSystem methods ~ ----------
  346.  
  347.         // Make a synchronous query for one point
  348.         // Returns: query option used, or -1 if there was an error or no points found
  349.         // Has no side-effects
  350.         int SyncQuery(TPSQueryID queryID, const QueryContext& context, CTacticalPoint& point);
  351.  
  352.         // Synchronous query for up to n best points in order of decreasing fitness
  353.         // Returns: query option used, or -1 if there was an error or no points found
  354.         // No side-effects
  355.         int SyncQueryShortlist(TPSQueryID queryID, const QueryContext& context, TTacticalPoints& points, int n);
  356.  
  357.         // Destroy all stored queries, usually on AI reload
  358.         void DestroyAllQueries();
  359.  
  360.         // Timesliced update within the main AI thread
  361.         // Ideally performs just housekeeping and manages the asynchronous subtasks
  362.         void Update(float fMaxTime);
  363.  
  364.         void Serialize(TSerialize ser);
  365.  
  366.         void DebugDraw() const;
  367.  
  368.         // Extend the language by adding new keywords
  369.         virtual bool ExtendQueryLanguage(const char* szName, ETacticalPointQueryType eType, ETacticalPointQueryCost eCost);
  370.  
  371.         // Language extenders
  372.         virtual bool       AddLanguageExtender(ITacticalPointLanguageExtender* pExtender);
  373.         virtual bool       RemoveLanguageExtender(ITacticalPointLanguageExtender* pExtender);
  374.         virtual IAIObject* CreateExtenderDummyObject(const char* szDummyName);
  375.         virtual void       ReleaseExtenderDummyObject(tAIObjectID id);
  376.  
  377. private:
  378.         struct SQueryInstance
  379.         {
  380.                 TPSQueryTicket                 nQueryInstanceID;
  381.                 TPSQueryID                     nQueryID;
  382.                 QueryContext                   queryContext;
  383.                 int                            flags;
  384.                 int                            nPoints;
  385.                 ITacticalPointResultsReceiver* pReceiver;
  386.  
  387.                 // Performance monitoring, for latency and throughput
  388.                 CTimeValue timeRequested;
  389.                 int        nFramesProcessed;
  390.                 SQueryInstance()
  391.                 {
  392.                         flags = 0;
  393.                         nPoints = 0;
  394.                         pReceiver = NULL;
  395.                         nFramesProcessed = 0;
  396.                 }
  397.         };
  398.  
  399.         // Struct used to represent points in the process of being evaluated
  400.         // Essentially storing the low level progress of a particular query option
  401.         // The extra state may be discarded after evaluation or preserved for debugging
  402.         // This structure could be shrunk - but shrink the CTacticalPoint structure first
  403.         struct SPointEvaluation
  404.         {
  405.                 // State of a TPS point in the evaluation pipeline
  406.                 // Note that a point is considered partial initially
  407.                 enum EPointEvaluationState
  408.                 {
  409.                         eEmpty,                                              // An invalid instance. Just present for convenience
  410.                         eRejected,                                           // Point failed one of the conditions
  411.                         ePartial,                                            // Point conditions are not fully evaluated
  412.                         eValid,                                              // Point is valid but not one of the n best
  413.                         eAccepted,                                           // Point was accepted as one of the n best
  414.                         eBest,                                               // Point was the best found
  415.                 } m_eEvalState;
  416.  
  417.                 // [0, 1] Track the min and max scores this point can potentially attain. These converge to precise result.
  418.                 float          m_fMin, m_fMax;
  419.  
  420.                 int            m_iQueryIndex;                          // The index of the next criterion to be evaluated on this point
  421.                 float          m_fSizeModifier;                        // Size of debug sphere. E.g. modify if known to be inside obstacle, for visibility
  422.                 CTacticalPoint m_Point;                                // The point being considered
  423.  
  424.                 SPointEvaluation(const CTacticalPoint& point, float fMin, float fMax, EPointEvaluationState eEvalState)
  425.                         : m_eEvalState(eEvalState)
  426.                         , m_fMin(fMin)
  427.                         , m_fMax(fMax)
  428.                         , m_iQueryIndex(0)
  429.                         , m_fSizeModifier(1.0f)
  430.                         , m_Point(point)
  431.                 {}
  432.  
  433.                 SPointEvaluation()
  434.                         : m_eEvalState(eEmpty)
  435.                         , m_fMin(-100.0f)
  436.                         , m_fMax(-100.0f)
  437.                         , m_iQueryIndex(-1)
  438.                         , m_fSizeModifier(1.0f)
  439.                 {}
  440.  
  441.                 // Used for heapsort. We always sort so that the highest _potential_ score comes to the top of the heap
  442.                 // Note that in the STL heap structure it is the largest element that resides in position 0
  443.                 bool operator<(const SPointEvaluation& that) const { return (m_fMax < that.m_fMax); }
  444.         };
  445.  
  446.         // The progress of a particular query instance at a high level
  447.         struct SQueryEvaluation
  448.         {
  449.                 // Describing our state machine. Note that we loop through a series of states for each option.
  450.                 // This could even have a couple more states, if generation/setup prove costly
  451.                 enum QueryEvaluationState
  452.                 {
  453.                         eEmpty,                                              // An invalid instance. Just present for convenience
  454.                         eReady,                                              // Moved out of waiting queue, ready to begin processing
  455.                         eInitialized,                                        // Generation complete, criterion have been sorted, points transformed into SPointEvaluations
  456.                         eHeapEvaluation,                                     // We've heapified and are processing the heap
  457.                         eWaitingForDeferred,                                 // We're waiting for deferred conditions to complete
  458.                         eCompletedOption,                                    // An option completed but returned no results - must initialise the next
  459.                         eCompleted,                                          // We are ready to return results from an option, or that all options failed
  460.                         eDebugging,                                          // Have returned a result, currently preserved for debugging
  461.                         eError,                                              // Something went wrong - aborting with no results
  462.                 }   eEvalState;
  463.                 int iCurrentQueryOption;                               // Whether we are currently evaluating the first option, the first fallback, the next, etc.
  464.  
  465.                 typedef std::vector<CCriterion> Criteria;
  466.                 Criteria                                            vCheapConds;
  467.                 Criteria                                            vExpConds;
  468.                 Criteria                                            vDefConds;
  469.                 Criteria                                            vCheapWeights;
  470.                 Criteria                                            vExpWeights;
  471.  
  472.                 PostureManager::PostureQueryID                      postureQueryID;
  473.  
  474.                 QueuedRayID                                         visibleRayID;
  475.                 int                                                 visibleResult;
  476.  
  477.                 QueuedRayID                                         canShootRayID;
  478.                 int                                                 canShootResult;
  479.  
  480.                 QueuedRayID                                         canShootSecondRayID;
  481.                 int                                                 canShootSecondRayResult;
  482.  
  483.                 ITacticalPointLanguageExtender::TDeferredCancelFunc deferredExtenderCancelFunc;
  484.  
  485.                 std::vector<SPointEvaluation>                       vPoints;
  486.  
  487.                 SQueryInstance                                      queryInstance; // Defines details of the query request
  488.                 int                                                 nFoundBestN;   // Number of best points found so far
  489.  
  490.                 EntityId                                            owner;
  491.  
  492.                 // Used for debugging
  493.                 bool       bPersistent;
  494.                 CTimeValue timePlaced, timeErase;
  495.  
  496.                 SQueryEvaluation();
  497.  
  498.                 ~SQueryEvaluation();
  499.  
  500.                 // Avoid using this on non-empty instances due to std::vector copy
  501.                 SQueryEvaluation(const SQueryEvaluation& that)
  502.                 {
  503.                         *this = that;
  504.                 }
  505.  
  506.                 // Avoid using this on non-empty instances due to std::vector copy
  507.                 void operator=(const SQueryEvaluation& that)
  508.                 {
  509.                         eEvalState = that.eEvalState;
  510.                         iCurrentQueryOption = that.iCurrentQueryOption;
  511.                         vCheapConds = that.vCheapConds;
  512.                         vExpConds = that.vExpConds;
  513.                         vCheapWeights = that.vCheapWeights;
  514.                         vExpWeights = that.vExpWeights;
  515.                         vPoints = that.vPoints;
  516.                         indexHeapEndRejectedBegin = that.indexHeapEndRejectedBegin;
  517.                         indexRejectedEndAcceptedBegin = that.indexRejectedEndAcceptedBegin;
  518.                         queryInstance = that.queryInstance;
  519.                         nFoundBestN = that.nFoundBestN;
  520.                         owner = that.owner;
  521.                         bPersistent = that.bPersistent;
  522.                         timePlaced = that.timePlaced;
  523.                         timeErase = that.timeErase;
  524.                         postureQueryID = that.postureQueryID;
  525.                         visibleRayID = that.visibleRayID;
  526.                         visibleResult = that.visibleResult;
  527.                         canShootRayID = that.canShootRayID;
  528.                         canShootResult = that.canShootResult;
  529.                         canShootSecondRayID = that.canShootSecondRayID;
  530.                         canShootSecondRayResult = that.canShootSecondRayResult;
  531.                         deferredExtenderCancelFunc = that.deferredExtenderCancelFunc;
  532.                         if (that.deferredExtenderCancelFunc)
  533.                         {
  534.                                 CryFatalError("Can't make a copy of a deferredExtenderCancelFunc - it will lead to a double call - this message should never happen.");
  535.                         }
  536.                 }
  537.  
  538.                 void Reset()
  539.                 {
  540.                         eEvalState = eEmpty;
  541.                         iCurrentQueryOption = 0;
  542.                         vCheapConds.clear();
  543.                         vCheapWeights.clear();
  544.                         vExpConds.clear();
  545.                         vExpWeights.clear();
  546.                         vPoints.clear();
  547.                         indexHeapEndRejectedBegin = 0;
  548.                         indexRejectedEndAcceptedBegin = 0;
  549.                         queryInstance = SQueryInstance();
  550.                         nFoundBestN = 0;
  551.                         owner = 0;
  552.                         bPersistent = false;
  553.                         timePlaced.SetValue(0);
  554.                         timeErase.SetValue(0);
  555.                         postureQueryID = 0;
  556.  
  557.                         visibleRayID = 0;
  558.                         visibleResult = -1;
  559.                         canShootRayID = 0;
  560.                         canShootResult = -1;
  561.                         canShootSecondRayID = 0;
  562.                         canShootSecondRayResult = -1;
  563.                         deferredExtenderCancelFunc = ITacticalPointLanguageExtender::TDeferredCancelFunc();
  564.                 }
  565.  
  566.                 const std::vector<SPointEvaluation>::iterator GetIterHeapEndRejectedBegin(void)
  567.                 {
  568.                         return vPoints.begin() + indexHeapEndRejectedBegin;
  569.                 }
  570.  
  571.                 const std::vector<SPointEvaluation>::iterator GetIterRejectedEndAcceptedBegin(void)
  572.                 {
  573.                         return vPoints.begin() + indexRejectedEndAcceptedBegin;
  574.                 }
  575.  
  576.                 void SetIterRejectedEndAcceptedBegin(const std::vector<SPointEvaluation>::iterator& it)
  577.                 {
  578.                         indexRejectedEndAcceptedBegin = std::distance(vPoints.begin(), it);
  579.                         assert(indexRejectedEndAcceptedBegin <= vPoints.size());
  580.                 }
  581.  
  582.                 void SetIterHeapEndRejectedBegin(const std::vector<SPointEvaluation>::iterator& it)
  583.                 {
  584.                         indexHeapEndRejectedBegin = std::distance(vPoints.begin(), it);
  585.                         assert(indexHeapEndRejectedBegin <= vPoints.size());
  586.                 }
  587.  
  588.         private:
  589.                 // Indices delineating ranges of the heap
  590.                 // Stored in this form to be safe for copying, but converted ot iterators for use.
  591.                 size_t indexHeapEndRejectedBegin;     // Defining the end of the heap range within the vector
  592.                 size_t indexRejectedEndAcceptedBegin; // Defining the beginning of the rejected points range within the vector
  593.         };
  594.  
  595.         CTacticalPointSystem();
  596.         ~CTacticalPointSystem();
  597.  
  598.         bool ApplyCost(uint32 uQueryId, ETacticalPointQueryCost eCost);
  599.         int  GetCost(uint32 uQueryId) const;
  600.  
  601.         bool VerifyQueryInstance(const SQueryInstance& instance) const;
  602.  
  603.         // Generates a vector of points based on a vector of generation criteria and any option criteria, based on the given actor
  604.         bool GeneratePoints(const std::vector<CCriterion>& vGeneration, const QueryContext& context, const COptionCriteria* pOption, std::vector<CTacticalPoint>& vPoints) const;
  605.  
  606.         // Take a query instance (request) and convert it into an evaluation structure
  607.         // Moves structure into m_eEvalState == eHeapEvaluation (we return true), or eError (we return false)
  608.         // Note we skip states currently
  609.         // Usually starts from eReady...
  610.         bool SetupQueryEvaluation(const SQueryInstance& instance, SQueryEvaluation& evaluation) const;
  611.  
  612.         // Continue evaluation of a query, whatever stage it might be at
  613.         // Structure should be m_eEvalState == {eInitialized, eHeapEvaluation, eCompletedOption }
  614.         // Moves structure to {eHeapEvaluation, eCompletedOption, eCompleted}
  615.         bool ContinueQueryEvaluation(SQueryEvaluation& evaluation, CTimeValue timeLimit) const;
  616.  
  617.         // For a query option, set up evaluation of points, including generation and heapifying
  618.         // -----------Structure should be m_eEvalState == {eInitialized, eCompletedOption }
  619.         // -----------Moves structure to {eHeapEvaluation, eCompletedOption, eCompleted}
  620.         bool SetupHeapEvaluation(const std::vector<CCriterion>& vConditions, const std::vector<CCriterion>& vWeights, const QueryContext& context, const std::vector<CTacticalPoint>& vPoints, int n, SQueryEvaluation& evaluation) const;
  621.  
  622.         // Takes a vector of points and vectors of conditions and weights to evaluate them with, upon the given actor
  623.         // Returns up to n valid points in the same vector
  624.         // Structure should be m_eEvalState == {eInitialized, eHeapEvaluation}
  625.         bool ContinueHeapEvaluation(SQueryEvaluation& eval, CTimeValue timeLimit) const;
  626.  
  627.         // Callback with results from a completed async query
  628.         void CallbackQuery(SQueryEvaluation& evaluation);
  629.  
  630.         // Test a single point against a single criterion, given an actor
  631.         bool       Test(const CCriterion& criterion, const CTacticalPoint& point, const QueryContext& context, bool& result) const;
  632.         // Test a single point against a single criterion, given an actor
  633.         AsyncState DeferredTest(const CCriterion& criterion, const CTacticalPoint& point, SQueryEvaluation& eval, bool& result) const;
  634.         // Calculate a single weighting criterion for a single point, given an actor
  635.         bool       Weight(const CCriterion& criterion, const CTacticalPoint& point, const QueryContext& context, float& result) const;
  636.         // Generate a set of points given a single criterion and an actor and any option criteria
  637.         bool       Generate(const CCriterion& criterion, const QueryContext& context, const COptionCriteria* pOption, TTacticalPoints& accumulator) const;
  638.         bool       GenerateInternal(TTacticalPointQuery query, const QueryContext& context, float fSearchDist, const COptionCriteria* pOption,
  639.                                     CAIObject* pObject, const Vec3& vObjectPos, CAIObject* pObjectAux, const Vec3& vObjectAuxPos, TTacticalPoints& accumulator) const;
  640.  
  641.         // If a query is based on a Boolean Property we gain the core bool result here before processing
  642.         bool                        BoolProperty(TTacticalPointQuery query, const CTacticalPoint& point, const QueryContext& context, bool& result) const;
  643.         bool                        BoolPropertyInternal(TTacticalPointQuery query, const CTacticalPoint& point, const QueryContext& context, bool& result) const;
  644.         // If a query is based on a Boolean Test we gain the core bool result here before processing
  645.         bool                        BoolTest(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, const QueryContext& context, bool& result) const;
  646.         bool                        BoolTestInternal(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, const QueryContext& context, bool& result) const;
  647.         AsyncState                  DeferredBoolTest(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, SQueryEvaluation& eval, bool& result) const;
  648.         ETacticalPointDeferredState DeferredBoolTestInternal(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, SQueryEvaluation& eval, bool& result) const;
  649.  
  650.         // If a query is based on a Real Property we gain the core _absolute_ float result here before processing
  651.         bool RealProperty(TTacticalPointQuery query, const CTacticalPoint& point, const QueryContext& context, float& result) const;
  652.         bool RealPropertyInternal(TTacticalPointQuery query, const CTacticalPoint& point, const QueryContext& context, float& result) const;
  653.         // If a query is based on a Real Measure we gain the core _absolute_ float result here before processing
  654.         bool RealMeasure(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, const QueryContext& context, float& result) const;
  655.         bool RealMeasureInternal(TTacticalPointQuery query, TTacticalPointQuery object, const CTacticalPoint& point, const QueryContext& context, float& result) const;
  656.  
  657.         // Obtain the absolute range of any given _real_ query,
  658.         // in the context of this complete query option, but independent of any given point
  659.         bool RealRange(TTacticalPointQuery query, float& min, float& max) const;
  660.         bool RealRangeInternal(TTacticalPointQuery query, float& min, float& max) const;
  661.  
  662.         // Apply limits to an absolute float result. This is trivial - in break from convention we assume it cannot fail.
  663.         inline bool Limit(TTacticalPointQuery limit, float fAbsoluteQueryResult, float fComparisonValue) const;
  664.         // Fetch object as a CAIObject. This might or might not later be moved into the Test and Weight methods?
  665.         inline bool GetObject(TTacticalPointQuery object, const QueryContext& context, CAIObject*& pObject, Vec3& vObjPos) const;
  666.         inline bool GetObjectInternal(TTacticalPointQuery object, const QueryContext& context, CAIObject*& pObject, Vec3& vObjPos) const;
  667.         inline Vec3 GetObjectInternalDir(TTacticalPointQuery object, const QueryContext& context) const;
  668.  
  669.         bool        Parse(const char* sSpec, TTacticalPointQuery& query, TTacticalPointQuery& limits,
  670.                           TTacticalPointQuery& object, TTacticalPointQuery& objectAux) const;
  671.         bool        Unparse(const CCriterion& criterion, string& description) const;
  672.         bool        CheckToken(TTacticalPointQuery token) const;
  673.  
  674.         // Our own concept of "hostile" target
  675.         bool IsHostile(EntityId entityId1, EntityId entityId2) const;
  676.  
  677.         // Convert a single word into a single TPS token - or eTPQ_None if unrecognised
  678.         TTacticalPointQuery Translate(const char* sWord) const;
  679.         // Convert a single TPS token into a single word - or NULL if unrecognised
  680.         const char*         Translate(TTacticalPointQuery etpToken) const;
  681.  
  682.         // Convert a single word into a single TPS parameter token - or eTPQP_None if unrecognised
  683.         ETacticalPointQueryParameter TranslateParam(const char* sWord) const;
  684.         ETPSRelativeValueSource      TranslateRelativeValueSource(const char* sWord) const;
  685.  
  686.         // bit of duplicaiton here...?
  687.         const CTacticalPointQuery* GetQuery(TPSQueryID nQueryID) const;
  688.         COptionCriteria*           GetQueryOptionPointer(TPSQueryID queryID, int options);
  689.  
  690.         // Generate a warning message, giving as many standard details of the context as possible
  691.         void TPSDescriptionWarning(const CTacticalPointQuery& query, const QueryContext& context, const COptionCriteria* pOption, const char* sMessage = NULL) const;
  692.  
  693. #ifdef CRYAISYSTEM_DEBUG
  694.         void AddQueryForDebug(SQueryEvaluation& evaluation);
  695. #endif
  696.  
  697.         static CTacticalPointSystem*                   s_pTPS;   // The singleton
  698.  
  699.         mutable string                                 sBuffer;  // Buffer used in string lookup
  700.  
  701.         std::map<string, TTacticalPointQuery>          m_mStringToToken; // Map of strings to their tokens
  702.         std::map<TTacticalPointQuery, string>          m_mTokenToString; // Map of tokens to their strings
  703.  
  704.         std::map<string, ETacticalPointQueryParameter> m_mParamStringToToken;               // Mapping of string parameters to enum values
  705.         std::map<string, ETPSRelativeValueSource>      m_mRelativeValueSourceStringToToken; // Mapping of string RelativeValueSource descriptions to enum values
  706.  
  707.         std::map<TTacticalPointQuery, int>             m_mIDToCost; // Map of strings to query costs
  708.         int m_CostMap[eTPQC_COUNT];
  709.         int m_GameQueryIdMap[eTPQT_COUNT];
  710.  
  711.         typedef std::vector<ITacticalPointLanguageExtender*> TLanguageExtenders;
  712.         TLanguageExtenders m_LanguageExtenders;
  713.  
  714.         typedef CCountedRef<CAIObject> TDummyRef;
  715.         typedef std::vector<TDummyRef> TDummyObjects;
  716.         TDummyObjects  m_LanguageExtenderDummyObjects;
  717.  
  718.         TPSQueryTicket m_nQueryInstanceTicket;
  719.  
  720.         std::map<TPSQueryTicket, const SQueryInstance> m_mQueryInstanceQueue;
  721.         std::map<TPSQueryTicket, SQueryEvaluation>     m_mQueryEvaluationsInProgress;
  722.  
  723. #ifdef CRYAISYSTEM_DEBUG
  724.         std::list<SQueryEvaluation> m_lstQueryEvaluationsForDebug;
  725. #endif
  726.  
  727.         TPSQueryID                                m_LastQueryID; // ID of last query created
  728.         std::map<TPSQueryID, CTacticalPointQuery> m_mIDToQuery;  // Map of IDs to stored queries
  729.         std::map<string, TPSQueryID>              m_mNameToID;   // Map of strings to query IDs
  730.  
  731.         typedef std::map<TPSQueryTicket, TTacticalPoints> TLockedPoints;
  732.         TLockedPoints                             m_locked;
  733.  
  734.         mutable std::vector<Vec3>                 m_occupiedSpots;
  735.         mutable std::vector<STacticalPointResult> m_points;
  736.  
  737.         struct AvoidCircle
  738.         {
  739.                 Vec3  pos;
  740.                 float radius;
  741.         };
  742.  
  743.         struct CVarsDef
  744.         {
  745.                 int   DebugTacticalPoints;
  746.                 int   DebugTacticalPointsBlocked;
  747.                 int   TacticalPointsDebugDrawMode;
  748.                 int   TacticalPointsDebugFadeMode;
  749.                 float TacticalPointsDebugScaling;
  750.                 float TacticalPointsDebugTime;
  751.                 int   TacticalPointsWarnings;
  752.         };
  753.  
  754.         typedef std::vector<AvoidCircle> AvoidCircles;
  755.         mutable AvoidCircles                  m_avoidCircles;
  756.         void GatherAvoidCircles(const Vec3& center, float range, CAIObject* requester, AvoidCircles& avoidCircles) const;
  757.         mutable CCoverSystem::CoverCollection m_cover;
  758.         void GatherCoverEyes();
  759.  
  760.         void VisibleRayComplete(const QueuedRayID& rayID, const RayCastResult& result);
  761.         void CanShootRayComplete(const QueuedRayID& rayID, const RayCastResult& result);
  762.         void CanShootSecondRayComplete(const QueuedRayID& rayID, const RayCastResult& result);
  763.  
  764.         // CVars
  765.         static CVarsDef CVars;
  766. };
  767.  
  768. #endif //__TacticalPointSystem_H__
  769.  
downloadTacticalPointSystem.h 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