CCreatureHandler.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. if(isbetw(lvl, 0, ARRAY_COUNT(creaturesOfLevel)))
  444. c->attachTo(&creaturesOfLevel[lvl]);
  445. else
  446. c->attachTo(&creaturesOfLevel[0]);
  447. }
  448. }
  449. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  450. b.attachTo(&allCreatures);
  451. ifs.close();
  452. ifs.clear();
  453. tlog5 << "\t\tReading config/cr_factions.txt" << std::endl;
  454. ifs.open(DATA_DIR "/config/cr_factions.txt");
  455. while(!ifs.eof())
  456. {
  457. int id, fact;
  458. ifs >> id >> fact;
  459. creatures[id]->faction = fact;
  460. }
  461. ifs.close();
  462. ifs.clear();
  463. tlog5 << "\t\tReading config/cr_upgrade_list.txt" << std::endl;
  464. ifs.open(DATA_DIR "/config/cr_upgrade_list.txt");
  465. while(!ifs.eof())
  466. {
  467. int id, up;
  468. ifs >> id >> up;
  469. creatures[id]->upgrades.insert(up);
  470. }
  471. ifs.close();
  472. ifs.clear();
  473. //loading unit animation def names
  474. tlog5 << "\t\tReading config/CREDEFS.TXT" << std::endl;
  475. std::ifstream inp(DATA_DIR "/config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
  476. inp.seekg(0,std::ios::end); // na koniec
  477. int andame2 = inp.tellg(); // read length
  478. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  479. char * bufor = new char[andame2+1]; // allocate memory
  480. inp.read((char*)bufor, andame2); // read map file to buffer
  481. inp.close();
  482. bufor[andame2] = 0;
  483. buf = std::string(bufor);
  484. delete [] bufor;
  485. i = 0; //buf iterator
  486. hmcr = 0;
  487. for(; i<andame2; ++i) //omitting rubbish
  488. {
  489. if(buf[i]=='\r')
  490. break;
  491. }
  492. i+=2;
  493. tlog5 << "We have "<<creatures.size() << " creatures\n";
  494. for(int s=0; s<creatures.size(); ++s)
  495. {
  496. //tlog5 <<"\t\t\t" << s <<". Reading defname. \n";
  497. int befi=i;
  498. std::string rub;
  499. for(; i<andame2; ++i)
  500. {
  501. if(buf[i]==' ')
  502. break;
  503. }
  504. rub = buf.substr(befi, i-befi);
  505. ++i;
  506. befi=i;
  507. for(; i<andame2; ++i)
  508. {
  509. if(buf[i]=='\r')
  510. break;
  511. }
  512. std::string defName = buf.substr(befi, i-befi);
  513. creatures[s]->animDefName = defName;
  514. }
  515. tlog5 << "\t\tReading CRANIM.TXT.txt" << std::endl;
  516. loadAnimationInfo();
  517. //loading id to projectile mapping
  518. tlog5 << "\t\tReading config/cr_shots.txt" << std::endl;
  519. std::ifstream inp2(DATA_DIR "/config/cr_shots.txt", std::ios::in | std::ios::binary); //this file is not in lod
  520. char dump [200];
  521. inp2.getline(dump, 200);
  522. while(true)
  523. {
  524. int id;
  525. std::string name;
  526. bool spin;
  527. inp2>>id;
  528. if(id == -1)
  529. break;
  530. inp2>>name;
  531. idToProjectile[id] = name;
  532. inp2>>spin;
  533. idToProjectileSpin[id] = spin;
  534. }
  535. inp2.close();
  536. //reading factionToTurretCreature
  537. tlog5 << "\t\tReading config/cr_to_turret.txt" << std::endl;
  538. std::ifstream inp3(DATA_DIR "/config/cr_to_turret.txt", std::ios::in | std::ios::binary); //this file is not in lod
  539. std::string dump2;
  540. inp3 >> dump2 >> dump2;
  541. for(int g=0; g<F_NUMBER; ++g)
  542. {
  543. inp3 >> factionToTurretCreature[g];
  544. }
  545. inp3.close();
  546. //reading creature ability names
  547. ifs.open(DATA_DIR "/config/bonusnames.txt");
  548. {
  549. std::string buf2, buf3, line;
  550. int i;
  551. std::map<std::string,int>::const_iterator it;
  552. getline(ifs, line); //skip 1st line
  553. while(!ifs.eof())
  554. {
  555. getline(ifs, buf, '\t');
  556. getline(ifs, buf2, '\t');
  557. getline(ifs, buf3);
  558. it = bonusNameMap.find(buf);
  559. if (it != bonusNameMap.end())
  560. stackBonuses[it->second] = std::pair<std::string, std::string>(buf2,buf3);
  561. else
  562. tlog2 << "Bonus " << buf << " not recognized, ingoring\n";
  563. }
  564. }
  565. ifs.close();
  566. if (STACK_EXP) //reading default stack experience bonuses
  567. {
  568. buf = bitmaph->getTextFile("CREXPBON.TXT");
  569. int it = 0;
  570. si32 creid = -1;
  571. Bonus b; //prototype with some default properties
  572. b.source = Bonus::STACK_EXPERIENCE;
  573. b.valType = Bonus::ADDITIVE_VALUE;
  574. b.additionalInfo = 0;
  575. BonusList bl;
  576. loadToIt (dump2, buf, it, 3); //ignore first line
  577. loadToIt (dump2, buf, it, 4); //ignore index
  578. loadStackExp(b, bl, buf, it);
  579. BOOST_FOREACH(Bonus * b, bl)
  580. addBonusForAllCreatures(b); //health bonus is common for all
  581. loadToIt (dump2, buf, it, 4); //crop comment
  582. for (i = 1; i < 8; ++i)
  583. {
  584. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  585. {
  586. loadToIt (dump2, buf, it, 4); //ignore index
  587. bl.clear();
  588. loadStackExp(b, bl, buf, it);
  589. BOOST_FOREACH(Bonus * b, bl)
  590. addBonusForTier(i, b);
  591. loadToIt (dump2, buf, it, 3); //crop comment
  592. }
  593. }
  594. do //parse everything that's left
  595. {
  596. loadToIt(creid, buf, it, 4); //get index
  597. b.id = creid; //id = this particular creature ID
  598. loadStackExp(b, creatures[creid]->bonuses, buf, it); //add directly to CCreature Node
  599. loadToIt (dump2, buf, it, 3); //crop comment
  600. } while (it < buf.size());
  601. //Calculate rank exp values, formula appears complicated bu no parsing needed
  602. expRanks.resize(8);
  603. int dif = 0;
  604. it = 8000; //ignore name of this variable
  605. expRanks[0].push_back(it);
  606. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  607. {
  608. expRanks[0].push_back(expRanks[0][j-1] + dif);
  609. dif += it/5;
  610. }
  611. for (int i = 1; i < 8; ++i)
  612. {
  613. dif = 0;
  614. it = 1000 * i;
  615. expRanks[i].push_back(it);
  616. for (int j = 1; j < 10; ++j)
  617. {
  618. expRanks[i].push_back(expRanks[i][j-1] + dif);
  619. dif += it/5;
  620. }
  621. }
  622. }//end of Stack Experience
  623. //experiment - add 100 to attack for creatures of tier 1
  624. // Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::OTHER, +100, 0, 0);
  625. // addBonusForTier(1, b);
  626. }
  627. void CCreatureHandler::loadAnimationInfo()
  628. {
  629. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  630. int andame = buf.size();
  631. int i=0; //buf iterator
  632. int hmcr=0;
  633. for(; i<andame; ++i)
  634. {
  635. if(buf[i]=='\r')
  636. ++hmcr;
  637. if(hmcr==2)
  638. break;
  639. }
  640. i+=2;
  641. for(int dd=0; dd<creatures.size(); ++dd)
  642. {
  643. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  644. loadUnitAnimInfo(*creatures[dd], buf, i);
  645. }
  646. return;
  647. }
  648. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  649. {
  650. int befi=i;
  651. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  652. while(unit.timeBetweenFidgets == 0.0)
  653. {
  654. for(; i<src.size(); ++i)
  655. {
  656. if(src[i]=='\r')
  657. break;
  658. }
  659. i+=2;
  660. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  661. }
  662. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  663. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  664. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  665. ///////////////////////
  666. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  667. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  668. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  669. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  670. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  671. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  672. ///////////////////////
  673. for(int jjj=0; jjj<12; ++jjj)
  674. {
  675. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  676. }
  677. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  678. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  679. for(; i<src.size(); ++i)
  680. {
  681. if(src[i]=='\r')
  682. break;
  683. }
  684. i+=2;
  685. }
  686. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it) //help function for parsing CREXPBON.txt
  687. { //TODO: handle rank limiters
  688. std::string buf, mod;
  689. bool enable = false; //some bonuses are activated with values 2 or 1
  690. loadToIt(buf, src, it, 4);
  691. loadToIt(mod, src, it, 4);
  692. switch (buf[0])
  693. {
  694. case 'H':
  695. b.type = Bonus::STACK_HEALTH;
  696. b.valType = Bonus::PERCENT_TO_BASE;
  697. break;
  698. case 'A':
  699. b.type = Bonus::PRIMARY_SKILL;
  700. b.subtype = PrimarySkill::ATTACK;
  701. break;
  702. case 'D':
  703. b.type = Bonus::PRIMARY_SKILL;
  704. b.subtype = PrimarySkill::DEFENSE;
  705. break;
  706. case 'M': //Max damage
  707. b.type = Bonus::CREATURE_DAMAGE;
  708. b.subtype = 2;
  709. break;
  710. case 'm': //Min damage
  711. b.type = Bonus::CREATURE_DAMAGE;
  712. b.subtype = 1;
  713. break;
  714. case 'S':
  715. b.type = Bonus::STACKS_SPEED; break;
  716. case 'b':
  717. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  718. case 'C':
  719. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  720. case 'e':
  721. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  722. case 'g':
  723. b.type = Bonus::SPELL_DAMAGE_REDUCTION; break;
  724. case 'R':
  725. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  726. case 'f': //on-off skill
  727. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  728. switch (mod[0])
  729. {
  730. case 'A':
  731. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  732. case 'b':
  733. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  734. case 'B':
  735. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  736. case 'c':
  737. b.type = Bonus::JOUSTING; break;
  738. case 'D':
  739. b.type = Bonus::ADDITIONAL_ATTACK; break;
  740. case 'f':
  741. b.type = Bonus::FEARLESS; break;
  742. case 'F':
  743. b.type = Bonus::FLYING; break;
  744. case 'm':
  745. b.type = Bonus::SELF_MORALE; break;
  746. case 'M':
  747. b.type = Bonus::NO_MORALE; break;
  748. case 'p': //Mind spells
  749. case 'P':
  750. {
  751. loadMindImmunity(b, bl, src, it);
  752. return;
  753. }
  754. return;
  755. case 'r': //TODO: Rebirth on/off? makes sense?
  756. break;
  757. case 'R':
  758. b.type = Bonus::BLOCKS_RETALIATION; break;
  759. case 's':
  760. b.type = Bonus::FREE_SHOOTING; break;
  761. case 'u':
  762. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  763. break;
  764. case 'U':
  765. b.type = Bonus::UNDEAD; break;
  766. default:
  767. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  768. break;
  769. }
  770. break;
  771. case 'w': //specific spell immunities, enabled/disabled
  772. enable = true;
  773. switch (mod[0])
  774. {
  775. case 'B': //Blind
  776. b.type = Bonus::SPELL_IMMUNITY;
  777. b.subtype = 74;
  778. break;
  779. case 'H': //Hypnotize
  780. b.type = Bonus::SPELL_IMMUNITY;
  781. b.subtype = 60;
  782. break;
  783. case 'I': //Implosion
  784. b.type = Bonus::SPELL_IMMUNITY;
  785. b.subtype = 18;
  786. break;
  787. case 'K': //Berserk
  788. b.type = Bonus::SPELL_IMMUNITY;
  789. b.subtype = 59;
  790. break;
  791. case 'M': //Meteor Shower
  792. b.type = Bonus::SPELL_IMMUNITY;
  793. b.subtype = 23;
  794. break;
  795. case 'R': //Armageddon
  796. b.type = Bonus::SPELL_IMMUNITY;
  797. b.subtype = 26;
  798. break;
  799. case 'S': //Slow
  800. b.type = Bonus::SPELL_IMMUNITY;
  801. b.subtype = 54;
  802. break;
  803. case '6': //problem - in VCMI value represents level, here it represents on/off
  804. case '7':
  805. case '8':
  806. case '9':
  807. b.type = Bonus::LEVEL_SPELL_IMMUNITY; //TODO - value can't be read afterwards
  808. b.val = std::atoi(mod.c_str()) - 5;
  809. break;
  810. case ':':
  811. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  812. b.val = SPELL_LEVELS; //in case someone adds higher level spells?
  813. break;
  814. default:
  815. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  816. }
  817. break;
  818. case 'i':
  819. enable = true;
  820. b.type = Bonus::NO_DISTANCE_PENALTY;
  821. break;
  822. case 'o':
  823. enable = true;
  824. b.type = Bonus::NO_OBSTACLES_PENALTY;
  825. break;
  826. case 'a':
  827. //case 'c': //some special abilities are threated as spells, will cause bugs
  828. b.type = Bonus::SPELL_AFTER_ATTACK;
  829. b.subtype = stringToNumber(mod);
  830. break;
  831. case 'h':
  832. b.type= Bonus::HATE;
  833. b.subtype = stringToNumber(mod);
  834. break;
  835. default:
  836. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  837. break;
  838. }
  839. switch (mod[0])
  840. {
  841. case '+':
  842. case '=': //should we allow percent values to stack or pick highest?
  843. b.valType = Bonus::ADDITIVE_VALUE;
  844. break;
  845. }
  846. //limiters, range
  847. si32 lastVal, curVal, lastLev = 0;
  848. if (enable) //0 and 2 means non-active, 1 - active
  849. {
  850. b.val = 0; //on-off ability, no value specified
  851. loadToIt (curVal, src, it, 4); // 0 level is never active
  852. for (int i = 1; i < 11; ++i)
  853. {
  854. loadToIt (curVal, src, it, 4);
  855. if (curVal == 1)
  856. {
  857. b.limiter.reset (new ExpRankLimiter(i));
  858. bl.push_back(new Bonus(b));
  859. break; //never turned off it seems
  860. }
  861. }
  862. }
  863. else
  864. {
  865. loadToIt (lastVal, src, it, 4); //basic value, not particularly useful but existent
  866. for (int i = 1; i < 11; ++i)
  867. {
  868. loadToIt (curVal, src, it, 4);
  869. if (curVal > lastVal) //threshold, add last bonus
  870. {
  871. b.val = lastVal;
  872. b.limiter.reset (new ExpRankLimiter(i));
  873. bl.push_back(new Bonus(b));
  874. lastLev = i; //start new range from here, i = previous rank
  875. }
  876. else if (curVal < lastVal)
  877. {
  878. b.val = lastVal;
  879. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  880. }
  881. }
  882. }
  883. }
  884. void CCreatureHandler::loadMindImmunity(Bonus & b, BonusList & bl, std::string & src, int & it)
  885. {
  886. CCreature * cre = creatures[b.id]; //odd workaround
  887. b.type = Bonus::SPELL_IMMUNITY;
  888. b.val = Bonus::BASE_NUMBER;
  889. std::vector<si32> values;
  890. values.resize(10);
  891. si32 val;
  892. loadToIt (val, src, it, 4); //basic value
  893. for (int i = 0; i < 10; ++i)
  894. {
  895. loadToIt (values[i], src, it, 4);
  896. if (values[i] = 2)
  897. values[i] = 0;
  898. }
  899. std::vector<int> mindSpells = getMindSpells();
  900. for (int g=0; g < mindSpells.size(); ++g)
  901. {
  902. b.subtype = mindSpells[g];
  903. cre->bonuses.push_back(new Bonus(b));
  904. }
  905. }
  906. int CCreatureHandler::stringToNumber(std::string & s)
  907. {
  908. boost::algorithm::replace_first(s,"#",""); //drop hash character
  909. return std::atoi(s.c_str());
  910. }
  911. CCreatureHandler::~CCreatureHandler()
  912. {
  913. }
  914. static int retreiveRandNum(const boost::function<int()> &randGen)
  915. {
  916. if(randGen)
  917. return randGen();
  918. else
  919. return rand();
  920. }
  921. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  922. {
  923. return v[retreiveRandNum(randGen) % v.size()];
  924. }
  925. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  926. {
  927. int r = 0;
  928. if(tier == -1) //pick any allowed creature
  929. {
  930. do
  931. {
  932. pickRandomElementOf(creatures, randGen);
  933. //r = retreiveRandNum(randGen) % CREATURES_COUNT;
  934. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  935. }
  936. else
  937. {
  938. assert(iswith(tier, 1, 7));
  939. std::vector<int> allowed;
  940. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].children)
  941. {
  942. assert(b->nodeType == CBonusSystemNode::CREATURE);
  943. int creid = static_cast<const CCreature*>(b)->idNumber;
  944. if(!vstd::contains(notUsedMonsters, creid))
  945. allowed.push_back(creid);
  946. }
  947. if(!allowed.size())
  948. {
  949. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  950. return 0;
  951. }
  952. return pickRandomElementOf(allowed, randGen);
  953. }
  954. return r;
  955. }
  956. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  957. {
  958. assert(iswith(tier, 1, 7));
  959. creaturesOfLevel[tier].addNewBonus(b);
  960. }
  961. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  962. {
  963. allCreatures.addNewBonus(b);
  964. }