CTownHandler.cpp 6.2 KB

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