CBuildingHandler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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, unsigned 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, unsigned 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(), it=0; //buf iterator
  41. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  42. //read 9 special buildings for every faction
  43. for(int i=0;i<F_NUMBER;i++)
  44. {
  45. temp = readTo(buf,it,'\n');//read blank line and faction name
  46. temp = readTo(buf,it,'\n');
  47. for(int bg = 0; bg<9; bg++)
  48. {
  49. CBuilding *nb = readBg(buf,it);
  50. nb->tid = i;
  51. nb->bid = bg+17;
  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. {
  62. buildings[f][bg] = new CBuilding(*nb);
  63. buildings[f][bg]->tid = f;
  64. buildings[f][bg]->bid = bg;
  65. }
  66. delete nb;
  67. }
  68. //reading 14 per faction dwellings
  69. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  70. for(int i=0;i<F_NUMBER;i++)
  71. {
  72. temp = readTo(buf,it,'\n');//read blank line
  73. temp = readTo(buf,it,'\n');// and faction name
  74. for(int bg = 0; bg<14; bg++)
  75. {
  76. CBuilding *nb = readBg(buf,it);
  77. nb->tid = i;
  78. nb->bid = bg+30;
  79. buildings[i][bg+30] = nb;
  80. }
  81. }
  82. /////done reading BUILDING.TXT*****************************
  83. char line[100]; //bufor
  84. std::ifstream ofs("config/hall.txt");
  85. int castles;
  86. ofs>>castles;
  87. for(int i=0;i<castles;i++)
  88. {
  89. int tid;
  90. unsigned int it, box=0;
  91. std::string pom;
  92. ofs >> tid >> pom;
  93. hall[tid].first = pom;
  94. (hall[tid].second).resize(5); //rows
  95. for(int j=0;j<5;j++)
  96. {
  97. box = it = 0;
  98. ofs.getline(line,100);
  99. if(!line[0])
  100. ofs.getline(line,100);
  101. std::string linia(line);
  102. bool areboxes=true;
  103. while(areboxes) //read all boxes
  104. {
  105. (hall[tid].second)[j].push_back(std::vector<int>()); //push new box
  106. int seppos = linia.find_first_of('|',it); //position of end of this box data
  107. if(seppos<0)
  108. seppos = linia.length();
  109. while(it<seppos)
  110. {
  111. int last = linia.find_first_of(' ',it);
  112. std::stringstream ss(linia.substr(it,last-it));
  113. it = last + 1;
  114. ss >> last;
  115. (hall[tid].second)[j][box].push_back(last);
  116. areboxes = it; //wyzeruje jak nie znajdzie kolejnej spacji = koniec linii
  117. if(!it)
  118. it = seppos+1;
  119. }
  120. box++;
  121. it+=2;
  122. }
  123. }
  124. }
  125. }
  126. const std::string & CBuilding::Name()
  127. {
  128. if(name.length())
  129. return name;
  130. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  131. return VLC->generaltexth->buildings[tid][bid].first;
  132. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  133. return "";
  134. }
  135. const std::string & CBuilding::Description()
  136. {
  137. if(description.length())
  138. return description;
  139. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  140. return VLC->generaltexth->buildings[tid][bid].second;
  141. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  142. return "";
  143. }