2
0

CArtHandler.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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(int 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"].Float();
  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"].Float();
  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 (int 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. treasures.clear();
  490. minors.clear();
  491. majors.clear();
  492. relics.clear();
  493. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  494. {
  495. if (allowed[i] && legalArtifact(i))
  496. allowedArtifacts.push_back(artifacts[i]);
  497. }
  498. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  499. {
  500. for (int i = 146; i <= 155; ++i)
  501. {
  502. allowedArtifacts.push_back(artifacts[i]);
  503. }
  504. }
  505. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  506. {
  507. if (legalArtifact(ArtifactID(i)))
  508. allowedArtifacts.push_back(artifacts[i]);
  509. //keep im mind that artifact can be worn by more than one type of bearer
  510. }
  511. }
  512. std::vector<bool> CArtHandler::getDefaultAllowedArtifacts() const
  513. {
  514. std::vector<bool> allowedArtifacts;
  515. allowedArtifacts.resize(127, true);
  516. allowedArtifacts.resize(141, false);
  517. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  518. return allowedArtifacts;
  519. }
  520. void CArtHandler::erasePickedArt(ArtifactID id)
  521. {
  522. CArtifact *art = artifacts[id];
  523. if(auto artifactList = listFromClass(art->aClass))
  524. {
  525. if(artifactList->empty())
  526. fillList(*artifactList, art->aClass);
  527. auto itr = vstd::find(*artifactList, art);
  528. if(itr != artifactList->end())
  529. {
  530. artifactList->erase(itr);
  531. }
  532. else
  533. tlog2 << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present\n";
  534. }
  535. else
  536. tlog2 << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)\n";
  537. }
  538. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  539. {
  540. switch(artifactClass)
  541. {
  542. case CArtifact::ART_TREASURE:
  543. return treasures;
  544. case CArtifact::ART_MINOR:
  545. return minors;
  546. case CArtifact::ART_MAJOR:
  547. return majors;
  548. case CArtifact::ART_RELIC:
  549. return relics;
  550. default: //special artifacts should not be erased
  551. return 0;
  552. }
  553. }
  554. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  555. {
  556. assert(listToBeFilled.empty());
  557. for (int i = 0; i < allowedArtifacts.size(); ++i)
  558. {
  559. if (allowedArtifacts[i]->aClass == artifactClass)
  560. listToBeFilled.push_back(allowedArtifacts[i]);
  561. }
  562. }
  563. CArtifactInstance::CArtifactInstance()
  564. {
  565. init();
  566. }
  567. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  568. {
  569. init();
  570. setType(Art);
  571. }
  572. void CArtifactInstance::setType( CArtifact *Art )
  573. {
  574. artType = Art;
  575. attachTo(Art);
  576. }
  577. std::string CArtifactInstance::nodeName() const
  578. {
  579. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  580. }
  581. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  582. {
  583. CArtifactInstance *ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  584. Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  585. ret->addNewBonus(b);
  586. return ret;
  587. }
  588. void CArtifactInstance::init()
  589. {
  590. id = ArtifactInstanceID();
  591. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  592. setNodeType(ARTIFACT_INSTANCE);
  593. }
  594. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  595. {
  596. BOOST_FOREACH(auto slot, artType->possibleSlots[h->bearerType()])
  597. {
  598. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  599. {
  600. //we've found a free suitable slot.
  601. return slot;
  602. }
  603. }
  604. //if haven't find proper slot, use backpack
  605. return firstBackpackSlot(h);
  606. }
  607. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  608. {
  609. if(!artType->isBig()) //discard big artifact
  610. return ArtifactPosition(
  611. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  612. return ArtifactPosition::PRE_FIRST;
  613. }
  614. bool CArtifactInstance::canBePutAt(const ArtifactLocation al, bool assumeDestRemoved /*= false*/) const
  615. {
  616. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  617. }
  618. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  619. {
  620. if(slot >= GameConstants::BACKPACK_START)
  621. {
  622. if(artType->isBig())
  623. return false;
  624. //TODO backpack limit
  625. return true;
  626. }
  627. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  628. if(possibleSlots == artType->possibleSlots.end())
  629. {
  630. tlog3 << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  631. << artSet->bearerType() << std::endl;
  632. return false;
  633. }
  634. if(!vstd::contains(possibleSlots->second, slot))
  635. return false;
  636. return artSet->isPositionFree(slot, assumeDestRemoved);
  637. }
  638. void CArtifactInstance::putAt(ArtifactLocation al)
  639. {
  640. assert(canBePutAt(al));
  641. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  642. if(al.slot < GameConstants::BACKPACK_START)
  643. al.getHolderNode()->attachTo(this);
  644. }
  645. void CArtifactInstance::removeFrom(ArtifactLocation al)
  646. {
  647. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  648. al.getHolderArtSet()->eraseArtSlot(al.slot);
  649. if(al.slot < GameConstants::BACKPACK_START)
  650. al.getHolderNode()->detachFrom(this);
  651. //TODO delete me?
  652. }
  653. bool CArtifactInstance::canBeDisassembled() const
  654. {
  655. return artType->constituents != nullptr;
  656. }
  657. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  658. {
  659. std::vector<const CArtifact *> ret;
  660. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  661. return ret;
  662. BOOST_FOREACH(const CArtifact * artifact, artType->constituentOf)
  663. {
  664. assert(artifact->constituents);
  665. bool possible = true;
  666. BOOST_FOREACH(const CArtifact * constituent, *artifact->constituents) //check if all constituents are available
  667. {
  668. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  669. {
  670. possible = false;
  671. break;
  672. }
  673. }
  674. if(possible)
  675. ret.push_back(artifact);
  676. }
  677. return ret;
  678. }
  679. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  680. {
  681. removeFrom(src);
  682. putAt(dst);
  683. }
  684. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  685. {
  686. if(!Art->constituents)
  687. {
  688. auto ret = new CArtifactInstance(Art);
  689. if (dynamic_cast<CGrowingArtifact *>(Art))
  690. {
  691. Bonus * bonus = new Bonus;
  692. bonus->type = Bonus::LEVEL_COUNTER;
  693. bonus->val = 0;
  694. ret->addNewBonus (bonus);
  695. }
  696. return ret;
  697. }
  698. else
  699. {
  700. CCombinedArtifactInstance * ret = new CCombinedArtifactInstance(Art);
  701. ret->createConstituents();
  702. return ret;
  703. }
  704. }
  705. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  706. {
  707. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  708. }
  709. void CArtifactInstance::deserializationFix()
  710. {
  711. setType(artType);
  712. }
  713. SpellID CArtifactInstance::getGivenSpellID() const
  714. {
  715. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  716. if(!b)
  717. {
  718. tlog3 << "Warning: " << nodeName() << " doesn't bear any spell!\n";
  719. return SpellID::NONE;
  720. }
  721. return SpellID(b->subtype);
  722. }
  723. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  724. {
  725. return supposedPart == this;
  726. }
  727. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  728. {
  729. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  730. if(!canMainArtifactBePlaced)
  731. return false; //no is no...
  732. if(slot >= GameConstants::BACKPACK_START)
  733. return true; //we can always remove combined art to the backapck
  734. assert(artType->constituents);
  735. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  736. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  737. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  738. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  739. {
  740. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  741. constituentsToBePlaced -= constituent;
  742. }
  743. //we iterate over all active slots and check if constituents fits them
  744. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  745. {
  746. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  747. {
  748. 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
  749. {
  750. constituentsToBePlaced.erase(art);
  751. break;
  752. }
  753. }
  754. }
  755. return constituentsToBePlaced.empty();
  756. }
  757. bool CCombinedArtifactInstance::canBeDisassembled() const
  758. {
  759. return true;
  760. }
  761. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  762. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  763. {
  764. }
  765. CCombinedArtifactInstance::CCombinedArtifactInstance()
  766. {
  767. }
  768. void CCombinedArtifactInstance::createConstituents()
  769. {
  770. assert(artType);
  771. assert(artType->constituents);
  772. BOOST_FOREACH(const CArtifact * art, *artType->constituents)
  773. {
  774. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  775. }
  776. }
  777. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  778. {
  779. assert(vstd::contains(*artType->constituents, art->artType.get()));
  780. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  781. constituentsInfo.push_back(ConstituentInfo(art, slot));
  782. attachTo(art);
  783. }
  784. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  785. {
  786. if(al.slot >= GameConstants::BACKPACK_START)
  787. {
  788. CArtifactInstance::putAt(al);
  789. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  790. ci.slot = ArtifactPosition::PRE_FIRST;
  791. }
  792. else
  793. {
  794. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  795. CArtifactInstance::putAt(al); //puts combined art (this)
  796. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  797. {
  798. if(ci.art != mainConstituent)
  799. {
  800. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  801. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  802. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  803. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  804. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  805. pos = ci.slot;
  806. else
  807. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  808. assert(pos < GameConstants::BACKPACK_START);
  809. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  810. }
  811. else
  812. {
  813. ci.slot = ArtifactPosition::PRE_FIRST;
  814. }
  815. }
  816. }
  817. }
  818. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  819. {
  820. if(al.slot >= GameConstants::BACKPACK_START)
  821. {
  822. CArtifactInstance::removeFrom(al);
  823. }
  824. else
  825. {
  826. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  827. {
  828. if(ci.slot >= 0)
  829. {
  830. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  831. ci.slot = ArtifactPosition::PRE_FIRST;
  832. }
  833. else
  834. {
  835. //main constituent
  836. CArtifactInstance::removeFrom(al);
  837. }
  838. }
  839. }
  840. }
  841. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  842. {
  843. CArtifactInstance *mainConstituent = NULL; //it'll be replaced with combined artifact, not a lock
  844. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  845. if(ci.slot == al.slot)
  846. mainConstituent = ci.art;
  847. if(!mainConstituent)
  848. {
  849. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  850. {
  851. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  852. {
  853. mainConstituent = ci.art;
  854. }
  855. }
  856. }
  857. return mainConstituent;
  858. }
  859. void CCombinedArtifactInstance::deserializationFix()
  860. {
  861. BOOST_FOREACH(ConstituentInfo &ci, constituentsInfo)
  862. attachTo(ci.art);
  863. }
  864. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  865. {
  866. bool me = CArtifactInstance::isPart(supposedPart);
  867. if(me)
  868. return true;
  869. //check for constituents
  870. BOOST_FOREACH(const ConstituentInfo &constituent, constituentsInfo)
  871. if(constituent.art == supposedPart)
  872. return true;
  873. return false;
  874. }
  875. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= NULL*/, ArtifactPosition Slot /*= -1*/)
  876. {
  877. art = Art;
  878. slot = Slot;
  879. }
  880. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  881. {
  882. return art == rhs.art && slot == rhs.slot;
  883. }
  884. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  885. {
  886. if(const ArtSlotInfo *si = getSlot(pos))
  887. {
  888. if(si->artifact && (!excludeLocked || !si->locked))
  889. return si->artifact;
  890. }
  891. return NULL;
  892. }
  893. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  894. {
  895. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  896. }
  897. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  898. {
  899. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  900. if(i->second.artifact->artType->id == aid)
  901. return i->first;
  902. if(onlyWorn)
  903. return ArtifactPosition::PRE_FIRST;
  904. for(int i = 0; i < artifactsInBackpack.size(); i++)
  905. if(artifactsInBackpack[i].artifact->artType->id == aid)
  906. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  907. return ArtifactPosition::PRE_FIRST;
  908. }
  909. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  910. {
  911. BOOST_FOREACH(auto i, artifactsWorn)
  912. if(i.second.artifact == art)
  913. return i.first;
  914. for(int i = 0; i < artifactsInBackpack.size(); i++)
  915. if(artifactsInBackpack[i].artifact == art)
  916. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  917. return ArtifactPosition::PRE_FIRST;
  918. }
  919. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  920. {
  921. BOOST_FOREACH(auto i, artifactsWorn)
  922. if(i.second.artifact->id == artInstId)
  923. return i.second.artifact;
  924. BOOST_FOREACH(auto i, artifactsInBackpack)
  925. if(i.artifact->id == artInstId)
  926. return i.artifact;
  927. return NULL;
  928. }
  929. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  930. {
  931. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  932. }
  933. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  934. {
  935. if(vstd::contains(artifactsWorn, pos))
  936. return &artifactsWorn[pos];
  937. if(pos >= ArtifactPosition::AFTER_LAST )
  938. {
  939. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  940. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  941. return NULL;
  942. else
  943. return &artifactsInBackpack[backpackPos];
  944. }
  945. return NULL;
  946. }
  947. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  948. {
  949. if(const ArtSlotInfo *s = getSlot(pos))
  950. return (onlyLockCheck || !s->artifact) && !s->locked;
  951. return true; //no slot means not used
  952. }
  953. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  954. {
  955. const CArtifactInstance * const a = getArt(pos);
  956. if(!a)
  957. {
  958. tlog2 << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)\n";
  959. return -1;
  960. }
  961. return a->artType->id;
  962. }
  963. CArtifactSet::~CArtifactSet()
  964. {
  965. }
  966. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  967. {
  968. assert(!vstd::contains(artifactsWorn, slot));
  969. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  970. ? artifactsWorn[slot]
  971. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  972. return ret;
  973. }
  974. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  975. {
  976. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  977. asi.artifact = art;
  978. asi.locked = locked;
  979. }
  980. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  981. {
  982. if(slot < GameConstants::BACKPACK_START)
  983. {
  984. artifactsWorn.erase(slot);
  985. }
  986. else
  987. {
  988. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  989. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  990. }
  991. }
  992. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  993. {
  994. for(auto i = artifactsWorn.begin(); i != artifactsWorn.end(); i++)
  995. if(i->second.artifact && !i->second.locked)
  996. node->attachTo(i->second.artifact);
  997. }