CommonConstructors.cpp 8.8 KB

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