AdventureSpellCast.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(spellID == SpellID::TOWN_PORTAL && town && town->visitingHero)
  33. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->getNameTranslated());
  34. if(town && spellID == SpellID::TOWN_PORTAL)
  35. {
  36. ai->selectedObject = town->id;
  37. }
  38. auto wait = cb->waitTillRealize;
  39. cb->waitTillRealize = true;
  40. cb->castSpell(hero.h, spellID, tile);
  41. if(town && spellID == SpellID::TOWN_PORTAL)
  42. {
  43. // visit town
  44. ai->moveHeroToTile(town->visitablePos(), hero);
  45. }
  46. cb->waitTillRealize = wait;
  47. throw goalFulfilledException(sptr(*this));
  48. }
  49. std::string AdventureSpellCast::toString() const
  50. {
  51. return "AdventureSpellCast " + spellID.toSpell()->getNameTranslated();
  52. }
  53. }