CArtHandler.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. #include "StdInc.h"
  2. #include "CArtHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "CGeneralTextHandler.h"
  5. #include <boost/random/linear_congruential.hpp>
  6. #include "VCMI_Lib.h"
  7. #include "CModHandler.h"
  8. #include "CSpellHandler.h"
  9. #include "CObjectHandler.h"
  10. #include "NetPacks.h"
  11. #include "GameConstants.h"
  12. using namespace boost::assign;
  13. /*
  14. * CArtHandler.cpp, part of VCMI engine
  15. *
  16. * Authors: listed in file AUTHORS in main folder
  17. *
  18. * License: GNU General Public License v2.0 or later
  19. * Full text of license available in license.txt file, in main folder
  20. *
  21. */
  22. extern boost::rand48 ran;
  23. // Note: list must match entries in ArtTraits.txt
  24. #define ART_POS_LIST \
  25. ART_POS(SPELLBOOK) \
  26. ART_POS(MACH4) \
  27. ART_POS(MACH3) \
  28. ART_POS(MACH2) \
  29. ART_POS(MACH1) \
  30. ART_POS(MISC5) \
  31. ART_POS(MISC4) \
  32. ART_POS(MISC3) \
  33. ART_POS(MISC2) \
  34. ART_POS(MISC1) \
  35. ART_POS(FEET) \
  36. ART_POS(LEFT_RING) \
  37. ART_POS(RIGHT_RING) \
  38. ART_POS(TORSO) \
  39. ART_POS(LEFT_HAND) \
  40. ART_POS(RIGHT_HAND) \
  41. ART_POS(NECK) \
  42. ART_POS(SHOULDERS) \
  43. ART_POS(HEAD);
  44. const std::string & CArtifact::Name() const
  45. {
  46. return name;
  47. }
  48. const std::string & CArtifact::Description() const
  49. {
  50. return description;
  51. }
  52. const std::string & CArtifact::EventText() const
  53. {
  54. return eventText;
  55. }
  56. bool CArtifact::isBig () const
  57. {
  58. return VLC->arth->isBigArtifact(id);
  59. }
  60. CArtifact::CArtifact()
  61. {
  62. setNodeType(ARTIFACT);
  63. possibleSlots[ArtBearer::HERO]; //we want to generate map entry even if it will be empty
  64. possibleSlots[ArtBearer::CREATURE]; //we want to generate map entry even if it will be empty
  65. possibleSlots[ArtBearer::COMMANDER];
  66. }
  67. CArtifact::~CArtifact()
  68. {
  69. }
  70. int CArtifact::getArtClassSerial() const
  71. {
  72. if(id == 1)
  73. return 4;
  74. switch(aClass)
  75. {
  76. case ART_TREASURE:
  77. return 0;
  78. case ART_MINOR:
  79. return 1;
  80. case ART_MAJOR:
  81. return 2;
  82. case ART_RELIC:
  83. return 3;
  84. case ART_SPECIAL:
  85. return 5;
  86. }
  87. return -1;
  88. }
  89. std::string CArtifact::nodeName() const
  90. {
  91. return "Artifact: " + Name();
  92. }
  93. void CArtifact::addNewBonus(Bonus *b)
  94. {
  95. b->source = Bonus::ARTIFACT;
  96. b->duration = Bonus::PERMANENT;
  97. b->description = name;
  98. CBonusSystemNode::addNewBonus(b);
  99. }
  100. void CGrowingArtifact::levelUpArtifact (CArtifactInstance * art)
  101. {
  102. Bonus b;
  103. b.type = Bonus::LEVEL_COUNTER;
  104. b.val = 1;
  105. b.duration = Bonus::COMMANDER_KILLED;
  106. art->accumulateBonus (b);
  107. BOOST_FOREACH (auto bonus, bonusesPerLevel)
  108. {
  109. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) % bonus.first == 0) //every n levels
  110. {
  111. art->accumulateBonus (bonus.second);
  112. }
  113. }
  114. BOOST_FOREACH (auto bonus, thresholdBonuses)
  115. {
  116. if (art->valOfBonuses(Bonus::LEVEL_COUNTER) == bonus.first) //every n levels
  117. {
  118. art->addNewBonus (&bonus.second);
  119. }
  120. }
  121. }
  122. CArtHandler::CArtHandler()
  123. {
  124. //VLC->arth = this;
  125. // War machines are the default big artifacts.
  126. for (ArtifactID i = ArtifactID::CATAPULT; i <= ArtifactID::FIRST_AID_TENT; i.advance(1))
  127. bigArtifacts.insert(i);
  128. }
  129. CArtHandler::~CArtHandler()
  130. {
  131. }
  132. void CArtHandler::load(bool onlyTxt)
  133. {
  134. if (onlyTxt)
  135. return; // looks to be broken anyway...
  136. #define ART_POS(x) ( #x)
  137. const std::vector<std::string> artSlots = boost::assign::list_of ART_POS_LIST;
  138. #undef ART_POS
  139. static std::map<char, std::string> classes =
  140. map_list_of('S',"SPECIAL")('T',"TREASURE")('N',"MINOR")('J',"MAJOR")('R',"RELIC");
  141. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  142. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  143. parser.endLine(); // header
  144. parser.endLine();
  145. std::map<ui32,ui8>::iterator itr;
  146. std::vector<JsonNode> h3Data;
  147. for (size_t i = 0; i < GameConstants::ARTIFACTS_QUANTITY; i++)
  148. {
  149. JsonNode artData;
  150. artData["graphics"]["iconIndex"].Float() = i;
  151. artData["text"]["name"].String() = parser.readString();
  152. artData["text"]["event"].String() = events.readString();
  153. artData["value"].Float() = parser.readNumber();
  154. for(size_t j=0; j<artSlots.size(); ++j)
  155. {
  156. if(parser.readString() == "x")
  157. {
  158. artData["slot"].Vector().push_back(JsonNode());
  159. artData["slot"].Vector().back().String() = artSlots.at(j);
  160. }
  161. }
  162. artData["class"].String() = classes[parser.readString()[0]];
  163. artData["text"]["description"].String() = parser.readString();
  164. parser.endLine();
  165. events.endLine();
  166. h3Data.push_back(artData);
  167. }
  168. artifacts.resize(GameConstants::ARTIFACTS_QUANTITY);
  169. JsonNode config(ResourceID("config/artifacts.json"));
  170. BOOST_FOREACH(auto & node, config["artifacts"].Struct())
  171. {
  172. int numeric = node.second["id"].Float();
  173. JsonNode & artData = h3Data[numeric];
  174. JsonUtils::merge(artData, node.second);
  175. artifacts[numeric] = loadArtifact(artData);
  176. artifacts[numeric]->id = ArtifactID(numeric);
  177. VLC->modh->identifiers.registerObject ("artifact." + node.first, numeric);
  178. }
  179. for (size_t i=0; i < artifacts.size(); i++)
  180. {
  181. if (artifacts[i] == nullptr)
  182. tlog0 << "Warning: artifact with id " << i << " is missing!\n";
  183. }
  184. }
  185. void CArtHandler::load(std::string objectID, const JsonNode & node)
  186. {
  187. CArtifact * art = loadArtifact(node);
  188. art->id = ArtifactID(artifacts.size());
  189. artifacts.push_back(art);
  190. tlog5 << "Added artifact: " << objectID << "\n";
  191. VLC->modh->identifiers.registerObject ("artifact." + objectID, art->id);
  192. }
  193. CArtifact * CArtHandler::loadArtifact(const JsonNode & node)
  194. {
  195. CArtifact * art;
  196. if (!VLC->modh->modules.COMMANDERS || node["growing"].isNull())
  197. art = new CArtifact();
  198. else
  199. {
  200. CGrowingArtifact * growing = new CGrowingArtifact();
  201. loadGrowingArt(growing, node);
  202. art = growing;
  203. }
  204. const JsonNode & text = node["text"];
  205. art->name = text["name"].String();
  206. art->description = text["description"].String();
  207. art->eventText = text["event"].String();
  208. const JsonNode & graphics = node["graphics"];
  209. art->iconIndex = graphics["iconIndex"].asInteger();
  210. art->image = graphics["image"].String();
  211. if (!graphics["large"].loadTo(art->large))
  212. art->large = art->image;
  213. art->advMapDef = graphics["map"].String();
  214. art->price = node["value"].asInteger();
  215. loadSlots(art, node);
  216. loadClass(art, node);
  217. loadType(art, node);
  218. loadComponents(art, node);
  219. BOOST_FOREACH (auto b, node["bonuses"].Vector())
  220. {
  221. auto bonus = JsonUtils::parseBonus (b);
  222. bonus->sid = art->id;
  223. art->addNewBonus (bonus);
  224. }
  225. return art;
  226. }
  227. void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
  228. {
  229. #define ART_POS(x) ( #x, ArtifactPosition::x )
  230. static const std::map<std::string, ArtifactPosition> artifactPositionMap = boost::assign::map_list_of ART_POS_LIST;
  231. #undef ART_POS
  232. if (slotID == "MISC")
  233. {
  234. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  235. }
  236. else if (slotID == "RING")
  237. {
  238. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  239. }
  240. else
  241. {
  242. auto it = artifactPositionMap.find (slotID);
  243. if (it != artifactPositionMap.end())
  244. {
  245. auto slot = it->second;
  246. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  247. }
  248. else
  249. tlog2 << "Warning! Artifact slot " << slotID << " not recognized!";
  250. }
  251. }
  252. void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node)
  253. {
  254. if (!node["slot"].isNull()) //we assume non-hero slots are irrelevant?
  255. {
  256. if (node["slot"].getType() == JsonNode::DATA_STRING)
  257. addSlot(art, node["slot"].String());
  258. else
  259. {
  260. BOOST_FOREACH (const JsonNode & slot, node["slot"].Vector())
  261. addSlot(art, slot.String());
  262. }
  263. }
  264. }
  265. void CArtHandler::loadClass(CArtifact * art, const JsonNode & node)
  266. {
  267. static const std::map<std::string, CArtifact::EartClass> artifactClassMap = boost::assign::map_list_of
  268. ("TREASURE", CArtifact::ART_TREASURE)
  269. ("MINOR", CArtifact::ART_MINOR)
  270. ("MAJOR", CArtifact::ART_MAJOR)
  271. ("RELIC", CArtifact::ART_RELIC)
  272. ("SPECIAL", CArtifact::ART_SPECIAL);
  273. auto it = artifactClassMap.find (node["class"].String());
  274. if (it != artifactClassMap.end())
  275. {
  276. art->aClass = it->second;
  277. }
  278. else
  279. {
  280. tlog2 << "Warning! Artifact rarity " << node["class"].String() << " not recognized!";
  281. art->aClass = CArtifact::ART_SPECIAL;
  282. }
  283. }
  284. void CArtHandler::loadType(CArtifact * art, const JsonNode & node)
  285. {
  286. #define ART_BEARER(x) ( #x, ArtBearer::x )
  287. static const std::map<std::string, int> artifactBearerMap = boost::assign::map_list_of ART_BEARER_LIST;
  288. #undef ART_BEARER
  289. BOOST_FOREACH (const JsonNode & b, node["type"].Vector())
  290. {
  291. auto it = artifactBearerMap.find (b.String());
  292. if (it != artifactBearerMap.end())
  293. {
  294. int bearerType = it->second;
  295. switch (bearerType)
  296. {
  297. case ArtBearer::HERO://TODO: allow arts having several possible bearers
  298. break;
  299. case ArtBearer::COMMANDER:
  300. makeItCommanderArt (art); //original artifacts should have only one bearer type
  301. break;
  302. case ArtBearer::CREATURE:
  303. makeItCreatureArt (art);
  304. break;
  305. }
  306. }
  307. else
  308. tlog2 << "Warning! Artifact type " << b.String() << " not recognized!";
  309. }
  310. }
  311. void CArtHandler::loadComponents(CArtifact * art, const JsonNode & node)
  312. {
  313. if (!node["components"].isNull())
  314. {
  315. art->constituents.reset(new std::vector<CArtifact *>());
  316. BOOST_FOREACH (auto component, node["components"].Vector())
  317. {
  318. VLC->modh->identifiers.requestIdentifier("artifact." + component.String(), [=](si32 id)
  319. {
  320. // when this code is called both combinational art as well as component are loaded
  321. // so it is safe to access any of them
  322. art->constituents->push_back(VLC->arth->artifacts[id]);
  323. VLC->arth->artifacts[id]->constituentOf.push_back(art);
  324. });
  325. }
  326. }
  327. }
  328. void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node)
  329. {
  330. BOOST_FOREACH (auto b, node["growing"]["bonusesPerLevel"].Vector())
  331. {
  332. art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  333. }
  334. BOOST_FOREACH (auto b, node["growing"]["thresholdBonuses"].Vector())
  335. {
  336. art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"].Vector())));
  337. }
  338. }
  339. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  340. {
  341. int dif = 142;
  342. switch (id)
  343. {
  344. case 147:
  345. dif--;
  346. break;
  347. case 148:
  348. dif++;
  349. break;
  350. }
  351. dif = -dif;
  352. return ArtifactID(id+dif);
  353. }
  354. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  355. {
  356. int dif = 142;
  357. switch (id)
  358. {
  359. case 6:
  360. dif--;
  361. break;
  362. case 5:
  363. dif++;
  364. break;
  365. }
  366. return CreatureID(id + dif);
  367. }
  368. ArtifactID CArtHandler::getRandomArt(int flags)
  369. {
  370. return getArtSync(ran(), flags, true);
  371. }
  372. ArtifactID CArtHandler::getArtSync (ui32 rand, int flags, bool erasePicked)
  373. {
  374. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  375. {
  376. if (arts->empty()) //restock available arts
  377. fillList(*arts, flag);
  378. for (size_t i = 0; i < arts->size(); ++i)
  379. {
  380. CArtifact *art = (*arts)[i];
  381. out.push_back(art);
  382. }
  383. };
  384. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  385. {
  386. if (flags & CArtifact::ART_TREASURE)
  387. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  388. if (flags & CArtifact::ART_MINOR)
  389. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  390. if (flags & CArtifact::ART_MAJOR)
  391. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  392. if (flags & CArtifact::ART_RELIC)
  393. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  394. if (!out.size()) //no artifact of specified rarity, we need to take another one
  395. {
  396. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  397. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  398. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  399. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  400. }
  401. if (!out.size()) //no arts are available at all
  402. {
  403. out.resize (64);
  404. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  405. }
  406. //tlog0 << "Treasure count: " << treasures.size() << std::endl;
  407. };
  408. std::vector<ConstTransitivePtr<CArtifact> > out;
  409. getAllowed(out);
  410. ArtifactID artID = out[rand % out.size()]->id;
  411. if(erasePicked)
  412. erasePickedArt (artID);
  413. return artID;
  414. }
  415. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  416. {
  417. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  418. added->additionalInfo = additionalInfo;
  419. added->valType = valType;
  420. added->limiter = limiter;
  421. return added;
  422. }
  423. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  424. {
  425. Bonus *added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  426. added->additionalInfo = additionalInfo;
  427. added->valType = Bonus::BASE_NUMBER;
  428. added->propagator = propagator;
  429. return added;
  430. }
  431. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  432. {
  433. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  434. }
  435. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= NULL*/, int additionalInfo)
  436. {
  437. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  438. }
  439. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  440. {
  441. bonus->sid = aid;
  442. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  443. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  444. else
  445. bonus->description = artifacts[aid]->Name();
  446. artifacts[aid]->addNewBonus(bonus);
  447. }
  448. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  449. {
  450. if (onlyCreature)
  451. {
  452. a->possibleSlots[ArtBearer::HERO].clear();
  453. a->possibleSlots[ArtBearer::COMMANDER].clear();
  454. }
  455. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  456. }
  457. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  458. {
  459. CArtifact *a = artifacts[aid];
  460. makeItCreatureArt (a, onlyCreature);
  461. }
  462. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  463. {
  464. if (onlyCommander)
  465. {
  466. a->possibleSlots[ArtBearer::HERO].clear();
  467. a->possibleSlots[ArtBearer::CREATURE].clear();
  468. }
  469. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  470. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  471. }
  472. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  473. {
  474. CArtifact *a = artifacts[aid];
  475. makeItCommanderArt (a, onlyCommander);
  476. }
  477. bool CArtHandler::legalArtifact(ArtifactID id)
  478. {
  479. auto art = artifacts[id];
  480. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  481. return (art->possibleSlots[ArtBearer::HERO].size() ||
  482. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  483. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  484. !(art->constituents); //no combo artifacts spawning
  485. }
  486. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  487. {
  488. allowedArtifacts.clear();
  489. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  490. {
  491. if (allowed[i] && legalArtifact(i))
  492. allowedArtifacts.push_back(artifacts[i]);
  493. }
  494. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  495. {
  496. for (int i = 146; i <= 155; ++i)
  497. {
  498. allowedArtifacts.push_back(artifacts[i]);
  499. }
  500. }
  501. for (size_t i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  502. {
  503. if (legalArtifact(ArtifactID(i)))
  504. allowedArtifacts.push_back(artifacts[i]);
  505. //keep im mind that artifact can be worn by more than one type of bearer
  506. }
  507. }
  508. std::vector<bool> CArtHandler::getDefaultAllowedArtifacts() const
  509. {
  510. std::vector<bool> allowedArtifacts;
  511. allowedArtifacts.resize(127, true);
  512. allowedArtifacts.resize(141, false);
  513. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  514. return allowedArtifacts;
  515. }
  516. void CArtHandler::erasePickedArt(ArtifactID id)
  517. {
  518. CArtifact *art = artifacts[id];
  519. if(auto artifactList = listFromClass(art->aClass))
  520. {
  521. if(artifactList->empty())
  522. fillList(*artifactList, art->aClass);
  523. auto itr = vstd::find(*artifactList, art);
  524. if(itr != artifactList->end())
  525. {
  526. artifactList->erase(itr);
  527. }
  528. else
  529. tlog2 << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present\n";
  530. }
  531. else
  532. tlog2 << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)\n";
  533. }
  534. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  535. {
  536. switch(artifactClass)
  537. {
  538. case CArtifact::ART_TREASURE:
  539. return treasures;
  540. case CArtifact::ART_MINOR:
  541. return minors;
  542. case CArtifact::ART_MAJOR:
  543. return majors;
  544. case CArtifact::ART_RELIC:
  545. return relics;
  546. default: //special artifacts should not be erased
  547. return 0;
  548. }
  549. }
  550. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  551. {
  552. assert(listToBeFilled.empty());
  553. for (size_t i = 0; i < allowedArtifacts.size(); ++i)
  554. {
  555. if (allowedArtifacts[i]->aClass == artifactClass)
  556. listToBeFilled.push_back(allowedArtifacts[i]);
  557. }
  558. }
  559. CArtifactInstance::CArtifactInstance()
  560. {
  561. init();
  562. }
  563. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  564. {
  565. init();
  566. setType(Art);
  567. }
  568. void CArtifactInstance::setType( CArtifact *Art )
  569. {
  570. artType = Art;
  571. attachTo(Art);
  572. }
  573. std::string CArtifactInstance::nodeName() const
  574. {
  575. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  576. }
  577. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  578. {
  579. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  580. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  581. ret->addNewBonus(b);
  582. return ret;
  583. }
  584. void CArtifactInstance::init()
  585. {
  586. id = ArtifactInstanceID();
  587. setNodeType(ARTIFACT_INSTANCE);
  588. }
  589. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  590. {
  591. BOOST_FOREACH(auto slot, artType->possibleSlots[h->bearerType()])
  592. {
  593. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  594. {
  595. //we've found a free suitable slot.
  596. return slot;
  597. }
  598. }
  599. //if haven't find proper slot, use backpack
  600. return firstBackpackSlot(h);
  601. }
  602. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  603. {
  604. if(!artType->isBig()) //discard big artifact
  605. return ArtifactPosition(
  606. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  607. return ArtifactPosition::PRE_FIRST;
  608. }
  609. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  610. {
  611. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  612. }
  613. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  614. {
  615. if(slot >= GameConstants::BACKPACK_START)
  616. {
  617. if(artType->isBig())
  618. return false;
  619. //TODO backpack limit
  620. return true;
  621. }
  622. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  623. if(possibleSlots == artType->possibleSlots.end())
  624. {
  625. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  626. << artSet->bearerType() << std::endl;
  627. return false;
  628. }
  629. if(!vstd::contains(possibleSlots->second, slot))
  630. return false;
  631. return artSet->isPositionFree(slot, assumeDestRemoved);
  632. }
  633. void CArtifactInstance::putAt(ArtifactLocation al)
  634. {
  635. assert(canBePutAt(al));
  636. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  637. if(al.slot < GameConstants::BACKPACK_START)
  638. al.getHolderNode()->attachTo(this);
  639. }
  640. void CArtifactInstance::removeFrom(ArtifactLocation al)
  641. {
  642. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  643. al.getHolderArtSet()->eraseArtSlot(al.slot);
  644. if(al.slot < GameConstants::BACKPACK_START)
  645. al.getHolderNode()->detachFrom(this);
  646. //TODO delete me?
  647. }
  648. bool CArtifactInstance::canBeDisassembled() const
  649. {
  650. return artType->constituents != nullptr;
  651. }
  652. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  653. {
  654. std::vector<const CArtifact *> ret;
  655. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  656. return ret;
  657. BOOST_FOREACH(const CArtifact * artifact, artType->constituentOf)
  658. {
  659. assert(artifact->constituents);
  660. bool possible = true;
  661. BOOST_FOREACH(const CArtifact * constituent, *artifact->constituents) //check if all constituents are available
  662. {
  663. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  664. {
  665. possible = false;
  666. break;
  667. }
  668. }
  669. if(possible)
  670. ret.push_back(artifact);
  671. }
  672. return ret;
  673. }
  674. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  675. {
  676. removeFrom(src);
  677. putAt(dst);
  678. }
  679. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  680. {
  681. if(!Art->constituents)
  682. {
  683. auto ret = new CArtifactInstance(Art);
  684. if (dynamic_cast<CGrowingArtifact *>(Art))
  685. {
  686. Bonus * bonus = new Bonus;
  687. bonus->type = Bonus::LEVEL_COUNTER;
  688. bonus->val = 0;
  689. ret->addNewBonus (bonus);
  690. }
  691. return ret;
  692. }
  693. else
  694. {
  695. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  696. ret->createConstituents();
  697. return ret;
  698. }
  699. }
  700. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  701. {
  702. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  703. }
  704. void CArtifactInstance::deserializationFix()
  705. {
  706. setType(artType);
  707. }
  708. SpellID CArtifactInstance::getGivenSpellID() const
  709. {
  710. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  711. if(!b)
  712. {
  713. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  714. return SpellID::NONE;
  715. }
  716. return SpellID(b->subtype);
  717. }
  718. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  719. {
  720. return supposedPart == this;
  721. }
  722. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  723. {
  724. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  725. if(!canMainArtifactBePlaced)
  726. return false; //no is no...
  727. if(slot >= GameConstants::BACKPACK_START)
  728. return true; //we can always remove combined art to the backapck
  729. assert(artType->constituents);
  730. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  731. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  732. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  733. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  734. {
  735. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  736. constituentsToBePlaced -= constituent;
  737. }
  738. //we iterate over all active slots and check if constituents fits them
  739. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  740. {
  741. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  742. {
  743. if(art->art->canBePutAt(artSet, ArtifactPosition(i), i == slot)) // i == al.slot because we can remove already worn artifact only from that slot that is our main destination
  744. {
  745. constituentsToBePlaced.erase(art);
  746. break;
  747. }
  748. }
  749. }
  750. return constituentsToBePlaced.empty();
  751. }
  752. bool CCombinedArtifactInstance::canBeDisassembled() const
  753. {
  754. return true;
  755. }
  756. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  757. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  758. {
  759. }
  760. CCombinedArtifactInstance::CCombinedArtifactInstance()
  761. {
  762. }
  763. void CCombinedArtifactInstance::createConstituents()
  764. {
  765. assert(artType);
  766. assert(artType->constituents);
  767. BOOST_FOREACH(const CArtifact * art, *artType->constituents)
  768. {
  769. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  770. }
  771. }
  772. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  773. {
  774. assert(vstd::contains(*artType->constituents, art->artType.get()));
  775. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  776. constituentsInfo.push_back(ConstituentInfo(art, slot));
  777. attachTo(art);
  778. }
  779. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  780. {
  781. if(al.slot >= GameConstants::BACKPACK_START)
  782. {
  783. CArtifactInstance::putAt(al);
  784. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  785. ci.slot = ArtifactPosition::PRE_FIRST;
  786. }
  787. else
  788. {
  789. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  790. CArtifactInstance::putAt(al); //puts combined art (this)
  791. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  792. {
  793. if(ci.art != mainConstituent)
  794. {
  795. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  796. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  797. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  798. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  799. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  800. pos = ci.slot;
  801. else
  802. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  803. assert(pos < GameConstants::BACKPACK_START);
  804. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  805. }
  806. else
  807. {
  808. ci.slot = ArtifactPosition::PRE_FIRST;
  809. }
  810. }
  811. }
  812. }
  813. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  814. {
  815. if(al.slot >= GameConstants::BACKPACK_START)
  816. {
  817. CArtifactInstance::removeFrom(al);
  818. }
  819. else
  820. {
  821. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  822. {
  823. if(ci.slot >= 0)
  824. {
  825. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  826. ci.slot = ArtifactPosition::PRE_FIRST;
  827. }
  828. else
  829. {
  830. //main constituent
  831. CArtifactInstance::removeFrom(al);
  832. }
  833. }
  834. }
  835. }
  836. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  837. {
  838. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  839. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  840. if(ci.slot == al.slot)
  841. mainConstituent = ci.art;
  842. if(!mainConstituent)
  843. {
  844. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  845. {
  846. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  847. {
  848. mainConstituent = ci.art;
  849. }
  850. }
  851. }
  852. return mainConstituent;
  853. }
  854. void CCombinedArtifactInstance::deserializationFix()
  855. {
  856. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  857. attachTo(ci.art);
  858. }
  859. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  860. {
  861. bool me = CArtifactInstance::isPart(supposedPart);
  862. if(me)
  863. return true;
  864. //check for constituents
  865. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  866. if(constituent.art == supposedPart)
  867. return true;
  868. return false;
  869. }
  870. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ArtifactPosition Slot /*= -1*/)
  871. {
  872. art = Art;
  873. slot = Slot;
  874. }
  875. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  876. {
  877. return art == rhs.art && slot == rhs.slot;
  878. }
  879. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  880. {
  881. if(const ArtSlotInfo *si = getSlot(pos))
  882. {
  883. if(si->artifact && (!excludeLocked || !si->locked))
  884. return si->artifact;
  885. }
  886. return NULL;
  887. }
  888. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  889. {
  890. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  891. }
  892. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  893. {
  894. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  895. if(i->second.artifact->artType->id == aid)
  896. return i->first;
  897. if(onlyWorn)
  898. return ArtifactPosition::PRE_FIRST;
  899. for(size_t i = 0; i < artifactsInBackpack.size(); ++i)
  900. if(artifactsInBackpack[i].artifact->artType->id == aid)
  901. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  902. return ArtifactPosition::PRE_FIRST;
  903. }
  904. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  905. {
  906. BOOST_FOREACH(auto i, artifactsWorn)
  907. if(i.second.artifact == art)
  908. return i.first;
  909. for(size_t i = 0; i < artifactsInBackpack.size(); ++i)
  910. if(artifactsInBackpack[i].artifact == art)
  911. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  912. return ArtifactPosition::PRE_FIRST;
  913. }
  914. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  915. {
  916. BOOST_FOREACH(auto i, artifactsWorn)
  917. if(i.second.artifact->id == artInstId)
  918. return i.second.artifact;
  919. BOOST_FOREACH(auto i, artifactsInBackpack)
  920. if(i.artifact->id == artInstId)
  921. return i.artifact;
  922. return NULL;
  923. }
  924. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  925. {
  926. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  927. }
  928. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  929. {
  930. if(vstd::contains(artifactsWorn, pos))
  931. return &artifactsWorn[pos];
  932. if(pos >= ArtifactPosition::AFTER_LAST )
  933. {
  934. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  935. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  936. return NULL;
  937. else
  938. return &artifactsInBackpack[backpackPos];
  939. }
  940. return NULL;
  941. }
  942. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  943. {
  944. if(const ArtSlotInfo *s = getSlot(pos))
  945. return (onlyLockCheck || !s->artifact) && !s->locked;
  946. return true; //no slot means not used
  947. }
  948. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  949. {
  950. const CArtifactInstance * const a = getArt(pos);
  951. if(!a)
  952. {
  953. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  954. return -1;
  955. }
  956. return a->artType->id;
  957. }
  958. CArtifactSet::~CArtifactSet()
  959. {
  960. }
  961. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  962. {
  963. assert(!vstd::contains(artifactsWorn, slot));
  964. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  965. ? artifactsWorn[slot]
  966. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  967. return ret;
  968. }
  969. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  970. {
  971. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  972. asi.artifact = art;
  973. asi.locked = locked;
  974. }
  975. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  976. {
  977. if(slot < GameConstants::BACKPACK_START)
  978. {
  979. artifactsWorn.erase(slot);
  980. }
  981. else
  982. {
  983. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  984. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  985. }
  986. }
  987. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  988. {
  989. for(auto i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  990. if(i->second.artifact && !i->second.locked)
  991. node->attachTo(i->second.artifact);
  992. }