CTownHandler.cpp 6.2 KB

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