GraphPaths.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * GraphPaths.h, 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. #pragma once
  11. #include "ObjectGraph.h"
  12. namespace NKAI
  13. {
  14. class Nullkiller;
  15. struct GraphPathNode;
  16. enum GrapthPathNodeType
  17. {
  18. NORMAL,
  19. BATTLE,
  20. LAST
  21. };
  22. struct GraphPathNodePointer
  23. {
  24. int3 coord = int3(-1);
  25. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  26. GraphPathNodePointer() = default;
  27. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  28. :coord(coord), nodeType(type)
  29. { }
  30. bool valid() const
  31. {
  32. return coord.isValid();
  33. }
  34. };
  35. using GraphNodeStorage = std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]>;
  36. class GraphNodeComparer
  37. {
  38. const GraphNodeStorage & pathNodes;
  39. public:
  40. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  41. :pathNodes(pathNodes)
  42. {
  43. }
  44. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  45. };
  46. struct GraphPathNode
  47. {
  48. const float BAD_COST = 100000;
  49. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  50. GraphPathNodePointer previous;
  51. float cost = BAD_COST;
  52. uint64_t linkDanger = 0;
  53. const CGObjectInstance * obj = nullptr;
  54. std::shared_ptr<SpecialAction> specialAction;
  55. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  56. TFibHeap::handle_type handle;
  57. bool isInQueue = false;
  58. bool reachable() const
  59. {
  60. return cost < BAD_COST;
  61. }
  62. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  63. };
  64. class GraphPaths
  65. {
  66. ObjectGraph graph;
  67. GraphNodeStorage pathNodes;
  68. std::string visualKey;
  69. public:
  70. GraphPaths();
  71. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai, uint8_t scanDepth);
  72. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  73. void quickAddChainInfoWithBlocker(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  74. void dumpToLog() const;
  75. private:
  76. GraphPathNode & getOrCreateNode(const GraphPathNodePointer & pos)
  77. {
  78. auto & node = pathNodes[pos.coord][pos.nodeType];
  79. node.nodeType = pos.nodeType;
  80. return node;
  81. }
  82. const GraphPathNode & getNode(const GraphPathNodePointer & pos) const
  83. {
  84. auto & node = pathNodes.at(pos.coord)[pos.nodeType];
  85. return node;
  86. }
  87. };
  88. }