CCreatureHandler.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. #include "StdInc.h"
  2. #include "CCreatureHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "Filesystem/CResourceLoader.h"
  5. #include "VCMI_Lib.h"
  6. #include "CGameState.h"
  7. #include "CTownHandler.h"
  8. #include "CModHandler.h"
  9. using namespace boost::assign;
  10. /*
  11. * CCreatureHandler.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. CCreatureHandler::CCreatureHandler()
  20. {
  21. VLC->creh = this;
  22. // Set the faction alignments to the defaults:
  23. // Good: Castle, Rampart, Tower
  24. // Evil: Inferno, Necropolis, Dungeon
  25. // Neutral: Stronghold, Fortess, Conflux
  26. factionAlignments += 1, 1, 1, -1, -1, -1, 0, 0, 0;
  27. doubledCreatures += 4, 14, 20, 28, 44, 60, 70, 72, 85, 86, 100, 104; //according to Strategija
  28. allCreatures.setDescription("All creatures");
  29. creaturesOfLevel[0].setDescription("Creatures of unnormalized tier");
  30. for(int i = 1; i < ARRAY_COUNT(creaturesOfLevel); i++)
  31. creaturesOfLevel[i].setDescription("Creatures of tier " + boost::lexical_cast<std::string>(i));
  32. }
  33. int CCreature::getQuantityID(const int & quantity)
  34. {
  35. if (quantity<5)
  36. return 1;
  37. if (quantity<10)
  38. return 2;
  39. if (quantity<20)
  40. return 3;
  41. if (quantity<50)
  42. return 4;
  43. if (quantity<100)
  44. return 5;
  45. if (quantity<250)
  46. return 6;
  47. if (quantity<500)
  48. return 7;
  49. if (quantity<1000)
  50. return 8;
  51. return 9;
  52. }
  53. int CCreature::estimateCreatureCount(ui32 countID)
  54. {
  55. static const int creature_count[] = { 0, 3, 8, 15, 35, 75, 175, 375, 750, 2500 };
  56. if (countID > 9)
  57. assert("Wrong countID!");
  58. return creature_count[countID];
  59. }
  60. bool CCreature::isDoubleWide() const
  61. {
  62. return doubleWide;
  63. }
  64. bool CCreature::isFlying() const
  65. {
  66. return hasBonusOfType(Bonus::FLYING);
  67. }
  68. bool CCreature::isShooting() const
  69. {
  70. return hasBonusOfType(Bonus::SHOOTER);
  71. }
  72. bool CCreature::isUndead() const
  73. {
  74. return hasBonusOfType(Bonus::UNDEAD);
  75. }
  76. /**
  77. * Determines if the creature is of a good alignment.
  78. * @return true if the creture is good, false otherwise.
  79. */
  80. bool CCreature::isGood () const
  81. {
  82. return VLC->creh->isGood(faction);
  83. }
  84. /**
  85. * Determines if the creature is of an evil alignment.
  86. * @return true if the creature is evil, false otherwise.
  87. */
  88. bool CCreature::isEvil () const
  89. {
  90. return VLC->creh->isEvil(faction);
  91. }
  92. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  93. {
  94. int ret = 2147483645;
  95. int resAmnt = std::min(res.size(),cost.size());
  96. for(int i=0;i<resAmnt;i++)
  97. if(cost[i])
  98. ret = std::min(ret,(int)(res[i]/cost[i]));
  99. return ret;
  100. }
  101. CCreature::CCreature()
  102. {
  103. doubleWide = false;
  104. setNodeType(CBonusSystemNode::CREATURE);
  105. }
  106. void CCreature::addBonus(int val, int type, int subtype /*= -1*/)
  107. {
  108. Bonus *added = new Bonus(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
  109. addNewBonus(added);
  110. }
  111. // void CCreature::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  112. // {
  113. // out.insert (VLC->creh->globalEffects);
  114. // }
  115. bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
  116. {
  117. //TODO upgrade of upgrade?
  118. return vstd::contains(upgrades, anotherCre->idNumber);
  119. }
  120. bool CCreature::valid() const
  121. {
  122. return this == VLC->creh->creatures[idNumber];
  123. }
  124. std::string CCreature::nodeName() const
  125. {
  126. return "\"" + namePl + "\"";
  127. }
  128. bool CCreature::isItNativeTerrain(int terrain) const
  129. {
  130. return VLC->townh->factions[0].nativeTerrain == terrain; //FIXME: handle neutral faction properly
  131. }
  132. int readNumber(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadCreatures() and loadUnitAnimInfo()
  133. {
  134. befi=i;
  135. for(; i<andame; ++i)
  136. {
  137. if(buf[i]=='\t')
  138. break;
  139. }
  140. std::string tmp = buf.substr(befi, i-befi);
  141. int ret = atoi(buf.substr(befi, i-befi).c_str());
  142. ++i;
  143. return ret;
  144. }
  145. /**
  146. * Determines if a faction is good.
  147. * @param ID of the faction.
  148. * @return true if the faction is good, false otherwise.
  149. */
  150. bool CCreatureHandler::isGood (si8 faction) const
  151. {
  152. return faction != -1 && factionAlignments[faction] == 1;
  153. }
  154. /**
  155. * Determines if a faction is evil.
  156. * @param ID of the faction.
  157. * @return true if the faction is evil, false otherwise.
  158. */
  159. bool CCreatureHandler::isEvil (si8 faction) const
  160. {
  161. return faction != -1 && factionAlignments[faction] == -1;
  162. }
  163. static void AddAbility(CCreature *cre, const JsonVector &ability_vec)
  164. {
  165. Bonus *nsf = new Bonus();
  166. std::string type = ability_vec[0].String();
  167. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  168. if (it == bonusNameMap.end()) {
  169. if (type == "DOUBLE_WIDE")
  170. cre->doubleWide = true;
  171. else if (type == "ENEMY_MORALE_DECREASING") {
  172. cre->addBonus(-1, Bonus::MORALE);
  173. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  174. }
  175. else if (type == "ENEMY_LUCK_DECREASING") {
  176. cre->addBonus(-1, Bonus::LUCK);
  177. cre->getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  178. } else
  179. tlog1 << "Error: invalid ability type " << type << " in creatures.txt" << std::endl;
  180. return;
  181. }
  182. nsf->type = it->second;
  183. nsf->val = ability_vec[1].Float();
  184. nsf->subtype = ability_vec[2].Float();
  185. nsf->additionalInfo = ability_vec[3].Float();
  186. nsf->source = Bonus::CREATURE_ABILITY;
  187. nsf->sid = cre->idNumber;
  188. //nsf->duration = Bonus::ONE_BATTLE; //what the?
  189. nsf->duration = Bonus::PERMANENT;
  190. nsf->turnsRemain = 0;
  191. cre->addNewBonus(nsf);
  192. }
  193. static void RemoveAbility(CCreature *cre, const JsonNode &ability)
  194. {
  195. std::string type = ability.String();
  196. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  197. if (it == bonusNameMap.end()) {
  198. if (type == "DOUBLE_WIDE")
  199. cre->doubleWide = false;
  200. else
  201. tlog1 << "Error: invalid ability type " << type << " in creatures.json" << std::endl;
  202. return;
  203. }
  204. int typeNo = it->second;
  205. Bonus::BonusType ecf = static_cast<Bonus::BonusType>(typeNo);
  206. Bonus *b = cre->getBonusLocalFirst(Selector::type(ecf));
  207. cre->removeBonus(b);
  208. }
  209. void CCreatureHandler::loadCreatures()
  210. {
  211. tlog5 << "\t\tReading config/cr_abils.json and ZCRTRAIT.TXT" << std::endl;
  212. ////////////reading ZCRTRAIT.TXT ///////////////////
  213. auto textFile = CResourceHandler::get()->loadData(ResourceID("DATA/ZCRTRAIT.TXT"));
  214. std::string buf((char*)textFile.first.get(), textFile.second);
  215. int andame = buf.size();
  216. int i=0; //buf iterator
  217. int hmcr=0;
  218. for(; i<andame; ++i)
  219. {
  220. if(buf[i]=='\r')
  221. ++hmcr;
  222. if(hmcr==2)
  223. break;
  224. }
  225. i+=2;
  226. while(i<buf.size())
  227. {
  228. CCreature &ncre = *new CCreature;
  229. ncre.idNumber = creatures.size();
  230. ncre.cost.resize(GameConstants::RESOURCE_QUANTITY);
  231. ncre.level=0;
  232. ncre.iconIndex = ncre.idNumber + 2; // +2 for empty\selection images
  233. int befi=i;
  234. for(; i<andame; ++i)
  235. {
  236. if(buf[i]=='\t')
  237. break;
  238. }
  239. ncre.nameSing = buf.substr(befi, i-befi);
  240. ++i;
  241. befi=i;
  242. for(; i<andame; ++i)
  243. {
  244. if(buf[i]=='\t')
  245. break;
  246. }
  247. ncre.namePl = buf.substr(befi, i-befi);
  248. ++i;
  249. for(int v=0; v<7; ++v)
  250. {
  251. ncre.cost[v] = readNumber(befi, i, andame, buf);
  252. }
  253. ncre.fightValue = readNumber(befi, i, andame, buf);
  254. ncre.AIValue = readNumber(befi, i, andame, buf);
  255. ncre.growth = readNumber(befi, i, andame, buf);
  256. ncre.hordeGrowth = readNumber(befi, i, andame, buf);
  257. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::STACK_HEALTH);
  258. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::STACKS_SPEED);
  259. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  260. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  261. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::CREATURE_DAMAGE, 1);
  262. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::CREATURE_DAMAGE, 2);
  263. ncre.addBonus(readNumber(befi, i, andame, buf), Bonus::SHOTS);
  264. //spells - not used?
  265. readNumber(befi, i, andame, buf);
  266. ncre.ammMin = readNumber(befi, i, andame, buf);
  267. ncre.ammMax = readNumber(befi, i, andame, buf);
  268. befi=i;
  269. for(; i<andame; ++i)
  270. {
  271. if(buf[i]=='\t')
  272. break;
  273. }
  274. ncre.abilityText = buf.substr(befi, i-befi);
  275. ++i;
  276. befi=i;
  277. for(; i<andame; ++i)
  278. {
  279. if(buf[i]=='\r')
  280. break;
  281. }
  282. ncre.abilityRefs = buf.substr(befi, i-befi);
  283. i+=2;
  284. if(true)
  285. { //adding abilities from ZCRTRAIT.TXT
  286. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  287. ncre.doubleWide = true;
  288. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  289. ncre.addBonus(0, Bonus::FLYING);
  290. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  291. ncre.addBonus(0, Bonus::SHOOTER);
  292. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  293. ncre.addBonus(0, Bonus::SIEGE_WEAPON);
  294. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  295. ncre.addBonus(1, Bonus::ADDITIONAL_ATTACK);
  296. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  297. ncre.addBonus(0, Bonus::BLOCKS_RETALIATION);
  298. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  299. ncre.addBonus(0, Bonus::UNDEAD);
  300. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  301. ncre.addBonus(0, Bonus::NO_MELEE_PENALTY);
  302. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  303. ncre.addBonus(0, Bonus::JOUSTING);
  304. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  305. {
  306. ncre.addBonus(+1, Bonus::MORALE);;
  307. ncre.getBonusList().back()->addPropagator(make_shared<CPropagatorNodeType>(CBonusSystemNode::HERO));
  308. }
  309. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  310. {
  311. ncre.addBonus(-1, Bonus::MORALE);;
  312. ncre.getBonusList().back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  313. }
  314. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  315. ncre.addBonus(0, Bonus::KING1);
  316. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  317. ncre.addBonus(0, Bonus::KING2);
  318. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  319. ncre.addBonus(0, Bonus::KING3);
  320. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  321. ncre.addBonus(0, Bonus::NO_WALL_PENALTY);
  322. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  323. ncre.addBonus(0, Bonus::CATAPULT);
  324. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  325. ncre.addBonus(0, Bonus::ATTACKS_ALL_ADJACENT);
  326. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  327. ncre.addBonus(0, Bonus::MIND_IMMUNITY); //giants are immune to mind spells
  328. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  329. ncre.addBonus(0, Bonus::FIRE_IMMUNITY);
  330. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  331. ncre.addBonus(0, Bonus::TWO_HEX_ATTACK_BREATH);;
  332. }
  333. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  334. {
  335. ncre.idNumber = creatures.size();
  336. creatures.push_back(&ncre);
  337. }
  338. }
  339. // loading creatures properties
  340. tlog5 << "\t\tReading config/creatures.json" << std::endl;
  341. const JsonNode config(ResourceID("config/creatures.json"));
  342. BOOST_FOREACH(const JsonNode &creature, config["creatures"].Vector())
  343. {
  344. int creatureID = creature["id"].Float();
  345. const JsonNode *value;
  346. /* A creature can have several names. */
  347. BOOST_FOREACH(const JsonNode &name, creature["name"].Vector())
  348. {
  349. boost::assign::insert(nameToID)(name.String(), creatureID);
  350. }
  351. // Set various creature properties
  352. CCreature *c = creatures[creatureID];
  353. c->level = creature["level"].Float();
  354. c->faction = creature["faction"].Float();
  355. c->animDefName = creature["defname"].String();
  356. BOOST_FOREACH(const JsonNode &value, creature["upgrades"].Vector())
  357. {
  358. c->upgrades.insert(value.Float());
  359. }
  360. value = &creature["projectile_defname"];
  361. if (!value->isNull())
  362. {
  363. c->projectile = value->String();
  364. value = &creature["projectile_spin"];
  365. c->projectileSpin = value->Bool();
  366. }
  367. value = &creature["turret_shooter"];
  368. if (!value->isNull() && value->Bool())
  369. factionToTurretCreature[c->faction] = creatureID;
  370. value = &creature["ability_add"];
  371. if (!value->isNull()) {
  372. BOOST_FOREACH(const JsonNode &ability, value->Vector())
  373. {
  374. AddAbility(c, ability.Vector());
  375. }
  376. }
  377. value = &creature["ability_remove"];
  378. if (!value->isNull())
  379. {
  380. BOOST_FOREACH(const JsonNode &ability, value->Vector())
  381. {
  382. RemoveAbility(c, ability);
  383. }
  384. }
  385. }
  386. BOOST_FOREACH(const JsonNode &creature, config["unused_creatures"].Vector())
  387. {
  388. notUsedMonsters += creature.Float();
  389. }
  390. loadAnimationInfo();
  391. loadSoundsInfo();
  392. //reading creature ability names
  393. const JsonNode config2(ResourceID("config/bonusnames.json"));
  394. BOOST_FOREACH(const JsonNode &bonus, config2["bonuses"].Vector())
  395. {
  396. std::map<std::string,int>::const_iterator it_map;
  397. std::string bonusID = bonus["id"].String();
  398. it_map = bonusNameMap.find(bonusID);
  399. if (it_map != bonusNameMap.end())
  400. stackBonuses[it_map->second] = std::pair<std::string, std::string>(bonus["name"].String(), bonus["description"].String());
  401. else
  402. tlog2 << "Bonus " << bonusID << " not recognized, ignoring\n";
  403. }
  404. //handle magic resistance secondary skill premy, potentialy may be buggy
  405. //std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  406. //stackBonuses[Bonus::SECONDARY_SKILL_PREMY] = std::pair<std::string, std::string>(it->second.first, it->second.second);
  407. if (VLC->modh->modules.STACK_EXP) //reading default stack experience bonuses
  408. {
  409. CLegacyConfigParser parser("DATA/CREXPBON.TXT");
  410. Bonus b; //prototype with some default properties
  411. b.source = Bonus::STACK_EXPERIENCE;
  412. b.duration = Bonus::PERMANENT;
  413. b.valType = Bonus::ADDITIVE_VALUE;
  414. b.effectRange = Bonus::NO_LIMIT;
  415. b.additionalInfo = 0;
  416. b.turnsRemain = 0;
  417. BonusList bl;
  418. parser.endLine();
  419. parser.readString(); //ignore index
  420. loadStackExp(b, bl, parser);
  421. BOOST_FOREACH(Bonus * b, bl)
  422. addBonusForAllCreatures(b); //health bonus is common for all
  423. parser.endLine();
  424. for (i = 1; i < 7; ++i)
  425. {
  426. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  427. {
  428. parser.readString(); //ignore index
  429. bl.clear();
  430. loadStackExp(b, bl, parser);
  431. BOOST_FOREACH(Bonus * b, bl)
  432. addBonusForTier(i, b);
  433. parser.endLine();
  434. }
  435. }
  436. for (int j = 0; j < 4; ++j) //tier 7
  437. {
  438. parser.readString(); //ignore index
  439. bl.clear();
  440. loadStackExp(b, bl, parser);
  441. BOOST_FOREACH(Bonus * b, bl)
  442. {
  443. addBonusForTier(7, b);
  444. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  445. }
  446. parser.endLine();
  447. }
  448. do //parse everything that's left
  449. {
  450. b.sid = parser.readNumber(); //id = this particular creature ID
  451. loadStackExp(b, creatures[b.sid]->getBonusList(), parser); //add directly to CCreature Node
  452. }
  453. while (parser.endLine());
  454. //Calculate rank exp values, formula appears complicated bu no parsing needed
  455. expRanks.resize(8);
  456. int dif = 0;
  457. int it = 8000; //ignore name of this variable
  458. expRanks[0].push_back(it);
  459. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  460. {
  461. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  462. dif += it/5;
  463. }
  464. for (i = 1; i < 8; ++i)
  465. {
  466. dif = 0;
  467. it = 1000 * i;
  468. expRanks[i].push_back(it);
  469. for (int j = 1; j < 10; ++j)
  470. {
  471. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  472. dif += it/5;
  473. }
  474. }
  475. CLegacyConfigParser expBonParser("DATA/CREXPMOD.TXT");
  476. expBonParser.endLine(); //header
  477. maxExpPerBattle.resize(8);
  478. for (i = 1; i < 8; ++i)
  479. {
  480. expBonParser.readString(); //index
  481. expBonParser.readString(); //float multiplier -> hardcoded
  482. expBonParser.readString(); //ignore upgrade mod? ->hardcoded
  483. expBonParser.readString(); //already calculated
  484. maxExpPerBattle[i] = expBonParser.readNumber();
  485. expRanks[i].push_back(expRanks[i].back() + expBonParser.readNumber());
  486. expBonParser.endLine();
  487. }
  488. //skeleton gets exp penalty
  489. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  490. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  491. //exp for tier >7, rank 11
  492. expRanks[0].push_back(147000);
  493. expAfterUpgrade = 75; //percent
  494. maxExpPerBattle[0] = maxExpPerBattle[7];
  495. }//end of Stack Experience
  496. tlog5 << "\t\tReading config/commanders.json" << std::endl;
  497. const JsonNode config3(ResourceID("config/commanders.json"));
  498. BOOST_FOREACH (auto creature, config3["factionCreatures"].Vector())
  499. {
  500. factionCommanders[creature["faction"].Float()] = creature["id"].Float();
  501. }
  502. BOOST_FOREACH (auto bonus, config3["bonusPerLevel"].Vector())
  503. {
  504. commanderLevelPremy.push_back(ParseBonus (bonus.Vector()));
  505. }
  506. i = 0;
  507. BOOST_FOREACH (auto skill, config3["skillLevels"].Vector())
  508. {
  509. skillLevels.push_back (std::vector<ui8>());
  510. BOOST_FOREACH (auto skillLevel, skill["levels"].Vector())
  511. {
  512. skillLevels[i].push_back (skillLevel.Float());
  513. }
  514. ++i;
  515. }
  516. BOOST_FOREACH (auto ability, config3["abilityRequirements"].Vector())
  517. {
  518. std::pair <Bonus, std::pair <ui8, ui8> > a;
  519. a.first = *ParseBonus (ability["ability"].Vector());
  520. a.second.first = ability["skills"].Vector()[0].Float();
  521. a.second.second = ability["skills"].Vector()[1].Float();
  522. skillRequirements.push_back (a);
  523. }
  524. }
  525. void CCreatureHandler::loadAnimationInfo()
  526. {
  527. CLegacyConfigParser parser("DATA/CRANIM.TXT");
  528. parser.endLine(); // header
  529. parser.endLine();
  530. for(int dd=0; dd<creatures.size(); ++dd)
  531. {
  532. while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
  533. ;
  534. loadUnitAnimInfo(*creatures[dd], parser);
  535. }
  536. }
  537. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, CLegacyConfigParser & parser)
  538. {
  539. unit.timeBetweenFidgets = parser.readNumber();
  540. unit.walkAnimationTime = parser.readNumber();
  541. unit.attackAnimationTime = parser.readNumber();
  542. unit.flightAnimationDistance = parser.readNumber();
  543. ///////////////////////
  544. unit.upperRightMissleOffsetX = parser.readNumber();
  545. unit.upperRightMissleOffsetY = parser.readNumber();
  546. unit.rightMissleOffsetX = parser.readNumber();
  547. unit.rightMissleOffsetY = parser.readNumber();
  548. unit.lowerRightMissleOffsetX = parser.readNumber();
  549. unit.lowerRightMissleOffsetY = parser.readNumber();
  550. ///////////////////////
  551. for(int jjj=0; jjj<12; ++jjj)
  552. {
  553. unit.missleFrameAngles[jjj] = parser.readNumber();
  554. }
  555. unit.troopCountLocationOffset= parser.readNumber();
  556. unit.attackClimaxFrame = parser.readNumber();
  557. parser.endLine();
  558. }
  559. void CCreatureHandler::loadSoundsInfo()
  560. {
  561. tlog5 << "\t\tReading config/cr_sounds.json" << std::endl;
  562. const JsonNode config(ResourceID("config/cr_sounds.json"));
  563. if (!config["creature_sounds"].isNull())
  564. {
  565. BOOST_FOREACH(const JsonNode &node, config["creature_sounds"].Vector())
  566. {
  567. const JsonNode *value;
  568. int id;
  569. value = &node["name"];
  570. bmap<std::string,int>::const_iterator i = nameToID.find(value->String());
  571. if (i != nameToID.end())
  572. id = i->second;
  573. else
  574. {
  575. tlog1 << "Sound info for an unknown creature: " << value->String() << std::endl;
  576. continue;
  577. }
  578. /* This is a bit ugly. Maybe we should use an array for
  579. * sound ids instead of separate variables and define
  580. * attack/defend/killed/... as indexes. */
  581. #define GET_SOUND_VALUE(value_name) do { value = &node[#value_name]; if (!value->isNull()) creatures[id]->sounds.value_name = value->String(); } while(0)
  582. GET_SOUND_VALUE(attack);
  583. GET_SOUND_VALUE(defend);
  584. GET_SOUND_VALUE(killed);
  585. GET_SOUND_VALUE(move);
  586. GET_SOUND_VALUE(shoot);
  587. GET_SOUND_VALUE(wince);
  588. GET_SOUND_VALUE(ext1);
  589. GET_SOUND_VALUE(ext2);
  590. GET_SOUND_VALUE(startMoving);
  591. GET_SOUND_VALUE(endMoving);
  592. #undef GET_SOUND_VALUE
  593. }
  594. }
  595. }
  596. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
  597. {
  598. bool enable = false; //some bonuses are activated with values 2 or 1
  599. std::string buf = parser.readString();
  600. std::string mod = parser.readString();
  601. switch (buf[0])
  602. {
  603. case 'H':
  604. b.type = Bonus::STACK_HEALTH;
  605. b.valType = Bonus::PERCENT_TO_BASE;
  606. break;
  607. case 'A':
  608. b.type = Bonus::PRIMARY_SKILL;
  609. b.subtype = PrimarySkill::ATTACK;
  610. break;
  611. case 'D':
  612. b.type = Bonus::PRIMARY_SKILL;
  613. b.subtype = PrimarySkill::DEFENSE;
  614. break;
  615. case 'M': //Max damage
  616. b.type = Bonus::CREATURE_DAMAGE;
  617. b.subtype = 2;
  618. break;
  619. case 'm': //Min damage
  620. b.type = Bonus::CREATURE_DAMAGE;
  621. b.subtype = 1;
  622. break;
  623. case 'S':
  624. b.type = Bonus::STACKS_SPEED; break;
  625. case 'O':
  626. b.type = Bonus::SHOTS; break;
  627. case 'b':
  628. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  629. case 'C':
  630. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  631. case 'd':
  632. b.type = Bonus::DEFENSIVE_STANCE; break;
  633. case 'e':
  634. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  635. case 'E':
  636. b.type = Bonus::DEATH_STARE;
  637. b.subtype = 0; //Gorgon
  638. break;
  639. case 'g':
  640. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  641. b.subtype = -1; //all magic schools
  642. break;
  643. case 'P':
  644. b.type = Bonus::CASTS; break;
  645. case 'R':
  646. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  647. case 'W':
  648. b.type = Bonus::MAGIC_RESISTANCE;
  649. b.subtype = 0; //otherwise creature window goes crazy
  650. break;
  651. case 'f': //on-off skill
  652. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  653. switch (mod[0])
  654. {
  655. case 'A':
  656. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  657. case 'b':
  658. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  659. case 'B':
  660. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  661. case 'c':
  662. b.type = Bonus::JOUSTING; break;
  663. case 'D':
  664. b.type = Bonus::ADDITIONAL_ATTACK; break;
  665. case 'f':
  666. b.type = Bonus::FEARLESS; break;
  667. case 'F':
  668. b.type = Bonus::FLYING; break;
  669. case 'm':
  670. b.type = Bonus::SELF_MORALE; break;
  671. case 'M':
  672. b.type = Bonus::NO_MORALE; break;
  673. case 'p': //Mind spells
  674. case 'P':
  675. b.type = Bonus::MIND_IMMUNITY; break;
  676. case 'r':
  677. b.type = Bonus::REBIRTH; //on/off? makes sense?
  678. b.subtype = 0;
  679. b.val = 20; //arbitrary value
  680. break;
  681. case 'R':
  682. b.type = Bonus::BLOCKS_RETALIATION; break;
  683. case 's':
  684. b.type = Bonus::FREE_SHOOTING; break;
  685. case 'u':
  686. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  687. case 'U':
  688. b.type = Bonus::UNDEAD; break;
  689. default:
  690. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  691. return;
  692. break;
  693. }
  694. break;
  695. case 'w': //specific spell immunities, enabled/disabled
  696. enable = true;
  697. switch (mod[0])
  698. {
  699. case 'B': //Blind
  700. b.type = Bonus::SPELL_IMMUNITY;
  701. b.subtype = 74;
  702. break;
  703. case 'H': //Hypnotize
  704. b.type = Bonus::SPELL_IMMUNITY;
  705. b.subtype = 60;
  706. break;
  707. case 'I': //Implosion
  708. b.type = Bonus::SPELL_IMMUNITY;
  709. b.subtype = 18;
  710. break;
  711. case 'K': //Berserk
  712. b.type = Bonus::SPELL_IMMUNITY;
  713. b.subtype = 59;
  714. break;
  715. case 'M': //Meteor Shower
  716. b.type = Bonus::SPELL_IMMUNITY;
  717. b.subtype = 23;
  718. break;
  719. case 'N': //dispell beneficial spells
  720. b.type = Bonus::SPELL_IMMUNITY;
  721. b.subtype = 78;
  722. break;
  723. case 'R': //Armageddon
  724. b.type = Bonus::SPELL_IMMUNITY;
  725. b.subtype = 26;
  726. break;
  727. case 'S': //Slow
  728. b.type = Bonus::SPELL_IMMUNITY;
  729. b.subtype = 54;
  730. break;
  731. case '6':
  732. case '7':
  733. case '8':
  734. case '9':
  735. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  736. b.val = std::atoi(mod.c_str()) - 5;
  737. break;
  738. case ':':
  739. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  740. b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
  741. break;
  742. case 'F':
  743. b.type = Bonus::FIRE_IMMUNITY;
  744. b.subtype = 1; //not positive
  745. break;
  746. case 'O':
  747. b.type = Bonus::FIRE_IMMUNITY;
  748. b.subtype = 2; //only direct damage
  749. break;
  750. case 'f':
  751. b.type = Bonus::FIRE_IMMUNITY;
  752. b.subtype = 0; //all
  753. break;
  754. case 'C':
  755. b.type = Bonus::WATER_IMMUNITY;
  756. b.subtype = 1; //not positive
  757. break;
  758. case 'W':
  759. b.type = Bonus::WATER_IMMUNITY;
  760. b.subtype = 2; //only direct damage
  761. break;
  762. case 'w':
  763. b.type = Bonus::WATER_IMMUNITY;
  764. b.subtype = 0; //all
  765. break;
  766. case 'E':
  767. b.type = Bonus::EARTH_IMMUNITY;
  768. b.subtype = 2; //only direct damage
  769. break;
  770. case 'e':
  771. b.type = Bonus::EARTH_IMMUNITY;
  772. b.subtype = 0; //all
  773. break;
  774. case 'A':
  775. b.type = Bonus::AIR_IMMUNITY;
  776. b.subtype = 2; //only direct damage
  777. break;
  778. case 'a':
  779. b.type = Bonus::AIR_IMMUNITY;
  780. b.subtype = 0; //all
  781. break;
  782. case 'D':
  783. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  784. break;
  785. case '0':
  786. b.type = Bonus::RECEPTIVE;
  787. break;
  788. default:
  789. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  790. return;
  791. }
  792. break;
  793. case 'i':
  794. enable = true;
  795. b.type = Bonus::NO_DISTANCE_PENALTY;
  796. break;
  797. case 'o':
  798. enable = true;
  799. b.type = Bonus::NO_WALL_PENALTY;
  800. break;
  801. case 'a':
  802. case 'c': //some special abilities are threated as spells, work in progress
  803. b.type = Bonus::SPELL_AFTER_ATTACK;
  804. b.subtype = stringToNumber(mod);
  805. break;
  806. case 'h':
  807. b.type= Bonus::HATE;
  808. b.subtype = stringToNumber(mod);
  809. break;
  810. case 'p':
  811. b.type = Bonus::SPELL_BEFORE_ATTACK;
  812. b.subtype = stringToNumber(mod);
  813. b.additionalInfo = 3; //always expert?
  814. break;
  815. default:
  816. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  817. return;
  818. break;
  819. }
  820. switch (mod[0])
  821. {
  822. case '+':
  823. case '=': //should we allow percent values to stack or pick highest?
  824. b.valType = Bonus::ADDITIVE_VALUE;
  825. break;
  826. }
  827. //limiters, range
  828. si32 lastVal, curVal, lastLev = 0;
  829. if (enable) //0 and 2 means non-active, 1 - active
  830. {
  831. if (b.type != Bonus::REBIRTH)
  832. b.val = 0; //on-off ability, no value specified
  833. curVal = parser.readNumber();// 0 level is never active
  834. for (int i = 1; i < 11; ++i)
  835. {
  836. curVal = parser.readNumber();
  837. if (curVal == 1)
  838. {
  839. b.limiter.reset (new RankRangeLimiter(i));
  840. bl.push_back(new Bonus(b));
  841. break; //never turned off it seems
  842. }
  843. }
  844. }
  845. else
  846. {
  847. lastVal = parser.readNumber(); //basic value, not particularly useful but existent
  848. for (int i = 1; i < 11; ++i)
  849. {
  850. curVal = parser.readNumber();
  851. if (b.type == Bonus::HATE)
  852. curVal *= 10; //odd fix
  853. if (curVal > lastVal) //threshold, add new bonus
  854. {
  855. b.val = curVal - lastVal;
  856. lastVal = curVal;
  857. b.limiter.reset (new RankRangeLimiter(i));
  858. bl.push_back(new Bonus(b));
  859. lastLev = i; //start new range from here, i = previous rank
  860. }
  861. else if (curVal < lastVal)
  862. {
  863. b.val = lastVal;
  864. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  865. }
  866. }
  867. }
  868. }
  869. int CCreatureHandler::stringToNumber(std::string & s)
  870. {
  871. boost::algorithm::replace_first(s,"#",""); //drop hash character
  872. return std::atoi(s.c_str());
  873. }
  874. CCreatureHandler::~CCreatureHandler()
  875. {
  876. }
  877. static int retreiveRandNum(const boost::function<int()> &randGen)
  878. {
  879. if(randGen)
  880. return randGen();
  881. else
  882. return rand();
  883. }
  884. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  885. {
  886. return v[retreiveRandNum(randGen) % v.size()];
  887. }
  888. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  889. {
  890. int r = 0;
  891. if(tier == -1) //pick any allowed creature
  892. {
  893. do
  894. {
  895. r = pickRandomElementOf(creatures, randGen)->idNumber;
  896. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  897. }
  898. else
  899. {
  900. assert(vstd::iswithin(tier, 1, 7));
  901. std::vector<int> allowed;
  902. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  903. {
  904. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  905. int creid = static_cast<const CCreature*>(b)->idNumber;
  906. if(!vstd::contains(notUsedMonsters, creid))
  907. allowed.push_back(creid);
  908. }
  909. if(!allowed.size())
  910. {
  911. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  912. return 0;
  913. }
  914. return pickRandomElementOf(allowed, randGen);
  915. }
  916. return r;
  917. }
  918. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  919. {
  920. assert(vstd::iswithin(tier, 1, 7));
  921. creaturesOfLevel[tier].addNewBonus(b);
  922. }
  923. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  924. {
  925. allCreatures.addNewBonus(b);
  926. }
  927. void CCreatureHandler::buildBonusTreeForTiers()
  928. {
  929. BOOST_FOREACH(CCreature *c, creatures)
  930. {
  931. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  932. c->attachTo(&creaturesOfLevel[c->level]);
  933. else
  934. c->attachTo(&creaturesOfLevel[0]);
  935. }
  936. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  937. b.attachTo(&allCreatures);
  938. }
  939. void CCreatureHandler::deserializationFix()
  940. {
  941. buildBonusTreeForTiers();
  942. }