AIMovementCostRule.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * AIMovementCostRule.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 "AIMovementCostRule.h"
  12. namespace AIPathfinding
  13. {
  14. AIMovementCostRule::AIMovementCostRule(std::shared_ptr<AINodeStorage> nodeStorage)
  15. : nodeStorage(nodeStorage)
  16. {
  17. }
  18. void AIMovementCostRule::process(
  19. const PathNodeInfo & source,
  20. CDestinationNodeInfo & destination,
  21. const PathfinderConfig * pathfinderConfig,
  22. CPathfinderHelper * pathfinderHelper) const
  23. {
  24. auto srcNode = nodeStorage->getAINode(source.node);
  25. auto dstNode = nodeStorage->getAINode(destination.node);
  26. auto srcActor = srcNode->actor;
  27. auto dstActor = dstNode->actor;
  28. if(srcActor == dstActor)
  29. {
  30. MovementCostRule::process(source, destination, pathfinderConfig, pathfinderHelper);
  31. return;
  32. }
  33. auto carrierActor = dstActor->carrierParent;
  34. auto otherActor = dstActor->otherParent;
  35. if(source.coord == destination.coord)
  36. {
  37. auto carrierNode = nodeStorage->getOrCreateNode(source.coord, source.node->layer, carrierActor).get();
  38. auto otherNode = nodeStorage->getOrCreateNode(source.coord, source.node->layer, otherActor).get();
  39. if(carrierNode->turns >= otherNode->turns)
  40. {
  41. destination.turn = carrierNode->turns;
  42. destination.cost = carrierNode->cost;
  43. return;
  44. }
  45. double waitingCost = otherNode->turns - carrierNode->turns - 1.0
  46. + carrierNode->moveRemains / (double)pathfinderHelper->getMaxMovePoints(carrierNode->layer);
  47. destination.turn = otherNode->turns;
  48. destination.cost = waitingCost;
  49. }
  50. else
  51. {
  52. // TODO: exchange through sail->land border might be more sofisticated
  53. destination.blocked = true;
  54. }
  55. }
  56. }