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