BoatActions.cpp 3.2 KB

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