CTownHandler.cpp 5.7 KB

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