BoatActions.cpp 3.2 KB

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