ObjectGraph.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "AIPathfinderConfig.h"
  13. #include "../../../CCallback.h"
  14. #include "../../../lib/mapping/CMap.h"
  15. #include "../Engine/Nullkiller.h"
  16. namespace NKAI
  17. {
  18. void ObjectGraph::updateGraph(const Nullkiller * ai)
  19. {
  20. auto cb = ai->cb;
  21. auto mapSize = cb->getMapSize();
  22. std::map<const CGHeroInstance *, HeroRole> actors;
  23. std::map<const CGHeroInstance *, const CGObjectInstance *> actorObjectMap;
  24. auto addObjectActor = [&](const CGObjectInstance * obj)
  25. {
  26. auto objectActor = new CGHeroInstance(obj->cb);
  27. CRandomGenerator rng;
  28. auto visitablePos = obj->visitablePos();
  29. objectActor->setOwner(ai->playerID); // lets avoid having multiple colors
  30. objectActor->initHero(rng, static_cast<HeroTypeID>(0));
  31. objectActor->pos = objectActor->convertFromVisitablePos(visitablePos);
  32. objectActor->initObj(rng);
  33. actorObjectMap[objectActor] = obj;
  34. actors[objectActor] = obj->ID == Obj::TOWN ? HeroRole::MAIN : HeroRole::SCOUT;
  35. addObject(obj);
  36. };
  37. for(auto obj : ai->memory->visitableObjs)
  38. {
  39. if(obj && obj->isVisitable() && obj->ID != Obj::HERO)
  40. {
  41. addObjectActor(obj);
  42. }
  43. }
  44. for(auto town : cb->getTownsInfo())
  45. {
  46. addObjectActor(town);
  47. }
  48. PathfinderSettings ps;
  49. ps.mainTurnDistanceLimit = 5;
  50. ps.scoutTurnDistanceLimit = 1;
  51. ps.allowBypassObjects = false;
  52. ai->pathfinder->updatePaths(actors, ps);
  53. foreach_tile_pos(cb.get(), [&](const CPlayerSpecificInfoCallback * cb, const int3 & pos)
  54. {
  55. auto paths = ai->pathfinder->getPathInfo(pos);
  56. for(AIPath & path1 : paths)
  57. {
  58. for(AIPath & path2 : paths)
  59. {
  60. if(path1.targetHero == path2.targetHero)
  61. continue;
  62. auto obj1 = actorObjectMap[path1.targetHero];
  63. auto obj2 = actorObjectMap[path2.targetHero];
  64. nodes[obj1->visitablePos()].connections[obj2->visitablePos()].update(
  65. path1.movementCost() + path2.movementCost(),
  66. path1.getPathDanger() + path2.getPathDanger());
  67. }
  68. }
  69. });
  70. for(auto h : actors)
  71. {
  72. delete h.first;
  73. }
  74. }
  75. void ObjectGraph::addObject(const CGObjectInstance * obj)
  76. {
  77. nodes[obj->visitablePos()].init(obj);
  78. }
  79. void ObjectGraph::connectHeroes(const Nullkiller * ai)
  80. {
  81. for(auto obj : ai->memory->visitableObjs)
  82. {
  83. if(obj && obj->ID == Obj::HERO)
  84. {
  85. addObject(obj);
  86. }
  87. }
  88. for(auto node : nodes)
  89. {
  90. auto pos = node.first;
  91. auto paths = ai->pathfinder->getPathInfo(pos);
  92. for(AIPath & path : paths)
  93. {
  94. auto heroPos = path.targetHero->visitablePos();
  95. nodes[pos].connections[heroPos].update(
  96. path.movementCost(),
  97. path.getPathDanger());
  98. nodes[heroPos].connections[pos].update(
  99. path.movementCost(),
  100. path.getPathDanger());
  101. }
  102. }
  103. }
  104. bool GraphNodeComparer::operator()(const GraphPathNodePointer & lhs, const GraphPathNodePointer & rhs) const
  105. {
  106. return pathNodes.at(lhs.coord)[lhs.nodeType].cost > pathNodes.at(rhs.coord)[rhs.nodeType].cost;
  107. }
  108. void GraphPaths::calculatePaths(const CGHeroInstance * targetHero, const Nullkiller * ai)
  109. {
  110. graph = *ai->baseGraph;
  111. graph.connectHeroes(ai);
  112. pathNodes.clear();
  113. GraphNodeComparer cmp(pathNodes);
  114. GraphPathNode::TFibHeap pq(cmp);
  115. pathNodes[targetHero->visitablePos()][GrapthPathNodeType::NORMAL].cost = 0;
  116. pq.emplace(GraphPathNodePointer(targetHero->visitablePos(), GrapthPathNodeType::NORMAL));
  117. while(!pq.empty())
  118. {
  119. GraphPathNodePointer pos = pq.top();
  120. pq.pop();
  121. auto & node = getNode(pos);
  122. node.isInQueue = false;
  123. graph.iterateConnections(pos.coord, [&](int3 target, ObjectLink o)
  124. {
  125. auto targetPointer = GraphPathNodePointer(target, pos.nodeType);
  126. auto & targetNode = getNode(targetPointer);
  127. if(targetNode.tryUpdate(pos, node, o))
  128. {
  129. if(targetNode.isInQueue)
  130. {
  131. pq.increase(targetNode.handle);
  132. }
  133. else
  134. {
  135. targetNode.handle = pq.emplace(targetPointer);
  136. targetNode.isInQueue = true;
  137. }
  138. }
  139. });
  140. }
  141. }
  142. void GraphPaths::dumpToLog() const
  143. {
  144. for(auto & tile : pathNodes)
  145. {
  146. for(auto & node : tile.second)
  147. {
  148. if(!node.previous.valid())
  149. continue;
  150. logAi->trace(
  151. "%s -> %s: %f !%d",
  152. node.previous.coord.toString(),
  153. tile.first.toString(),
  154. node.cost,
  155. node.danger);
  156. }
  157. }
  158. }
  159. bool GraphPathNode::tryUpdate(const GraphPathNodePointer & pos, const GraphPathNode & prev, const ObjectLink & link)
  160. {
  161. auto newCost = prev.cost + link.cost;
  162. if(newCost < cost)
  163. {
  164. previous = pos;
  165. danger = prev.danger + link.danger;
  166. cost = newCost;
  167. return true;
  168. }
  169. return false;
  170. }
  171. void GraphPaths::addChainInfo(std::vector<AIPath> & paths, int3 tile, const CGHeroInstance * hero, const Nullkiller * ai) const
  172. {
  173. auto nodes = pathNodes.find(tile);
  174. if(nodes == pathNodes.end())
  175. return;
  176. for(auto & node : (*nodes).second)
  177. {
  178. if(!node.reachable())
  179. continue;
  180. std::vector<int3> tilesToPass;
  181. uint64_t danger = node.danger;
  182. float cost = node.cost;
  183. bool allowBattle = false;
  184. auto current = GraphPathNodePointer(nodes->first, node.nodeType);
  185. while(true)
  186. {
  187. auto currentTile = pathNodes.find(current.coord);
  188. if(currentTile == pathNodes.end())
  189. break;
  190. auto currentNode = currentTile->second[current.nodeType];
  191. allowBattle = allowBattle || currentNode.nodeType == GrapthPathNodeType::BATTLE;
  192. vstd::amax(danger, currentNode.danger);
  193. vstd::amax(cost, currentNode.cost);
  194. tilesToPass.push_back(current.coord);
  195. if(currentNode.cost < 2)
  196. break;
  197. current = currentNode.previous;
  198. }
  199. if(tilesToPass.empty())
  200. continue;
  201. auto entryPaths = ai->pathfinder->getPathInfo(tilesToPass.back());
  202. for(auto & path : entryPaths)
  203. {
  204. if(path.targetHero != hero)
  205. continue;
  206. for(auto graphTile = tilesToPass.rbegin(); graphTile != tilesToPass.rend(); graphTile++)
  207. {
  208. AIPathNodeInfo n;
  209. n.coord = *graphTile;
  210. n.cost = cost;
  211. n.turns = static_cast<ui8>(cost) + 1; // just in case lets select worst scenario
  212. n.danger = danger;
  213. n.targetHero = hero;
  214. for(auto & node : path.nodes)
  215. {
  216. node.parentIndex++;
  217. }
  218. path.nodes.insert(path.nodes.begin(), n);
  219. }
  220. path.armyLoss += ai->pathfinder->getStorage()->evaluateArmyLoss(path.targetHero, path.heroArmy->getArmyStrength(), danger);
  221. path.targetObjectDanger = ai->pathfinder->getStorage()->evaluateDanger(tile, path.targetHero, !allowBattle);
  222. path.targetObjectArmyLoss = ai->pathfinder->getStorage()->evaluateArmyLoss(path.targetHero, path.heroArmy->getArmyStrength(), path.targetObjectDanger);
  223. paths.push_back(path);
  224. }
  225. }
  226. }
  227. }