CBuildingHandler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "StdInc.h"
  2. #include "CBuildingHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "VCMI_Lib.h"
  5. #include "Filesystem/CResourceLoader.h"
  6. #include "JsonNode.h"
  7. #include "GameConstants.h"
  8. /*
  9. * CBuildingHandler.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. CBuilding * readBuilding(CLegacyConfigParser & parser, int townID, int buildID)
  18. {
  19. CBuilding * ret = new CBuilding;
  20. ret->tid = townID;
  21. ret->bid = buildID;
  22. for (size_t i=0; i< ret->resources.size(); i++)
  23. ret->resources[i] = parser.readNumber();
  24. parser.endLine();
  25. return ret;
  26. }
  27. void CBuildingHandler::loadBuildings()
  28. {
  29. CLegacyConfigParser parser("DATA/BUILDING.TXT");
  30. buildings.resize(GameConstants::F_NUMBER);
  31. parser.endLine(); // header
  32. parser.endLine();
  33. //Unique buildings
  34. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  35. {
  36. parser.endLine(); //header
  37. parser.endLine();
  38. int buildID = 17;
  39. do
  40. {
  41. buildings[town][buildID] = readBuilding(parser, town, buildID);
  42. buildID++;
  43. }
  44. while (!parser.isNextEntryEmpty());
  45. }
  46. // Common buildings
  47. parser.endLine(); // header
  48. parser.endLine();
  49. parser.endLine();
  50. int buildID = 0;
  51. do
  52. {
  53. buildings[0][buildID] = readBuilding(parser, 0, buildID);
  54. for (size_t town=1; town<GameConstants::F_NUMBER; town++)
  55. {
  56. buildings[town][buildID] = new CBuilding(*buildings[0][buildID]);
  57. buildings[town][buildID]->tid = town;
  58. }
  59. buildID++;
  60. }
  61. while (!parser.isNextEntryEmpty());
  62. parser.endLine(); //header
  63. parser.endLine();
  64. //Dwellings
  65. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  66. {
  67. parser.endLine(); //header
  68. parser.endLine();
  69. int buildID = 30;
  70. do
  71. {
  72. buildings[town][buildID] = readBuilding(parser, town, buildID);
  73. buildID++;
  74. }
  75. while (!parser.isNextEntryEmpty());
  76. }
  77. /////done reading BUILDING.TXT*****************************
  78. const JsonNode config(ResourceID("config/hall.json"));
  79. BOOST_FOREACH(const JsonNode &town, config["town"].Vector())
  80. {
  81. int tid = town["id"].Float();
  82. hall[tid].first = town["image"].String();
  83. (hall[tid].second).resize(5); //rows
  84. int row_num = 0;
  85. BOOST_FOREACH(const JsonNode &row, town["boxes"].Vector())
  86. {
  87. BOOST_FOREACH(const JsonNode &box, row.Vector())
  88. {
  89. (hall[tid].second)[row_num].push_back(std::vector<int>()); //push new box
  90. std::vector<int> &box_vec = (hall[tid].second)[row_num].back();
  91. BOOST_FOREACH(const JsonNode &value, box.Vector())
  92. {
  93. box_vec.push_back(value.Float());
  94. }
  95. }
  96. row_num ++;
  97. }
  98. assert (row_num == 5);
  99. }
  100. // Buildings dependencies. Which building depend on which other building.
  101. const JsonNode buildingsConf(ResourceID("config/buildings.json"));
  102. // Iterate for each city type
  103. int townID = 0;
  104. BOOST_FOREACH(const JsonNode &town_node, buildingsConf["town_type"].Vector())
  105. {
  106. BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector())
  107. {
  108. int id = node["id"].Float();
  109. CBuilding * build = buildings[townID][id];
  110. if (build)
  111. {
  112. BOOST_FOREACH(const JsonNode &building, node["requires"].Vector())
  113. {
  114. build->requirements.insert(building.Float());
  115. }
  116. }
  117. }
  118. townID++;
  119. }
  120. }
  121. CBuildingHandler::~CBuildingHandler()
  122. {
  123. for(std::vector< bmap<int, ConstTransitivePtr<CBuilding> > >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  124. for(std::map<int, ConstTransitivePtr<CBuilding> >::iterator j=i->begin(); j!=i->end(); j++)
  125. j->second.dellNull();
  126. }
  127. static std::string emptyStr = "";
  128. const std::string & CBuilding::Name() const
  129. {
  130. if(name.length())
  131. return name;
  132. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  133. return VLC->generaltexth->buildings[tid][bid].first;
  134. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  135. return emptyStr;
  136. }
  137. const std::string & CBuilding::Description() const
  138. {
  139. if(description.length())
  140. return description;
  141. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  142. return VLC->generaltexth->buildings[tid][bid].second;
  143. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  144. return emptyStr;
  145. }
  146. CBuilding::CBuilding( int TID, int BID )
  147. {
  148. tid = TID;
  149. bid = BID;
  150. }
  151. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  152. {
  153. using namespace boost::assign;
  154. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  155. (6)(26)(17)(21)(22)(23)
  156. ; //creature generators with banks - handled separately
  157. if (camp < campToERMU.size())
  158. {
  159. return campToERMU[camp];
  160. }
  161. static const std::vector<int> hordeLvlsPerTType[GameConstants::F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  162. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  163. int curPos = campToERMU.size();
  164. for (int i=0; i<7; ++i)
  165. {
  166. if(camp == curPos) //non-upgraded
  167. return 30 + i;
  168. curPos++;
  169. if(camp == curPos) //upgraded
  170. return 37 + i;
  171. curPos++;
  172. //horde building
  173. if (vstd::contains(hordeLvlsPerTType[townType], i))
  174. {
  175. if (camp == curPos)
  176. {
  177. if (hordeLvlsPerTType[townType][0] == i)
  178. {
  179. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  180. return 19;
  181. else //upgraded dwelling not presents
  182. return 18;
  183. }
  184. else
  185. {
  186. if(hordeLvlsPerTType[townType].size() > 1)
  187. {
  188. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  189. return 25;
  190. else //upgraded dwelling not presents
  191. return 24;
  192. }
  193. }
  194. }
  195. curPos++;
  196. }
  197. }
  198. assert(0);
  199. return -1; //not found
  200. }