CGeneralTextHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "StdInc.h"
  2. #include "CGeneralTextHandler.h"
  3. #include "../lib/VCMI_Lib.h"
  4. #include "CLodHandler.h"
  5. #include "GameConstants.h"
  6. /*
  7. * CGeneralTextHandler.cpp, part of VCMI engine
  8. *
  9. * Authors: listed in file AUTHORS in main folder
  10. *
  11. * License: GNU General Public License v2.0 or later
  12. * Full text of license available in license.txt file, in main folder
  13. *
  14. */
  15. std::string readTo(const std::string &in, int &it, char end)
  16. {
  17. int pom = it;
  18. int last = in.find_first_of(end,it);
  19. it+=(1+last-it);
  20. return in.substr(pom,last-pom);
  21. }
  22. void trimQuotation(std::string &op)
  23. {
  24. if(op.length() && op[0] == '\"' && op[op.size()-1] == '\"')
  25. op = op.substr(1,op.size()-2);
  26. }
  27. void CGeneralTextHandler::load()
  28. {
  29. std::string buf1 = bitmaph->getTextFile("ZELP.TXT");
  30. int itr=0, eol=-1, eolnext=-1, pom;
  31. eolnext = buf1.find_first_of('\r',itr);
  32. while(itr<buf1.size())
  33. {
  34. eol = eolnext; //end of this line
  35. eolnext = buf1.find_first_of('\r',eol+1); //end of the next line
  36. pom=buf1.find_first_of('\t',itr); //upcoming tab
  37. if(eol<0 || pom<0)
  38. break;
  39. if(pom>eol) //in current line there is not tab
  40. zelp.push_back(std::pair<std::string,std::string>());
  41. else
  42. {
  43. zelp.push_back
  44. (std::pair<std::string,std::string>
  45. (buf1.substr(itr,pom-itr),
  46. buf1.substr(pom+1,eol-pom-1)));
  47. boost::algorithm::replace_all(zelp[zelp.size()-1].first,"\t","");
  48. boost::algorithm::replace_all(zelp[zelp.size()-1].second,"\t","");
  49. trimQuotation(zelp.back().second);
  50. }
  51. itr=eol+2;
  52. }
  53. std::string buf = bitmaph->getTextFile("VCDESC.TXT");
  54. int andame = buf.size();
  55. int i=0; //buf iterator
  56. for(int gg=0; gg<14; ++gg)
  57. {
  58. int befi=i;
  59. for(; i<andame; ++i)
  60. {
  61. if(buf[i]=='\r')
  62. break;
  63. }
  64. victoryConditions[gg] = buf.substr(befi, i-befi);
  65. i+=2;
  66. }
  67. buf = bitmaph->getTextFile("LCDESC.TXT");
  68. andame = buf.size();
  69. i=0; //buf iterator
  70. for(int gg=0; gg<4; ++gg)
  71. {
  72. int befi=i;
  73. for(; i<andame; ++i)
  74. {
  75. if(buf[i]=='\r')
  76. break;
  77. }
  78. lossCondtions[gg] = buf.substr(befi, i-befi);
  79. i+=2;
  80. }
  81. hTxts.resize(GameConstants::HEROES_QUANTITY);
  82. buf = bitmaph->getTextFile("HEROSPEC.TXT");
  83. i=0;
  84. std::string dump;
  85. for(int iii=0; iii<2; ++iii)
  86. {
  87. loadToIt(dump,buf,i,3);
  88. }
  89. for (int iii=0;iii<hTxts.size();iii++)
  90. {
  91. loadToIt(hTxts[iii].bonusName,buf,i,4);
  92. loadToIt(hTxts[iii].shortBonus,buf,i,4);
  93. loadToIt(hTxts[iii].longBonus,buf,i,3);
  94. trimQuotation(hTxts[iii].longBonus);
  95. }
  96. buf = bitmaph->getTextFile("HEROBIOS.TXT");
  97. i=0;
  98. for (int iii=0;iii<hTxts.size();iii++)
  99. {
  100. loadToIt(hTxts[iii].biography,buf,i,3);
  101. trimQuotation(hTxts[iii].biography);
  102. }
  103. int it;
  104. buf = bitmaph->getTextFile("BLDGNEUT.TXT");
  105. andame = buf.size(), it=0;
  106. for(int b=0;b<15;b++)
  107. {
  108. std::string name = readTo(buf,it,'\t'),
  109. description = readTo(buf,it,'\n');
  110. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  111. {
  112. buildings[fi][b].first = name;
  113. buildings[fi][b].second = description;
  114. }
  115. }
  116. buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  117. //shipyard with the ship
  118. std::string name = readTo(buf,it,'\t'),
  119. description = readTo(buf,it,'\n');
  120. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  121. {
  122. buildings[fi][20].first = name;
  123. buildings[fi][20].second = description;
  124. }
  125. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  126. {
  127. buildings[fi][16].first = readTo(buf,it,'\t'),
  128. buildings[fi][16].second = readTo(buf,it,'\n');
  129. }
  130. /////done reading "BLDGNEUT.TXT"******************************
  131. buf = bitmaph->getTextFile("BLDGSPEC.TXT");
  132. andame = buf.size(), it=0;
  133. for(int f=0;f<GameConstants::F_NUMBER;f++)
  134. {
  135. for(int b=0;b<9;b++)
  136. {
  137. buildings[f][17+b].first = readTo(buf,it,'\t');
  138. buildings[f][17+b].second = readTo(buf,it,'\n');
  139. }
  140. buildings[f][26].first = readTo(buf,it,'\t');
  141. buildings[f][26].second = readTo(buf,it,'\n');
  142. buildings[f][15].first = readTo(buf,it,'\t'); //resource silo
  143. buildings[f][15].second = readTo(buf,it,'\n');//resource silo
  144. }
  145. /////done reading BLDGSPEC.TXT*********************************
  146. buf = bitmaph->getTextFile("DWELLING.TXT");
  147. andame = buf.size(), it=0;
  148. for(int f=0;f<GameConstants::F_NUMBER;f++)
  149. {
  150. for(int b=0;b<14;b++)
  151. {
  152. buildings[f][30+b].first = readTo(buf,it,'\t');
  153. buildings[f][30+b].second = readTo(buf,it,'\n');
  154. }
  155. }
  156. //remove prceeding / trailing whitespaces nad quoation marks from buildings descriptions
  157. for(std::map<int, std::map<int, std::pair<std::string, std::string> > >::iterator i = buildings.begin(); i != buildings.end(); i++)
  158. {
  159. for(std::map<int, std::pair<std::string, std::string> >::iterator j = i->second.begin(); j != i->second.end(); j++)
  160. {
  161. std::string &str = j->second.second;
  162. boost::algorithm::trim(str);
  163. trimQuotation(str);
  164. }
  165. }
  166. buf = bitmaph->getTextFile("TCOMMAND.TXT");
  167. itr=0;
  168. while(itr<buf.length()-1)
  169. {
  170. std::string tmp;
  171. loadToIt(tmp, buf, itr, 3);
  172. tcommands.push_back(tmp);
  173. }
  174. buf = bitmaph->getTextFile("HALLINFO.TXT");
  175. itr=0;
  176. while(itr<buf.length()-1)
  177. {
  178. std::string tmp;
  179. loadToIt(tmp, buf, itr, 3);
  180. hcommands.push_back(tmp);
  181. }
  182. buf = bitmaph->getTextFile("CASTINFO.TXT");
  183. itr=0;
  184. while(itr<buf.length()-1)
  185. {
  186. std::string tmp;
  187. loadToIt(tmp, buf, itr, 3);
  188. fcommands.push_back(tmp);
  189. }
  190. std::istringstream ins, namess;
  191. ins.str(bitmaph->getTextFile("TOWNTYPE.TXT"));
  192. namess.str(bitmaph->getTextFile("TOWNNAME.TXT"));
  193. int si=0;
  194. char bufname[75];
  195. while (!ins.eof())
  196. {
  197. ins.getline(bufname,50);
  198. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  199. townNames.resize(si+1);
  200. for (int i=0; i<GameConstants::NAMES_PER_TOWN; i++)
  201. {
  202. namess.getline(bufname,50);
  203. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  204. }
  205. si++;
  206. }
  207. tlog5 << "\t\tReading OBJNAMES \n";
  208. buf = bitmaph->getTextFile("OBJNAMES.TXT");
  209. it=0; //hope that -1 will not break this
  210. while (it<buf.length()-1)
  211. {
  212. std::string nobj;
  213. loadToIt(nobj, buf, it, 3);
  214. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  215. {
  216. nobj = nobj.substr(0, nobj.size()-1);
  217. }
  218. names.push_back(nobj);
  219. }
  220. tlog5 << "\t\tReading ADVEVENT \n";
  221. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  222. it=0;
  223. std::string temp;
  224. while (it<buf.length()-1)
  225. {
  226. loadToIt(temp,buf,it,3);
  227. if (temp[0]=='\"')
  228. {
  229. temp = temp.substr(1,temp.length()-2);
  230. }
  231. boost::algorithm::replace_all(temp,"\"\"","\"");
  232. advobtxt.push_back(temp);
  233. }
  234. tlog5 << "\t\tReading XTRAINFO \n";
  235. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  236. it=0;
  237. while (it<buf.length()-1)
  238. {
  239. loadToIt(temp,buf,it,3);
  240. xtrainfo.push_back(temp);
  241. }
  242. tlog5 << "\t\tReading MINENAME \n";
  243. buf = bitmaph->getTextFile("MINENAME.TXT");
  244. it=0;
  245. while (it<buf.length()-1)
  246. {
  247. loadToIt(temp,buf,it,3);
  248. mines.push_back(std::pair<std::string,std::string>(temp,""));
  249. }
  250. tlog5 << "\t\tReading MINEEVNT \n";
  251. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  252. it=0;
  253. i=0;
  254. while (it<buf.length()-1)
  255. {
  256. loadToIt(temp,buf,it,3);
  257. temp = temp.substr(1,temp.length()-2);
  258. if(i < mines.size())
  259. mines[i++].second = temp;
  260. else
  261. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  262. }
  263. tlog5 << "\t\tReading RESTYPES \n";
  264. buf = bitmaph->getTextFile("RESTYPES.TXT");
  265. it=0;
  266. while (it<buf.length()-1)
  267. {
  268. loadToIt(temp,buf,it,3);
  269. restypes.push_back(temp);
  270. }
  271. tlog5 << "\t\tReading TERRNAME \n";
  272. buf = bitmaph->getTextFile("TERRNAME.TXT");
  273. it=0;
  274. while (it<buf.length()-1)
  275. {
  276. loadToIt(temp,buf,it,3);
  277. terrainNames.push_back(temp);
  278. }
  279. tlog5 << "\t\tReading RANDSIGN \n";
  280. buf = bitmaph->getTextFile("RANDSIGN.TXT");
  281. it=0;
  282. while (it<buf.length()-1)
  283. {
  284. loadToIt(temp,buf,it,3);
  285. randsign.push_back(temp);
  286. }
  287. tlog5 << "\t\tReading ZCRGN1 \n";
  288. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  289. it=0;
  290. while (it<buf.length()-1)
  291. {
  292. loadToIt(temp,buf,it,3);
  293. creGens.push_back(temp);
  294. }
  295. tlog5 << "\t\tReading CRGN4 \n";
  296. buf = bitmaph->getTextFile("CRGEN4.TXT");
  297. it=0;
  298. while (it<buf.length()-1)
  299. {
  300. loadToIt(temp,buf,it,3);
  301. creGens4.push_back(temp);
  302. }
  303. buf = bitmaph->getTextFile("GENRLTXT.TXT");
  304. std::string tmp;
  305. andame = buf.size();
  306. i=0; //buf iterator
  307. for(; i<andame; ++i)
  308. {
  309. if(buf[i]=='\r')
  310. break;
  311. }
  312. i+=2;
  313. std::string buflet;
  314. for(int jj=0; jj<764; ++jj)
  315. {
  316. loadToIt(buflet, buf, i, 2);
  317. trimQuotation(buflet);
  318. boost::algorithm::replace_all(buflet,"\"\"","\"");
  319. allTexts.push_back(buflet);
  320. }
  321. std::string stro = bitmaph->getTextFile("Overview.txt");
  322. itr=0;
  323. while(itr<stro.length()-1)
  324. {
  325. loadToIt(tmp, stro, itr, 3);
  326. trimQuotation(tmp);
  327. overview.push_back(tmp);
  328. }
  329. std::string strc = bitmaph->getTextFile("PLCOLORS.TXT");
  330. itr=0;
  331. while(itr<strc.length()-1)
  332. {
  333. loadToIt(tmp, strc, itr, 3);
  334. colors.push_back(tmp);
  335. tmp[0] = toupper(tmp[0]);
  336. capColors.push_back(tmp);
  337. }
  338. std::string strs = bitmaph->getTextFile("ARRAYTXT.TXT");
  339. itr=0;
  340. while(itr<strs.length()-1)
  341. {
  342. loadToIt(tmp, strs, itr, 3);
  343. trimQuotation(tmp);
  344. arraytxt.push_back(tmp);
  345. }
  346. itr = 0;
  347. std::string strin = bitmaph->getTextFile("PRISKILL.TXT");
  348. for(int hh=0; hh<4; ++hh)
  349. {
  350. loadToIt(tmp, strin, itr, 3);
  351. primarySkillNames.push_back(tmp);
  352. }
  353. itr = 0;
  354. strin = bitmaph->getTextFile("JKTEXT.TXT");
  355. for(int hh=0; hh<45; ++hh)
  356. {
  357. loadToIt(tmp, strin, itr, 3);
  358. trimQuotation(tmp);
  359. jktexts.push_back(tmp);
  360. }
  361. itr = 0;
  362. strin = bitmaph->getTextFile("TVRNINFO.TXT");
  363. for(int hh=0; hh<8; ++hh)
  364. {
  365. loadToIt(tmp, strin, itr, 3);
  366. tavernInfo.push_back(tmp);
  367. }
  368. itr = 0;
  369. strin = bitmaph->getTextFile("TURNDUR.TXT");
  370. for(int hh=0; hh<11; ++hh)
  371. {
  372. loadToIt(tmp, strin, itr, 3);
  373. turnDurations.push_back(tmp);
  374. }
  375. itr = 0;
  376. strin = bitmaph->getTextFile("HEROSCRN.TXT");
  377. for(int hh=0; hh<33; ++hh)
  378. {
  379. loadToIt(tmp, strin, itr, 3);
  380. heroscrn.push_back(tmp);
  381. }
  382. itr = 0;
  383. strin = bitmaph->getTextFile("ARTEVENT.TXT");
  384. for(; itr<strin.size();)
  385. {
  386. loadToIt(tmp, strin, itr, 2);
  387. // boost::algorithm::trim(tmp);
  388. trimQuotation(tmp);
  389. boost::algorithm::replace_all(tmp,"\"\"","\"");
  390. artifEvents.push_back(tmp);
  391. }
  392. buf = bitmaph->getTextFile("SSTRAITS.TXT");
  393. it=0;
  394. for(int i=0; i<2; ++i)
  395. loadToIt(dump,buf,it,3);
  396. skillName.resize(GameConstants::SKILL_QUANTITY);
  397. skillInfoTexts.resize(GameConstants::SKILL_QUANTITY);
  398. for (int i=0; i<GameConstants::SKILL_QUANTITY; i++)
  399. {
  400. skillInfoTexts[i].resize(3);
  401. loadToIt(skillName[i],buf,it,4);
  402. loadToIt(skillInfoTexts[i][0],buf,it,4);
  403. loadToIt(skillInfoTexts[i][1],buf,it,4);
  404. loadToIt(skillInfoTexts[i][2],buf,it,3);
  405. for(int j = 0; j < 3; j++)
  406. trimQuotation(skillInfoTexts[i][j]);
  407. }
  408. buf = bitmaph->getTextFile("SKILLLEV.TXT");
  409. it=0;
  410. for(int i=0; i<6; ++i)
  411. {
  412. std::string buffo;
  413. loadToIt(buffo,buf,it,3);
  414. levels.push_back(buffo);
  415. }
  416. buf = bitmaph->getTextFile ("SEERHUT.TXT");
  417. it = 0;
  418. loadToIt (dump, buf, it, 3);
  419. loadToIt (dump, buf, it, 4); //dump description
  420. seerEmpty.resize(6);
  421. for (i = 0; i < 5; ++i)
  422. {
  423. loadToIt(seerEmpty[i], buf, it, 4);
  424. trimQuotation (seerEmpty[i]);
  425. }
  426. loadToIt (seerEmpty[5], buf, it, 3);
  427. trimQuotation (seerEmpty[5]);
  428. int j,k;
  429. quests.resize(10);
  430. for (i = 0; i < 9; ++i) //9 types of quests
  431. {
  432. quests[i].resize(5);
  433. for (j = 0; j < 5; ++j)
  434. {
  435. loadToIt (dump, buf, it, 4); //front description
  436. quests[i][j].resize(6);
  437. for (k = 0; k < 5; ++k)
  438. {
  439. loadToIt (quests[i][j][k], buf, it, 4);
  440. trimQuotation (quests[i][j][k]);
  441. }
  442. loadToIt (quests[i][j][5], buf, it, 3);
  443. trimQuotation (quests[i][j][5]);
  444. }
  445. }
  446. quests[9].resize(1);
  447. quests[9][0].resize(6);
  448. for (k = 0; k < 5; ++k) //Time limit
  449. {
  450. loadToIt (quests[9][0][k], buf, it, 4);
  451. }
  452. loadToIt (quests[9][0][k], buf, it, 3);
  453. for (i = 0; i < 2; ++i) //gap description
  454. loadToIt(dump,buf,it,3);
  455. seerNames.resize(48);
  456. for (i = 0; i < 48; ++i)
  457. loadToIt(seerNames[i], buf, it, 3);
  458. buf = bitmaph->getTextFile("TENTCOLR.TXT");
  459. itr=0;
  460. while(itr<buf.length()-1)
  461. {
  462. std::string tmp;
  463. loadToIt(tmp, buf, itr, 3);
  464. tentColors.push_back(tmp);
  465. }
  466. //campaigns
  467. buf = bitmaph->getTextFile ("CAMPTEXT.TXT");
  468. it = 0;
  469. loadToIt (dump, buf, it, 3); //comment
  470. std::string nameBuf;
  471. do //map names
  472. {
  473. loadToIt(nameBuf, buf, it, 3);
  474. if(nameBuf.size())
  475. {
  476. campaignMapNames.push_back(nameBuf);
  477. }
  478. } while (nameBuf.size());
  479. campaignRegionNames.resize(campaignMapNames.size()); //allocating space
  480. for(int g=0; g<campaignMapNames.size(); ++g) //region names
  481. {
  482. do //dump comments and empty lines
  483. {
  484. loadToIt(nameBuf, buf, it, 3);
  485. } while (!nameBuf.size() || nameBuf[0] != '/');
  486. do //actual names
  487. {
  488. loadToIt(nameBuf, buf, it, 3);
  489. if(nameBuf.size())
  490. {
  491. campaignRegionNames[g].push_back(nameBuf);
  492. }
  493. } while (nameBuf.size());
  494. }
  495. buf = bitmaph->getTextFile ("ZCREXP.TXT");
  496. it = 0;
  497. loadToIt (dump, buf, it, 3); //comment
  498. for (int i = 0; i < 459; ++i) //some texts seem to be empty
  499. {
  500. loadToIt(dump, buf, it, 4); //description, usually useless
  501. loadToIt(nameBuf, buf, it, 3);
  502. zcrexp.push_back(nameBuf);
  503. }
  504. std::string threatLevelDir = GameConstants::DATA_DIR + "/config/threatlevel.txt";
  505. std::ifstream ifs(threatLevelDir.c_str(), std::ios::in | std::ios::binary);
  506. getline(ifs, buf); //skip 1st line
  507. for (int i = 0; i < 13; ++i)
  508. {
  509. getline(ifs, buf);
  510. threat.push_back(buf);
  511. }
  512. }
  513. std::string CGeneralTextHandler::getTitle(const std::string & text)
  514. {
  515. std::string ret;
  516. int i=0;
  517. while ((text[i++]!='{'));
  518. while ((text[i]!='}') && (i<text.length()))
  519. ret+=text[i++];
  520. return ret;
  521. }
  522. std::string CGeneralTextHandler::getDescr(const std::string & text)
  523. {
  524. std::string ret;
  525. int i=0;
  526. while ((text[i++]!='}'));
  527. i+=2;
  528. while ((text[i]!='"') && (i<text.length()))
  529. ret+=text[i++];
  530. return ret;
  531. }
  532. CGeneralTextHandler::CGeneralTextHandler()
  533. {
  534. }