CTownHandler.cpp 5.9 KB

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