ObjectGraph.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. template<typename Func>
  63. void iterateConnections(const int3 & pos, Func fn)
  64. {
  65. for(auto & connection : nodes.at(pos).connections)
  66. {
  67. fn(connection.first, connection.second);
  68. }
  69. }
  70. const ObjectNode & getNode(int3 tile) const
  71. {
  72. return nodes.at(tile);
  73. }
  74. bool hasNodeAt(const int3 & tile) const
  75. {
  76. return vstd::contains(nodes, tile);
  77. }
  78. };
  79. struct GraphPathNode;
  80. enum GrapthPathNodeType
  81. {
  82. NORMAL,
  83. BATTLE,
  84. LAST
  85. };
  86. struct GraphPathNodePointer
  87. {
  88. int3 coord = int3(-1);
  89. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  90. GraphPathNodePointer() = default;
  91. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  92. :coord(coord), nodeType(type)
  93. { }
  94. bool valid() const
  95. {
  96. return coord.valid();
  97. }
  98. };
  99. typedef std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]> GraphNodeStorage;
  100. class GraphNodeComparer
  101. {
  102. const GraphNodeStorage & pathNodes;
  103. public:
  104. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  105. :pathNodes(pathNodes)
  106. {
  107. }
  108. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  109. };
  110. struct GraphPathNode
  111. {
  112. const float BAD_COST = 100000;
  113. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  114. GraphPathNodePointer previous;
  115. float cost = BAD_COST;
  116. uint64_t danger = 0;
  117. const CGObjectInstance * obj = nullptr;
  118. std::shared_ptr<SpecialAction> specialAction;
  119. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  120. TFibHeap::handle_type handle;
  121. bool isInQueue = false;
  122. bool reachable() const
  123. {
  124. return cost < BAD_COST;
  125. }
  126. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  127. };
  128. class GraphPaths
  129. {
  130. ObjectGraph graph;
  131. GraphNodeStorage pathNodes;
  132. std::string visualKey;
  133. public:
  134. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  135. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  136. void dumpToLog() const;
  137. private:
  138. GraphPathNode & getOrCreateNode(const GraphPathNodePointer & pos)
  139. {
  140. auto & node = pathNodes[pos.coord][pos.nodeType];
  141. node.nodeType = pos.nodeType;
  142. return node;
  143. }
  144. const GraphPathNode & getNode(const GraphPathNodePointer & pos) const
  145. {
  146. auto & node = pathNodes.at(pos.coord)[pos.nodeType];
  147. return node;
  148. }
  149. };
  150. }