CTownHandler.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include "CGeneralTextHandler.h"
  8. #include "../lib/JsonNode.h"
  9. #include <boost/foreach.hpp>
  10. extern CLodHandler * bitmaph;
  11. void loadToIt(std::string &dest, const std::string &src, int &iter, int mode);
  12. /*
  13. * CTownHandler.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. CTownHandler::CTownHandler()
  22. {
  23. VLC->townh = this;
  24. }
  25. CTownHandler::~CTownHandler()
  26. {
  27. for( std::vector<std::map<int, Structure*> >::iterator i= structures.begin(); i!=structures.end(); i++)
  28. for( std::map<int, Structure*>::iterator j = i->begin(); j!=i->end(); j++)
  29. delete j->second;
  30. }
  31. void CTownHandler::loadNames()
  32. {
  33. int si, itr;
  34. char bufname[75];
  35. for (si=0; si<F_NUMBER; si++)
  36. {
  37. CTown town;
  38. town.typeID=si;
  39. town.bonus=towns.size();
  40. if (town.bonus==8) town.bonus=3;
  41. towns.push_back(town);
  42. }
  43. loadStructures();
  44. std::ifstream of;
  45. for(int x=0;x<towns.size();x++)
  46. towns[x].basicCreatures.resize(7);
  47. of.open(DATA_DIR "/config/basicCres.txt");
  48. while(!of.eof())
  49. {
  50. int tid, lid, cid; //town,level,creature
  51. of >> tid >> lid >> cid;
  52. if(lid < towns[tid].basicCreatures.size())
  53. towns[tid].basicCreatures[lid]=cid;
  54. }
  55. of.close();
  56. of.clear();
  57. for(int x=0;x<towns.size();x++)
  58. towns[x].upgradedCreatures.resize(7);
  59. of.open(DATA_DIR "/config/creatures_upgr.txt");
  60. while(!of.eof())
  61. {
  62. int tid, lid, cid; //town,level,creature
  63. of >> tid >> lid >> cid;
  64. if(lid < towns[tid].upgradedCreatures.size())
  65. towns[tid].upgradedCreatures[lid]=cid;
  66. }
  67. of.close();
  68. of.clear();
  69. of.open(DATA_DIR "/config/building_horde.txt");
  70. while(!of.eof())
  71. {
  72. int tid, lid, cid; //town,horde serial,creature level
  73. of >> tid >> lid >> cid;
  74. towns[tid].hordeLvl[--lid] = cid;
  75. }
  76. of.close();
  77. of.clear();
  78. of.open(DATA_DIR "/config/mageLevel.txt");
  79. of >> si;
  80. for(itr=0; itr<si; itr++)
  81. {
  82. of >> towns[itr].mageLevel >> towns[itr].primaryRes >> towns[itr].warMachine;
  83. }
  84. of.close();
  85. of.clear();
  86. of.open(DATA_DIR "/config/requirements.txt");
  87. requirements.resize(F_NUMBER);
  88. while(!of.eof())
  89. {
  90. int ile, town, build, pom;
  91. of >> ile;
  92. for(int i=0;i<ile;i++)
  93. {
  94. of >> town;
  95. while(true)
  96. {
  97. of.getline(bufname,75);
  98. if(!bufname[0] || bufname[0] == '\n' || bufname[0] == '\r')
  99. of.getline(bufname,75);
  100. std::istringstream ifs(bufname);
  101. ifs >> build;
  102. if(build<0)
  103. break;
  104. while(!ifs.eof())
  105. {
  106. ifs >> pom;
  107. requirements[town][build].insert(pom);
  108. }
  109. }
  110. }
  111. }
  112. of.close();
  113. of.clear();
  114. }
  115. void CTownHandler::loadStructures()
  116. {
  117. std::ifstream of;
  118. structures.resize(F_NUMBER);
  119. // read city properties
  120. const JsonNode config(DATA_DIR "/config/buildings.json");
  121. const JsonVector &town_type_vec = config["town_type"].Vector();
  122. int townID=0;
  123. // Iterate for each city type
  124. for (JsonVector::const_iterator it = town_type_vec.begin(); it!=town_type_vec.end(); ++it, ++townID) {
  125. std::map<int, Structure*> &town = structures[townID];
  126. const JsonNode &town_node = *it;
  127. const JsonVector &defnames_vec = town_node["defnames"].Vector();
  128. // Read buildings coordinates for that city
  129. for (JsonVector::const_iterator it2 = defnames_vec.begin(); it2!=defnames_vec.end(); ++it2) {
  130. const JsonNode &node = *it2;
  131. Structure *vinya = new Structure;
  132. const JsonNode *value;
  133. vinya->group = -1;
  134. vinya->townID = townID;
  135. vinya->ID = node["id"].Float();
  136. vinya->defName = node["defname"].String();
  137. vinya->name = vinya->defName; //TODO - use normal names
  138. vinya->pos.x = node["x"].Float();
  139. vinya->pos.y = node["y"].Float();
  140. vinya->pos.z = 0;
  141. value = &node["border"];
  142. if (!value->isNull())
  143. vinya->borderName = value->String();
  144. value = &node["area"];
  145. if (!value->isNull())
  146. vinya->areaName = value->String();
  147. town[vinya->ID] = vinya;
  148. }
  149. // Read buildings blit order for that city
  150. const JsonVector &blit_order_vec = town_node["blit_order"].Vector();
  151. int itr = 1;
  152. for (JsonVector::const_iterator it2 = blit_order_vec.begin(); it2!=blit_order_vec.end(); ++it2) {
  153. const JsonNode &node = *it2;
  154. int buildingID = node.Float();
  155. /* Find the building and set its order. */
  156. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  157. if (i2 != (town.end()))
  158. i2->second->pos.z = itr++;
  159. else
  160. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  161. }
  162. }
  163. int group_num=0;
  164. // Iterate for each city
  165. BOOST_FOREACH(const JsonNode &town_node, config["town_groups"].Vector()) {
  166. townID = town_node["id"].Float();
  167. // Iterate for each group for that city
  168. BOOST_FOREACH(const JsonNode &group, town_node["groups"].Vector()) {
  169. group_num ++;
  170. // Iterate for each bulding value in the group
  171. BOOST_FOREACH(const JsonNode &value, group.Vector()) {
  172. int buildingID = value.Float();
  173. std::vector<std::map<int, Structure*> >::iterator i;
  174. std::map<int, Structure*>::iterator i2;
  175. if (townID >= 0) {
  176. if ((i = structures.begin() + townID) != structures.end()) {
  177. if ((i2=(i->find(buildingID)))!=(i->end()))
  178. i2->second->group = group_num;
  179. else
  180. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<townID<<std::endl;
  181. }
  182. else
  183. tlog3 << "Warning3: Castle "<<townID<<" not defined."<<std::endl;
  184. } else {
  185. // Set group for selected building in ALL castles
  186. for (i=structures.begin();i!=structures.end();i++) {
  187. for(i2=i->begin(); i2!=i->end(); i2++) {
  188. if(i2->first == buildingID) {
  189. i2->second->group = group_num;
  190. break;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. const std::string & CTown::Name() const
  200. {
  201. if(name.length())
  202. return name;
  203. else
  204. return VLC->generaltexth->townTypes[typeID];
  205. }
  206. const std::vector<std::string> & CTown::Names() const
  207. {
  208. if(names.size())
  209. return names;
  210. else
  211. return VLC->generaltexth->townNames[typeID];
  212. }