From f76c6c2300f0d40c5055356917063f5e0f8b9480 Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Sat, 16 Feb 2019 10:33:24 +0200 Subject: [PATCH] Remove a few more usages of thread shared ai and cb --- AI/VCAI/AIUtility.cpp | 6 ------ AI/VCAI/FuzzyHelper.cpp | 19 +++++++++++++------ AI/VCAI/FuzzyHelper.h | 4 ++-- AI/VCAI/Goals/VisitHero.cpp | 2 +- AI/VCAI/Pathfinding/AINodeStorage.cpp | 5 +++-- AI/VCAI/Pathfinding/AINodeStorage.h | 5 +++-- AI/VCAI/Pathfinding/AIPathfinder.cpp | 5 ++--- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/AI/VCAI/AIUtility.cpp b/AI/VCAI/AIUtility.cpp index 78818934f..efe694952 100644 --- a/AI/VCAI/AIUtility.cpp +++ b/AI/VCAI/AIUtility.cpp @@ -194,12 +194,6 @@ bool CDistanceSorter::operator()(const CGObjectInstance * lhs, const CGObjectIns return ln->cost < rn->cost; } - -bool compareDanger(const CGObjectInstance * lhs, const CGObjectInstance * rhs) -{ - return fh->evaluateDanger(lhs) < fh->evaluateDanger(rhs); -} - bool isSafeToVisit(HeroPtr h, crint3 tile) { return isSafeToVisit(h, fh->evaluateDanger(tile, h.get())); diff --git a/AI/VCAI/FuzzyHelper.cpp b/AI/VCAI/FuzzyHelper.cpp index 288b435b5..b4457fd98 100644 --- a/AI/VCAI/FuzzyHelper.cpp +++ b/AI/VCAI/FuzzyHelper.cpp @@ -207,11 +207,12 @@ void FuzzyHelper::setPriority(Goals::TSubgoal & g) //calls evaluate - Visitor pa ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor) { - return evaluateDanger(tile, visitor, cb.get()); + return evaluateDanger(tile, visitor, ai.get()); } -ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const CPlayerSpecificInfoCallback * cb) +ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai) { + auto cb = ai->myCb; const TerrainTile * t = cb->getTile(tile, false); if(!t) //we can know about guard but can't check its tile (the edge of fow) return 190000000; //MUCH @@ -231,7 +232,7 @@ ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, co if(const CGObjectInstance * dangerousObject = vstd::backOrNull(visitableObjects)) { - objectDanger = evaluateDanger(dangerousObject); //unguarded objects can also be dangerous or unhandled + objectDanger = evaluateDanger(dangerousObject, ai); //unguarded objects can also be dangerous or unhandled if(objectDanger) { //TODO: don't downcast objects AI shouldn't know about! @@ -251,7 +252,9 @@ ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, co auto guards = cb->getGuardingCreatures(it->second->visitablePos()); for(auto cre : guards) { - vstd::amax(guardDanger, evaluateDanger(cre) * tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast(cre))); + float tacticalAdvantage = tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast(cre)); + + vstd::amax(guardDanger, evaluateDanger(cre, ai) * tacticalAdvantage); } } } @@ -260,15 +263,19 @@ ui64 FuzzyHelper::evaluateDanger(crint3 tile, const CGHeroInstance * visitor, co auto guards = cb->getGuardingCreatures(tile); for(auto cre : guards) { - vstd::amax(guardDanger, evaluateDanger(cre) * tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast(cre))); //we are interested in strongest monster around + float tacticalAdvantage = tacticalAdvantageEngine.getTacticalAdvantage(visitor, dynamic_cast(cre)); + + vstd::amax(guardDanger, evaluateDanger(cre, ai) * tacticalAdvantage); //we are interested in strongest monster around } //TODO mozna odwiedzic blockvis nie ruszajac straznika return std::max(objectDanger, guardDanger); } -ui64 FuzzyHelper::evaluateDanger(const CGObjectInstance * obj) +ui64 FuzzyHelper::evaluateDanger(const CGObjectInstance * obj, const VCAI * ai) { + auto cb = ai->myCb; + if(obj->tempOwner < PlayerColor::PLAYER_LIMIT && cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES) //owned or allied objects don't pose any threat return 0; diff --git a/AI/VCAI/FuzzyHelper.h b/AI/VCAI/FuzzyHelper.h index 679c4f6d3..036374acb 100644 --- a/AI/VCAI/FuzzyHelper.h +++ b/AI/VCAI/FuzzyHelper.h @@ -43,7 +43,7 @@ public: Goals::TSubgoal chooseSolution(Goals::TGoalVec vec); //std::shared_ptr chooseSolution (std::vector> & vec); - ui64 evaluateDanger(const CGObjectInstance * obj); - ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const CPlayerSpecificInfoCallback * cb); + ui64 evaluateDanger(const CGObjectInstance * obj, const VCAI * ai); + ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor, const VCAI * ai); ui64 evaluateDanger(crint3 tile, const CGHeroInstance * visitor); }; diff --git a/AI/VCAI/Goals/VisitHero.cpp b/AI/VCAI/Goals/VisitHero.cpp index 701ac0d71..f0498db58 100644 --- a/AI/VCAI/Goals/VisitHero.cpp +++ b/AI/VCAI/Goals/VisitHero.cpp @@ -45,7 +45,7 @@ TSubgoal VisitHero::whatToDoToAchieve() if(hero && ai->isAccessibleForHero(pos, hero, true) && isSafeToVisit(hero, pos)) //enemy heroes can get reinforcements { - if(hero->pos == pos) + if(hero->visitablePos() == pos) logAi->error("Hero %s tries to visit himself.", hero.name); else { diff --git a/AI/VCAI/Pathfinding/AINodeStorage.cpp b/AI/VCAI/Pathfinding/AINodeStorage.cpp index 42aac3e89..95af9d144 100644 --- a/AI/VCAI/Pathfinding/AINodeStorage.cpp +++ b/AI/VCAI/Pathfinding/AINodeStorage.cpp @@ -185,10 +185,11 @@ std::vector AINodeStorage::calculateNeighbours( return neighbours; } -void AINodeStorage::setHero(HeroPtr heroPtr, const CPlayerSpecificInfoCallback * _cb) +void AINodeStorage::setHero(HeroPtr heroPtr, const VCAI * _ai) { hero = heroPtr.get(); - cb = _cb; + cb = _ai->myCb.get(); + ai = _ai; } std::vector AINodeStorage::calculateTeleportations( diff --git a/AI/VCAI/Pathfinding/AINodeStorage.h b/AI/VCAI/Pathfinding/AINodeStorage.h index 6c773bbb9..6130e4e10 100644 --- a/AI/VCAI/Pathfinding/AINodeStorage.h +++ b/AI/VCAI/Pathfinding/AINodeStorage.h @@ -62,6 +62,7 @@ private: /// 1-3 - position on map, 4 - layer (air, water, land), 5 - chain (normal, battle, spellcast and combinations) boost::multi_array nodes; const CPlayerSpecificInfoCallback * cb; + const VCAI * ai; const CGHeroInstance * hero; std::unique_ptr dangerEvaluator; @@ -106,7 +107,7 @@ public: std::vector getChainInfo(const int3 & pos, bool isOnLand) const; bool isTileAccessible(const int3 & pos, const EPathfindingLayer layer) const; - void setHero(HeroPtr heroPtr, const CPlayerSpecificInfoCallback * cb); + void setHero(HeroPtr heroPtr, const VCAI * ai); const CGHeroInstance * getHero() const { @@ -115,7 +116,7 @@ public: uint64_t evaluateDanger(const int3 & tile) const { - return dangerEvaluator->evaluateDanger(tile, hero, cb); + return dangerEvaluator->evaluateDanger(tile, hero, ai); } private: diff --git a/AI/VCAI/Pathfinding/AIPathfinder.cpp b/AI/VCAI/Pathfinding/AIPathfinder.cpp index de296ab3c..4c9e6bfac 100644 --- a/AI/VCAI/Pathfinding/AIPathfinder.cpp +++ b/AI/VCAI/Pathfinding/AIPathfinder.cpp @@ -62,7 +62,6 @@ void AIPathfinder::updatePaths(std::vector heroes) std::vector calculationTasks; - // TODO: go parallel? for(HeroPtr hero : heroes) { std::shared_ptr nodeStorage; @@ -78,7 +77,7 @@ void AIPathfinder::updatePaths(std::vector heroes) } storageMap[hero] = nodeStorage; - nodeStorage->setHero(hero, cb); + nodeStorage->setHero(hero, ai); auto config = std::make_shared(cb, ai, nodeStorage); @@ -121,7 +120,7 @@ void AIPathfinder::updatePaths(const HeroPtr & hero) } storageMap[hero] = nodeStorage; - nodeStorage->setHero(hero, cb); + nodeStorage->setHero(hero, ai); } else {