CBuildingHandler.cpp 6.4 KB

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