AIMovementToDestinationRule.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * AIMovementToDestinationRule.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "AIMovementToDestinationRule.h"
  12. namespace NK2AI
  13. {
  14. namespace AIPathfinding
  15. {
  16. AIMovementToDestinationRule::AIMovementToDestinationRule(const std::shared_ptr<AINodeStorage> & nodeStorage, const bool allowBypassObjects, CCallback & cc)
  17. : nodeStorage(nodeStorage), allowBypassObjects(allowBypassObjects), cc(cc)
  18. {
  19. }
  20. void AIMovementToDestinationRule::process(
  21. const PathNodeInfo & source,
  22. CDestinationNodeInfo & destination,
  23. const PathfinderConfig * pathfinderConfig,
  24. CPathfinderHelper * pathfinderHelper
  25. ) const
  26. {
  27. auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
  28. if(blocker == BlockingReason::NONE)
  29. return;
  30. if(blocker == BlockingReason::DESTINATION_BLOCKED && destination.action == EPathNodeAction::EMBARK
  31. && nodeStorage->getAINode(destination.node)->specialAction)
  32. {
  33. return;
  34. }
  35. if(blocker == BlockingReason::SOURCE_GUARDED)
  36. {
  37. auto actor = nodeStorage->getAINode(source.node)->actor;
  38. if(!allowBypassObjects)
  39. {
  40. if(source.node->getCost() < 0.0001f)
  41. return;
  42. // when actor represents moster graph node, we need to let him escape monster
  43. if(cc.getGuardingCreaturePosition(source.coord) == actor->initialPosition)
  44. return;
  45. }
  46. if(actor->allowBattle)
  47. {
  48. #if NK2AI_PATHFINDER_TRACE_LEVEL >= 1
  49. logAi->trace("Bypass src guard while moving from %s to %s", source.coord.toString(), destination.coord.toString());
  50. #endif
  51. return;
  52. }
  53. }
  54. destination.blocked = true;
  55. }
  56. }
  57. }