CBuildingHandler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "StdInc.h"
  2. #include "CBuildingHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "CLodHandler.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include "../lib/JsonNode.h"
  7. #include "GameConstants.h"
  8. extern CLodHandler * bitmaph;
  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. std::string buf = bitmaph->getTextFile("BUILDING.TXT"), temp;
  43. int it=0; //buf iterator
  44. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//read 2 lines of file info
  45. //read 9 special buildings for every faction
  46. buildings.resize(GameConstants::F_NUMBER);
  47. for(int i=0;i<GameConstants::F_NUMBER;i++)
  48. {
  49. temp = readTo(buf,it,'\n');//read blank line and faction name
  50. temp = readTo(buf,it,'\n');
  51. for(int bg = 0; bg<9; bg++)
  52. {
  53. CBuilding *nb = readBg(buf,it);
  54. nb->tid = i;
  55. nb->bid = bg+17;
  56. buildings[i][bg+17] = nb;
  57. }
  58. }
  59. //reading 17 neutral (common) buildings
  60. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//neutral buildings - skip 3 lines
  61. for(int bg = 0; bg<17; bg++)
  62. {
  63. CBuilding *nb = readBg(buf,it);
  64. for(int f=0;f<GameConstants::F_NUMBER;f++)
  65. {
  66. buildings[f][bg] = new CBuilding(*nb);
  67. buildings[f][bg]->tid = f;
  68. buildings[f][bg]->bid = bg;
  69. }
  70. delete nb;
  71. }
  72. //create Grail entries
  73. for(int i=0; i<GameConstants::F_NUMBER; i++)
  74. buildings[i][26] = new CBuilding(i,26);
  75. //reading 14 per faction dwellings
  76. temp = readTo(buf,it,'\n');temp = readTo(buf,it,'\n');//dwellings - skip 2 lines
  77. for(int i=0;i<GameConstants::F_NUMBER;i++)
  78. {
  79. temp = readTo(buf,it,'\n');//read blank line
  80. temp = readTo(buf,it,'\n');// and faction name
  81. for(int bg = 0; bg<14; bg++)
  82. {
  83. CBuilding *nb = readBg(buf,it);
  84. nb->tid = i;
  85. nb->bid = bg+30;
  86. buildings[i][bg+30] = nb;
  87. }
  88. }
  89. /////done reading BUILDING.TXT*****************************
  90. const JsonNode config(GameConstants::DATA_DIR + "/config/hall.json");
  91. BOOST_FOREACH(const JsonNode &town, config["town"].Vector())
  92. {
  93. int tid = town["id"].Float();
  94. hall[tid].first = town["image"].String();
  95. (hall[tid].second).resize(5); //rows
  96. int row_num = 0;
  97. BOOST_FOREACH(const JsonNode &row, town["boxes"].Vector())
  98. {
  99. BOOST_FOREACH(const JsonNode &box, row.Vector())
  100. {
  101. (hall[tid].second)[row_num].push_back(std::vector<int>()); //push new box
  102. std::vector<int> &box_vec = (hall[tid].second)[row_num].back();
  103. BOOST_FOREACH(const JsonNode &value, box.Vector())
  104. {
  105. box_vec.push_back(value.Float());
  106. }
  107. }
  108. row_num ++;
  109. }
  110. assert (row_num == 5);
  111. }
  112. }
  113. CBuildingHandler::~CBuildingHandler()
  114. {
  115. for(std::vector< bmap<int, ConstTransitivePtr<CBuilding> > >::iterator i=buildings.begin(); i!=buildings.end(); i++)
  116. for(std::map<int, ConstTransitivePtr<CBuilding> >::iterator j=i->begin(); j!=i->end(); j++)
  117. j->second.dellNull();
  118. }
  119. static std::string emptyStr = "";
  120. const std::string & CBuilding::Name() const
  121. {
  122. if(name.length())
  123. return name;
  124. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  125. return VLC->generaltexth->buildings[tid][bid].first;
  126. tlog2 << "Warning: Cannot find name text for building " << bid << "for " << tid << "town.\n";
  127. return emptyStr;
  128. }
  129. const std::string & CBuilding::Description() const
  130. {
  131. if(description.length())
  132. return description;
  133. else if(vstd::contains(VLC->generaltexth->buildings,tid) && vstd::contains(VLC->generaltexth->buildings[tid],bid))
  134. return VLC->generaltexth->buildings[tid][bid].second;
  135. tlog2 << "Warning: Cannot find description text for building " << bid << "for " << tid << "town.\n";
  136. return emptyStr;
  137. }
  138. CBuilding::CBuilding( int TID, int BID )
  139. {
  140. tid = TID;
  141. bid = BID;
  142. }
  143. int CBuildingHandler::campToERMU( int camp, int townType, std::set<si32> builtBuildings )
  144. {
  145. using namespace boost::assign;
  146. static const std::vector<int> campToERMU = list_of(11)(12)(13)(7)(8)(9)(5)(16)(14)(15)(-1)(0)(1)(2)(3)(4)
  147. (6)(26)(17)(21)(22)(23)
  148. ; //creature generators with banks - handled separately
  149. if (camp < campToERMU.size())
  150. {
  151. return campToERMU[camp];
  152. }
  153. static const std::vector<int> hordeLvlsPerTType[GameConstants::F_NUMBER] = {list_of(2), list_of(1), list_of(1)(4), list_of(0)(2),
  154. list_of(0), list_of(0), list_of(0), list_of(0), list_of(0)};
  155. int curPos = campToERMU.size();
  156. for (int i=0; i<7; ++i)
  157. {
  158. if(camp == curPos) //non-upgraded
  159. return 30 + i;
  160. curPos++;
  161. if(camp == curPos) //upgraded
  162. return 37 + i;
  163. curPos++;
  164. //horde building
  165. if (vstd::contains(hordeLvlsPerTType[townType], i))
  166. {
  167. if (camp == curPos)
  168. {
  169. if (hordeLvlsPerTType[townType][0] == i)
  170. {
  171. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][0])) //if upgraded dwelling is built
  172. return 19;
  173. else //upgraded dwelling not presents
  174. return 18;
  175. }
  176. else
  177. {
  178. if(hordeLvlsPerTType[townType].size() > 1)
  179. {
  180. if(vstd::contains(builtBuildings, 37 + hordeLvlsPerTType[townType][1])) //if upgraded dwelling is built
  181. return 25;
  182. else //upgraded dwelling not presents
  183. return 24;
  184. }
  185. }
  186. }
  187. curPos++;
  188. }
  189. }
  190. assert(0);
  191. return -1; //not found
  192. }