AdventureSpellCast.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/mapping/CMap.h" //for victory conditions
  16. #include "../../../lib/CPathfinder.h"
  17. extern boost::thread_specific_ptr<CCallback> cb;
  18. extern boost::thread_specific_ptr<VCAI> ai;
  19. extern FuzzyHelper * fh;
  20. using namespace Goals;
  21. bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const
  22. {
  23. return hero.h == other.hero.h;
  24. }
  25. TSubgoal AdventureSpellCast::whatToDoToAchieve()
  26. {
  27. if(!hero.validAndSet())
  28. throw cannotFulfillGoalException("Invalid hero!");
  29. auto spell = getSpell();
  30. logAi->trace("Decomposing adventure spell cast of %s for hero %s", spell->getName(), hero->name);
  31. if(!spell->isAdventure())
  32. throw cannotFulfillGoalException(spell->getName() + " is not an adventure spell.");
  33. if(!hero->canCastThisSpell(spell))
  34. throw cannotFulfillGoalException("Hero can not cast " + spell->getName());
  35. if(hero->mana < hero->getSpellCost(spell))
  36. throw cannotFulfillGoalException("Hero has not enough mana to cast " + spell->getName());
  37. if(spellID == SpellID::TOWN_PORTAL && town && town->visitingHero)
  38. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->name);
  39. return iAmElementar();
  40. }
  41. void AdventureSpellCast::accept(VCAI * ai)
  42. {
  43. if(town && spellID == SpellID::TOWN_PORTAL)
  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 && spellID == SpellID::TOWN_PORTAL)
  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()->getName();
  61. }
  62. std::string AdventureSpellCast::completeMessage() const
  63. {
  64. return "Spell cast successfully " + getSpell()->getName();
  65. }