CCreatureHandler.cpp 28 KB

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