CBuildingHandler.cpp 5.0 KB

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