BVB Source Codes

CRYENGINE Show TacticalPointQuery.cpp Source code

Return Download CRYENGINE: download TacticalPointQuery.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 "TacticalPointQuery.h"
  5. #include "TacticalPointQueryEnum.h"
  6. #include "TacticalPointSystem.h"
  7.  
  8. #include "Navigation/NavigationSystem/NavigationSystem.h"
  9.  
  10. using std::vector;
  11.  
  12. //------ CCriterion -------------------------------------------------------------------------------//
  13.  
  14. CCriterion::CCriterion()
  15. {
  16.         m_eQuery = eTPQ_None;
  17.         m_eLimits = eTPQ_None;
  18.         m_eObject = eTPQ_None;
  19.         m_eObjectAux = eTPQ_None;
  20.         m_nRelativeValueSource = eTPSRVS_Invalid;
  21. }
  22.  
  23. bool CCriterion::IsValid() const
  24. {
  25.         CTacticalPointSystem* pTPS = gAIEnv.pTacticalPointSystem;
  26.  
  27.         // Check the necessary tokens all exist and that we haven't corrupted them in any way
  28.         if (!pTPS->CheckToken(GetQuery())) return false;
  29.  
  30.         if (GetLimits())
  31.         {
  32.                 if (!pTPS->CheckToken(GetLimits())) return false;
  33.                 if (!(GetQuery() & (eTPQ_FLAG_PROP_REAL | eTPQ_FLAG_MEASURE))) return false;
  34.         }
  35.  
  36.         if (GetObject())
  37.         {
  38.                 if (!pTPS->CheckToken(GetObject())) return false;
  39.                 if (!(GetQuery() & (eTPQ_FLAG_TEST | eTPQ_FLAG_MEASURE | eTPQ_FLAG_GENERATOR | eTPQ_FLAG_GENERATOR_O))) return false;
  40.         }
  41.         else
  42.         {
  43.                 if (!(GetQuery() & (eTPQ_FLAG_PROP_BOOL | eTPQ_FLAG_PROP_REAL))) return false;
  44.         }
  45.  
  46.         if (GetObjectAux())
  47.         {
  48.                 if (!pTPS->CheckToken(GetObjectAux())) return false;
  49.                 if (!(GetQuery() & eTPQ_FLAG_GENERATOR_O)) return false;
  50.         }
  51.         else
  52.         {
  53.                 if (GetQuery() & (eTPQ_FLAG_GENERATOR_O)) return false;    // Generator_O is a special case
  54.         }
  55.  
  56.         return true;
  57. }
  58.  
  59. //------ COptionCriteria --------------------------------------------------------------------------//
  60.  
  61. bool COptionCriteria::AddToParameters(const char* sSpec, float fValue)
  62. {
  63.         bool bRet = true;
  64.  
  65.         // get proper param index
  66.         ETacticalPointQueryParameter nParam = gAIEnv.pTacticalPointSystem->TranslateParam(sSpec);
  67.  
  68.         // if its invalid - report it
  69.         if (nParam == eTPQP_Invalid)
  70.         {
  71.                 bRet = false;
  72.  
  73.                 AIWarningID("<TacticalPointSystem> ", "AddToParameters - Parameter missing: \"%s\"", sSpec);
  74.         }
  75.         else
  76.         {
  77.                 switch (nParam)
  78.                 {
  79.                 case eTPQP_ObjectsType:
  80.                         m_params.iObjectsType = (int)fValue;
  81.                         break;
  82.                 case eTPQP_Density:
  83.                         m_params.fDensity = fValue;
  84.                         break;
  85.                 case eTPQP_Height:
  86.                         m_params.fHeight = fValue;
  87.                         break;
  88.                 case eTPQP_HorizontalSpacing:
  89.                         m_params.fHorizontalSpacing = fValue;
  90.                         break;
  91.                 default:
  92.                         AIWarningID("<TacticalPointSystem> ", "AddToParameters - Unhandled parameter (code bug) \"%s\"", sSpec);
  93.                         break;
  94.                 }
  95.         }
  96.  
  97.         return bRet;
  98. }
  99.  
  100. bool COptionCriteria::AddToParameters(const char* sSpec, bool bValue)
  101. {
  102.         bool bRet = true;
  103.  
  104.         // get proper param index
  105.         ETacticalPointQueryParameter nParam = gAIEnv.pTacticalPointSystem->TranslateParam(sSpec);
  106.  
  107.         // if its invalid - report it
  108.         if (nParam == eTPQP_Invalid)
  109.         {
  110.                 bRet = false;
  111.  
  112.                 AIWarningID("<TacticalPointSystem> ", "AddToParameters - Parameter missing: \"%s\"", sSpec);
  113.         }
  114.         else
  115.         {
  116.                 //              switch(nParam)
  117.                 //              {
  118.                 //              default:
  119.                 AIWarningID("<TacticalPointSystem> ", "AddToParameters - Unhandled parameter (code bug) \"%s\"", sSpec);
  120.                 //                      break;
  121.                 //              }
  122.         }
  123.  
  124.         return bRet;
  125. }
  126.  
  127. bool COptionCriteria::AddToParameters(const char* sSpec, const char* sValue)
  128. {
  129.         bool bRet = true;
  130.  
  131.         // get proper param index
  132.         ETacticalPointQueryParameter nParam = gAIEnv.pTacticalPointSystem->TranslateParam(sSpec);
  133.  
  134.         // if its invalid - report it
  135.         if (nParam == eTPQP_Invalid)
  136.         {
  137.                 bRet = false;
  138.  
  139.                 AIWarningID("<TacticalPointSystem> ", "AddToParameters - Parameter missing: \"%s\"", sSpec);
  140.         }
  141.         else
  142.         {
  143.                 switch (nParam)
  144.                 {
  145.                 case eTPQP_OptionLabel:
  146.                         m_params.sSignalToSend = sValue;
  147.                         break;
  148.                 case eTPQP_TagPointPostfix:
  149.                         m_params.tagPointPostfix = sValue;
  150.                         break;
  151.                 case eTPQP_ExtenderStringParameter:
  152.                         m_params.extenderStringParamenter = sValue;
  153.                         break;
  154.                 case eTPQP_NavigationAgentType:
  155.                         m_params.sNavigationAgentType = sValue;
  156.                         if (gAIEnv.pNavigationSystem != NULL)
  157.                         {
  158.                                 if (gAIEnv.pNavigationSystem->GetAgentTypeID(m_params.sNavigationAgentType) == NavigationAgentTypeID())
  159.                                 {
  160.                                         AIWarningID("<TacticalPointSystem> ", "AddToParameters - NavigationAgentType '%s' is invalid! \"%s\"", m_params.sNavigationAgentType.c_str(), sSpec);
  161.                                 }
  162.                         }
  163.                         break;
  164.                 default:
  165.                         AIWarningID("<TacticalPointSystem> ", "AddToParameters - Unhandled parameter (code bug) \"%s\"", sSpec);
  166.                         break;
  167.                 }
  168.         }
  169.  
  170.         return bRet;
  171. }
  172.  
  173. bool COptionCriteria::AddToGeneration(const char* sSpec, float fValue)
  174. {
  175.         int validFlags = (eTPQ_FLAG_GENERATOR | eTPQ_FLAG_GENERATOR_O);
  176.         CCriterion criterion = GetCriterion(sSpec);
  177.  
  178.         // Check this is the right kind of query and that it is a valid one
  179.         if (!(criterion.GetQuery() & validFlags) || !criterion.IsValid())
  180.         {
  181.                 AIWarningID("<TacticalPointSystem> ", "AddToGeneration - Invalid query \"%s\"", sSpec);
  182.                 return false;
  183.         }
  184.  
  185.         // Apply the value given (generation queries only take floats or ETPSRelativeValueSource)
  186.         criterion.SetValue(fValue);
  187.  
  188.         // Add to list for this option
  189.         m_vGeneration.push_back(criterion);
  190.         return true;
  191. }
  192.  
  193. bool COptionCriteria::AddToGeneration(const char* sSpec, ETPSRelativeValueSource nSource)
  194. {
  195.         assert(sSpec != NULL && "Empty parameter passed as sSpec to COptionCriteria::AddToGeneration!");
  196.  
  197.         int validFlags = (eTPQ_FLAG_GENERATOR | eTPQ_FLAG_GENERATOR_O);
  198.         CCriterion criterion = GetCriterion(sSpec);
  199.  
  200.         // Check this is the right kind of query and that it is a valid one
  201.         if (!(criterion.GetQuery() & validFlags) || !criterion.IsValid())
  202.         {
  203.                 AIWarningID("<TacticalPointSystem> ", "AddToGeneration - Invalid query \"%s\"", sSpec);
  204.                 return false;
  205.         }
  206.  
  207.         // Apply the value given (generation queries only take floats or ETPSRelativeValueSource)
  208.         criterion.SetValue(nSource);
  209.  
  210.         // Add to list for this option
  211.         m_vGeneration.push_back(criterion);
  212.         return true;
  213. }
  214.  
  215. bool COptionCriteria::AddToConditions(const char* sSpec, bool bValue)
  216. {
  217.         // Taking a boolean value so must be a Boolean Property or a Test (query returning a bool)
  218.         int validFlags = eTPQ_FLAG_PROP_BOOL | eTPQ_FLAG_TEST;
  219.         CCriterion criterion = GetCriterion(sSpec);
  220.  
  221.         // Check this is the right kind of query and that it is a valid one
  222.         if (!(criterion.GetQuery() & validFlags) || !criterion.IsValid())
  223.         {
  224.                 AIWarningID("<TacticalPointSystem> ", "AddToConditions(bool) - Invalid query \"%s\"", sSpec);
  225.                 return false;
  226.         }
  227.  
  228.         // Furthermore, Bool Properties and Tests return bools
  229.         // Hence must _not_ include a Limit
  230.         if (criterion.GetLimits()) return false;
  231.  
  232.         // Apply the value given (generation queries only take floats)
  233.         criterion.SetValue(bValue);
  234.  
  235.         // Add to list for this option
  236.         m_vConditions.push_back(criterion);
  237.         return true;
  238. }
  239.  
  240. bool COptionCriteria::AddToConditions(const char* sSpec, float fValue)
  241. {
  242.         // Taking a float value so must be a Real Property or a Measure (query returning a float)
  243.         int validFlags = eTPQ_FLAG_PROP_REAL | eTPQ_FLAG_MEASURE;
  244.         CCriterion criterion = GetCriterion(sSpec);
  245.  
  246.         // Check this is the right kind of query and that it is a valid one
  247.         if (!(criterion.GetQuery() & validFlags) || !criterion.IsValid())
  248.         {
  249.                 AIWarningID("<TacticalPointSystem> ", "AddToConditions(float) - Invalid query \"%s\"", sSpec);
  250.                 return false;
  251.         }
  252.  
  253.         // Furthermore, Real Properties and Measures return floats and we need to know how to compare against our float
  254.         // Hence must include a Limit
  255.         if (!criterion.GetLimits()) return false;
  256.  
  257.         // Apply the value given (generation queries only take floats)
  258.         criterion.SetValue(fValue);
  259.  
  260.         // Add to list for this option
  261.         m_vConditions.push_back(criterion);
  262.         return true;
  263. }
  264.  
  265. bool COptionCriteria::AddToWeights(const char* sSpec, float fWeight)
  266. {
  267.         // Note that although weights always end up producing a normalised [0-1] result, that can come from a boolean
  268.         // Hence yes/no properties and tests are valid, as expressing a simple preference
  269.         int validFlags = eTPQ_FLAG_PROP_REAL | eTPQ_FLAG_MEASURE | eTPQ_FLAG_PROP_BOOL | eTPQ_FLAG_TEST;
  270.         CCriterion criterion = GetCriterion(sSpec);
  271.  
  272.         // Check this is the right kind of query and that it is a valid one
  273.         if (!(criterion.GetQuery() & validFlags) || !criterion.IsValid())
  274.         {
  275.                 AIWarningID("<TacticalPointSystem> ", "AddToWeights - Invalid query \"%s\"", sSpec);
  276.                 return false;
  277.         }
  278.  
  279.         // Furthermore, weights can't include comparisons (because the value is used as the relative weight!)
  280.         // Hence must _not_ include a Limit
  281.         if (criterion.GetLimits()) return false;
  282.  
  283.         // Apply the value given - which for weights, is the relative weight of this query result
  284.         criterion.SetValue(fWeight);
  285.  
  286.         // Add to list for this option
  287.         m_vWeights.push_back(criterion);
  288.         return true;
  289. }
  290.  
  291. string COptionCriteria::GetDescription() const
  292. {
  293.         vector<CCriterion>::const_iterator itC;
  294.         string sResult, sBuffer;
  295.         CTacticalPointSystem* pTPS = gAIEnv.pTacticalPointSystem;
  296.         bool bOk = true;
  297.         sResult.reserve(256);
  298.         sResult.append("Generation = { ");
  299.         for (itC = m_vGeneration.begin(); itC != m_vGeneration.end(); ++itC)
  300.         {
  301.                 bOk &= pTPS->Unparse(*itC, sBuffer);
  302.                 sResult.append(sBuffer);
  303.                 sBuffer.Format(" = %f, ", itC->GetValueAsFloat());
  304.                 sResult.append(sBuffer);
  305.         }
  306.         sResult.append(" }, ");
  307.         sResult.append("Conditions = { ");
  308.         for (itC = m_vConditions.begin(); itC != m_vConditions.end(); ++itC)
  309.         {
  310.                 bOk &= pTPS->Unparse(*itC, sBuffer);
  311.                 sResult.append(sBuffer);
  312.                 if (itC->GetLimits())
  313.                         sBuffer.Format(" = %f, ", itC->GetValueAsFloat());
  314.                 else
  315.                         sBuffer.Format(" = %s, ", itC->GetValueAsBool() ? "true" : "false");
  316.                 sResult.append(sBuffer);
  317.         }
  318.         sResult.append(" }, ");
  319.         sResult.append("Weights = { ");
  320.         for (itC = m_vWeights.begin(); itC != m_vWeights.end(); ++itC)
  321.         {
  322.                 bOk &= pTPS->Unparse(*itC, sBuffer);
  323.                 sResult.append(sBuffer);
  324.                 sBuffer.Format(" = %f, ", itC->GetValueAsFloat());
  325.                 sResult.append(sBuffer);
  326.         }
  327.         sResult.append(" }, ");
  328.  
  329.         return sResult;
  330. }
  331.  
  332. bool COptionCriteria::IsValid() const
  333. {
  334.         // This is pretty redundant but might be handy - each criteria is checked when added.
  335.         // What we _don't_ make sure of here is that each is valid for the role it is assigned.
  336.         // We could refactor that out of the code that adds them.
  337.  
  338.         vector<CCriterion>::const_iterator itC;
  339.         for (itC = m_vGeneration.begin(); itC != m_vGeneration.end(); ++itC)
  340.                 if (!itC->IsValid()) return false;
  341.         for (itC = m_vConditions.begin(); itC != m_vConditions.end(); ++itC)
  342.                 if (!itC->IsValid()) return false;
  343.         for (itC = m_vWeights.begin(); itC != m_vWeights.end(); ++itC)
  344.                 if (!itC->IsValid()) return false;
  345.  
  346.         return true;
  347. }
  348.  
  349. CCriterion COptionCriteria::GetCriterion(const char* sSpec)
  350. {
  351.         TTacticalPointQuery query, limits, object, objectAux;
  352.  
  353.         // Call central system to parse to tokens
  354.         bool bOK = gAIEnv.pTacticalPointSystem->Parse(sSpec, query, limits, object, objectAux);
  355.         if (!bOK)
  356.         {
  357.                 AIWarningID("<TacticalPointSystem> ", "Invalid criterion \"%s\"", (sSpec ? sSpec : "NULL"));
  358.                 return CCriterion();
  359.         }
  360.  
  361.         // Build as a criterion
  362.         CCriterion criterion;
  363.         criterion.SetQuery(query);
  364.         criterion.SetLimits(limits);
  365.         criterion.SetObject(object);
  366.         criterion.SetObjectAux(objectAux);
  367.  
  368.         return criterion;
  369. }
  370.  
  371. //------ CTacticalPointQuery ----------------------------------------------------------------------//
  372.  
  373. CTacticalPointQuery::CTacticalPointQuery(const char* psName) : m_sName(psName)
  374. {
  375.         // Check there are no existing queries with that name
  376.         assert(0 == gAIEnv.pTacticalPointSystem->GetQueryID(psName));
  377. }
  378.  
  379. CTacticalPointQuery::~CTacticalPointQuery()
  380. {
  381. }
  382.  
  383. void CTacticalPointQuery::AddOption(const COptionCriteria& option)
  384. {
  385.         m_vOptions.push_back(option);
  386. }
  387.  
  388. bool CTacticalPointQuery::IsValid() const
  389. {
  390.         vector<COptionCriteria>::const_iterator itO;
  391.         for (itO = m_vOptions.begin(); itO != m_vOptions.end(); ++itO)
  392.                 if (!itO->IsValid()) return false;
  393.         return true;
  394. }
  395.  
  396. const COptionCriteria* CTacticalPointQuery::GetOption(int i) const
  397. {
  398.         if (i < 0 || i >= (int)m_vOptions.size())
  399.                 return NULL;
  400.         return &(m_vOptions[i]);
  401. }
  402.  
  403. COptionCriteria* CTacticalPointQuery::GetOption(int i)
  404. {
  405.         if (i < 0 || i >= (int)m_vOptions.size())
  406.                 return NULL;
  407.         return &(m_vOptions[i]);
  408. }
  409.  
downloadTacticalPointQuery.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