CTownHandler.cpp 7.0 KB

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