CommonConstructors.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "../CCreatureHandler.h"
  13. #include "../CGeneralTextHandler.h"
  14. #include "../CHeroHandler.h"
  15. #include "../CModHandler.h"
  16. #include "../IGameCallback.h"
  17. #include "../JsonRandom.h"
  18. #include "../StringConstants.h"
  19. #include "../TerrainHandler.h"
  20. #include "../mapObjects/CBank.h"
  21. #include "../mapObjects/CGHeroInstance.h"
  22. #include "../mapObjects/CGMarket.h"
  23. #include "../mapObjects/CGTownInstance.h"
  24. #include "../mapObjects/MiscObjects.h"
  25. #include "../mapObjects/ObjectTemplate.h"
  26. #include "../mapping/CMapDefines.h"
  27. VCMI_LIB_NAMESPACE_BEGIN
  28. bool CObstacleConstructor::isStaticObject()
  29. {
  30. return true;
  31. }
  32. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  33. {
  34. VLC->modh->identifiers.requestIdentifier("faction", input["faction"], [&](si32 index)
  35. {
  36. faction = (*VLC->townh)[index];
  37. });
  38. filtersJson = input["filters"];
  39. // change scope of "filters" to scope of object that is being loaded
  40. // since this filters require to resolve building ID's
  41. filtersJson.setMeta(input["faction"].meta);
  42. }
  43. void CTownInstanceConstructor::afterLoadFinalization()
  44. {
  45. assert(faction);
  46. for(const auto & entry : filtersJson.Struct())
  47. {
  48. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  49. {
  50. return BuildingID(VLC->modh->identifiers.getIdentifier("building." + faction->getJsonKey(), node.Vector()[0]).value());
  51. });
  52. }
  53. }
  54. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  55. {
  56. const auto * town = dynamic_cast<const CGTownInstance *>(object);
  57. auto buildTest = [&](const BuildingID & id)
  58. {
  59. return town->hasBuilt(id);
  60. };
  61. return filters.count(templ->stringID) != 0 && filters.at(templ->stringID).test(buildTest);
  62. }
  63. void CTownInstanceConstructor::initializeObject(CGTownInstance * obj) const
  64. {
  65. obj->town = faction->town;
  66. obj->tempOwner = PlayerColor::NEUTRAL;
  67. }
  68. void CTownInstanceConstructor::randomizeObject(CGTownInstance * object, CRandomGenerator & rng) const
  69. {
  70. auto templ = getOverride(CGObjectInstance::cb->getTile(object->pos)->terType->getId(), object);
  71. if(templ)
  72. object->appearance = templ;
  73. }
  74. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  75. {
  76. VLC->modh->identifiers.requestIdentifier(
  77. "heroClass",
  78. input["heroClass"],
  79. [&](si32 index) { heroClass = VLC->heroh->classes[index]; });
  80. filtersJson = input["filters"];
  81. }
  82. void CHeroInstanceConstructor::afterLoadFinalization()
  83. {
  84. for(const auto & entry : filtersJson.Struct())
  85. {
  86. filters[entry.first] = LogicalExpression<HeroTypeID>(entry.second, [](const JsonNode & node)
  87. {
  88. return HeroTypeID(VLC->modh->identifiers.getIdentifier("hero", node.Vector()[0]).value());
  89. });
  90. }
  91. }
  92. bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, std::shared_ptr<const ObjectTemplate> templ) const
  93. {
  94. const auto * hero = dynamic_cast<const CGHeroInstance *>(object);
  95. auto heroTest = [&](const HeroTypeID & id)
  96. {
  97. return hero->type->getId() == id;
  98. };
  99. if(filters.count(templ->stringID))
  100. {
  101. return filters.at(templ->stringID).test(heroTest);
  102. }
  103. return false;
  104. }
  105. void CHeroInstanceConstructor::initializeObject(CGHeroInstance * obj) const
  106. {
  107. obj->type = nullptr; //FIXME: set to valid value. somehow.
  108. }
  109. void CHeroInstanceConstructor::randomizeObject(CGHeroInstance * object, CRandomGenerator & rng) const
  110. {
  111. }
  112. bool CDwellingInstanceConstructor::hasNameTextID() const
  113. {
  114. return true;
  115. }
  116. void CDwellingInstanceConstructor::initTypeData(const JsonNode & input)
  117. {
  118. if (input.Struct().count("name") == 0)
  119. logMod->warn("Dwelling %s missing name!", getJsonKey());
  120. VLC->generaltexth->registerString( input.meta, getNameTextID(), input["name"].String());
  121. const JsonVector & levels = input["creatures"].Vector();
  122. const auto totalLevels = levels.size();
  123. availableCreatures.resize(totalLevels);
  124. for(auto currentLevel = 0; currentLevel < totalLevels; currentLevel++)
  125. {
  126. const JsonVector & creaturesOnLevel = levels[currentLevel].Vector();
  127. const auto creaturesNumber = creaturesOnLevel.size();
  128. availableCreatures[currentLevel].resize(creaturesNumber);
  129. for(auto currentCreature = 0; currentCreature < creaturesNumber; currentCreature++)
  130. {
  131. VLC->modh->identifiers.requestIdentifier("creature", creaturesOnLevel[currentCreature], [=] (si32 index)
  132. {
  133. availableCreatures[currentLevel][currentCreature] = VLC->creh->objects[index];
  134. });
  135. }
  136. assert(!availableCreatures[currentLevel].empty());
  137. }
  138. guards = input["guards"];
  139. }
  140. bool CDwellingInstanceConstructor::objectFilter(const CGObjectInstance * obj, std::shared_ptr<const ObjectTemplate> tmpl) const
  141. {
  142. return false;
  143. }
  144. void CDwellingInstanceConstructor::initializeObject(CGDwelling * obj) const
  145. {
  146. obj->creatures.resize(availableCreatures.size());
  147. for(const auto & entry : availableCreatures)
  148. {
  149. for(const CCreature * cre : entry)
  150. obj->creatures.back().second.push_back(cre->getId());
  151. }
  152. }
  153. void CDwellingInstanceConstructor::randomizeObject(CGDwelling * object, CRandomGenerator &rng) const
  154. {
  155. auto * dwelling = dynamic_cast<CGDwelling *>(object);
  156. dwelling->creatures.clear();
  157. dwelling->creatures.reserve(availableCreatures.size());
  158. for(const auto & entry : availableCreatures)
  159. {
  160. dwelling->creatures.resize(dwelling->creatures.size() + 1);
  161. for(const CCreature * cre : entry)
  162. dwelling->creatures.back().second.push_back(cre->getId());
  163. }
  164. bool guarded = false; //TODO: serialize for sanity
  165. if(guards.getType() == JsonNode::JsonType::DATA_BOOL) //simple switch
  166. {
  167. if(guards.Bool())
  168. {
  169. guarded = true;
  170. }
  171. }
  172. else if(guards.getType() == JsonNode::JsonType::DATA_VECTOR) //custom guards (eg. Elemental Conflux)
  173. {
  174. for(auto & stack : JsonRandom::loadCreatures(guards, rng))
  175. {
  176. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->getId(), stack.count));
  177. }
  178. }
  179. else //default condition - creatures are of level 5 or higher
  180. {
  181. for(auto creatureEntry : availableCreatures)
  182. {
  183. if(creatureEntry.at(0)->getLevel() >= 5)
  184. {
  185. guarded = true;
  186. break;
  187. }
  188. }
  189. }
  190. if(guarded)
  191. {
  192. for(auto creatureEntry : availableCreatures)
  193. {
  194. const CCreature * crea = creatureEntry.at(0);
  195. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(crea->getId(), crea->getGrowth() * 3));
  196. }
  197. }
  198. }
  199. bool CDwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  200. {
  201. for(const auto & entry : availableCreatures)
  202. {
  203. for(const CCreature * cre : entry)
  204. if(crea == cre)
  205. return true;
  206. }
  207. return false;
  208. }
  209. std::vector<const CCreature *> CDwellingInstanceConstructor::getProducedCreatures() const
  210. {
  211. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  212. for(const auto & entry : availableCreatures)
  213. {
  214. for(const CCreature * cre : entry)
  215. creatures.push_back(cre);
  216. }
  217. return creatures;
  218. }
  219. void BoatInstanceConstructor::initTypeData(const JsonNode & input)
  220. {
  221. layer = EPathfindingLayer::SAIL;
  222. int pos = vstd::find_pos(NPathfindingLayer::names, input["layer"].String());
  223. if(pos != -1)
  224. layer = EPathfindingLayer(pos);
  225. onboardAssaultAllowed = input["onboardAssaultAllowed"].Bool();
  226. onboardVisitAllowed = input["onboardVisitAllowed"].Bool();
  227. actualAnimation = input["actualAnimation"].String();
  228. overlayAnimation = input["overlayAnimation"].String();
  229. for(int i = 0; i < flagAnimations.size() && i < input["flagAnimations"].Vector().size(); ++i)
  230. flagAnimations[i] = input["flagAnimations"].Vector()[i].String();
  231. bonuses = JsonRandom::loadBonuses(input["bonuses"]);
  232. }
  233. void BoatInstanceConstructor::initializeObject(CGBoat * boat) const
  234. {
  235. boat->layer = layer;
  236. boat->actualAnimation = actualAnimation;
  237. boat->overlayAnimation = overlayAnimation;
  238. boat->flagAnimations = flagAnimations;
  239. boat->onboardAssaultAllowed = onboardAssaultAllowed;
  240. boat->onboardVisitAllowed = onboardVisitAllowed;
  241. for(auto & b : bonuses)
  242. boat->addNewBonus(std::make_shared<Bonus>(b));
  243. }
  244. std::string BoatInstanceConstructor::getBoatAnimationName() const
  245. {
  246. return actualAnimation;
  247. }
  248. void BoatInstanceConstructor::afterLoadFinalization()
  249. {
  250. if (layer == EPathfindingLayer::SAIL)
  251. {
  252. if (getTemplates(TerrainId(ETerrainId::WATER)).empty())
  253. logMod->warn("Boat of type %s has no templates suitable for water!", getJsonKey());
  254. }
  255. }
  256. void MarketInstanceConstructor::initTypeData(const JsonNode & input)
  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. title = input["title"].String();
  266. speech = input["speech"].String();
  267. }
  268. CGMarket * MarketInstanceConstructor::createObject() const
  269. {
  270. if(marketModes.size() == 1)
  271. {
  272. switch(*marketModes.begin())
  273. {
  274. case EMarketMode::ARTIFACT_RESOURCE:
  275. case EMarketMode::RESOURCE_ARTIFACT:
  276. return new CGBlackMarket;
  277. case EMarketMode::RESOURCE_SKILL:
  278. return new CGUniversity;
  279. }
  280. }
  281. return new CGMarket;
  282. }
  283. void MarketInstanceConstructor::initializeObject(CGMarket * market) const
  284. {
  285. market->marketModes = marketModes;
  286. market->marketEfficiency = marketEfficiency;
  287. market->title = market->getObjectName();
  288. if(!title.empty())
  289. market->title = VLC->generaltexth->translate(title);
  290. if (!speech.empty())
  291. market->speech = VLC->generaltexth->translate(speech);
  292. }
  293. void MarketInstanceConstructor::randomizeObject(CGMarket * object, CRandomGenerator & rng) const
  294. {
  295. if(auto * university = dynamic_cast<CGUniversity *>(object))
  296. {
  297. for(auto skill : JsonRandom::loadSecondary(predefinedOffer, rng))
  298. university->skills.push_back(skill.first.getNum());
  299. }
  300. }
  301. VCMI_LIB_NAMESPACE_END