CCreatureHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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/list_of.hpp>
  9. #include <boost/algorithm/string.hpp>
  10. #include <boost/algorithm/string/find.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include "../lib/VCMI_Lib.h"
  13. using namespace boost::assign;
  14. extern CLodHandler * bitmaph;
  15. /*
  16. * CCreatureHandler.cpp, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. static std::vector<int> getMindSpells()
  25. {
  26. std::vector<int> ret;
  27. ret.push_back(50); //sorrow
  28. ret.push_back(59); //berserk
  29. ret.push_back(60); //hypnotize
  30. ret.push_back(61); //forgetfulness
  31. return ret;
  32. }
  33. CCreatureHandler::CCreatureHandler()
  34. {
  35. VLC->creh = this;
  36. }
  37. int CCreature::getQuantityID(const int & quantity)
  38. {
  39. if (quantity<5)
  40. return 0;
  41. if (quantity<10)
  42. return 1;
  43. if (quantity<20)
  44. return 2;
  45. if (quantity<50)
  46. return 3;
  47. if (quantity<100)
  48. return 4;
  49. if (quantity<250)
  50. return 5;
  51. if (quantity<500)
  52. return 5;
  53. if (quantity<1000)
  54. return 6;
  55. if (quantity<4000)
  56. return 7;
  57. return 8;
  58. }
  59. bool CCreature::isDoubleWide() const
  60. {
  61. return vstd::contains(abilities, StackFeature::DOUBLE_WIDE);
  62. }
  63. bool CCreature::isFlying() const
  64. {
  65. return vstd::contains(abilities, StackFeature::FLYING);
  66. }
  67. bool CCreature::isShooting() const
  68. {
  69. return vstd::contains(abilities, StackFeature::SHOOTER);
  70. }
  71. bool CCreature::isUndead() const
  72. {
  73. return vstd::contains(abilities, StackFeature::UNDEAD);
  74. }
  75. si32 CCreature::maxAmount(const std::vector<si32> &res) const //how many creatures can be bought
  76. {
  77. int ret = 2147483645;
  78. int resAmnt = std::min(res.size(),cost.size());
  79. for(int i=0;i<resAmnt;i++)
  80. if(cost[i])
  81. ret = std::min(ret,(int)(res[i]/cost[i]));
  82. return ret;
  83. }
  84. int readNumber(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadCreatures() and loadUnitAnimInfo()
  85. {
  86. befi=i;
  87. for(i; i<andame; ++i)
  88. {
  89. if(buf[i]=='\t')
  90. break;
  91. }
  92. std::string tmp = buf.substr(befi, i-befi);
  93. int ret = atoi(buf.substr(befi, i-befi).c_str());
  94. ++i;
  95. return ret;
  96. }
  97. float readFloat(int & befi, int & i, int andame, std::string & buf) //helper function for void CCreatureHandler::loadUnitAnimInfo()
  98. {
  99. befi=i;
  100. for(i; i<andame; ++i)
  101. {
  102. if(buf[i]=='\t')
  103. break;
  104. }
  105. std::string tmp = buf.substr(befi, i-befi);
  106. float ret = atof(buf.substr(befi, i-befi).c_str());
  107. ++i;
  108. return ret;
  109. }
  110. void CCreatureHandler::loadCreatures()
  111. {
  112. notUsedMonsters += 122,124,126,128,145,146,147,148,149,160,161,162,163,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191;
  113. tlog5 << "\t\tReading config/cr_abils.txt and ZCRTRAIT.TXT" << std::endl;
  114. bool useCreAbilsFromZCRTRAIT = true;
  115. ////////////reading cr_abils.txt ///////////////////
  116. std::ifstream abils("config" PATHSEPARATOR "cr_abils.txt", std::ios::in | std::ios::binary); //this file is not in lod
  117. const int MAX_LINE_SIZE = 1000;
  118. char abilLine[MAX_LINE_SIZE+1];
  119. for(int i=0; i<5; ++i) //removing 5 comment lines
  120. {
  121. abils.getline(abilLine, MAX_LINE_SIZE);
  122. }
  123. //reading first line (determining if we should use creature abilities from ZCRTRAIT.TXT)
  124. abils.getline(abilLine, MAX_LINE_SIZE);
  125. useCreAbilsFromZCRTRAIT = atoi(abilLine);
  126. ////////////reading ZCRTRAIT.TXT ///////////////////
  127. std::string buf = bitmaph->getTextFile("ZCRTRAIT.TXT");
  128. int andame = buf.size();
  129. int i=0; //buf iterator
  130. int hmcr=0;
  131. for(i; i<andame; ++i)
  132. {
  133. if(buf[i]=='\r')
  134. ++hmcr;
  135. if(hmcr==2)
  136. break;
  137. }
  138. i+=2;
  139. while(i<buf.size())
  140. {
  141. CCreature ncre;
  142. ncre.cost.resize(RESOURCE_QUANTITY);
  143. ncre.level=0;
  144. ncre.sounds.attack = soundBase::invalid;
  145. ncre.sounds.defend = soundBase::invalid;
  146. ncre.sounds.killed = soundBase::invalid;
  147. ncre.sounds.move = soundBase::invalid;
  148. ncre.sounds.shoot = soundBase::invalid;
  149. ncre.sounds.wince = soundBase::invalid;
  150. ncre.sounds.ext1 = soundBase::invalid;
  151. ncre.sounds.ext2 = soundBase::invalid;
  152. ncre.sounds.startMoving = soundBase::invalid;
  153. ncre.sounds.endMoving = soundBase::invalid;
  154. int befi=i;
  155. for(i; i<andame; ++i)
  156. {
  157. if(buf[i]=='\t')
  158. break;
  159. }
  160. ncre.nameSing = buf.substr(befi, i-befi);
  161. ++i;
  162. befi=i;
  163. for(i; i<andame; ++i)
  164. {
  165. if(buf[i]=='\t')
  166. break;
  167. }
  168. ncre.namePl = buf.substr(befi, i-befi);
  169. ++i;
  170. for(int v=0; v<7; ++v)
  171. {
  172. ncre.cost[v] = readNumber(befi, i, andame, buf);
  173. }
  174. ncre.fightValue = readNumber(befi, i, andame, buf);
  175. ncre.AIValue = readNumber(befi, i, andame, buf);
  176. ncre.growth = readNumber(befi, i, andame, buf);
  177. ncre.hordeGrowth = readNumber(befi, i, andame, buf);
  178. ncre.hitPoints = readNumber(befi, i, andame, buf);
  179. ncre.speed = readNumber(befi, i, andame, buf);
  180. ncre.attack = readNumber(befi, i, andame, buf);
  181. ncre.defence = readNumber(befi, i, andame, buf);
  182. ncre.damageMin = readNumber(befi, i, andame, buf);
  183. ncre.damageMax = readNumber(befi, i, andame, buf);
  184. ncre.shots = readNumber(befi, i, andame, buf);
  185. ncre.spells = readNumber(befi, i, andame, buf);
  186. ncre.ammMin = readNumber(befi, i, andame, buf);
  187. ncre.ammMax = readNumber(befi, i, andame, buf);
  188. befi=i;
  189. for(i; i<andame; ++i)
  190. {
  191. if(buf[i]=='\t')
  192. break;
  193. }
  194. ncre.abilityText = buf.substr(befi, i-befi);
  195. ++i;
  196. befi=i;
  197. for(i; i<andame; ++i)
  198. {
  199. if(buf[i]=='\r')
  200. break;
  201. }
  202. ncre.abilityRefs = buf.substr(befi, i-befi);
  203. i+=2;
  204. if(useCreAbilsFromZCRTRAIT)
  205. { //adding abilities from ZCRTRAIT.TXT
  206. if(boost::algorithm::find_first(ncre.abilityRefs, "DOUBLE_WIDE"))
  207. ncre.abilities.push_back(makeCreatureAbility(StackFeature::DOUBLE_WIDE, 0));
  208. if(boost::algorithm::find_first(ncre.abilityRefs, "FLYING_ARMY"))
  209. ncre.abilities.push_back(makeCreatureAbility(StackFeature::FLYING, 0));
  210. if(boost::algorithm::find_first(ncre.abilityRefs, "SHOOTING_ARMY"))
  211. ncre.abilities.push_back(makeCreatureAbility(StackFeature::SHOOTER, 0));
  212. if(boost::algorithm::find_first(ncre.abilityRefs, "SIEGE_WEAPON"))
  213. ncre.abilities.push_back(makeCreatureAbility(StackFeature::SIEGE_WEAPON, 0));
  214. if(boost::algorithm::find_first(ncre.abilityRefs, "const_two_attacks"))
  215. ncre.abilities.push_back(makeCreatureAbility(StackFeature::ADDITIONAL_ATTACK, 1));
  216. if(boost::algorithm::find_first(ncre.abilityRefs, "const_free_attack"))
  217. ncre.abilities.push_back(makeCreatureAbility(StackFeature::BLOCKS_RETALIATION, 0));
  218. if(boost::algorithm::find_first(ncre.abilityRefs, "IS_UNDEAD"))
  219. ncre.abilities.push_back(makeCreatureAbility(StackFeature::UNDEAD, 0));
  220. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_melee_penalty"))
  221. ncre.abilities.push_back(makeCreatureAbility(StackFeature::NO_MELEE_PENALTY, 0));
  222. if(boost::algorithm::find_first(ncre.abilityRefs, "const_jousting"))
  223. ncre.abilities.push_back(makeCreatureAbility(StackFeature::JOUSTING, 0));
  224. if(boost::algorithm::find_first(ncre.abilityRefs, "const_raises_morale"))
  225. ncre.abilities.push_back(makeCreatureAbility(StackFeature::RAISING_MORALE, 1));
  226. if(boost::algorithm::find_first(ncre.abilityRefs, "const_lowers_morale"))
  227. ncre.abilities.push_back(makeCreatureAbility(StackFeature::ENEMY_MORALE_DECREASING, 1));
  228. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_1"))
  229. ncre.abilities.push_back(makeCreatureAbility(StackFeature::KING1, 0));
  230. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_2"))
  231. ncre.abilities.push_back(makeCreatureAbility(StackFeature::KING2, 0));
  232. if(boost::algorithm::find_first(ncre.abilityRefs, "KING_3"))
  233. ncre.abilities.push_back(makeCreatureAbility(StackFeature::KING3, 0));
  234. if(boost::algorithm::find_first(ncre.abilityRefs, "const_no_wall_penalty"))
  235. ncre.abilities.push_back(makeCreatureAbility(StackFeature::NO_WALL_PENALTY, 0));
  236. if(boost::algorithm::find_first(ncre.abilityRefs, "CATAPULT"))
  237. ncre.abilities.push_back(makeCreatureAbility(StackFeature::CATAPULT, 0));
  238. if(boost::algorithm::find_first(ncre.abilityRefs, "MULTI_HEADED"))
  239. ncre.abilities.push_back(makeCreatureAbility(StackFeature::ATTACKS_ALL_ADJACENT, 0));
  240. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_MIND_SPELLS"))
  241. {
  242. std::vector<int> mindSpells = getMindSpells();
  243. for(int g=0; g<mindSpells.size(); ++g)
  244. {
  245. creatures[40].abilities += makeCreatureAbility(StackFeature::SPELL_IMMUNITY, 0, mindSpells[g]); //giants are immune to mind spells
  246. }
  247. }
  248. if(boost::algorithm::find_first(ncre.abilityRefs, "IMMUNE_TO_FIRE_SPELLS"))
  249. ncre.abilities.push_back(makeCreatureAbility(StackFeature::FIRE_IMMUNITY, 0));
  250. if(boost::algorithm::find_first(ncre.abilityRefs, "HAS_EXTENDED_ATTACK"))
  251. ncre.abilities.push_back(makeCreatureAbility(StackFeature::TWO_HEX_ATTACK_BREATH, 0));
  252. }
  253. if(ncre.nameSing!=std::string("") && ncre.namePl!=std::string(""))
  254. {
  255. ncre.idNumber = creatures.size();
  256. creatures.push_back(ncre);
  257. }
  258. }
  259. // Map types names
  260. #define VCMI_CREATURE_ABILITY_NAME(x) ( #x, StackFeature::x )
  261. std::map<std::string, int> type_list = map_list_of VCMI_CREATURE_ABILITY_LIST;
  262. #undef VCMI_CREATURE_ABILITY_NAME
  263. ////second part of reading cr_abils.txt////
  264. bool contReading = true;
  265. while(contReading) //main reading loop
  266. {
  267. abils.getline(abilLine, MAX_LINE_SIZE);
  268. std::istringstream reader(abilLine);
  269. char command;
  270. reader >> command;
  271. switch(command)
  272. {
  273. case '+': //add new ability
  274. {
  275. int creatureID;
  276. StackFeature nsf;
  277. si32 buf;
  278. std::string type;
  279. reader >> creatureID;
  280. reader >> type;
  281. std::map<std::string, int>::iterator it = type_list.find(type);
  282. if (it == type_list.end()) {
  283. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  284. break;
  285. }
  286. nsf.type = it->second;
  287. reader >> buf; nsf.value = buf;
  288. reader >> buf; nsf.subtype = buf;
  289. reader >> buf; nsf.additionalInfo = buf;
  290. nsf.source = StackFeature::CREATURE_ABILITY;
  291. nsf.duration = StackFeature::WHOLE_BATTLE;
  292. nsf.turnsRemain = 0;
  293. creatures[creatureID].abilities += nsf;
  294. break;
  295. }
  296. case '-': //remove ability
  297. {
  298. int creatureID;
  299. std::string type;
  300. reader >> creatureID;
  301. reader >> type;
  302. std::map<std::string, int>::iterator it = type_list.find(type);
  303. if (it == type_list.end())
  304. {
  305. tlog1 << "Error: invalid type " << type << " in cr_abils.txt" << std::endl;
  306. break;
  307. }
  308. int typeNo = it->second;
  309. StackFeature::ECombatFeatures ecf = static_cast<StackFeature::ECombatFeatures>(typeNo);
  310. creatures[creatureID].abilities -= ecf;
  311. break;
  312. }
  313. case '0': //end reading
  314. {
  315. contReading = false;
  316. break;
  317. }
  318. default: //invalid command
  319. {
  320. tlog1 << "Parse error in file config/cr_abils.txt" << std::endl;
  321. break;
  322. }
  323. }
  324. }
  325. abils.close();
  326. tlog5 << "\t\tReading config/crerefnam.txt" << std::endl;
  327. //loading reference names
  328. std::ifstream ifs("config/crerefnam.txt");
  329. int tempi;
  330. std::string temps;
  331. for (;;)
  332. {
  333. ifs >> tempi >> temps;
  334. if (tempi>=creatures.size())
  335. break;
  336. boost::assign::insert(nameToID)(temps,tempi);
  337. creatures[tempi].nameRef=temps;
  338. }
  339. ifs.close();
  340. ifs.clear();
  341. for(int i=1;i<=10;i++)
  342. levelCreatures.insert(std::pair<int,std::vector<CCreature*> >(i,std::vector<CCreature*>()));
  343. tlog5 << "\t\tReading config/monsters.txt" << std::endl;
  344. ifs.open("config/monsters.txt");
  345. {
  346. while(!ifs.eof())
  347. {
  348. int id, lvl;
  349. ifs >> id >> lvl;
  350. if(lvl>0)
  351. {
  352. creatures[id].level = lvl;
  353. levelCreatures[lvl].push_back(&(creatures[id]));
  354. }
  355. }
  356. }
  357. ifs.close();
  358. ifs.clear();
  359. tlog5 << "\t\tReading config/cr_factions.txt" << std::endl;
  360. ifs.open("config/cr_factions.txt");
  361. while(!ifs.eof())
  362. {
  363. int id, fact;
  364. ifs >> id >> fact;
  365. creatures[id].faction = fact;
  366. }
  367. ifs.close();
  368. ifs.clear();
  369. tlog5 << "\t\tReading config/cr_upgrade_list.txt" << std::endl;
  370. ifs.open("config/cr_upgrade_list.txt");
  371. while(!ifs.eof())
  372. {
  373. int id, up;
  374. ifs >> id >> up;
  375. creatures[id].upgrades.insert(up);
  376. }
  377. ifs.close();
  378. ifs.clear();
  379. //loading unit animation def names
  380. tlog5 << "\t\tReading config/CREDEFS.TXT" << std::endl;
  381. std::ifstream inp("config/CREDEFS.TXT", std::ios::in | std::ios::binary); //this file is not in lod
  382. inp.seekg(0,std::ios::end); // na koniec
  383. int andame2 = inp.tellg(); // read length
  384. inp.seekg(0,std::ios::beg); // wracamy na poczatek
  385. char * bufor = new char[andame2+1]; // allocate memory
  386. inp.read((char*)bufor, andame2); // read map file to buffer
  387. inp.close();
  388. bufor[andame2] = 0;
  389. buf = std::string(bufor);
  390. delete [] bufor;
  391. i = 0; //buf iterator
  392. hmcr = 0;
  393. for(i; i<andame2; ++i) //omitting rubbish
  394. {
  395. if(buf[i]=='\r')
  396. break;
  397. }
  398. i+=2;
  399. tlog5 << "We have "<<creatures.size() << " creatures\n";
  400. for(int s=0; s<creatures.size(); ++s)
  401. {
  402. //tlog5 <<"\t\t\t" << s <<". Reading defname. \n";
  403. int befi=i;
  404. std::string rub;
  405. for(i; i<andame2; ++i)
  406. {
  407. if(buf[i]==' ')
  408. break;
  409. }
  410. rub = buf.substr(befi, i-befi);
  411. ++i;
  412. befi=i;
  413. for(i; i<andame2; ++i)
  414. {
  415. if(buf[i]=='\r')
  416. break;
  417. }
  418. std::string defName = buf.substr(befi, i-befi);
  419. creatures[s].animDefName = defName;
  420. }
  421. tlog5 << "\t\tReading CRANIM.TXT.txt" << std::endl;
  422. loadAnimationInfo();
  423. //loading id to projectile mapping
  424. tlog5 << "\t\tReading config/cr_shots.txt" << std::endl;
  425. std::ifstream inp2("config" PATHSEPARATOR "cr_shots.txt", std::ios::in | std::ios::binary); //this file is not in lod
  426. char dump [200];
  427. inp2.getline(dump, 200);
  428. while(true)
  429. {
  430. int id;
  431. std::string name;
  432. bool spin;
  433. inp2>>id;
  434. if(id == -1)
  435. break;
  436. inp2>>name;
  437. idToProjectile[id] = name;
  438. inp2>>spin;
  439. idToProjectileSpin[id] = spin;
  440. }
  441. inp2.close();
  442. }
  443. void CCreatureHandler::loadAnimationInfo()
  444. {
  445. std::string buf = bitmaph->getTextFile("CRANIM.TXT");
  446. int andame = buf.size();
  447. int i=0; //buf iterator
  448. int hmcr=0;
  449. for(i; i<andame; ++i)
  450. {
  451. if(buf[i]=='\r')
  452. ++hmcr;
  453. if(hmcr==2)
  454. break;
  455. }
  456. i+=2;
  457. for(int dd=0; dd<creatures.size(); ++dd)
  458. {
  459. //tlog5 << "\t\t\tReading animation info for creature " << dd << std::endl;
  460. loadUnitAnimInfo(creatures[dd], buf, i);
  461. }
  462. return;
  463. }
  464. void CCreatureHandler::loadUnitAnimInfo(CCreature & unit, std::string & src, int & i)
  465. {
  466. int befi=i;
  467. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  468. while(unit.timeBetweenFidgets == 0.0)
  469. {
  470. for(i; i<src.size(); ++i)
  471. {
  472. if(src[i]=='\r')
  473. break;
  474. }
  475. i+=2;
  476. unit.timeBetweenFidgets = readFloat(befi, i, src.size(), src);
  477. }
  478. unit.walkAnimationTime = readFloat(befi, i, src.size(), src);
  479. unit.attackAnimationTime = readFloat(befi, i, src.size(), src);
  480. unit.flightAnimationDistance = readFloat(befi, i, src.size(), src);
  481. ///////////////////////
  482. unit.upperRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  483. unit.upperRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  484. unit.rightMissleOffsetX = readNumber(befi, i, src.size(), src);
  485. unit.rightMissleOffsetY = readNumber(befi, i, src.size(), src);
  486. unit.lowerRightMissleOffsetX = readNumber(befi, i, src.size(), src);
  487. unit.lowerRightMissleOffsetY = readNumber(befi, i, src.size(), src);
  488. ///////////////////////
  489. for(int jjj=0; jjj<12; ++jjj)
  490. {
  491. unit.missleFrameAngles[jjj] = readFloat(befi, i, src.size(), src);
  492. }
  493. unit.troopCountLocationOffset= readNumber(befi, i, src.size(), src);
  494. unit.attackClimaxFrame = readNumber(befi, i, src.size(), src);
  495. for(i; i<src.size(); ++i)
  496. {
  497. if(src[i]=='\r')
  498. break;
  499. }
  500. i+=2;
  501. }
  502. CCreatureHandler::~CCreatureHandler()
  503. {
  504. }