CCreatureHandler.cpp 30 KB

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