CommonConstructors.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. kingdomOverviewImage = AnimationPath::fromJson(config["kingdomOverviewImage"]);
  92. }
  93. GameResID MineInstanceConstructor::getResourceType() const
  94. {
  95. return resourceType;
  96. }
  97. ui32 MineInstanceConstructor::getDefaultQuantity() const
  98. {
  99. return defaultQuantity;
  100. }
  101. std::string MineInstanceConstructor::getDescriptionTextID() const
  102. {
  103. return TextIdentifier(getBaseTextID(), "description").get();
  104. }
  105. std::string MineInstanceConstructor::getDescriptionTranslated() const
  106. {
  107. return LIBRARY->generaltexth->translate(getDescriptionTextID());
  108. }
  109. AnimationPath MineInstanceConstructor::getKingdomOverviewImage() const
  110. {
  111. return kingdomOverviewImage;
  112. }
  113. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  114. {
  115. LIBRARY->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
  116. {
  117. faction = (*LIBRARY->townh)[index];
  118. });
  119. filtersJson = input["filters"];
  120. // change scope of "filters" to scope of object that is being loaded
  121. // since this filters require to resolve building ID's
  122. filtersJson.setModScope(input["faction"].getModScope());
  123. }
  124. void CTownInstanceConstructor::afterLoadFinalization()
  125. {
  126. assert(faction);
  127. for(const auto & entry : filtersJson.Struct())
  128. {
  129. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  130. {
  131. return BuildingID(LIBRARY->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value_or(-1));
  132. });
  133. }
  134. }
  135. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  136. {
  137. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  138. auto buildTest = [&](const BuildingID & id)
  139. {
  140. return town->hasBuilt(id);
  141. };
  142. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  143. }
  144. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  145. {
  146. obj->tempOwner = PlayerColor::NEUTRAL;
  147. }
  148. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, IGameRandomizer & gameRandomizer) const
  149. {
  150. auto templ = getOverride(object->cb->getTile(object->pos)->getTerrainID(), object);
  151. if(templ)
  152. object->appearance = templ;
  153. }
  154. bool CTownInstanceConstructor::hasNameTextID() const
  155. {
  156. return true;
  157. }
  158. std::string CTownInstanceConstructor::getNameTextID() const
  159. {
  160. return faction->getNameTextID();
  161. }
  162. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  163. {
  164. LIBRARY->identifiers()->requestIdentifier(
  165. "heroClass",
  166. input["heroClass"],
  167. [&](si32 index) { heroClass = HeroClassID(index).toHeroClass(); });
  168. for (const auto & [name, config] : input["filters"].Struct())
  169. {
  170. HeroFilter filter;
  171. filter.allowFemale = config["female"].Bool();
  172. filter.allowMale = config["male"].Bool();
  173. filters[name] = filter;
  174. if (!config["hero"].isNull())
  175. {
  176. LIBRARY->identifiers()->requestIdentifier( "hero", config["hero"], [this, templateName = name](si32 index) {
  177. filters.at(templateName).fixedHero = HeroTypeID(index);
  178. });
  179. }
  180. }
  181. }
  182. std::shared_ptr<const ObjectTemplate> CHeroInstanceConstructor::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  183. {
  184. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  185. std::vector<std::shared_ptr<const ObjectTemplate>> allTemplates = getTemplates();
  186. std::shared_ptr<const ObjectTemplate> candidateFullMatch;
  187. std::shared_ptr<const ObjectTemplate> candidateGenderMatch;
  188. std::shared_ptr<const ObjectTemplate> candidateBase;
  189. assert(hero->gender != EHeroGender::DEFAULT);
  190. for (const auto & templ : allTemplates)
  191. {
  192. if (filters.count(templ->stringID))
  193. {
  194. const auto & filter = filters.at(templ->stringID);
  195. if (filter.fixedHero.hasValue())
  196. {
  197. if (filter.fixedHero == hero->getHeroTypeID())
  198. candidateFullMatch = templ;
  199. }
  200. else if (filter.allowMale)
  201. {
  202. if (hero->gender == EHeroGender::MALE)
  203. candidateGenderMatch = templ;
  204. }
  205. else if (filter.allowFemale)
  206. {
  207. if (hero->gender == EHeroGender::FEMALE)
  208. candidateGenderMatch = templ;
  209. }
  210. else
  211. {
  212. candidateBase = templ;
  213. }
  214. }
  215. else
  216. {
  217. candidateBase = templ;
  218. }
  219. }
  220. if (candidateFullMatch)
  221. return candidateFullMatch;
  222. if (candidateGenderMatch)
  223. return candidateGenderMatch;
  224. return candidateBase;
  225. }
  226. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, IGameRandomizer & gameRandomizer) const
  227. {
  228. }
  229. bool CHeroInstanceConstructor::hasNameTextID() const
  230. {
  231. return true;
  232. }
  233. std::string CHeroInstanceConstructor::getNameTextID() const
  234. {
  235. return heroClass->getNameTextID();
  236. }
  237. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  238. {
  239. layer = EPathfindingLayer::SAIL;
  240. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  241. if(pos != -1)
  242. layer = EPathfindingLayer(pos);
  243. else
  244. logMod->error("Unknown layer %s found in boat!", input["layer"].String());
  245. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  246. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  247. actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
  248. overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
  249. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  250. flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
  251. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  252. }
  253. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  254. {
  255. boat->layer = layer;
  256. boat->actualAnimation = actualAnimation;
  257. boat->overlayAnimation = overlayAnimation;
  258. boat->flagAnimations = flagAnimations;
  259. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  260. boat->onboardVisitAllowed = onboardVisitAllowed;
  261. for(auto & b : bonuses)
  262. boat->addNewBonus(b);
  263. }
  264. AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
  265. {
  266. return actualAnimation;
  267. }
  268. VCMI_LIB_NAMESPACE_END