CCreatureHandler.cpp 27 KB

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