ObjectGraph.h 4.3 KB

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