2
0

ObjectConfig.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "../VCMI_Lib.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, const CompoundMapObjectID & objid)
  27. {
  28. customObjects.push_back(object);
  29. auto & lastObject = customObjects.back();
  30. lastObject.setAllTemplates(objid.primaryID, objid.secondaryID);
  31. assert(lastObject.templates.size() > 0);
  32. logGlobal->info("Added custom object of type %d.%d", objid.primaryID, objid.secondaryID);
  33. }
  34. void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
  35. {
  36. // TODO: We need serializer utility for list of enum values
  37. static const boost::bimap<EObjectCategory, std::string> OBJECT_CATEGORY_STRINGS = boost::assign::list_of<boost::bimap<EObjectCategory, std::string>::relation>
  38. (EObjectCategory::OTHER, "other")
  39. (EObjectCategory::ALL, "all")
  40. (EObjectCategory::NONE, "none")
  41. (EObjectCategory::CREATURE_BANK, "creatureBank")
  42. (EObjectCategory::BONUS, "bonus")
  43. (EObjectCategory::DWELLING, "dwelling")
  44. (EObjectCategory::RESOURCE, "resource")
  45. (EObjectCategory::RESOURCE_GENERATOR, "resourceGenerator")
  46. (EObjectCategory::SPELL_SCROLL, "spellScroll")
  47. (EObjectCategory::RANDOM_ARTIFACT, "randomArtifact")
  48. (EObjectCategory::PANDORAS_BOX, "pandorasBox")
  49. (EObjectCategory::QUEST_ARTIFACT, "questArtifact")
  50. (EObjectCategory::SEER_HUT, "seerHut");
  51. // TODO: Separate into individual methods to enforce RAII destruction?
  52. {
  53. auto categories = handler.enterArray("bannedCategories");
  54. if (handler.saving)
  55. {
  56. for (const auto& category : bannedObjectCategories)
  57. {
  58. auto str = OBJECT_CATEGORY_STRINGS.left.at(category);
  59. categories.serializeString(categories.size(), str);
  60. }
  61. }
  62. else
  63. {
  64. std::vector<std::string> categoryNames;
  65. categories.serializeArray(categoryNames);
  66. for (const auto & categoryName : categoryNames)
  67. {
  68. auto it = OBJECT_CATEGORY_STRINGS.right.find(categoryName);
  69. if (it != OBJECT_CATEGORY_STRINGS.right.end())
  70. {
  71. bannedObjectCategories.push_back(it->second);
  72. }
  73. }
  74. }
  75. }
  76. // FIXME: Doesn't seem to use this field at all
  77. {
  78. auto bannedObjectData = handler.enterArray("bannedObjects");
  79. if (handler.saving)
  80. {
  81. // FIXME: Do we even need to serialize / store banned objects?
  82. /*
  83. for (const auto & object : bannedObjects)
  84. {
  85. // TODO: Translate id back to string?
  86. JsonNode node;
  87. node.String() = VLC->objtypeh->getHandlerFor(object.primaryID, object.secondaryID);
  88. // TODO: Check if AI-generated code is right
  89. }
  90. // handler.serializeRaw("bannedObjects", node, std::nullopt);
  91. */
  92. }
  93. else
  94. {
  95. std::vector<std::string> objectNames;
  96. bannedObjectData.serializeArray(objectNames);
  97. for (const auto & objectName : objectNames)
  98. {
  99. VLC->objtypeh->resolveObjectCompoundId(objectName,
  100. [this](CompoundMapObjectID objid)
  101. {
  102. addBannedObject(objid);
  103. }
  104. );
  105. }
  106. }
  107. }
  108. auto commonObjectData = handler.getCurrent()["commonObjects"].Vector();
  109. if (handler.saving)
  110. {
  111. //TODO?
  112. }
  113. else
  114. {
  115. for (const auto & objectConfig : commonObjectData)
  116. {
  117. auto objectName = objectConfig["id"].String();
  118. auto rmg = objectConfig["rmg"].Struct();
  119. // TODO: Use common code with default rmg config
  120. auto objectValue = rmg["value"].Integer();
  121. auto objectProbability = rmg["rarity"].Integer();
  122. auto objectMaxPerZone = rmg["zoneLimit"].Integer();
  123. if (objectMaxPerZone == 0)
  124. {
  125. objectMaxPerZone = std::numeric_limits<int>::max();
  126. }
  127. VLC->objtypeh->resolveObjectCompoundId(objectName,
  128. [this, objectValue, objectProbability, objectMaxPerZone](CompoundMapObjectID objid)
  129. {
  130. ObjectInfo object(objid.primaryID, objid.secondaryID);
  131. // TODO: Configure basic generateObject function
  132. object.value = objectValue;
  133. object.probability = objectProbability;
  134. object.maxPerZone = objectMaxPerZone;
  135. addCustomObject(object, objid);
  136. }
  137. );
  138. }
  139. }
  140. }
  141. const std::vector<ObjectInfo> & ObjectConfig::getConfiguredObjects() const
  142. {
  143. return customObjects;
  144. }
  145. const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
  146. {
  147. return bannedObjects;
  148. }
  149. const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
  150. {
  151. return bannedObjectCategories;
  152. }
  153. VCMI_LIB_NAMESPACE_END