ObjectClusterizer.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 *, ObjectInfo> & 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. nearObjects.reset();
  143. farObjects.reset();
  144. blockedObjects.clear();
  145. Obj ignoreObjects[] = {
  146. Obj::MONSTER,
  147. Obj::SIGN,
  148. Obj::REDWOOD_OBSERVATORY,
  149. Obj::MONOLITH_TWO_WAY,
  150. Obj::MONOLITH_ONE_WAY_ENTRANCE,
  151. Obj::MONOLITH_ONE_WAY_EXIT,
  152. Obj::BUOY
  153. };
  154. logAi->debug("Begin object clusterization");
  155. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  156. {
  157. if(!shouldVisitObject(obj))
  158. continue;
  159. auto paths = ai->pathfinder->getPathInfo(obj->visitablePos());
  160. if(paths.empty())
  161. continue;
  162. std::sort(paths.begin(), paths.end(), [](const AIPath & p1, const AIPath & p2) -> bool
  163. {
  164. return p1.movementCost() < p2.movementCost();
  165. });
  166. if(vstd::contains(ignoreObjects, obj->ID))
  167. {
  168. farObjects.addObject(obj, paths.front(), 0);
  169. continue;
  170. }
  171. bool added = false;
  172. bool directlyAccessible = false;
  173. std::set<const CGHeroInstance *> heroesProcessed;
  174. for(auto & path : paths)
  175. {
  176. if(vstd::contains(heroesProcessed, path.targetHero))
  177. continue;
  178. heroesProcessed.insert(path.targetHero);
  179. if(path.nodes.size() > 1)
  180. {
  181. auto blocker = getBlocker(path);
  182. if(blocker)
  183. {
  184. auto cluster = blockedObjects[blocker];
  185. if(!cluster)
  186. {
  187. cluster.reset(new ObjectCluster(blocker));
  188. blockedObjects[blocker] = cluster;
  189. }
  190. if(!vstd::contains(cluster->objects, obj))
  191. {
  192. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
  193. cluster->addObject(obj, path, priority);
  194. added = true;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. directlyAccessible = true;
  201. }
  202. }
  203. if(!added || directlyAccessible)
  204. {
  205. AIPath & shortestPath = paths.front();
  206. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(shortestPath, obj)));
  207. if(shortestPath.turn() <= 2 || priority > 0.6f)
  208. nearObjects.addObject(obj, shortestPath, 0);
  209. else
  210. farObjects.addObject(obj, shortestPath, 0);
  211. }
  212. }
  213. logAi->trace("Near objects count: %i", nearObjects.objects.size());
  214. logAi->trace("Far objects count: %i", farObjects.objects.size());
  215. for(auto pair : blockedObjects)
  216. {
  217. logAi->trace("Cluster %s %s count: %i", pair.first->getObjectName(), pair.first->visitablePos().toString(), pair.second->objects.size());
  218. #if AI_TRACE_LEVEL >= 1
  219. for(auto obj : pair.second->getObjects())
  220. {
  221. logAi->trace("Object %s %s", obj->getObjectName(), obj->visitablePos().toString());
  222. }
  223. #endif
  224. }
  225. }