BuyArmy.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "../FuzzyHelper.h"
  13. #include "../AIhelper.h"
  14. #include "../../../lib/mapObjects/CGTownInstance.h"
  15. extern boost::thread_specific_ptr<CCallback> cb;
  16. extern boost::thread_specific_ptr<VCAI> ai;
  17. extern FuzzyHelper * fh;
  18. using namespace Goals;
  19. bool BuyArmy::operator==(const BuyArmy & other) const
  20. {
  21. return town == other.town && objid == other.objid;
  22. }
  23. std::string BuyArmy::toString() const
  24. {
  25. return "Buy army at " + town->name;
  26. }
  27. void BuyArmy::accept(VCAI * ai)
  28. {
  29. ui64 valueBought = 0;
  30. //buy the stacks with largest AI value
  31. auto upgradeSuccessfull = ai->makePossibleUpgrades(town);
  32. auto armyToBuy = ai->ah->getArmyAvailableToBuy(town->getUpperArmy(), town);
  33. if(armyToBuy.empty())
  34. {
  35. if(upgradeSuccessfull)
  36. return;
  37. throw cannotFulfillGoalException("No creatures to buy.");
  38. }
  39. for(int i = 0; valueBought < value && i < armyToBuy.size(); i++)
  40. {
  41. auto res = cb->getResourceAmount();
  42. auto & ci = armyToBuy[i];
  43. if(objid != -1 && ci.creID != objid)
  44. continue;
  45. vstd::amin(ci.count, res / ci.cre->cost);
  46. if(ci.count)
  47. {
  48. cb->recruitCreatures(town, town->getUpperArmy(), ci.creID, ci.count, ci.level);
  49. valueBought += ci.count * ci.cre->AIValue;
  50. }
  51. }
  52. if(!valueBought)
  53. {
  54. throw cannotFulfillGoalException("No creatures to buy.");
  55. }
  56. if(town->visitingHero)
  57. {
  58. ai->moveHeroToTile(town->visitablePos(), town->visitingHero.get());
  59. }
  60. }