ObjectConfig.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * ObjectConfig.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include <boost/bimap.hpp>
  12. #include <boost/assign.hpp>
  13. #include "ObjectInfo.h"
  14. #include "ObjectConfig.h"
  15. #include "../GameLibrary.h"
  16. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  17. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  18. #include "../serializer/JsonSerializeFormat.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
  21. {
  22. // FIXME: We do not need to store the object info, just the id
  23. bannedObjects.push_back(objid);
  24. logGlobal->info("Banned object of type %d.%d", objid.primaryID, objid.secondaryID);
  25. }
  26. void ObjectConfig::addCustomObject(const ObjectInfo & object)
  27. {
  28. customObjects.push_back(object);
  29. auto & lastObject = customObjects.back();
  30. lastObject.setAllTemplates(object.primaryID, object.secondaryID);
  31. // also ban object to prevent default configuration from being used in this zone
  32. bannedObjects.push_back(CompoundMapObjectID(object.primaryID, object.secondaryID));
  33. assert(lastObject.templates.size() > 0);
  34. logGlobal->info("Added custom object of type %d.%d", object.primaryID, object.secondaryID);
  35. }
  36. void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
  37. {
  38. static const std::map<std::string, EObjectCategory> OBJECT_CATEGORY_STRINGS = {
  39. { "other", EObjectCategory::OTHER},
  40. { "all", EObjectCategory::ALL},
  41. { "none", EObjectCategory::NONE},
  42. { "creatureBank", EObjectCategory::CREATURE_BANK},
  43. { "bonus", EObjectCategory::BONUS},
  44. { "dwelling", EObjectCategory::DWELLING},
  45. { "resource", EObjectCategory::RESOURCE},
  46. { "resourceGenerator", EObjectCategory::RESOURCE_GENERATOR},
  47. { "spellScroll", EObjectCategory::SPELL_SCROLL},
  48. { "randomArtifact", EObjectCategory::RANDOM_ARTIFACT},
  49. { "pandorasBox", EObjectCategory::PANDORAS_BOX},
  50. { "questArtifact", EObjectCategory::QUEST_ARTIFACT},
  51. { "seerHut", EObjectCategory::SEER_HUT}
  52. };
  53. const JsonNode & config = handler.getCurrent();
  54. const JsonNode & configBannedCategories = config["bannedCategories"];
  55. const JsonNode & configBannedObjects = config["bannedObjects"];
  56. const JsonNode & configCommonObjects = config["commonObjects"];
  57. for(const auto & node : configBannedCategories.Vector())
  58. {
  59. auto it = OBJECT_CATEGORY_STRINGS.find(node.String());
  60. if(it != OBJECT_CATEGORY_STRINGS.end())
  61. bannedObjectCategories.push_back(it->second);
  62. }
  63. if(configBannedObjects.isVector())
  64. {
  65. // MOD COMPATIBILITY - 1.6 format
  66. for(const auto & node : configBannedObjects.Vector())
  67. {
  68. LIBRARY->objtypeh->resolveObjectCompoundId(node.String(),
  69. [this](CompoundMapObjectID objid)
  70. {
  71. addBannedObject(objid);
  72. }
  73. );
  74. }
  75. }
  76. else
  77. {
  78. for(const auto & node : configBannedObjects.Struct())
  79. {
  80. LIBRARY->identifiers()->requestIdentifierIfFound(node.second.getModScope(), "object", node.first, [this, node](int primaryID)
  81. {
  82. if (node.second.Bool())
  83. addBannedObject(CompoundMapObjectID(primaryID, -1));
  84. for (const auto & subNode : node.second.Struct())
  85. {
  86. const std::string jsonKey = LIBRARY->objtypeh->getJsonKey(primaryID);
  87. LIBRARY->identifiers()->requestIdentifierIfFound(node.second.getModScope(), jsonKey, subNode.first, [this, primaryID](int secondaryID)
  88. {
  89. addBannedObject(CompoundMapObjectID(primaryID, secondaryID));
  90. });
  91. }
  92. });
  93. }
  94. }
  95. for (const auto & objectConfig : configCommonObjects.Vector())
  96. {
  97. auto rmg = objectConfig["rmg"].Struct();
  98. // TODO: Use common code with default rmg config
  99. ObjectInfo object;
  100. // TODO: Configure basic generateObject function
  101. object.value = rmg["value"].Integer();
  102. object.probability = rmg["rarity"].Integer();
  103. object.maxPerZone = rmg["zoneLimit"].Integer();
  104. if (object.maxPerZone == 0)
  105. object.maxPerZone = std::numeric_limits<int>::max();
  106. if (objectConfig["id"].isNull())
  107. {
  108. LIBRARY->identifiers()->requestIdentifierIfFound("object", objectConfig["type"], [this, object, objectConfig](int primaryID)
  109. {
  110. if (objectConfig["subtype"].isNull())
  111. {
  112. auto objectWithID = object;
  113. objectWithID.primaryID = primaryID;
  114. objectWithID.secondaryID = 0;
  115. addCustomObject(object);
  116. }
  117. else
  118. {
  119. const std::string jsonKey = LIBRARY->objtypeh->getJsonKey(primaryID);
  120. LIBRARY->identifiers()->requestIdentifierIfFound(jsonKey, objectConfig["subtype"], [this, primaryID, object](int secondaryID)
  121. {
  122. auto objectWithID = object;
  123. objectWithID.primaryID = primaryID;
  124. objectWithID.secondaryID = secondaryID;
  125. addCustomObject(object);
  126. });
  127. }
  128. });
  129. }
  130. else
  131. {
  132. // MOD COMPATIBILITY - 1.6 format
  133. auto objectName = objectConfig["id"].String();
  134. LIBRARY->objtypeh->resolveObjectCompoundId(objectName, [this, object](CompoundMapObjectID objid)
  135. {
  136. auto objectWithID = object;
  137. objectWithID.primaryID = objid.primaryID;
  138. objectWithID.secondaryID = objid.secondaryID;
  139. if (objectWithID.secondaryID == -1)
  140. objectWithID.secondaryID = 0;
  141. addCustomObject(objectWithID);
  142. });
  143. }
  144. }
  145. }
  146. const std::vector<ObjectInfo> & ObjectConfig::getConfiguredObjects() const
  147. {
  148. return customObjects;
  149. }
  150. const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
  151. {
  152. return bannedObjects;
  153. }
  154. const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
  155. {
  156. return bannedObjectCategories;
  157. }
  158. VCMI_LIB_NAMESPACE_END