CommonConstructors.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "../texts/CGeneralTextHandler.h"
  13. #include "../json/JsonRandom.h"
  14. #include "../constants/StringConstants.h"
  15. #include "../TerrainHandler.h"
  16. #include "../GameLibrary.h"
  17. #include "../CConfigHandler.h"
  18. #include "../callback/IGameInfoCallback.h"
  19. #include "../entities/faction/CTownHandler.h"
  20. #include "../entities/hero/CHeroClass.h"
  21. #include "../json/JsonUtils.h"
  22. #include "../mapObjects/CGHeroInstance.h"
  23. #include "../mapObjects/CGMarket.h"
  24. #include "../mapObjects/CGTownInstance.h"
  25. #include "../mapObjects/MiscObjects.h"
  26. #include "../mapObjects/ObjectTemplate.h"
  27. #include "../mapping/TerrainTile.h"
  28. #include "../modding/IdentifierStorage.h"
  29. VCMI_LIB_NAMESPACE_BEGIN
  30. bool CObstacleConstructor::isStaticObject()
  31. {
  32. return true;
  33. }
  34. bool CreatureInstanceConstructor::hasNameTextID() const
  35. {
  36. return true;
  37. }
  38. std::string CreatureInstanceConstructor::getNameTextID() const
  39. {
  40. return LIBRARY->creatures()->getByIndex(getSubIndex())->getNamePluralTextID();
  41. }
  42. void ResourceInstanceConstructor::initTypeData(const JsonNode & input)
  43. {
  44. config = input;
  45. resourceType = GameResID::GOLD; //set up fallback
  46. LIBRARY->identifiers()->requestIdentifierIfNotNull("resource", input["resource"], [&](si32 index)
  47. {
  48. resourceType = GameResID(index);
  49. });
  50. }
  51. bool ResourceInstanceConstructor::hasNameTextID() const
  52. {
  53. return true;
  54. }
  55. std::string ResourceInstanceConstructor::getNameTextID() const
  56. {
  57. return TextIdentifier("core", "restypes", resourceType.getNum()).get();
  58. }
  59. GameResID ResourceInstanceConstructor::getResourceType() const
  60. {
  61. return resourceType;
  62. }
  63. int ResourceInstanceConstructor::getAmountMultiplier() const
  64. {
  65. if (config["amountMultiplier"].isNull())
  66. return 1;
  67. return config["amountMultiplier"].Integer();
  68. }
  69. void ResourceInstanceConstructor::randomizeObject(CGResource * object, IGameRandomizer & gameRandomizer) const
  70. {
  71. if (object->amount != CGResource::RANDOM_AMOUNT)
  72. return;
  73. JsonRandom randomizer(object->cb, gameRandomizer);
  74. JsonRandom::Variables dummy;
  75. if (!config["amounts"].isNull())
  76. object->amount = randomizer.loadValue(config["amounts"], dummy, 0) * getAmountMultiplier();
  77. else
  78. object->amount = 5 * getAmountMultiplier();
  79. }
  80. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  81. {
  82. LIBRARY->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
  83. {
  84. faction = (*LIBRARY->townh)[index];
  85. });
  86. filtersJson = input["filters"];
  87. // change scope of "filters" to scope of object that is being loaded
  88. // since this filters require to resolve building ID's
  89. filtersJson.setModScope(input["faction"].getModScope());
  90. }
  91. void CTownInstanceConstructor::afterLoadFinalization()
  92. {
  93. assert(faction);
  94. for(const auto & entry : filtersJson.Struct())
  95. {
  96. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  97. {
  98. return BuildingID(LIBRARY->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value_or(-1));
  99. });
  100. }
  101. }
  102. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  103. {
  104. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  105. auto buildTest = [&](const BuildingID & id)
  106. {
  107. return town->hasBuilt(id);
  108. };
  109. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  110. }
  111. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  112. {
  113. obj->tempOwner = PlayerColor::NEUTRAL;
  114. }
  115. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, IGameRandomizer & gameRandomizer) const
  116. {
  117. auto templ = getOverride(object->cb->getTile(object->pos)->getTerrainID(), object);
  118. if(templ)
  119. object->appearance = templ;
  120. }
  121. bool CTownInstanceConstructor::hasNameTextID() const
  122. {
  123. return true;
  124. }
  125. std::string CTownInstanceConstructor::getNameTextID() const
  126. {
  127. return faction->getNameTextID();
  128. }
  129. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  130. {
  131. LIBRARY->identifiers()->requestIdentifier(
  132. "heroClass",
  133. input["heroClass"],
  134. [&](si32 index) { heroClass = HeroClassID(index).toHeroClass(); });
  135. for (const auto & [name, config] : input["filters"].Struct())
  136. {
  137. HeroFilter filter;
  138. filter.allowFemale = config["female"].Bool();
  139. filter.allowMale = config["male"].Bool();
  140. filters[name] = filter;
  141. if (!config["hero"].isNull())
  142. {
  143. LIBRARY->identifiers()->requestIdentifier( "hero", config["hero"], [this, templateName = name](si32 index) {
  144. filters.at(templateName).fixedHero = HeroTypeID(index);
  145. });
  146. }
  147. }
  148. }
  149. std::shared_ptr<const ObjectTemplate> CHeroInstanceConstructor::getOverride(TerrainId terrainType, const CGObjectInstance * object) const
  150. {
  151. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  152. std::vector<std::shared_ptr<const ObjectTemplate>> allTemplates = getTemplates();
  153. std::shared_ptr<const ObjectTemplate> candidateFullMatch;
  154. std::shared_ptr<const ObjectTemplate> candidateGenderMatch;
  155. std::shared_ptr<const ObjectTemplate> candidateBase;
  156. assert(hero->gender != EHeroGender::DEFAULT);
  157. for (const auto & templ : allTemplates)
  158. {
  159. if (filters.count(templ->stringID))
  160. {
  161. const auto & filter = filters.at(templ->stringID);
  162. if (filter.fixedHero.hasValue())
  163. {
  164. if (filter.fixedHero == hero->getHeroTypeID())
  165. candidateFullMatch = templ;
  166. }
  167. else if (filter.allowMale)
  168. {
  169. if (hero->gender == EHeroGender::MALE)
  170. candidateGenderMatch = templ;
  171. }
  172. else if (filter.allowFemale)
  173. {
  174. if (hero->gender == EHeroGender::FEMALE)
  175. candidateGenderMatch = templ;
  176. }
  177. else
  178. {
  179. candidateBase = templ;
  180. }
  181. }
  182. else
  183. {
  184. candidateBase = templ;
  185. }
  186. }
  187. if (candidateFullMatch)
  188. return candidateFullMatch;
  189. if (candidateGenderMatch)
  190. return candidateGenderMatch;
  191. return candidateBase;
  192. }
  193. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, IGameRandomizer & gameRandomizer) const
  194. {
  195. }
  196. bool CHeroInstanceConstructor::hasNameTextID() const
  197. {
  198. return true;
  199. }
  200. std::string CHeroInstanceConstructor::getNameTextID() const
  201. {
  202. return heroClass->getNameTextID();
  203. }
  204. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  205. {
  206. layer = EPathfindingLayer::SAIL;
  207. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  208. if(pos != -1)
  209. layer = EPathfindingLayer(pos);
  210. else
  211. logMod->error("Unknown layer %s found in boat!", input["layer"].String());
  212. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  213. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  214. actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
  215. overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
  216. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  217. flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
  218. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  219. }
  220. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  221. {
  222. boat->layer = layer;
  223. boat->actualAnimation = actualAnimation;
  224. boat->overlayAnimation = overlayAnimation;
  225. boat->flagAnimations = flagAnimations;
  226. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  227. boat->onboardVisitAllowed = onboardVisitAllowed;
  228. for(auto & b : bonuses)
  229. boat->addNewBonus(b);
  230. }
  231. AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
  232. {
  233. return actualAnimation;
  234. }
  235. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  236. {
  237. if (settings["mods"]["validation"].String() != "off")
  238. JsonUtils::validate(input, "vcmi:market", getJsonKey());
  239. if (!input["description"].isNull())
  240. {
  241. std::string description = input["description"].String();
  242. descriptionTextID = TextIdentifier(getBaseTextID(), "description").get();
  243. LIBRARY->generaltexth->registerString( input.getModScope(), descriptionTextID, input["description"]);
  244. }
  245. if (!input["speech"].isNull())
  246. {
  247. std::string speech = input["speech"].String();
  248. if (!speech.empty() && speech.at(0) == '@')
  249. {
  250. speechTextID = speech.substr(1);
  251. }
  252. else
  253. {
  254. speechTextID = TextIdentifier(getBaseTextID(), "speech").get();
  255. LIBRARY->generaltexth->registerString( input.getModScope(), speechTextID, input["speech"]);
  256. }
  257. }
  258. for(auto & element : input["modes"].Vector())
  259. {
  260. if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
  261. marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
  262. }
  263. marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
  264. predefinedOffer = input["offer"];
  265. }
  266. bool MarketInstanceConstructor::hasDescription() const
  267. {
  268. return !descriptionTextID.empty();
  269. }
  270. std::shared_ptr<CGMarket> MarketInstanceConstructor::createObject(IGameInfoCallback * cb) const
  271. {
  272. if(marketModes.size() == 1)
  273. {
  274. switch(*marketModes.begin())
  275. {
  276. case EMarketMode::ARTIFACT_RESOURCE:
  277. case EMarketMode::RESOURCE_ARTIFACT:
  278. return std::make_shared<CGBlackMarket>(cb);
  279. case EMarketMode::RESOURCE_SKILL:
  280. return std::make_shared<CGUniversity>(cb);
  281. }
  282. }
  283. return std::make_shared<CGMarket>(cb);
  284. }
  285. const std::set<EMarketMode> & MarketInstanceConstructor::availableModes() const
  286. {
  287. return marketModes;
  288. }
  289. void MarketInstanceConstructor::randomizeObject(CGMarket * object, IGameRandomizer & gameRandomizer) const
  290. {
  291. JsonRandom randomizer(object->cb, gameRandomizer);
  292. JsonRandom::Variables emptyVariables;
  293. if(auto * university = dynamic_cast<CGUniversity *>(object))
  294. {
  295. for(auto skill : randomizer.loadSecondaries(predefinedOffer, emptyVariables))
  296. university->skills.push_back(skill.first);
  297. }
  298. }
  299. std::string MarketInstanceConstructor::getSpeechTranslated() const
  300. {
  301. assert(marketModes.count(EMarketMode::RESOURCE_SKILL));
  302. return LIBRARY->generaltexth->translate(speechTextID);
  303. }
  304. int MarketInstanceConstructor::getMarketEfficiency() const
  305. {
  306. return marketEfficiency;
  307. }
  308. VCMI_LIB_NAMESPACE_END