Interface.cpp 6.1 KB

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