CBuildingHandler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. }
  137. CBuildingHandler::~CBuildingHandler()
  138. {
  139. for(std::vector< bmap<int, ConstTransitivePtr<CBuilding> > >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  140. for(std::map<int, ConstTransitivePtr<CBuilding> >::iterator j=i->begin(); j!=i->end(); j++)
  141. j->second.dellNull();
  142. }
  143. static std::string emptyStr = "";
  144. const std::string & CBuilding::Name() const
  145. {
  146. if(name.length())
  147. return name;
  148. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  149. return VLC->generaltexth->buildings[tid][bid].first;
  150. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  151. return emptyStr;
  152. }
  153. const std::string & CBuilding::Description() const
  154. {
  155. if(description.length())
  156. return description;
  157. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  158. return VLC->generaltexth->buildings[tid][bid].second;
  159. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  160. return emptyStr;
  161. }
  162. CBuilding::CBuilding( int TID, int BID )
  163. {
  164. tid = TID;
  165. bid = BID;
  166. }
  167. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  168. {
  169. using namespace boost::assign;
  170. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  171. (6)(26)(17)(21)(22)(23)
  172. ; //creature generators with banks - handled separately
  173. if (camp < campToERMU.size())
  174. {
  175. return campToERMU[camp];
  176. }
  177. static const std::vector<int> hordeLvlsPerTType[F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  178. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  179. int curPos = campToERMU.size();
  180. for (int i=0; i<7; ++i)
  181. {
  182. if(camp == curPos) //non-upgraded
  183. return 30 + i;
  184. curPos++;
  185. if(camp == curPos) //upgraded
  186. return 37 + i;
  187. curPos++;
  188. //horde building
  189. if (vstd::contains(hordeLvlsPerTType[townType], i))
  190. {
  191. if (camp == curPos)
  192. {
  193. if (hordeLvlsPerTType[townType][0] == i)
  194. {
  195. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  196. return 19;
  197. else //upgraded dwelling not presents
  198. return 18;
  199. }
  200. else
  201. {
  202. if(hordeLvlsPerTType[townType].size() > 1)
  203. {
  204. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  205. return 25;
  206. else //upgraded dwelling not presents
  207. return 24;
  208. }
  209. }
  210. }
  211. curPos++;
  212. }
  213. }
  214. assert(0);
  215. return -1; //not found
  216. }