AdventureSpellCast.cpp 2.1 KB

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