Rewardable.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Rewardable.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 "Rewardable.h"
  12. #include "../CHeroHandler.h"
  13. #include "../CSoundBase.h"
  14. #include "../NetPacks.h"
  15. #include "../IGameCallback.h"
  16. #include "../CPlayerState.h"
  17. #include "../spells/CSpellHandler.h"
  18. #include "../spells/ISpellMechanics.h"
  19. #include "../mapObjects/MiscObjects.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. bool CRewardLimiter::heroAllowed(const CGHeroInstance * hero) const
  22. {
  23. if(dayOfWeek != 0)
  24. {
  25. if (IObjectInterface::cb->getDate(Date::DAY_OF_WEEK) != dayOfWeek)
  26. return false;
  27. }
  28. if(daysPassed != 0)
  29. {
  30. if (IObjectInterface::cb->getDate(Date::DAY) < daysPassed)
  31. return false;
  32. }
  33. for(const auto & reqStack : creatures)
  34. {
  35. size_t count = 0;
  36. for(const auto & slot : hero->Slots())
  37. {
  38. const CStackInstance * heroStack = slot.second;
  39. if (heroStack->type == reqStack.type)
  40. count += heroStack->count;
  41. }
  42. if (count < reqStack.count) //not enough creatures of this kind
  43. return false;
  44. }
  45. if(!IObjectInterface::cb->getPlayerState(hero->tempOwner)->resources.canAfford(resources))
  46. return false;
  47. if(heroLevel > static_cast<si32>(hero->level))
  48. return false;
  49. if(static_cast<TExpType>(heroExperience) > hero->exp)
  50. return false;
  51. if(manaPoints > hero->mana)
  52. return false;
  53. if(manaPercentage > 100 * hero->mana / hero->manaLimit())
  54. return false;
  55. for(size_t i=0; i<primary.size(); i++)
  56. {
  57. if(primary[i] > hero->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i)))
  58. return false;
  59. }
  60. for(const auto & skill : secondary)
  61. {
  62. if (skill.second > hero->getSecSkillLevel(skill.first))
  63. return false;
  64. }
  65. for(const auto & spell : spells)
  66. {
  67. if (!hero->spellbookContainsSpell(spell))
  68. return false;
  69. }
  70. for(const auto & art : artifacts)
  71. {
  72. if (!hero->hasArt(art))
  73. return false;
  74. }
  75. for(const auto & sublimiter : noneOf)
  76. {
  77. if (sublimiter->heroAllowed(hero))
  78. return false;
  79. }
  80. for(const auto & sublimiter : allOf)
  81. {
  82. if (!sublimiter->heroAllowed(hero))
  83. return false;
  84. }
  85. if(anyOf.empty())
  86. return true;
  87. for(const auto & sublimiter : anyOf)
  88. {
  89. if (sublimiter->heroAllowed(hero))
  90. return true;
  91. }
  92. return false;
  93. }
  94. si32 CRewardInfo::calculateManaPoints(const CGHeroInstance * hero) const
  95. {
  96. si32 manaScaled = hero->mana;
  97. if (manaPercentage >= 0)
  98. manaScaled = hero->manaLimit() * manaPercentage / 100;
  99. si32 manaMissing = std::max(0, hero->manaLimit() - manaScaled);
  100. si32 manaGranted = std::min(manaMissing, manaDiff);
  101. si32 manaOverflow = manaDiff - manaGranted;
  102. si32 manaOverLimit = manaOverflow * manaOverflowFactor / 100;
  103. si32 manaOutput = manaScaled + manaGranted + manaOverLimit;
  104. return manaOutput;
  105. }
  106. Component CRewardInfo::getDisplayedComponent(const CGHeroInstance * h) const
  107. {
  108. std::vector<Component> comps;
  109. loadComponents(comps, h);
  110. assert(!comps.empty());
  111. return comps.front();
  112. }
  113. void CRewardInfo::loadComponents(std::vector<Component> & comps,
  114. const CGHeroInstance * h) const
  115. {
  116. for (auto comp : extraComponents)
  117. comps.push_back(comp);
  118. if (heroExperience)
  119. {
  120. comps.emplace_back(Component::EComponentType::EXPERIENCE, 0, static_cast<si32>(h->calculateXp(heroExperience)), 0);
  121. }
  122. if (heroLevel)
  123. comps.emplace_back(Component::EComponentType::EXPERIENCE, 1, heroLevel, 0);
  124. if (manaDiff || manaPercentage >= 0)
  125. comps.emplace_back(Component::EComponentType::PRIM_SKILL, 5, calculateManaPoints(h) - h->mana, 0);
  126. for (size_t i=0; i<primary.size(); i++)
  127. {
  128. if (primary[i] != 0)
  129. comps.emplace_back(Component::EComponentType::PRIM_SKILL, static_cast<ui16>(i), primary[i], 0);
  130. }
  131. for(const auto & entry : secondary)
  132. comps.emplace_back(Component::EComponentType::SEC_SKILL, entry.first, entry.second, 0);
  133. for(const auto & entry : artifacts)
  134. comps.emplace_back(Component::EComponentType::ARTIFACT, entry, 1, 0);
  135. for(const auto & entry : spells)
  136. comps.emplace_back(Component::EComponentType::SPELL, entry, 1, 0);
  137. for(const auto & entry : creatures)
  138. comps.emplace_back(Component::EComponentType::CREATURE, entry.type->getId(), entry.count, 0);
  139. for (size_t i=0; i<resources.size(); i++)
  140. {
  141. if (resources[i] !=0)
  142. comps.emplace_back(Component::EComponentType::RESOURCE, static_cast<ui16>(i), resources[i], 0);
  143. }
  144. }
  145. std::vector<ui32> Rewardable::Interface::getAvailableRewards(const CGHeroInstance * hero, CRewardVisitInfo::ERewardEventType event) const
  146. {
  147. std::vector<ui32> ret;
  148. for(size_t i = 0; i < _configuration.info.size(); i++)
  149. {
  150. const CRewardVisitInfo & visit = _configuration.info[i];
  151. if(event == visit.visitType && visit.limiter.heroAllowed(hero))
  152. {
  153. logGlobal->trace("Reward %d is allowed", i);
  154. ret.push_back(static_cast<ui32>(i));
  155. }
  156. }
  157. return ret;
  158. }
  159. Rewardable::EVisitMode Rewardable::Configuration::getVisitMode() const
  160. {
  161. return static_cast<EVisitMode>(visitMode);
  162. }
  163. ui16 Rewardable::Configuration::getResetDuration() const
  164. {
  165. return resetParameters.period;
  166. }
  167. const Rewardable::Configuration & Rewardable::Interface::getConfiguration() const
  168. {
  169. return _configuration;
  170. }
  171. Rewardable::Configuration & Rewardable::Interface::configuration()
  172. {
  173. return _configuration;
  174. }
  175. void Rewardable::Interface::grantRewardBeforeLevelup(IGameCallback * cb, const CRewardVisitInfo & info, const CGHeroInstance * hero) const
  176. {
  177. assert(hero);
  178. assert(hero->tempOwner.isValidPlayer());
  179. assert(info.reward.creatures.size() <= GameConstants::ARMY_SIZE);
  180. cb->giveResources(hero->tempOwner, info.reward.resources);
  181. for(const auto & entry : info.reward.secondary)
  182. {
  183. int current = hero->getSecSkillLevel(entry.first);
  184. if( (current != 0 && current < entry.second) ||
  185. (hero->canLearnSkill() ))
  186. {
  187. cb->changeSecSkill(hero, entry.first, entry.second);
  188. }
  189. }
  190. for(int i=0; i< info.reward.primary.size(); i++)
  191. cb->changePrimSkill(hero, static_cast<PrimarySkill::PrimarySkill>(i), info.reward.primary[i], false);
  192. si64 expToGive = 0;
  193. if (info.reward.heroLevel > 0)
  194. expToGive += VLC->heroh->reqExp(hero->level+info.reward.heroLevel) - VLC->heroh->reqExp(hero->level);
  195. if (info.reward.heroExperience > 0)
  196. expToGive += hero->calculateXp(info.reward.heroExperience);
  197. if(expToGive)
  198. cb->changePrimSkill(hero, PrimarySkill::EXPERIENCE, expToGive);
  199. }
  200. void Rewardable::Interface::grantRewardAfterLevelup(IGameCallback * cb, const CRewardVisitInfo & info, const CArmedInstance * army, const CGHeroInstance * hero) const
  201. {
  202. if(info.reward.manaDiff || info.reward.manaPercentage >= 0)
  203. cb->setManaPoints(hero->id, info.reward.calculateManaPoints(hero));
  204. if(info.reward.movePoints || info.reward.movePercentage >= 0)
  205. {
  206. SetMovePoints smp;
  207. smp.hid = hero->id;
  208. smp.val = hero->movement;
  209. if (info.reward.movePercentage >= 0) // percent from max
  210. smp.val = hero->maxMovePoints(hero->boat && hero->boat->layer == EPathfindingLayer::SAIL) * info.reward.movePercentage / 100;
  211. smp.val = std::max<si32>(0, smp.val + info.reward.movePoints);
  212. cb->setMovePoints(&smp);
  213. }
  214. for(const Bonus & bonus : info.reward.bonuses)
  215. {
  216. assert(bonus.source == Bonus::OBJECT);
  217. GiveBonus gb;
  218. gb.who = GiveBonus::ETarget::HERO;
  219. gb.bonus = bonus;
  220. gb.id = hero->id.getNum();
  221. cb->giveHeroBonus(&gb);
  222. }
  223. for(const ArtifactID & art : info.reward.artifacts)
  224. cb->giveHeroNewArtifact(hero, VLC->arth->objects[art],ArtifactPosition::FIRST_AVAILABLE);
  225. if(!info.reward.spells.empty())
  226. {
  227. std::set<SpellID> spellsToGive(info.reward.spells.begin(), info.reward.spells.end());
  228. cb->changeSpells(hero, true, spellsToGive);
  229. }
  230. if(!info.reward.creaturesChange.empty())
  231. {
  232. for(const auto & slot : hero->Slots())
  233. {
  234. const CStackInstance * heroStack = slot.second;
  235. for(const auto & change : info.reward.creaturesChange)
  236. {
  237. if (heroStack->type->getId() == change.first)
  238. {
  239. StackLocation location(hero, slot.first);
  240. cb->changeStackType(location, change.second.toCreature());
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. if(!info.reward.creatures.empty())
  247. {
  248. CCreatureSet creatures;
  249. for(const auto & crea : info.reward.creatures)
  250. creatures.addToSlot(creatures.getFreeSlot(), new CStackInstance(crea.type, crea.count));
  251. if(auto * army = dynamic_cast<const CArmedInstance*>(this)) //TODO: to fix that, CArmedInstance must be splitted on map instance part and interface part
  252. cb->giveCreatures(army, hero, creatures, false);
  253. }
  254. if(info.reward.spellCast.first != SpellID::NONE)
  255. {
  256. caster.setActualCaster(hero);
  257. caster.setSpellSchoolLevel(info.reward.spellCast.second);
  258. cb->castSpell(&caster, info.reward.spellCast.first, int3{-1, -1, -1});
  259. if(info.reward.removeObject)
  260. logMod->warn("Removal of object with spell casts is not supported!");
  261. }
  262. else if(info.reward.removeObject) //FIXME: object can't track spell cancel or finish, so removeObject leads to crash
  263. if(auto * instance = dynamic_cast<const CGObjectInstance*>(this))
  264. cb->removeObject(instance);
  265. }
  266. VCMI_LIB_NAMESPACE_END