CCreatureHandler.cpp 29 KB

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