1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Merge pull request #533 from nullkiller/AI-fix-freeze-on-buyarmy

AI: fix one more freeze in buy army
This commit is contained in:
Alexander Shishkin 2019-01-07 05:34:48 +03:00 committed by GitHub
commit a1ff355927
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -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;
}
}

View File

@ -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));

View File

@ -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)));
}