ObjectClusterizer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * DangerHitMapAnalyzer.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 "ObjectClusterizer.h"
  12. #include "../Goals/ExecuteHeroChain.h"
  13. #include "../VCAI.h"
  14. #include "../Engine/Nullkiller.h"
  15. #include "lib/mapping/CMap.h" //for victory conditions
  16. void ObjectCluster::addObject(const CGObjectInstance * obj, const AIPath & path, float priority)
  17. {
  18. auto & info = objects[obj];
  19. if(info.priority < priority)
  20. {
  21. info.priority = priority;
  22. info.movementCost = path.movementCost() - path.firstNode().cost;
  23. info.danger = path.targetObjectDanger;
  24. info.turn = path.turn();
  25. }
  26. }
  27. const CGObjectInstance * ObjectCluster::calculateCenter() const
  28. {
  29. auto v = getObjects();
  30. auto tile = int3(0);
  31. float priority = 0;
  32. for(auto pair : objects)
  33. {
  34. auto newPoint = pair.first->visitablePos();
  35. float newPriority = std::pow(pair.second.priority, 4); // lets make high priority targets more weghtful
  36. int3 direction = newPoint - tile;
  37. float priorityRatio = newPriority / (priority + newPriority);
  38. tile += direction * priorityRatio;
  39. priority += newPriority;
  40. }
  41. auto closestPair = *vstd::minElementByFun(objects, [&](const std::pair<const CGObjectInstance *, ClusterObjectInfo> & pair) -> int
  42. {
  43. return pair.first->visitablePos().dist2dSQ(tile);
  44. });
  45. return closestPair.first;
  46. }
  47. std::vector<const CGObjectInstance *> ObjectCluster::getObjects() const
  48. {
  49. std::vector<const CGObjectInstance *> result;
  50. for(auto pair : objects)
  51. {
  52. result.push_back(pair.first);
  53. }
  54. return result;
  55. }
  56. std::vector<const CGObjectInstance *> ObjectClusterizer::getNearbyObjects() const
  57. {
  58. return nearObjects.getObjects();
  59. }
  60. std::vector<const CGObjectInstance *> ObjectClusterizer::getFarObjects() const
  61. {
  62. return farObjects.getObjects();
  63. }
  64. std::vector<std::shared_ptr<ObjectCluster>> ObjectClusterizer::getLockedClusters() const
  65. {
  66. std::vector<std::shared_ptr<ObjectCluster>> result;
  67. for(auto pair : blockedObjects)
  68. {
  69. result.push_back(pair.second);
  70. }
  71. return result;
  72. }
  73. const CGObjectInstance * ObjectClusterizer::getBlocker(const AIPath & path) const
  74. {
  75. for(auto node = path.nodes.rbegin(); node != path.nodes.rend(); node++)
  76. {
  77. auto guardPos = ai->cb->getGuardingCreaturePosition(node->coord);
  78. auto blockers = ai->cb->getVisitableObjs(node->coord);
  79. if(guardPos.valid())
  80. {
  81. auto guard = ai->cb->getTopObj(ai->cb->getGuardingCreaturePosition(node->coord));
  82. if(guard)
  83. {
  84. blockers.insert(blockers.begin(), guard);
  85. }
  86. }
  87. if(node->specialAction && node->actionIsBlocked)
  88. {
  89. auto blockerObject = node->specialAction->targetObject();
  90. if(blockerObject)
  91. {
  92. blockers.push_back(blockerObject);
  93. }
  94. }
  95. if(blockers.empty())
  96. continue;
  97. auto blocker = blockers.front();
  98. if(blocker->ID == Obj::GARRISON
  99. || blocker->ID == Obj::MONSTER
  100. || blocker->ID == Obj::GARRISON2
  101. || blocker->ID == Obj::BORDERGUARD
  102. || blocker->ID == Obj::QUEST_GUARD
  103. || blocker->ID == Obj::BORDER_GATE
  104. || blocker->ID == Obj::SHIPYARD)
  105. {
  106. if(!isObjectPassable(blocker))
  107. return blocker;
  108. }
  109. }
  110. return nullptr;
  111. }
  112. bool ObjectClusterizer::shouldVisitObject(const CGObjectInstance * obj) const
  113. {
  114. if(isObjectRemovable(obj))
  115. {
  116. return true;
  117. }
  118. const int3 pos = obj->visitablePos();
  119. if(obj->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->memory->alreadyVisited, obj)
  120. || obj->wasVisited(ai->playerID))
  121. {
  122. return false;
  123. }
  124. auto playerRelations = ai->cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  125. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(obj))
  126. {
  127. return false;
  128. }
  129. //it may be hero visiting this obj
  130. //we don't try visiting object on which allied or owned hero stands
  131. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  132. const CGObjectInstance * topObj = ai->cb->getTopObj(pos);
  133. if(!topObj)
  134. return false; // partly visible obj but its visitable pos is not visible.
  135. if(topObj->ID == Obj::HERO && ai->cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  136. return false;
  137. else
  138. return true; //all of the following is met
  139. }
  140. void ObjectClusterizer::clusterize()
  141. {
  142. auto start = boost::chrono::high_resolution_clock::now();
  143. nearObjects.reset();
  144. farObjects.reset();
  145. blockedObjects.clear();
  146. Obj ignoreObjects[] = {
  147. Obj::MONSTER,
  148. Obj::SIGN,
  149. Obj::REDWOOD_OBSERVATORY,
  150. Obj::MONOLITH_TWO_WAY,
  151. Obj::MONOLITH_ONE_WAY_ENTRANCE,
  152. Obj::MONOLITH_ONE_WAY_EXIT,
  153. Obj::BUOY
  154. };
  155. logAi->debug("Begin object clusterization");
  156. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  157. {
  158. if(!shouldVisitObject(obj))
  159. continue;
  160. auto paths = ai->pathfinder->getPathInfo(obj->visitablePos());
  161. if(paths.empty())
  162. continue;
  163. std::sort(paths.begin(), paths.end(), [](const AIPath & p1, const AIPath & p2) -> bool
  164. {
  165. return p1.movementCost() < p2.movementCost();
  166. });
  167. if(vstd::contains(ignoreObjects, obj->ID))
  168. {
  169. farObjects.addObject(obj, paths.front(), 0);
  170. continue;
  171. }
  172. bool added = false;
  173. bool directlyAccessible = false;
  174. std::set<const CGHeroInstance *> heroesProcessed;
  175. for(auto & path : paths)
  176. {
  177. if(vstd::contains(heroesProcessed, path.targetHero))
  178. continue;
  179. heroesProcessed.insert(path.targetHero);
  180. if(path.nodes.size() > 1)
  181. {
  182. auto blocker = getBlocker(path);
  183. if(blocker)
  184. {
  185. auto cluster = blockedObjects[blocker];
  186. if(!cluster)
  187. {
  188. cluster.reset(new ObjectCluster(blocker));
  189. blockedObjects[blocker] = cluster;
  190. }
  191. if(!vstd::contains(cluster->objects, obj))
  192. {
  193. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
  194. cluster->addObject(obj, path, priority);
  195. added = true;
  196. }
  197. }
  198. }
  199. else
  200. {
  201. directlyAccessible = true;
  202. }
  203. }
  204. if(!added || directlyAccessible)
  205. {
  206. AIPath & shortestPath = paths.front();
  207. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(shortestPath, obj)));
  208. if(shortestPath.turn() <= 2 || priority > 0.6f)
  209. nearObjects.addObject(obj, shortestPath, 0);
  210. else
  211. farObjects.addObject(obj, shortestPath, 0);
  212. }
  213. }
  214. logAi->trace("Near objects count: %i", nearObjects.objects.size());
  215. logAi->trace("Far objects count: %i", farObjects.objects.size());
  216. for(auto pair : blockedObjects)
  217. {
  218. logAi->trace("Cluster %s %s count: %i", pair.first->getObjectName(), pair.first->visitablePos().toString(), pair.second->objects.size());
  219. #if AI_TRACE_LEVEL >= 1
  220. for(auto obj : pair.second->getObjects())
  221. {
  222. logAi->trace("Object %s %s", obj->getObjectName(), obj->visitablePos().toString());
  223. }
  224. #endif
  225. }
  226. logAi->trace("Clusterization complete in %ld", timeElapsed(start));
  227. }