CGeneralTextHandler.cpp 13 KB

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