AICover.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "AICover.h"
  23. #include "AIController.h"
  24. struct CoverSearch
  25. {
  26. Point3F loc;
  27. Point3F from;
  28. F32 dist;
  29. F32 best;
  30. CoverPoint* point;
  31. CoverSearch() : loc(0, 0, 0), from(0, 0, 0)
  32. {
  33. best = -FLT_MAX;
  34. point = NULL;
  35. dist = FLT_MAX;
  36. }
  37. };
  38. static void findCoverCallback(SceneObject* obj, void* key)
  39. {
  40. CoverPoint* p = dynamic_cast<CoverPoint*>(obj);
  41. if (!p || p->isOccupied())
  42. return;
  43. CoverSearch* s = static_cast<CoverSearch*>(key);
  44. Point3F dir = s->from - p->getPosition();
  45. dir.normalizeSafe();
  46. // Score first based on angle of cover point to enemy.
  47. F32 score = mDot(p->getNormal(), dir);
  48. // Score also based on distance from seeker.
  49. score -= (p->getPosition() - s->loc).len() / s->dist;
  50. // Finally, consider cover size.
  51. score += (p->getSize() + 1) / CoverPoint::NumSizes;
  52. score *= p->getQuality();
  53. if (score > s->best)
  54. {
  55. s->best = score;
  56. s->point = p;
  57. }
  58. }
  59. bool AIController::findCover(const Point3F& from, F32 radius)
  60. {
  61. if (radius <= 0)
  62. return false;
  63. // Create a search state.
  64. CoverSearch s;
  65. s.loc = getAIInfo()->getPosition();
  66. s.dist = radius;
  67. // Direction we seek cover FROM.
  68. s.from = from;
  69. // Find cover points.
  70. Box3F box(radius * 2.0f);
  71. box.setCenter(getAIInfo()->getPosition());
  72. getAIInfo()->mObj->getContainer()->findObjects(box, MarkerObjectType, findCoverCallback, &s);
  73. // Go to cover!
  74. if (s.point)
  75. {
  76. // Calling setPathDestination clears cover...
  77. bool foundPath = getNav()->setPathDestination(s.point->getPosition());
  78. setCover(s.point);
  79. s.point->setOccupied(true);
  80. return foundPath;
  81. }
  82. return false;
  83. }
  84. DefineEngineMethod(AIController, findCover, S32, (Point3F from, F32 radius), ,
  85. "@brief Tells the AI to find cover nearby.\n\n"
  86. "@param from Location to find cover from (i.e., enemy position).\n"
  87. "@param radius Distance to search for cover.\n"
  88. "@return Cover point ID if cover was found, -1 otherwise.\n\n")
  89. {
  90. if (object->findCover(from, radius))
  91. {
  92. CoverPoint* cover = object->getCover()->mCoverPoint.getObject();
  93. return cover ? cover->getId() : -1;
  94. }
  95. else
  96. {
  97. return -1;
  98. }
  99. }