diff --git a/AI/VCAI/AIUtility.cpp b/AI/VCAI/AIUtility.cpp index b8b710dc4..d20bfea15 100644 --- a/AI/VCAI/AIUtility.cpp +++ b/AI/VCAI/AIUtility.cpp @@ -528,11 +528,18 @@ creInfo infoFromDC(const dwellingContent & dc) ui64 howManyReinforcementsCanBuy(const CArmedInstance * h, const CGDwelling * t) { ui64 aivalue = 0; - + TResources availableRes = cb->getResourceAmount(); int freeHeroSlots = GameConstants::ARMY_SIZE - h->stacksCount(); + for(auto const dc : t->creatures) { creInfo ci = infoFromDC(dc); + + if(!ci.count || ci.creID == -1) + continue; + + vstd::amin(ci.count, availableRes / ci.cre->cost); //max count we can afford + if(ci.count && ci.creID != -1) //valid creature at this level { //can be merged with another stack? @@ -547,6 +554,7 @@ ui64 howManyReinforcementsCanBuy(const CArmedInstance * h, const CGDwelling * t) //we found matching occupied or free slot aivalue += ci.count * ci.cre->AIValue; + availableRes -= ci.cre->cost * ci.count; } } diff --git a/AI/VCAI/Goals/GatherArmy.cpp b/AI/VCAI/Goals/GatherArmy.cpp index 3aedd8ec3..158da808c 100644 --- a/AI/VCAI/Goals/GatherArmy.cpp +++ b/AI/VCAI/Goals/GatherArmy.cpp @@ -79,12 +79,6 @@ TGoalVec GatherArmy::getAllPossibleSubgoals() int val = *std::min_element(values.begin(), values.end()); - logAi->trace( - "Army value need %i, to hero %i, to town %i", - value, - (int)howManyReinforcementsCanBuy(hero.get(), t), - (int)howManyReinforcementsCanBuy(t->getUpperArmy(), t)); - if (val) { auto goal = sptr(BuyArmy(t, val).sethero(hero)); diff --git a/AI/VCAI/Goals/GatherTroops.cpp b/AI/VCAI/Goals/GatherTroops.cpp index 59242e5b7..10b15147d 100644 --- a/AI/VCAI/Goals/GatherTroops.cpp +++ b/AI/VCAI/Goals/GatherTroops.cpp @@ -81,7 +81,15 @@ TGoalVec GatherTroops::getAllPossibleSubgoals() if(count >= this->value) { - vstd::concatenate(solutions, ai->ah->howToVisitObj(t)); + if(t->visitingHero) + { + solutions.push_back(sptr(VisitObj(t->id.getNum()).sethero(t->visitingHero.get()))); + } + else + { + vstd::concatenate(solutions, ai->ah->howToVisitObj(t)); + } + continue; } @@ -97,7 +105,7 @@ TGoalVec GatherTroops::getAllPossibleSubgoals() continue; BuildingID bid(BuildingID::DWELL_FIRST + creature->level - 1 + upgradeNumber * GameConstants::CREATURES_PER_TOWN); - if(t->hasBuilt(bid)) //this assumes only creatures with dwellings are assigned to faction + if(t->hasBuilt(bid) && ai->ah->freeResources().canAfford(creature->cost)) //this assumes only creatures with dwellings are assigned to faction { solutions.push_back(sptr(BuyArmy(t, creature->AIValue * this->value).setobjid(objid))); }