ArtifactUtils.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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)
  79. {
  80. if(heroPtr && !heroPtr->hasSpellbook())
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(const ArtifactPosition & slot)
  87. {
  88. return slot >= ArtifactPosition::BACKPACK_START;
  89. }
  90. DLL_LINKAGE bool ArtifactUtils::isSlotEquipment(const ArtifactPosition & slot)
  91. {
  92. return slot < ArtifactPosition::BACKPACK_START && slot >= ArtifactPosition(0);
  93. }
  94. DLL_LINKAGE bool ArtifactUtils::isBackpackFreeSlots(const CArtifactSet * target, const size_t reqSlots)
  95. {
  96. const auto backpackCap = VLC->settings()->getInteger(EGameSettings::HEROES_BACKPACK_CAP);
  97. if(backpackCap < 0)
  98. return true;
  99. else
  100. return target->artifactsInBackpack.size() + reqSlots <= backpackCap;
  101. }
  102. DLL_LINKAGE std::vector<const CArtifact*> ArtifactUtils::assemblyPossibilities(
  103. const CArtifactSet * artSet, const ArtifactID & aid)
  104. {
  105. std::vector<const CArtifact*> arts;
  106. const auto * art = aid.toArtifact();
  107. if(art->isCombined())
  108. return arts;
  109. for(const auto artifact : art->getPartOf())
  110. {
  111. assert(artifact->isCombined());
  112. bool possible = true;
  113. for(const auto constituent : artifact->getConstituents()) //check if all constituents are available
  114. {
  115. if(!artSet->hasArt(constituent->getId(), false, false, false))
  116. {
  117. possible = false;
  118. break;
  119. }
  120. }
  121. if(possible)
  122. arts.push_back(artifact);
  123. }
  124. return arts;
  125. }
  126. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createScroll(const SpellID & sid)
  127. {
  128. auto ret = new CArtifactInstance(VLC->arth->objects[ArtifactID::SPELL_SCROLL]);
  129. auto bonus = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::SPELL,
  130. BonusSource::ARTIFACT_INSTANCE, -1, ArtifactID::SPELL_SCROLL, sid);
  131. ret->addNewBonus(bonus);
  132. return ret;
  133. }
  134. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(CArtifact * art)
  135. {
  136. assert(art);
  137. auto * artInst = new CArtifactInstance(art);
  138. if(art->isCombined())
  139. {
  140. assert(art->isCombined());
  141. for(const auto & part : art->getConstituents())
  142. artInst->addPart(ArtifactUtils::createNewArtifactInstance(part), ArtifactPosition::PRE_FIRST);
  143. }
  144. if(art->isGrowing())
  145. {
  146. auto bonus = std::make_shared<Bonus>();
  147. bonus->type = BonusType::LEVEL_COUNTER;
  148. bonus->val = 0;
  149. artInst->addNewBonus(bonus);
  150. }
  151. return artInst;
  152. }
  153. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(const ArtifactID & aid)
  154. {
  155. return ArtifactUtils::createNewArtifactInstance(VLC->arth->objects[aid]);
  156. }
  157. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createArtifact(CMap * map, const ArtifactID & aid, SpellID spellID)
  158. {
  159. CArtifactInstance * art = nullptr;
  160. if(aid.getNum() >= 0)
  161. {
  162. if(spellID == SpellID::NONE)
  163. {
  164. art = ArtifactUtils::createNewArtifactInstance(aid);
  165. }
  166. else
  167. {
  168. art = ArtifactUtils::createScroll(spellID);
  169. }
  170. }
  171. else
  172. {
  173. art = new CArtifactInstance(); // random, empty
  174. }
  175. map->addNewArtifactInstance(art);
  176. if(art->artType && art->isCombined())
  177. {
  178. for(auto & part : art->getPartsInfo())
  179. {
  180. map->addNewArtifactInstance(part.art);
  181. }
  182. }
  183. return art;
  184. }
  185. DLL_LINKAGE void ArtifactUtils::insertScrrollSpellName(std::string & description, const SpellID & sid)
  186. {
  187. // We expect scroll description to be like this: This scroll contains the [spell name] spell which is added
  188. // into spell book for as long as hero carries the scroll. So we want to replace text in [...] with a spell name.
  189. // However other language versions don't have name placeholder at all, so we have to be careful
  190. auto nameStart = description.find_first_of('[');
  191. auto nameEnd = description.find_first_of(']', nameStart);
  192. if(sid.getNum() >= 0)
  193. {
  194. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  195. description = description.replace(nameStart, nameEnd - nameStart + 1, sid.toSpell(VLC->spells())->getNameTranslated());
  196. }
  197. }
  198. VCMI_LIB_NAMESPACE_END