CArtHandler.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * CArtHandler.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 "CArtHandler.h"
  12. #include "filesystem/Filesystem.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "VCMI_Lib.h"
  15. #include "CModHandler.h"
  16. #include "CSpellHandler.h"
  17. #include "mapObjects/MapObjects.h"
  18. #include "NetPacksBase.h"
  19. #include "GameConstants.h"
  20. #include "CRandomGenerator.h"
  21. #include "mapObjects/CObjectClassesHandler.h"
  22. using namespace boost::assign;
  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. for (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. for (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. for(CArtifact * art : artifacts)
  132. delete art;
  133. }
  134. std::vector<JsonNode> CArtHandler::loadLegacyData(size_t dataSize)
  135. {
  136. artifacts.resize(dataSize);
  137. std::vector<JsonNode> h3Data;
  138. h3Data.reserve(dataSize);
  139. #define ART_POS(x) #x ,
  140. const std::vector<std::string> artSlots = { ART_POS_LIST };
  141. #undef ART_POS
  142. static std::map<char, std::string> classes =
  143. {{'S',"SPECIAL"}, {'T',"TREASURE"},{'N',"MINOR"},{'J',"MAJOR"},{'R',"RELIC"},};
  144. CLegacyConfigParser parser("DATA/ARTRAITS.TXT");
  145. CLegacyConfigParser events("DATA/ARTEVENT.TXT");
  146. parser.endLine(); // header
  147. parser.endLine();
  148. for (size_t i = 0; i < dataSize; i++)
  149. {
  150. JsonNode artData;
  151. artData["text"]["name"].String() = parser.readString();
  152. artData["text"]["event"].String() = events.readString();
  153. artData["value"].Float() = parser.readNumber();
  154. for(auto & artSlot : artSlots)
  155. {
  156. if(parser.readString() == "x")
  157. {
  158. artData["slot"].Vector().push_back(JsonNode());
  159. artData["slot"].Vector().back().String() = artSlot;
  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. return h3Data;
  169. }
  170. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  171. {
  172. auto object = loadFromJson(data);
  173. object->id = ArtifactID(artifacts.size());
  174. object->iconIndex = object->id + 5;
  175. artifacts.push_back(object);
  176. VLC->modh->identifiers.registerObject(scope, "artifact", name, object->id);
  177. }
  178. void CArtHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  179. {
  180. auto object = loadFromJson(data);
  181. object->id = ArtifactID(index);
  182. object->iconIndex = object->id;
  183. assert(artifacts[index] == nullptr); // ensure that this id was not loaded before
  184. artifacts[index] = object;
  185. VLC->modh->identifiers.registerObject(scope, "artifact", name, object->id);
  186. }
  187. CArtifact * CArtHandler::loadFromJson(const JsonNode & node)
  188. {
  189. CArtifact * art;
  190. if (!VLC->modh->modules.COMMANDERS || node["growing"].isNull())
  191. art = new CArtifact();
  192. else
  193. {
  194. auto growing = new CGrowingArtifact();
  195. loadGrowingArt(growing, node);
  196. art = growing;
  197. }
  198. const JsonNode & text = node["text"];
  199. art->name = text["name"].String();
  200. art->description = text["description"].String();
  201. art->eventText = text["event"].String();
  202. const JsonNode & graphics = node["graphics"];
  203. art->image = graphics["image"].String();
  204. if (!graphics["large"].isNull())
  205. art->large = graphics["large"].String();
  206. else
  207. art->large = art->image;
  208. art->advMapDef = graphics["map"].String();
  209. art->price = node["value"].Float();
  210. loadSlots(art, node);
  211. loadClass(art, node);
  212. loadType(art, node);
  213. loadComponents(art, node);
  214. for (auto b : node["bonuses"].Vector())
  215. {
  216. auto bonus = JsonUtils::parseBonus (b);
  217. art->addNewBonus(bonus);
  218. }
  219. return art;
  220. }
  221. ArtifactPosition CArtHandler::stringToSlot(std::string slotName)
  222. {
  223. #define ART_POS(x) { #x, ArtifactPosition::x },
  224. static const std::map<std::string, ArtifactPosition> artifactPositionMap = { ART_POS_LIST };
  225. #undef ART_POS
  226. auto it = artifactPositionMap.find (slotName);
  227. if (it != artifactPositionMap.end())
  228. return it->second;
  229. logGlobal->warnStream() << "Warning! Artifact slot " << slotName << " not recognized!";
  230. return ArtifactPosition::PRE_FIRST;
  231. }
  232. void CArtHandler::addSlot(CArtifact * art, const std::string & slotID)
  233. {
  234. if (slotID == "MISC")
  235. {
  236. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::MISC1, ArtifactPosition::MISC2, ArtifactPosition::MISC3, ArtifactPosition::MISC4, ArtifactPosition::MISC5;
  237. }
  238. else if (slotID == "RING")
  239. {
  240. art->possibleSlots[ArtBearer::HERO] += ArtifactPosition::LEFT_RING, ArtifactPosition::RIGHT_RING;
  241. }
  242. else
  243. {
  244. auto slot = stringToSlot(slotID);
  245. if (slot != ArtifactPosition::PRE_FIRST)
  246. art->possibleSlots[ArtBearer::HERO].push_back (slot);
  247. }
  248. }
  249. void CArtHandler::loadSlots(CArtifact * art, const JsonNode & node)
  250. {
  251. if (!node["slot"].isNull()) //we assume non-hero slots are irrelevant?
  252. {
  253. if (node["slot"].getType() == JsonNode::DATA_STRING)
  254. addSlot(art, node["slot"].String());
  255. else
  256. {
  257. for (const JsonNode & slot : node["slot"].Vector())
  258. addSlot(art, slot.String());
  259. }
  260. }
  261. }
  262. CArtifact::EartClass CArtHandler::stringToClass(std::string className)
  263. {
  264. static const std::map<std::string, CArtifact::EartClass> artifactClassMap =
  265. {
  266. {"TREASURE", CArtifact::ART_TREASURE},
  267. {"MINOR", CArtifact::ART_MINOR},
  268. {"MAJOR", CArtifact::ART_MAJOR},
  269. {"RELIC", CArtifact::ART_RELIC},
  270. {"SPECIAL", CArtifact::ART_SPECIAL}
  271. };
  272. auto it = artifactClassMap.find (className);
  273. if (it != artifactClassMap.end())
  274. return it->second;
  275. logGlobal->warnStream() << "Warning! Artifact rarity " << className << " not recognized!";
  276. return CArtifact::ART_SPECIAL;
  277. }
  278. void CArtHandler::loadClass(CArtifact * art, const JsonNode & node)
  279. {
  280. art->aClass = stringToClass(node["class"].String());
  281. }
  282. void CArtHandler::loadType(CArtifact * art, const JsonNode & node)
  283. {
  284. #define ART_BEARER(x) { #x, ArtBearer::x },
  285. static const std::map<std::string, int> artifactBearerMap = { ART_BEARER_LIST };
  286. #undef ART_BEARER
  287. for (const JsonNode & b : node["type"].Vector())
  288. {
  289. auto it = artifactBearerMap.find (b.String());
  290. if (it != artifactBearerMap.end())
  291. {
  292. int bearerType = it->second;
  293. switch (bearerType)
  294. {
  295. case ArtBearer::HERO://TODO: allow arts having several possible bearers
  296. break;
  297. case ArtBearer::COMMANDER:
  298. makeItCommanderArt (art); //original artifacts should have only one bearer type
  299. break;
  300. case ArtBearer::CREATURE:
  301. makeItCreatureArt (art);
  302. break;
  303. }
  304. }
  305. else
  306. logGlobal->warnStream() << "Warning! Artifact type " << b.String() << " not recognized!";
  307. }
  308. }
  309. void CArtHandler::loadComponents(CArtifact * art, const JsonNode & node)
  310. {
  311. if (!node["components"].isNull())
  312. {
  313. art->constituents.reset(new std::vector<CArtifact *>());
  314. for (auto component : node["components"].Vector())
  315. {
  316. VLC->modh->identifiers.requestIdentifier("artifact", component, [=](si32 id)
  317. {
  318. // when this code is called both combinational art as well as component are loaded
  319. // so it is safe to access any of them
  320. art->constituents->push_back(VLC->arth->artifacts[id]);
  321. VLC->arth->artifacts[id]->constituentOf.push_back(art);
  322. });
  323. }
  324. }
  325. }
  326. void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node)
  327. {
  328. for (auto b : node["growing"]["bonusesPerLevel"].Vector())
  329. {
  330. art->bonusesPerLevel.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"])));
  331. }
  332. for (auto b : node["growing"]["thresholdBonuses"].Vector())
  333. {
  334. art->thresholdBonuses.push_back (std::pair <ui16, Bonus> (b["level"].Float(), *JsonUtils::parseBonus (b["bonus"])));
  335. }
  336. }
  337. //TODO: use bimap
  338. ArtifactID CArtHandler::creatureToMachineID(CreatureID id)
  339. {
  340. switch (id)
  341. {
  342. case CreatureID::CATAPULT: //Catapult
  343. return ArtifactID::CATAPULT;
  344. break;
  345. case CreatureID::BALLISTA: //Ballista
  346. return ArtifactID::BALLISTA;
  347. break;
  348. case CreatureID::FIRST_AID_TENT: //First Aid tent
  349. return ArtifactID::FIRST_AID_TENT;
  350. break;
  351. case CreatureID::AMMO_CART: //Ammo cart
  352. return ArtifactID::AMMO_CART;
  353. break;
  354. }
  355. return ArtifactID::NONE; //this creature is not artifact
  356. }
  357. CreatureID CArtHandler::machineIDToCreature(ArtifactID id)
  358. {
  359. switch (id)
  360. {
  361. case ArtifactID::CATAPULT:
  362. return CreatureID::CATAPULT;
  363. break;
  364. case ArtifactID::BALLISTA:
  365. return CreatureID::BALLISTA;
  366. break;
  367. case ArtifactID::FIRST_AID_TENT:
  368. return CreatureID::FIRST_AID_TENT;
  369. break;
  370. case ArtifactID::AMMO_CART:
  371. return CreatureID::AMMO_CART;
  372. break;
  373. }
  374. return CreatureID::NONE; //this artifact is not a creature
  375. }
  376. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags, std::function<bool(ArtifactID)> accepts)
  377. {
  378. auto getAllowedArts = [&](std::vector<ConstTransitivePtr<CArtifact> > &out, std::vector<CArtifact*> *arts, CArtifact::EartClass flag)
  379. {
  380. if (arts->empty()) //restock available arts
  381. fillList(*arts, flag);
  382. for (auto & arts_i : *arts)
  383. {
  384. if (accepts(arts_i->id))
  385. {
  386. CArtifact *art = arts_i;
  387. out.push_back(art);
  388. }
  389. }
  390. };
  391. auto getAllowed = [&](std::vector<ConstTransitivePtr<CArtifact> > &out)
  392. {
  393. if (flags & CArtifact::ART_TREASURE)
  394. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  395. if (flags & CArtifact::ART_MINOR)
  396. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  397. if (flags & CArtifact::ART_MAJOR)
  398. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  399. if (flags & CArtifact::ART_RELIC)
  400. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  401. if (!out.size()) //no artifact of specified rarity, we need to take another one
  402. {
  403. getAllowedArts (out, &treasures, CArtifact::ART_TREASURE);
  404. getAllowedArts (out, &minors, CArtifact::ART_MINOR);
  405. getAllowedArts (out, &majors, CArtifact::ART_MAJOR);
  406. getAllowedArts (out, &relics, CArtifact::ART_RELIC);
  407. }
  408. if (!out.size()) //no arts are available at all
  409. {
  410. out.resize (64);
  411. std::fill_n (out.begin(), 64, artifacts[2]); //Give Grail - this can't be banned (hopefully)
  412. }
  413. };
  414. std::vector<ConstTransitivePtr<CArtifact> > out;
  415. getAllowed(out);
  416. ArtifactID artID = (*RandomGeneratorUtil::nextItem(out, rand))->id;
  417. erasePickedArt(artID);
  418. return artID;
  419. }
  420. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, std::function<bool(ArtifactID)> accepts)
  421. {
  422. return pickRandomArtifact(rand, 0xff, accepts);
  423. }
  424. ArtifactID CArtHandler::pickRandomArtifact(CRandomGenerator & rand, int flags)
  425. {
  426. return pickRandomArtifact(rand, flags, [](ArtifactID){ return true;});
  427. }
  428. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter = shared_ptr<ILimiter>(), int additionalInfo = 0)
  429. {
  430. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  431. added->additionalInfo = additionalInfo;
  432. added->valType = valType;
  433. added->limiter = limiter;
  434. return added;
  435. }
  436. Bonus *createBonus(Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator = shared_ptr<IPropagator>(), int additionalInfo = 0)
  437. {
  438. auto added = new Bonus(Bonus::PERMANENT,type,Bonus::ARTIFACT,val,-1,subtype);
  439. added->additionalInfo = additionalInfo;
  440. added->valType = Bonus::BASE_NUMBER;
  441. added->propagator = propagator;
  442. return added;
  443. }
  444. void CArtHandler::giveArtBonus( ArtifactID aid, Bonus::BonusType type, int val, int subtype, Bonus::ValueType valType, shared_ptr<ILimiter> limiter, int additionalInfo)
  445. {
  446. giveArtBonus(aid, createBonus(type, val, subtype, valType, limiter, additionalInfo));
  447. }
  448. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus::BonusType type, int val, int subtype, shared_ptr<IPropagator> propagator /*= nullptr*/, int additionalInfo)
  449. {
  450. giveArtBonus(aid, createBonus(type, val, subtype, propagator, additionalInfo));
  451. }
  452. void CArtHandler::giveArtBonus(ArtifactID aid, Bonus *bonus)
  453. {
  454. bonus->sid = aid;
  455. if(bonus->subtype == Bonus::MORALE || bonus->type == Bonus::LUCK)
  456. bonus->description = artifacts[aid]->Name() + (bonus->val > 0 ? " +" : " ") + boost::lexical_cast<std::string>(bonus->val);
  457. else
  458. bonus->description = artifacts[aid]->Name();
  459. artifacts[aid]->addNewBonus(bonus);
  460. }
  461. void CArtHandler::makeItCreatureArt (CArtifact * a, bool onlyCreature /*=true*/)
  462. {
  463. if (onlyCreature)
  464. {
  465. a->possibleSlots[ArtBearer::HERO].clear();
  466. a->possibleSlots[ArtBearer::COMMANDER].clear();
  467. }
  468. a->possibleSlots[ArtBearer::CREATURE].push_back(ArtifactPosition::CREATURE_SLOT);
  469. }
  470. void CArtHandler::makeItCreatureArt (ArtifactID aid, bool onlyCreature /*=true*/)
  471. {
  472. CArtifact *a = artifacts[aid];
  473. makeItCreatureArt (a, onlyCreature);
  474. }
  475. void CArtHandler::makeItCommanderArt (CArtifact * a, bool onlyCommander /*= true*/ )
  476. {
  477. if (onlyCommander)
  478. {
  479. a->possibleSlots[ArtBearer::HERO].clear();
  480. a->possibleSlots[ArtBearer::CREATURE].clear();
  481. }
  482. for (int i = ArtifactPosition::COMMANDER1; i <= ArtifactPosition::COMMANDER6; ++i)
  483. a->possibleSlots[ArtBearer::COMMANDER].push_back(ArtifactPosition(i));
  484. }
  485. void CArtHandler::makeItCommanderArt( ArtifactID aid, bool onlyCommander /*= true*/ )
  486. {
  487. CArtifact *a = artifacts[aid];
  488. makeItCommanderArt (a, onlyCommander);
  489. }
  490. bool CArtHandler::legalArtifact(ArtifactID id)
  491. {
  492. auto art = artifacts[id];
  493. //assert ( (!art->constituents) || art->constituents->size() ); //artifacts is not combined or has some components
  494. return (art->possibleSlots[ArtBearer::HERO].size() ||
  495. (art->possibleSlots[ArtBearer::COMMANDER].size() && VLC->modh->modules.COMMANDERS) ||
  496. (art->possibleSlots[ArtBearer::CREATURE].size() && VLC->modh->modules.STACK_ARTIFACT)) &&
  497. !(art->constituents); //no combo artifacts spawning
  498. }
  499. void CArtHandler::initAllowedArtifactsList(const std::vector<bool> &allowed)
  500. {
  501. allowedArtifacts.clear();
  502. treasures.clear();
  503. minors.clear();
  504. majors.clear();
  505. relics.clear();
  506. for (ArtifactID i=ArtifactID::SPELLBOOK; i<ArtifactID::ART_SELECTION; i.advance(1))
  507. {
  508. if (allowed[i] && legalArtifact(i))
  509. allowedArtifacts.push_back(artifacts[i]);
  510. }
  511. if (VLC->modh->modules.COMMANDERS) //allow all commander artifacts for testing
  512. {
  513. for (int i = 146; i <= 155; ++i)
  514. {
  515. allowedArtifacts.push_back(artifacts[i]);
  516. }
  517. }
  518. for (int i = GameConstants::ARTIFACTS_QUANTITY; i < artifacts.size(); ++i) //allow all new artifacts by default
  519. {
  520. if (legalArtifact(ArtifactID(i)))
  521. allowedArtifacts.push_back(artifacts[i]);
  522. //keep im mind that artifact can be worn by more than one type of bearer
  523. }
  524. }
  525. std::vector<bool> CArtHandler::getDefaultAllowed() const
  526. {
  527. std::vector<bool> allowedArtifacts;
  528. allowedArtifacts.resize(127, true);
  529. allowedArtifacts.resize(141, false);
  530. allowedArtifacts.resize(GameConstants::ARTIFACTS_QUANTITY, true);
  531. return allowedArtifacts;
  532. }
  533. void CArtHandler::erasePickedArt(ArtifactID id)
  534. {
  535. CArtifact *art = artifacts[id];
  536. if(auto artifactList = listFromClass(art->aClass))
  537. {
  538. if(artifactList->empty())
  539. fillList(*artifactList, art->aClass);
  540. auto itr = vstd::find(*artifactList, art);
  541. if(itr != artifactList->end())
  542. {
  543. artifactList->erase(itr);
  544. }
  545. else
  546. logGlobal->warnStream() << "Problem: cannot erase artifact " << art->Name() << " from list, it was not present";
  547. }
  548. else
  549. logGlobal->warnStream() << "Problem: cannot find list for artifact " << art->Name() << ", strange class. (special?)";
  550. }
  551. boost::optional<std::vector<CArtifact*>&> CArtHandler::listFromClass( CArtifact::EartClass artifactClass )
  552. {
  553. switch(artifactClass)
  554. {
  555. case CArtifact::ART_TREASURE:
  556. return treasures;
  557. case CArtifact::ART_MINOR:
  558. return minors;
  559. case CArtifact::ART_MAJOR:
  560. return majors;
  561. case CArtifact::ART_RELIC:
  562. return relics;
  563. default: //special artifacts should not be erased
  564. return nullptr;
  565. }
  566. }
  567. void CArtHandler::fillList( std::vector<CArtifact*> &listToBeFilled, CArtifact::EartClass artifactClass )
  568. {
  569. assert(listToBeFilled.empty());
  570. for (auto & elem : allowedArtifacts)
  571. {
  572. if (elem->aClass == artifactClass)
  573. listToBeFilled.push_back(elem);
  574. }
  575. }
  576. void CArtHandler::afterLoadFinalization()
  577. {
  578. //All artifacts have their id, so we can properly update their bonuses' source ids.
  579. for(auto &art : artifacts)
  580. {
  581. for(auto &bonus : art->getExportedBonusList())
  582. {
  583. assert(art == artifacts[art->id]);
  584. assert(bonus->source == Bonus::ARTIFACT);
  585. bonus->sid = art->id;
  586. }
  587. }
  588. for (CArtifact * art : artifacts)
  589. {
  590. VLC->objtypeh->loadSubObject(art->Name(), JsonNode(), Obj::ARTIFACT, art->id.num);
  591. if (!art->advMapDef.empty())
  592. {
  593. JsonNode templ;
  594. templ["animation"].String() = art->advMapDef;
  595. // add new template.
  596. // Necessary for objects added via mods that don't have any templates in H3
  597. VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->addTemplate(templ);
  598. }
  599. // object does not have any templates - this is not usable object (e.g. pseudo-art like lock)
  600. if (VLC->objtypeh->getHandlerFor(Obj::ARTIFACT, art->id)->getTemplates().empty())
  601. VLC->objtypeh->removeSubObject(Obj::ARTIFACT, art->id);
  602. }
  603. }
  604. CArtifactInstance::CArtifactInstance()
  605. {
  606. init();
  607. }
  608. CArtifactInstance::CArtifactInstance( CArtifact *Art)
  609. {
  610. init();
  611. setType(Art);
  612. }
  613. void CArtifactInstance::setType( CArtifact *Art )
  614. {
  615. artType = Art;
  616. attachTo(Art);
  617. }
  618. std::string CArtifactInstance::nodeName() const
  619. {
  620. return "Artifact instance of " + (artType ? artType->Name() : std::string("uninitialized")) + " type";
  621. }
  622. CArtifactInstance * CArtifactInstance::createScroll( const CSpell *s)
  623. {
  624. auto ret = new CArtifactInstance(VLC->arth->artifacts[1]);
  625. auto b = new Bonus(Bonus::PERMANENT, Bonus::SPELL, Bonus::ARTIFACT_INSTANCE, -1, 1, s->id);
  626. ret->addNewBonus(b);
  627. return ret;
  628. }
  629. void CArtifactInstance::init()
  630. {
  631. id = ArtifactInstanceID();
  632. id = static_cast<ArtifactInstanceID>(ArtifactID::NONE); //to be randomized
  633. setNodeType(ARTIFACT_INSTANCE);
  634. }
  635. ArtifactPosition CArtifactInstance::firstAvailableSlot(const CArtifactSet *h) const
  636. {
  637. for(auto slot : artType->possibleSlots.at(h->bearerType()))
  638. {
  639. if(canBePutAt(h, slot)) //if(artType->fitsAt(h->artifWorn, slot))
  640. {
  641. //we've found a free suitable slot.
  642. return slot;
  643. }
  644. }
  645. //if haven't find proper slot, use backpack
  646. return firstBackpackSlot(h);
  647. }
  648. ArtifactPosition CArtifactInstance::firstBackpackSlot(const CArtifactSet *h) const
  649. {
  650. if(!artType->isBig()) //discard big artifact
  651. return ArtifactPosition(
  652. GameConstants::BACKPACK_START + h->artifactsInBackpack.size());
  653. return ArtifactPosition::PRE_FIRST;
  654. }
  655. bool CArtifactInstance::canBePutAt(const ArtifactLocation & al, bool assumeDestRemoved /*= false*/) const
  656. {
  657. return canBePutAt(al.getHolderArtSet(), al.slot, assumeDestRemoved);
  658. }
  659. bool CArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  660. {
  661. if(slot >= GameConstants::BACKPACK_START)
  662. {
  663. if(artType->isBig())
  664. return false;
  665. //TODO backpack limit
  666. return true;
  667. }
  668. auto possibleSlots = artType->possibleSlots.find(artSet->bearerType());
  669. if(possibleSlots == artType->possibleSlots.end())
  670. {
  671. logGlobal->warnStream() << "Warning: artifact " << artType->Name() << " doesn't have defined allowed slots for bearer of type "
  672. << artSet->bearerType();
  673. return false;
  674. }
  675. if(!vstd::contains(possibleSlots->second, slot))
  676. return false;
  677. return artSet->isPositionFree(slot, assumeDestRemoved);
  678. }
  679. void CArtifactInstance::putAt(ArtifactLocation al)
  680. {
  681. assert(canBePutAt(al));
  682. al.getHolderArtSet()->setNewArtSlot(al.slot, this, false);
  683. if(al.slot < GameConstants::BACKPACK_START)
  684. al.getHolderNode()->attachTo(this);
  685. }
  686. void CArtifactInstance::removeFrom(ArtifactLocation al)
  687. {
  688. assert(al.getHolderArtSet()->getArt(al.slot) == this);
  689. al.getHolderArtSet()->eraseArtSlot(al.slot);
  690. if(al.slot < GameConstants::BACKPACK_START)
  691. al.getHolderNode()->detachFrom(this);
  692. //TODO delete me?
  693. }
  694. bool CArtifactInstance::canBeDisassembled() const
  695. {
  696. return bool(artType->constituents);
  697. }
  698. std::vector<const CArtifact *> CArtifactInstance::assemblyPossibilities(const CArtifactSet *h) const
  699. {
  700. std::vector<const CArtifact *> ret;
  701. if(artType->constituents) //combined artifact already: no combining of combined artifacts... for now.
  702. return ret;
  703. for(const CArtifact * artifact : artType->constituentOf)
  704. {
  705. assert(artifact->constituents);
  706. bool possible = true;
  707. for(const CArtifact * constituent : *artifact->constituents) //check if all constituents are available
  708. {
  709. if(!h->hasArt(constituent->id, true)) //constituent must be equipped
  710. {
  711. possible = false;
  712. break;
  713. }
  714. }
  715. if(possible)
  716. ret.push_back(artifact);
  717. }
  718. return ret;
  719. }
  720. void CArtifactInstance::move(ArtifactLocation src, ArtifactLocation dst)
  721. {
  722. removeFrom(src);
  723. putAt(dst);
  724. }
  725. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(CArtifact *Art)
  726. {
  727. if(!Art->constituents)
  728. {
  729. auto ret = new CArtifactInstance(Art);
  730. if (dynamic_cast<CGrowingArtifact *>(Art))
  731. {
  732. auto bonus = new Bonus;
  733. bonus->type = Bonus::LEVEL_COUNTER;
  734. bonus->val = 0;
  735. ret->addNewBonus (bonus);
  736. }
  737. return ret;
  738. }
  739. else
  740. {
  741. auto ret = new CCombinedArtifactInstance(Art);
  742. ret->createConstituents();
  743. return ret;
  744. }
  745. }
  746. CArtifactInstance * CArtifactInstance::createNewArtifactInstance(int aid)
  747. {
  748. return createNewArtifactInstance(VLC->arth->artifacts[aid]);
  749. }
  750. void CArtifactInstance::deserializationFix()
  751. {
  752. setType(artType);
  753. }
  754. SpellID CArtifactInstance::getGivenSpellID() const
  755. {
  756. const Bonus * b = getBonusLocalFirst(Selector::type(Bonus::SPELL));
  757. if(!b)
  758. {
  759. logGlobal->warnStream() << "Warning: " << nodeName() << " doesn't bear any spell!";
  760. return SpellID::NONE;
  761. }
  762. return SpellID(b->subtype);
  763. }
  764. bool CArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  765. {
  766. return supposedPart == this;
  767. }
  768. bool CCombinedArtifactInstance::canBePutAt(const CArtifactSet *artSet, ArtifactPosition slot, bool assumeDestRemoved /*= false*/) const
  769. {
  770. bool canMainArtifactBePlaced = CArtifactInstance::canBePutAt(artSet, slot, assumeDestRemoved);
  771. if(!canMainArtifactBePlaced)
  772. return false; //no is no...
  773. if(slot >= GameConstants::BACKPACK_START)
  774. return true; //we can always remove combined art to the backapck
  775. assert(artType->constituents);
  776. std::vector<ConstituentInfo> constituentsToBePlaced = constituentsInfo; //we'll remove constituents from that list, as we find a suitable slot for them
  777. //it may be that we picked a combined artifact in hero screen (though technically it's still there) to move it
  778. //so we remove from the list all constituents that are already present on dst hero in the form of locks
  779. for(const ConstituentInfo &constituent : constituentsInfo)
  780. {
  781. if(constituent.art == artSet->getArt(constituent.slot, false)) //no need to worry about locked constituent
  782. constituentsToBePlaced -= constituent;
  783. }
  784. //we iterate over all active slots and check if constituents fits them
  785. for (int i = 0; i < GameConstants::BACKPACK_START; i++)
  786. {
  787. for(auto art = constituentsToBePlaced.begin(); art != constituentsToBePlaced.end(); art++)
  788. {
  789. 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
  790. {
  791. constituentsToBePlaced.erase(art);
  792. break;
  793. }
  794. }
  795. }
  796. return constituentsToBePlaced.empty();
  797. }
  798. bool CCombinedArtifactInstance::canBeDisassembled() const
  799. {
  800. return true;
  801. }
  802. CCombinedArtifactInstance::CCombinedArtifactInstance(CArtifact *Art)
  803. : CArtifactInstance(Art) //TODO: seems unued, but need to be written
  804. {
  805. }
  806. CCombinedArtifactInstance::CCombinedArtifactInstance()
  807. {
  808. }
  809. void CCombinedArtifactInstance::createConstituents()
  810. {
  811. assert(artType);
  812. assert(artType->constituents);
  813. for(const CArtifact * art : *artType->constituents)
  814. {
  815. addAsConstituent(CArtifactInstance::createNewArtifactInstance(art->id), ArtifactPosition::PRE_FIRST);
  816. }
  817. }
  818. void CCombinedArtifactInstance::addAsConstituent(CArtifactInstance *art, ArtifactPosition slot)
  819. {
  820. assert(vstd::contains(*artType->constituents, art->artType.get()));
  821. assert(art->getParentNodes().size() == 1 && art->getParentNodes().front() == art->artType);
  822. constituentsInfo.push_back(ConstituentInfo(art, slot));
  823. attachTo(art);
  824. }
  825. void CCombinedArtifactInstance::putAt(ArtifactLocation al)
  826. {
  827. if(al.slot >= GameConstants::BACKPACK_START)
  828. {
  829. CArtifactInstance::putAt(al);
  830. for(ConstituentInfo &ci : constituentsInfo)
  831. ci.slot = ArtifactPosition::PRE_FIRST;
  832. }
  833. else
  834. {
  835. CArtifactInstance *mainConstituent = figureMainConstituent(al); //it'll be replaced with combined artifact, not a lock
  836. CArtifactInstance::putAt(al); //puts combined art (this)
  837. for(ConstituentInfo &ci : constituentsInfo)
  838. {
  839. if(ci.art != mainConstituent)
  840. {
  841. const ArtifactLocation suggestedPos(al.artHolder, ci.slot);
  842. const bool inActiveSlot = vstd::isbetween(ci.slot, 0, GameConstants::BACKPACK_START);
  843. const bool suggestedPosValid = ci.art->canBePutAt(suggestedPos);
  844. ArtifactPosition pos = ArtifactPosition::PRE_FIRST;
  845. if(inActiveSlot && suggestedPosValid) //there is a valid suggestion where to place lock
  846. pos = ci.slot;
  847. else
  848. ci.slot = pos = ci.art->firstAvailableSlot(al.getHolderArtSet());
  849. assert(pos < GameConstants::BACKPACK_START);
  850. al.getHolderArtSet()->setNewArtSlot(pos, ci.art, true); //sets as lock
  851. }
  852. else
  853. {
  854. ci.slot = ArtifactPosition::PRE_FIRST;
  855. }
  856. }
  857. }
  858. }
  859. void CCombinedArtifactInstance::removeFrom(ArtifactLocation al)
  860. {
  861. if(al.slot >= GameConstants::BACKPACK_START)
  862. {
  863. CArtifactInstance::removeFrom(al);
  864. }
  865. else
  866. {
  867. for(ConstituentInfo &ci : constituentsInfo)
  868. {
  869. if(ci.slot >= 0)
  870. {
  871. al.getHolderArtSet()->eraseArtSlot(ci.slot);
  872. ci.slot = ArtifactPosition::PRE_FIRST;
  873. }
  874. else
  875. {
  876. //main constituent
  877. CArtifactInstance::removeFrom(al);
  878. }
  879. }
  880. }
  881. }
  882. CArtifactInstance * CCombinedArtifactInstance::figureMainConstituent(const ArtifactLocation al)
  883. {
  884. CArtifactInstance *mainConstituent = nullptr; //it'll be replaced with combined artifact, not a lock
  885. for(ConstituentInfo &ci : constituentsInfo)
  886. if(ci.slot == al.slot)
  887. mainConstituent = ci.art;
  888. if(!mainConstituent)
  889. {
  890. for(ConstituentInfo &ci : constituentsInfo)
  891. {
  892. if(vstd::contains(ci.art->artType->possibleSlots[al.getHolderArtSet()->bearerType()], al.slot))
  893. {
  894. mainConstituent = ci.art;
  895. }
  896. }
  897. }
  898. return mainConstituent;
  899. }
  900. void CCombinedArtifactInstance::deserializationFix()
  901. {
  902. for(ConstituentInfo &ci : constituentsInfo)
  903. attachTo(ci.art);
  904. }
  905. bool CCombinedArtifactInstance::isPart(const CArtifactInstance *supposedPart) const
  906. {
  907. bool me = CArtifactInstance::isPart(supposedPart);
  908. if(me)
  909. return true;
  910. //check for constituents
  911. for(const ConstituentInfo &constituent : constituentsInfo)
  912. if(constituent.art == supposedPart)
  913. return true;
  914. return false;
  915. }
  916. CCombinedArtifactInstance::ConstituentInfo::ConstituentInfo(CArtifactInstance *Art /*= nullptr*/, ArtifactPosition Slot /*= -1*/)
  917. {
  918. art = Art;
  919. slot = Slot;
  920. }
  921. bool CCombinedArtifactInstance::ConstituentInfo::operator==(const ConstituentInfo &rhs) const
  922. {
  923. return art == rhs.art && slot == rhs.slot;
  924. }
  925. const CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/) const
  926. {
  927. if(const ArtSlotInfo *si = getSlot(pos))
  928. {
  929. if(si->artifact && (!excludeLocked || !si->locked))
  930. return si->artifact;
  931. }
  932. return nullptr;
  933. }
  934. CArtifactInstance* CArtifactSet::getArt(ArtifactPosition pos, bool excludeLocked /*= true*/)
  935. {
  936. return const_cast<CArtifactInstance*>((const_cast<const CArtifactSet*>(this))->getArt(pos, excludeLocked));
  937. }
  938. ArtifactPosition CArtifactSet::getArtPos(int aid, bool onlyWorn /*= true*/) const
  939. {
  940. for(auto i = artifactsWorn.cbegin(); i != artifactsWorn.cend(); i++)
  941. if(i->second.artifact->artType->id == aid)
  942. return i->first;
  943. if(onlyWorn)
  944. return ArtifactPosition::PRE_FIRST;
  945. for(int i = 0; i < artifactsInBackpack.size(); i++)
  946. if(artifactsInBackpack[i].artifact->artType->id == aid)
  947. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  948. return ArtifactPosition::PRE_FIRST;
  949. }
  950. ArtifactPosition CArtifactSet::getArtPos(const CArtifactInstance *art) const
  951. {
  952. for(auto i : artifactsWorn)
  953. if(i.second.artifact == art)
  954. return i.first;
  955. for(int i = 0; i < artifactsInBackpack.size(); i++)
  956. if(artifactsInBackpack[i].artifact == art)
  957. return ArtifactPosition(GameConstants::BACKPACK_START + i);
  958. return ArtifactPosition::PRE_FIRST;
  959. }
  960. const CArtifactInstance * CArtifactSet::getArtByInstanceId( ArtifactInstanceID artInstId ) const
  961. {
  962. for(auto i : artifactsWorn)
  963. if(i.second.artifact->id == artInstId)
  964. return i.second.artifact;
  965. for(auto i : artifactsInBackpack)
  966. if(i.artifact->id == artInstId)
  967. return i.artifact;
  968. return nullptr;
  969. }
  970. bool CArtifactSet::hasArt(ui32 aid, bool onlyWorn /*= false*/) const
  971. {
  972. return getArtPos(aid, onlyWorn) != ArtifactPosition::PRE_FIRST;
  973. }
  974. const ArtSlotInfo * CArtifactSet::getSlot(ArtifactPosition pos) const
  975. {
  976. if(vstd::contains(artifactsWorn, pos))
  977. return &artifactsWorn.at(pos);
  978. if(pos >= ArtifactPosition::AFTER_LAST )
  979. {
  980. int backpackPos = (int)pos - GameConstants::BACKPACK_START;
  981. if(backpackPos < 0 || backpackPos >= artifactsInBackpack.size())
  982. return nullptr;
  983. else
  984. return &artifactsInBackpack[backpackPos];
  985. }
  986. return nullptr;
  987. }
  988. bool CArtifactSet::isPositionFree(ArtifactPosition pos, bool onlyLockCheck /*= false*/) const
  989. {
  990. if(const ArtSlotInfo *s = getSlot(pos))
  991. return (onlyLockCheck || !s->artifact) && !s->locked;
  992. return true; //no slot means not used
  993. }
  994. si32 CArtifactSet::getArtTypeId(ArtifactPosition pos) const
  995. {
  996. const CArtifactInstance * const a = getArt(pos);
  997. if(!a)
  998. {
  999. logGlobal->warnStream() << (dynamic_cast<const CGHeroInstance*>(this))->name << " has no artifact at " << pos << " (getArtTypeId)";
  1000. return -1;
  1001. }
  1002. return a->artType->id;
  1003. }
  1004. CArtifactSet::~CArtifactSet()
  1005. {
  1006. }
  1007. ArtSlotInfo & CArtifactSet::retreiveNewArtSlot(ArtifactPosition slot)
  1008. {
  1009. assert(!vstd::contains(artifactsWorn, slot));
  1010. ArtSlotInfo &ret = slot < GameConstants::BACKPACK_START
  1011. ? artifactsWorn[slot]
  1012. : *artifactsInBackpack.insert(artifactsInBackpack.begin() + (slot - GameConstants::BACKPACK_START), ArtSlotInfo());
  1013. return ret;
  1014. }
  1015. void CArtifactSet::setNewArtSlot(ArtifactPosition slot, CArtifactInstance *art, bool locked)
  1016. {
  1017. ArtSlotInfo &asi = retreiveNewArtSlot(slot);
  1018. asi.artifact = art;
  1019. asi.locked = locked;
  1020. }
  1021. void CArtifactSet::eraseArtSlot(ArtifactPosition slot)
  1022. {
  1023. if(slot < GameConstants::BACKPACK_START)
  1024. {
  1025. artifactsWorn.erase(slot);
  1026. }
  1027. else
  1028. {
  1029. slot = ArtifactPosition(slot - GameConstants::BACKPACK_START);
  1030. artifactsInBackpack.erase(artifactsInBackpack.begin() + slot);
  1031. }
  1032. }
  1033. void CArtifactSet::artDeserializationFix(CBonusSystemNode *node)
  1034. {
  1035. for(auto & elem : artifactsWorn)
  1036. if(elem.second.artifact && !elem.second.locked)
  1037. node->attachTo(elem.second.artifact);
  1038. }