CTownHandler.cpp 5.2 KB

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