BoatActions.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * BoatActions.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 "BoatActions.h"
  18. namespace NKAI
  19. {
  20. namespace AIPathfinding
  21. {
  22. void BuildBoatAction::execute(const CGHeroInstance * hero) const
  23. {
  24. return Goals::BuildBoat(shipyard).accept(ai);
  25. }
  26. Goals::TSubgoal BuildBoatAction::decompose(const CGHeroInstance * hero) const
  27. {
  28. if(cb->getPlayerRelations(ai->playerID, shipyard->getObject()->getOwner()) == PlayerRelations::ENEMIES)
  29. {
  30. return Goals::sptr(Goals::CaptureObject(targetObject()));
  31. }
  32. return Goals::sptr(Goals::Invalid());
  33. }
  34. bool BuildBoatAction::canAct(const AIPathNode * source) const
  35. {
  36. auto hero = source->actor->hero;
  37. if(cb->getPlayerRelations(hero->tempOwner, shipyard->getObject()->getOwner()) == PlayerRelations::ENEMIES)
  38. {
  39. #if NKAI_TRACE_LEVEL > 1
  40. logAi->trace("Can not build a boat. Shipyard is enemy.");
  41. #endif
  42. return false;
  43. }
  44. TResources boatCost;
  45. shipyard->getBoatCost(boatCost);
  46. if(!cb->getResourceAmount().canAfford(source->actor->armyCost + boatCost))
  47. {
  48. #if NKAI_TRACE_LEVEL > 1
  49. logAi->trace("Can not build a boat. Not enough resources.");
  50. #endif
  51. return false;
  52. }
  53. return true;
  54. }
  55. const CGObjectInstance * BuildBoatAction::targetObject() const
  56. {
  57. return dynamic_cast<const CGObjectInstance*>(shipyard);
  58. }
  59. const ChainActor * BuildBoatAction::getActor(const ChainActor * sourceActor) const
  60. {
  61. return sourceActor->resourceActor;
  62. }
  63. void SummonBoatAction::execute(const CGHeroInstance * hero) const
  64. {
  65. Goals::AdventureSpellCast(hero, SpellID::SUMMON_BOAT).accept(ai);
  66. }
  67. const ChainActor * SummonBoatAction::getActor(const ChainActor * sourceActor) const
  68. {
  69. return sourceActor->castActor;
  70. }
  71. void SummonBoatAction::applyOnDestination(
  72. const CGHeroInstance * hero,
  73. CDestinationNodeInfo & destination,
  74. const PathNodeInfo & source,
  75. AIPathNode * dstMode,
  76. const AIPathNode * srcNode) const
  77. {
  78. dstMode->manaCost = srcNode->manaCost + getManaCost(hero);
  79. dstMode->theNodeBefore = source.node;
  80. }
  81. std::string BuildBoatAction::toString() const
  82. {
  83. return "Build Boat at " + shipyard->getObject()->visitablePos().toString();
  84. }
  85. bool SummonBoatAction::canAct(const AIPathNode * source) const
  86. {
  87. auto hero = source->actor->hero;
  88. #ifdef VCMI_TRACE_PATHFINDER
  89. logAi->trace(
  90. "Hero %s has %d mana and needed %d and already spent %d",
  91. hero->name,
  92. hero->mana,
  93. getManaCost(hero),
  94. source->manaCost);
  95. #endif
  96. return hero->mana >= source->manaCost + getManaCost(hero);
  97. }
  98. std::string SummonBoatAction::toString() const
  99. {
  100. return "Summon Boat";
  101. }
  102. int32_t SummonBoatAction::getManaCost(const CGHeroInstance * hero) const
  103. {
  104. SpellID summonBoat = SpellID::SUMMON_BOAT;
  105. return hero->getSpellCost(summonBoat.toSpell());
  106. }
  107. }
  108. }