CTownHandler.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "StdInc.h"
  2. #include "CTownHandler.h"
  3. #include "VCMI_Lib.h"
  4. #include "CGeneralTextHandler.h"
  5. #include "JsonNode.h"
  6. #include "GameConstants.h"
  7. #include "Filesystem/CResourceLoader.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. towns[x].creatures.resize(GameConstants::CREATURES_PER_TOWN);
  40. structures.resize(GameConstants::F_NUMBER);
  41. // read city properties
  42. const JsonNode config(ResourceID("config/buildings.json"));
  43. // Iterate for each city type
  44. townID = 0;
  45. BOOST_FOREACH(const JsonNode &town_node, config["town_type"].Vector()) {
  46. int level;
  47. std::map<int, Structure*> &town = structures[townID];
  48. // Read buildings coordinates for that city
  49. BOOST_FOREACH(const JsonNode &node, town_node["defnames"].Vector()) {
  50. Structure *vinya = new Structure;
  51. const JsonNode *value;
  52. vinya->group = -1;
  53. vinya->townID = townID;
  54. vinya->ID = node["id"].Float();
  55. vinya->defName = node["defname"].String();
  56. vinya->name = vinya->defName; //TODO - use normal names
  57. vinya->pos.x = node["x"].Float();
  58. vinya->pos.y = node["y"].Float();
  59. vinya->pos.z = 0;
  60. value = &node["border"];
  61. if (!value->isNull())
  62. vinya->borderName = value->String();
  63. value = &node["area"];
  64. if (!value->isNull())
  65. vinya->areaName = value->String();
  66. town[vinya->ID] = vinya;
  67. }
  68. // Read buildings blit order for that city
  69. int itr = 1;
  70. BOOST_FOREACH(const JsonNode &node, town_node["blit_order"].Vector()) {
  71. int buildingID = node.Float();
  72. /* Find the building and set its order. */
  73. std::map<int, Structure*>::iterator i2 = town.find(buildingID);
  74. if (i2 != (town.end()))
  75. i2->second->pos.z = itr++;
  76. else
  77. tlog3 << "Warning1: No building " << buildingID << " in the castle " << townID << std::endl;
  78. }
  79. // Read creatures belonging to that city
  80. level = 0;
  81. BOOST_FOREACH(const JsonNode &list, town_node["creatures"].Vector())
  82. {
  83. BOOST_FOREACH(const JsonNode &node, list.Vector())
  84. {
  85. towns[townID].creatures[level].push_back(node.Float());
  86. }
  87. level ++;
  88. }
  89. // Horde building creature level
  90. level = 0;
  91. BOOST_FOREACH(const JsonNode &node, town_node["horde"].Vector()) {
  92. towns[townID].hordeLvl[level] = node.Float();
  93. level ++;
  94. }
  95. // Misc.
  96. towns[townID].mageLevel = town_node["mage_guild"].Float();
  97. towns[townID].primaryRes = town_node["primary_resource"].Float();
  98. towns[townID].warMachine = town_node["war_machine"].Float();
  99. townID ++;
  100. }
  101. int group_num=0;
  102. // Iterate for each city
  103. BOOST_FOREACH(const JsonNode &town_node, config["town_groups"].Vector()) {
  104. townID = town_node["id"].Float();
  105. // Iterate for each group for that city
  106. BOOST_FOREACH(const JsonNode &group, town_node["groups"].Vector()) {
  107. group_num ++;
  108. // Iterate for each bulding value in the group
  109. BOOST_FOREACH(const JsonNode &value, group.Vector()) {
  110. int buildingID = value.Float();
  111. std::vector<std::map<int, Structure*> >::iterator i;
  112. std::map<int, Structure*>::iterator i2;
  113. if (townID >= 0) {
  114. if ((i = structures.begin() + townID) != structures.end()) {
  115. if ((i2=(i->find(buildingID)))!=(i->end()))
  116. i2->second->group = group_num;
  117. else
  118. tlog3 << "Warning3: No building "<<buildingID<<" in the castle "<<townID<<std::endl;
  119. }
  120. else
  121. tlog3 << "Warning3: Castle "<<townID<<" not defined."<<std::endl;
  122. } else {
  123. // Set group for selected building in ALL castles
  124. for (i=structures.begin();i!=structures.end();i++) {
  125. for(i2=i->begin(); i2!=i->end(); i2++) {
  126. if(i2->first == buildingID) {
  127. i2->second->group = group_num;
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. const std::string & CTown::Name() const
  138. {
  139. if(name.length())
  140. return name;
  141. else
  142. return VLC->generaltexth->townTypes[typeID];
  143. }
  144. const std::vector<std::string> & CTown::Names() const
  145. {
  146. if(names.size())
  147. return names;
  148. else
  149. return VLC->generaltexth->townNames[typeID];
  150. }