CommonConstructors.cpp 7.4 KB

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