CTownHandler.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CTownHandler.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. #include "../lib/VCMI_Lib.h"
  7. #include "CGeneralTextHandler.h"
  8. #include "../lib/JsonNode.h"
  9. extern CLodHandler * bitmaph;
  10. void loadToIt(std::string &dest, const std::string &src, int &iter, int mode);
  11. /*
  12. * CTownHandler.cpp, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. CTownHandler::CTownHandler()
  21. {
  22. VLC->townh = this;
  23. }
  24. CTownHandler::~CTownHandler()
  25. {
  26. for( std::vector<std::map<int, Structure*> >::iterator i= structures.begin(); i!=structures.end(); i++)
  27. for( std::map<int, Structure*>::iterator j = i->begin(); j!=i->end(); j++)
  28. delete j->second;
  29. }
  30. void CTownHandler::loadNames()
  31. {
  32. int si, itr;
  33. char bufname[75];
  34. for (si=0; si<F_NUMBER; si++)
  35. {
  36. CTown town;
  37. town.typeID=si;
  38. town.bonus=towns.size();
  39. if (town.bonus==8) town.bonus=3;
  40. towns.push_back(town);
  41. }
  42. loadStructures();
  43. std::ifstream of;
  44. for(int x=0;x<towns.size();x++)
  45. towns[x].basicCreatures.resize(7);
  46. of.open(DATA_DIR "/config/basicCres.txt");
  47. while(!of.eof())
  48. {
  49. int tid, lid, cid; //town,level,creature
  50. of >> tid >> lid >> cid;
  51. if(lid < towns[tid].basicCreatures.size())
  52. towns[tid].basicCreatures[lid]=cid;
  53. }
  54. of.close();
  55. of.clear();
  56. for(int x=0;x<towns.size();x++)
  57. towns[x].upgradedCreatures.resize(7);
  58. of.open(DATA_DIR "/config/creatures_upgr.txt");
  59. while(!of.eof())
  60. {
  61. int tid, lid, cid; //town,level,creature
  62. of >> tid >> lid >> cid;
  63. if(lid < towns[tid].upgradedCreatures.size())
  64. towns[tid].upgradedCreatures[lid]=cid;
  65. }
  66. of.close();
  67. of.clear();
  68. of.open(DATA_DIR "/config/building_horde.txt");
  69. while(!of.eof())
  70. {
  71. int tid, lid, cid; //town,horde serial,creature level
  72. of >> tid >> lid >> cid;
  73. towns[tid].hordeLvl[--lid] = cid;
  74. }
  75. of.close();
  76. of.clear();
  77. of.open(DATA_DIR "/config/mageLevel.txt");
  78. of >> si;
  79. for(itr=0; itr<si; itr++)
  80. {
  81. of >> towns[itr].mageLevel >> towns[itr].primaryRes >> towns[itr].warMachine;
  82. }
  83. of.close();
  84. of.clear();
  85. of.open(DATA_DIR "/config/requirements.txt");
  86. requirements.resize(F_NUMBER);
  87. while(!of.eof())
  88. {
  89. int ile, town, build, pom;
  90. of >> ile;
  91. for(int i=0;i<ile;i++)
  92. {
  93. of >> town;
  94. while(true)
  95. {
  96. of.getline(bufname,75);
  97. if(!bufname[0] || bufname[0] == '\n' || bufname[0] == '\r')
  98. of.getline(bufname,75);
  99. std::istringstream ifs(bufname);
  100. ifs >> build;
  101. if(build<0)
  102. break;
  103. while(!ifs.eof())
  104. {
  105. ifs >> pom;
  106. requirements[town][build].insert(pom);
  107. }
  108. }
  109. }
  110. }
  111. of.close();
  112. of.clear();
  113. }
  114. void CTownHandler::loadStructures()
  115. {
  116. std::ifstream of;
  117. structures.resize(F_NUMBER);
  118. // read city properties
  119. const JsonNode config(DATA_DIR "/config/buildings.json");
  120. const JsonVector &town_type_vec = config["town_type"].Vector();
  121. int townID=0;
  122. // Iterate for each city type
  123. for (JsonVector::const_iterator it = town_type_vec.begin(); it!=town_type_vec.end(); ++it, ++townID) {
  124. std::map<int, Structure*> &town = structures[townID];
  125. const JsonNode &town_node = *it;
  126. const JsonVector &defnames_vec = town_node["defnames"].Vector();
  127. // Read buildings coordinates for that city
  128. for (JsonVector::const_iterator it2 = defnames_vec.begin(); it2!=defnames_vec.end(); ++it2) {
  129. const JsonNode &node = *it2;
  130. Structure *vinya = new Structure;
  131. const JsonNode *value;
  132. vinya->group = -1;
  133. vinya->townID = townID;
  134. vinya->ID = node["id"].Float();
  135. vinya->defName = node["defname"].String();
  136. vinya->name = vinya->defName; //TODO - use normal names
  137. vinya->pos.x = node["x"].Float();
  138. vinya->pos.y = node["y"].Float();
  139. vinya->pos.z = 0;
  140. value = &node["border"];
  141. if (!value->isNull())
  142. vinya->borderName = value->String();
  143. value = &node["area"];
  144. if (!value->isNull())
  145. vinya->areaName = value->String();
  146. town[vinya->ID] = vinya;
  147. }
  148. // Read buildings blit order for that city
  149. const JsonVector &blit_order_vec = town_node["blit_order"].Vector();
  150. int itr = 1;
  151. for (JsonVector::const_iterator it2 = blit_order_vec.begin(); it2!=blit_order_vec.end(); ++it2) {
  152. const JsonNode &node = *it2;
  153. int buildingID = node.Float();
  154. /* Find the building and set its order. */
  155. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  156. if (i2 != (town.end()))
  157. i2->second->pos.z = itr++;
  158. else
  159. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  160. }
  161. }
  162. //read borders and areas names
  163. int format;
  164. std::string s;
  165. //read groups
  166. of.open(DATA_DIR "/config/buildings4.txt");
  167. of >> format;
  168. if(format!=1)
  169. {
  170. tlog1 << "Unhandled format of buildings4.txt \n";
  171. }
  172. else
  173. {
  174. of >> s;
  175. int itr=1;
  176. while(!of.eof())
  177. {
  178. std::vector<std::map<int, Structure*> >::iterator i;
  179. std::map<int, Structure*>::iterator i2;
  180. int buildingID;
  181. int castleID;
  182. itr++;
  183. if (s == "CASTLE")
  184. {
  185. of >> castleID;
  186. }
  187. else if(s == "ALL")
  188. {
  189. castleID = -1;
  190. }
  191. else
  192. {
  193. break;
  194. }
  195. of >> s;
  196. while(1) //read groups for castle
  197. {
  198. if (s == "GROUP")
  199. {
  200. while(1)
  201. {
  202. of >> s;
  203. if((s == "GROUP") || (s == "EOD") || (s == "CASTLE")) //
  204. break;
  205. buildingID = atoi(s.c_str());
  206. if(castleID>=0)
  207. {
  208. if((i = structures.begin() + castleID) != structures.end())
  209. if((i2=(i->find(buildingID)))!=(i->end()))
  210. i2->second->group = itr;
  211. else
  212. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<castleID<<std::endl;
  213. else
  214. tlog3 << "Warning3: Castle "<<castleID<<" not defined."<<std::endl;
  215. }
  216. else //set group for selected building in ALL castles
  217. {
  218. for(i=structures.begin();i!=structures.end();i++)
  219. {
  220. for(i2=i->begin(); i2!=i->end(); i2++)
  221. {
  222. if(i2->first == buildingID)
  223. {
  224. i2->second->group = itr;
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. if(s == "CASTLE")
  232. break;
  233. itr++;
  234. }//if (s == "GROUP")
  235. else if(s == "EOD")
  236. break;
  237. }
  238. }
  239. of.close();
  240. of.clear();
  241. }
  242. }
  243. const std::string & CTown::Name() const
  244. {
  245. if(name.length())
  246. return name;
  247. else
  248. return VLC->generaltexth->townTypes[typeID];
  249. }
  250. const std::vector<std::string> & CTown::Names() const
  251. {
  252. if(names.size())
  253. return names;
  254. else
  255. return VLC->generaltexth->townNames[typeID];
  256. }