AdventureSpellCast.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * AdventureSpellCast.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 "AdventureSpellCast.h"
  12. #include "../AIGateway.h"
  13. namespace NKAI
  14. {
  15. using namespace Goals;
  16. bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const
  17. {
  18. return hero == other.hero;
  19. }
  20. void AdventureSpellCast::accept(AIGateway * ai)
  21. {
  22. if(!hero)
  23. throw cannotFulfillGoalException("Invalid hero!");
  24. auto spell = getSpell();
  25. logAi->trace("Decomposing adventure spell cast of %s for hero %s", spell->getNameTranslated(), hero->getNameTranslated());
  26. if(!spell->isAdventure())
  27. throw cannotFulfillGoalException(spell->getNameTranslated() + " is not an adventure spell.");
  28. if(!hero->canCastThisSpell(spell))
  29. throw cannotFulfillGoalException("Hero can not cast " + spell->getNameTranslated());
  30. if(hero->mana < hero->getSpellCost(spell))
  31. throw cannotFulfillGoalException("Hero has not enough mana to cast " + spell->getNameTranslated());
  32. if(town && spellID == SpellID::TOWN_PORTAL)
  33. {
  34. ai->selectedObject = town->id;
  35. if(town->getVisitingHero() && town->tempOwner == ai->playerID && !town->getUpperArmy()->stacksCount())
  36. {
  37. ai->myCb->swapGarrisonHero(town);
  38. }
  39. if(town->getVisitingHero())
  40. throw cannotFulfillGoalException("The town is already occupied by " + town->getVisitingHero()->getNameTranslated());
  41. }
  42. if (hero->isGarrisoned())
  43. ai->myCb->swapGarrisonHero(hero->getVisitedTown());
  44. auto wait = cb->waitTillRealize;
  45. cb->waitTillRealize = true;
  46. cb->castSpell(hero, spellID, tile);
  47. if(town && spellID == SpellID::TOWN_PORTAL)
  48. {
  49. // visit town
  50. ai->moveHeroToTile(town->visitablePos(), hero);
  51. }
  52. cb->waitTillRealize = wait;
  53. throw goalFulfilledException(sptr(*this));
  54. }
  55. std::string AdventureSpellCast::toString() const
  56. {
  57. return "AdventureSpellCast " + spellID.toSpell()->getNameTranslated();
  58. }
  59. }