ObjectGraph.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 NKAI
  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 * ai)
  42. {
  43. auto cb = ai->cb;
  44. ObjectGraphCalculator calculator(this, ai);
  45. calculator.setGraphObjects();
  46. calculator.calculateConnections();
  47. calculator.addMinimalDistanceJunctions();
  48. calculator.calculateConnections();
  49. if(NKAI_GRAPH_TRACE_LEVEL >= 1)
  50. dumpToLog("graph");
  51. }
  52. void ObjectGraph::addObject(const CGObjectInstance * obj)
  53. {
  54. if(!hasNodeAt(obj->visitablePos()))
  55. nodes[obj->visitablePos()].init(obj);
  56. }
  57. void ObjectGraph::addVirtualBoat(const int3 & pos, const CGObjectInstance * shipyard)
  58. {
  59. if(!isVirtualBoat(pos))
  60. {
  61. virtualBoats[pos] = shipyard->id;
  62. }
  63. }
  64. void ObjectGraph::registerJunction(const int3 & pos)
  65. {
  66. if(!hasNodeAt(pos))
  67. nodes[pos].initJunction();
  68. }
  69. void ObjectGraph::removeObject(const CGObjectInstance * obj)
  70. {
  71. nodes[obj->visitablePos()].objectExists = false;
  72. if(obj->ID == Obj::BOAT && !isVirtualBoat(obj->visitablePos()))
  73. {
  74. vstd::erase_if(nodes[obj->visitablePos()].connections, [&](const std::pair<int3, ObjectLink> & link) -> bool
  75. {
  76. auto tile = cb->getTile(link.first, false);
  77. return tile && tile->isWater();
  78. });
  79. }
  80. }
  81. void ObjectGraph::connectHeroes(const Nullkiller * ai)
  82. {
  83. for(auto obj : ai->memory->visitableObjs)
  84. {
  85. if(obj && obj->ID == Obj::HERO)
  86. {
  87. addObject(obj);
  88. }
  89. }
  90. for(auto & node : nodes)
  91. {
  92. auto pos = node.first;
  93. auto paths = ai->pathfinder->getPathInfo(pos);
  94. for(AIPath & path : paths)
  95. {
  96. if(path.getFirstBlockedAction())
  97. continue;
  98. auto heroPos = path.targetHero->visitablePos();
  99. nodes[pos].connections[heroPos].update(
  100. std::max(0.0f, path.movementCost()),
  101. path.getPathDanger());
  102. nodes[heroPos].connections[pos].update(
  103. std::max(0.0f, path.movementCost()),
  104. path.getPathDanger());
  105. }
  106. }
  107. }
  108. void ObjectGraph::dumpToLog(std::string visualKey) const
  109. {
  110. logVisual->updateWithLock(visualKey, [&](IVisualLogBuilder & logBuilder)
  111. {
  112. for(auto & tile : nodes)
  113. {
  114. for(auto & node : tile.second.connections)
  115. {
  116. if(NKAI_GRAPH_TRACE_LEVEL >= 2)
  117. {
  118. logAi->trace(
  119. "%s -> %s: %f !%d",
  120. node.first.toString(),
  121. tile.first.toString(),
  122. node.second.cost,
  123. node.second.danger);
  124. }
  125. logBuilder.addLine(tile.first, node.first);
  126. }
  127. }
  128. });
  129. }
  130. }