CTownHandler.cpp 5.5 KB

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