DwellingInstanceConstructor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * DwellingInstanceConstructor.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 "DwellingInstanceConstructor.h"
  12. #include "../CCreatureHandler.h"
  13. #include "../texts/CGeneralTextHandler.h"
  14. #include "../json/JsonRandom.h"
  15. #include "../GameLibrary.h"
  16. #include "../mapObjects/army/CStackInstance.h"
  17. #include "../mapObjects/CGDwelling.h"
  18. #include "../mapObjects/ObjectTemplate.h"
  19. #include "../modding/IdentifierStorage.h"
  20. #include "../CConfigHandler.h"
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. bool DwellingInstanceConstructor::hasNameTextID() const
  23. {
  24. return true;
  25. }
  26. void DwellingInstanceConstructor::initTypeData(const JsonNode & input)
  27. {
  28. if (input.Struct().count("name") == 0)
  29. logMod->warn("Dwelling %s missing name!", getJsonKey());
  30. LIBRARY->generaltexth->registerString( input.getModScope(), getNameTextID(), input["name"]);
  31. const JsonVector & levels = input["creatures"].Vector();
  32. const auto totalLevels = levels.size();
  33. availableCreatures.resize(totalLevels);
  34. for(int currentLevel = 0; currentLevel < totalLevels; currentLevel++)
  35. {
  36. const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
  37. const auto creaturesNumber = creaturesOnLevel.size();
  38. availableCreatures[currentLevel].resize(creaturesNumber);
  39. for(int currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
  40. {
  41. LIBRARY->identifiers()->requestIdentifier("creature", creaturesOnLevel[currentCreature], [this, currentLevel, currentCreature] (si32 index)
  42. {
  43. availableCreatures.at(currentLevel).at(currentCreature) = CreatureID(index).toCreature();
  44. });
  45. }
  46. assert(!availableCreatures[currentLevel].empty());
  47. }
  48. guards = input["guards"];
  49. bannedForRandomDwelling = input["bannedForRandomDwelling"].Bool();
  50. kingdomOverviewImage = AnimationPath::fromJson(input["kingdomOverviewImage"]);
  51. for (const auto & mapTemplate : getTemplates())
  52. onTemplateAdded(mapTemplate);
  53. }
  54. void DwellingInstanceConstructor::onTemplateAdded(const std::shared_ptr<const ObjectTemplate> mapTemplate)
  55. {
  56. if (bannedForRandomDwelling || settings["mods"]["validation"].String() == "off")
  57. return;
  58. bool invalidForRandomDwelling = false;
  59. int3 corner = mapTemplate->getCornerOffset();
  60. for (const auto & tile : mapTemplate->getBlockedOffsets())
  61. invalidForRandomDwelling |= (tile.x != -corner.x && tile.x != -corner.x-1) || (tile.y != -corner.y && tile.y != -corner.y-1);
  62. for (const auto & tile : {mapTemplate->getVisitableOffset()})
  63. invalidForRandomDwelling |= (tile.x != corner.x && tile.x != corner.x+1) || tile.y != corner.y;
  64. invalidForRandomDwelling |= !mapTemplate->isBlockedAt(corner.x+0, corner.y) && !mapTemplate->isVisibleAt(corner.x+0, corner.y);
  65. invalidForRandomDwelling |= !mapTemplate->isBlockedAt(corner.x+1, corner.y) && !mapTemplate->isVisibleAt(corner.x+1, corner.y);
  66. if (invalidForRandomDwelling)
  67. logMod->warn("Dwelling %s has template %s which is not valid for a random dwelling! Dwellings must not block tiles outside 2x2 range and must be visitable in bottom row. Change dwelling mask or mark dwelling as 'bannedForRandomDwelling'", getJsonKey(), mapTemplate->animationFile.getOriginalName());
  68. }
  69. bool DwellingInstanceConstructor::isBannedForRandomDwelling() const
  70. {
  71. return bannedForRandomDwelling;
  72. }
  73. bool DwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  74. {
  75. return false;
  76. }
  77. void DwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
  78. {
  79. obj->creatures.resize(availableCreatures.size());
  80. for(const auto & entry : availableCreatures)
  81. {
  82. for(const CCreature * cre : entry)
  83. obj->creatures.back().second.push_back(cre->getId());
  84. }
  85. }
  86. void DwellingInstanceConstructor::randomizeObject(CGDwelling * dwelling, IGameRandomizer & gameRandomizer) const
  87. {
  88. JsonRandom randomizer(dwelling->cb, gameRandomizer);
  89. dwelling->creatures.clear();
  90. dwelling->creatures.reserve(availableCreatures.size());
  91. for(const auto & entry : availableCreatures)
  92. {
  93. dwelling->creatures.resize(dwelling->creatures.size() + 1);
  94. for(const CCreature * cre : entry)
  95. dwelling->creatures.back().second.push_back(cre->getId());
  96. }
  97. bool guarded = false;
  98. if(guards.getType() == JsonNode::JsonType::DATA_BOOL)
  99. {
  100. //simple switch
  101. if(guards.Bool())
  102. {
  103. guarded = true;
  104. }
  105. }
  106. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR)
  107. {
  108. //custom guards (eg. Elemental Conflux)
  109. JsonRandom::Variables emptyVariables;
  110. for(auto & stack : randomizer.loadCreatures(guards, emptyVariables))
  111. {
  112. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, stack.getId(), stack.getCount()));
  113. }
  114. }
  115. else if (dwelling->ID == Obj::CREATURE_GENERATOR1 || dwelling->ID == Obj::CREATURE_GENERATOR4)
  116. {
  117. //default condition - this is dwelling with creatures of level 5 or higher
  118. for(auto creatureEntry : availableCreatures)
  119. {
  120. if(creatureEntry.at(0)->getLevel() >= 5)
  121. {
  122. guarded = true;
  123. break;
  124. }
  125. }
  126. }
  127. if(guarded)
  128. {
  129. for(auto creatureEntry : availableCreatures)
  130. {
  131. const CCreature * crea = creatureEntry.at(0);
  132. dwelling->putStack(SlotID(dwelling->stacksCount()), std::make_unique<CStackInstance>(dwelling->cb, crea->getId(), crea->getGrowth() * 3));
  133. }
  134. }
  135. }
  136. bool DwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  137. {
  138. for(const auto & entry : availableCreatures)
  139. {
  140. for(const CCreature * cre : entry)
  141. if(crea == cre)
  142. return true;
  143. }
  144. return false;
  145. }
  146. std::vector<const CCreature *> DwellingInstanceConstructor::getProducedCreatures() const
  147. {
  148. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  149. for(const auto & entry : availableCreatures)
  150. {
  151. for(const CCreature * cre : entry)
  152. creatures.push_back(cre);
  153. }
  154. return creatures;
  155. }
  156. AnimationPath DwellingInstanceConstructor::getKingdomOverviewImage() const
  157. {
  158. return kingdomOverviewImage;
  159. }
  160. VCMI_LIB_NAMESPACE_END