CBuildingHandler.cpp 4.4 KB

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