Interface.cpp 5.9 KB

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