CCreatureHandler.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. using namespace boost::assign;
  17. extern CLodHandler * bitmaph;
  18. /*
  19. * CCreatureHandler.cpp, part of VCMI engine
  20. *
  21. * Authors: listed in file AUTHORS in main folder
  22. *
  23. * License: GNU General Public License v2.0 or later
  24. * Full text of license available in license.txt file, in main folder
  25. *
  26. */
  27. static std::vector<int> getMindSpells()
  28. {
  29. std::vector<int> ret;
  30. ret.push_back(50); //sorrow
  31. ret.push_back(59); //berserk
  32. ret.push_back(60); //hypnotize
  33. ret.push_back(61); //forgetfulness
  34. ret.push_back(62); //blind
  35. return ret;
  36. }
  37. CCreatureHandler::CCreatureHandler()
  38. {
  39. VLC->creh = this;
  40. // Set the faction alignments to the defaults:
  41. // Good: Castle, Rampart, Tower // Evil: Inferno, Necropolis, Dungeon
  42. // Neutral: Stronghold, Fortess, Conflux
  43. factionAlignments += 1, 1, 1, -1, -1, -1, 0, 0, 0;
  44. doubledCreatures += 4, 14, 20, 28, 42, 44, 60, 70, 72, 85, 86, 100, 104; //according to Strategija
  45. }
  46. int CCreature::getQuantityID(const int & quantity)
  47. {
  48. if (quantity<5)
  49. return 0;
  50. if (quantity<10)
  51. return 1;
  52. if (quantity<20)
  53. return 2;
  54. if (quantity<50)
  55. return 3;
  56. if (quantity<100)
  57. return 4;
  58. if (quantity<250)
  59. return 5;
  60. if (quantity<500)
  61. return 5;
  62. if (quantity<1000)
  63. return 6;
  64. if (quantity<4000)
  65. return 7;
  66. return 8;
  67. }
  68. bool CCreature::isDoubleWide() const
  69. {
  70. return doubleWide;
  71. }
  72. bool CCreature::isFlying() const
  73. {
  74. return hasBonusOfType(Bonus::FLYING);
  75. }
  76. bool CCreature::isShooting() const
  77. {
  78. return hasBonusOfType(Bonus::SHOOTER);
  79. }
  80. bool CCreature::isUndead() const
  81. {
  82. return hasBonusOfType(Bonus::UNDEAD);
  83. }
  84. /**
  85. * Determines if the creature is of a good alignment.
  86. * @return true if the creture is good, false otherwise.
  87. */
  88. bool CCreature::isGood () const
  89. {
  90. return VLC->creh->isGood(faction);
  91. }
  92. /**
  93. * Determines if the creature is of an evil alignment.
  94. * @return true if the creature is evil, false otherwise.
  95. */
  96. bool CCreature::isEvil () const
  97. {
  98. return VLC->creh->isEvil(faction);
  99. }
  100. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  101. {
  102. int ret = 2147483645;
  103. int resAmnt = std::min(res.size(),cost.size());
  104. for(int i=0;i<resAmnt;i++)
  105. if(cost[i])
  106. ret = std::min(ret,(int)(res[i]/cost[i]));
  107. return ret;
  108. }
  109. CCreature::CCreature()
  110. {
  111. doubleWide = false;
  112. nodeType = CBonusSystemNode::CREATURE;
  113. }
  114. void CCreature::addBonus(int val, int type, int subtype /*= -1*/)
  115. {
  116. Bonus *added = new Bonus(Bonus::PERMANENT, type, Bonus::CREATURE_ABILITY, val, idNumber, subtype, Bonus::BASE_NUMBER);
  117. addNewBonus(added);
  118. }
  119. // void CCreature::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  120. // {
  121. // out.insert (VLC->creh->globalEffects);
  122. // }
  123. bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
  124. {
  125. //TODO upgrade of upgrade?
  126. return vstd::contains(upgrades, anotherCre->idNumber);
  127. }
  128. bool CCreature::valid() const
  129. {
  130. return this == VLC->creh->creatures[idNumber];
  131. }
  132. std::string CCreature::nodeName() const
  133. {
  134. return "Type of creature " + namePl;
  135. }
  136. int readNumber(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadCreatures() and loadUnitAnimInfo()
  137. {
  138. befi=i;
  139. for(; i<andame; ++i)
  140. {
  141. if(buf[i]=='\t')
  142. break;
  143. }
  144. std::string tmp = buf.substr(befi, i-befi);
  145. int ret = atoi(buf.substr(befi, i-befi).c_str());
  146. ++i;
  147. return ret;
  148. }
  149. float readFloat(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadUnitAnimInfo()
  150. {
  151. befi=i;
  152. for(; i<andame; ++i)
  153. {
  154. if(buf[i]=='\t')
  155. break;
  156. }
  157. std::string tmp = buf.substr(befi, i-befi);
  158. float ret = atof(buf.substr(befi, i-befi).c_str());
  159. ++i;
  160. return ret;
  161. }
  162. /**
  163. * Determines if a faction is good.
  164. * @param ID of the faction.
  165. * @return true if the faction is good, false otherwise.
  166. */
  167. bool CCreatureHandler::isGood (si8 faction) const
  168. {
  169. return faction != -1 && factionAlignments[faction] == 1;
  170. }
  171. /**
  172. * Determines if a faction is evil.
  173. * @param ID of the faction.
  174. * @return true if the faction is evil, false otherwise.
  175. */
  176. bool CCreatureHandler::isEvil (si8 faction) const
  177. {
  178. return faction != -1 && factionAlignments[faction] == -1;
  179. }
  180. void CCreatureHandler::loadCreatures()
  181. {
  182. 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;
  183. tlog5 << "\t\tReading config/cr_abils.txt and ZCRTRAIT.TXT" << std::endl;
  184. bool useCreAbilsFromZCRTRAIT = true;
  185. ////////////reading cr_abils.txt ///////////////////
  186. std::ifstream abils(DATA_DIR "/config/cr_abils.txt", std::ios::in | std::ios::binary); //this file is not in lod
  187. const int MAX_LINE_SIZE = 1000;
  188. char abilLine[MAX_LINE_SIZE+1];
  189. for(int i=0; i<5; ++i) //removing 5 comment lines
  190. {
  191. abils.getline(abilLine, MAX_LINE_SIZE);
  192. }
  193. //reading first line (determining if we should use creature abilities from ZCRTRAIT.TXT)
  194. abils.getline(abilLine, MAX_LINE_SIZE);
  195. useCreAbilsFromZCRTRAIT = atoi(abilLine);
  196. ////////////reading ZCRTRAIT.TXT ///////////////////
  197. std::string buf = bitmaph->getTextFile("ZCRTRAIT.TXT");
  198. int andame = buf.size();
  199. int i=0; //buf iterator
  200. int hmcr=0;
  201. for(; i<andame; ++i)
  202. {
  203. if(buf[i]=='\r')
  204. ++hmcr;
  205. if(hmcr==2)
  206. break;
  207. }
  208. i+=2;
  209. while(i<buf.size())
  210. {
  211. CCreature &ncre = *new CCreature;
  212. ncre.idNumber = creatures.size();
  213. ncre.cost.resize(RESOURCE_QUANTITY);
  214. ncre.level=0;
  215. int befi=i;
  216. for(; i<andame; ++i)
  217. {
  218. if(buf[i]=='\t')
  219. break;
  220. }
  221. ncre.nameSing = buf.substr(befi, i-befi);
  222. ++i;
  223. befi=i;
  224. for(; i<andame; ++i)
  225. {
  226. if(buf[i]=='\t')
  227. break;
  228. }
  229. ncre.namePl = buf.substr(befi, i-befi);
  230. ++i;
  231. for(int v=0; v<7; ++v)
  232. {
  233. ncre.cost[v] = readNumber(befi, i, andame, buf);
  234. }
  235. ncre.fightValue = readNumber(befi, i, andame, buf);
  236. ncre.AIValue = readNumber(befi, i, andame, buf);
  237. ncre.growth = readNumber(befi, i, andame, buf);
  238. ncre.hordeGrowth = readNumber(befi, i, andame, buf);
  239. ncre.hitPoints = readNumber(befi, i, andame, buf);
  240. ncre.addBonus(ncre.hitPoints, Bonus::STACK_HEALTH);
  241. ncre.speed = readNumber(befi, i, andame, buf);
  242. ncre.addBonus(ncre.speed, Bonus::STACKS_SPEED);
  243. ncre.attack = readNumber(befi, i, andame, buf);
  244. ncre.addBonus(ncre.attack, Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  245. ncre.defence = readNumber(befi, i, andame, buf);
  246. ncre.addBonus(ncre.defence, Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  247. ncre.damageMin = readNumber(befi, i, andame, buf); //not used anymore?
  248. ncre.addBonus(ncre.damageMin, Bonus::CREATURE_DAMAGE, 1);
  249. ncre.damageMax = readNumber(befi, i, andame, buf);
  250. ncre.addBonus(ncre.damageMax, Bonus::CREATURE_DAMAGE, 2);
  251. ncre.shots = readNumber(befi, i, andame, buf);
  252. ncre.spells = readNumber(befi, i, andame, buf);
  253. ncre.ammMin = readNumber(befi, i, andame, buf);
  254. ncre.ammMax = readNumber(befi, i, andame, buf);
  255. befi=i;
  256. for(; i<andame; ++i)
  257. {
  258. if(buf[i]=='\t')
  259. break;
  260. }
  261. ncre.abilityText = buf.substr(befi, i-befi);
  262. ++i;
  263. befi=i;
  264. for(; i<andame; ++i)
  265. {
  266. if(buf[i]=='\r')
  267. break;
  268. }
  269. ncre.abilityRefs = buf.substr(befi, i-befi);
  270. i+=2;
  271. if(useCreAbilsFromZCRTRAIT)
  272. { //adding abilities from ZCRTRAIT.TXT
  273. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  274. ncre.doubleWide = true;
  275. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  276. ncre.addBonus(0, Bonus::FLYING);
  277. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  278. ncre.addBonus(0, Bonus::SHOOTER);
  279. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  280. ncre.addBonus(0, Bonus::SIEGE_WEAPON);
  281. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  282. ncre.addBonus(1, Bonus::ADDITIONAL_ATTACK);
  283. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  284. ncre.addBonus(0, Bonus::BLOCKS_RETALIATION);
  285. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  286. ncre.addBonus(0, Bonus::UNDEAD);
  287. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  288. ncre.addBonus(0, Bonus::NO_MELEE_PENALTY);
  289. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  290. ncre.addBonus(0, Bonus::JOUSTING);
  291. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  292. {
  293. ncre.addBonus(+1, Bonus::MORALE);;
  294. ncre.bonuses.back()->effectRange = Bonus::ONLY_ALLIED_ARMY;
  295. }
  296. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  297. {
  298. ncre.addBonus(-1, Bonus::MORALE);;
  299. ncre.bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  300. }
  301. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  302. ncre.addBonus(0, Bonus::KING1);
  303. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  304. ncre.addBonus(0, Bonus::KING2);
  305. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  306. ncre.addBonus(0, Bonus::KING3);
  307. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  308. ncre.addBonus(0, Bonus::NO_WALL_PENALTY);
  309. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  310. ncre.addBonus(0, Bonus::CATAPULT);
  311. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  312. ncre.addBonus(0, Bonus::ATTACKS_ALL_ADJACENT);
  313. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  314. {
  315. std::vector<int> mindSpells = getMindSpells();
  316. for(int g=0; g<mindSpells.size(); ++g)
  317. ncre.addBonus(0, Bonus::SPELL_IMMUNITY, mindSpells[g]); //giants are immune to mind spells
  318. }
  319. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  320. ncre.addBonus(0, Bonus::FIRE_IMMUNITY);
  321. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  322. ncre.addBonus(0, Bonus::TWO_HEX_ATTACK_BREATH);;
  323. }
  324. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  325. {
  326. ncre.idNumber = creatures.size();
  327. creatures.push_back(&ncre);
  328. }
  329. }
  330. ////second part of reading cr_abils.txt////
  331. bool contReading = true;
  332. while(contReading) //main reading loop
  333. {
  334. abils.getline(abilLine, MAX_LINE_SIZE);
  335. std::istringstream reader(abilLine);
  336. char command;
  337. reader >> command;
  338. switch(command)
  339. {
  340. case '+': //add new ability
  341. {
  342. int creatureID;
  343. Bonus *nsf = new Bonus();
  344. si32 buf;
  345. std::string type;
  346. reader >> creatureID;
  347. reader >> type;
  348. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  349. CCreature *cre = creatures[creatureID];
  350. if (it == bonusNameMap.end())
  351. {
  352. if(type == "DOUBLE_WIDE")
  353. cre->doubleWide = true;
  354. else if(type == "ENEMY_MORALE_DECREASING")
  355. {
  356. cre->addBonus(-1, Bonus::MORALE);
  357. cre->bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  358. }
  359. else if(type == "ENEMY_LUCK_DECREASING")
  360. {
  361. cre->addBonus(-1, Bonus::LUCK);
  362. cre->bonuses.back()->effectRange = Bonus::ONLY_ENEMY_ARMY;
  363. }
  364. else
  365. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  366. break;
  367. }
  368. nsf->type = it->second;
  369. reader >> buf; nsf->val = buf;
  370. reader >> buf; nsf->subtype = buf;
  371. reader >> buf; nsf->additionalInfo = buf;
  372. nsf->source = Bonus::CREATURE_ABILITY;
  373. nsf->id = cre->idNumber;
  374. nsf->duration = Bonus::ONE_BATTLE;
  375. nsf->turnsRemain = 0;
  376. cre->addNewBonus(nsf);
  377. break;
  378. }
  379. case '-': //remove ability
  380. {
  381. int creatureID;
  382. std::string type;
  383. reader >> creatureID;
  384. reader >> type;
  385. std::map<std::string, int>::const_iterator it = bonusNameMap.find(type);
  386. if (it == bonusNameMap.end())
  387. {
  388. if(type == "DOUBLE_WIDE")
  389. creatures[creatureID]->doubleWide = false;
  390. else
  391. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  392. break;
  393. }
  394. int typeNo = it->second;
  395. Bonus::BonusType ecf = static_cast<Bonus::BonusType>(typeNo);
  396. Bonus *b = creatures[creatureID]->getBonus(Selector::type(ecf));
  397. creatures[creatureID]->removeBonus(b);
  398. break;
  399. }
  400. case '0': //end reading
  401. {
  402. contReading = false;
  403. break;
  404. }
  405. default: //invalid command
  406. {
  407. tlog1 << "Parse error in file config/cr_abils.txt" << std::endl;
  408. break;
  409. }
  410. }
  411. }
  412. abils.close();
  413. tlog5 << "\t\tReading config/crerefnam.txt" << std::endl;
  414. //loading reference names
  415. std::ifstream ifs(DATA_DIR "/config/crerefnam.txt");
  416. int tempi;
  417. std::string temps;
  418. for (;;)
  419. {
  420. ifs >> tempi >> temps;
  421. if (tempi>=creatures.size())
  422. break;
  423. boost::assign::insert(nameToID)(temps,tempi);
  424. creatures[tempi]->nameRef=temps;
  425. }
  426. ifs.close();
  427. ifs.clear();
  428. for(i = 1; i <= CRE_LEVELS; i++)
  429. levelCreatures[i];
  430. tlog5 << "\t\tReading config/monsters.txt" << std::endl;
  431. ifs.open(DATA_DIR "/config/monsters.txt");
  432. {
  433. while(!ifs.eof())
  434. {
  435. int id, lvl;
  436. ifs >> id >> lvl;
  437. if(lvl>0)
  438. {
  439. creatures[id]->level = lvl;
  440. levelCreatures[lvl].push_back(creatures[id]);
  441. }
  442. }
  443. }
  444. ifs.close();
  445. ifs.clear();
  446. tlog5 << "\t\tReading config/cr_factions.txt" << std::endl;
  447. ifs.open(DATA_DIR "/config/cr_factions.txt");
  448. while(!ifs.eof())
  449. {
  450. int id, fact;
  451. ifs >> id >> fact;
  452. creatures[id]->faction = fact;
  453. }
  454. ifs.close();
  455. ifs.clear();
  456. tlog5 << "\t\tReading config/cr_upgrade_list.txt" << std::endl;
  457. ifs.open(DATA_DIR "/config/cr_upgrade_list.txt");
  458. while(!ifs.eof())
  459. {
  460. int id, up;
  461. ifs >> id >> up;
  462. creatures[id]->upgrades.insert(up);
  463. }
  464. ifs.close();
  465. ifs.clear();
  466. //loading unit animation def names
  467. tlog5 << "\t\tReading config/CREDEFS.TXT" << std::endl;
  468. std::ifstream inp(DATA_DIR "/config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
  469. inp.seekg(0,std::ios::end); // na koniec
  470. int andame2 = inp.tellg(); // read length
  471. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  472. char * bufor = new char[andame2+1]; // allocate memory
  473. inp.read((char*)bufor, andame2); // read map file to buffer
  474. inp.close();
  475. bufor[andame2] = 0;
  476. buf = std::string(bufor);
  477. delete [] bufor;
  478. i = 0; //buf iterator
  479. hmcr = 0;
  480. for(; i<andame2; ++i) //omitting rubbish
  481. {
  482. if(buf[i]=='\r')
  483. break;
  484. }
  485. i+=2;
  486. tlog5 << "We have "<<creatures.size() << " creatures\n";
  487. for(int s=0; s<creatures.size(); ++s)
  488. {
  489. //tlog5 <<"\t\t\t" << s <<". Reading defname. \n";
  490. int befi=i;
  491. std::string rub;
  492. for(; i<andame2; ++i)
  493. {
  494. if(buf[i]==' ')
  495. break;
  496. }
  497. rub = buf.substr(befi, i-befi);
  498. ++i;
  499. befi=i;
  500. for(; i<andame2; ++i)
  501. {
  502. if(buf[i]=='\r')
  503. break;
  504. }
  505. std::string defName = buf.substr(befi, i-befi);
  506. creatures[s]->animDefName = defName;
  507. }
  508. tlog5 << "\t\tReading CRANIM.TXT.txt" << std::endl;
  509. loadAnimationInfo();
  510. //loading id to projectile mapping
  511. tlog5 << "\t\tReading config/cr_shots.txt" << std::endl;
  512. std::ifstream inp2(DATA_DIR "/config/cr_shots.txt", std::ios::in | std::ios::binary); //this file is not in lod
  513. char dump [200];
  514. inp2.getline(dump, 200);
  515. while(true)
  516. {
  517. int id;
  518. std::string name;
  519. bool spin;
  520. inp2>>id;
  521. if(id == -1)
  522. break;
  523. inp2>>name;
  524. idToProjectile[id] = name;
  525. inp2>>spin;
  526. idToProjectileSpin[id] = spin;
  527. }
  528. inp2.close();
  529. //reading factionToTurretCreature
  530. tlog5 << "\t\tReading config/cr_to_turret.txt" << std::endl;
  531. std::ifstream inp3(DATA_DIR "/config/cr_to_turret.txt", std::ios::in | std::ios::binary); //this file is not in lod
  532. std::string dump2;
  533. inp3 >> dump2 >> dump2;
  534. for(int g=0; g<F_NUMBER; ++g)
  535. {
  536. inp3 >> factionToTurretCreature[g];
  537. }
  538. inp3.close();
  539. //reading creature ability names
  540. ifs.open(DATA_DIR "/config/bonusnames.txt");
  541. {
  542. std::string buf2, buf3, line;
  543. int i;
  544. std::map<std::string,int>::const_iterator it;
  545. getline(ifs, line); //skip 1st line
  546. while(!ifs.eof())
  547. {
  548. getline(ifs, buf, '\t');
  549. getline(ifs, buf2, '\t');
  550. getline(ifs, buf3);
  551. it = bonusNameMap.find(buf);
  552. if (it != bonusNameMap.end())
  553. stackBonuses[it->second] = std::pair<std::string, std::string>(buf2,buf3);
  554. else
  555. tlog2 << "Bonus " << buf << " not recognized, ingoring\n";
  556. }
  557. }
  558. ifs.close();
  559. if (STACK_EXP) //reading default stack experience bonuses
  560. {
  561. buf = bitmaph->getTextFile("CREXPBON.TXT");
  562. int it = 0;
  563. si32 creid = -1;
  564. commonBonuses.resize(8); //8 tiers
  565. stackExperience b;
  566. b.expBonuses.resize(10);
  567. b.source = Bonus::STACK_EXPERIENCE;
  568. b.additionalInfo = 0;
  569. b.enable = false; //Bonuses are always active by default
  570. loadToIt (dump2, buf, it, 3); //ignore first line
  571. loadToIt (dump2, buf, it, 4); //ignore index
  572. loadStackExp(b, buf, it);
  573. loadToIt (dump2, buf, it, 4); //crop comment
  574. for (i = 0; i < 8; ++i)
  575. {
  576. commonBonuses[i].push_back(new stackExperience(b));//health bonus common for all
  577. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  578. {
  579. loadToIt (dump2, buf, it, 4); //ignore index
  580. loadStackExp(b, buf, it);
  581. commonBonuses[i].push_back(new stackExperience(b));
  582. loadToIt (dump2, buf, it, 3); //crop comment
  583. }
  584. }
  585. do //parse everything that's left
  586. {
  587. loadToIt(creid, buf, it, 4); //get index
  588. b.id = creid; //id = this particular creature ID
  589. loadStackExp(b, buf, it);
  590. creatures[creid]->bonuses.push_back(new stackExperience(b)); //experience list is common for creatures of that type
  591. loadToIt (dump2, buf, it, 3); //crop comment
  592. } while (it < buf.size());
  593. // BOOST_FOREACH(CCreature *c, creatures)
  594. // {
  595. // if (it = c->level < 7)
  596. // std::copy(commonBonuses[it-1].begin(), commonBonuses[it-1].end(), c->bonuses.begin());
  597. // else
  598. // std::copy(commonBonuses[7].begin(), commonBonuses[7].end(), c->bonuses.begin()); //common for tiers 8+
  599. // }
  600. } //end of stack experience
  601. }
  602. void CCreatureHandler::loadAnimationInfo()
  603. {
  604. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  605. int andame = buf.size();
  606. int i=0; //buf iterator
  607. int hmcr=0;
  608. for(; i<andame; ++i)
  609. {
  610. if(buf[i]=='\r')
  611. ++hmcr;
  612. if(hmcr==2)
  613. break;
  614. }
  615. i+=2;
  616. for(int dd=0; dd<creatures.size(); ++dd)
  617. {
  618. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  619. loadUnitAnimInfo(*creatures[dd], buf, i);
  620. }
  621. return;
  622. }
  623. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  624. {
  625. int befi=i;
  626. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  627. while(unit.timeBetweenFidgets == 0.0)
  628. {
  629. for(; i<src.size(); ++i)
  630. {
  631. if(src[i]=='\r')
  632. break;
  633. }
  634. i+=2;
  635. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  636. }
  637. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  638. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  639. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  640. ///////////////////////
  641. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  642. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  643. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  644. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  645. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  646. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  647. ///////////////////////
  648. for(int jjj=0; jjj<12; ++jjj)
  649. {
  650. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  651. }
  652. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  653. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  654. for(; i<src.size(); ++i)
  655. {
  656. if(src[i]=='\r')
  657. break;
  658. }
  659. i+=2;
  660. }
  661. void CCreatureHandler::loadStackExp(stackExperience & b, std::string & src, int & it) //help function for parsing CREXPBON.txt, assuming all its details are already defined
  662. {
  663. std::string buf, mod;
  664. loadToIt(buf, src, it, 4);
  665. loadToIt(mod, src, it, 4);
  666. switch (buf[0])
  667. {
  668. case 'H':
  669. b.type = Bonus::STACK_HEALTH;
  670. break;
  671. case 'A':
  672. b.type = Bonus::PRIMARY_SKILL;
  673. b.subtype = PrimarySkill::ATTACK;
  674. break;
  675. case 'D':
  676. b.type = Bonus::PRIMARY_SKILL;
  677. b.subtype = PrimarySkill::DEFENSE;
  678. break;
  679. case 'M': //Max damage
  680. b.type = Bonus::CREATURE_DAMAGE;
  681. b.subtype = 2;
  682. break;
  683. case 'm': //Min damage
  684. b.type = Bonus::CREATURE_DAMAGE;
  685. b.subtype = 1;
  686. break;
  687. case 'S':
  688. b.type = Bonus::STACKS_SPEED; break;
  689. case 'b':
  690. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  691. case 'C':
  692. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  693. case 'e':
  694. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  695. case 'g':
  696. b.type = Bonus::SPELL_DAMAGE_REDUCTION; break;
  697. case 'f': //on-off skill
  698. b.enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  699. switch (mod[0])
  700. {
  701. case 'A':
  702. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  703. case 'b':
  704. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  705. case 'B':
  706. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  707. case 'c':
  708. b.type = Bonus::JOUSTING; break;
  709. case 'D':
  710. b.type = Bonus::ADDITIONAL_ATTACK; break;
  711. case 'f':
  712. b.type = Bonus::FEARLESS; break;
  713. case 'F':
  714. b.type = Bonus::FLYING; break;
  715. case 'm':
  716. b.type = Bonus::SELF_MORALE; break;
  717. case 'M':
  718. b.type = Bonus::NO_MORALE; break;
  719. case 'p': //Mind spells
  720. case 'P':
  721. {
  722. loadMindImmunity(b, src, it);
  723. return;
  724. }
  725. return;
  726. case 'r': //TODO: Rebirth on/off? makes sense?
  727. break;
  728. case 'R':
  729. b.type = Bonus::BLOCKS_RETALIATION; break;
  730. case 's':
  731. b.type = Bonus::FREE_SHOOTING; break;
  732. case 'u':
  733. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  734. break;
  735. case 'U':
  736. b.type = Bonus::UNDEAD; break;
  737. default:
  738. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  739. break;
  740. }
  741. break;
  742. case 'i':
  743. b.enable = true;
  744. b.type = Bonus::NO_DISTANCE_PENALTY;
  745. break;
  746. case 'o':
  747. b.enable = true;
  748. b.type = Bonus::NO_OBSTACLES_PENALTY;
  749. break;
  750. default:
  751. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  752. break;
  753. }
  754. switch (mod[0])
  755. {
  756. case '+':
  757. case '=': //should we allow percent values to stack or pick highest?
  758. b.valType = Bonus::BASE_NUMBER;
  759. break;
  760. }
  761. loadToIt (b.val, src, it, 4); //basic value, not particularly useful but existent
  762. for (int i = 0; i < 10; ++i)
  763. {
  764. loadToIt (b.expBonuses[i], src, it, 4); //vector must have length 10
  765. }
  766. if (b.enable) //switch 2 to 0
  767. {
  768. if (b.val == 2)
  769. b.val = 0;
  770. for (int i = 0; i < 10; ++i)
  771. {
  772. if (b.expBonuses[i] == 2)
  773. b.expBonuses[i] = 0;
  774. else break; //higher levels are rarely disabled?
  775. }
  776. }
  777. }
  778. void CCreatureHandler::loadMindImmunity(stackExperience & b, std::string & src, int & it)
  779. {
  780. CCreature * cre = creatures[b.id]; //odd workaround
  781. b.type = Bonus::SPELL_IMMUNITY;
  782. b.val = Bonus::BASE_NUMBER;
  783. std::vector<si32> values;
  784. values.resize(10);
  785. si32 val;
  786. loadToIt (val, src, it, 4); //basic value
  787. for (int i = 0; i < 10; ++i)
  788. {
  789. loadToIt (values[i], src, it, 4);
  790. if (values[i] = 2)
  791. values[i] = 0;
  792. }
  793. std::vector<int> mindSpells = getMindSpells();
  794. for (int g=0; g < mindSpells.size(); ++g)
  795. {
  796. b.subtype = mindSpells[g];
  797. cre->bonuses.push_back(new stackExperience(b));
  798. }
  799. }
  800. CCreatureHandler::~CCreatureHandler()
  801. {
  802. }
  803. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen) const
  804. {
  805. int r = 0;
  806. do
  807. {
  808. if(randGen)
  809. r = randGen();
  810. else
  811. r = rand();
  812. r %= 197;
  813. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  814. return r;
  815. }