Browse Source

AI: fix one more freeze in buy army

Andrii Danylchenko 6 years ago
parent
commit
f36ac1a6e2
3 changed files with 19 additions and 9 deletions
  1. 9 1
      AI/VCAI/AIUtility.cpp
  2. 0 6
      AI/VCAI/Goals/GatherArmy.cpp
  3. 10 2
      AI/VCAI/Goals/GatherTroops.cpp

+ 9 - 1
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;
 		}
 	}
 

+ 0 - 6
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));

+ 10 - 2
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)));
 			}