ArtifactUtils.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, bool equipped)
  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(equipped)
  116. {
  117. // Search for equipped arts
  118. if(!artSet->hasArt(constituent->getId(), true, false, false))
  119. {
  120. possible = false;
  121. break;
  122. }
  123. }
  124. else
  125. {
  126. // Search in backpack
  127. if(!artSet->hasArtBackpack(constituent->getId()))
  128. {
  129. possible = false;
  130. break;
  131. }
  132. }
  133. }
  134. if(possible)
  135. arts.push_back(artifact);
  136. }
  137. return arts;
  138. }
  139. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createScroll(const SpellID & sid)
  140. {
  141. auto ret = new CArtifactInstance(VLC->arth->objects[ArtifactID::SPELL_SCROLL]);
  142. auto bonus = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::SPELL,
  143. BonusSource::ARTIFACT_INSTANCE, -1, ArtifactID::SPELL_SCROLL, sid);
  144. ret->addNewBonus(bonus);
  145. return ret;
  146. }
  147. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(CArtifact * art)
  148. {
  149. assert(art);
  150. auto * artInst = new CArtifactInstance(art);
  151. if(art->isCombined())
  152. {
  153. for(const auto & part : art->getConstituents())
  154. artInst->addPart(ArtifactUtils::createNewArtifactInstance(part), ArtifactPosition::PRE_FIRST);
  155. }
  156. if(art->isGrowing())
  157. {
  158. auto bonus = std::make_shared<Bonus>();
  159. bonus->type = BonusType::LEVEL_COUNTER;
  160. bonus->val = 0;
  161. artInst->addNewBonus(bonus);
  162. }
  163. return artInst;
  164. }
  165. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createNewArtifactInstance(const ArtifactID & aid)
  166. {
  167. return ArtifactUtils::createNewArtifactInstance(VLC->arth->objects[aid]);
  168. }
  169. DLL_LINKAGE CArtifactInstance * ArtifactUtils::createArtifact(CMap * map, const ArtifactID & aid, SpellID spellID)
  170. {
  171. CArtifactInstance * art = nullptr;
  172. if(aid.getNum() >= 0)
  173. {
  174. if(spellID == SpellID::NONE)
  175. {
  176. art = ArtifactUtils::createNewArtifactInstance(aid);
  177. }
  178. else
  179. {
  180. art = ArtifactUtils::createScroll(spellID);
  181. }
  182. }
  183. else
  184. {
  185. art = new CArtifactInstance(); // random, empty
  186. }
  187. map->addNewArtifactInstance(art);
  188. if(art->artType && art->isCombined())
  189. {
  190. for(auto & part : art->getPartsInfo())
  191. {
  192. map->addNewArtifactInstance(part.art);
  193. }
  194. }
  195. return art;
  196. }
  197. DLL_LINKAGE void ArtifactUtils::insertScrrollSpellName(std::string & description, const SpellID & sid)
  198. {
  199. // We expect scroll description to be like this: This scroll contains the [spell name] spell which is added
  200. // into spell book for as long as hero carries the scroll. So we want to replace text in [...] with a spell name.
  201. // However other language versions don't have name placeholder at all, so we have to be careful
  202. auto nameStart = description.find_first_of('[');
  203. auto nameEnd = description.find_first_of(']', nameStart);
  204. if(sid.getNum() >= 0)
  205. {
  206. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  207. description = description.replace(nameStart, nameEnd - nameStart + 1, sid.toSpell(VLC->spells())->getNameTranslated());
  208. }
  209. }
  210. VCMI_LIB_NAMESPACE_END