AdventureSpellCast.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.h == other.hero.h;
  19. }
  20. void AdventureSpellCast::accept(AIGateway * ai)
  21. {
  22. if(!hero.validAndSet())
  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->visitingHero && town->tempOwner == ai->playerID && !town->getUpperArmy()->stacksCount())
  36. {
  37. ai->myCb->swapGarrisonHero(town);
  38. }
  39. if(town->visitingHero)
  40. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->getNameTranslated());
  41. }
  42. auto wait = cb->waitTillRealize;
  43. cb->waitTillRealize = true;
  44. cb->castSpell(hero.h, spellID, tile);
  45. if(town && spellID == SpellID::TOWN_PORTAL)
  46. {
  47. // visit town
  48. ai->moveHeroToTile(town->visitablePos(), hero);
  49. }
  50. cb->waitTillRealize = wait;
  51. throw goalFulfilledException(sptr(*this));
  52. }
  53. std::string AdventureSpellCast::toString() const
  54. {
  55. return "AdventureSpellCast " + spellID.toSpell()->getNameTranslated();
  56. }
  57. }