CBuildingHandler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CBuildingHandler.h"
  4. #include "CGeneralTextHandler.h"
  5. #include "CLodHandler.h"
  6. #include "../lib/VCMI_Lib.h"
  7. #include <sstream>
  8. #include <fstream>
  9. extern CLodHandler * bitmaph;
  10. unsigned int readNr(std::string &in, int &it)
  11. {
  12. int last=it;
  13. for(;last<in.size();last++)
  14. if(in[last]=='\t' || in[last]=='\n' || in[last]==' ' || in[last]=='\r' || in[last]=='\n')
  15. break;
  16. if(last==in.size())
  17. #ifndef __GNUC__
  18. throw new std::exception("Cannot read number...");
  19. #else
  20. throw new std::exception();
  21. #endif
  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, int& it)
  28. {
  29. CBuilding * nb = new CBuilding();
  30. nb->resources.resize(RESOURCE_QUANTITY);
  31. for(int res=0;res<7;res++)
  32. nb->resources[res] = readNr(buf,it);
  33. /*nb->refName = */readTo(buf,it,'\n');
  34. //reference name is ommitted, it's seems to be useless
  35. return nb;
  36. }
  37. void CBuildingHandler::loadBuildings()
  38. {
  39. std::string buf = bitmaph->getTextFile("BUILDING.TXT"), temp;
  40. unsigned int andame = buf.size();
  41. int it=0; //buf iterator
  42. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  43. //read 9 special buildings for every faction
  44. for(int i=0;i<F_NUMBER;i++)
  45. {
  46. temp = readTo(buf,it,'\n');//read blank line and faction name
  47. temp = readTo(buf,it,'\n');
  48. for(int bg = 0; bg<9; bg++)
  49. {
  50. CBuilding *nb = readBg(buf,it);
  51. nb->tid = i;
  52. nb->bid = bg+17;
  53. buildings[i][bg+17] = nb;
  54. }
  55. }
  56. //reading 17 neutral (common) buildings
  57. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//neutral buildings - skip 3 lines
  58. for(int bg = 0; bg<17; bg++)
  59. {
  60. CBuilding *nb = readBg(buf,it);
  61. for(int f=0;f<F_NUMBER;f++)
  62. {
  63. buildings[f][bg] = new CBuilding(*nb);
  64. buildings[f][bg]->tid = f;
  65. buildings[f][bg]->bid = bg;
  66. }
  67. delete nb;
  68. }
  69. //reading 14 per faction dwellings
  70. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  71. for(int i=0;i<F_NUMBER;i++)
  72. {
  73. temp = readTo(buf,it,'\n');//read blank line
  74. temp = readTo(buf,it,'\n');// and faction name
  75. for(int bg = 0; bg<14; bg++)
  76. {
  77. CBuilding *nb = readBg(buf,it);
  78. nb->tid = i;
  79. nb->bid = bg+30;
  80. buildings[i][bg+30] = nb;
  81. }
  82. }
  83. /////done reading BUILDING.TXT*****************************
  84. char line[100]; //bufor
  85. std::ifstream ofs("config/hall.txt");
  86. int castles;
  87. ofs>>castles;
  88. for(int i=0;i<castles;i++)
  89. {
  90. int tid;
  91. unsigned int it, box=0;
  92. std::string pom;
  93. ofs >> tid >> pom;
  94. hall[tid].first = pom;
  95. (hall[tid].second).resize(5); //rows
  96. for(int j=0;j<5;j++)
  97. {
  98. box = it = 0;
  99. ofs.getline(line,100);
  100. if(!line[0])
  101. ofs.getline(line,100);
  102. std::string linia(line);
  103. bool areboxes=true;
  104. while(areboxes) //read all boxes
  105. {
  106. (hall[tid].second)[j].push_back(std::vector<int>()); //push new box
  107. int seppos = linia.find_first_of('|',it); //position of end of this box data
  108. if(seppos<0)
  109. seppos = linia.length();
  110. while(it<seppos)
  111. {
  112. int last = linia.find_first_of(' ',it);
  113. std::stringstream ss(linia.substr(it,last-it));
  114. it = last + 1;
  115. ss >> last;
  116. (hall[tid].second)[j][box].push_back(last);
  117. areboxes = it; //wyzeruje jak nie znajdzie kolejnej spacji = koniec linii
  118. if(!it)
  119. it = seppos+1;
  120. }
  121. box++;
  122. it+=2;
  123. }
  124. }
  125. }
  126. }
  127. const std::string & CBuilding::Name()
  128. {
  129. if(name.length())
  130. return name;
  131. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  132. return VLC->generaltexth->buildings[tid][bid].first;
  133. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  134. return "";
  135. }
  136. const std::string & CBuilding::Description()
  137. {
  138. if(description.length())
  139. return description;
  140. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  141. return VLC->generaltexth->buildings[tid][bid].second;
  142. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  143. return "";
  144. }