CCreatureHandler.cpp 31 KB

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