AdventureSpellCast.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. extern boost::thread_specific_ptr<CCallback> cb;
  16. extern boost::thread_specific_ptr<AIGateway> ai;
  17. using namespace Goals;
  18. bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const
  19. {
  20. return hero.h == other.hero.h;
  21. }
  22. void AdventureSpellCast::accept(AIGateway * ai)
  23. {
  24. if(!hero.validAndSet())
  25. throw cannotFulfillGoalException("Invalid hero!");
  26. auto spell = getSpell();
  27. logAi->trace("Decomposing adventure spell cast of %s for hero %s", spell->getNameTranslated(), hero->getNameTranslated());
  28. if(!spell->isAdventure())
  29. throw cannotFulfillGoalException(spell->getNameTranslated() + " is not an adventure spell.");
  30. if(!hero->canCastThisSpell(spell))
  31. throw cannotFulfillGoalException("Hero can not cast " + spell->getNameTranslated());
  32. if(hero->mana < hero->getSpellCost(spell))
  33. throw cannotFulfillGoalException("Hero has not enough mana to cast " + spell->getNameTranslated());
  34. if(spellID == SpellID::TOWN_PORTAL && town && town->visitingHero)
  35. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->getNameTranslated());
  36. if(town && spellID == SpellID::TOWN_PORTAL)
  37. {
  38. ai->selectedObject = town->id;
  39. }
  40. auto wait = cb->waitTillRealize;
  41. cb->waitTillRealize = true;
  42. cb->castSpell(hero.h, spellID, tile);
  43. if(town && spellID == SpellID::TOWN_PORTAL)
  44. {
  45. // visit town
  46. ai->moveHeroToTile(town->visitablePos(), hero);
  47. }
  48. cb->waitTillRealize = wait;
  49. throw goalFulfilledException(sptr(*this));
  50. }
  51. std::string AdventureSpellCast::toString() const
  52. {
  53. return "AdventureSpellCast " + spellID.toSpell()->getNameTranslated();
  54. }
  55. }