2
0

CTownHandler.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "StdInc.h"
  2. #include "CTownHandler.h"
  3. #include "CLodHandler.h"
  4. #include "../lib/VCMI_Lib.h"
  5. #include "CGeneralTextHandler.h"
  6. #include "../lib/JsonNode.h"
  7. #include "GameConstants.h"
  8. /*
  9. * CTownHandler.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. CTownHandler::CTownHandler()
  18. {
  19. VLC->townh = this;
  20. }
  21. CTownHandler::~CTownHandler()
  22. {
  23. for( std::vector<std::map<int, Structure*> >::iterator i= structures.begin(); i!=structures.end(); i++)
  24. for( std::map<int, Structure*>::iterator j = i->begin(); j!=i->end(); j++)
  25. delete j->second;
  26. }
  27. void CTownHandler::loadStructures()
  28. {
  29. int townID;
  30. for (townID=0; townID<GameConstants::F_NUMBER; townID++)
  31. {
  32. CTown town;
  33. town.typeID=townID;
  34. town.bonus=towns.size();
  35. if (town.bonus==8) town.bonus=3;
  36. towns.push_back(town);
  37. }
  38. for(int x=0;x<towns.size();x++) {
  39. /* There is actually 8 basic creatures, but we ignore the 8th
  40. * entry for now */
  41. towns[x].basicCreatures.resize(7);
  42. towns[x].upgradedCreatures.resize(7);
  43. }
  44. structures.resize(GameConstants::F_NUMBER);
  45. // read city properties
  46. const JsonNode config(GameConstants::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. int level;
  51. std::map<int, Structure*> &town = structures[townID];
  52. // Read buildings coordinates for that city
  53. BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) {
  54. Structure *vinya = new Structure;
  55. const JsonNode *value;
  56. vinya->group = -1;
  57. vinya->townID = townID;
  58. vinya->ID = node["id"].Float();
  59. vinya->defName = node["defname"].String();
  60. vinya->name = vinya->defName; //TODO - use normal names
  61. vinya->pos.x = node["x"].Float();
  62. vinya->pos.y = node["y"].Float();
  63. vinya->pos.z = 0;
  64. value = &node["border"];
  65. if (!value->isNull())
  66. vinya->borderName = value->String();
  67. value = &node["area"];
  68. if (!value->isNull())
  69. vinya->areaName = value->String();
  70. town[vinya->ID] = vinya;
  71. }
  72. // Read buildings blit order for that city
  73. int itr = 1;
  74. BOOST_FOREACH(const JsonNode &node, town_node["blit_order"].Vector()) {
  75. int buildingID = node.Float();
  76. /* Find the building and set its order. */
  77. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  78. if (i2 != (town.end()))
  79. i2->second->pos.z = itr++;
  80. else
  81. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  82. }
  83. // Read basic creatures belonging to that city
  84. level = 0;
  85. BOOST_FOREACH(const JsonNode &node, town_node["creatures_basic"].Vector()) {
  86. // This if ignores the 8th field (WoG creature)
  87. if (level < towns[townID].basicCreatures.size())
  88. towns[townID].basicCreatures[level] = node.Float();
  89. level ++;
  90. }
  91. // Read upgraded creatures belonging to that city
  92. level = 0;
  93. BOOST_FOREACH(const JsonNode &node, town_node["creatures_upgraded"].Vector()) {
  94. towns[townID].upgradedCreatures[level] = node.Float();
  95. level ++;
  96. }
  97. // Horde building creature level
  98. level = 0;
  99. BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) {
  100. towns[townID].hordeLvl[level] = node.Float();
  101. level ++;
  102. }
  103. // Buildings dependencies. Which building depend on which other building.
  104. requirements.resize(GameConstants::F_NUMBER);
  105. BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector()) {
  106. std::set<int> &requires = requirements[townID][node["id"].Float()];
  107. BOOST_FOREACH(const JsonNode &building, node["requires"].Vector()) {
  108. requires.insert(building.Float());
  109. }
  110. }
  111. // Misc.
  112. towns[townID].mageLevel = town_node["mage_guild"].Float();
  113. towns[townID].primaryRes = town_node["primary_resource"].Float();
  114. towns[townID].warMachine = town_node["war_machine"].Float();
  115. townID ++;
  116. }
  117. int group_num=0;
  118. // Iterate for each city
  119. BOOST_FOREACH(const JsonNode &town_node, config["town_groups"].Vector()) {
  120. townID = town_node["id"].Float();
  121. // Iterate for each group for that city
  122. BOOST_FOREACH(const JsonNode &group, town_node["groups"].Vector()) {
  123. group_num ++;
  124. // Iterate for each bulding value in the group
  125. BOOST_FOREACH(const JsonNode &value, group.Vector()) {
  126. int buildingID = value.Float();
  127. std::vector<std::map<int, Structure*> >::iterator i;
  128. std::map<int, Structure*>::iterator i2;
  129. if (townID >= 0) {
  130. if ((i = structures.begin() + townID) != structures.end()) {
  131. if ((i2=(i->find(buildingID)))!=(i->end()))
  132. i2->second->group = group_num;
  133. else
  134. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<townID<<std::endl;
  135. }
  136. else
  137. tlog3 << "Warning3: Castle "<<townID<<" not defined."<<std::endl;
  138. } else {
  139. // Set group for selected building in ALL castles
  140. for (i=structures.begin();i!=structures.end();i++) {
  141. for(i2=i->begin(); i2!=i->end(); i2++) {
  142. if(i2->first == buildingID) {
  143. i2->second->group = group_num;
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. const std::string & CTown::Name() const
  154. {
  155. if(name.length())
  156. return name;
  157. else
  158. return VLC->generaltexth->townTypes[typeID];
  159. }
  160. const std::vector<std::string> & CTown::Names() const
  161. {
  162. if(names.size())
  163. return names;
  164. else
  165. return VLC->generaltexth->townNames[typeID];
  166. }