CBuildingHandler.cpp 5.1 KB

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