BoatActions.cpp 3.0 KB

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