CTownHandler.cpp 6.7 KB

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