AdventureSpellCast.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "../../../lib/CPathfinder.h"
  14. namespace NKAI
  15. {
  16. extern boost::thread_specific_ptr<CCallback> cb;
  17. extern boost::thread_specific_ptr<AIGateway> ai;
  18. using namespace Goals;
  19. bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const
  20. {
  21. return hero.h == other.hero.h;
  22. }
  23. void AdventureSpellCast::accept(AIGateway * ai)
  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. if(spellID == SpellID::TOWN_PORTAL && town && town->visitingHero)
  36. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->getNameTranslated());
  37. if(town && spellID == SpellID::TOWN_PORTAL)
  38. {
  39. ai->selectedObject = town->id;
  40. }
  41. auto wait = cb->waitTillRealize;
  42. cb->waitTillRealize = true;
  43. cb->castSpell(hero.h, spellID, tile);
  44. if(town && spellID == SpellID::TOWN_PORTAL)
  45. {
  46. // visit town
  47. ai->moveHeroToTile(town->visitablePos(), hero);
  48. }
  49. cb->waitTillRealize = wait;
  50. throw goalFulfilledException(sptr(*this));
  51. }
  52. std::string AdventureSpellCast::toString() const
  53. {
  54. return "AdventureSpellCast " + spellID.toSpell()->getNameTranslated();
  55. }
  56. }