CBuildingHandler.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "StdInc.h"
  2. #include "CBuildingHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "CLodHandler.h"
  5. #include "../lib/Filesystem/CResourceLoader.h"
  6. #include "../lib/VCMI_Lib.h"
  7. #include "../lib/JsonNode.h"
  8. #include "GameConstants.h"
  9. /*
  10. * CBuildingHandler.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. static ui32 readNr(std::string &in, int &it)
  19. {
  20. int last=it;
  21. for(;last<in.size();last++)
  22. if(in[last]=='\t' || in[last]=='\n' || in[last]==' ' || in[last]=='\r' || in[last]=='\n')
  23. break;
  24. if(last==in.size())
  25. throw std::runtime_error("Cannot read number...");
  26. std::istringstream ss(in.substr(it,last-it));
  27. it+=(1+last-it);
  28. ss >> last;
  29. return last;
  30. }
  31. static CBuilding * readBg(std::string &buf, int& it)
  32. {
  33. CBuilding * nb = new CBuilding();
  34. for(int res=0;res<7;res++)
  35. nb->resources[res] = readNr(buf,it);
  36. /*nb->refName = */readTo(buf,it,'\n');
  37. //reference name is omitted, it's seems to be useless
  38. return nb;
  39. }
  40. void CBuildingHandler::loadBuildings()
  41. {
  42. auto textFile = CResourceHandler::get()->loadData(ResourceID("DATA/BUILDING.TXT"));
  43. std::string buf((char*)textFile.first.get(), textFile.second);
  44. std::string temp;
  45. int it=0; //buf iterator
  46. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  47. //read 9 special buildings for every faction
  48. buildings.resize(GameConstants::F_NUMBER);
  49. for(int i=0;i<GameConstants::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<GameConstants::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<GameConstants::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<GameConstants::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. const JsonNode config(GameConstants::DATA_DIR + "/config/hall.json");
  93. BOOST_FOREACH(const JsonNode &town, config["town"].Vector())
  94. {
  95. int tid = town["id"].Float();
  96. hall[tid].first = town["image"].String();
  97. (hall[tid].second).resize(5); //rows
  98. int row_num = 0;
  99. BOOST_FOREACH(const JsonNode &row, town["boxes"].Vector())
  100. {
  101. BOOST_FOREACH(const JsonNode &box, row.Vector())
  102. {
  103. (hall[tid].second)[row_num].push_back(std::vector<int>()); //push new box
  104. std::vector<int> &box_vec = (hall[tid].second)[row_num].back();
  105. BOOST_FOREACH(const JsonNode &value, box.Vector())
  106. {
  107. box_vec.push_back(value.Float());
  108. }
  109. }
  110. row_num ++;
  111. }
  112. assert (row_num == 5);
  113. }
  114. }
  115. CBuildingHandler::~CBuildingHandler()
  116. {
  117. for(std::vector< bmap<int, ConstTransitivePtr<CBuilding> > >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  118. for(std::map<int, ConstTransitivePtr<CBuilding> >::iterator j=i->begin(); j!=i->end(); j++)
  119. j->second.dellNull();
  120. }
  121. static std::string emptyStr = "";
  122. const std::string & CBuilding::Name() const
  123. {
  124. if(name.length())
  125. return name;
  126. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  127. return VLC->generaltexth->buildings[tid][bid].first;
  128. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  129. return emptyStr;
  130. }
  131. const std::string & CBuilding::Description() const
  132. {
  133. if(description.length())
  134. return description;
  135. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  136. return VLC->generaltexth->buildings[tid][bid].second;
  137. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  138. return emptyStr;
  139. }
  140. CBuilding::CBuilding( int TID, int BID )
  141. {
  142. tid = TID;
  143. bid = BID;
  144. }
  145. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  146. {
  147. using namespace boost::assign;
  148. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  149. (6)(26)(17)(21)(22)(23)
  150. ; //creature generators with banks - handled separately
  151. if (camp < campToERMU.size())
  152. {
  153. return campToERMU[camp];
  154. }
  155. static const std::vector<int> hordeLvlsPerTType[GameConstants::F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  156. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  157. int curPos = campToERMU.size();
  158. for (int i=0; i<7; ++i)
  159. {
  160. if(camp == curPos) //non-upgraded
  161. return 30 + i;
  162. curPos++;
  163. if(camp == curPos) //upgraded
  164. return 37 + i;
  165. curPos++;
  166. //horde building
  167. if (vstd::contains(hordeLvlsPerTType[townType], i))
  168. {
  169. if (camp == curPos)
  170. {
  171. if (hordeLvlsPerTType[townType][0] == i)
  172. {
  173. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  174. return 19;
  175. else //upgraded dwelling not presents
  176. return 18;
  177. }
  178. else
  179. {
  180. if(hordeLvlsPerTType[townType].size() > 1)
  181. {
  182. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  183. return 25;
  184. else //upgraded dwelling not presents
  185. return 24;
  186. }
  187. }
  188. }
  189. curPos++;
  190. }
  191. }
  192. assert(0);
  193. return -1; //not found
  194. }