CTownHandler.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 &node = *it;
  126. const JsonVector &defnames_vec = 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 &ai = *it2;
  130. Structure *vinya = new Structure;
  131. vinya->group = -1;
  132. vinya->townID = townID;
  133. vinya->ID = ai["id"].Float();
  134. vinya->defName = ai["defname"].String();
  135. vinya->name = vinya->defName; //TODO - use normal names
  136. vinya->pos.x = ai["x"].Float();
  137. vinya->pos.y = ai["y"].Float();
  138. vinya->pos.z = 0;
  139. town[vinya->ID] = vinya;
  140. }
  141. // Read buildings blit order for that city
  142. const JsonVector &blit_order_vec = node["blit_order"].Vector();
  143. int itr = 1;
  144. for (JsonVector::const_iterator it2 = blit_order_vec.begin(); it2!=blit_order_vec.end(); ++it2) {
  145. const JsonNode &ai = *it2;
  146. int buildingID = ai.Float();
  147. /* Find the building and set its order. */
  148. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  149. if (i2 != (town.end()))
  150. i2->second->pos.z = itr++;
  151. else
  152. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  153. }
  154. }
  155. //read borders and areas names
  156. int format;
  157. std::string s;
  158. of.open(DATA_DIR "/config/buildings3.txt");
  159. while(!of.eof())
  160. {
  161. std::vector<std::map<int, Structure*> >::iterator i;
  162. std::map<int, Structure*>::iterator i2;
  163. int town, id;
  164. std::string border, area;
  165. of >> town >> id >> border >> border >> area;
  166. if( (i = structures.begin() + town) != structures.end() )
  167. if((i2=(i->find(id)))!=(i->end()))
  168. {
  169. i2->second->borderName = border;
  170. i2->second->areaName = area;
  171. }
  172. else
  173. tlog3 << "Warning2: No building "<<id<<" in the castle "<<town<<std::endl;
  174. else
  175. tlog3 << "Warning2: Castle "<<town<<" not defined."<<std::endl;
  176. }
  177. of.close();
  178. of.clear();
  179. //read groups
  180. of.open(DATA_DIR "/config/buildings4.txt");
  181. of >> format;
  182. if(format!=1)
  183. {
  184. tlog1 << "Unhandled format of buildings4.txt \n";
  185. }
  186. else
  187. {
  188. of >> s;
  189. int itr=1;
  190. while(!of.eof())
  191. {
  192. std::vector<std::map<int, Structure*> >::iterator i;
  193. std::map<int, Structure*>::iterator i2;
  194. int buildingID;
  195. int castleID;
  196. itr++;
  197. if (s == "CASTLE")
  198. {
  199. of >> castleID;
  200. }
  201. else if(s == "ALL")
  202. {
  203. castleID = -1;
  204. }
  205. else
  206. {
  207. break;
  208. }
  209. of >> s;
  210. while(1) //read groups for castle
  211. {
  212. if (s == "GROUP")
  213. {
  214. while(1)
  215. {
  216. of >> s;
  217. if((s == "GROUP") || (s == "EOD") || (s == "CASTLE")) //
  218. break;
  219. buildingID = atoi(s.c_str());
  220. if(castleID>=0)
  221. {
  222. if((i = structures.begin() + castleID) != structures.end())
  223. if((i2=(i->find(buildingID)))!=(i->end()))
  224. i2->second->group = itr;
  225. else
  226. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<castleID<<std::endl;
  227. else
  228. tlog3 << "Warning3: Castle "<<castleID<<" not defined."<<std::endl;
  229. }
  230. else //set group for selected building in ALL castles
  231. {
  232. for(i=structures.begin();i!=structures.end();i++)
  233. {
  234. for(i2=i->begin(); i2!=i->end(); i2++)
  235. {
  236. if(i2->first == buildingID)
  237. {
  238. i2->second->group = itr;
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. if(s == "CASTLE")
  246. break;
  247. itr++;
  248. }//if (s == "GROUP")
  249. else if(s == "EOD")
  250. break;
  251. }
  252. }
  253. of.close();
  254. of.clear();
  255. }
  256. }
  257. const std::string & CTown::Name() const
  258. {
  259. if(name.length())
  260. return name;
  261. else
  262. return VLC->generaltexth->townTypes[typeID];
  263. }
  264. const std::vector<std::string> & CTown::Names() const
  265. {
  266. if(names.size())
  267. return names;
  268. else
  269. return VLC->generaltexth->townNames[typeID];
  270. }