Interface.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. VCMI_LIB_NAMESPACE_BEGIN
  20. std::vector<ui32> Rewardable::Interface::getAvailableRewards(const CGHeroInstance * hero, Rewardable::EEventType event) const
  21. {
  22. std::vector<ui32> ret;
  23. for(size_t i = 0; i < configuration.info.size(); i++)
  24. {
  25. const Rewardable::VisitInfo & visit = configuration.info[i];
  26. if(event == visit.visitType && visit.limiter.heroAllowed(hero))
  27. {
  28. logGlobal->trace("Reward %d is allowed", i);
  29. ret.push_back(static_cast<ui32>(i));
  30. }
  31. }
  32. return ret;
  33. }
  34. void Rewardable::Interface::grantRewardBeforeLevelup(IGameCallback * cb, const Rewardable::VisitInfo & info, const CGHeroInstance * hero) const
  35. {
  36. assert(hero);
  37. assert(hero->tempOwner.isValidPlayer());
  38. assert(info.reward.creatures.size() <= GameConstants::ARMY_SIZE);
  39. cb->giveResources(hero->tempOwner, info.reward.resources);
  40. for(const auto & entry : info.reward.secondary)
  41. {
  42. int current = hero->getSecSkillLevel(entry.first);
  43. if( (current != 0 && current < entry.second) ||
  44. (hero->canLearnSkill() ))
  45. {
  46. cb->changeSecSkill(hero, entry.first, entry.second);
  47. }
  48. }
  49. for(int i=0; i< info.reward.primary.size(); i++)
  50. cb->changePrimSkill(hero, static_cast<PrimarySkill::PrimarySkill>(i), info.reward.primary[i], false);
  51. si64 expToGive = 0;
  52. if (info.reward.heroLevel > 0)
  53. expToGive += VLC->heroh->reqExp(hero->level+info.reward.heroLevel) - VLC->heroh->reqExp(hero->level);
  54. if (info.reward.heroExperience > 0)
  55. expToGive += hero->calculateXp(info.reward.heroExperience);
  56. if(expToGive)
  57. cb->changePrimSkill(hero, PrimarySkill::EXPERIENCE, expToGive);
  58. }
  59. void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const Rewardable::VisitInfo & info, const CArmedInstance * army, const CGHeroInstance * hero) const
  60. {
  61. if(info.reward.manaDiff || info.reward.manaPercentage >= 0)
  62. cb->setManaPoints(hero->id, info.reward.calculateManaPoints(hero));
  63. if(info.reward.movePoints || info.reward.movePercentage >= 0)
  64. {
  65. SetMovePoints smp;
  66. smp.hid = hero->id;
  67. smp.val = hero->movementPointsRemaining();
  68. if (info.reward.movePercentage >= 0) // percent from max
  69. smp.val = hero->movementPointsLimit(hero->boat && hero->boat->layer == EPathfindingLayer::SAIL) * info.reward.movePercentage / 100;
  70. smp.val = std::max<si32>(0, smp.val + info.reward.movePoints);
  71. cb->setMovePoints(&smp);
  72. }
  73. for(const Bonus & bonus : info.reward.bonuses)
  74. {
  75. GiveBonus gb;
  76. gb.who = GiveBonus::ETarget::HERO;
  77. gb.bonus = bonus;
  78. gb.id = hero->id.getNum();
  79. cb->giveHeroBonus(&gb);
  80. }
  81. for(const ArtifactID & art : info.reward.artifacts)
  82. cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE);
  83. if(!info.reward.spells.empty())
  84. {
  85. std::set<SpellID> spellsToGive(info.reward.spells.begin(), info.reward.spells.end());
  86. cb->changeSpells(hero, true, spellsToGive);
  87. }
  88. if(!info.reward.creaturesChange.empty())
  89. {
  90. for(const auto & slot : hero->Slots())
  91. {
  92. const CStackInstance * heroStack = slot.second;
  93. for(const auto & change : info.reward.creaturesChange)
  94. {
  95. if (heroStack->type->getId() == change.first)
  96. {
  97. StackLocation location(hero, slot.first);
  98. cb->changeStackType(location, change.second.toCreature());
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. if(!info.reward.creatures.empty())
  105. {
  106. CCreatureSet creatures;
  107. for(const auto & crea : info.reward.creatures)
  108. creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
  109. if(auto * army = dynamic_cast<const CArmedInstance*>(this)) //TODO: to fix that, CArmedInstance must be splitted on map instance part and interface part
  110. cb->giveCreatures(army, hero, creatures, false);
  111. }
  112. if(info.reward.spellCast.first != SpellID::NONE)
  113. {
  114. caster.setActualCaster(hero);
  115. caster.setSpellSchoolLevel(info.reward.spellCast.second);
  116. cb->castSpell(&caster, info.reward.spellCast.first, int3{-1, -1, -1});
  117. if(info.reward.removeObject)
  118. logMod->warn("Removal of object with spell casts is not supported!");
  119. }
  120. else if(info.reward.removeObject) //FIXME: object can't track spell cancel or finish, so removeObject leads to crash
  121. if(auto * instance = dynamic_cast<const CGObjectInstance*>(this))
  122. cb->removeObject(instance);
  123. }
  124. VCMI_LIB_NAMESPACE_END