CTownHandler.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. vinya->group = -1;
  58. vinya->townID = townID;
  59. vinya->ID = node["id"].Float();
  60. vinya->defName = node["defname"].String();
  61. vinya->name = vinya->defName; //TODO - use normal names
  62. vinya->pos.x = node["x"].Float();
  63. vinya->pos.y = node["y"].Float();
  64. vinya->pos.z = 0;
  65. vinya->borderName = node["border"].String();
  66. vinya->areaName = node["area"].String();
  67. town[vinya->ID] = vinya;
  68. }
  69. // Read buildings blit order for that city
  70. int itr = 1;
  71. BOOST_FOREACH(const JsonNode &node, town_node["blit_order"].Vector()) {
  72. int buildingID = node.Float();
  73. /* Find the building and set its order. */
  74. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  75. if (i2 != (town.end()))
  76. i2->second->pos.z = itr++;
  77. else
  78. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  79. }
  80. // Read basic creatures belonging to that city
  81. level = 0;
  82. BOOST_FOREACH(const JsonNode &node, town_node["creatures_basic"].Vector()) {
  83. // This if ignores the 8th field (WoG creature)
  84. if (level < towns[townID].basicCreatures.size())
  85. towns[townID].basicCreatures[level] = node.Float();
  86. level ++;
  87. }
  88. // Read upgraded creatures belonging to that city
  89. level = 0;
  90. BOOST_FOREACH(const JsonNode &node, town_node["creatures_upgraded"].Vector()) {
  91. towns[townID].upgradedCreatures[level] = node.Float();
  92. level ++;
  93. }
  94. // Horde building creature level
  95. level = 0;
  96. BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) {
  97. towns[townID].hordeLvl[level] = node.Float();
  98. level ++;
  99. }
  100. // Buildings dependencies. Which building depend on which other building.
  101. requirements.resize(F_NUMBER);
  102. BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector()) {
  103. std::set<int> &requires = requirements[townID][node["id"].Float()];
  104. BOOST_FOREACH(const JsonNode &building, node["requires"].Vector()) {
  105. requires.insert(building.Float());
  106. }
  107. }
  108. // Misc.
  109. towns[townID].mageLevel = town_node["mage_guild"].Float();
  110. towns[townID].primaryRes = town_node["primary_ressource"].Float();
  111. towns[townID].warMachine = town_node["war_machine"].Float();
  112. townID ++;
  113. }
  114. int group_num=0;
  115. // Iterate for each city
  116. BOOST_FOREACH(const JsonNode &town_node, config["town_groups"].Vector()) {
  117. townID = town_node["id"].Float();
  118. // Iterate for each group for that city
  119. BOOST_FOREACH(const JsonNode &group, town_node["groups"].Vector()) {
  120. group_num ++;
  121. // Iterate for each bulding value in the group
  122. BOOST_FOREACH(const JsonNode &value, group.Vector()) {
  123. int buildingID = value.Float();
  124. std::vector<std::map<int, Structure*> >::iterator i;
  125. std::map<int, Structure*>::iterator i2;
  126. if (townID >= 0) {
  127. if ((i = structures.begin() + townID) != structures.end()) {
  128. if ((i2=(i->find(buildingID)))!=(i->end()))
  129. i2->second->group = group_num;
  130. else
  131. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<townID<<std::endl;
  132. }
  133. else
  134. tlog3 << "Warning3: Castle "<<townID<<" not defined."<<std::endl;
  135. } else {
  136. // Set group for selected building in ALL castles
  137. for (i=structures.begin();i!=structures.end();i++) {
  138. for(i2=i->begin(); i2!=i->end(); i2++) {
  139. if(i2->first == buildingID) {
  140. i2->second->group = group_num;
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. const std::string & CTown::Name() const
  151. {
  152. if(name.length())
  153. return name;
  154. else
  155. return VLC->generaltexth->townTypes[typeID];
  156. }
  157. const std::vector<std::string> & CTown::Names() const
  158. {
  159. if(names.size())
  160. return names;
  161. else
  162. return VLC->generaltexth->townNames[typeID];
  163. }