SummonBoatEffect.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * SummonBoatEffect.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 "SummonBoatEffect.h"
  12. #include "../CSpellHandler.h"
  13. #include "../../mapObjects/CGHeroInstance.h"
  14. #include "../../mapObjects/MiscObjects.h"
  15. #include "../../mapping/CMap.h"
  16. #include "../../modding/IdentifierStorage.h"
  17. #include "../../networkPacks/PacksForClient.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. SummonBoatEffect::SummonBoatEffect(const CSpell * s, const JsonNode & config)
  20. : owner(s)
  21. , useExistingBoat(config["useExistingBoat"].Bool())
  22. {
  23. if (!config["createdBoat"].isNull())
  24. {
  25. LIBRARY->identifiers()->requestIdentifier("core:boat", config["createdBoat"], [=](int32_t boatTypeID)
  26. {
  27. createdBoat = BoatId(boatTypeID);
  28. });
  29. }
  30. }
  31. bool SummonBoatEffect::canCreateNewBoat() const
  32. {
  33. return createdBoat != BoatId::NONE;
  34. }
  35. int SummonBoatEffect::getSuccessChance(const spells::Caster * caster) const
  36. {
  37. const auto schoolLevel = caster->getSpellSchoolLevel(owner);
  38. return owner->getLevelPower(schoolLevel);
  39. }
  40. bool SummonBoatEffect::canBeCastImpl(spells::Problem & problem, const IGameInfoCallback * cb, const spells::Caster * caster) const
  41. {
  42. if(!caster->getHeroCaster())
  43. return false;
  44. if(caster->getHeroCaster()->inBoat())
  45. {
  46. MetaString message = MetaString::createFromTextID("core.genrltxt.333");
  47. caster->getCasterName(message);
  48. problem.add(std::move(message));
  49. return false;
  50. }
  51. int3 summonPos = caster->getHeroCaster()->bestLocation();
  52. if(summonPos.x < 0)
  53. {
  54. MetaString message = MetaString::createFromTextID("core.genrltxt.334");
  55. caster->getCasterName(message);
  56. problem.add(std::move(message));
  57. return false;
  58. }
  59. return true;
  60. }
  61. ESpellCastResult SummonBoatEffect::applyAdventureEffects(SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const
  62. {
  63. //check if spell works at all
  64. if(env->getRNG()->nextInt(0, 99) >= getSuccessChance(parameters.caster)) //power is % chance of success
  65. {
  66. InfoWindow iw;
  67. iw.player = parameters.caster->getCasterOwner();
  68. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 336); //%s tried to summon a boat, but failed.
  69. parameters.caster->getCasterName(iw.text);
  70. env->apply(iw);
  71. return ESpellCastResult::OK;
  72. }
  73. //try to find unoccupied boat to summon
  74. const CGBoat * nearest = nullptr;
  75. if (useExistingBoat)
  76. {
  77. double dist = 0;
  78. for(const auto & b : env->getMap()->getObjects<CGBoat>())
  79. {
  80. if(b->getBoardedHero() || b->layer != EPathfindingLayer::SAIL)
  81. continue; //we're looking for unoccupied boat
  82. double nDist = b->visitablePos().dist2d(parameters.caster->getHeroCaster()->visitablePos());
  83. if(!nearest || nDist < dist) //it's first boat or closer than previous
  84. {
  85. nearest = b;
  86. dist = nDist;
  87. }
  88. }
  89. }
  90. int3 summonPos = parameters.caster->getHeroCaster()->bestLocation();
  91. if(nullptr != nearest) //we found boat to summon
  92. {
  93. ChangeObjPos cop;
  94. cop.objid = nearest->id;
  95. cop.nPos = summonPos;
  96. cop.initiator = parameters.caster->getCasterOwner();
  97. env->apply(cop);
  98. }
  99. else if(!canCreateNewBoat()) //none or basic level -> cannot create boat :(
  100. {
  101. InfoWindow iw;
  102. iw.player = parameters.caster->getCasterOwner();
  103. iw.text.appendLocalString(EMetaText::GENERAL_TXT, 335); //There are no boats to summon.
  104. env->apply(iw);
  105. return ESpellCastResult::ERROR;
  106. }
  107. else //create boat
  108. {
  109. env->createBoat(summonPos, createdBoat, parameters.caster->getCasterOwner());
  110. }
  111. return ESpellCastResult::OK;
  112. }
  113. VCMI_LIB_NAMESPACE_END