|
|
@@ -50,179 +50,164 @@ bool CaptureObjectsBehavior::operator==(const CaptureObjectsBehavior & other) co
|
|
|
&& vectorEquals(objectSubTypes, other.objectSubTypes);
|
|
|
}
|
|
|
|
|
|
-Goals::TGoalVec CaptureObjectsBehavior::decompose() const
|
|
|
+Goals::TGoalVec CaptureObjectsBehavior::getVisitGoals(const std::vector<AIPath> & paths, const CGObjectInstance * objToVisit)
|
|
|
{
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
|
|
- auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void{
|
|
|
- if(objs.empty())
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- logAi->debug("Scanning objects, count %d", objs.size());
|
|
|
-
|
|
|
- for(auto objToVisit : objs)
|
|
|
- {
|
|
|
-#if AI_TRACE_LEVEL >= 1
|
|
|
- logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
|
|
|
-#endif
|
|
|
-
|
|
|
- if(!shouldVisitObject(objToVisit))
|
|
|
- continue;
|
|
|
+ tasks.reserve(paths.size());
|
|
|
|
|
|
- const int3 pos = objToVisit->visitablePos();
|
|
|
+ const AIPath * closestWay = nullptr;
|
|
|
+ std::vector<ExecuteHeroChain *> waysToVisitObj;
|
|
|
|
|
|
- auto paths = ai->ah->getPathsToTile(pos);
|
|
|
- std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
|
|
- std::shared_ptr<ExecuteHeroChain> closestWay;
|
|
|
-
|
|
|
-#if AI_TRACE_LEVEL >= 1
|
|
|
- logAi->trace("Found %d paths", paths.size());
|
|
|
-#endif
|
|
|
+ for(auto & path : paths)
|
|
|
+ {
|
|
|
+ tasks.push_back(sptr(Goals::Invalid()));
|
|
|
|
|
|
- for(auto & path : paths)
|
|
|
- {
|
|
|
#if AI_TRACE_LEVEL >= 2
|
|
|
- logAi->trace("Path found %s", path.toString());
|
|
|
+ logAi->trace("Path found %s", path.toString());
|
|
|
#endif
|
|
|
|
|
|
- if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
- {
|
|
|
+ if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
+ {
|
|
|
#if AI_TRACE_LEVEL >= 2
|
|
|
- logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
|
|
|
+ logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
|
|
|
#endif
|
|
|
- continue;
|
|
|
- }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- if(!shouldVisit(path.targetHero, objToVisit))
|
|
|
- continue;
|
|
|
+ if(objToVisit && !shouldVisit(path.targetHero, objToVisit))
|
|
|
+ continue;
|
|
|
|
|
|
- auto hero = path.targetHero;
|
|
|
- auto danger = path.getTotalDanger();
|
|
|
+ auto hero = path.targetHero;
|
|
|
+ auto danger = path.getTotalDanger();
|
|
|
|
|
|
- if(ai->ah->getHeroRole(hero) == HeroRole::SCOUT && danger == 0 && path.exchangeCount > 1)
|
|
|
- continue;
|
|
|
+ if(ai->ah->getHeroRole(hero) == HeroRole::SCOUT && danger == 0 && path.exchangeCount > 1)
|
|
|
+ continue;
|
|
|
|
|
|
- auto firstBlockedAction = path.getFirstBlockedAction();
|
|
|
- if(firstBlockedAction)
|
|
|
- {
|
|
|
- auto subGoal = firstBlockedAction->decompose(path.targetHero);
|
|
|
+ auto firstBlockedAction = path.getFirstBlockedAction();
|
|
|
+ if(firstBlockedAction)
|
|
|
+ {
|
|
|
+ auto subGoal = firstBlockedAction->decompose(path.targetHero);
|
|
|
|
|
|
#if AI_TRACE_LEVEL >= 2
|
|
|
- logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
|
|
|
+ logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
|
|
|
#endif
|
|
|
|
|
|
- if(!subGoal->invalid())
|
|
|
- {
|
|
|
- Composition composition;
|
|
|
+ if(!subGoal->invalid())
|
|
|
+ {
|
|
|
+ Composition composition;
|
|
|
+
|
|
|
+ composition.addNext(ExecuteHeroChain(path, objToVisit));
|
|
|
+ composition.addNext(subGoal);
|
|
|
|
|
|
- composition.addNext(ExecuteHeroChain(path, objToVisit));
|
|
|
- composition.addNext(subGoal);
|
|
|
+ tasks[tasks.size() - 1] = sptr(composition);
|
|
|
+ }
|
|
|
|
|
|
- tasks.push_back(sptr(composition));
|
|
|
- }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- continue;
|
|
|
- }
|
|
|
+ auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
|
|
|
|
|
|
- auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
|
|
|
-
|
|
|
#if AI_TRACE_LEVEL >= 2
|
|
|
- logAi->trace(
|
|
|
- "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
|
|
|
- isSafe ? "safe" : "not safe",
|
|
|
- objToVisit->getObjectName(),
|
|
|
- hero->name,
|
|
|
- path.getHeroStrength(),
|
|
|
- danger,
|
|
|
- path.getTotalArmyLoss());
|
|
|
+ logAi->trace(
|
|
|
+ "It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
|
|
|
+ isSafe ? "safe" : "not safe",
|
|
|
+ objToVisit ? objToVisit->getObjectName() : path.targetTile().toString(),
|
|
|
+ hero->name,
|
|
|
+ path.getHeroStrength(),
|
|
|
+ danger,
|
|
|
+ path.getTotalArmyLoss());
|
|
|
#endif
|
|
|
|
|
|
- if(isSafe)
|
|
|
- {
|
|
|
- auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
|
|
|
-
|
|
|
- waysToVisitObj.push_back(newWay);
|
|
|
+ if(isSafe)
|
|
|
+ {
|
|
|
+ auto newWay = new ExecuteHeroChain(path, objToVisit);
|
|
|
+ TSubgoal sharedPtr;
|
|
|
|
|
|
- if(!closestWay || closestWay->getPath().movementCost() > newWay->getPath().movementCost())
|
|
|
- closestWay = newWay;
|
|
|
- }
|
|
|
- }
|
|
|
+ sharedPtr.reset(newWay);
|
|
|
|
|
|
- if(waysToVisitObj.empty())
|
|
|
- continue;
|
|
|
+ if(!closestWay || closestWay->movementCost() > path.movementCost())
|
|
|
+ closestWay = &path;
|
|
|
|
|
|
- for(auto way : waysToVisitObj)
|
|
|
+ if(!ai->nullkiller->arePathHeroesLocked(path))
|
|
|
{
|
|
|
- if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
|
|
|
- continue;
|
|
|
-
|
|
|
- way->closestWayRatio
|
|
|
- = closestWay->getPath().movementCost() / way->getPath().movementCost();
|
|
|
-
|
|
|
- tasks.push_back(sptr(*way));
|
|
|
+ waysToVisitObj.push_back(newWay);
|
|
|
+ tasks[tasks.size() - 1] = sharedPtr;
|
|
|
}
|
|
|
}
|
|
|
- };
|
|
|
-
|
|
|
- if(specificObjects)
|
|
|
- {
|
|
|
- captureObjects(objectsToCapture);
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+ for(auto way : waysToVisitObj)
|
|
|
{
|
|
|
- captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
|
|
|
+ way->closestWayRatio
|
|
|
+ = closestWay->movementCost() / way->getPath().movementCost();
|
|
|
}
|
|
|
|
|
|
return tasks;
|
|
|
}
|
|
|
|
|
|
-bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
|
|
|
+Goals::TGoalVec CaptureObjectsBehavior::decompose() const
|
|
|
{
|
|
|
- const CGObjectInstance* objInstance = obj;
|
|
|
- if(!objInstance)
|
|
|
- return false;
|
|
|
+ Goals::TGoalVec tasks;
|
|
|
|
|
|
- if(objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void{
|
|
|
+ if(objs.empty())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ logAi->debug("Scanning objects, count %d", objs.size());
|
|
|
|
|
|
- if(objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
|
|
|
+ for(auto objToVisit : objs)
|
|
|
+ {
|
|
|
+#if AI_TRACE_LEVEL >= 1
|
|
|
+ logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
|
|
|
+#endif
|
|
|
+
|
|
|
+ if(!shouldVisitObject(objToVisit))
|
|
|
+ continue;
|
|
|
+
|
|
|
+ const int3 pos = objToVisit->visitablePos();
|
|
|
+
|
|
|
+ auto paths = ai->ah->getPathsToTile(pos);
|
|
|
+ std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
|
|
+ std::shared_ptr<ExecuteHeroChain> closestWay;
|
|
|
+
|
|
|
+#if AI_TRACE_LEVEL >= 1
|
|
|
+ logAi->trace("Found %d paths", paths.size());
|
|
|
+#endif
|
|
|
+ vstd::concatenate(tasks, getVisitGoals(paths, objToVisit));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ if(specificObjects)
|
|
|
{
|
|
|
- return false;
|
|
|
+ captureObjects(objectsToCapture);
|
|
|
}
|
|
|
-
|
|
|
- if(isObjectRemovable(obj))
|
|
|
+ else
|
|
|
{
|
|
|
- return true;
|
|
|
+ captureObjects(ai->nullkiller->objectClusterizer->getNearbyObjects());
|
|
|
}
|
|
|
|
|
|
- const int3 pos = objInstance->visitablePos();
|
|
|
+ vstd::erase_if(tasks, [](TSubgoal task) -> bool
|
|
|
+ {
|
|
|
+ return task->invalid();
|
|
|
+ });
|
|
|
+
|
|
|
+ return tasks;
|
|
|
+}
|
|
|
|
|
|
- if(objInstance->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->alreadyVisited, objInstance)
|
|
|
- || obj->wasVisited(ai->playerID))
|
|
|
+bool CaptureObjectsBehavior::shouldVisitObject(const CGObjectInstance * obj) const
|
|
|
+{
|
|
|
+ if(objectTypes.size() && !vstd::contains(objectTypes, obj->ID.num))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
|
|
|
- if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
|
|
|
+ if(objectSubTypes.size() && !vstd::contains(objectSubTypes, obj->subID))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- //it may be hero visiting this obj
|
|
|
- //we don't try visiting object on which allied or owned hero stands
|
|
|
- // -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
|
|
|
- const CGObjectInstance * topObj = cb->getTopObj(pos);
|
|
|
-
|
|
|
- if(!topObj)
|
|
|
- return false; // partly visible obj but its visitable pos is not visible.
|
|
|
-
|
|
|
- if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
|
|
|
- return false;
|
|
|
- else
|
|
|
- return true; //all of the following is met
|
|
|
+ return true;
|
|
|
}
|