ObjectConfig.cpp 4.9 KB

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