ObjectGraph.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class GraphNodeComparer
  58. {
  59. const std::unordered_map<int3, GraphPathNode> & pathNodes;
  60. public:
  61. GraphNodeComparer(const std::unordered_map<int3, GraphPathNode> & pathNodes)
  62. :pathNodes(pathNodes)
  63. {
  64. }
  65. bool operator()(int3 lhs, int3 rhs) const;
  66. };
  67. struct GraphPathNode
  68. {
  69. const float BAD_COST = 100000;
  70. int3 previous = int3(-1);
  71. float cost = BAD_COST;
  72. uint64_t danger = 0;
  73. using TFibHeap = boost::heap::fibonacci_heap<int3, boost::heap::compare<GraphNodeComparer>>;
  74. TFibHeap::handle_type handle;
  75. bool isInQueue = false;
  76. bool reachable() const
  77. {
  78. return cost < BAD_COST;
  79. }
  80. bool tryUpdate(const int3 & pos, const GraphPathNode & prev, const ObjectLink & link)
  81. {
  82. auto newCost = prev.cost + link.cost;
  83. if(newCost < cost)
  84. {
  85. previous = pos;
  86. danger = prev.danger + link.danger;
  87. cost = newCost;
  88. return true;
  89. }
  90. return false;
  91. }
  92. };
  93. class GraphPaths
  94. {
  95. ObjectGraph graph;
  96. std::unordered_map<int3, GraphPathNode> pathNodes;
  97. public:
  98. void calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai);
  99. void addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const;
  100. void dumpToLog() const;
  101. };
  102. }