CTownHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. of.open(DATA_DIR "/config/buildings4.txt");
  191. of >> format;
  192. if(format!=1)
  193. {
  194. tlog1 << "Unhandled format of buildings4.txt \n";
  195. }
  196. else
  197. {
  198. of >> s;
  199. int itr=1;
  200. while(!of.eof())
  201. {
  202. std::vector<std::map<int, Structure*> >::iterator i;
  203. std::map<int, Structure*>::iterator i2;
  204. int buildingID;
  205. int castleID;
  206. itr++;
  207. if (s == "CASTLE")
  208. {
  209. of >> castleID;
  210. }
  211. else if(s == "ALL")
  212. {
  213. castleID = -1;
  214. }
  215. else
  216. {
  217. break;
  218. }
  219. of >> s;
  220. while(1) //read groups for castle
  221. {
  222. if (s == "GROUP")
  223. {
  224. while(1)
  225. {
  226. of >> s;
  227. if((s == "GROUP") || (s == "EOD") || (s == "CASTLE")) //
  228. break;
  229. buildingID = atoi(s.c_str());
  230. if(castleID>=0)
  231. {
  232. if((i = structures.begin() + castleID) != structures.end())
  233. if((i2=(i->find(buildingID)))!=(i->end()))
  234. i2->second->group = itr;
  235. else
  236. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<castleID<<std::endl;
  237. else
  238. tlog3 << "Warning3: Castle "<<castleID<<" not defined."<<std::endl;
  239. }
  240. else //set group for selected building in ALL castles
  241. {
  242. for(i=structures.begin();i!=structures.end();i++)
  243. {
  244. for(i2=i->begin(); i2!=i->end(); i2++)
  245. {
  246. if(i2->first == buildingID)
  247. {
  248. i2->second->group = itr;
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. if(s == "CASTLE")
  256. break;
  257. itr++;
  258. }//if (s == "GROUP")
  259. else if(s == "EOD")
  260. break;
  261. }
  262. }
  263. of.close();
  264. of.clear();
  265. }
  266. }
  267. const std::string & CTown::Name() const
  268. {
  269. if(name.length())
  270. return name;
  271. else
  272. return VLC->generaltexth->townTypes[typeID];
  273. }
  274. const std::vector<std::string> & CTown::Names() const
  275. {
  276. if(names.size())
  277. return names;
  278. else
  279. return VLC->generaltexth->townNames[typeID];
  280. }