CCreatureHandler.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. const JsonNode *value;
  434. /* A creature can have several names. */
  435. const JsonVector &names_vec = creature["name"].Vector();
  436. for (JsonVector::const_iterator it = names_vec.begin(); it!=names_vec.end(); ++it) {
  437. const std::string name = (*it).String();
  438. boost::assign::insert(nameToID)(name, creatureID);
  439. }
  440. // Set various creature properties
  441. CCreature *c = creatures[creatureID];
  442. c->level = creature["level"].Float();
  443. c->faction = creature["faction"].Float();
  444. c->animDefName = creature["defname"].String();
  445. value = &creature["upgrade"];
  446. if (!value->isNull())
  447. c->upgrades.insert(value->Float());
  448. value = &creature["projectile_defname"];
  449. if (!value->isNull()) {
  450. idToProjectile[creatureID] = value->String();
  451. value = &creature["projectile_spin"];
  452. idToProjectileSpin[creatureID] = value->Bool();
  453. }
  454. value = &creature["turret_shooter"];
  455. if (!value->isNull() && value->Bool())
  456. factionToTurretCreature[c->faction] = creatureID;
  457. }
  458. buildBonusTreeForTiers();
  459. loadAnimationInfo();
  460. //reading creature ability names
  461. const JsonNode config2(DATA_DIR "/config/bonusnames.json");
  462. const JsonVector &bonuses_vec = config2["bonuses"].Vector();
  463. for (JsonVector::const_iterator it = bonuses_vec.begin(); it!=bonuses_vec.end(); ++it) {
  464. const JsonNode &bonus = *it;
  465. std::map<std::string,int>::const_iterator it_map;
  466. std::string bonusID = bonus["id"].String();
  467. it_map = bonusNameMap.find(bonusID);
  468. if (it_map != bonusNameMap.end()) {
  469. stackBonuses[it_map->second] = std::pair<std::string, std::string>(bonus["name"].String(), bonus["description"].String());
  470. } else
  471. tlog2 << "Bonus " << bonusID << " not recognized, ignoring\n";
  472. }
  473. //handle magic resistance secondary skill premy, potentialy may be buggy
  474. std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  475. stackBonuses[Bonus::SECONDARY_SKILL_PREMY] = std::pair<std::string, std::string>(it->second.first, it->second.second);
  476. if (STACK_EXP) //reading default stack experience bonuses
  477. {
  478. buf = bitmaph->getTextFile("CREXPBON.TXT");
  479. int it = 0;
  480. si32 creid = -1;
  481. Bonus b; //prototype with some default properties
  482. b.source = Bonus::STACK_EXPERIENCE;
  483. b.duration = Bonus::PERMANENT;
  484. b.valType = Bonus::ADDITIVE_VALUE;
  485. b.effectRange = Bonus::NO_LIMIT;
  486. b.additionalInfo = 0;
  487. b.turnsRemain = 0;
  488. BonusList bl;
  489. std::string dump2;
  490. loadToIt (dump2, buf, it, 3); //ignore first line
  491. loadToIt (dump2, buf, it, 4); //ignore index
  492. loadStackExp(b, bl, buf, it);
  493. BOOST_FOREACH(Bonus * b, bl)
  494. addBonusForAllCreatures(b); //health bonus is common for all
  495. loadToIt (dump2, buf, it, 3); //crop comment
  496. for (i = 1; i < 7; ++i)
  497. {
  498. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  499. {
  500. loadToIt (dump2, buf, it, 4); //ignore index
  501. bl.clear();
  502. loadStackExp(b, bl, buf, it);
  503. BOOST_FOREACH(Bonus * b, bl)
  504. addBonusForTier(i, b);
  505. loadToIt (dump2, buf, it, 3); //crop comment
  506. }
  507. }
  508. for (int j = 0; j < 4; ++j) //tier 7
  509. {
  510. loadToIt (dump2, buf, it, 4); //ignore index
  511. bl.clear();
  512. loadStackExp(b, bl, buf, it);
  513. BOOST_FOREACH(Bonus * b, bl)
  514. {
  515. addBonusForTier(7, b);
  516. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  517. }
  518. loadToIt (dump2, buf, it, 3); //crop comment
  519. }
  520. do //parse everything that's left
  521. {
  522. loadToIt(creid, buf, it, 4); //get index
  523. b.sid = creid; //id = this particular creature ID
  524. loadStackExp(b, creatures[creid]->getBonusList(), buf, it); //add directly to CCreature Node
  525. loadToIt (dump2, buf, it, 3); //crop comment
  526. } while (it < buf.size());
  527. //Calculate rank exp values, formula appears complicated bu no parsing needed
  528. expRanks.resize(8);
  529. int dif = 0;
  530. it = 8000; //ignore name of this variable
  531. expRanks[0].push_back(it);
  532. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  533. {
  534. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  535. dif += it/5;
  536. }
  537. for (i = 1; i < 8; ++i)
  538. {
  539. dif = 0;
  540. it = 1000 * i;
  541. expRanks[i].push_back(it);
  542. for (int j = 1; j < 10; ++j)
  543. {
  544. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  545. dif += it/5;
  546. }
  547. }
  548. buf = bitmaph->getTextFile("CREXPMOD.TXT"); //could be hardcoded though, lots of useless info
  549. it = 0;
  550. loadToIt (dump2, buf, it, 3); //ignore first line
  551. maxExpPerBattle.resize(8);
  552. si32 val;
  553. for (i = 1; i < 8; ++i)
  554. {
  555. loadToIt (dump2, buf, it, 4); //index
  556. loadToIt (dump2, buf, it, 4); //float multiplier -> hardcoded
  557. loadToIt (dump2, buf, it, 4); //ignore upgrade mod? ->hardcoded
  558. loadToIt (dump2, buf, it, 4); //already calculated
  559. loadToIt (val, buf, it, 4);
  560. maxExpPerBattle[i] = (ui32)val;
  561. loadToIt (val, buf, it, 4); //11th level
  562. val += (si32)expRanks[i].back();
  563. expRanks[i].push_back((ui32)val);
  564. loadToIt (dump2, buf, it, 3); //crop comment
  565. }
  566. //skeleton gets exp penalty
  567. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  568. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  569. //exp for tier >7, rank 11
  570. expRanks[0].push_back(147000);
  571. expAfterUpgrade = 75; //percent
  572. maxExpPerBattle[0] = maxExpPerBattle[7];
  573. }//end of Stack Experience
  574. //experiment - add 100 to attack for creatures of tier 1
  575. // Bonus *b = new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::OTHER, +100, 0, 0);
  576. // addBonusForTier(1, b);
  577. }
  578. void CCreatureHandler::loadAnimationInfo()
  579. {
  580. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  581. int andame = buf.size();
  582. int i=0; //buf iterator
  583. int hmcr=0;
  584. for(; i<andame; ++i)
  585. {
  586. if(buf[i]=='\r')
  587. ++hmcr;
  588. if(hmcr==2)
  589. break;
  590. }
  591. i+=2;
  592. for(int dd=0; dd<creatures.size(); ++dd)
  593. {
  594. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  595. loadUnitAnimInfo(*creatures[dd], buf, i);
  596. }
  597. return;
  598. }
  599. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  600. {
  601. int befi=i;
  602. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  603. while(unit.timeBetweenFidgets == 0.0)
  604. {
  605. for(; i<src.size(); ++i)
  606. {
  607. if(src[i]=='\r')
  608. break;
  609. }
  610. i+=2;
  611. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  612. }
  613. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  614. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  615. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  616. ///////////////////////
  617. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  618. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  619. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  620. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  621. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  622. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  623. ///////////////////////
  624. for(int jjj=0; jjj<12; ++jjj)
  625. {
  626. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  627. }
  628. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  629. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  630. for(; i<src.size(); ++i)
  631. {
  632. if(src[i]=='\r')
  633. break;
  634. }
  635. i+=2;
  636. }
  637. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, std::string & src, int & it) //help function for parsing CREXPBON.txt
  638. {
  639. std::string buf, mod;
  640. bool enable = false; //some bonuses are activated with values 2 or 1
  641. loadToIt(buf, src, it, 4);
  642. loadToIt(mod, src, it, 4);
  643. switch (buf[0])
  644. {
  645. case 'H':
  646. b.type = Bonus::STACK_HEALTH;
  647. b.valType = Bonus::PERCENT_TO_BASE;
  648. break;
  649. case 'A':
  650. b.type = Bonus::PRIMARY_SKILL;
  651. b.subtype = PrimarySkill::ATTACK;
  652. break;
  653. case 'D':
  654. b.type = Bonus::PRIMARY_SKILL;
  655. b.subtype = PrimarySkill::DEFENSE;
  656. break;
  657. case 'M': //Max damage
  658. b.type = Bonus::CREATURE_DAMAGE;
  659. b.subtype = 2;
  660. break;
  661. case 'm': //Min damage
  662. b.type = Bonus::CREATURE_DAMAGE;
  663. b.subtype = 1;
  664. break;
  665. case 'S':
  666. b.type = Bonus::STACKS_SPEED; break;
  667. case 'O':
  668. b.type = Bonus::SHOTS; break;
  669. case 'b':
  670. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  671. case 'C':
  672. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  673. case 'd':
  674. b.type = Bonus::DEFENSIVE_STANCE; break;
  675. case 'e':
  676. b.type = Bonus::DOUBLE_DAMAGE_CHANCE; break;
  677. case 'E':
  678. b.type = Bonus::DEATH_STARE;
  679. b.subtype = 0; //Gorgon
  680. break;
  681. case 'g':
  682. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  683. b.subtype = -1; //all magic schools
  684. break;
  685. case 'P':
  686. b.type = Bonus::CASTS; break;
  687. case 'R':
  688. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  689. case 'W':
  690. b.type = Bonus::MAGIC_RESISTANCE;
  691. b.subtype = 0; //otherwise creature window goes crazy
  692. break;
  693. case 'f': //on-off skill
  694. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  695. switch (mod[0])
  696. {
  697. case 'A':
  698. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  699. case 'b':
  700. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  701. case 'B':
  702. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  703. case 'c':
  704. b.type = Bonus::JOUSTING; break;
  705. case 'D':
  706. b.type = Bonus::ADDITIONAL_ATTACK; break;
  707. case 'f':
  708. b.type = Bonus::FEARLESS; break;
  709. case 'F':
  710. b.type = Bonus::FLYING; break;
  711. case 'm':
  712. b.type = Bonus::SELF_MORALE; break;
  713. case 'M':
  714. b.type = Bonus::NO_MORALE; break;
  715. case 'p': //Mind spells
  716. case 'P':
  717. {
  718. loadMindImmunity(b, bl, src, it);
  719. return;
  720. }
  721. return;
  722. case 'r':
  723. b.type = Bonus::REBIRTH; //on/off? makes sense?
  724. b.subtype = 0;
  725. b.val = 20; //arbitrary value
  726. break;
  727. case 'R':
  728. b.type = Bonus::BLOCKS_RETALIATION; break;
  729. case 's':
  730. b.type = Bonus::FREE_SHOOTING; break;
  731. case 'u':
  732. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  733. case 'U':
  734. b.type = Bonus::UNDEAD; break;
  735. default:
  736. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  737. return;
  738. break;
  739. }
  740. break;
  741. case 'w': //specific spell immunities, enabled/disabled
  742. enable = true;
  743. switch (mod[0])
  744. {
  745. case 'B': //Blind
  746. b.type = Bonus::SPELL_IMMUNITY;
  747. b.subtype = 74;
  748. break;
  749. case 'H': //Hypnotize
  750. b.type = Bonus::SPELL_IMMUNITY;
  751. b.subtype = 60;
  752. break;
  753. case 'I': //Implosion
  754. b.type = Bonus::SPELL_IMMUNITY;
  755. b.subtype = 18;
  756. break;
  757. case 'K': //Berserk
  758. b.type = Bonus::SPELL_IMMUNITY;
  759. b.subtype = 59;
  760. break;
  761. case 'M': //Meteor Shower
  762. b.type = Bonus::SPELL_IMMUNITY;
  763. b.subtype = 23;
  764. break;
  765. case 'N': //dispell beneficial spells
  766. b.type = Bonus::SPELL_IMMUNITY;
  767. b.subtype = 78;
  768. break;
  769. case 'R': //Armageddon
  770. b.type = Bonus::SPELL_IMMUNITY;
  771. b.subtype = 26;
  772. break;
  773. case 'S': //Slow
  774. b.type = Bonus::SPELL_IMMUNITY;
  775. b.subtype = 54;
  776. break;
  777. case '6':
  778. case '7':
  779. case '8':
  780. case '9':
  781. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  782. b.val = std::atoi(mod.c_str()) - 5;
  783. break;
  784. case ':':
  785. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  786. b.val = SPELL_LEVELS; //in case someone adds higher level spells?
  787. break;
  788. case 'F':
  789. b.type = Bonus::FIRE_IMMUNITY;
  790. b.subtype = 1; //not positive
  791. break;
  792. case 'O':
  793. b.type = Bonus::FIRE_IMMUNITY;
  794. b.subtype = 2; //only direct damage
  795. break;
  796. case 'f':
  797. b.type = Bonus::FIRE_IMMUNITY;
  798. b.subtype = 0; //all
  799. break;
  800. case 'C':
  801. b.type = Bonus::WATER_IMMUNITY;
  802. b.subtype = 1; //not positive
  803. break;
  804. case 'W':
  805. b.type = Bonus::WATER_IMMUNITY;
  806. b.subtype = 2; //only direct damage
  807. break;
  808. case 'w':
  809. b.type = Bonus::WATER_IMMUNITY;
  810. b.subtype = 0; //all
  811. break;
  812. case 'E':
  813. b.type = Bonus::EARTH_IMMUNITY;
  814. b.subtype = 2; //only direct damage
  815. break;
  816. case 'e':
  817. b.type = Bonus::EARTH_IMMUNITY;
  818. b.subtype = 0; //all
  819. break;
  820. case 'A':
  821. b.type = Bonus::AIR_IMMUNITY;
  822. b.subtype = 2; //only direct damage
  823. break;
  824. case 'a':
  825. b.type = Bonus::AIR_IMMUNITY;
  826. b.subtype = 0; //all
  827. break;
  828. case 'D':
  829. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  830. break;
  831. case '0':
  832. b.type = Bonus::RECEPTIVE;
  833. break;
  834. default:
  835. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  836. return;
  837. }
  838. break;
  839. case 'i':
  840. enable = true;
  841. b.type = Bonus::NO_DISTANCE_PENALTY;
  842. break;
  843. case 'o':
  844. enable = true;
  845. b.type = Bonus::NO_OBSTACLES_PENALTY;
  846. break;
  847. case 'a':
  848. case 'c': //some special abilities are threated as spells, work in progress
  849. b.type = Bonus::SPELL_AFTER_ATTACK;
  850. b.subtype = stringToNumber(mod);
  851. break;
  852. case 'h':
  853. b.type= Bonus::HATE;
  854. b.subtype = stringToNumber(mod);
  855. break;
  856. case 'p':
  857. b.type = Bonus::SPELL_BEFORE_ATTACK;
  858. b.subtype = stringToNumber(mod);
  859. b.additionalInfo = 3; //always expert?
  860. break;
  861. default:
  862. tlog3 << "Not parsed bonus " << buf << mod << "\n";
  863. return;
  864. break;
  865. }
  866. switch (mod[0])
  867. {
  868. case '+':
  869. case '=': //should we allow percent values to stack or pick highest?
  870. b.valType = Bonus::ADDITIVE_VALUE;
  871. break;
  872. }
  873. //limiters, range
  874. si32 lastVal, curVal, lastLev = 0;
  875. if (enable) //0 and 2 means non-active, 1 - active
  876. {
  877. if (b.type != Bonus::REBIRTH)
  878. b.val = 0; //on-off ability, no value specified
  879. loadToIt (curVal, src, it, 4); // 0 level is never active
  880. for (int i = 1; i < 11; ++i)
  881. {
  882. loadToIt (curVal, src, it, 4);
  883. if (curVal == 1)
  884. {
  885. b.limiter.reset (new RankRangeLimiter(i));
  886. bl.push_back(new Bonus(b));
  887. break; //never turned off it seems
  888. }
  889. }
  890. }
  891. else
  892. {
  893. loadToIt (lastVal, src, it, 4); //basic value, not particularly useful but existent
  894. for (int i = 1; i < 11; ++i)
  895. {
  896. loadToIt (curVal, src, it, 4);
  897. if (b.type == Bonus::HATE)
  898. curVal *= 10; //odd fix
  899. if (curVal > lastVal) //threshold, add new bonus
  900. {
  901. b.val = curVal - lastVal;
  902. lastVal = curVal;
  903. b.limiter.reset (new RankRangeLimiter(i));
  904. bl.push_back(new Bonus(b));
  905. lastLev = i; //start new range from here, i = previous rank
  906. }
  907. else if (curVal < lastVal)
  908. {
  909. b.val = lastVal;
  910. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  911. }
  912. }
  913. }
  914. }
  915. void CCreatureHandler::loadMindImmunity(Bonus & b, BonusList & bl, std::string & src, int & it)
  916. {
  917. CCreature * cre = creatures[b.sid]; //odd workaround
  918. b.type = Bonus::SPELL_IMMUNITY;
  919. b.val = Bonus::BASE_NUMBER;
  920. si32 curVal;
  921. b.val = 0; //on-off ability, no value specified
  922. loadToIt (curVal, src, it, 4); // 0 level is never active
  923. for (int i = 1; i < 11; ++i)
  924. {
  925. loadToIt (curVal, src, it, 4);
  926. if (curVal == 1)
  927. {
  928. b.limiter.reset (new RankRangeLimiter(i));
  929. break; //only one limiter here
  930. }
  931. }
  932. std::vector<int> mindSpells = getMindSpells(); //multiplicate spells
  933. for (int g=0; g < mindSpells.size(); ++g)
  934. {
  935. b.subtype = mindSpells[g];
  936. cre->getBonusList().push_back(new Bonus(b));
  937. }
  938. }
  939. int CCreatureHandler::stringToNumber(std::string & s)
  940. {
  941. boost::algorithm::replace_first(s,"#",""); //drop hash character
  942. return std::atoi(s.c_str());
  943. }
  944. CCreatureHandler::~CCreatureHandler()
  945. {
  946. }
  947. static int retreiveRandNum(const boost::function<int()> &randGen)
  948. {
  949. if(randGen)
  950. return randGen();
  951. else
  952. return rand();
  953. }
  954. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  955. {
  956. return v[retreiveRandNum(randGen) % v.size()];
  957. }
  958. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  959. {
  960. int r = 0;
  961. if(tier == -1) //pick any allowed creature
  962. {
  963. do
  964. {
  965. r = pickRandomElementOf(creatures, randGen)->idNumber;
  966. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  967. }
  968. else
  969. {
  970. assert(iswith(tier, 1, 7));
  971. std::vector<int> allowed;
  972. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  973. {
  974. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  975. int creid = static_cast<const CCreature*>(b)->idNumber;
  976. if(!vstd::contains(notUsedMonsters, creid))
  977. allowed.push_back(creid);
  978. }
  979. if(!allowed.size())
  980. {
  981. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  982. return 0;
  983. }
  984. return pickRandomElementOf(allowed, randGen);
  985. }
  986. return r;
  987. }
  988. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  989. {
  990. assert(iswith(tier, 1, 7));
  991. creaturesOfLevel[tier].addNewBonus(b);
  992. }
  993. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  994. {
  995. allCreatures.addNewBonus(b);
  996. }
  997. void CCreatureHandler::buildBonusTreeForTiers()
  998. {
  999. BOOST_FOREACH(CCreature *c, creatures)
  1000. {
  1001. if(isbetw(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  1002. c->attachTo(&creaturesOfLevel[c->level]);
  1003. else
  1004. c->attachTo(&creaturesOfLevel[0]);
  1005. }
  1006. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  1007. b.attachTo(&allCreatures);
  1008. }
  1009. void CCreatureHandler::deserializationFix()
  1010. {
  1011. buildBonusTreeForTiers();
  1012. }