ArtifactUtils.cpp 6.3 KB

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