CommonConstructors.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * CommonConstructors.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 "CommonConstructors.h"
  12. #include "../json/JsonRandom.h"
  13. #include "../constants/StringConstants.h"
  14. #include "../GameLibrary.h"
  15. #include "../callback/IGameInfoCallback.h"
  16. #include "../entities/faction/CTownHandler.h"
  17. #include "../entities/hero/CHeroClass.h"
  18. #include "../mapObjects/CGHeroInstance.h"
  19. #include "../mapObjects/CGTownInstance.h"
  20. #include "../mapObjects/MiscObjects.h"
  21. #include "../mapObjects/ObjectTemplate.h"
  22. #include "../mapping/TerrainTile.h"
  23. #include "../modding/IdentifierStorage.h"
  24. #include "../texts/TextIdentifier.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. bool CObstacleConstructor::isStaticObject()
  27. {
  28. return true;
  29. }
  30. bool CreatureInstanceConstructor::hasNameTextID() const
  31. {
  32. return true;
  33. }
  34. std::string CreatureInstanceConstructor::getNameTextID() const
  35. {
  36. return LIBRARY->creatures()->getByIndex(getSubIndex())->getNamePluralTextID();
  37. }
  38. void ResourceInstanceConstructor::initTypeData(const JsonNode & input)
  39. {
  40. config = input;
  41. resourceType = GameResID::GOLD; //set up fallback
  42. LIBRARY->identifiers()->requestIdentifierIfNotNull("resource", input["resource"], [&](si32 index)
  43. {
  44. resourceType = GameResID(index);
  45. });
  46. }
  47. bool ResourceInstanceConstructor::hasNameTextID() const
  48. {
  49. return true;
  50. }
  51. std::string ResourceInstanceConstructor::getNameTextID() const
  52. {
  53. return TextIdentifier("core", "restypes", resourceType.getNum()).get();
  54. }
  55. GameResID ResourceInstanceConstructor::getResourceType() const
  56. {
  57. return resourceType;
  58. }
  59. int ResourceInstanceConstructor::getAmountMultiplier() const
  60. {
  61. if (config["amountMultiplier"].isNull())
  62. return 1;
  63. return config["amountMultiplier"].Integer();
  64. }
  65. void ResourceInstanceConstructor::randomizeObject(CGResource * object, IGameRandomizer & gameRandomizer) const
  66. {
  67. if (object->amount != CGResource::RANDOM_AMOUNT)
  68. return;
  69. JsonRandom randomizer(object->cb, gameRandomizer);
  70. JsonRandom::Variables dummy;
  71. if (!config["amounts"].isNull())
  72. object->amount = randomizer.loadValue(config["amounts"], dummy, 0) * getAmountMultiplier();
  73. else
  74. object->amount = 5 * getAmountMultiplier();
  75. }
  76. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  77. {
  78. LIBRARY->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
  79. {
  80. faction = (*LIBRARY->townh)[index];
  81. });
  82. filtersJson = input["filters"];
  83. // change scope of "filters" to scope of object that is being loaded
  84. // since this filters require to resolve building ID's
  85. filtersJson.setModScope(input["faction"].getModScope());
  86. }
  87. void CTownInstanceConstructor::afterLoadFinalization()
  88. {
  89. assert(faction);
  90. for(const auto & entry : filtersJson.Struct())
  91. {
  92. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  93. {
  94. return BuildingID(LIBRARY->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value_or(-1));
  95. });
  96. }
  97. }
  98. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  99. {
  100. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  101. auto buildTest = [&](const BuildingID & id)
  102. {
  103. return town->hasBuilt(id);
  104. };
  105. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  106. }
  107. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  108. {
  109. obj->tempOwner = PlayerColor::NEUTRAL;
  110. }
  111. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, IGameRandomizer & gameRandomizer) const
  112. {
  113. auto templ = getOverride(object->cb->getTile(object->pos)->getTerrainID(), object);
  114. if(templ)
  115. object->appearance = templ;
  116. }
  117. bool CTownInstanceConstructor::hasNameTextID() const
  118. {
  119. return true;
  120. }
  121. std::string CTownInstanceConstructor::getNameTextID() const
  122. {
  123. return faction->getNameTextID();
  124. }
  125. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  126. {
  127. LIBRARY->identifiers()->requestIdentifier(
  128. "heroClass",
  129. input["heroClass"],
  130. [&](si32 index) { heroClass = HeroClassID(index).toHeroClass(); });
  131. for (const auto & [name, config] : input["filters"].Struct())
  132. {
  133. HeroFilter filter;
  134. filter.allowFemale = config["female"].Bool();
  135. filter.allowMale = config["male"].Bool();
  136. filters[name] = filter;
  137. if (!config["hero"].isNull())
  138. {
  139. LIBRARY->identifiers()->requestIdentifier( "hero", config["hero"], [this, templateName = name](si32 index) {
  140. filters.at(templateName).fixedHero = HeroTypeID(index);
  141. });
  142. }
  143. }
  144. }
  145. std::shared_ptr<const ObjectTemplate> CHeroInstanceConstructor::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  146. {
  147. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  148. std::vector<std::shared_ptr<const ObjectTemplate>> allTemplates = getTemplates();
  149. std::shared_ptr<const ObjectTemplate> candidateFullMatch;
  150. std::shared_ptr<const ObjectTemplate> candidateGenderMatch;
  151. std::shared_ptr<const ObjectTemplate> candidateBase;
  152. assert(hero->gender != EHeroGender::DEFAULT);
  153. for (const auto & templ : allTemplates)
  154. {
  155. if (filters.count(templ->stringID))
  156. {
  157. const auto & filter = filters.at(templ->stringID);
  158. if (filter.fixedHero.hasValue())
  159. {
  160. if (filter.fixedHero == hero->getHeroTypeID())
  161. candidateFullMatch = templ;
  162. }
  163. else if (filter.allowMale)
  164. {
  165. if (hero->gender == EHeroGender::MALE)
  166. candidateGenderMatch = templ;
  167. }
  168. else if (filter.allowFemale)
  169. {
  170. if (hero->gender == EHeroGender::FEMALE)
  171. candidateGenderMatch = templ;
  172. }
  173. else
  174. {
  175. candidateBase = templ;
  176. }
  177. }
  178. else
  179. {
  180. candidateBase = templ;
  181. }
  182. }
  183. if (candidateFullMatch)
  184. return candidateFullMatch;
  185. if (candidateGenderMatch)
  186. return candidateGenderMatch;
  187. return candidateBase;
  188. }
  189. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, IGameRandomizer & gameRandomizer) const
  190. {
  191. }
  192. bool CHeroInstanceConstructor::hasNameTextID() const
  193. {
  194. return true;
  195. }
  196. std::string CHeroInstanceConstructor::getNameTextID() const
  197. {
  198. return heroClass->getNameTextID();
  199. }
  200. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  201. {
  202. layer = EPathfindingLayer::SAIL;
  203. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  204. if(pos != -1)
  205. layer = EPathfindingLayer(pos);
  206. else
  207. logMod->error("Unknown layer %s found in boat!", input["layer"].String());
  208. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  209. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  210. actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
  211. overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
  212. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  213. flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
  214. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  215. }
  216. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  217. {
  218. boat->layer = layer;
  219. boat->actualAnimation = actualAnimation;
  220. boat->overlayAnimation = overlayAnimation;
  221. boat->flagAnimations = flagAnimations;
  222. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  223. boat->onboardVisitAllowed = onboardVisitAllowed;
  224. for(auto & b : bonuses)
  225. boat->addNewBonus(b);
  226. }
  227. AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
  228. {
  229. return actualAnimation;
  230. }
  231. VCMI_LIB_NAMESPACE_END