ObjectGraph.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * ObjectGraph.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 "AINodeStorage.h"
  12. #include "../AIUtility.h"
  13. namespace NKAI
  14. {
  15. class Nullkiller;
  16. struct ObjectLink
  17. {
  18. float cost = 100000; // some big number
  19. uint64_t danger = 0;
  20. bool update(float newCost, uint64_t newDanger)
  21. {
  22. if(cost > newCost)
  23. {
  24. cost = newCost;
  25. danger = newDanger;
  26. return true;
  27. }
  28. return false;
  29. }
  30. };
  31. struct ObjectNode
  32. {
  33. ObjectInstanceID objID;
  34. MapObjectID objTypeID;
  35. bool objectExists;
  36. std::unordered_map<int3, ObjectLink> connections;
  37. void init(const CGObjectInstance * obj)
  38. {
  39. objectExists = true;
  40. objID = obj->id;
  41. objTypeID = obj->ID;
  42. }
  43. void initJunction()
  44. {
  45. objectExists = false;
  46. objID = ObjectInstanceID();
  47. objTypeID = Obj();
  48. }
  49. };
  50. class ObjectGraph
  51. {
  52. std::unordered_map<int3, ObjectNode> nodes;
  53. public:
  54. void updateGraph(const Nullkiller * ai);
  55. void addObject(const CGObjectInstance * obj);
  56. void registerJunction(const int3 & pos);
  57. void connectHeroes(const Nullkiller * ai);
  58. void removeObject(const CGObjectInstance * obj);
  59. bool tryAddConnection(const int3 & from, const int3 & to, float cost, uint64_t danger);
  60. void removeConnection(const int3 & from, const int3 & to);
  61. void dumpToLog(std::string visualKey) const;
  62. void copyFrom(const ObjectGraph & other)
  63. {
  64. nodes = other.nodes;
  65. }
  66. template<typename Func>
  67. void iterateConnections(const int3 & pos, Func fn)
  68. {
  69. for(auto & connection : nodes.at(pos).connections)
  70. {
  71. fn(connection.first, connection.second);
  72. }
  73. }
  74. const ObjectNode & getNode(int3 tile) const
  75. {
  76. return nodes.at(tile);
  77. }
  78. bool hasNodeAt(const int3 & tile) const
  79. {
  80. return vstd::contains(nodes, tile);
  81. }
  82. };
  83. struct GraphPathNode;
  84. enum GrapthPathNodeType
  85. {
  86. NORMAL,
  87. BATTLE,
  88. LAST
  89. };
  90. struct GraphPathNodePointer
  91. {
  92. int3 coord = int3(-1);
  93. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  94. GraphPathNodePointer() = default;
  95. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  96. :coord(coord), nodeType(type)
  97. { }
  98. bool valid() const
  99. {
  100. return coord.valid();
  101. }
  102. };
  103. typedef std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]> GraphNodeStorage;
  104. class GraphNodeComparer
  105. {
  106. const GraphNodeStorage & pathNodes;
  107. public:
  108. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  109. :pathNodes(pathNodes)
  110. {
  111. }
  112. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  113. };
  114. struct GraphPathNode
  115. {
  116. const float BAD_COST = 100000;
  117. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  118. GraphPathNodePointer previous;
  119. float cost = BAD_COST;
  120. uint64_t danger = 0;
  121. const CGObjectInstance * obj = nullptr;
  122. std::shared_ptr<SpecialAction> specialAction;
  123. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  124. TFibHeap::handle_type handle;
  125. bool isInQueue = false;
  126. bool reachable() const
  127. {
  128. return cost < BAD_COST;
  129. }
  130. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  131. };
  132. class GraphPaths
  133. {
  134. ObjectGraph graph;
  135. GraphNodeStorage pathNodes;
  136. std::string visualKey;
  137. public:
  138. GraphPaths();
  139. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  140. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  141. void quickAddChainInfoWithBlocker(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  142. void dumpToLog() const;
  143. private:
  144. GraphPathNode & getOrCreateNode(const GraphPathNodePointer & pos)
  145. {
  146. auto & node = pathNodes[pos.coord][pos.nodeType];
  147. node.nodeType = pos.nodeType;
  148. return node;
  149. }
  150. const GraphPathNode & getNode(const GraphPathNodePointer & pos) const
  151. {
  152. auto & node = pathNodes.at(pos.coord)[pos.nodeType];
  153. return node;
  154. }
  155. };
  156. }