CCreatureHandler.cpp 32 KB

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