CBuildingHandler.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. // Grail. It may not have entries in building.txt
  78. for (size_t town=0; town<GameConstants::F_NUMBER; town++)
  79. {
  80. if (!vstd::contains(buildings[town], 26))
  81. {
  82. buildings[town][26] = new CBuilding();
  83. buildings[town][26]->tid = town;
  84. buildings[town][26]->bid = 26;
  85. }
  86. }
  87. /////done reading BUILDING.TXT*****************************
  88. const JsonNode config(ResourceID("config/hall.json"));
  89. BOOST_FOREACH(const JsonNode &town, config["town"].Vector())
  90. {
  91. int tid = town["id"].Float();
  92. hall[tid].first = town["image"].String();
  93. (hall[tid].second).resize(5); //rows
  94. int row_num = 0;
  95. BOOST_FOREACH(const JsonNode &row, town["boxes"].Vector())
  96. {
  97. BOOST_FOREACH(const JsonNode &box, row.Vector())
  98. {
  99. (hall[tid].second)[row_num].push_back(std::vector<int>()); //push new box
  100. std::vector<int> &box_vec = (hall[tid].second)[row_num].back();
  101. BOOST_FOREACH(const JsonNode &value, box.Vector())
  102. {
  103. box_vec.push_back(value.Float());
  104. }
  105. }
  106. row_num ++;
  107. }
  108. assert (row_num == 5);
  109. }
  110. // Buildings dependencies. Which building depend on which other building.
  111. const JsonNode buildingsConf(ResourceID("config/buildings.json"));
  112. // Iterate for each city type
  113. int townID = 0;
  114. BOOST_FOREACH(const JsonNode &town_node, buildingsConf["town_type"].Vector())
  115. {
  116. BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector())
  117. {
  118. int id = node["id"].Float();
  119. CBuilding * build = buildings[townID][id];
  120. if (build)
  121. {
  122. BOOST_FOREACH(const JsonNode &building, node["requires"].Vector())
  123. {
  124. build->requirements.insert(building.Float());
  125. }
  126. }
  127. }
  128. townID++;
  129. }
  130. }
  131. CBuildingHandler::~CBuildingHandler()
  132. {
  133. for(std::vector< bmap<int, ConstTransitivePtr<CBuilding> > >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  134. for(std::map<int, ConstTransitivePtr<CBuilding> >::iterator j=i->begin(); j!=i->end(); j++)
  135. j->second.dellNull();
  136. }
  137. static std::string emptyStr = "";
  138. const std::string & CBuilding::Name() const
  139. {
  140. if(name.length())
  141. return name;
  142. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  143. return VLC->generaltexth->buildings[tid][bid].first;
  144. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  145. return emptyStr;
  146. }
  147. const std::string & CBuilding::Description() const
  148. {
  149. if(description.length())
  150. return description;
  151. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  152. return VLC->generaltexth->buildings[tid][bid].second;
  153. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  154. return emptyStr;
  155. }
  156. CBuilding::CBuilding( int TID, int BID )
  157. {
  158. tid = TID;
  159. bid = BID;
  160. }
  161. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  162. {
  163. using namespace boost::assign;
  164. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  165. (6)(26)(17)(21)(22)(23)
  166. ; //creature generators with banks - handled separately
  167. if (camp < campToERMU.size())
  168. {
  169. return campToERMU[camp];
  170. }
  171. static const std::vector<int> hordeLvlsPerTType[GameConstants::F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  172. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  173. int curPos = campToERMU.size();
  174. for (int i=0; i<7; ++i)
  175. {
  176. if(camp == curPos) //non-upgraded
  177. return 30 + i;
  178. curPos++;
  179. if(camp == curPos) //upgraded
  180. return 37 + i;
  181. curPos++;
  182. //horde building
  183. if (vstd::contains(hordeLvlsPerTType[townType], i))
  184. {
  185. if (camp == curPos)
  186. {
  187. if (hordeLvlsPerTType[townType][0] == i)
  188. {
  189. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  190. return 19;
  191. else //upgraded dwelling not presents
  192. return 18;
  193. }
  194. else
  195. {
  196. if(hordeLvlsPerTType[townType].size() > 1)
  197. {
  198. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  199. return 25;
  200. else //upgraded dwelling not presents
  201. return 24;
  202. }
  203. }
  204. }
  205. curPos++;
  206. }
  207. }
  208. assert(0);
  209. return -1; //not found
  210. }