CGeneralTextHandler.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 CGeneralTextHandler::load()
  27. {
  28. std::string buf1 = bitmaph->getTextFile("ZELP.TXT");
  29. int itr=0, eol=-1, eolnext=-1, pom;
  30. eolnext = buf1.find_first_of('\r',itr);
  31. while(itr<buf1.size())
  32. {
  33. eol = eolnext; //end of this line
  34. eolnext = buf1.find_first_of('\r',eol+1); //end of the next line
  35. pom=buf1.find_first_of('\t',itr); //upcoming tab
  36. if(eol<0 || pom<0)
  37. break;
  38. if(pom>eol) //in current line there is not tab
  39. zelp.push_back(std::pair<std::string,std::string>());
  40. else
  41. {
  42. zelp.push_back
  43. (std::pair<std::string,std::string>
  44. (buf1.substr(itr,pom-itr),
  45. buf1.substr(pom+1,eol-pom-1)));
  46. boost::algorithm::replace_all(zelp[zelp.size()-1].first,"\t","");
  47. boost::algorithm::replace_all(zelp[zelp.size()-1].second,"\t","");
  48. }
  49. itr=eol+2;
  50. }
  51. std::string buf = bitmaph->getTextFile("VCDESC.TXT");
  52. int andame = buf.size();
  53. int i=0; //buf iterator
  54. for(int gg=0; gg<14; ++gg)
  55. {
  56. int befi=i;
  57. for(i; i<andame; ++i)
  58. {
  59. if(buf[i]=='\r')
  60. break;
  61. }
  62. victoryConditions[gg] = buf.substr(befi, i-befi);
  63. i+=2;
  64. }
  65. buf = bitmaph->getTextFile("LCDESC.TXT");
  66. andame = buf.size();
  67. i=0; //buf iterator
  68. for(int gg=0; gg<4; ++gg)
  69. {
  70. int befi=i;
  71. for(i; i<andame; ++i)
  72. {
  73. if(buf[i]=='\r')
  74. break;
  75. }
  76. lossCondtions[gg] = buf.substr(befi, i-befi);
  77. i+=2;
  78. }
  79. hTxts.resize(HEROES_QUANTITY);
  80. buf = bitmaph->getTextFile("HEROSPEC.TXT");
  81. i=0;
  82. std::string dump;
  83. for(int iii=0; iii<2; ++iii)
  84. {
  85. loadToIt(dump,buf,i,3);
  86. }
  87. for (int iii=0;iii<hTxts.size();iii++)
  88. {
  89. loadToIt(hTxts[iii].bonusName,buf,i,4);
  90. loadToIt(hTxts[iii].shortBonus,buf,i,4);
  91. loadToIt(hTxts[iii].longBonus,buf,i,3);
  92. }
  93. buf = bitmaph->getTextFile("HEROBIOS.TXT");
  94. i=0;
  95. for (int iii=0;iii<hTxts.size();iii++)
  96. {
  97. loadToIt(hTxts[iii].biography,buf,i,3);
  98. }
  99. int it;
  100. buf = bitmaph->getTextFile("BLDGNEUT.TXT");
  101. andame = buf.size(), it=0;
  102. for(int b=0;b<15;b++)
  103. {
  104. std::string name = readTo(buf,it,'\t'),
  105. description = readTo(buf,it,'\n');
  106. for(int fi=0;fi<F_NUMBER;fi++)
  107. {
  108. buildings[fi][b].first = name;
  109. buildings[fi][b].second = description;
  110. }
  111. }
  112. buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  113. //shipyard with the ship
  114. std::string name = readTo(buf,it,'\t'),
  115. description = readTo(buf,it,'\n');
  116. for(int fi=0;fi<F_NUMBER;fi++)
  117. {
  118. buildings[fi][20].first = name;
  119. buildings[fi][20].second = description;
  120. }
  121. for(int fi=0;fi<F_NUMBER;fi++)
  122. {
  123. buildings[fi][16].first = readTo(buf,it,'\t'),
  124. buildings[fi][16].second = readTo(buf,it,'\n');
  125. }
  126. /////done reading "BLDGNEUT.TXT"******************************
  127. buf = bitmaph->getTextFile("BLDGSPEC.TXT");
  128. andame = buf.size(), it=0;
  129. for(int f=0;f<F_NUMBER;f++)
  130. {
  131. for(int b=0;b<9;b++)
  132. {
  133. buildings[f][17+b].first = readTo(buf,it,'\t');
  134. buildings[f][17+b].second = readTo(buf,it,'\n');
  135. }
  136. buildings[f][26].first = readTo(buf,it,'\t');
  137. buildings[f][26].second = readTo(buf,it,'\n');
  138. buildings[f][15].first = readTo(buf,it,'\t'); //resource silo
  139. buildings[f][15].second = readTo(buf,it,'\n');//resource silo
  140. }
  141. /////done reading BLDGSPEC.TXT*********************************
  142. buf = bitmaph->getTextFile("DWELLING.TXT");
  143. andame = buf.size(), it=0;
  144. for(int f=0;f<F_NUMBER;f++)
  145. {
  146. for(int b=0;b<14;b++)
  147. {
  148. buildings[f][30+b].first = readTo(buf,it,'\t');
  149. buildings[f][30+b].second = readTo(buf,it,'\n');
  150. }
  151. }
  152. buf = bitmaph->getTextFile("TCOMMAND.TXT");
  153. itr=0;
  154. while(itr<buf.length()-1)
  155. {
  156. std::string tmp;
  157. loadToIt(tmp, buf, itr, 3);
  158. tcommands.push_back(tmp);
  159. }
  160. buf = bitmaph->getTextFile("HALLINFO.TXT");
  161. itr=0;
  162. while(itr<buf.length()-1)
  163. {
  164. std::string tmp;
  165. loadToIt(tmp, buf, itr, 3);
  166. hcommands.push_back(tmp);
  167. }
  168. std::istringstream ins, namess;
  169. ins.str(bitmaph->getTextFile("TOWNTYPE.TXT"));
  170. namess.str(bitmaph->getTextFile("TOWNNAME.TXT"));
  171. int si=0;
  172. char bufname[75];
  173. while (!ins.eof())
  174. {
  175. ins.getline(bufname,50);
  176. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  177. townNames.resize(si+1);
  178. for (int i=0; i<NAMES_PER_TOWN; i++)
  179. {
  180. namess.getline(bufname,50);
  181. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  182. }
  183. si++;
  184. }
  185. tlog5 << "\t\tReading OBJNAMES \n";
  186. buf = bitmaph->getTextFile("OBJNAMES.TXT");
  187. it=0; //hope that -1 will not break this
  188. while (it<buf.length()-1)
  189. {
  190. std::string nobj;
  191. loadToIt(nobj, buf, it, 3);
  192. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  193. {
  194. nobj = nobj.substr(0, nobj.size()-1);
  195. }
  196. names.push_back(nobj);
  197. }
  198. tlog5 << "\t\tReading ADVEVENT \n";
  199. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  200. it=0;
  201. std::string temp;
  202. while (it<buf.length()-1)
  203. {
  204. loadToIt(temp,buf,it,3);
  205. if (temp[0]=='\"')
  206. {
  207. temp = temp.substr(1,temp.length()-2);
  208. }
  209. boost::algorithm::replace_all(temp,"\"\"","\"");
  210. advobtxt.push_back(temp);
  211. }
  212. tlog5 << "\t\tReading XTRAINFO \n";
  213. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  214. it=0;
  215. while (it<buf.length()-1)
  216. {
  217. loadToIt(temp,buf,it,3);
  218. xtrainfo.push_back(temp);
  219. }
  220. tlog5 << "\t\tReading MINENAME \n";
  221. buf = bitmaph->getTextFile("MINENAME.TXT");
  222. it=0;
  223. while (it<buf.length()-1)
  224. {
  225. loadToIt(temp,buf,it,3);
  226. mines.push_back(std::pair<std::string,std::string>(temp,""));
  227. }
  228. tlog5 << "\t\tReading MINEEVNT \n";
  229. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  230. it=0;
  231. i=0;
  232. while (it<buf.length()-1)
  233. {
  234. loadToIt(temp,buf,it,3);
  235. temp = temp.substr(1,temp.length()-2);
  236. if(i < mines.size())
  237. mines[i++].second = temp;
  238. else
  239. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  240. }
  241. tlog5 << "\t\tReading RESTYPES \n";
  242. buf = bitmaph->getTextFile("RESTYPES.TXT");
  243. it=0;
  244. while (it<buf.length()-1)
  245. {
  246. loadToIt(temp,buf,it,3);
  247. restypes.push_back(temp);
  248. }
  249. tlog5 << "\t\tReading RANDSIGN \n";
  250. buf = bitmaph->getTextFile("RANDSIGN.TXT");
  251. it=0;
  252. while (it<buf.length()-1)
  253. {
  254. loadToIt(temp,buf,it,3);
  255. randsign.push_back(temp);
  256. }
  257. tlog5 << "\t\tReading ZCRGN1 \n";
  258. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  259. it=0;
  260. while (it<buf.length()-1)
  261. {
  262. loadToIt(temp,buf,it,3);
  263. creGens.push_back(temp);
  264. }
  265. buf = bitmaph->getTextFile("GENRLTXT.TXT");
  266. std::string tmp;
  267. andame = buf.size();
  268. i=0; //buf iterator
  269. for(i; i<andame; ++i)
  270. {
  271. if(buf[i]=='\r')
  272. break;
  273. }
  274. i+=2;
  275. std::string buflet;
  276. for(int jj=0; jj<764; ++jj)
  277. {
  278. loadToIt(buflet, buf, i, 2);
  279. if(buflet[0] == '"' && buflet[buflet.size()-1] == '"')
  280. buflet = buflet.substr(1,buflet.size()-2);
  281. allTexts.push_back(buflet);
  282. }
  283. std::string strs = bitmaph->getTextFile("ARRAYTXT.TXT");
  284. itr=0;
  285. while(itr<strs.length()-1)
  286. {
  287. loadToIt(tmp, strs, itr, 3);
  288. if(tmp[0] == '"' && tmp[tmp.size()-1] == '"')
  289. tmp = tmp.substr(1,tmp.size()-2);
  290. arraytxt.push_back(tmp);
  291. }
  292. itr = 0;
  293. std::string strin = bitmaph->getTextFile("PRISKILL.TXT");
  294. for(int hh=0; hh<4; ++hh)
  295. {
  296. loadToIt(tmp, strin, itr, 3);
  297. primarySkillNames.push_back(tmp);
  298. }
  299. itr = 0;
  300. strin = bitmaph->getTextFile("JKTEXT.TXT");
  301. for(int hh=0; hh<45; ++hh)
  302. {
  303. loadToIt(tmp, strin, itr, 3);
  304. jktexts.push_back(tmp);
  305. }
  306. itr = 0;
  307. strin = bitmaph->getTextFile("TVRNINFO.TXT");
  308. for(int hh=0; hh<8; ++hh)
  309. {
  310. loadToIt(tmp, strin, itr, 3);
  311. tavernInfo.push_back(tmp);
  312. }
  313. itr = 0;
  314. strin = bitmaph->getTextFile("HEROSCRN.TXT");
  315. for(int hh=0; hh<33; ++hh)
  316. {
  317. loadToIt(tmp, strin, itr, 3);
  318. heroscrn.push_back(tmp);
  319. }
  320. strin = bitmaph->getTextFile("ARTEVENT.TXT");
  321. for(itr = 0; itr<strin.size();itr++)
  322. {
  323. loadToIt(tmp, strin, itr, 3);
  324. artifEvents.push_back(tmp);
  325. }
  326. buf = bitmaph->getTextFile("SSTRAITS.TXT");
  327. it=0;
  328. for(int i=0; i<2; ++i)
  329. loadToIt(dump,buf,it,3);
  330. skillName.resize(SKILL_QUANTITY);
  331. skillInfoTexts.resize(SKILL_QUANTITY);
  332. for (int i=0; i<SKILL_QUANTITY; i++)
  333. {
  334. skillInfoTexts[i].resize(3);
  335. loadToIt(skillName[i],buf,it,4);
  336. loadToIt(skillInfoTexts[i][0],buf,it,4);
  337. loadToIt(skillInfoTexts[i][1],buf,it,4);
  338. loadToIt(skillInfoTexts[i][2],buf,it,3);
  339. }
  340. buf = bitmaph->getTextFile("SKILLLEV.TXT");
  341. it=0;
  342. for(int i=0; i<6; ++i)
  343. {
  344. std::string buffo;
  345. loadToIt(buffo,buf,it,3);
  346. levels.push_back(buffo);
  347. }
  348. }
  349. std::string CGeneralTextHandler::getTitle(std::string text)
  350. {
  351. std::string ret;
  352. int i=0;
  353. while ((text[i++]!='{'));
  354. while ((text[i]!='}') && (i<text.length()))
  355. ret+=text[i++];
  356. return ret;
  357. }
  358. std::string CGeneralTextHandler::getDescr(std::string text)
  359. {
  360. std::string ret;
  361. int i=0;
  362. while ((text[i++]!='}'));
  363. i+=2;
  364. while ((text[i]!='"') && (i<text.length()))
  365. ret+=text[i++];
  366. return ret;
  367. }
  368. CGeneralTextHandler::CGeneralTextHandler()
  369. {
  370. }