CommonConstructors.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "../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. bool CreatureInstanceConstructor::hasNameTextID() const
  33. {
  34. return true;
  35. }
  36. std::string CreatureInstanceConstructor::getNameTextID() const
  37. {
  38. return VLC->creatures()->getByIndex(getSubIndex())->getNamePluralTextID();
  39. }
  40. bool ResourceInstanceConstructor::hasNameTextID() const
  41. {
  42. return true;
  43. }
  44. std::string ResourceInstanceConstructor::getNameTextID() const
  45. {
  46. return TextIdentifier("core", "restypes", getSubIndex()).get();
  47. }
  48. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  49. {
  50. VLC->identifiers()->requestIdentifier("faction", input["faction"], [&](si32 index)
  51. {
  52. faction = (*VLC->townh)[index];
  53. });
  54. filtersJson = input["filters"];
  55. // change scope of "filters" to scope of object that is being loaded
  56. // since this filters require to resolve building ID's
  57. filtersJson.setMeta(input["faction"].meta);
  58. }
  59. void CTownInstanceConstructor::afterLoadFinalization()
  60. {
  61. assert(faction);
  62. for(const auto & entry : filtersJson.Struct())
  63. {
  64. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  65. {
  66. return BuildingID(VLC->identifiers()->getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value());
  67. });
  68. }
  69. }
  70. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  71. {
  72. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  73. auto buildTest = [&](const BuildingID & id)
  74. {
  75. return town->hasBuilt(id);
  76. };
  77. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  78. }
  79. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  80. {
  81. obj->town = faction->town;
  82. obj->tempOwner = PlayerColor::NEUTRAL;
  83. }
  84. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const
  85. {
  86. auto templ = getOverride(CGObjectInstance::cb->getTile(object->pos)->terType->getId(), object);
  87. if(templ)
  88. object->appearance = templ;
  89. }
  90. bool CTownInstanceConstructor::hasNameTextID() const
  91. {
  92. return true;
  93. }
  94. std::string CTownInstanceConstructor::getNameTextID() const
  95. {
  96. return faction->getNameTextID();
  97. }
  98. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  99. {
  100. VLC->identifiers()->requestIdentifier(
  101. "heroClass",
  102. input["heroClass"],
  103. [&](si32 index) { heroClass = VLC->heroh->classes[index]; });
  104. filtersJson = input["filters"];
  105. }
  106. void CHeroInstanceConstructor::afterLoadFinalization()
  107. {
  108. for(const auto & entry : filtersJson.Struct())
  109. {
  110. filters[entry.first] = LogicalExpression<HeroTypeID>(entry.second, [](const JsonNode & node)
  111. {
  112. return HeroTypeID(VLC->identifiers()->getIdentifier("hero", node.Vector()[0]).value());
  113. });
  114. }
  115. }
  116. bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  117. {
  118. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  119. auto heroTest = [&](const HeroTypeID & id)
  120. {
  121. return hero->type->getId() == id;
  122. };
  123. if(filters.count(templ->stringID))
  124. {
  125. return filters.at(templ->stringID).test(heroTest);
  126. }
  127. return false;
  128. }
  129. void CHeroInstanceConstructor::initializeObject(CGHeroInstance * obj) const
  130. {
  131. obj->type = nullptr; //FIXME: set to valid value. somehow.
  132. }
  133. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomGenerator & rng) const
  134. {
  135. }
  136. bool CHeroInstanceConstructor::hasNameTextID() const
  137. {
  138. return true;
  139. }
  140. std::string CHeroInstanceConstructor::getNameTextID() const
  141. {
  142. return heroClass->getNameTextID();
  143. }
  144. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  145. {
  146. layer = EPathfindingLayer::SAIL;
  147. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  148. if(pos != -1)
  149. layer = EPathfindingLayer(pos);
  150. else
  151. logMod->error("Unknown layer %s found in boat!", input["layer"].String());
  152. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  153. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  154. actualAnimation = AnimationPath::fromJson(input["actualAnimation"]);
  155. overlayAnimation = AnimationPath::fromJson(input["overlayAnimation"]);
  156. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  157. flagAnimations[i] = AnimationPath::fromJson(input["flagAnimations"].Vector()[i]);
  158. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  159. }
  160. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  161. {
  162. boat->layer = layer;
  163. boat->actualAnimation = actualAnimation;
  164. boat->overlayAnimation = overlayAnimation;
  165. boat->flagAnimations = flagAnimations;
  166. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  167. boat->onboardVisitAllowed = onboardVisitAllowed;
  168. for(auto & b : bonuses)
  169. boat->addNewBonus(std::make_shared<Bonus>(b));
  170. }
  171. AnimationPath BoatInstanceConstructor::getBoatAnimationName() const
  172. {
  173. return actualAnimation;
  174. }
  175. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  176. {
  177. for(auto & element : input["modes"].Vector())
  178. {
  179. if(MappedKeys::MARKET_NAMES_TO_TYPES.count(element.String()))
  180. marketModes.insert(MappedKeys::MARKET_NAMES_TO_TYPES.at(element.String()));
  181. }
  182. marketEfficiency = input["efficiency"].isNull() ? 5 : input["efficiency"].Integer();
  183. predefinedOffer = input["offer"];
  184. title = input["title"].String();
  185. speech = input["speech"].String();
  186. }
  187. CGMarket * MarketInstanceConstructor::createObject() const
  188. {
  189. if(marketModes.size() == 1)
  190. {
  191. switch(*marketModes.begin())
  192. {
  193. case EMarketMode::ARTIFACT_RESOURCE:
  194. case EMarketMode::RESOURCE_ARTIFACT:
  195. return new CGBlackMarket;
  196. case EMarketMode::RESOURCE_SKILL:
  197. return new CGUniversity;
  198. }
  199. }
  200. return new CGMarket;
  201. }
  202. void MarketInstanceConstructor::initializeObject(CGMarket * market) const
  203. {
  204. market->marketModes = marketModes;
  205. market->marketEfficiency = marketEfficiency;
  206. market->title = market->getObjectName();
  207. if(!title.empty())
  208. market->title = VLC->generaltexth->translate(title);
  209. if (!speech.empty())
  210. market->speech = VLC->generaltexth->translate(speech);
  211. }
  212. void MarketInstanceConstructor::randomizeObject(CGMarket * object, CRandomGenerator & rng) const
  213. {
  214. JsonRandom::Variables emptyVariables;
  215. if(auto * university = dynamic_cast<CGUniversity *>(object))
  216. {
  217. for(auto skill : JsonRandom::loadSecondaries(predefinedOffer, rng, emptyVariables))
  218. university->skills.push_back(skill.first);
  219. }
  220. }
  221. VCMI_LIB_NAMESPACE_END