BoatActions.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. bool BuildBoatAction::canAct(const AIPathNode * source) const
  34. {
  35. auto hero = source->actor->hero;
  36. if(cb->getPlayerRelations(hero->tempOwner, shipyard->o->tempOwner) == PlayerRelations::ENEMIES)
  37. {
  38. #if AI_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(source->actor->armyCost + boatCost))
  46. {
  47. #if AI_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. const CGObjectInstance * BuildBoatAction::targetObject() const
  55. {
  56. return shipyard->o;
  57. }
  58. const ChainActor * BuildBoatAction::getActor(const ChainActor * sourceActor) const
  59. {
  60. return sourceActor->resourceActor;
  61. }
  62. void SummonBoatAction::execute(const CGHeroInstance * hero) const
  63. {
  64. Goals::AdventureSpellCast(hero, SpellID::SUMMON_BOAT).accept(ai.get());
  65. }
  66. const ChainActor * SummonBoatAction::getActor(const ChainActor * sourceActor) const
  67. {
  68. return sourceActor->castActor;
  69. }
  70. void SummonBoatAction::applyOnDestination(
  71. const CGHeroInstance * hero,
  72. CDestinationNodeInfo & destination,
  73. const PathNodeInfo & source,
  74. AIPathNode * dstMode,
  75. const AIPathNode * srcNode) const
  76. {
  77. dstMode->manaCost = srcNode->manaCost + getManaCost(hero);
  78. dstMode->theNodeBefore = source.node;
  79. }
  80. std::string BuildBoatAction::toString() const
  81. {
  82. return "Build Boat at " + shipyard->o->getObjectName();
  83. }
  84. bool SummonBoatAction::canAct(const AIPathNode * source) const
  85. {
  86. auto hero = source->actor->hero;
  87. #ifdef VCMI_TRACE_PATHFINDER
  88. logAi->trace(
  89. "Hero %s has %d mana and needed %d and already spent %d",
  90. hero->name,
  91. hero->mana,
  92. getManaCost(hero),
  93. source->manaCost);
  94. #endif
  95. return hero->mana >= source->manaCost + getManaCost(hero);
  96. }
  97. std::string SummonBoatAction::toString() const
  98. {
  99. return "Summon Boat";
  100. }
  101. uint32_t SummonBoatAction::getManaCost(const CGHeroInstance * hero) const
  102. {
  103. SpellID summonBoat = SpellID::SUMMON_BOAT;
  104. return hero->getSpellCost(summonBoat.toSpell());
  105. }
  106. }