AdventureSpellCast.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/mapObjects/CGTownInstance.h"
  16. extern boost::thread_specific_ptr<CCallback> cb;
  17. extern boost::thread_specific_ptr<VCAI> ai;
  18. extern FuzzyHelper * fh;
  19. using namespace Goals;
  20. bool AdventureSpellCast::operator==(const AdventureSpellCast & other) const
  21. {
  22. return hero.h == other.hero.h;
  23. }
  24. TSubgoal AdventureSpellCast::whatToDoToAchieve()
  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->getNameTranslated(), hero->getNameTranslated());
  30. if(!spell->isAdventure())
  31. throw cannotFulfillGoalException(spell->getNameTranslated() + " is not an adventure spell.");
  32. if(!hero->canCastThisSpell(spell))
  33. throw cannotFulfillGoalException("Hero can not cast " + spell->getNameTranslated());
  34. if(hero->mana < hero->getSpellCost(spell))
  35. throw cannotFulfillGoalException("Hero has not enough mana to cast " + spell->getNameTranslated());
  36. if(spellID == SpellID::TOWN_PORTAL && town && town->visitingHero)
  37. throw cannotFulfillGoalException("The town is already occupied by " + town->visitingHero->getNameTranslated());
  38. return iAmElementar();
  39. }
  40. void AdventureSpellCast::accept(VCAI * ai)
  41. {
  42. if(town && spellID == SpellID::TOWN_PORTAL)
  43. {
  44. ai->selectedObject = town->id;
  45. }
  46. auto wait = cb->waitTillRealize;
  47. cb->waitTillRealize = true;
  48. cb->castSpell(hero.h, spellID, tile);
  49. if(town && spellID == SpellID::TOWN_PORTAL)
  50. {
  51. // visit town
  52. ai->moveHeroToTile(town->visitablePos(), hero);
  53. }
  54. cb->waitTillRealize = wait;
  55. throw goalFulfilledException(sptr(*this));
  56. }
  57. std::string AdventureSpellCast::name() const
  58. {
  59. return "AdventureSpellCast " + getSpell()->getNameTranslated();
  60. }
  61. std::string AdventureSpellCast::completeMessage() const
  62. {
  63. return "Spell cast successfully " + getSpell()->getNameTranslated();
  64. }