ObjectGraph.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. };
  44. class ObjectGraph
  45. {
  46. std::unordered_map<int3, ObjectNode> nodes;
  47. public:
  48. void updateGraph(const Nullkiller * ai);
  49. void addObject(const CGObjectInstance * obj);
  50. void connectHeroes(const Nullkiller * ai);
  51. void removeObject(const CGObjectInstance * obj);
  52. void dumpToLog(std::string visualKey) const;
  53. template<typename Func>
  54. void iterateConnections(const int3 & pos, Func fn)
  55. {
  56. for(auto & connection : nodes.at(pos).connections)
  57. {
  58. fn(connection.first, connection.second);
  59. }
  60. }
  61. const ObjectNode & getNode(int3 tile) const
  62. {
  63. return nodes.at(tile);
  64. }
  65. };
  66. struct GraphPathNode;
  67. enum GrapthPathNodeType
  68. {
  69. NORMAL,
  70. BATTLE,
  71. LAST
  72. };
  73. struct GraphPathNodePointer
  74. {
  75. int3 coord = int3(-1);
  76. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  77. GraphPathNodePointer() = default;
  78. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  79. :coord(coord), nodeType(type)
  80. { }
  81. bool valid() const
  82. {
  83. return coord.valid();
  84. }
  85. };
  86. typedef std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]> GraphNodeStorage;
  87. class GraphNodeComparer
  88. {
  89. const GraphNodeStorage & pathNodes;
  90. public:
  91. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  92. :pathNodes(pathNodes)
  93. {
  94. }
  95. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  96. };
  97. struct GraphPathNode
  98. {
  99. const float BAD_COST = 100000;
  100. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  101. GraphPathNodePointer previous;
  102. float cost = BAD_COST;
  103. uint64_t danger = 0;
  104. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  105. TFibHeap::handle_type handle;
  106. bool isInQueue = false;
  107. bool reachable() const
  108. {
  109. return cost < BAD_COST;
  110. }
  111. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  112. };
  113. class GraphPaths
  114. {
  115. ObjectGraph graph;
  116. GraphNodeStorage pathNodes;
  117. std::string visualKey;
  118. public:
  119. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  120. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  121. void dumpToLog() const;
  122. private:
  123. GraphPathNode & getNode(const GraphPathNodePointer & pos)
  124. {
  125. auto & node = pathNodes[pos.coord][pos.nodeType];
  126. node.nodeType = pos.nodeType;
  127. return node;
  128. }
  129. };
  130. }