CommonConstructors.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "../CGeneralTextHandler.h"
  13. #include "../CHeroHandler.h"
  14. #include "../CTownHandler.h"
  15. #include "../IGameCallback.h"
  16. #include "../json/JsonRandom.h"
  17. #include "../constants/StringConstants.h"
  18. #include "../TerrainHandler.h"
  19. #include "../VCMI_Lib.h"
  20. #include "../mapObjects/CGHeroInstance.h"
  21. #include "../mapObjects/CGMarket.h"
  22. #include "../mapObjects/CGTownInstance.h"
  23. #include "../mapObjects/MiscObjects.h"
  24. #include "../mapObjects/ObjectTemplate.h"
  25. #include "../modding/IdentifierStorage.h"
  26. #include "../mapping/CMapDefines.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. bool CObstacleConstructor::isStaticObject()
  29. {
  30. return true;
  31. }
  32. void CObstacleConstructor::initTypeData(const JsonNode & input)
  33. {
  34. if (!input["biome"].isNull())
  35. {
  36. obstacleType = ObstacleSet::typeFromString(input["biome"]["objectType"].String());
  37. }
  38. else
  39. {
  40. obstacleType = ObstacleSet::EObstacleType::INVALID;
  41. }
  42. }
  43. void CObstacleConstructor::afterLoadFinalization()
  44. {
  45. if (obstacleType == ObstacleSet::EObstacleType::INVALID)
  46. return;
  47. auto templates = getTemplates();
  48. logGlobal->info("Loaded %d templates for %s", templates.size(), getJsonKey());
  49. if (!templates.empty())
  50. {
  51. auto terrains = templates.front()->getAllowedTerrains();
  52. // FIXME: 0 terrains
  53. logGlobal->info("Found %d terrains for %s", terrains.size(), getJsonKey());
  54. // For now assume that all templates are from the same biome
  55. for (auto terrain : terrains)
  56. {
  57. std::shared_ptr<ObstacleSet> os = std::make_shared<ObstacleSet>(obstacleType, terrain);
  58. for (auto tmpl : templates)
  59. {
  60. os->addObstacle(tmpl);
  61. }
  62. VLC->biomeHandler->addObstacleSet(os);
  63. logGlobal->info("Loaded obstacle set from mod %s, terrain: %s", getJsonKey(), terrain.encode(terrain.getNum()));
  64. }
  65. }
  66. }
  67. bool CreatureInstanceConstructor::hasNameTextID() const
  68. {
  69. return true;
  70. }
  71. std::string CreatureInstanceConstructor::getNameTextID() const
  72. {
  73. return VLC->creatures()->getByIndex(getSubIndex())->getNamePluralTextID();
  74. }
  75. bool ResourceInstanceConstructor::hasNameTextID() const
  76. {
  77. return true;
  78. }
  79. std::string ResourceInstanceConstructor::getNameTextID() const
  80. {
  81. return TextIdentifier("core", "restypes", getSubIndex()).get();
  82. }
  83. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  84. {
  85. VLC->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
  86. {
  87. faction = (*VLC->townh)[index];
  88. });
  89. filtersJson = input["filters"];
  90. // change scope of "filters" to scope of object that is being loaded
  91. // since this filters require to resolve building ID's
  92. filtersJson.setModScope(input["faction"].getModScope());
  93. }
  94. void CTownInstanceConstructor::afterLoadFinalization()
  95. {
  96. assert(faction);
  97. for(const auto & entry : filtersJson.Struct())
  98. {
  99. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  100. {
  101. return BuildingID(VLC->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value_or(-1));
  102. });
  103. }
  104. }
  105. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  106. {
  107. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  108. auto buildTest = [&](const BuildingID & id)
  109. {
  110. return town->hasBuilt(id);
  111. };
  112. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  113. }
  114. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  115. {
  116. obj->town = faction->town;
  117. obj->tempOwner = PlayerColor::NEUTRAL;
  118. }
  119. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const
  120. {
  121. auto templ = getOverride(object->cb->getTile(object->pos)->terType->getId(), object);
  122. if(templ)
  123. object->appearance = templ;
  124. }
  125. bool CTownInstanceConstructor::hasNameTextID() const
  126. {
  127. return true;
  128. }
  129. std::string CTownInstanceConstructor::getNameTextID() const
  130. {
  131. return faction->getNameTextID();
  132. }
  133. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  134. {
  135. VLC->identifiers()->requestIdentifier(
  136. "heroClass",
  137. input["heroClass"],
  138. [&](si32 index) { heroClass = HeroClassID(index).toHeroClass(); });
  139. filtersJson = input["filters"];
  140. }
  141. void CHeroInstanceConstructor::afterLoadFinalization()
  142. {
  143. for(const auto & entry : filtersJson.Struct())
  144. {
  145. filters[entry.first] = LogicalExpression<HeroTypeID>(entry.second, [](const JsonNode & node)
  146. {
  147. return HeroTypeID(VLC->identifiers()->getIdentifier("hero", node.Vector()[0]).value_or(-1));
  148. });
  149. }
  150. }
  151. bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  152. {
  153. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  154. auto heroTest = [&](const HeroTypeID & id)
  155. {
  156. return hero->type->getId() == id;
  157. };
  158. if(filters.count(templ->stringID))
  159. {
  160. return filters.at(templ->stringID).test(heroTest);
  161. }
  162. return false;
  163. }
  164. void CHeroInstanceConstructor::initializeObject(CGHeroInstance * obj) const
  165. {
  166. obj->type = nullptr; //FIXME: set to valid value. somehow.
  167. }
  168. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomGenerator & rng) const
  169. {
  170. }
  171. bool CHeroInstanceConstructor::hasNameTextID() const
  172. {
  173. return true;
  174. }
  175. std::string CHeroInstanceConstructor::getNameTextID() const
  176. {
  177. return heroClass->getNameTextID();
  178. }
  179. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  180. {
  181. layer = EPathfindingLayer::SAIL;
  182. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  183. if(pos != -1)
  184. layer = EPathfindingLayer(pos);
  185. else
  186. logMod->error("Unknown layer %s found in boat!", input["layer"].String());
  187. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  188. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  189. actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
  190. overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
  191. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  192. flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
  193. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  194. }
  195. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  196. {
  197. boat->layer = layer;
  198. boat->actualAnimation = actualAnimation;
  199. boat->overlayAnimation = overlayAnimation;
  200. boat->flagAnimations = flagAnimations;
  201. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  202. boat->onboardVisitAllowed = onboardVisitAllowed;
  203. for(auto & b : bonuses)
  204. boat->addNewBonus(std::make_shared<Bonus>(b));
  205. }
  206. AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
  207. {
  208. return actualAnimation;
  209. }
  210. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  211. {
  212. for(auto & element : input["modes"].Vector())
  213. {
  214. if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
  215. marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
  216. }
  217. marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
  218. predefinedOffer = input["offer"];
  219. title = input["title"].String();
  220. speech = input["speech"].String();
  221. }
  222. CGMarket * MarketInstanceConstructor::createObject(IGameCallback * cb) const
  223. {
  224. if(marketModes.size() == 1)
  225. {
  226. switch(*marketModes.begin())
  227. {
  228. case EMarketMode::ARTIFACT_RESOURCE:
  229. case EMarketMode::RESOURCE_ARTIFACT:
  230. return new CGBlackMarket(cb);
  231. case EMarketMode::RESOURCE_SKILL:
  232. return new CGUniversity(cb);
  233. }
  234. }
  235. else if(marketModes.size() == 2)
  236. {
  237. if(vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP))
  238. return new CGArtifactsAltar(cb);
  239. }
  240. return new CGMarket(cb);
  241. }
  242. void MarketInstanceConstructor::initializeObject(CGMarket * market) const
  243. {
  244. market->marketModes = marketModes;
  245. market->marketEfficiency = marketEfficiency;
  246. market->title = market->getObjectName();
  247. if(!title.empty())
  248. market->title = VLC->generaltexth->translate(title);
  249. if (!speech.empty())
  250. market->speech = VLC->generaltexth->translate(speech);
  251. }
  252. void MarketInstanceConstructor::randomizeObject(CGMarket * object, CRandomGenerator & rng) const
  253. {
  254. JsonRandom randomizer(object->cb);
  255. JsonRandom::Variables emptyVariables;
  256. if(auto * university = dynamic_cast<CGUniversity *>(object))
  257. {
  258. for(auto skill : randomizer.loadSecondaries(predefinedOffer, rng, emptyVariables))
  259. university->skills.push_back(skill.first);
  260. }
  261. }
  262. VCMI_LIB_NAMESPACE_END