AdventureSpellCast.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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->name, hero->name);
  31. if(!spell->isAdventureSpell())
  32. throw cannotFulfillGoalException(spell->name + " is not an adventure spell.");
  33. if(!hero->canCastThisSpell(spell))
  34. throw cannotFulfillGoalException("Hero can not cast " + spell->name);
  35. if(hero->mana < hero->getSpellCost(spell))
  36. throw cannotFulfillGoalException("Hero has not enough mana to cast " + spell->name);
  37. return iAmElementar();
  38. }
  39. void AdventureSpellCast::accept(VCAI * ai)
  40. {
  41. if(town && spellID == SpellID::TOWN_PORTAL)
  42. {
  43. ai->selectedObject = town->id;
  44. }
  45. auto wait = cb->waitTillRealize;
  46. cb->waitTillRealize = true;
  47. cb->castSpell(hero.h, spellID, tile);
  48. if(town && spellID == SpellID::TOWN_PORTAL)
  49. {
  50. // visit town
  51. ai->moveHeroToTile(town->visitablePos(), hero);
  52. }
  53. cb->waitTillRealize = wait;
  54. throw goalFulfilledException(sptr(*this));
  55. }
  56. std::string AdventureSpellCast::name() const
  57. {
  58. return "AdventureSpellCast " + spellID.toSpell()->name;
  59. }
  60. std::string AdventureSpellCast::completeMessage() const
  61. {
  62. return "Spell casted successfully " + spellID.toSpell()->name;
  63. }