CBuildingHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <assert.h>
  10. #include <boost/assign/list_of.hpp>
  11. extern CLodHandler * bitmaph;
  12. /*
  13. * CBuildingHandler.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. static unsigned int readNr(std::string &in, int &it)
  22. {
  23. int last=it;
  24. for(;last<in.size();last++)
  25. if(in[last]=='\t' || in[last]=='\n' || in[last]==' ' || in[last]=='\r' || in[last]=='\n')
  26. break;
  27. if(last==in.size())
  28. throw std::string("Cannot read number...");
  29. std::istringstream ss(in.substr(it,last-it));
  30. it+=(1+last-it);
  31. ss >> last;
  32. return last;
  33. }
  34. static CBuilding * readBg(std::string &buf, int& it)
  35. {
  36. CBuilding * nb = new CBuilding();
  37. nb->resources.resize(RESOURCE_QUANTITY);
  38. for(int res=0;res<7;res++)
  39. nb->resources[res] = readNr(buf,it);
  40. /*nb->refName = */readTo(buf,it,'\n');
  41. //reference name is ommitted, it's seems to be useless
  42. return nb;
  43. }
  44. void CBuildingHandler::loadBuildings()
  45. {
  46. std::string buf = bitmaph->getTextFile("BUILDING.TXT"), temp;
  47. int it=0; //buf iterator
  48. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  49. //read 9 special buildings for every faction
  50. buildings.resize(F_NUMBER);
  51. for(int i=0;i<F_NUMBER;i++)
  52. {
  53. temp = readTo(buf,it,'\n');//read blank line and faction name
  54. temp = readTo(buf,it,'\n');
  55. for(int bg = 0; bg<9; bg++)
  56. {
  57. CBuilding *nb = readBg(buf,it);
  58. nb->tid = i;
  59. nb->bid = bg+17;
  60. buildings[i][bg+17] = nb;
  61. }
  62. }
  63. //reading 17 neutral (common) buildings
  64. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//neutral buildings - skip 3 lines
  65. for(int bg = 0; bg<17; bg++)
  66. {
  67. CBuilding *nb = readBg(buf,it);
  68. for(int f=0;f<F_NUMBER;f++)
  69. {
  70. buildings[f][bg] = new CBuilding(*nb);
  71. buildings[f][bg]->tid = f;
  72. buildings[f][bg]->bid = bg;
  73. }
  74. delete nb;
  75. }
  76. //create Grail entries
  77. for(int i=0; i<F_NUMBER; i++)
  78. buildings[i][26] = new CBuilding(i,26);
  79. //reading 14 per faction dwellings
  80. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  81. for(int i=0;i<F_NUMBER;i++)
  82. {
  83. temp = readTo(buf,it,'\n');//read blank line
  84. temp = readTo(buf,it,'\n');// and faction name
  85. for(int bg = 0; bg<14; bg++)
  86. {
  87. CBuilding *nb = readBg(buf,it);
  88. nb->tid = i;
  89. nb->bid = bg+30;
  90. buildings[i][bg+30] = nb;
  91. }
  92. }
  93. /////done reading BUILDING.TXT*****************************
  94. char line[100]; //bufor
  95. std::ifstream ofs(DATA_DIR "/config/hall.txt");
  96. int castles;
  97. ofs>>castles;
  98. for(int i=0;i<castles;i++)
  99. {
  100. int tid;
  101. unsigned int it, box=0;
  102. std::string pom;
  103. ofs >> tid >> pom;
  104. hall[tid].first = pom;
  105. (hall[tid].second).resize(5); //rows
  106. for(int j=0;j<5;j++)
  107. {
  108. box = it = 0;
  109. ofs.getline(line,100);
  110. if(!line[0] || line[0] == '\n' || line[0] == '\r')
  111. ofs.getline(line,100);
  112. std::string linia(line);
  113. bool areboxes=true;
  114. while(areboxes) //read all boxes
  115. {
  116. (hall[tid].second)[j].push_back(std::vector<int>()); //push new box
  117. int seppos = linia.find_first_of('|',it); //position of end of this box data
  118. if(seppos<0)
  119. seppos = linia.length();
  120. while(it<seppos)
  121. {
  122. int last = linia.find_first_of(' ',it);
  123. std::istringstream ss(linia.substr(it,last-it));
  124. it = last + 1;
  125. ss >> last;
  126. (hall[tid].second)[j][box].push_back(last);
  127. areboxes = it; //wyzeruje jak nie znajdzie kolejnej spacji = koniec linii
  128. if(!it)
  129. it = seppos+1;
  130. }
  131. box++;
  132. it+=2;
  133. }
  134. }
  135. }
  136. //loading ERMU to picture
  137. std::ifstream etp(DATA_DIR "/config/ERMU_to_picture.txt");
  138. assert(etp.is_open());
  139. for(int g=0; g<44; ++g)
  140. {
  141. for (int b=0; b<ARRAY_COUNT(ERMUtoPicture); ++b)
  142. {
  143. std::string buf;
  144. etp >> buf;
  145. ERMUtoPicture[b][g] = buf;
  146. }
  147. }
  148. etp.close();
  149. }
  150. CBuildingHandler::~CBuildingHandler()
  151. {
  152. for(std::vector< std::map<int, CBuilding*> >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  153. for(std::map<int, CBuilding*>::iterator j=i->begin(); j!=i->end(); j++)
  154. delete j->second;
  155. }
  156. static std::string emptyStr = "";
  157. const std::string & CBuilding::Name() const
  158. {
  159. if(name.length())
  160. return name;
  161. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  162. return VLC->generaltexth->buildings[tid][bid].first;
  163. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  164. return emptyStr;
  165. }
  166. const std::string & CBuilding::Description() const
  167. {
  168. if(description.length())
  169. return description;
  170. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  171. return VLC->generaltexth->buildings[tid][bid].second;
  172. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  173. return emptyStr;
  174. }
  175. CBuilding::CBuilding( int TID, int BID )
  176. {
  177. tid = TID;
  178. bid = BID;
  179. }
  180. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  181. {
  182. using namespace boost::assign;
  183. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  184. (6)(26)(17)(21)(22)(23)
  185. ; //creature generators with banks - handled separately
  186. if (camp < campToERMU.size())
  187. {
  188. return campToERMU[camp];
  189. }
  190. static const std::vector<int> hordeLvlsPerTType[F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  191. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  192. int curPos = campToERMU.size();
  193. for (int i=0; i<7; ++i)
  194. {
  195. if(camp == curPos) //non-upgraded
  196. return 30 + i;
  197. curPos++;
  198. if(camp == curPos) //upgraded
  199. return 37 + i;
  200. curPos++;
  201. //horde building
  202. if (vstd::contains(hordeLvlsPerTType[townType], i))
  203. {
  204. if (camp == curPos)
  205. {
  206. if (hordeLvlsPerTType[townType][0] == i)
  207. {
  208. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  209. return 19;
  210. else //upgraded dwelling not presents
  211. return 18;
  212. }
  213. else
  214. {
  215. if(hordeLvlsPerTType[townType].size() > 1)
  216. {
  217. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  218. return 25;
  219. else //upgraded dwelling not presents
  220. return 24;
  221. }
  222. }
  223. }
  224. curPos++;
  225. }
  226. }
  227. assert(0);
  228. return -1; //not found
  229. }