CCreatureHandler.cpp 29 KB

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