Interface.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Interface.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 "Interface.h"
  12. #include "../CHeroHandler.h"
  13. #include "../TerrainHandler.h"
  14. #include "../CSoundBase.h"
  15. #include "../NetPacks.h"
  16. #include "../spells/CSpellHandler.h"
  17. #include "../spells/ISpellMechanics.h"
  18. #include "../mapObjects/MiscObjects.h"
  19. #include "../IGameCallback.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. std::vector<ui32> Rewardable::Interface::getAvailableRewards(const CGHeroInstance * hero, Rewardable::EEventType event) const
  22. {
  23. std::vector<ui32> ret;
  24. for(size_t i = 0; i < configuration.info.size(); i++)
  25. {
  26. const Rewardable::VisitInfo & visit = configuration.info[i];
  27. if(event == visit.visitType && visit.limiter.heroAllowed(hero))
  28. {
  29. logGlobal->trace("Reward %d is allowed", i);
  30. ret.push_back(static_cast<ui32>(i));
  31. }
  32. }
  33. return ret;
  34. }
  35. void Rewardable::Interface::grantRewardBeforeLevelup(IGameCallback * cb, const Rewardable::VisitInfo & info, const CGHeroInstance * hero) const
  36. {
  37. assert(hero);
  38. assert(hero->tempOwner.isValidPlayer());
  39. assert(info.reward.creatures.size() <= GameConstants::ARMY_SIZE);
  40. cb->giveResources(hero->tempOwner, info.reward.resources);
  41. if (info.reward.revealTiles)
  42. {
  43. auto const & props = *info.reward.revealTiles;
  44. FoWChange fw;
  45. if (props.hide)
  46. fw.mode = FoWChange::Mode::HIDE;
  47. else
  48. fw.mode = FoWChange::Mode::REVEAL;
  49. fw.player = hero->tempOwner;
  50. auto const functor = [&props](const TerrainTile * tile)
  51. {
  52. int score = 0;
  53. if (tile->terType->isSurface())
  54. score += props.scoreSurface;
  55. if (tile->terType->isUnderground())
  56. score += props.scoreSubterra;
  57. if (tile->terType->isWater())
  58. score += props.scoreWater;
  59. if (tile->terType->isRock())
  60. score += props.scoreRock;
  61. return score > 0;
  62. };
  63. if (props.radius > 0)
  64. {
  65. cb->getTilesInRange(fw.tiles, hero->getSightCenter(), props.radius, hero->tempOwner, 1);
  66. vstd::erase_if(fw.tiles, [&](const int3 & coord){
  67. return functor(cb->getTile(coord));
  68. });
  69. }
  70. else
  71. {
  72. cb->getAllTiles(fw.tiles, hero->tempOwner, -1, functor);
  73. }
  74. cb->sendAndApply(&fw);
  75. }
  76. for(const auto & entry : info.reward.secondary)
  77. {
  78. int current = hero->getSecSkillLevel(entry.first);
  79. if( (current != 0 && current < entry.second) ||
  80. (hero->canLearnSkill() ))
  81. {
  82. cb->changeSecSkill(hero, entry.first, entry.second);
  83. }
  84. }
  85. for(int i=0; i< info.reward.primary.size(); i++)
  86. cb->changePrimSkill(hero, static_cast<PrimarySkill>(i), info.reward.primary[i], false);
  87. si64 expToGive = 0;
  88. if (info.reward.heroLevel > 0)
  89. expToGive += VLC->heroh->reqExp(hero->level+info.reward.heroLevel) - VLC->heroh->reqExp(hero->level);
  90. if (info.reward.heroExperience > 0)
  91. expToGive += hero->calculateXp(info.reward.heroExperience);
  92. if(expToGive)
  93. cb->changePrimSkill(hero, PrimarySkill::EXPERIENCE, expToGive);
  94. }
  95. void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const Rewardable::VisitInfo & info, const CArmedInstance * army, const CGHeroInstance * hero) const
  96. {
  97. if(info.reward.manaDiff || info.reward.manaPercentage >= 0)
  98. cb->setManaPoints(hero->id, info.reward.calculateManaPoints(hero));
  99. if(info.reward.movePoints || info.reward.movePercentage >= 0)
  100. {
  101. SetMovePoints smp;
  102. smp.hid = hero->id;
  103. smp.val = hero->movementPointsRemaining();
  104. if (info.reward.movePercentage >= 0) // percent from max
  105. smp.val = hero->movementPointsLimit(hero->boat && hero->boat->layer == EPathfindingLayer::SAIL) * info.reward.movePercentage / 100;
  106. smp.val = std::max<si32>(0, smp.val + info.reward.movePoints);
  107. cb->setMovePoints(&smp);
  108. }
  109. for(const Bonus & bonus : info.reward.bonuses)
  110. {
  111. GiveBonus gb;
  112. gb.who = GiveBonus::ETarget::HERO;
  113. gb.bonus = bonus;
  114. gb.id = hero->id.getNum();
  115. cb->giveHeroBonus(&gb);
  116. }
  117. for(const ArtifactID & art : info.reward.artifacts)
  118. cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE);
  119. if(!info.reward.spells.empty())
  120. {
  121. std::set<SpellID> spellsToGive(info.reward.spells.begin(), info.reward.spells.end());
  122. cb->changeSpells(hero, true, spellsToGive);
  123. }
  124. if(!info.reward.creaturesChange.empty())
  125. {
  126. for(const auto & slot : hero->Slots())
  127. {
  128. const CStackInstance * heroStack = slot.second;
  129. for(const auto & change : info.reward.creaturesChange)
  130. {
  131. if (heroStack->type->getId() == change.first)
  132. {
  133. StackLocation location(hero, slot.first);
  134. cb->changeStackType(location, change.second.toCreature());
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. if(!info.reward.creatures.empty())
  141. {
  142. CCreatureSet creatures;
  143. for(const auto & crea : info.reward.creatures)
  144. creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
  145. if(auto * army = dynamic_cast<const CArmedInstance*>(this)) //TODO: to fix that, CArmedInstance must be splitted on map instance part and interface part
  146. cb->giveCreatures(army, hero, creatures, false);
  147. }
  148. if(info.reward.spellCast.first != SpellID::NONE)
  149. {
  150. caster.setActualCaster(hero);
  151. caster.setSpellSchoolLevel(info.reward.spellCast.second);
  152. cb->castSpell(&caster, info.reward.spellCast.first, int3{-1, -1, -1});
  153. }
  154. if(info.reward.removeObject)
  155. if(auto * instance = dynamic_cast<const CGObjectInstance*>(this))
  156. cb->removeAfterVisit(instance);
  157. }
  158. void Rewardable::Interface::serializeJson(JsonSerializeFormat & handler)
  159. {
  160. configuration.serializeJson(handler);
  161. }
  162. VCMI_LIB_NAMESPACE_END