CCreatureHandler.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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.spells = readNumber(befi, i, andame, buf);
  258. ncre.ammMin = readNumber(befi, i, andame, buf);
  259. ncre.ammMax = readNumber(befi, i, andame, buf);
  260. befi=i;
  261. for(; i<andame; ++i)
  262. {
  263. if(buf[i]=='\t')
  264. break;
  265. }
  266. ncre.abilityText = buf.substr(befi, i-befi);
  267. ++i;
  268. befi=i;
  269. for(; i<andame; ++i)
  270. {
  271. if(buf[i]=='\r')
  272. break;
  273. }
  274. ncre.abilityRefs = buf.substr(befi, i-befi);
  275. i+=2;
  276. if(useCreAbilsFromZCRTRAIT)
  277. { //adding abilities from ZCRTRAIT.TXT
  278. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  279. ncre.doubleWide = true;
  280. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  281. ncre.addBonus(0, Bonus::FLYING);
  282. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  283. ncre.addBonus(0, Bonus::SHOOTER);
  284. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  285. ncre.addBonus(0, Bonus::SIEGE_WEAPON);
  286. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  287. ncre.addBonus(1, Bonus::ADDITIONAL_ATTACK);
  288. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  289. ncre.addBonus(0, Bonus::BLOCKS_RETALIATION);
  290. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  291. ncre.addBonus(0, Bonus::UNDEAD);
  292. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  293. ncre.addBonus(0, Bonus::NO_MELEE_PENALTY);
  294. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  295. ncre.addBonus(0, Bonus::JOUSTING);
  296. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  297. {
  298. ncre.addBonus(+1, Bonus::MORALE);;
  299. ncre.bonuses.back()->effectRange = Bonus::ONLY_ALLIED_ARMY;
  300. }
  301. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  302. {
  303. ncre.addBonus(-1, Bonus::MORALE);;
  304. ncre.bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  305. }
  306. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  307. ncre.addBonus(0, Bonus::KING1);
  308. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  309. ncre.addBonus(0, Bonus::KING2);
  310. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  311. ncre.addBonus(0, Bonus::KING3);
  312. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  313. ncre.addBonus(0, Bonus::NO_WALL_PENALTY);
  314. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  315. ncre.addBonus(0, Bonus::CATAPULT);
  316. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  317. ncre.addBonus(0, Bonus::ATTACKS_ALL_ADJACENT);
  318. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  319. {
  320. std::vector<int> mindSpells = getMindSpells();
  321. for(int g=0; g<mindSpells.size(); ++g)
  322. ncre.addBonus(0, Bonus::SPELL_IMMUNITY, mindSpells[g]); //giants are immune to mind spells
  323. }
  324. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  325. ncre.addBonus(0, Bonus::FIRE_IMMUNITY);
  326. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  327. ncre.addBonus(0, Bonus::TWO_HEX_ATTACK_BREATH);;
  328. }
  329. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  330. {
  331. ncre.idNumber = creatures.size();
  332. creatures.push_back(&ncre);
  333. }
  334. }
  335. ////second part of reading cr_abils.txt////
  336. bool contReading = true;
  337. while(contReading) //main reading loop
  338. {
  339. abils.getline(abilLine, MAX_LINE_SIZE);
  340. std::istringstream reader(abilLine);
  341. char command;
  342. reader >> command;
  343. switch(command)
  344. {
  345. case '+': //add new ability
  346. {
  347. int creatureID;
  348. Bonus *nsf = new Bonus();
  349. si32 buf;
  350. std::string type;
  351. reader >> creatureID;
  352. reader >> type;
  353. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  354. CCreature *cre = creatures[creatureID];
  355. if (it == bonusNameMap.end())
  356. {
  357. if(type == "DOUBLE_WIDE")
  358. cre->doubleWide = true;
  359. else if(type == "ENEMY_MORALE_DECREASING")
  360. {
  361. cre->addBonus(-1, Bonus::MORALE);
  362. cre->bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  363. }
  364. else if(type == "ENEMY_LUCK_DECREASING")
  365. {
  366. cre->addBonus(-1, Bonus::LUCK);
  367. cre->bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  368. }
  369. else
  370. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  371. break;
  372. }
  373. nsf->type = it->second;
  374. reader >> buf; nsf->val = buf;
  375. reader >> buf; nsf->subtype = buf;
  376. reader >> buf; nsf->additionalInfo = buf;
  377. nsf->source = Bonus::CREATURE_ABILITY;
  378. nsf->id = cre->idNumber;
  379. nsf->duration = Bonus::ONE_BATTLE;
  380. nsf->turnsRemain = 0;
  381. cre->addNewBonus(nsf);
  382. break;
  383. }
  384. case '-': //remove ability
  385. {
  386. int creatureID;
  387. std::string type;
  388. reader >> creatureID;
  389. reader >> type;
  390. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  391. if (it == bonusNameMap.end())
  392. {
  393. if(type == "DOUBLE_WIDE")
  394. creatures[creatureID]->doubleWide = false;
  395. else
  396. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  397. break;
  398. }
  399. int typeNo = it->second;
  400. Bonus::BonusType ecf = static_cast<Bonus::BonusType>(typeNo);
  401. Bonus *b = creatures[creatureID]->getBonus(Selector::type(ecf));
  402. creatures[creatureID]->removeBonus(b);
  403. break;
  404. }
  405. case '0': //end reading
  406. {
  407. contReading = false;
  408. break;
  409. }
  410. default: //invalid command
  411. {
  412. tlog1 << "Parse error in file config/cr_abils.txt" << std::endl;
  413. break;
  414. }
  415. }
  416. }
  417. abils.close();
  418. tlog5 << "\t\tReading config/crerefnam.txt" << std::endl;
  419. //loading reference names
  420. std::ifstream ifs(DATA_DIR "/config/crerefnam.txt");
  421. int tempi;
  422. std::string temps;
  423. for (;;)
  424. {
  425. ifs >> tempi >> temps;
  426. if (tempi>=creatures.size())
  427. break;
  428. boost::assign::insert(nameToID)(temps,tempi);
  429. creatures[tempi]->nameRef=temps;
  430. }
  431. ifs.close();
  432. ifs.clear();
  433. tlog5 << "\t\tReading config/monsters.txt" << std::endl;
  434. ifs.open(DATA_DIR "/config/monsters.txt");
  435. {
  436. while(!ifs.eof())
  437. {
  438. int id, lvl;
  439. ifs >> id >> lvl;
  440. if(!ifs.good())
  441. break;
  442. CCreature *c = creatures[id];
  443. c->level = lvl;
  444. if(isbetw(lvl, 0, ARRAY_COUNT(creaturesOfLevel)))
  445. c->attachTo(&creaturesOfLevel[lvl]);
  446. else
  447. c->attachTo(&creaturesOfLevel[0]);
  448. }
  449. }
  450. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  451. b.attachTo(&allCreatures);
  452. ifs.close();
  453. ifs.clear();
  454. tlog5 << "\t\tReading config/cr_factions.txt" << std::endl;
  455. ifs.open(DATA_DIR "/config/cr_factions.txt");
  456. while(!ifs.eof())
  457. {
  458. int id, fact;
  459. ifs >> id >> fact;
  460. creatures[id]->faction = fact;
  461. }
  462. ifs.close();
  463. ifs.clear();
  464. tlog5 << "\t\tReading config/cr_upgrade_list.txt" << std::endl;
  465. ifs.open(DATA_DIR "/config/cr_upgrade_list.txt");
  466. while(!ifs.eof())
  467. {
  468. int id, up;
  469. ifs >> id >> up;
  470. creatures[id]->upgrades.insert(up);
  471. }
  472. ifs.close();
  473. ifs.clear();
  474. //loading unit animation def names
  475. tlog5 << "\t\tReading config/CREDEFS.TXT" << std::endl;
  476. std::ifstream inp(DATA_DIR "/config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
  477. inp.seekg(0,std::ios::end); // na koniec
  478. int andame2 = inp.tellg(); // read length
  479. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  480. char * bufor = new char[andame2+1]; // allocate memory
  481. inp.read((char*)bufor, andame2); // read map file to buffer
  482. inp.close();
  483. bufor[andame2] = 0;
  484. buf = std::string(bufor);
  485. delete [] bufor;
  486. i = 0; //buf iterator
  487. hmcr = 0;
  488. for(; i<andame2; ++i) //omitting rubbish
  489. {
  490. if(buf[i]=='\r')
  491. break;
  492. }
  493. i+=2;
  494. tlog5 << "We have "<<creatures.size() << " creatures\n";
  495. for(int s=0; s<creatures.size(); ++s)
  496. {
  497. //tlog5 <<"\t\t\t" << s <<". Reading defname. \n";
  498. int befi=i;
  499. std::string rub;
  500. for(; i<andame2; ++i)
  501. {
  502. if(buf[i]==' ')
  503. break;
  504. }
  505. rub = buf.substr(befi, i-befi);
  506. ++i;
  507. befi=i;
  508. for(; i<andame2; ++i)
  509. {
  510. if(buf[i]=='\r')
  511. break;
  512. }
  513. std::string defName = buf.substr(befi, i-befi);
  514. creatures[s]->animDefName = defName;
  515. }
  516. tlog5 << "\t\tReading CRANIM.TXT.txt" << std::endl;
  517. loadAnimationInfo();
  518. //loading id to projectile mapping
  519. tlog5 << "\t\tReading config/cr_shots.txt" << std::endl;
  520. std::ifstream inp2(DATA_DIR "/config/cr_shots.txt", std::ios::in | std::ios::binary); //this file is not in lod
  521. char dump [200];
  522. inp2.getline(dump, 200);
  523. while(true)
  524. {
  525. int id;
  526. std::string name;
  527. bool spin;
  528. inp2>>id;
  529. if(id == -1)
  530. break;
  531. inp2>>name;
  532. idToProjectile[id] = name;
  533. inp2>>spin;
  534. idToProjectileSpin[id] = spin;
  535. }
  536. inp2.close();
  537. //reading factionToTurretCreature
  538. tlog5 << "\t\tReading config/cr_to_turret.txt" << std::endl;
  539. std::ifstream inp3(DATA_DIR "/config/cr_to_turret.txt", std::ios::in | std::ios::binary); //this file is not in lod
  540. std::string dump2;
  541. inp3 >> dump2 >> dump2;
  542. for(int g=0; g<F_NUMBER; ++g)
  543. {
  544. inp3 >> factionToTurretCreature[g];
  545. }
  546. inp3.close();
  547. //reading creature ability names
  548. ifs.open(DATA_DIR "/config/bonusnames.txt");
  549. {
  550. std::string buf2, buf3, line;
  551. int i;
  552. std::map<std::string,int>::const_iterator it;
  553. getline(ifs, line); //skip 1st line
  554. while(!ifs.eof())
  555. {
  556. getline(ifs, buf, '\t');
  557. getline(ifs, buf2, '\t');
  558. getline(ifs, buf3);
  559. it = bonusNameMap.find(buf);
  560. if (it != bonusNameMap.end())
  561. stackBonuses[it->second] = std::pair<std::string, std::string>(buf2,buf3);
  562. else
  563. tlog2 << "Bonus " << buf << " not recognized, ingoring\n";
  564. }
  565. }
  566. ifs.close();
  567. if (STACK_EXP) //reading default stack experience bonuses
  568. {
  569. buf = bitmaph->getTextFile("CREXPBON.TXT");
  570. int it = 0;
  571. si32 creid = -1;
  572. Bonus b; //prototype with some default properties
  573. b.source = Bonus::STACK_EXPERIENCE;
  574. b.duration = Bonus::PERMANENT;
  575. b.valType = Bonus::ADDITIVE_VALUE;
  576. b.effectRange = Bonus::NO_LIMIT;
  577. b.additionalInfo = 0;
  578. BonusList bl;
  579. loadToIt (dump2, buf, it, 3); //ignore first line
  580. loadToIt (dump2, buf, it, 4); //ignore index
  581. loadStackExp(b, bl, buf, it);
  582. BOOST_FOREACH(Bonus * b, bl)
  583. addBonusForAllCreatures(b); //health bonus is common for all
  584. loadToIt (dump2, buf, it, 3); //crop comment
  585. for (i = 1; i < 7; ++i)
  586. {
  587. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  588. {
  589. loadToIt (dump2, buf, it, 4); //ignore index
  590. bl.clear();
  591. loadStackExp(b, bl, buf, it);
  592. BOOST_FOREACH(Bonus * b, bl)
  593. addBonusForTier(i, b);
  594. loadToIt (dump2, buf, it, 3); //crop comment
  595. }
  596. }
  597. for (int j = 0; j < 4; ++j) //tier 7
  598. {
  599. loadToIt (dump2, buf, it, 4); //ignore index
  600. bl.clear();
  601. loadStackExp(b, bl, buf, it);
  602. BOOST_FOREACH(Bonus * b, bl)
  603. {
  604. addBonusForTier(7, b);
  605. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  606. }
  607. loadToIt (dump2, buf, it, 3); //crop comment
  608. }
  609. do //parse everything that's left
  610. {
  611. loadToIt(creid, buf, it, 4); //get index
  612. b.id = creid; //id = this particular creature ID
  613. loadStackExp(b, creatures[creid]->bonuses, buf, it); //add directly to CCreature Node
  614. loadToIt (dump2, buf, it, 3); //crop comment
  615. } while (it < buf.size());
  616. //Calculate rank exp values, formula appears complicated bu no parsing needed
  617. expRanks.resize(8);
  618. int dif = 0;
  619. it = 8000; //ignore name of this variable
  620. expRanks[0].push_back(it);
  621. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  622. {
  623. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  624. dif += it/5;
  625. }
  626. for (i = 1; i < 8; ++i)
  627. {
  628. dif = 0;
  629. it = 1000 * i;
  630. expRanks[i].push_back(it);
  631. for (int j = 1; j < 10; ++j)
  632. {
  633. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  634. dif += it/5;
  635. }
  636. }
  637. buf = bitmaph->getTextFile("CREXPMOD.TXT"); //could be hardcoded though, lots of useless info
  638. it = 0;
  639. loadToIt (dump2, buf, it, 3); //ignore first line
  640. maxExpPerBattle.resize(8);
  641. si32 val;
  642. for (i = 1; i < 8; ++i)
  643. {
  644. loadToIt (dump2, buf, it, 4); //index
  645. loadToIt (dump2, buf, it, 4); //float multiplier -> hardcoded
  646. loadToIt (dump2, buf, it, 4); //ignore upgrade mod? ->hardcoded
  647. loadToIt (dump2, buf, it, 4); //already calculated
  648. loadToIt (val, buf, it, 4);
  649. maxExpPerBattle[i] = (ui32)val;
  650. loadToIt (val, buf, it, 4); //11th level
  651. val += (si32)expRanks[i].back();
  652. expRanks[i].push_back((ui32)val);
  653. loadToIt (dump2, buf, it, 3); //crop comment
  654. }
  655. //skeleton gets exp penalty
  656. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  657. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  658. //exp for tier >7, rank 11
  659. expRanks[0].push_back(147000);
  660. expAfterUpgrade = 75; //percent
  661. maxExpPerBattle[0] = maxExpPerBattle[7];
  662. }//end of Stack Experience
  663. //experiment - add 100 to attack for creatures of tier 1
  664. // Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::OTHER, +100, 0, 0);
  665. // addBonusForTier(1, b);
  666. }
  667. void CCreatureHandler::loadAnimationInfo()
  668. {
  669. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  670. int andame = buf.size();
  671. int i=0; //buf iterator
  672. int hmcr=0;
  673. for(; i<andame; ++i)
  674. {
  675. if(buf[i]=='\r')
  676. ++hmcr;
  677. if(hmcr==2)
  678. break;
  679. }
  680. i+=2;
  681. for(int dd=0; dd<creatures.size(); ++dd)
  682. {
  683. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  684. loadUnitAnimInfo(*creatures[dd], buf, i);
  685. }
  686. return;
  687. }
  688. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  689. {
  690. int befi=i;
  691. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  692. while(unit.timeBetweenFidgets == 0.0)
  693. {
  694. for(; i<src.size(); ++i)
  695. {
  696. if(src[i]=='\r')
  697. break;
  698. }
  699. i+=2;
  700. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  701. }
  702. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  703. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  704. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  705. ///////////////////////
  706. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  707. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  708. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  709. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  710. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  711. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  712. ///////////////////////
  713. for(int jjj=0; jjj<12; ++jjj)
  714. {
  715. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  716. }
  717. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  718. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  719. for(; i<src.size(); ++i)
  720. {
  721. if(src[i]=='\r')
  722. break;
  723. }
  724. i+=2;
  725. }
  726. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it) //help function for parsing CREXPBON.txt
  727. {
  728. std::string buf, mod;
  729. bool enable = false; //some bonuses are activated with values 2 or 1
  730. loadToIt(buf, src, it, 4);
  731. loadToIt(mod, src, it, 4);
  732. switch (buf[0])
  733. {
  734. case 'H':
  735. b.type = Bonus::STACK_HEALTH;
  736. b.valType = Bonus::PERCENT_TO_BASE;
  737. break;
  738. case 'A':
  739. b.type = Bonus::PRIMARY_SKILL;
  740. b.subtype = PrimarySkill::ATTACK;
  741. break;
  742. case 'D':
  743. b.type = Bonus::PRIMARY_SKILL;
  744. b.subtype = PrimarySkill::DEFENSE;
  745. break;
  746. case 'M': //Max damage
  747. b.type = Bonus::CREATURE_DAMAGE;
  748. b.subtype = 2;
  749. break;
  750. case 'm': //Min damage
  751. b.type = Bonus::CREATURE_DAMAGE;
  752. b.subtype = 1;
  753. break;
  754. case 'S':
  755. b.type = Bonus::STACKS_SPEED; break;
  756. case 'b':
  757. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  758. case 'C':
  759. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  760. case 'e':
  761. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  762. case 'g':
  763. b.type = Bonus::SPELL_DAMAGE_REDUCTION; break;
  764. case 'R':
  765. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  766. case 'f': //on-off skill
  767. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  768. switch (mod[0])
  769. {
  770. case 'A':
  771. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  772. case 'b':
  773. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  774. case 'B':
  775. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  776. case 'c':
  777. b.type = Bonus::JOUSTING; break;
  778. case 'D':
  779. b.type = Bonus::ADDITIONAL_ATTACK; break;
  780. case 'f':
  781. b.type = Bonus::FEARLESS; break;
  782. case 'F':
  783. b.type = Bonus::FLYING; break;
  784. case 'm':
  785. b.type = Bonus::SELF_MORALE; break;
  786. case 'M':
  787. b.type = Bonus::NO_MORALE; break;
  788. case 'p': //Mind spells
  789. case 'P':
  790. {
  791. loadMindImmunity(b, bl, src, it);
  792. return;
  793. }
  794. return;
  795. case 'r': //TODO: Rebirth on/off? makes sense?
  796. break;
  797. case 'R':
  798. b.type = Bonus::BLOCKS_RETALIATION; break;
  799. case 's':
  800. b.type = Bonus::FREE_SHOOTING; break;
  801. case 'u':
  802. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  803. case 'U':
  804. b.type = Bonus::UNDEAD; break;
  805. default:
  806. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  807. break;
  808. }
  809. break;
  810. case 'w': //specific spell immunities, enabled/disabled
  811. enable = true;
  812. switch (mod[0])
  813. {
  814. case 'B': //Blind
  815. b.type = Bonus::SPELL_IMMUNITY;
  816. b.subtype = 74;
  817. break;
  818. case 'H': //Hypnotize
  819. b.type = Bonus::SPELL_IMMUNITY;
  820. b.subtype = 60;
  821. break;
  822. case 'I': //Implosion
  823. b.type = Bonus::SPELL_IMMUNITY;
  824. b.subtype = 18;
  825. break;
  826. case 'K': //Berserk
  827. b.type = Bonus::SPELL_IMMUNITY;
  828. b.subtype = 59;
  829. break;
  830. case 'M': //Meteor Shower
  831. b.type = Bonus::SPELL_IMMUNITY;
  832. b.subtype = 23;
  833. break;
  834. case 'R': //Armageddon
  835. b.type = Bonus::SPELL_IMMUNITY;
  836. b.subtype = 26;
  837. break;
  838. case 'S': //Slow
  839. b.type = Bonus::SPELL_IMMUNITY;
  840. b.subtype = 54;
  841. break;
  842. case '6':
  843. case '7':
  844. case '8':
  845. case '9':
  846. b.type = Bonus::LEVEL_SPELL_IMMUNITY; //TODO - value can't be read afterwards
  847. b.val = std::atoi(mod.c_str()) - 5;
  848. break;
  849. case ':':
  850. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  851. b.val = SPELL_LEVELS; //in case someone adds higher level spells?
  852. break;
  853. default:
  854. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  855. }
  856. break;
  857. case 'i':
  858. enable = true;
  859. b.type = Bonus::NO_DISTANCE_PENALTY;
  860. break;
  861. case 'o':
  862. enable = true;
  863. b.type = Bonus::NO_OBSTACLES_PENALTY;
  864. break;
  865. case 'a':
  866. //case 'c': //some special abilities are threated as spells, will cause bugs
  867. b.type = Bonus::SPELL_AFTER_ATTACK;
  868. b.subtype = stringToNumber(mod);
  869. break;
  870. case 'h':
  871. b.type= Bonus::HATE;
  872. b.subtype = stringToNumber(mod);
  873. break;
  874. default:
  875. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  876. break;
  877. }
  878. switch (mod[0])
  879. {
  880. case '+':
  881. case '=': //should we allow percent values to stack or pick highest?
  882. b.valType = Bonus::ADDITIVE_VALUE;
  883. break;
  884. }
  885. //limiters, range
  886. si32 lastVal, curVal, lastLev = 0;
  887. if (enable) //0 and 2 means non-active, 1 - active
  888. {
  889. b.val = 0; //on-off ability, no value specified
  890. loadToIt (curVal, src, it, 4); // 0 level is never active
  891. for (int i = 1; i < 11; ++i)
  892. {
  893. loadToIt (curVal, src, it, 4);
  894. if (curVal == 1)
  895. {
  896. b.val = curVal;
  897. b.limiter.reset (new RankRangeLimiter(i));
  898. bl.push_back(new Bonus(b));
  899. break; //never turned off it seems
  900. }
  901. }
  902. }
  903. else
  904. {
  905. loadToIt (lastVal, src, it, 4); //basic value, not particularly useful but existent
  906. for (int i = 1; i < 11; ++i)
  907. {
  908. loadToIt (curVal, src, it, 4);
  909. if (curVal > lastVal) //threshold, add new bonus
  910. {
  911. b.val = curVal - lastVal;
  912. lastVal = curVal;
  913. b.limiter.reset (new RankRangeLimiter(i));
  914. bl.push_back(new Bonus(b));
  915. lastLev = i; //start new range from here, i = previous rank
  916. }
  917. else if (curVal < lastVal)
  918. {
  919. b.val = lastVal;
  920. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  921. }
  922. }
  923. }
  924. }
  925. void CCreatureHandler::loadMindImmunity(Bonus & b, BonusList & bl, std::string & src, int & it)
  926. {
  927. CCreature * cre = creatures[b.id]; //odd workaround
  928. b.type = Bonus::SPELL_IMMUNITY;
  929. b.val = Bonus::BASE_NUMBER;
  930. std::vector<si32> values;
  931. values.resize(10);
  932. si32 val;
  933. loadToIt (val, src, it, 4); //basic value
  934. for (int i = 0; i < 10; ++i)
  935. {
  936. loadToIt (values[i], src, it, 4);
  937. if (values[i] = 2)
  938. values[i] = 0;
  939. }
  940. std::vector<int> mindSpells = getMindSpells();
  941. for (int g=0; g < mindSpells.size(); ++g)
  942. {
  943. b.subtype = mindSpells[g];
  944. cre->bonuses.push_back(new Bonus(b));
  945. }
  946. }
  947. int CCreatureHandler::stringToNumber(std::string & s)
  948. {
  949. boost::algorithm::replace_first(s,"#",""); //drop hash character
  950. return std::atoi(s.c_str());
  951. }
  952. CCreatureHandler::~CCreatureHandler()
  953. {
  954. }
  955. static int retreiveRandNum(const boost::function<int()> &randGen)
  956. {
  957. if(randGen)
  958. return randGen();
  959. else
  960. return rand();
  961. }
  962. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  963. {
  964. return v[retreiveRandNum(randGen) % v.size()];
  965. }
  966. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  967. {
  968. int r = 0;
  969. if(tier == -1) //pick any allowed creature
  970. {
  971. do
  972. {
  973. pickRandomElementOf(creatures, randGen);
  974. //r = retreiveRandNum(randGen) % CREATURES_COUNT;
  975. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  976. }
  977. else
  978. {
  979. assert(iswith(tier, 1, 7));
  980. std::vector<int> allowed;
  981. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].children)
  982. {
  983. assert(b->nodeType == CBonusSystemNode::CREATURE);
  984. int creid = static_cast<const CCreature*>(b)->idNumber;
  985. if(!vstd::contains(notUsedMonsters, creid))
  986. allowed.push_back(creid);
  987. }
  988. if(!allowed.size())
  989. {
  990. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  991. return 0;
  992. }
  993. return pickRandomElementOf(allowed, randGen);
  994. }
  995. return r;
  996. }
  997. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  998. {
  999. assert(iswith(tier, 1, 7));
  1000. creaturesOfLevel[tier].addNewBonus(b);
  1001. }
  1002. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  1003. {
  1004. allCreatures.addNewBonus(b);
  1005. }