CTownHandler.cpp 6.7 KB

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