ObjectClusterizer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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::BORDER_GATE
  103. || blocker->ID == Obj::SHIPYARD)
  104. {
  105. if(!isObjectPassable(blocker))
  106. return blocker;
  107. }
  108. if(blocker->ID == Obj::QUEST_GUARD && node->actionIsBlocked)
  109. {
  110. return blocker;
  111. }
  112. }
  113. return nullptr;
  114. }
  115. bool ObjectClusterizer::shouldVisitObject(const CGObjectInstance * obj) const
  116. {
  117. if(isObjectRemovable(obj))
  118. {
  119. return true;
  120. }
  121. const int3 pos = obj->visitablePos();
  122. if(obj->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->memory->alreadyVisited, obj)
  123. || obj->wasVisited(ai->playerID))
  124. {
  125. return false;
  126. }
  127. auto playerRelations = ai->cb->getPlayerRelations(ai->playerID, obj->tempOwner);
  128. if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(obj))
  129. {
  130. return false;
  131. }
  132. //it may be hero visiting this obj
  133. //we don't try visiting object on which allied or owned hero stands
  134. // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
  135. const CGObjectInstance * topObj = ai->cb->getTopObj(pos);
  136. if(!topObj)
  137. return false; // partly visible obj but its visitable pos is not visible.
  138. if(topObj->ID == Obj::HERO && ai->cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
  139. return false;
  140. else
  141. return true; //all of the following is met
  142. }
  143. void ObjectClusterizer::clusterize()
  144. {
  145. auto start = boost::chrono::high_resolution_clock::now();
  146. nearObjects.reset();
  147. farObjects.reset();
  148. blockedObjects.clear();
  149. Obj ignoreObjects[] = {
  150. Obj::BOAT,
  151. Obj::EYE_OF_MAGI,
  152. Obj::MONOLITH_ONE_WAY_ENTRANCE,
  153. Obj::MONOLITH_ONE_WAY_EXIT,
  154. Obj::MONOLITH_TWO_WAY,
  155. Obj::SUBTERRANEAN_GATE,
  156. Obj::WHIRLPOOL,
  157. Obj::BUOY,
  158. Obj::SIGN,
  159. Obj::SIGN,
  160. Obj::GARRISON,
  161. Obj::MONSTER,
  162. Obj::GARRISON2,
  163. Obj::BORDERGUARD,
  164. Obj::QUEST_GUARD,
  165. Obj::BORDER_GATE,
  166. Obj::REDWOOD_OBSERVATORY,
  167. Obj::CARTOGRAPHER,
  168. Obj::PILLAR_OF_FIRE
  169. };
  170. logAi->debug("Begin object clusterization");
  171. for(const CGObjectInstance * obj : ai->memory->visitableObjs)
  172. {
  173. if(!shouldVisitObject(obj))
  174. continue;
  175. #if AI_TRACE_LEVEL >= 2
  176. logAi->trace("Check object %s%s.", obj->getObjectName(), obj->visitablePos().toString());
  177. #endif
  178. auto paths = ai->pathfinder->getPathInfo(obj->visitablePos());
  179. if(paths.empty())
  180. {
  181. #if AI_TRACE_LEVEL >= 2
  182. logAi->trace("No paths found.");
  183. #endif
  184. continue;
  185. }
  186. std::sort(paths.begin(), paths.end(), [](const AIPath & p1, const AIPath & p2) -> bool
  187. {
  188. return p1.movementCost() < p2.movementCost();
  189. });
  190. if(vstd::contains(ignoreObjects, obj->ID))
  191. {
  192. farObjects.addObject(obj, paths.front(), 0);
  193. #if AI_TRACE_LEVEL >= 2
  194. logAi->trace("Object ignored. Moved to far objects with path %s", paths.front().toString());
  195. #endif
  196. continue;
  197. }
  198. std::set<const CGHeroInstance *> heroesProcessed;
  199. for(auto & path : paths)
  200. {
  201. #if AI_TRACE_LEVEL >= 2
  202. logAi->trace("Checking path %s", path.toString());
  203. #endif
  204. if(!shouldVisit(path.targetHero, obj))
  205. {
  206. #if AI_TRACE_LEVEL >= 2
  207. logAi->trace("Hero %s does not need to visit %s", path.targetHero->name, obj->getObjectName());
  208. #endif
  209. continue;
  210. }
  211. if(path.nodes.size() > 1)
  212. {
  213. auto blocker = getBlocker(path);
  214. if(blocker)
  215. {
  216. if(vstd::contains(heroesProcessed, path.targetHero))
  217. {
  218. #if AI_TRACE_LEVEL >= 2
  219. logAi->trace("Hero %s is already processed.", path.targetHero->name);
  220. #endif
  221. continue;
  222. }
  223. heroesProcessed.insert(path.targetHero);
  224. auto cluster = blockedObjects[blocker];
  225. if(!cluster)
  226. {
  227. cluster.reset(new ObjectCluster(blocker));
  228. blockedObjects[blocker] = cluster;
  229. }
  230. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
  231. if(priority < MIN_PRIORITY)
  232. continue;
  233. cluster->addObject(obj, path, priority);
  234. #if AI_TRACE_LEVEL >= 2
  235. logAi->trace("Path added to cluster %s%s", blocker->getObjectName(), blocker->visitablePos().toString());
  236. #endif
  237. continue;
  238. }
  239. }
  240. heroesProcessed.insert(path.targetHero);
  241. float priority = ai->priorityEvaluator->evaluate(Goals::sptr(Goals::ExecuteHeroChain(path, obj)));
  242. if(priority < MIN_PRIORITY)
  243. continue;
  244. bool interestingObject = path.turn() <= 2 || priority > 0.5f;
  245. if(interestingObject)
  246. {
  247. nearObjects.addObject(obj, path, priority);
  248. }
  249. else
  250. {
  251. farObjects.addObject(obj, path, priority);
  252. }
  253. #if AI_TRACE_LEVEL >= 2
  254. logAi->trace("Path %s added to %s objects. Turn: %d, priority: %f",
  255. path.toString(),
  256. interestingObject ? "near" : "far",
  257. path.turn(),
  258. priority);
  259. #endif
  260. }
  261. }
  262. vstd::erase_if(blockedObjects, [](std::pair<const CGObjectInstance *, std::shared_ptr<ObjectCluster>> pair) -> bool
  263. {
  264. return pair.second->objects.empty();
  265. });
  266. logAi->trace("Near objects count: %i", nearObjects.objects.size());
  267. logAi->trace("Far objects count: %i", farObjects.objects.size());
  268. for(auto pair : blockedObjects)
  269. {
  270. logAi->trace("Cluster %s %s count: %i", pair.first->getObjectName(), pair.first->visitablePos().toString(), pair.second->objects.size());
  271. #if AI_TRACE_LEVEL >= 1
  272. for(auto obj : pair.second->getObjects())
  273. {
  274. logAi->trace("Object %s %s", obj->getObjectName(), obj->visitablePos().toString());
  275. }
  276. #endif
  277. }
  278. logAi->trace("Clusterization complete in %ld", timeElapsed(start));
  279. }