CCreatureHandler.cpp 31 KB

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