Added encapsulation for movement points access

This commit is contained in:
Ivan Savenko
2023-06-25 17:42:36 +03:00
parent edf7756783
commit 08cfbe79cf
24 changed files with 86 additions and 74 deletions
+2 -2
View File
@@ -802,8 +802,8 @@ void AIGateway::makeTurn()
//for debug purpose
for (auto h : cb->getHeroesInfo())
{
if (h->movement)
logAi->warn("Hero %s has %d MP left", h->getNameTranslated(), h->movement);
if (h->movementPointsRemaining())
logAi->warn("Hero %s has %d MP left", h->getNameTranslated(), h->movementPointsRemaining());
}
#if NKAI_TRACE_LEVEL == 0
}
+1 -1
View File
@@ -187,7 +187,7 @@ void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInsta
if(ai->nullkiller->isHeroLocked(existingHero)
|| existingHero->getArmyStrength() > hero->getArmyStrength()
|| ai->nullkiller->heroManager->getHeroRole(existingHero) == HeroRole::MAIN
|| existingHero->movement
|| existingHero->movementPointsRemaining()
|| existingHero->artifactsWorn.size() > (existingHero->hasSpellbook() ? 2 : 1))
continue;
+1 -1
View File
@@ -206,7 +206,7 @@ Goals::TGoalVec StartupBehavior::decompose() const
for(const CGTownInstance * town : towns)
{
if(town->garrisonHero
&& town->garrisonHero->movement
&& town->garrisonHero->movementPointsRemaining()
&& !town->visitingHero
&& ai->nullkiller->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE)
{
+1 -1
View File
@@ -790,7 +790,7 @@ public:
if(garrisonHero && swapCommand.getLockingReason() == HeroLockedReason::DEFENCE)
{
auto defenderRole = evaluationContext.evaluator.ai->heroManager->getHeroRole(garrisonHero);
auto mpLeft = garrisonHero->movement / (float)garrisonHero->maxMovePoints(true);
auto mpLeft = garrisonHero->movementPointsRemaining() / (float)garrisonHero->movementPointsLimit(true);
evaluationContext.movementCost += mpLeft;
evaluationContext.movementCostByRole[defenderRole] += mpLeft;
+4 -4
View File
@@ -78,7 +78,7 @@ void ExecuteHeroChain::accept(AIGateway * ai)
try
{
if(hero->movement)
if(hero->movementPointsRemaining() > 0)
{
ai->nullkiller->setActive(hero, node.coord);
@@ -117,7 +117,7 @@ void ExecuteHeroChain::accept(AIGateway * ai)
}
}
if(hero->movement)
if(hero->movementPointsRemaining())
{
try
{
@@ -135,14 +135,14 @@ void ExecuteHeroChain::accept(AIGateway * ai)
return;
}
if(hero->movement > 0)
if(hero->movementPointsRemaining() > 0)
{
CGPath path;
bool isOk = cb->getPathsInfo(hero)->getPath(path, node.coord);
if(isOk && path.nodes.back().turns > 0)
{
logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movement, node.coord.toString());
logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero->getNameTranslated(), hero->movementPointsRemaining(), node.coord.toString());
ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
return;
+2 -2
View File
@@ -888,7 +888,7 @@ void AINodeStorage::setHeroes(std::map<const CGHeroInstance *, HeroRole> heroes)
if(actor->hero->tempOwner != ai->playerID)
{
bool onLand = !actor->hero->boat || actor->hero->boat->layer != EPathfindingLayer::SAIL;
actor->initialMovement = actor->hero->maxMovePoints(onLand);
actor->initialMovement = actor->hero->movementPointsLimit(onLand);
}
playerID = actor->hero->tempOwner;
@@ -1053,7 +1053,7 @@ struct TowmPortalFinder
return std::nullopt;
AIPathNode * node = nodeOptional.value();
float movementCost = (float)movementNeeded / (float)hero->maxMovePoints(EPathfindingLayer::LAND);
float movementCost = (float)movementNeeded / (float)hero->movementPointsLimit(EPathfindingLayer::LAND);
movementCost += bestNode->getCost();
+2 -2
View File
@@ -43,7 +43,7 @@ ChainActor::ChainActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t
{
initialPosition = hero->visitablePos();
layer = hero->boat ? hero->boat->layer : EPathfindingLayer::LAND;
initialMovement = hero->movement;
initialMovement = hero->movementPointsRemaining();
initialTurn = 0;
armyValue = hero->getArmyStrength();
heroFightingStrength = hero->getFightingStrength();
@@ -75,7 +75,7 @@ int ChainActor::maxMovePoints(CGPathNode::ELayer layer)
throw std::logic_error("Asking movement points for static actor");
#endif
return hero->maxMovePointsCached(layer, tiCache.get());
return hero->movementPointsLimitCached(layer, tiCache.get());
}
std::string ChainActor::toString() const
+1 -1
View File
@@ -267,7 +267,7 @@ TGoalVec Explore::getAllPossibleSubgoals()
if(!ai->isAbleToExplore(h))
return true;
return !h->movement; //saves time, immobile heroes are useless anyway
return !h->movementPointsRemaining(); //saves time, immobile heroes are useless anyway
});
}
+2 -2
View File
@@ -113,7 +113,7 @@ std::vector<CGPathNode *> AINodeStorage::getInitialNodes()
auto initialNode = getOrCreateNode(hpos, hero->boat ? EPathfindingLayer::SAIL : EPathfindingLayer::LAND, NORMAL_CHAIN).value();
initialNode->turns = 0;
initialNode->moveRemains = hero->movement;
initialNode->moveRemains = hero->movementPointsRemaining();
initialNode->danger = 0;
initialNode->setCost(0.0);
@@ -245,7 +245,7 @@ void AINodeStorage::calculateTownPortalTeleportations(
auto skillLevel = hero->getSpellSchoolLevel(townPortal);
auto movementCost = GameConstants::BASE_MOVEMENT_COST * (skillLevel >= 3 ? 2 : 3);
if(hero->movement < movementCost)
if(hero->movementPointsRemaining() < movementCost)
{
return;
}
+9 -9
View File
@@ -813,8 +813,8 @@ void VCAI::makeTurn()
//for debug purpose
for (auto h : cb->getHeroesInfo())
{
if (h->movement)
logAi->warn("Hero %s has %d MP left", h->getNameTranslated(), h->movement);
if (h->movementPointsRemaining())
logAi->warn("Hero %s has %d MP left", h->getNameTranslated(), h->movementPointsRemaining());
}
}
catch (boost::thread_interrupted & e)
@@ -949,7 +949,7 @@ void VCAI::mainLoop()
if (bestGoal->hero) //lock this hero to fulfill goal
{
setGoal(bestGoal->hero, bestGoal);
if (!bestGoal->hero->movement || vstd::contains(invalidPathHeroes, bestGoal->hero))
if (!bestGoal->hero->movementPointsRemaining() || vstd::contains(invalidPathHeroes, bestGoal->hero))
{
if (!vstd::erase_if_present(possibleGoals, bestGoal))
{
@@ -1354,7 +1354,7 @@ void VCAI::wander(HeroPtr h)
TimeCheck tc("looking for wander destination");
while(h->movement)
while(h->movementPointsRemaining())
{
validateVisitableObjs();
ah->updatePaths(getMyHeroes());
@@ -2031,7 +2031,7 @@ void VCAI::tryRealize(Goals::RecruitHero & g)
void VCAI::tryRealize(Goals::VisitTile & g)
{
if(!g.hero->movement)
if(!g.hero->movementPointsRemaining())
throw cannotFulfillGoalException("Cannot visit tile: hero is out of MPs!");
if(g.tile == g.hero->visitablePos() && cb->getVisitableObjs(g.hero->visitablePos()).size() < 2)
{
@@ -2047,7 +2047,7 @@ void VCAI::tryRealize(Goals::VisitTile & g)
void VCAI::tryRealize(Goals::VisitObj & g)
{
auto position = g.tile;
if(!g.hero->movement)
if(!g.hero->movementPointsRemaining())
throw cannotFulfillGoalException("Cannot visit object: hero is out of MPs!");
if(position == g.hero->visitablePos() && cb->getVisitableObjs(g.hero->visitablePos()).size() < 2)
{
@@ -2062,7 +2062,7 @@ void VCAI::tryRealize(Goals::VisitObj & g)
void VCAI::tryRealize(Goals::VisitHero & g)
{
if(!g.hero->movement)
if(!g.hero->movementPointsRemaining())
throw cannotFulfillGoalException("Cannot visit target hero: hero is out of MPs!");
const CGObjectInstance * obj = cb->getObj(ObjectInstanceID(g.objid));
@@ -2263,7 +2263,7 @@ bool VCAI::canAct(HeroPtr h) const
return false;
}
return h->movement;
return h->movementPointsRemaining();
}
HeroPtr VCAI::primaryHero() const
@@ -2412,7 +2412,7 @@ void VCAI::performTypicalActions()
if(!h) //hero might be lost. getUnblockedHeroes() called once on start of turn
continue;
logAi->debug("Hero %s started wandering, MP=%d", h->getNameTranslated(), h->movement);
logAi->debug("Hero %s started wandering, MP=%d", h->getNameTranslated(), h->movementPointsRemaining());
makePossibleUpgrades(*h);
pickBestArtifacts(*h);
try