AdventureSpellCast.cpp 1.9 KB

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