2
0

ObjectGraph.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. bool tryAddConnection(const int3 & from, const int3 & to, float cost, uint64_t danger);
  66. };
  67. struct GraphPathNode;
  68. enum GrapthPathNodeType
  69. {
  70. NORMAL,
  71. BATTLE,
  72. LAST
  73. };
  74. struct GraphPathNodePointer
  75. {
  76. int3 coord = int3(-1);
  77. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  78. GraphPathNodePointer() = default;
  79. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  80. :coord(coord), nodeType(type)
  81. { }
  82. bool valid() const
  83. {
  84. return coord.valid();
  85. }
  86. };
  87. typedef std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]> GraphNodeStorage;
  88. class GraphNodeComparer
  89. {
  90. const GraphNodeStorage & pathNodes;
  91. public:
  92. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  93. :pathNodes(pathNodes)
  94. {
  95. }
  96. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  97. };
  98. struct GraphPathNode
  99. {
  100. const float BAD_COST = 100000;
  101. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  102. GraphPathNodePointer previous;
  103. float cost = BAD_COST;
  104. uint64_t danger = 0;
  105. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  106. TFibHeap::handle_type handle;
  107. bool isInQueue = false;
  108. bool reachable() const
  109. {
  110. return cost < BAD_COST;
  111. }
  112. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  113. };
  114. class GraphPaths
  115. {
  116. ObjectGraph graph;
  117. GraphNodeStorage pathNodes;
  118. std::string visualKey;
  119. public:
  120. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  121. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  122. void dumpToLog() const;
  123. private:
  124. GraphPathNode & getNode(const GraphPathNodePointer & pos)
  125. {
  126. auto & node = pathNodes[pos.coord][pos.nodeType];
  127. node.nodeType = pos.nodeType;
  128. return node;
  129. }
  130. };
  131. }