AdventureSpellCast.cpp 2.3 KB

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