CommonConstructors.cpp 9.9 KB

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