ArtifactUtils.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * ArtifactUtils.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 "ArtifactUtils.h"
  11. #include "CArtHandler.h"
  12. #include "GameSettings.h"
  13. #include "spells/CSpellHandler.h"
  14. #include "mapping/CMap.h"
  15. #include "mapObjects/CGHeroInstance.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtAnyPosition(const CArtifactSet * target, const ArtifactID & aid)
  18. {
  19. const auto * art = aid.toArtifact();
  20. for(const auto & slot : art->getPossibleSlots().at(target->bearerType()))
  21. {
  22. if(art->canBePutAt(target, slot))
  23. return slot;
  24. }
  25. return getArtBackpackPosition(target, aid);
  26. }
  27. DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtBackpackPosition(const CArtifactSet * target, const ArtifactID & aid)
  28. {
  29. const auto * art = aid.toArtifact();
  30. if(art->canBePutAt(target, ArtifactPosition::BACKPACK_START))
  31. {
  32. return ArtifactPosition::BACKPACK_START;
  33. }
  34. return ArtifactPosition::PRE_FIRST;
  35. }
  36. DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::unmovableSlots()
  37. {
  38. static const std::vector<ArtifactPosition> positions =
  39. {
  40. ArtifactPosition::SPELLBOOK,
  41. ArtifactPosition::MACH4
  42. };
  43. return positions;
  44. }
  45. DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::constituentWornSlots()
  46. {
  47. static const std::vector<ArtifactPosition> positions =
  48. {
  49. ArtifactPosition::HEAD,
  50. ArtifactPosition::SHOULDERS,
  51. ArtifactPosition::NECK,
  52. ArtifactPosition::RIGHT_HAND,
  53. ArtifactPosition::LEFT_HAND,
  54. ArtifactPosition::TORSO,
  55. ArtifactPosition::RIGHT_RING,
  56. ArtifactPosition::LEFT_RING,
  57. ArtifactPosition::FEET,
  58. ArtifactPosition::MISC1,
  59. ArtifactPosition::MISC2,
  60. ArtifactPosition::MISC3,
  61. ArtifactPosition::MISC4,
  62. ArtifactPosition::MISC5,
  63. };
  64. return positions;
  65. }
  66. DLL_LINKAGE bool ArtifactUtils::isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot)
  67. {
  68. return slot.second.artifact
  69. && !slot.second.locked
  70. && !vstd::contains(unmovableSlots(), slot.first);
  71. }
  72. DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, const ArtifactID & artID, const ArtifactPosition & slot)
  73. {
  74. // TODO what'll happen if Titan's thunder is equipped by pickin git up or the start of game?
  75. // Titan's Thunder creates new spellbook on equip
  76. if(artID == ArtifactID::TITANS_THUNDER && slot == ArtifactPosition::RIGHT_HAND)
  77. {
  78. if(heroPtr && !heroPtr->hasSpellbook())
  79. {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(const ArtifactPosition & slot)
  86. {
  87. return slot >= ArtifactPosition::BACKPACK_START;
  88. }
  89. DLL_LINKAGE bool ArtifactUtils::isSlotEquipment(const ArtifactPosition & slot)
  90. {
  91. return slot < ArtifactPosition::BACKPACK_START && slot >= ArtifactPosition(0);
  92. }
  93. DLL_LINKAGE bool ArtifactUtils::isBackpackFreeSlots(const CArtifactSet * target, const size_t reqSlots)
  94. {
  95. const auto backpackCap = VLC->settings()->getInteger(EGameSettings::HEROES_BACKPACK_CAP);
  96. if(backpackCap < 0)
  97. return true;
  98. else
  99. return target->artifactsInBackpack.size() + reqSlots <= backpackCap;
  100. }
  101. DLL_LINKAGE std::vector<const CArtifact*> ArtifactUtils::assemblyPossibilities(
  102. const CArtifactSet * artSet, const ArtifactID & aid)
  103. {
  104. std::vector<const CArtifact*> arts;
  105. const auto * art = aid.toArtifact();
  106. if(art->isCombined())
  107. return arts;
  108. for(const auto artifact : art->getPartOf())
  109. {
  110. assert(artifact->isCombined());
  111. bool possible = true;
  112. for(const auto constituent : artifact->getConstituents()) //check if all constituents are available
  113. {
  114. if(!artSet->hasArt(constituent->getId(), false, false, false))
  115. {
  116. possible = false;
  117. break;
  118. }
  119. }
  120. if(possible)
  121. arts.push_back(artifact);
  122. }
  123. return arts;
  124. }
  125. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createScroll(const SpellID & sid)
  126. {
  127. auto ret = new CArtifactInstance(VLC->arth->objects[ArtifactID::SPELL_SCROLL]);
  128. auto bonus = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::SPELL,
  129. BonusSource::ARTIFACT_INSTANCE, -1, ArtifactID::SPELL_SCROLL, sid);
  130. ret->addNewBonus(bonus);
  131. return ret;
  132. }
  133. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(CArtifact * art)
  134. {
  135. assert(art);
  136. auto * artInst = new CArtifactInstance(art);
  137. if(art->isCombined())
  138. {
  139. for(const auto & part : art->getConstituents())
  140. artInst->addPart(ArtifactUtils::createNewArtifactInstance(part), ArtifactPosition::PRE_FIRST);
  141. }
  142. if(art->isGrowing())
  143. {
  144. auto bonus = std::make_shared<Bonus>();
  145. bonus->type = BonusType::LEVEL_COUNTER;
  146. bonus->val = 0;
  147. artInst->addNewBonus(bonus);
  148. }
  149. return artInst;
  150. }
  151. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(const ArtifactID & aid)
  152. {
  153. return ArtifactUtils::createNewArtifactInstance(VLC->arth->objects[aid]);
  154. }
  155. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createArtifact(CMap * map, const ArtifactID & aid, SpellID spellID)
  156. {
  157. CArtifactInstance * art = nullptr;
  158. if(aid.getNum() >= 0)
  159. {
  160. if(spellID == SpellID::NONE)
  161. {
  162. art = ArtifactUtils::createNewArtifactInstance(aid);
  163. }
  164. else
  165. {
  166. art = ArtifactUtils::createScroll(spellID);
  167. }
  168. }
  169. else
  170. {
  171. art = new CArtifactInstance(); // random, empty
  172. }
  173. map->addNewArtifactInstance(art);
  174. if(art->artType && art->isCombined())
  175. {
  176. for(auto & part : art->getPartsInfo())
  177. {
  178. map->addNewArtifactInstance(part.art);
  179. }
  180. }
  181. return art;
  182. }
  183. DLL_LINKAGE void ArtifactUtils::insertScrrollSpellName(std::string & description, const SpellID & sid)
  184. {
  185. // We expect scroll description to be like this: This scroll contains the [spell name] spell which is added
  186. // into spell book for as long as hero carries the scroll. So we want to replace text in [...] with a spell name.
  187. // However other language versions don't have name placeholder at all, so we have to be careful
  188. auto nameStart = description.find_first_of('[');
  189. auto nameEnd = description.find_first_of(']', nameStart);
  190. if(sid.getNum() >= 0)
  191. {
  192. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  193. description = description.replace(nameStart, nameEnd - nameStart + 1, sid.toSpell(VLC->spells())->getNameTranslated());
  194. }
  195. }
  196. VCMI_LIB_NAMESPACE_END