2
0

ObjectGraph.cpp 3.4 KB

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