BoatActions.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(AIGateway * ai, const CGHeroInstance * hero) const
  23. {
  24. return Goals::BuildBoat(shipyard).accept(ai);
  25. }
  26. Goals::TSubgoal BuildBoatAction::decompose(const Nullkiller * ai, 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 Nullkiller * ai, const CGHeroInstance * hero, const TResources & reservedResources) const
  35. {
  36. if(cb->getPlayerRelations(hero->tempOwner, shipyard->getObject()->getOwner()) == PlayerRelations::ENEMIES)
  37. {
  38. #if NKAI_TRACE_LEVEL > 1
  39. logAi->trace("Can not build a boat. Shipyard is enemy.");
  40. #endif
  41. return false;
  42. }
  43. TResources boatCost;
  44. shipyard->getBoatCost(boatCost);
  45. if(!cb->getResourceAmount().canAfford(reservedResources + boatCost))
  46. {
  47. #if NKAI_TRACE_LEVEL > 1
  48. logAi->trace("Can not build a boat. Not enough resources.");
  49. #endif
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool BuildBoatAction::canAct(const Nullkiller * ai, const AIPathNode * source) const
  55. {
  56. return canAct(ai, source->actor->hero, source->actor->armyCost);
  57. }
  58. bool BuildBoatAction::canAct(const Nullkiller * ai, const AIPathNodeInfo & source) const
  59. {
  60. TResources res;
  61. return canAct(ai, source.targetHero, res);
  62. }
  63. const CGObjectInstance * BuildBoatAction::targetObject() const
  64. {
  65. return dynamic_cast<const CGObjectInstance*>(shipyard);
  66. }
  67. const ChainActor * BuildBoatAction::getActor(const ChainActor * sourceActor) const
  68. {
  69. return sourceActor->resourceActor;
  70. }
  71. std::shared_ptr<SpecialAction> BuildBoatActionFactory::create(const Nullkiller * ai)
  72. {
  73. return std::make_shared<BuildBoatAction>(ai->cb.get(), dynamic_cast<const IShipyard * >(ai->cb->getObj(shipyard)));
  74. }
  75. void SummonBoatAction::execute(AIGateway * ai, const CGHeroInstance * hero) const
  76. {
  77. Goals::AdventureSpellCast(hero, usedSpell).accept(ai);
  78. }
  79. const ChainActor * SummonBoatAction::getActor(const ChainActor * sourceActor) const
  80. {
  81. return sourceActor->castActor;
  82. }
  83. void SummonBoatAction::applyOnDestination(
  84. const CGHeroInstance * hero,
  85. CDestinationNodeInfo & destination,
  86. const PathNodeInfo & source,
  87. AIPathNode * dstMode,
  88. const AIPathNode * srcNode) const
  89. {
  90. dstMode->manaCost = srcNode->manaCost + getManaCost(hero);
  91. dstMode->theNodeBefore = source.node;
  92. }
  93. std::string BuildBoatAction::toString() const
  94. {
  95. return "Build Boat at " + shipyard->getObject()->visitablePos().toString();
  96. }
  97. bool SummonBoatAction::canAct(const Nullkiller * ai, const AIPathNode * source) const
  98. {
  99. auto hero = source->actor->hero;
  100. #ifdef VCMI_TRACE_PATHFINDER
  101. logAi->trace(
  102. "Hero %s has %d mana and needed %d and already spent %d",
  103. hero->name,
  104. hero->mana,
  105. getManaCost(hero),
  106. source->manaCost);
  107. #endif
  108. return hero->mana >= source->manaCost + getManaCost(hero);
  109. }
  110. std::string SummonBoatAction::toString() const
  111. {
  112. return "Summon Boat";
  113. }
  114. int32_t SummonBoatAction::getManaCost(const CGHeroInstance * hero) const
  115. {
  116. // FIXME: this should be hero->getSpellCost, however currently queries to bonus system are too slow
  117. return usedSpell.toSpell()->getCost(0);
  118. }
  119. }
  120. }