CGeneralTextHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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(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[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; 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; 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. if(str[0] == '"' && str[str.size()-1] == '"')
  166. str = str.substr(1,str.size()-2);
  167. }
  168. }
  169. buf = bitmaph->getTextFile("TCOMMAND.TXT");
  170. itr=0;
  171. while(itr<buf.length()-1)
  172. {
  173. std::string tmp;
  174. loadToIt(tmp, buf, itr, 3);
  175. tcommands.push_back(tmp);
  176. }
  177. buf = bitmaph->getTextFile("HALLINFO.TXT");
  178. itr=0;
  179. while(itr<buf.length()-1)
  180. {
  181. std::string tmp;
  182. loadToIt(tmp, buf, itr, 3);
  183. hcommands.push_back(tmp);
  184. }
  185. buf = bitmaph->getTextFile("CASTINFO.TXT");
  186. itr=0;
  187. while(itr<buf.length()-1)
  188. {
  189. std::string tmp;
  190. loadToIt(tmp, buf, itr, 3);
  191. fcommands.push_back(tmp);
  192. }
  193. std::istringstream ins, namess;
  194. ins.str(bitmaph->getTextFile("TOWNTYPE.TXT"));
  195. namess.str(bitmaph->getTextFile("TOWNNAME.TXT"));
  196. int si=0;
  197. char bufname[75];
  198. while (!ins.eof())
  199. {
  200. ins.getline(bufname,50);
  201. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  202. townNames.resize(si+1);
  203. for (int i=0; i<NAMES_PER_TOWN; i++)
  204. {
  205. namess.getline(bufname,50);
  206. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  207. }
  208. si++;
  209. }
  210. tlog5 << "\t\tReading OBJNAMES \n";
  211. buf = bitmaph->getTextFile("OBJNAMES.TXT");
  212. it=0; //hope that -1 will not break this
  213. while (it<buf.length()-1)
  214. {
  215. std::string nobj;
  216. loadToIt(nobj, buf, it, 3);
  217. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  218. {
  219. nobj = nobj.substr(0, nobj.size()-1);
  220. }
  221. names.push_back(nobj);
  222. }
  223. tlog5 << "\t\tReading ADVEVENT \n";
  224. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  225. it=0;
  226. std::string temp;
  227. while (it<buf.length()-1)
  228. {
  229. loadToIt(temp,buf,it,3);
  230. if (temp[0]=='\"')
  231. {
  232. temp = temp.substr(1,temp.length()-2);
  233. }
  234. boost::algorithm::replace_all(temp,"\"\"","\"");
  235. advobtxt.push_back(temp);
  236. }
  237. tlog5 << "\t\tReading XTRAINFO \n";
  238. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  239. it=0;
  240. while (it<buf.length()-1)
  241. {
  242. loadToIt(temp,buf,it,3);
  243. xtrainfo.push_back(temp);
  244. }
  245. tlog5 << "\t\tReading MINENAME \n";
  246. buf = bitmaph->getTextFile("MINENAME.TXT");
  247. it=0;
  248. while (it<buf.length()-1)
  249. {
  250. loadToIt(temp,buf,it,3);
  251. mines.push_back(std::pair<std::string,std::string>(temp,""));
  252. }
  253. tlog5 << "\t\tReading MINEEVNT \n";
  254. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  255. it=0;
  256. i=0;
  257. while (it<buf.length()-1)
  258. {
  259. loadToIt(temp,buf,it,3);
  260. temp = temp.substr(1,temp.length()-2);
  261. if(i < mines.size())
  262. mines[i++].second = temp;
  263. else
  264. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  265. }
  266. tlog5 << "\t\tReading RESTYPES \n";
  267. buf = bitmaph->getTextFile("RESTYPES.TXT");
  268. it=0;
  269. while (it<buf.length()-1)
  270. {
  271. loadToIt(temp,buf,it,3);
  272. restypes.push_back(temp);
  273. }
  274. tlog5 << "\t\tReading TERRNAME \n";
  275. buf = bitmaph->getTextFile("TERRNAME.TXT");
  276. it=0;
  277. while (it<buf.length()-1)
  278. {
  279. loadToIt(temp,buf,it,3);
  280. terrainNames.push_back(temp);
  281. }
  282. tlog5 << "\t\tReading RANDSIGN \n";
  283. buf = bitmaph->getTextFile("RANDSIGN.TXT");
  284. it=0;
  285. while (it<buf.length()-1)
  286. {
  287. loadToIt(temp,buf,it,3);
  288. randsign.push_back(temp);
  289. }
  290. tlog5 << "\t\tReading ZCRGN1 \n";
  291. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  292. it=0;
  293. while (it<buf.length()-1)
  294. {
  295. loadToIt(temp,buf,it,3);
  296. creGens.push_back(temp);
  297. }
  298. tlog5 << "\t\tReading CRGN4 \n";
  299. buf = bitmaph->getTextFile("CRGEN4.TXT");
  300. it=0;
  301. while (it<buf.length()-1)
  302. {
  303. loadToIt(temp,buf,it,3);
  304. creGens4.push_back(temp);
  305. }
  306. buf = bitmaph->getTextFile("GENRLTXT.TXT");
  307. std::string tmp;
  308. andame = buf.size();
  309. i=0; //buf iterator
  310. for(i; i<andame; ++i)
  311. {
  312. if(buf[i]=='\r')
  313. break;
  314. }
  315. i+=2;
  316. std::string buflet;
  317. for(int jj=0; jj<764; ++jj)
  318. {
  319. loadToIt(buflet, buf, i, 2);
  320. trimQuotation(buflet);
  321. boost::algorithm::replace_all(temp,"\"\"","\"");
  322. allTexts.push_back(buflet);
  323. }
  324. std::string stro = bitmaph->getTextFile("Overview.txt");
  325. itr=0;
  326. while(itr<stro.length()-1)
  327. {
  328. loadToIt(tmp, stro, itr, 3);
  329. trimQuotation(tmp);
  330. overview.push_back(tmp);
  331. }
  332. std::string strs = bitmaph->getTextFile("ARRAYTXT.TXT");
  333. itr=0;
  334. while(itr<strs.length()-1)
  335. {
  336. loadToIt(tmp, strs, itr, 3);
  337. trimQuotation(tmp);
  338. arraytxt.push_back(tmp);
  339. }
  340. itr = 0;
  341. std::string strin = bitmaph->getTextFile("PRISKILL.TXT");
  342. for(int hh=0; hh<4; ++hh)
  343. {
  344. loadToIt(tmp, strin, itr, 3);
  345. primarySkillNames.push_back(tmp);
  346. }
  347. itr = 0;
  348. strin = bitmaph->getTextFile("JKTEXT.TXT");
  349. for(int hh=0; hh<45; ++hh)
  350. {
  351. loadToIt(tmp, strin, itr, 3);
  352. jktexts.push_back(tmp);
  353. }
  354. itr = 0;
  355. strin = bitmaph->getTextFile("TVRNINFO.TXT");
  356. for(int hh=0; hh<8; ++hh)
  357. {
  358. loadToIt(tmp, strin, itr, 3);
  359. tavernInfo.push_back(tmp);
  360. }
  361. itr = 0;
  362. strin = bitmaph->getTextFile("HEROSCRN.TXT");
  363. for(int hh=0; hh<33; ++hh)
  364. {
  365. loadToIt(tmp, strin, itr, 3);
  366. heroscrn.push_back(tmp);
  367. }
  368. itr = 0;
  369. strin = bitmaph->getTextFile("ARTEVENT.TXT");
  370. for(; itr<strin.size();)
  371. {
  372. loadToIt(tmp, strin, itr, 2);
  373. // boost::algorithm::trim(tmp);
  374. trimQuotation(tmp);
  375. boost::algorithm::replace_all(temp,"\"\"","\"");
  376. artifEvents.push_back(tmp);
  377. }
  378. buf = bitmaph->getTextFile("SSTRAITS.TXT");
  379. it=0;
  380. for(int i=0; i<2; ++i)
  381. loadToIt(dump,buf,it,3);
  382. skillName.resize(SKILL_QUANTITY);
  383. skillInfoTexts.resize(SKILL_QUANTITY);
  384. for (int i=0; i<SKILL_QUANTITY; i++)
  385. {
  386. skillInfoTexts[i].resize(3);
  387. loadToIt(skillName[i],buf,it,4);
  388. loadToIt(skillInfoTexts[i][0],buf,it,4);
  389. loadToIt(skillInfoTexts[i][1],buf,it,4);
  390. loadToIt(skillInfoTexts[i][2],buf,it,3);
  391. for(int j = 0; j < 3; j++)
  392. trimQuotation(skillInfoTexts[i][j]);
  393. }
  394. buf = bitmaph->getTextFile("SKILLLEV.TXT");
  395. it=0;
  396. for(int i=0; i<6; ++i)
  397. {
  398. std::string buffo;
  399. loadToIt(buffo,buf,it,3);
  400. levels.push_back(buffo);
  401. }
  402. buf = bitmaph->getTextFile ("SEERHUT.TXT");
  403. it = 0;
  404. loadToIt (dump, buf, it, 3);
  405. loadToIt (dump, buf, it, 4); //dump description
  406. seerEmpty.resize(6);
  407. for (i = 0; i < 5; ++i)
  408. {
  409. loadToIt(seerEmpty[i], buf, it, 4);
  410. trimQuotation (seerEmpty[i]);
  411. }
  412. loadToIt (seerEmpty[5], buf, it, 3);
  413. trimQuotation (seerEmpty[5]);
  414. int j,k;
  415. quests.resize(10);
  416. for (i = 0; i < 9; ++i) //9 types of quests
  417. {
  418. quests[i].resize(5);
  419. for (j = 0; j < 5; ++j)
  420. {
  421. loadToIt (dump, buf, it, 4); //front desciption
  422. quests[i][j].resize(6);
  423. for (k = 0; k < 5; ++k)
  424. {
  425. loadToIt (quests[i][j][k], buf, it, 4);
  426. trimQuotation (quests[i][j][k]);
  427. }
  428. loadToIt (quests[i][j][5], buf, it, 3);
  429. trimQuotation (quests[i][j][5]);
  430. }
  431. }
  432. quests[9].resize(1);
  433. quests[9][0].resize(6);
  434. for (k = 0; k < 5; ++k) //Time limit
  435. {
  436. loadToIt (quests[9][0][k], buf, it, 4);
  437. }
  438. loadToIt (quests[9][0][k], buf, it, 3);
  439. for (i = 0; i < 2; ++i) //gap description
  440. loadToIt(dump,buf,it,3);
  441. seerNames.resize(48);
  442. for (i = 0; i < 14; ++i) //additional tabs
  443. {
  444. loadToIt(seerNames[i], buf, it, 4);
  445. loadToIt(dump, buf, it, 3);
  446. }
  447. for (i = 14; i < 48; ++i)
  448. loadToIt(seerNames[i], buf, it, 3);
  449. }
  450. std::string CGeneralTextHandler::getTitle(std::string text)
  451. {
  452. std::string ret;
  453. int i=0;
  454. while ((text[i++]!='{'));
  455. while ((text[i]!='}') && (i<text.length()))
  456. ret+=text[i++];
  457. return ret;
  458. }
  459. std::string CGeneralTextHandler::getDescr(std::string text)
  460. {
  461. std::string ret;
  462. int i=0;
  463. while ((text[i++]!='}'));
  464. i+=2;
  465. while ((text[i]!='"') && (i<text.length()))
  466. ret+=text[i++];
  467. return ret;
  468. }
  469. CGeneralTextHandler::CGeneralTextHandler()
  470. {
  471. }