CCreatureHandler.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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["turret_shooter"];
  293. if (!value->isNull() && value->Bool())
  294. factionToTurretCreature[c->faction] = creatureID;
  295. value = &creature["ability_remove"];//remove first - arch devil
  296. if (!value->isNull())
  297. {
  298. BOOST_FOREACH(const JsonNode &ability, value->Vector())
  299. {
  300. RemoveAbility(c, ability);
  301. }
  302. }
  303. value = &creature["ability_add"];
  304. if (!value->isNull()) {
  305. BOOST_FOREACH(const JsonNode &ability, value->Vector())
  306. {
  307. AddAbility(c, ability.Vector());
  308. }
  309. }
  310. }
  311. BOOST_FOREACH(const JsonNode &creature, config["unused_creatures"].Vector())
  312. {
  313. notUsedMonsters += creature.Float();
  314. }
  315. loadAnimationInfo();
  316. loadSoundsInfo();
  317. //reading creature ability names
  318. const JsonNode config2(ResourceID("config/bonusnames.json"));
  319. BOOST_FOREACH(const JsonNode &bonus, config2["bonuses"].Vector())
  320. {
  321. std::map<std::string,int>::const_iterator it_map;
  322. std::string bonusID = bonus["id"].String();
  323. it_map = bonusNameMap.find(bonusID);
  324. if (it_map != bonusNameMap.end())
  325. stackBonuses[it_map->second] = std::pair<std::string, std::string>(bonus["name"].String(), bonus["description"].String());
  326. else
  327. tlog2 << "Bonus " << bonusID << " not recognized, ignoring\n";
  328. }
  329. //handle magic resistance secondary skill premy, potentialy may be buggy
  330. //std::map<TBonusType, std::pair<std::string, std::string> >::iterator it = stackBonuses.find(Bonus::MAGIC_RESISTANCE);
  331. //stackBonuses[Bonus::SECONDARY_SKILL_PREMY] = std::pair<std::string, std::string>(it->second.first, it->second.second);
  332. if (VLC->modh->modules.STACK_EXP) //reading default stack experience bonuses
  333. {
  334. CLegacyConfigParser parser("DATA/CREXPBON.TXT");
  335. Bonus b; //prototype with some default properties
  336. b.source = Bonus::STACK_EXPERIENCE;
  337. b.duration = Bonus::PERMANENT;
  338. b.valType = Bonus::ADDITIVE_VALUE;
  339. b.effectRange = Bonus::NO_LIMIT;
  340. b.additionalInfo = 0;
  341. b.turnsRemain = 0;
  342. BonusList bl;
  343. parser.endLine();
  344. parser.readString(); //ignore index
  345. loadStackExp(b, bl, parser);
  346. BOOST_FOREACH(Bonus * b, bl)
  347. addBonusForAllCreatures(b); //health bonus is common for all
  348. parser.endLine();
  349. for (int i = 1; i < 7; ++i)
  350. {
  351. for (int j = 0; j < 4; ++j) //four modifiers common for tiers
  352. {
  353. parser.readString(); //ignore index
  354. bl.clear();
  355. loadStackExp(b, bl, parser);
  356. BOOST_FOREACH(Bonus * b, bl)
  357. addBonusForTier(i, b);
  358. parser.endLine();
  359. }
  360. }
  361. for (int j = 0; j < 4; ++j) //tier 7
  362. {
  363. parser.readString(); //ignore index
  364. bl.clear();
  365. loadStackExp(b, bl, parser);
  366. BOOST_FOREACH(Bonus * b, bl)
  367. {
  368. addBonusForTier(7, b);
  369. creaturesOfLevel[0].addNewBonus(b); //bonuses from level 7 are given to high-level creatures
  370. }
  371. parser.endLine();
  372. }
  373. do //parse everything that's left
  374. {
  375. b.sid = parser.readNumber(); //id = this particular creature ID
  376. loadStackExp(b, creatures[b.sid]->getBonusList(), parser); //add directly to CCreature Node
  377. }
  378. while (parser.endLine());
  379. //Calculate rank exp values, formula appears complicated bu no parsing needed
  380. expRanks.resize(8);
  381. int dif = 0;
  382. int it = 8000; //ignore name of this variable
  383. expRanks[0].push_back(it);
  384. for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
  385. {
  386. expRanks[0].push_back(expRanks[0][j-1] + it + dif);
  387. dif += it/5;
  388. }
  389. for (int i = 1; i < 8; ++i)
  390. {
  391. dif = 0;
  392. it = 1000 * i;
  393. expRanks[i].push_back(it);
  394. for (int j = 1; j < 10; ++j)
  395. {
  396. expRanks[i].push_back(expRanks[i][j-1] + it + dif);
  397. dif += it/5;
  398. }
  399. }
  400. CLegacyConfigParser expBonParser("DATA/CREXPMOD.TXT");
  401. expBonParser.endLine(); //header
  402. maxExpPerBattle.resize(8);
  403. for (int i = 1; i < 8; ++i)
  404. {
  405. expBonParser.readString(); //index
  406. expBonParser.readString(); //float multiplier -> hardcoded
  407. expBonParser.readString(); //ignore upgrade mod? ->hardcoded
  408. expBonParser.readString(); //already calculated
  409. maxExpPerBattle[i] = expBonParser.readNumber();
  410. expRanks[i].push_back(expRanks[i].back() + expBonParser.readNumber());
  411. expBonParser.endLine();
  412. }
  413. //skeleton gets exp penalty
  414. creatures[56].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  415. creatures[57].get()->addBonus(-50, Bonus::EXP_MULTIPLIER, -1);
  416. //exp for tier >7, rank 11
  417. expRanks[0].push_back(147000);
  418. expAfterUpgrade = 75; //percent
  419. maxExpPerBattle[0] = maxExpPerBattle[7];
  420. }//end of Stack Experience
  421. tlog5 << "\t\tReading config/commanders.json" << std::endl;
  422. const JsonNode config3(ResourceID("config/commanders.json"));
  423. BOOST_FOREACH (auto creature, config3["factionCreatures"].Vector())
  424. {
  425. factionCommanders[creature["faction"].Float()] = creature["id"].Float();
  426. }
  427. BOOST_FOREACH (auto bonus, config3["bonusPerLevel"].Vector())
  428. {
  429. commanderLevelPremy.push_back(ParseBonus (bonus.Vector()));
  430. }
  431. int i = 0;
  432. BOOST_FOREACH (auto skill, config3["skillLevels"].Vector())
  433. {
  434. skillLevels.push_back (std::vector<ui8>());
  435. BOOST_FOREACH (auto skillLevel, skill["levels"].Vector())
  436. {
  437. skillLevels[i].push_back (skillLevel.Float());
  438. }
  439. ++i;
  440. }
  441. BOOST_FOREACH (auto ability, config3["abilityRequirements"].Vector())
  442. {
  443. std::pair <Bonus, std::pair <ui8, ui8> > a;
  444. a.first = *ParseBonus (ability["ability"].Vector());
  445. a.second.first = ability["skills"].Vector()[0].Float();
  446. a.second.second = ability["skills"].Vector()[1].Float();
  447. skillRequirements.push_back (a);
  448. }
  449. }
  450. void CCreatureHandler::loadAnimationInfo()
  451. {
  452. CLegacyConfigParser parser("DATA/CRANIM.TXT");
  453. parser.endLine(); // header
  454. parser.endLine();
  455. for(int dd=0; dd<creatures.size(); ++dd)
  456. {
  457. while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
  458. ;
  459. loadUnitAnimInfo(*creatures[dd], parser);
  460. }
  461. }
  462. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, CLegacyConfigParser & parser)
  463. {
  464. unit.timeBetweenFidgets = parser.readNumber();
  465. unit.walkAnimationTime = parser.readNumber();
  466. unit.attackAnimationTime = parser.readNumber();
  467. unit.flightAnimationDistance = parser.readNumber();
  468. ///////////////////////
  469. unit.upperRightMissleOffsetX = parser.readNumber();
  470. unit.upperRightMissleOffsetY = parser.readNumber();
  471. unit.rightMissleOffsetX = parser.readNumber();
  472. unit.rightMissleOffsetY = parser.readNumber();
  473. unit.lowerRightMissleOffsetX = parser.readNumber();
  474. unit.lowerRightMissleOffsetY = parser.readNumber();
  475. ///////////////////////
  476. for(int jjj=0; jjj<12; ++jjj)
  477. {
  478. unit.missleFrameAngles[jjj] = parser.readNumber();
  479. }
  480. unit.troopCountLocationOffset= parser.readNumber();
  481. unit.attackClimaxFrame = parser.readNumber();
  482. parser.endLine();
  483. }
  484. void CCreatureHandler::loadSoundsInfo()
  485. {
  486. tlog5 << "\t\tReading config/cr_sounds.json" << std::endl;
  487. const JsonNode config(ResourceID("config/cr_sounds.json"));
  488. if (!config["creature_sounds"].isNull())
  489. {
  490. BOOST_FOREACH(const JsonNode &node, config["creature_sounds"].Vector())
  491. {
  492. const JsonNode *value;
  493. int id;
  494. value = &node["name"];
  495. bmap<std::string,int>::const_iterator i = nameToID.find(value->String());
  496. if (i != nameToID.end())
  497. id = i->second;
  498. else
  499. {
  500. tlog1 << "Sound info for an unknown creature: " << value->String() << std::endl;
  501. continue;
  502. }
  503. /* This is a bit ugly. Maybe we should use an array for
  504. * sound ids instead of separate variables and define
  505. * attack/defend/killed/... as indexes. */
  506. #define GET_SOUND_VALUE(value_name) do { value = &node[#value_name]; if (!value->isNull()) creatures[id]->sounds.value_name = value->String(); } while(0)
  507. GET_SOUND_VALUE(attack);
  508. GET_SOUND_VALUE(defend);
  509. GET_SOUND_VALUE(killed);
  510. GET_SOUND_VALUE(move);
  511. GET_SOUND_VALUE(shoot);
  512. GET_SOUND_VALUE(wince);
  513. GET_SOUND_VALUE(ext1);
  514. GET_SOUND_VALUE(ext2);
  515. GET_SOUND_VALUE(startMoving);
  516. GET_SOUND_VALUE(endMoving);
  517. #undef GET_SOUND_VALUE
  518. }
  519. }
  520. }
  521. void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) //help function for parsing CREXPBON.txt
  522. {
  523. bool enable = false; //some bonuses are activated with values 2 or 1
  524. std::string buf = parser.readString();
  525. std::string mod = parser.readString();
  526. switch (buf[0])
  527. {
  528. case 'H':
  529. b.type = Bonus::STACK_HEALTH;
  530. b.valType = Bonus::PERCENT_TO_BASE;
  531. break;
  532. case 'A':
  533. b.type = Bonus::PRIMARY_SKILL;
  534. b.subtype = PrimarySkill::ATTACK;
  535. break;
  536. case 'D':
  537. b.type = Bonus::PRIMARY_SKILL;
  538. b.subtype = PrimarySkill::DEFENSE;
  539. break;
  540. case 'M': //Max damage
  541. b.type = Bonus::CREATURE_DAMAGE;
  542. b.subtype = 2;
  543. break;
  544. case 'm': //Min damage
  545. b.type = Bonus::CREATURE_DAMAGE;
  546. b.subtype = 1;
  547. break;
  548. case 'S':
  549. b.type = Bonus::STACKS_SPEED; break;
  550. case 'O':
  551. b.type = Bonus::SHOTS; break;
  552. case 'b':
  553. b.type = Bonus::ENEMY_DEFENCE_REDUCTION; break;
  554. case 'C':
  555. b.type = Bonus::CHANGES_SPELL_COST_FOR_ALLY; break;
  556. case 'd':
  557. b.type = Bonus::DEFENSIVE_STANCE; break;
  558. case 'e':
  559. b.type = Bonus::DOUBLE_DAMAGE_CHANCE;
  560. b.subtype = 0;
  561. break;
  562. case 'E':
  563. b.type = Bonus::DEATH_STARE;
  564. b.subtype = 0; //Gorgon
  565. break;
  566. case 'g':
  567. b.type = Bonus::SPELL_DAMAGE_REDUCTION;
  568. b.subtype = -1; //all magic schools
  569. break;
  570. case 'P':
  571. b.type = Bonus::CASTS; break;
  572. case 'R':
  573. b.type = Bonus::ADDITIONAL_RETALIATION; break;
  574. case 'W':
  575. b.type = Bonus::MAGIC_RESISTANCE;
  576. b.subtype = 0; //otherwise creature window goes crazy
  577. break;
  578. case 'f': //on-off skill
  579. enable = true; //sometimes format is: 2 -> 0, 1 -> 1
  580. switch (mod[0])
  581. {
  582. case 'A':
  583. b.type = Bonus::ATTACKS_ALL_ADJACENT; break;
  584. case 'b':
  585. b.type = Bonus::RETURN_AFTER_STRIKE; break;
  586. case 'B':
  587. b.type = Bonus::TWO_HEX_ATTACK_BREATH; break;
  588. case 'c':
  589. b.type = Bonus::JOUSTING; break;
  590. case 'D':
  591. b.type = Bonus::ADDITIONAL_ATTACK; break;
  592. case 'f':
  593. b.type = Bonus::FEARLESS; break;
  594. case 'F':
  595. b.type = Bonus::FLYING; break;
  596. case 'm':
  597. b.type = Bonus::SELF_MORALE; break;
  598. case 'M':
  599. b.type = Bonus::NO_MORALE; break;
  600. case 'p': //Mind spells
  601. case 'P':
  602. b.type = Bonus::MIND_IMMUNITY; break;
  603. case 'r':
  604. b.type = Bonus::REBIRTH; //on/off? makes sense?
  605. b.subtype = 0;
  606. b.val = 20; //arbitrary value
  607. break;
  608. case 'R':
  609. b.type = Bonus::BLOCKS_RETALIATION; break;
  610. case 's':
  611. b.type = Bonus::FREE_SHOOTING; break;
  612. case 'u':
  613. b.type = Bonus::SPELL_RESISTANCE_AURA; break;
  614. case 'U':
  615. b.type = Bonus::UNDEAD; break;
  616. default:
  617. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  618. return;
  619. break;
  620. }
  621. break;
  622. case 'w': //specific spell immunities, enabled/disabled
  623. enable = true;
  624. switch (mod[0])
  625. {
  626. case 'B': //Blind
  627. b.type = Bonus::SPELL_IMMUNITY;
  628. b.subtype = 74;
  629. break;
  630. case 'H': //Hypnotize
  631. b.type = Bonus::SPELL_IMMUNITY;
  632. b.subtype = 60;
  633. break;
  634. case 'I': //Implosion
  635. b.type = Bonus::SPELL_IMMUNITY;
  636. b.subtype = 18;
  637. break;
  638. case 'K': //Berserk
  639. b.type = Bonus::SPELL_IMMUNITY;
  640. b.subtype = 59;
  641. break;
  642. case 'M': //Meteor Shower
  643. b.type = Bonus::SPELL_IMMUNITY;
  644. b.subtype = 23;
  645. break;
  646. case 'N': //dispell beneficial spells
  647. b.type = Bonus::SPELL_IMMUNITY;
  648. b.subtype = 78;
  649. break;
  650. case 'R': //Armageddon
  651. b.type = Bonus::SPELL_IMMUNITY;
  652. b.subtype = 26;
  653. break;
  654. case 'S': //Slow
  655. b.type = Bonus::SPELL_IMMUNITY;
  656. b.subtype = 54;
  657. break;
  658. case '6':
  659. case '7':
  660. case '8':
  661. case '9':
  662. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  663. b.val = std::atoi(mod.c_str()) - 5;
  664. break;
  665. case ':':
  666. b.type = Bonus::LEVEL_SPELL_IMMUNITY;
  667. b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
  668. break;
  669. case 'F':
  670. b.type = Bonus::FIRE_IMMUNITY;
  671. b.subtype = 1; //not positive
  672. break;
  673. case 'O':
  674. b.type = Bonus::FIRE_IMMUNITY;
  675. b.subtype = 2; //only direct damage
  676. break;
  677. case 'f':
  678. b.type = Bonus::FIRE_IMMUNITY;
  679. b.subtype = 0; //all
  680. break;
  681. case 'C':
  682. b.type = Bonus::WATER_IMMUNITY;
  683. b.subtype = 1; //not positive
  684. break;
  685. case 'W':
  686. b.type = Bonus::WATER_IMMUNITY;
  687. b.subtype = 2; //only direct damage
  688. break;
  689. case 'w':
  690. b.type = Bonus::WATER_IMMUNITY;
  691. b.subtype = 0; //all
  692. break;
  693. case 'E':
  694. b.type = Bonus::EARTH_IMMUNITY;
  695. b.subtype = 2; //only direct damage
  696. break;
  697. case 'e':
  698. b.type = Bonus::EARTH_IMMUNITY;
  699. b.subtype = 0; //all
  700. break;
  701. case 'A':
  702. b.type = Bonus::AIR_IMMUNITY;
  703. b.subtype = 2; //only direct damage
  704. break;
  705. case 'a':
  706. b.type = Bonus::AIR_IMMUNITY;
  707. b.subtype = 0; //all
  708. break;
  709. case 'D':
  710. b.type = Bonus::DIRECT_DAMAGE_IMMUNITY;
  711. break;
  712. case '0':
  713. b.type = Bonus::RECEPTIVE;
  714. break;
  715. default:
  716. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  717. return;
  718. }
  719. break;
  720. case 'i':
  721. enable = true;
  722. b.type = Bonus::NO_DISTANCE_PENALTY;
  723. break;
  724. case 'o':
  725. enable = true;
  726. b.type = Bonus::NO_WALL_PENALTY;
  727. break;
  728. case 'a':
  729. case 'c': //some special abilities are threated as spells, work in progress
  730. b.type = Bonus::SPELL_AFTER_ATTACK;
  731. b.subtype = stringToNumber(mod);
  732. break;
  733. case 'h':
  734. b.type= Bonus::HATE;
  735. b.subtype = stringToNumber(mod);
  736. break;
  737. case 'p':
  738. b.type = Bonus::SPELL_BEFORE_ATTACK;
  739. b.subtype = stringToNumber(mod);
  740. b.additionalInfo = 3; //always expert?
  741. break;
  742. default:
  743. tlog5 << "Not parsed bonus " << buf << mod << "\n";
  744. return;
  745. break;
  746. }
  747. switch (mod[0])
  748. {
  749. case '+':
  750. case '=': //should we allow percent values to stack or pick highest?
  751. b.valType = Bonus::ADDITIVE_VALUE;
  752. break;
  753. }
  754. //limiters, range
  755. si32 lastVal, curVal, lastLev = 0;
  756. if (enable) //0 and 2 means non-active, 1 - active
  757. {
  758. if (b.type != Bonus::REBIRTH)
  759. b.val = 0; //on-off ability, no value specified
  760. curVal = parser.readNumber();// 0 level is never active
  761. for (int i = 1; i < 11; ++i)
  762. {
  763. curVal = parser.readNumber();
  764. if (curVal == 1)
  765. {
  766. b.limiter.reset (new RankRangeLimiter(i));
  767. bl.push_back(new Bonus(b));
  768. break; //never turned off it seems
  769. }
  770. }
  771. }
  772. else
  773. {
  774. lastVal = parser.readNumber(); //basic value, not particularly useful but existent
  775. for (int i = 1; i < 11; ++i)
  776. {
  777. curVal = parser.readNumber();
  778. if (b.type == Bonus::HATE)
  779. curVal *= 10; //odd fix
  780. if (curVal > lastVal) //threshold, add new bonus
  781. {
  782. b.val = curVal - lastVal;
  783. lastVal = curVal;
  784. b.limiter.reset (new RankRangeLimiter(i));
  785. bl.push_back(new Bonus(b));
  786. lastLev = i; //start new range from here, i = previous rank
  787. }
  788. else if (curVal < lastVal)
  789. {
  790. b.val = lastVal;
  791. b.limiter.reset (new RankRangeLimiter(lastLev, i));
  792. }
  793. }
  794. }
  795. }
  796. int CCreatureHandler::stringToNumber(std::string & s)
  797. {
  798. boost::algorithm::replace_first(s,"#",""); //drop hash character
  799. return std::atoi(s.c_str());
  800. }
  801. CCreatureHandler::~CCreatureHandler()
  802. {
  803. }
  804. static int retreiveRandNum(const boost::function<int()> &randGen)
  805. {
  806. if(randGen)
  807. return randGen();
  808. else
  809. return rand();
  810. }
  811. template <typename T> const T & pickRandomElementOf(const std::vector<T> &v, const boost::function<int()> &randGen)
  812. {
  813. return v[retreiveRandNum(randGen) % v.size()];
  814. }
  815. int CCreatureHandler::pickRandomMonster(const boost::function<int()> &randGen, int tier) const
  816. {
  817. int r = 0;
  818. if(tier == -1) //pick any allowed creature
  819. {
  820. do
  821. {
  822. r = pickRandomElementOf(creatures, randGen)->idNumber;
  823. } while (vstd::contains(VLC->creh->notUsedMonsters,r));
  824. }
  825. else
  826. {
  827. assert(vstd::iswithin(tier, 1, 7));
  828. std::vector<int> allowed;
  829. BOOST_FOREACH(const CBonusSystemNode *b, creaturesOfLevel[tier].getChildrenNodes())
  830. {
  831. assert(b->getNodeType() == CBonusSystemNode::CREATURE);
  832. int creid = static_cast<const CCreature*>(b)->idNumber;
  833. if(!vstd::contains(notUsedMonsters, creid))
  834. allowed.push_back(creid);
  835. }
  836. if(!allowed.size())
  837. {
  838. tlog2 << "Cannot pick a random creature of tier " << tier << "!\n";
  839. return 0;
  840. }
  841. return pickRandomElementOf(allowed, randGen);
  842. }
  843. return r;
  844. }
  845. void CCreatureHandler::addBonusForTier(int tier, Bonus *b)
  846. {
  847. assert(vstd::iswithin(tier, 1, 7));
  848. creaturesOfLevel[tier].addNewBonus(b);
  849. }
  850. void CCreatureHandler::addBonusForAllCreatures(Bonus *b)
  851. {
  852. allCreatures.addNewBonus(b);
  853. }
  854. void CCreatureHandler::buildBonusTreeForTiers()
  855. {
  856. BOOST_FOREACH(CCreature *c, creatures)
  857. {
  858. if(vstd::isbetween(c->level, 0, ARRAY_COUNT(creaturesOfLevel)))
  859. c->attachTo(&creaturesOfLevel[c->level]);
  860. else
  861. c->attachTo(&creaturesOfLevel[0]);
  862. }
  863. BOOST_FOREACH(CBonusSystemNode &b, creaturesOfLevel)
  864. b.attachTo(&allCreatures);
  865. }
  866. void CCreatureHandler::deserializationFix()
  867. {
  868. buildBonusTreeForTiers();
  869. }