BuyArmy.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "../VCAI.h"
  14. #include "../Engine/Nullkiller.h"
  15. extern boost::thread_specific_ptr<CCallback> cb;
  16. extern boost::thread_specific_ptr<VCAI> ai;
  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->name;
  25. }
  26. void BuyArmy::accept(VCAI * ai)
  27. {
  28. ui64 valueBought = 0;
  29. //buy the stacks with largest AI value
  30. auto upgradeSuccessfull = ai->makePossibleUpgrades(town);
  31. auto armyToBuy = ai->nullkiller->armyManager->getArmyAvailableToBuy(town->getUpperArmy(), town);
  32. if(armyToBuy.empty())
  33. {
  34. if(upgradeSuccessfull)
  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 != -1 && ci.creID != objid)
  43. continue;
  44. vstd::amin(ci.count, res / ci.cre->cost);
  45. if(ci.count)
  46. {
  47. cb->recruitCreatures(town, town->getUpperArmy(), ci.creID, ci.count, ci.level);
  48. valueBought += ci.count * ci.cre->AIValue;
  49. }
  50. }
  51. if(!valueBought)
  52. {
  53. throw cannotFulfillGoalException("No creatures to buy.");
  54. }
  55. if(town->visitingHero)
  56. {
  57. ai->moveHeroToTile(town->visitablePos(), town->visitingHero.get());
  58. }
  59. }