ObjectGraph.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. void update(float newCost, uint64_t newDanger)
  21. {
  22. if(cost > newCost)
  23. {
  24. cost = newCost;
  25. danger = newDanger;
  26. }
  27. }
  28. };
  29. struct ObjectNode
  30. {
  31. ObjectInstanceID objID;
  32. bool objectExists;
  33. std::unordered_map<int3, ObjectLink> connections;
  34. void init(const CGObjectInstance * obj)
  35. {
  36. objectExists = true;
  37. objID = obj->id;
  38. }
  39. };
  40. class ObjectGraph
  41. {
  42. std::unordered_map<int3, ObjectNode> nodes;
  43. public:
  44. void updateGraph(const Nullkiller * ai);
  45. void addObject(const CGObjectInstance * obj);
  46. void connectHeroes(const Nullkiller * ai);
  47. template<typename Func>
  48. void iterateConnections(const int3 & pos, Func fn)
  49. {
  50. for(auto connection : nodes.at(pos).connections)
  51. {
  52. fn(connection.first, connection.second);
  53. }
  54. }
  55. };
  56. struct GraphPathNode;
  57. enum GrapthPathNodeType
  58. {
  59. NORMAL,
  60. BATTLE,
  61. LAST
  62. };
  63. struct GraphPathNodePointer
  64. {
  65. int3 coord = int3(-1);
  66. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  67. GraphPathNodePointer() = default;
  68. GraphPathNodePointer(int3 coord, GrapthPathNodeType type)
  69. :coord(coord), nodeType(type)
  70. { }
  71. bool valid() const
  72. {
  73. return coord.valid();
  74. }
  75. };
  76. typedef std::unordered_map<int3, GraphPathNode[GrapthPathNodeType::LAST]> GraphNodeStorage;
  77. class GraphNodeComparer
  78. {
  79. const GraphNodeStorage & pathNodes;
  80. public:
  81. GraphNodeComparer(const GraphNodeStorage & pathNodes)
  82. :pathNodes(pathNodes)
  83. {
  84. }
  85. bool operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const;
  86. };
  87. struct GraphPathNode
  88. {
  89. const float BAD_COST = 100000;
  90. GrapthPathNodeType nodeType = GrapthPathNodeType::NORMAL;
  91. GraphPathNodePointer previous;
  92. float cost = BAD_COST;
  93. uint64_t danger = 0;
  94. using TFibHeap = boost::heap::fibonacci_heap<GraphPathNodePointer, boost::heap::compare<GraphNodeComparer>>;
  95. TFibHeap::handle_type handle;
  96. bool isInQueue = false;
  97. bool reachable() const
  98. {
  99. return cost < BAD_COST;
  100. }
  101. bool tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link);
  102. };
  103. class GraphPaths
  104. {
  105. ObjectGraph graph;
  106. GraphNodeStorage pathNodes;
  107. public:
  108. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  109. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  110. void dumpToLog() const;
  111. private:
  112. GraphPathNode & getNode(const GraphPathNodePointer & pos)
  113. {
  114. return pathNodes[pos.coord][pos.nodeType];
  115. }
  116. };
  117. }