ObjectInfo.cpp 6.0 KB

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