2
0

BuyArmy.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * BuyArmy.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "../StdInc.h"
  11. #include "BuyArmy.h"
  12. #include "../../../lib/mapObjects/CGTownInstance.h"
  13. #include "../AIGateway.h"
  14. #include "../Engine/Nullkiller.h"
  15. namespace NKAI
  16. {
  17. using namespace Goals;
  18. bool BuyArmy::operator==(const BuyArmy & other) const
  19. {
  20. return town == other.town && objid == other.objid;
  21. }
  22. std::string BuyArmy::toString() const
  23. {
  24. return "Buy army at " + town->getNameTranslated();
  25. }
  26. void BuyArmy::accept(AIGateway * ai)
  27. {
  28. ui64 valueBought = 0;
  29. //buy the stacks with largest AI value
  30. auto upgradeSuccessful = ai->makePossibleUpgrades(town);
  31. auto armyToBuy = ai->nullkiller->armyManager->getArmyAvailableToBuy(town->getUpperArmy(), town);
  32. if(armyToBuy.empty())
  33. {
  34. if(upgradeSuccessful)
  35. return;
  36. throw cannotFulfillGoalException("No creatures to buy.");
  37. }
  38. for(int i = 0; valueBought < value && i < armyToBuy.size(); i++)
  39. {
  40. auto res = cb->getResourceAmount();
  41. auto & ci = armyToBuy[i];
  42. if(objid != CreatureID::NONE && ci.creID.getNum() != objid)
  43. continue;
  44. vstd::amin(ci.count, res / ci.creID.toCreature()->getFullRecruitCost());
  45. if(ci.count)
  46. {
  47. if (town->getUpperArmy()->stacksCount() == GameConstants::ARMY_SIZE)
  48. {
  49. SlotID lowestValueSlot;
  50. int lowestValue = std::numeric_limits<int>::max();
  51. for (const auto & slot : town->getUpperArmy()->Slots())
  52. {
  53. if (slot.second->getCreatureID() != CreatureID::NONE)
  54. {
  55. int currentStackMarketValue =
  56. slot.second->getCreatureID().toCreature()->getFullRecruitCost().marketValue() * slot.second->getCount();
  57. if (slot.second->getCreatureID().toCreature()->getFactionID() == town->getFactionID())
  58. continue;
  59. if (currentStackMarketValue < lowestValue)
  60. {
  61. lowestValue = currentStackMarketValue;
  62. lowestValueSlot = slot.first;
  63. }
  64. }
  65. }
  66. if (lowestValueSlot.validSlot())
  67. {
  68. cb->dismissCreature(town->getUpperArmy(), lowestValueSlot);
  69. }
  70. }
  71. if (town->getUpperArmy()->stacksCount() < GameConstants::ARMY_SIZE || town->getUpperArmy()->getSlotFor(ci.creID).validSlot()) //It is possible we don't scrap despite we wanted to due to not scrapping stacks that fit our faction
  72. {
  73. cb->recruitCreatures(town, town->getUpperArmy(), ci.creID, ci.count, ci.level);
  74. }
  75. valueBought += ci.count * ci.creID.toCreature()->getAIValue();
  76. }
  77. }
  78. if(!valueBought)
  79. {
  80. throw cannotFulfillGoalException("No creatures to buy.");
  81. }
  82. if(town->getVisitingHero() && !town->getGarrisonHero())
  83. {
  84. ai->moveHeroToTile(town->visitablePos(), town->getVisitingHero());
  85. }
  86. }
  87. }