AdventureSpellCastMovementActions.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, DayFlags flagsToAdd)
  23. :spellToCast(spellToCast), hero(hero), flagsToAdd(flagsToAdd)
  24. {
  25. manaCost = hero->getSpellCost(spellToCast.toSpell());
  26. }
  27. WaterWalkingAction::WaterWalkingAction(const CGHeroInstance * hero, SpellID spellToCast)
  28. :AdventureCastAction(spellToCast, hero, DayFlags::WATER_WALK_CAST)
  29. { }
  30. AirWalkingAction::AirWalkingAction(const CGHeroInstance * hero, SpellID spellToCast)
  31. : AdventureCastAction(spellToCast, hero, DayFlags::FLY_CAST)
  32. {
  33. }
  34. void AdventureCastAction::applyOnDestination(
  35. const CGHeroInstance * hero,
  36. CDestinationNodeInfo & destination,
  37. const PathNodeInfo & source,
  38. AIPathNode * dstNode,
  39. const AIPathNode * srcNode) const
  40. {
  41. dstNode->manaCost = srcNode->manaCost + manaCost;
  42. dstNode->theNodeBefore = source.node;
  43. dstNode->dayFlags = static_cast<DayFlags>(dstNode->dayFlags | flagsToAdd);
  44. }
  45. void AdventureCastAction::execute(AIGateway * ai, const CGHeroInstance * hero) const
  46. {
  47. assert(hero == this->hero);
  48. Goals::AdventureSpellCast(hero, spellToCast).accept(ai);
  49. }
  50. bool AdventureCastAction::canAct(const Nullkiller * ai, const AIPathNode * source) const
  51. {
  52. assert(hero == this->hero);
  53. auto hero = source->actor->hero;
  54. #ifdef VCMI_TRACE_PATHFINDER
  55. logAi->trace(
  56. "Hero %s has %d mana and needed %d and already spent %d",
  57. hero->name,
  58. hero->mana,
  59. getManaCost(hero),
  60. source->manaCost);
  61. #endif
  62. return hero->mana >= source->manaCost + manaCost;
  63. }
  64. std::string AdventureCastAction::toString() const
  65. {
  66. return "Cast " + spellToCast.toSpell()->getNameTranslated() + " by " + hero->getNameTranslated();
  67. }
  68. }
  69. }