CGeneralTextHandler.cpp 14 KB

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