CCreatureHandler.cpp 31 KB

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