CBuildingHandler.cpp 4.9 KB

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