CommonConstructors.cpp 8.5 KB

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