CTownHandler.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. }
  11. CTownHandler::~CTownHandler()
  12. {
  13. delete smallIcons;
  14. //todo - delete structures info
  15. }
  16. void CTownHandler::loadNames()
  17. {
  18. std::istringstream ins, names;
  19. ins.str(CGI->bitmaph->getTextFile("TOWNTYPE.TXT"));
  20. names.str(CGI->bitmaph->getTextFile("TOWNNAME.TXT"));
  21. int si=0;
  22. char bufname[50];
  23. while (!ins.eof())
  24. {
  25. CTown town;
  26. ins.getline(bufname,50);
  27. town.name = std::string(bufname);
  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 = CGI->bitmaph->getTextFile("TCOMMAND.TXT");
  41. int itr=0;
  42. while(itr<strs.length()-1)
  43. {
  44. std::string tmp;
  45. CGeneralTextHandler::loadToIt(tmp, strs, itr, 3);
  46. tcommands.push_back(tmp);
  47. }
  48. //read buildings coords
  49. std::ifstream of("config/buildings.txt");
  50. while(!of.eof())
  51. {
  52. Structure *vinya = new Structure;
  53. vinya->group = -1;
  54. of >> vinya->townID;
  55. of >> vinya->ID;
  56. of >> vinya->defName;
  57. vinya->name = vinya->defName; //TODO - use normal names
  58. of >> vinya->pos.x;
  59. of >> vinya->pos.y;
  60. vinya->pos.z = 0;
  61. structures[vinya->townID][vinya->ID] = vinya;
  62. }
  63. of.close();
  64. of.clear();
  65. //read building priorities
  66. of.open("config/buildings2.txt");
  67. int format, idt;
  68. std::string s;
  69. of >> format >> idt;
  70. while(!of.eof())
  71. {
  72. std::map<int,std::map<int, Structure*> >::iterator i;
  73. std::map<int, Structure*>::iterator i2;
  74. int itr=1, buildingID;
  75. int castleID;
  76. of >> s;
  77. if (s != "CASTLE")
  78. break;
  79. of >> castleID;
  80. while(1)
  81. {
  82. of >> s;
  83. if (s == "END")
  84. break;
  85. else
  86. if((i=structures.find(castleID))!=structures.end())
  87. if((i2=(i->second.find(buildingID=atoi(s.c_str()))))!=(i->second.end()))
  88. i2->second->pos.z=itr++;
  89. else
  90. std::cout << "Warning1: No building "<<buildingID<<" in the castle "<<castleID<<std::endl;
  91. else
  92. std::cout << "Warning1: Castle "<<castleID<<" not defined."<<std::endl;
  93. }
  94. }
  95. of.close();
  96. of.clear();
  97. //read borders and areas names
  98. of.open("config/buildings3.txt");
  99. while(!of.eof())
  100. {
  101. std::map<int,std::map<int, Structure*> >::iterator i;
  102. std::map<int, Structure*>::iterator i2;
  103. int town, id;
  104. std::string border, area;
  105. of >> town >> id >> border >> border >> area;
  106. if((i=structures.find(town))!=structures.end())
  107. if((i2=(i->second.find(id)))!=(i->second.end()))
  108. {
  109. i2->second->borderName = border;
  110. i2->second->areaName = area;
  111. }
  112. else
  113. std::cout << "Warning2: No building "<<id<<" in the castle "<<town<<std::endl;
  114. else
  115. std::cout << "Warning2: Castle "<<town<<" not defined."<<std::endl;
  116. }
  117. of.close();
  118. of.clear();
  119. //read groups
  120. itr = 0;
  121. of.open("config/buildings4.txt");
  122. of >> format;
  123. if(format!=1)
  124. {
  125. std::cout << "Unhandled format of buildings4.txt \n";
  126. }
  127. else
  128. {
  129. of >> s;
  130. int itr=1;
  131. while(!of.eof())
  132. {
  133. std::map<int,std::map<int, Structure*> >::iterator i;
  134. std::map<int, Structure*>::iterator i2;
  135. int buildingID;
  136. int castleID;
  137. itr++;
  138. if (s == "CASTLE")
  139. {
  140. of >> castleID;
  141. }
  142. else if(s == "ALL")
  143. {
  144. castleID = -1;
  145. }
  146. else
  147. {
  148. break;
  149. }
  150. of >> s;
  151. while(1) //read groups for castle
  152. {
  153. if (s == "GROUP")
  154. {
  155. while(1)
  156. {
  157. of >> s;
  158. if((s == "GROUP") || (s == "EOD") || (s == "CASTLE")) //
  159. break;
  160. buildingID = atoi(s.c_str());
  161. if(castleID>=0)
  162. {
  163. if((i=structures.find(castleID))!=structures.end())
  164. if((i2=(i->second.find(buildingID)))!=(i->second.end()))
  165. i2->second->group = itr;
  166. else
  167. std::cout << "Warning3: No building "<<buildingID<<" in the castle "<<castleID<<std::endl;
  168. else
  169. std::cout << "Warning3: Castle "<<castleID<<" not defined."<<std::endl;
  170. }
  171. else //set group for selected building in ALL castles
  172. {
  173. for(i=structures.begin();i!=structures.end();i++)
  174. {
  175. for(i2=i->second.begin(); i2!=i->second.end(); i2++)
  176. {
  177. if(i2->first == buildingID)
  178. {
  179. i2->second->group = itr;
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. if(s == "CASTLE")
  187. break;
  188. itr++;
  189. }//if (s == "GROUP")
  190. else if(s == "EOD")
  191. break;
  192. }
  193. }
  194. of.close();
  195. of.clear();
  196. for(int x=0;x<towns.size();x++)
  197. towns[x].basicCreatures.resize(7);
  198. of.open("config/basicCres.txt");
  199. while(!of.eof())
  200. {
  201. int tid, lid, cid; //town,level,creature
  202. of >> tid >> lid >> cid;
  203. if(lid < towns[tid].basicCreatures.size())
  204. towns[tid].basicCreatures[lid]=cid;
  205. }
  206. of.close();
  207. of.clear();
  208. }
  209. }
  210. SDL_Surface * CTownHandler::getPic(int ID, bool fort, bool builded)
  211. {
  212. if (ID==-1)
  213. return smallIcons->ourImages[0].bitmap;
  214. else if (ID==-2)
  215. return smallIcons->ourImages[1].bitmap;
  216. else if (ID==-3)
  217. return smallIcons->ourImages[2+F_NUMBER*4].bitmap;
  218. else if (ID>F_NUMBER || ID<-3)
  219. throw new std::exception("Invalid ID");
  220. else
  221. {
  222. int pom = 3;
  223. if(!fort)
  224. pom+=F_NUMBER*2;
  225. pom += ID*2;
  226. if (!builded)
  227. pom--;
  228. return smallIcons->ourImages[pom].bitmap;
  229. }
  230. }
  231. int CTownHandler::getTypeByDefName(std::string name)
  232. {
  233. //TODO
  234. return 0;
  235. }