CArtifact.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * CArtifact.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 "CArtifact.h"
  12. #include "ArtifactUtils.h"
  13. #include "CArtifactFittingSet.h"
  14. #include "../../bonuses/Limiters.h"
  15. #include "../../texts/CGeneralTextHandler.h"
  16. #include "../../GameLibrary.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. bool CCombinedArtifact::isCombined() const
  19. {
  20. return !(constituents.empty());
  21. }
  22. const std::vector<const CArtifact*> & CCombinedArtifact::getConstituents() const
  23. {
  24. return constituents;
  25. }
  26. const std::set<const CArtifact*> & CCombinedArtifact::getPartOf() const
  27. {
  28. return partOf;
  29. }
  30. void CCombinedArtifact::setFused(bool isFused)
  31. {
  32. fused = isFused;
  33. }
  34. bool CCombinedArtifact::isFused() const
  35. {
  36. return fused;
  37. }
  38. bool CCombinedArtifact::hasParts() const
  39. {
  40. return isCombined() && !isFused();
  41. }
  42. bool CScrollArtifact::isScroll() const
  43. {
  44. return static_cast<const CArtifact*>(this)->getId() == ArtifactID::SPELL_SCROLL;
  45. }
  46. bool CGrowingArtifact::isGrowing() const
  47. {
  48. return !bonusesPerLevel.empty() || !thresholdBonuses.empty();
  49. }
  50. std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getBonusesPerLevel()
  51. {
  52. return bonusesPerLevel;
  53. }
  54. const std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getBonusesPerLevel() const
  55. {
  56. return bonusesPerLevel;
  57. }
  58. std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getThresholdBonuses()
  59. {
  60. return thresholdBonuses;
  61. }
  62. const std::vector <std::pair<ui16, std::shared_ptr<Bonus>>> & CGrowingArtifact::getThresholdBonuses() const
  63. {
  64. return thresholdBonuses;
  65. }
  66. int32_t CArtifact::getIndex() const
  67. {
  68. return id.toEnum();
  69. }
  70. int32_t CArtifact::getIconIndex() const
  71. {
  72. return iconIndex;
  73. }
  74. std::string CArtifact::getJsonKey() const
  75. {
  76. return modScope + ':' + identifier;
  77. }
  78. std::string CArtifact::getModScope() const
  79. {
  80. return modScope;
  81. }
  82. void CArtifact::registerIcons(const IconRegistar & cb) const
  83. {
  84. cb(getIconIndex(), 0, "ARTIFACT", image);
  85. }
  86. ArtifactID CArtifact::getId() const
  87. {
  88. return id;
  89. }
  90. const IBonusBearer * CArtifact::getBonusBearer() const
  91. {
  92. return this;
  93. }
  94. std::string CArtifact::getDescriptionTranslated() const
  95. {
  96. return LIBRARY->generaltexth->translate(getDescriptionTextID());
  97. }
  98. std::string CArtifact::getEventTranslated() const
  99. {
  100. return LIBRARY->generaltexth->translate(getEventTextID());
  101. }
  102. std::string CArtifact::getNameTranslated() const
  103. {
  104. return LIBRARY->generaltexth->translate(getNameTextID());
  105. }
  106. std::string CArtifact::getDescriptionTextID() const
  107. {
  108. return TextIdentifier("artifact", modScope, identifier, "description").get();
  109. }
  110. std::string CArtifact::getEventTextID() const
  111. {
  112. return TextIdentifier("artifact", modScope, identifier, "event").get();
  113. }
  114. std::string CArtifact::getNameTextID() const
  115. {
  116. return TextIdentifier("artifact", modScope, identifier, "name").get();
  117. }
  118. std::string CArtifact::getBonusTextID(const std::string & bonusID) const
  119. {
  120. return TextIdentifier("artifact", modScope, identifier, "bonus", bonusID).get();
  121. }
  122. uint32_t CArtifact::getPrice() const
  123. {
  124. return price;
  125. }
  126. CreatureID CArtifact::getWarMachine() const
  127. {
  128. return warMachine;
  129. }
  130. bool CArtifact::isBig() const
  131. {
  132. return warMachine != CreatureID::NONE;
  133. }
  134. bool CArtifact::isTradable() const
  135. {
  136. switch(id.toEnum())
  137. {
  138. case ArtifactID::SPELLBOOK:
  139. return false;
  140. default:
  141. return !isBig();
  142. }
  143. }
  144. bool CArtifact::canBePutAt(const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) const
  145. {
  146. auto simpleArtCanBePutAt = [this](const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) -> bool
  147. {
  148. if(artSet->bearerType() == ArtBearer::HERO && ArtifactUtils::isSlotBackpack(slot))
  149. {
  150. if(isBig() || (!assumeDestRemoved && !ArtifactUtils::isBackpackFreeSlots(artSet)))
  151. return false;
  152. return true;
  153. }
  154. if(!vstd::contains(possibleSlots.at(artSet->bearerType()), slot))
  155. return false;
  156. return artSet->isPositionFree(slot, assumeDestRemoved);
  157. };
  158. auto artCanBePutAt = [this, simpleArtCanBePutAt](const CArtifactSet * artSet, ArtifactPosition slot, bool assumeDestRemoved) -> bool
  159. {
  160. if(hasParts())
  161. {
  162. if(!simpleArtCanBePutAt(artSet, slot, assumeDestRemoved))
  163. return false;
  164. if(ArtifactUtils::isSlotBackpack(slot))
  165. return true;
  166. CArtifactFittingSet fittingSet(artSet->getCallback(), artSet->bearerType());
  167. fittingSet.artifactsWorn = artSet->artifactsWorn;
  168. if(assumeDestRemoved)
  169. fittingSet.removeArtifact(slot);
  170. for(const auto art : constituents)
  171. {
  172. auto possibleSlot = ArtifactUtils::getArtAnyPosition(&fittingSet, art->getId());
  173. if(ArtifactUtils::isSlotEquipment(possibleSlot))
  174. {
  175. if (fittingSet.getSlot(possibleSlot) == nullptr)
  176. fittingSet.artifactsWorn.insert(std::make_pair(possibleSlot, ArtSlotInfo(fittingSet.cb)));
  177. fittingSet.lockSlot(possibleSlot);
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. else
  187. {
  188. return simpleArtCanBePutAt(artSet, slot, assumeDestRemoved);
  189. }
  190. };
  191. if(slot == ArtifactPosition::TRANSITION_POS)
  192. return true;
  193. if(slot == ArtifactPosition::FIRST_AVAILABLE)
  194. {
  195. for(const auto & possibleSlot : possibleSlots.at(artSet->bearerType()))
  196. {
  197. if(artCanBePutAt(artSet, possibleSlot, assumeDestRemoved))
  198. return true;
  199. }
  200. return artCanBePutAt(artSet, ArtifactPosition::BACKPACK_START, assumeDestRemoved);
  201. }
  202. else if(ArtifactUtils::isSlotBackpack(slot))
  203. {
  204. return artCanBePutAt(artSet, ArtifactPosition::BACKPACK_START, assumeDestRemoved);
  205. }
  206. else
  207. {
  208. return artCanBePutAt(artSet, slot, assumeDestRemoved);
  209. }
  210. }
  211. CChargedArtifact::CChargedArtifact()
  212. : condition(DischargeArtifactCondition::NONE)
  213. , removeOnDepletion(false)
  214. , defaultStartCharges(0)
  215. {
  216. }
  217. bool CChargedArtifact::isCharged() const
  218. {
  219. return condition != DischargeArtifactCondition::NONE;
  220. }
  221. void CChargedArtifact::setCondition(const DischargeArtifactCondition & dischargeCondition)
  222. {
  223. condition = dischargeCondition;
  224. }
  225. void CChargedArtifact::setRemoveOnDepletion(const bool remove)
  226. {
  227. removeOnDepletion = remove;
  228. }
  229. void CChargedArtifact::setDefaultStartCharges(const uint16_t charges)
  230. {
  231. defaultStartCharges = charges;
  232. }
  233. uint16_t CChargedArtifact::getDefaultStartCharges() const
  234. {
  235. return defaultStartCharges;
  236. }
  237. DischargeArtifactCondition CChargedArtifact::getDischargeCondition() const
  238. {
  239. return condition;
  240. }
  241. bool CChargedArtifact::getRemoveOnDepletion() const
  242. {
  243. return removeOnDepletion;
  244. }
  245. std::optional<uint16_t> CChargedArtifact::getChargeCost(const SpellID & id) const
  246. {
  247. auto art = static_cast<const CArtifact*>(this);
  248. for(const auto & bonus : art->instanceBonuses)
  249. {
  250. if(bonus->type == BonusType::SPELL && bonus->subtype.as<SpellID>() == id)
  251. {
  252. if(const auto chargesLimiter = std::static_pointer_cast<const HasChargesLimiter>(bonus->limiter))
  253. return chargesLimiter->chargeCost;
  254. }
  255. }
  256. return std::nullopt;
  257. }
  258. CArtifact::CArtifact()
  259. : CBonusSystemNode(BonusNodeType::ARTIFACT),
  260. iconIndex(ArtifactID::NONE),
  261. price(0)
  262. {
  263. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  264. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  265. possibleSlots[ArtBearer::COMMANDER];
  266. possibleSlots[ArtBearer::ALTAR];
  267. }
  268. //This destructor should be placed here to avoid side effects
  269. CArtifact::~CArtifact() = default;
  270. int CArtifact::getArtClassSerial() const
  271. {
  272. if(id == ArtifactID::SPELL_SCROLL)
  273. return 4;
  274. switch(aClass)
  275. {
  276. case EArtifactClass::ART_TREASURE:
  277. return 0;
  278. case EArtifactClass::ART_MINOR:
  279. return 1;
  280. case EArtifactClass::ART_MAJOR:
  281. return 2;
  282. case EArtifactClass::ART_RELIC:
  283. return 3;
  284. case EArtifactClass::ART_SPECIAL:
  285. return 5;
  286. }
  287. return -1;
  288. }
  289. std::string CArtifact::nodeName() const
  290. {
  291. return "Artifact: " + getNameTranslated();
  292. }
  293. void CArtifact::addNewBonus(const std::shared_ptr<Bonus>& b)
  294. {
  295. b->source = BonusSource::ARTIFACT;
  296. b->duration = BonusDuration::PERMANENT;
  297. if (b->description.empty() && (b->type == BonusType::LUCK || b->type == BonusType::MORALE))
  298. {
  299. b->description.appendTextID(getNameTextID());
  300. b->description.appendRawString(" %+d");
  301. }
  302. CBonusSystemNode::addNewBonus(b);
  303. }
  304. const std::map<ArtBearer, std::vector<ArtifactPosition>> & CArtifact::getPossibleSlots() const
  305. {
  306. return possibleSlots;
  307. }
  308. void CArtifact::updateFrom(const JsonNode& data)
  309. {
  310. //TODO:CArtifact::updateFrom
  311. }
  312. void CArtifact::setImage(int32_t newIconIndex, const std::string & newImage, const std::string & newLargeImage)
  313. {
  314. iconIndex = newIconIndex;
  315. image = newImage;
  316. scenarioBonus = newLargeImage;
  317. }
  318. VCMI_LIB_NAMESPACE_END