CBuildingHandler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CBuildingHandler.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. #include <fstream>
  7. extern CLodHandler * bitmaph;
  8. std::string readTo(std::string &in, unsigned int &it, char end)
  9. {
  10. int pom = it;
  11. int last = in.find_first_of(end,it);
  12. it+=(1+last-it);
  13. return in.substr(pom,last-pom);
  14. }
  15. unsigned int readNr(std::string &in, unsigned int &it)
  16. {
  17. int last=it;
  18. for(;last<in.size();last++)
  19. if(in[last]=='\t' || in[last]=='\n' || in[last]==' ' || in[last]=='\r' || in[last]=='\n')
  20. break;
  21. if(last==in.size())
  22. #ifndef __GNUC__
  23. throw new std::exception("Cannot read number...");
  24. #else
  25. throw new std::exception();
  26. #endif
  27. std::stringstream ss(in.substr(it,last-it));
  28. it+=(1+last-it);
  29. ss >> last;
  30. return last;
  31. }
  32. CBuilding * readBg(std::string &buf, unsigned int& it)
  33. {
  34. CBuilding * nb = new CBuilding();
  35. nb->resources.resize(RESOURCE_QUANTITY);
  36. for(int res=0;res<7;res++)
  37. nb->resources[res] = readNr(buf,it);
  38. nb->refName = readTo(buf,it,'\n');
  39. return nb;
  40. }
  41. void CBuildingHandler::loadBuildings()
  42. {
  43. std::string buf = bitmaph->getTextFile("BUILDING.TXT"), temp;
  44. unsigned int andame = buf.size(), it=0; //buf iterator
  45. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  46. //read 9 special buildings for every faction
  47. for(int i=0;i<F_NUMBER;i++)
  48. {
  49. temp = readTo(buf,it,'\n');//read blank line and faction name
  50. temp = readTo(buf,it,'\n');
  51. for(int bg = 0; bg<9; bg++)
  52. {
  53. CBuilding *nb = readBg(buf,it);
  54. buildings[i][bg+17] = nb;
  55. }
  56. }
  57. //reading 17 neutral (common) buildings
  58. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//neutral buildings - skip 3 lines
  59. for(int bg = 0; bg<17; bg++)
  60. {
  61. CBuilding *nb = readBg(buf,it);
  62. for(int f=0;f<F_NUMBER;f++)
  63. buildings[f][bg] = new CBuilding(*nb);
  64. delete nb;
  65. }
  66. //reading 14 per faction dwellings
  67. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  68. for(int i=0;i<F_NUMBER;i++)
  69. {
  70. temp = readTo(buf,it,'\n');//read blank line
  71. temp = readTo(buf,it,'\n');// and faction name
  72. for(int bg = 0; bg<14; bg++)
  73. {
  74. CBuilding *nb = readBg(buf,it);
  75. buildings[i][bg+30] = nb;
  76. }
  77. }
  78. /////done reading BUILDING.TXT*****************************
  79. buf = bitmaph->getTextFile("BLDGNEUT.TXT");
  80. andame = buf.size(), it=0;
  81. for(int b=0;b<15;b++)
  82. {
  83. std::string name = readTo(buf,it,'\t'),
  84. description = readTo(buf,it,'\n');
  85. for(int fi=0;fi<F_NUMBER;fi++)
  86. {
  87. buildings[fi][b]->name = name;
  88. buildings[fi][b]->description = description;
  89. }
  90. }
  91. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  92. //shipyard with the ship
  93. std::string name = readTo(buf,it,'\t'),
  94. description = readTo(buf,it,'\n');
  95. for(int fi=0;fi<F_NUMBER;fi++)
  96. {
  97. buildings[fi][20]->name = name;
  98. buildings[fi][20]->description = description;
  99. }
  100. for(int fi=0;fi<F_NUMBER;fi++)
  101. {
  102. buildings[fi][16]->name = readTo(buf,it,'\t'),
  103. buildings[fi][16]->description = readTo(buf,it,'\n');
  104. }
  105. /////done reading "BLDGNEUT.TXT"******************************
  106. buf = bitmaph->getTextFile("BLDGSPEC.TXT");
  107. andame = buf.size(), it=0;
  108. for(int f=0;f<F_NUMBER;f++)
  109. {
  110. for(int b=0;b<9;b++)
  111. {
  112. buildings[f][17+b]->name = readTo(buf,it,'\t');
  113. buildings[f][17+b]->description = readTo(buf,it,'\n');
  114. }
  115. buildings[f][26] = new CBuilding();//grail
  116. buildings[f][26]->name = readTo(buf,it,'\t');
  117. buildings[f][26]->description = readTo(buf,it,'\n');
  118. buildings[f][15]->name = readTo(buf,it,'\t'); //resource silo
  119. buildings[f][15]->description = readTo(buf,it,'\n');//resource silo
  120. }
  121. /////done reading BLDGSPEC.TXT*********************************
  122. buf = bitmaph->getTextFile("DWELLING.TXT");
  123. andame = buf.size(), it=0;
  124. for(int f=0;f<F_NUMBER;f++)
  125. {
  126. for(int b=0;b<14;b++)
  127. {
  128. buildings[f][30+b]->name = readTo(buf,it,'\t');
  129. buildings[f][30+b]->description = readTo(buf,it,'\n');
  130. }
  131. }
  132. char line[100]; //bufor
  133. std::ifstream ofs("config/hall.txt");
  134. int castles;
  135. ofs>>castles;
  136. for(int i=0;i<castles;i++)
  137. {
  138. int tid;
  139. unsigned int it, box=0;
  140. std::string pom;
  141. ofs >> tid >> pom;
  142. hall[tid].first = pom;
  143. (hall[tid].second).resize(5); //rows
  144. for(int j=0;j<5;j++)
  145. {
  146. box = it = 0;
  147. ofs.getline(line,100);
  148. if(!line[0])
  149. ofs.getline(line,100);
  150. std::string linia(line);
  151. bool areboxes=true;
  152. while(areboxes) //read all boxes
  153. {
  154. (hall[tid].second)[j].push_back(std::vector<int>()); //push new box
  155. int seppos = linia.find_first_of('|',it); //position of end of this box data
  156. if(seppos<0)
  157. seppos = linia.length();
  158. while(it<seppos)
  159. {
  160. int last = linia.find_first_of(' ',it);
  161. std::stringstream ss(linia.substr(it,last-it));
  162. it = last + 1;
  163. ss >> last;
  164. (hall[tid].second)[j][box].push_back(last);
  165. areboxes = it; //wyzeruje jak nie znajdzie kolejnej spacji = koniec linii
  166. if(!it)
  167. it = seppos+1;
  168. }
  169. box++;
  170. it+=2;
  171. }
  172. }
  173. }
  174. }