CTownHandler.cpp 6.3 KB

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