CTownHandler.cpp 5.9 KB

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