ObjectInfo.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * ObjectInfo.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 "../VCMI_Lib.h"
  15. #include "../mapObjectConstructors/CObjectClassesHandler.h"
  16. #include "../mapObjectConstructors/AObjectTypeHandler.h"
  17. #include "../serializer/JsonSerializeFormat.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. ObjectInfo::ObjectInfo():
  20. destroyObject([](CGObjectInstance * obj){}),
  21. maxPerZone(std::numeric_limits<ui32>::max())
  22. {
  23. }
  24. ObjectInfo::ObjectInfo(const ObjectInfo & other)
  25. {
  26. templates = other.templates;
  27. value = other.value;
  28. probability = other.probability;
  29. maxPerZone = other.maxPerZone;
  30. generateObject = other.generateObject;
  31. destroyObject = other.destroyObject;
  32. }
  33. ObjectInfo & ObjectInfo::operator=(const ObjectInfo & other)
  34. {
  35. if (this == &other)
  36. return *this;
  37. templates = other.templates;
  38. value = other.value;
  39. probability = other.probability;
  40. maxPerZone = other.maxPerZone;
  41. generateObject = other.generateObject;
  42. destroyObject = other.destroyObject;
  43. return *this;
  44. }
  45. void ObjectInfo::setTemplates(MapObjectID type, MapObjectSubID subtype, TerrainId terrainType)
  46. {
  47. auto templHandler = VLC->objtypeh->getHandlerFor(type, subtype);
  48. if(!templHandler)
  49. return;
  50. templates = templHandler->getTemplates(terrainType);
  51. }
  52. void ObjectConfig::addBannedObject(const CompoundMapObjectID & objid)
  53. {
  54. // FIXME: We do not need to store the object info, just the id
  55. bannedObjects.push_back(objid);
  56. logGlobal->info("Banned object of type %d.%d", objid.primaryID, objid.secondaryID);
  57. }
  58. void ObjectConfig::serializeJson(JsonSerializeFormat & handler)
  59. {
  60. // TODO: We need serializer utility for list of enum values
  61. static const boost::bimap<EObjectCategory, std::string> OBJECT_CATEGORY_STRINGS = boost::assign::list_of<boost::bimap<EObjectCategory, std::string>::relation>
  62. (EObjectCategory::OTHER, "other")
  63. (EObjectCategory::ALL, "all")
  64. (EObjectCategory::NONE, "none")
  65. (EObjectCategory::CREATURE_BANK, "creatureBank")
  66. (EObjectCategory::PERMANENT_BONUS, "permanentBonus")
  67. (EObjectCategory::NEXT_BATTLE_BONUS, "nextBattleBonus")
  68. (EObjectCategory::DWELLING, "dwelling")
  69. (EObjectCategory::RESOURCE, "resource")
  70. (EObjectCategory::RESOURCE_GENERATOR, "resourceGenerator")
  71. (EObjectCategory::SPELL_SCROLL, "spellScroll")
  72. (EObjectCategory::RANDOM_ARTIFACT, "randomArtifact")
  73. (EObjectCategory::PANDORAS_BOX, "pandorasBox")
  74. (EObjectCategory::QUEST_ARTIFACT, "questArtifact");
  75. auto categories = handler.enterArray("bannedCategories");
  76. if (handler.saving)
  77. {
  78. for (const auto& category : bannedObjectCategories)
  79. {
  80. auto str = OBJECT_CATEGORY_STRINGS.left.at(category);
  81. categories.serializeString(categories.size(), str);
  82. }
  83. }
  84. else
  85. {
  86. std::vector<std::string> categoryNames;
  87. categories.serializeArray(categoryNames);
  88. for (const auto & categoryName : categoryNames)
  89. {
  90. auto it = OBJECT_CATEGORY_STRINGS.right.find(categoryName);
  91. if (it != OBJECT_CATEGORY_STRINGS.right.end())
  92. {
  93. bannedObjectCategories.push_back(it->second);
  94. }
  95. }
  96. }
  97. auto bannedObjectData = handler.enterArray("bannedObjects");
  98. if (handler.saving)
  99. {
  100. // FIXME: Do we even need to serialize / store banned objects?
  101. /*
  102. for (const auto & object : bannedObjects)
  103. {
  104. // TODO: Translate id back to string?
  105. JsonNode node;
  106. node.String() = VLC->objtypeh->getHandlerFor(object.primaryID, object.secondaryID);
  107. // TODO: Check if AI-generated code is right
  108. }
  109. // handler.serializeRaw("bannedObjects", node, std::nullopt);
  110. */
  111. }
  112. else
  113. {
  114. /*
  115. auto zonesData = handler.enterStruct("zones");
  116. for(const auto & idAndZone : zonesData->getCurrent().Struct())
  117. {
  118. auto guard = handler.enterStruct(idAndZone.first);
  119. auto zone = std::make_shared<ZoneOptions>();
  120. zone->setId(decodeZoneId(idAndZone.first));
  121. zone->serializeJson(handler);
  122. zones[zone->getId()] = zone;
  123. }
  124. */
  125. std::vector<std::string> objectNames;
  126. bannedObjectData.serializeArray(objectNames);
  127. for (const auto & objectName : objectNames)
  128. {
  129. VLC->objtypeh->resolveObjectCompoundId(objectName,
  130. [this](CompoundMapObjectID objid)
  131. {
  132. addBannedObject(objid);
  133. }
  134. );
  135. }
  136. }
  137. }
  138. const std::vector<ObjectInfo> & ObjectConfig::getCustomObjects() const
  139. {
  140. return customObjects;
  141. }
  142. const std::vector<CompoundMapObjectID> & ObjectConfig::getBannedObjects() const
  143. {
  144. return bannedObjects;
  145. }
  146. const std::vector<ObjectConfig::EObjectCategory> & ObjectConfig::getBannedObjectCategories() const
  147. {
  148. return bannedObjectCategories;
  149. }
  150. VCMI_LIB_NAMESPACE_END