CTownHandler.cpp 7.1 KB

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