AdventureSpellCastMovementActions.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * AdventureSpellCastMovementActions.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 "../../AIGateway.h"
  12. #include "../../Goals/AdventureSpellCast.h"
  13. #include "../../Goals/CaptureObject.h"
  14. #include "../../Goals/Invalid.h"
  15. #include "../../Goals/BuildBoat.h"
  16. #include "../../../../lib/mapObjects/MapObjects.h"
  17. #include "AdventureSpellCastMovementActions.h"
  18. namespace NKAI
  19. {
  20. namespace AIPathfinding
  21. {
  22. AdventureCastAction::AdventureCastAction(SpellID spellToCast, const CGHeroInstance * hero)
  23. :spellToCast(spellToCast), hero(hero)
  24. {
  25. manaCost = hero->getSpellCost(spellToCast.toSpell());
  26. }
  27. WaterWalkingAction::WaterWalkingAction(const CGHeroInstance * hero)
  28. :AdventureCastAction(SpellID::WATER_WALK, hero)
  29. { }
  30. AirWalkingAction::AirWalkingAction(const CGHeroInstance * hero)
  31. : AdventureCastAction(SpellID::FLY, hero)
  32. {
  33. }
  34. void AdventureCastAction::applyOnDestination(
  35. const CGHeroInstance * hero,
  36. CDestinationNodeInfo & destination,
  37. const PathNodeInfo & source,
  38. AIPathNode * dstMode,
  39. const AIPathNode * srcNode) const
  40. {
  41. dstMode->manaCost = srcNode->manaCost + manaCost;
  42. dstMode->theNodeBefore = source.node;
  43. }
  44. void AdventureCastAction::execute(const CGHeroInstance * hero) const
  45. {
  46. assert(hero == this->hero);
  47. Goals::AdventureSpellCast(hero, spellToCast).accept(ai);
  48. }
  49. bool AdventureCastAction::canAct(const AIPathNode * source) const
  50. {
  51. assert(hero == this->hero);
  52. auto hero = source->actor->hero;
  53. #ifdef VCMI_TRACE_PATHFINDER
  54. logAi->trace(
  55. "Hero %s has %d mana and needed %d and already spent %d",
  56. hero->name,
  57. hero->mana,
  58. getManaCost(hero),
  59. source->manaCost);
  60. #endif
  61. return hero->mana >= source->manaCost + manaCost;
  62. }
  63. std::string AdventureCastAction::toString() const
  64. {
  65. return "Cast " + spellToCast.toSpell()->getNameTranslated() + " by " + hero->getNameTranslated();
  66. }
  67. }
  68. }