2
0

ObjectGraph.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * ObjectGraph.cpp, 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. #include "StdInc.h"
  11. #include "ObjectGraph.h"
  12. #include "ObjectGraphCalculator.h"
  13. #include "AIPathfinderConfig.h"
  14. #include "../../../lib/CRandomGenerator.h"
  15. #include "../../../lib/mapping/CMap.h"
  16. #include "../Engine/Nullkiller.h"
  17. #include "../../../lib/logging/VisualLogger.h"
  18. #include "Actions/QuestAction.h"
  19. #include "../pforeach.h"
  20. #include "Actions/BoatActions.h"
  21. namespace NK2AI
  22. {
  23. bool ObjectGraph::tryAddConnection(
  24. const int3 & from,
  25. const int3 & to,
  26. float cost,
  27. uint64_t danger)
  28. {
  29. auto result = nodes[from].connections[to].update(cost, danger);
  30. auto & connection = nodes[from].connections[to];
  31. if(result && isVirtualBoat(to) && !connection.specialAction)
  32. {
  33. connection.specialAction = std::make_shared<AIPathfinding::BuildBoatActionFactory>(virtualBoats[to]);
  34. }
  35. return result;
  36. }
  37. void ObjectGraph::removeConnection(const int3 & from, const int3 & to)
  38. {
  39. nodes[from].connections.erase(to);
  40. }
  41. void ObjectGraph::updateGraph(const Nullkiller * aiNk)
  42. {
  43. ObjectGraphCalculator calculator(this, aiNk);
  44. calculator.setGraphObjects();
  45. calculator.calculateConnections();
  46. calculator.addMinimalDistanceJunctions();
  47. calculator.calculateConnections();
  48. if(NKAI_GRAPH_TRACE_LEVEL >= 1)
  49. dumpToLog("graph");
  50. }
  51. void ObjectGraph::addObject(const CGObjectInstance * obj)
  52. {
  53. if(!hasNodeAt(obj->visitablePos()))
  54. nodes[obj->visitablePos()].init(obj);
  55. }
  56. void ObjectGraph::addVirtualBoat(const int3 & pos, const CGObjectInstance * shipyard)
  57. {
  58. if(!isVirtualBoat(pos))
  59. {
  60. virtualBoats[pos] = shipyard->id;
  61. }
  62. }
  63. void ObjectGraph::registerJunction(const int3 & pos)
  64. {
  65. if(!hasNodeAt(pos))
  66. nodes[pos].initJunction();
  67. }
  68. void ObjectGraph::removeObject(const CGObjectInstance * obj)
  69. {
  70. nodes[obj->visitablePos()].objectExists = false;
  71. if(obj->ID == Obj::BOAT && !isVirtualBoat(obj->visitablePos()))
  72. {
  73. vstd::erase_if(nodes[obj->visitablePos()].connections, [&](const std::pair<int3, ObjectLink> & link) -> bool
  74. {
  75. auto tile = ccTl->getTile(link.first, false);
  76. return tile && tile->isWater();
  77. });
  78. }
  79. }
  80. void ObjectGraph::connectHeroes(const Nullkiller * aiNk)
  81. {
  82. for(auto obj : aiNk->memory->visitableObjs)
  83. {
  84. if(obj && obj->ID == Obj::HERO)
  85. {
  86. addObject(obj);
  87. }
  88. }
  89. for(auto & node : nodes)
  90. {
  91. auto pos = node.first;
  92. auto paths = aiNk->pathfinder->getPathInfo(pos);
  93. for(AIPath & path : paths)
  94. {
  95. if(path.getFirstBlockedAction())
  96. continue;
  97. auto heroPos = path.targetHero->visitablePos();
  98. nodes[pos].connections[heroPos].update(
  99. std::max(0.0f, path.movementCost()),
  100. path.getPathDanger());
  101. nodes[heroPos].connections[pos].update(
  102. std::max(0.0f, path.movementCost()),
  103. path.getPathDanger());
  104. }
  105. }
  106. }
  107. void ObjectGraph::dumpToLog(std::string visualKey) const
  108. {
  109. logVisual->updateWithLock(visualKey, [&](IVisualLogBuilder & logBuilder)
  110. {
  111. for(auto & tile : nodes)
  112. {
  113. for(auto & node : tile.second.connections)
  114. {
  115. if(NKAI_GRAPH_TRACE_LEVEL >= 2)
  116. {
  117. logAi->trace(
  118. "%s -> %s: %f !%d",
  119. node.first.toString(),
  120. tile.first.toString(),
  121. node.second.cost,
  122. node.second.danger);
  123. }
  124. logBuilder.addLine(tile.first, node.first);
  125. }
  126. }
  127. });
  128. }
  129. }