CGeneralTextHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. std::istringstream ins, namess;
  186. ins.str(bitmaph->getTextFile("TOWNTYPE.TXT"));
  187. namess.str(bitmaph->getTextFile("TOWNNAME.TXT"));
  188. int si=0;
  189. char bufname[75];
  190. while (!ins.eof())
  191. {
  192. ins.getline(bufname,50);
  193. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  194. townNames.resize(si+1);
  195. for (int i=0; i<NAMES_PER_TOWN; i++)
  196. {
  197. namess.getline(bufname,50);
  198. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  199. }
  200. si++;
  201. }
  202. tlog5 << "\t\tReading OBJNAMES \n";
  203. buf = bitmaph->getTextFile("OBJNAMES.TXT");
  204. it=0; //hope that -1 will not break this
  205. while (it<buf.length()-1)
  206. {
  207. std::string nobj;
  208. loadToIt(nobj, buf, it, 3);
  209. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  210. {
  211. nobj = nobj.substr(0, nobj.size()-1);
  212. }
  213. names.push_back(nobj);
  214. }
  215. tlog5 << "\t\tReading ADVEVENT \n";
  216. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  217. it=0;
  218. std::string temp;
  219. while (it<buf.length()-1)
  220. {
  221. loadToIt(temp,buf,it,3);
  222. if (temp[0]=='\"')
  223. {
  224. temp = temp.substr(1,temp.length()-2);
  225. }
  226. boost::algorithm::replace_all(temp,"\"\"","\"");
  227. advobtxt.push_back(temp);
  228. }
  229. tlog5 << "\t\tReading XTRAINFO \n";
  230. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  231. it=0;
  232. while (it<buf.length()-1)
  233. {
  234. loadToIt(temp,buf,it,3);
  235. xtrainfo.push_back(temp);
  236. }
  237. tlog5 << "\t\tReading MINENAME \n";
  238. buf = bitmaph->getTextFile("MINENAME.TXT");
  239. it=0;
  240. while (it<buf.length()-1)
  241. {
  242. loadToIt(temp,buf,it,3);
  243. mines.push_back(std::pair<std::string,std::string>(temp,""));
  244. }
  245. tlog5 << "\t\tReading MINEEVNT \n";
  246. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  247. it=0;
  248. i=0;
  249. while (it<buf.length()-1)
  250. {
  251. loadToIt(temp,buf,it,3);
  252. temp = temp.substr(1,temp.length()-2);
  253. if(i < mines.size())
  254. mines[i++].second = temp;
  255. else
  256. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  257. }
  258. tlog5 << "\t\tReading RESTYPES \n";
  259. buf = bitmaph->getTextFile("RESTYPES.TXT");
  260. it=0;
  261. while (it<buf.length()-1)
  262. {
  263. loadToIt(temp,buf,it,3);
  264. restypes.push_back(temp);
  265. }
  266. tlog5 << "\t\tReading RANDSIGN \n";
  267. buf = bitmaph->getTextFile("RANDSIGN.TXT");
  268. it=0;
  269. while (it<buf.length()-1)
  270. {
  271. loadToIt(temp,buf,it,3);
  272. randsign.push_back(temp);
  273. }
  274. tlog5 << "\t\tReading ZCRGN1 \n";
  275. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  276. it=0;
  277. while (it<buf.length()-1)
  278. {
  279. loadToIt(temp,buf,it,3);
  280. creGens.push_back(temp);
  281. }
  282. buf = bitmaph->getTextFile("GENRLTXT.TXT");
  283. std::string tmp;
  284. andame = buf.size();
  285. i=0; //buf iterator
  286. for(i; i<andame; ++i)
  287. {
  288. if(buf[i]=='\r')
  289. break;
  290. }
  291. i+=2;
  292. std::string buflet;
  293. for(int jj=0; jj<764; ++jj)
  294. {
  295. loadToIt(buflet, buf, i, 2);
  296. if(buflet[0] == '"' && buflet[buflet.size()-1] == '"')
  297. buflet = buflet.substr(1,buflet.size()-2);
  298. allTexts.push_back(buflet);
  299. }
  300. std::string strs = bitmaph->getTextFile("ARRAYTXT.TXT");
  301. itr=0;
  302. while(itr<strs.length()-1)
  303. {
  304. loadToIt(tmp, strs, itr, 3);
  305. if(tmp[0] == '"' && tmp[tmp.size()-1] == '"')
  306. tmp = tmp.substr(1,tmp.size()-2);
  307. arraytxt.push_back(tmp);
  308. }
  309. itr = 0;
  310. std::string strin = bitmaph->getTextFile("PRISKILL.TXT");
  311. for(int hh=0; hh<4; ++hh)
  312. {
  313. loadToIt(tmp, strin, itr, 3);
  314. primarySkillNames.push_back(tmp);
  315. }
  316. itr = 0;
  317. strin = bitmaph->getTextFile("JKTEXT.TXT");
  318. for(int hh=0; hh<45; ++hh)
  319. {
  320. loadToIt(tmp, strin, itr, 3);
  321. jktexts.push_back(tmp);
  322. }
  323. itr = 0;
  324. strin = bitmaph->getTextFile("TVRNINFO.TXT");
  325. for(int hh=0; hh<8; ++hh)
  326. {
  327. loadToIt(tmp, strin, itr, 3);
  328. tavernInfo.push_back(tmp);
  329. }
  330. itr = 0;
  331. strin = bitmaph->getTextFile("HEROSCRN.TXT");
  332. for(int hh=0; hh<33; ++hh)
  333. {
  334. loadToIt(tmp, strin, itr, 3);
  335. heroscrn.push_back(tmp);
  336. }
  337. strin = bitmaph->getTextFile("ARTEVENT.TXT");
  338. for(itr = 0; itr<strin.size();itr++)
  339. {
  340. loadToIt(tmp, strin, itr, 3);
  341. artifEvents.push_back(tmp);
  342. }
  343. buf = bitmaph->getTextFile("SSTRAITS.TXT");
  344. it=0;
  345. for(int i=0; i<2; ++i)
  346. loadToIt(dump,buf,it,3);
  347. skillName.resize(SKILL_QUANTITY);
  348. skillInfoTexts.resize(SKILL_QUANTITY);
  349. for (int i=0; i<SKILL_QUANTITY; i++)
  350. {
  351. skillInfoTexts[i].resize(3);
  352. loadToIt(skillName[i],buf,it,4);
  353. loadToIt(skillInfoTexts[i][0],buf,it,4);
  354. loadToIt(skillInfoTexts[i][1],buf,it,4);
  355. loadToIt(skillInfoTexts[i][2],buf,it,3);
  356. for(int j = 0; j < 3; j++)
  357. trimQuotation(skillInfoTexts[i][j]);
  358. }
  359. buf = bitmaph->getTextFile("SKILLLEV.TXT");
  360. it=0;
  361. for(int i=0; i<6; ++i)
  362. {
  363. std::string buffo;
  364. loadToIt(buffo,buf,it,3);
  365. levels.push_back(buffo);
  366. }
  367. }
  368. std::string CGeneralTextHandler::getTitle(std::string text)
  369. {
  370. std::string ret;
  371. int i=0;
  372. while ((text[i++]!='{'));
  373. while ((text[i]!='}') && (i<text.length()))
  374. ret+=text[i++];
  375. return ret;
  376. }
  377. std::string CGeneralTextHandler::getDescr(std::string text)
  378. {
  379. std::string ret;
  380. int i=0;
  381. while ((text[i++]!='}'));
  382. i+=2;
  383. while ((text[i]!='"') && (i<text.length()))
  384. ret+=text[i++];
  385. return ret;
  386. }
  387. CGeneralTextHandler::CGeneralTextHandler()
  388. {
  389. }