Interface.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "../CSoundBase.h"
  14. #include "../NetPacks.h"
  15. #include "../spells/CSpellHandler.h"
  16. #include "../spells/ISpellMechanics.h"
  17. #include "../mapObjects/MiscObjects.h"
  18. #include "../IGameCallback.h"
  19. #include "../CPlayerState.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. for(const auto & entry : info.reward.secondary)
  42. {
  43. int current = hero->getSecSkillLevel(entry.first);
  44. if( (current != 0 && current < entry.second) ||
  45. (hero->canLearnSkill() ))
  46. {
  47. cb->changeSecSkill(hero, entry.first, entry.second);
  48. }
  49. }
  50. for(int i=0; i< info.reward.primary.size(); i++)
  51. cb->changePrimSkill(hero, static_cast<PrimarySkill::PrimarySkill>(i), info.reward.primary[i], false);
  52. si64 expToGive = 0;
  53. if (info.reward.heroLevel > 0)
  54. expToGive += VLC->heroh->reqExp(hero->level+info.reward.heroLevel) - VLC->heroh->reqExp(hero->level);
  55. if (info.reward.heroExperience > 0)
  56. expToGive += hero->calculateXp(info.reward.heroExperience);
  57. if(expToGive)
  58. cb->changePrimSkill(hero, PrimarySkill::EXPERIENCE, expToGive);
  59. }
  60. void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const Rewardable::VisitInfo & info, const CArmedInstance * army, const CGHeroInstance * hero) const
  61. {
  62. if(info.reward.manaDiff || info.reward.manaPercentage >= 0)
  63. cb->setManaPoints(hero->id, info.reward.calculateManaPoints(hero));
  64. if(info.reward.movePoints || info.reward.movePercentage >= 0)
  65. {
  66. SetMovePoints smp;
  67. smp.hid = hero->id;
  68. smp.val = hero->movement;
  69. if (info.reward.movePercentage >= 0) // percent from max
  70. smp.val = hero->maxMovePoints(hero->boat && hero->boat->layer == EPathfindingLayer::SAIL) * info.reward.movePercentage / 100;
  71. smp.val = std::max<si32>(0, smp.val + info.reward.movePoints);
  72. cb->setMovePoints(&smp);
  73. }
  74. for(const Bonus & bonus : info.reward.bonuses)
  75. {
  76. assert(bonus.source == Bonus::OBJECT);
  77. GiveBonus gb;
  78. gb.who = GiveBonus::ETarget::HERO;
  79. gb.bonus = bonus;
  80. gb.id = hero->id.getNum();
  81. cb->giveHeroBonus(&gb);
  82. }
  83. for(const ArtifactID & art : info.reward.artifacts)
  84. cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE);
  85. if(!info.reward.spells.empty())
  86. {
  87. std::set<SpellID> spellsToGive(info.reward.spells.begin(), info.reward.spells.end());
  88. cb->changeSpells(hero, true, spellsToGive);
  89. }
  90. if(!info.reward.creaturesChange.empty())
  91. {
  92. for(const auto & slot : hero->Slots())
  93. {
  94. const CStackInstance * heroStack = slot.second;
  95. for(const auto & change : info.reward.creaturesChange)
  96. {
  97. if (heroStack->type->getId() == change.first)
  98. {
  99. StackLocation location(hero, slot.first);
  100. cb->changeStackType(location, change.second.toCreature());
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. if(!info.reward.creatures.empty())
  107. {
  108. CCreatureSet creatures;
  109. for(const auto & crea : info.reward.creatures)
  110. creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
  111. if(auto * army = dynamic_cast<const CArmedInstance*>(this)) //TODO: to fix that, CArmedInstance must be splitted on map instance part and interface part
  112. cb->giveCreatures(army, hero, creatures, false);
  113. }
  114. if(info.reward.spellCast.first != SpellID::NONE)
  115. {
  116. caster.setActualCaster(hero);
  117. caster.setSpellSchoolLevel(info.reward.spellCast.second);
  118. cb->castSpell(&caster, info.reward.spellCast.first, int3{-1, -1, -1});
  119. if(info.reward.removeObject)
  120. logMod->warn("Removal of object with spell casts is not supported!");
  121. }
  122. else if(info.reward.removeObject) //FIXME: object can't track spell cancel or finish, so removeObject leads to crash
  123. if(auto * instance = dynamic_cast<const CGObjectInstance*>(this))
  124. cb->removeObject(instance);
  125. }
  126. VCMI_LIB_NAMESPACE_END