CommonConstructors.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #include "StdInc.h"
  2. #include "CommonConstructors.h"
  3. #include "CGTownInstance.h"
  4. #include "CGHeroInstance.h"
  5. #include "CBank.h"
  6. #include "../mapping/CMap.h"
  7. #include "../CHeroHandler.h"
  8. #include "../CCreatureHandler.h"
  9. #include "JsonRandom.h"
  10. #include "../CModHandler.h"
  11. #include "../IGameCallback.h"
  12. /*
  13. * CommonConstructors.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. CObstacleConstructor::CObstacleConstructor()
  22. {
  23. }
  24. bool CObstacleConstructor::isStaticObject()
  25. {
  26. return true;
  27. }
  28. CTownInstanceConstructor::CTownInstanceConstructor():
  29. faction(nullptr)
  30. {
  31. }
  32. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  33. {
  34. VLC->modh->identifiers.requestIdentifier("faction", input["faction"], [&](si32 index)
  35. {
  36. faction = VLC->townh->factions[index];
  37. });
  38. filtersJson = input["filters"];
  39. }
  40. void CTownInstanceConstructor::afterLoadFinalization()
  41. {
  42. assert(faction);
  43. for (auto entry : filtersJson.Struct())
  44. {
  45. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [this](const JsonNode & node)
  46. {
  47. return BuildingID(VLC->modh->identifiers.getIdentifier("building." + faction->identifier, node.Vector()[0]).get());
  48. });
  49. }
  50. }
  51. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, const ObjectTemplate & templ) const
  52. {
  53. auto town = dynamic_cast<const CGTownInstance *>(object);
  54. auto buildTest = [&](const BuildingID & id)
  55. {
  56. return town->hasBuilt(id);
  57. };
  58. if (filters.count(templ.stringID))
  59. return filters.at(templ.stringID).test(buildTest);
  60. return false;
  61. }
  62. CGObjectInstance * CTownInstanceConstructor::create(ObjectTemplate tmpl) const
  63. {
  64. CGTownInstance * obj = createTyped(tmpl);
  65. obj->town = faction->town;
  66. obj->tempOwner = PlayerColor::NEUTRAL;
  67. return obj;
  68. }
  69. void CTownInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  70. {
  71. auto templ = getOverride(object->cb->getTile(object->pos)->terType, object);
  72. if (templ)
  73. object->appearance = templ.get();
  74. }
  75. CHeroInstanceConstructor::CHeroInstanceConstructor()
  76. {
  77. }
  78. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  79. {
  80. VLC->modh->identifiers.requestIdentifier("heroClass", input["heroClass"],
  81. [&](si32 index) { heroClass = VLC->heroh->classes.heroClasses[index]; });
  82. filtersJson = input["filters"];
  83. }
  84. void CHeroInstanceConstructor::afterLoadFinalization()
  85. {
  86. for (auto entry : filtersJson.Struct())
  87. {
  88. filters[entry.first] = LogicalExpression<HeroTypeID>(entry.second, [this](const JsonNode & node)
  89. {
  90. return HeroTypeID(VLC->modh->identifiers.getIdentifier("hero", node.Vector()[0]).get());
  91. });
  92. }
  93. }
  94. bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, const ObjectTemplate & templ) const
  95. {
  96. auto hero = dynamic_cast<const CGHeroInstance *>(object);
  97. auto heroTest = [&](const HeroTypeID & id)
  98. {
  99. return hero->type->ID == id;
  100. };
  101. if (filters.count(templ.stringID))
  102. {
  103. return filters.at(templ.stringID).test(heroTest);
  104. }
  105. return false;
  106. }
  107. CGObjectInstance * CHeroInstanceConstructor::create(ObjectTemplate tmpl) const
  108. {
  109. CGHeroInstance * obj = createTyped(tmpl);
  110. obj->type = nullptr; //FIXME: set to valid value. somehow.
  111. return obj;
  112. }
  113. void CHeroInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  114. {
  115. }
  116. CDwellingInstanceConstructor::CDwellingInstanceConstructor()
  117. {
  118. }
  119. void CDwellingInstanceConstructor::initTypeData(const JsonNode & input)
  120. {
  121. const JsonVector & levels = input["creatures"].Vector();
  122. availableCreatures.resize(levels.size());
  123. for (size_t i=0; i<levels.size(); i++)
  124. {
  125. const JsonVector & creatures = levels[i].Vector();
  126. availableCreatures[i].resize(creatures.size());
  127. for (size_t j=0; j<creatures.size(); j++)
  128. {
  129. VLC->modh->identifiers.requestIdentifier("creature", creatures[j], [=] (si32 index)
  130. {
  131. availableCreatures[i][j] = VLC->creh->creatures[index];
  132. });
  133. }
  134. assert(!availableCreatures[i].empty());
  135. }
  136. guards = input["guards"];
  137. }
  138. bool CDwellingInstanceConstructor::objectFilter(const CGObjectInstance *, const ObjectTemplate &) const
  139. {
  140. return false;
  141. }
  142. CGObjectInstance * CDwellingInstanceConstructor::create(ObjectTemplate tmpl) const
  143. {
  144. CGDwelling * obj = createTyped(tmpl);
  145. obj->creatures.resize(availableCreatures.size());
  146. for (auto & entry : availableCreatures)
  147. {
  148. for (const CCreature * cre : entry)
  149. obj->creatures.back().second.push_back(cre->idNumber);
  150. }
  151. return obj;
  152. }
  153. void CDwellingInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator &rng) const
  154. {
  155. CGDwelling * dwelling = dynamic_cast<CGDwelling*>(object);
  156. dwelling->creatures.clear();
  157. dwelling->creatures.reserve(availableCreatures.size());
  158. for (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->idNumber);
  163. }
  164. if (guards.getType() == JsonNode::DATA_BOOL)
  165. {
  166. if (guards.Bool())
  167. {
  168. const CCreature * crea = availableCreatures.at(0).at(0);
  169. dwelling->putStack(SlotID(0), new CStackInstance(crea->idNumber, crea->growth * 3 ));
  170. }
  171. }
  172. else for (auto & stack : JsonRandom::loadCreatures(guards, rng))
  173. {
  174. dwelling->putStack(SlotID(dwelling->stacksCount()), new CStackInstance(stack.type->idNumber, stack.count));
  175. }
  176. }
  177. bool CDwellingInstanceConstructor::producesCreature(const CCreature * crea) const
  178. {
  179. for (auto & entry : availableCreatures)
  180. {
  181. for (const CCreature * cre : entry)
  182. if (crea == cre)
  183. return true;
  184. }
  185. return false;
  186. }
  187. std::vector<const CCreature *> CDwellingInstanceConstructor::getProducedCreatures() const
  188. {
  189. std::vector<const CCreature *> creatures; //no idea why it's 2D, to be honest
  190. for (auto & entry : availableCreatures)
  191. {
  192. for (const CCreature * cre : entry)
  193. creatures.push_back(cre);
  194. }
  195. return creatures;
  196. }
  197. CBankInstanceConstructor::CBankInstanceConstructor()
  198. {
  199. }
  200. void CBankInstanceConstructor::initTypeData(const JsonNode & input)
  201. {
  202. //TODO: name = input["name"].String();
  203. levels = input["levels"].Vector();
  204. bankResetDuration = input["resetDuration"].Float();
  205. }
  206. CGObjectInstance *CBankInstanceConstructor::create(ObjectTemplate tmpl) const
  207. {
  208. return createTyped(tmpl);
  209. }
  210. BankConfig CBankInstanceConstructor::generateConfig(const JsonNode & level, CRandomGenerator & rng) const
  211. {
  212. BankConfig bc;
  213. bc.chance = level["chance"].Float();
  214. bc.guards = JsonRandom::loadCreatures(level["guards"], rng);
  215. bc.upgradeChance = level["upgrade_chance"].Float();
  216. bc.combatValue = level["combat_value"].Float();
  217. std::vector<SpellID> spells;
  218. for (size_t i=0; i<6; i++)
  219. IObjectInterface::cb->getAllowedSpells(spells, i);
  220. bc.resources = Res::ResourceSet(level["reward"]["resources"]);
  221. bc.creatures = JsonRandom::loadCreatures(level["reward"]["creatures"], rng);
  222. bc.artifacts = JsonRandom::loadArtifacts(level["reward"]["artifacts"], rng);
  223. bc.spells = JsonRandom::loadSpells(level["reward"]["spells"], rng, spells);
  224. bc.value = level["value"].Float();
  225. return bc;
  226. }
  227. void CBankInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  228. {
  229. //logGlobal->debugStream() << "Seed used to configure bank is " << rng.nextInt();
  230. auto bank = dynamic_cast<CBank*>(object);
  231. bank->resetDuration = bankResetDuration;
  232. si32 totalChance = 0;
  233. for (auto & node : levels)
  234. totalChance += node["chance"].Float();
  235. assert(totalChance != 0);
  236. si32 selectedChance = rng.nextInt(totalChance - 1);
  237. //logGlobal->debugStream() << "Selected chance for bank config is " << selectedChance;
  238. for (auto & node : levels)
  239. {
  240. if (selectedChance < node["chance"].Float())
  241. {
  242. bank->setConfig(generateConfig(node, rng));
  243. }
  244. else
  245. {
  246. selectedChance -= node["chance"].Float();
  247. }
  248. }
  249. }
  250. CBankInfo::CBankInfo(JsonVector config):
  251. config(config)
  252. {
  253. assert(!config.empty());
  254. }
  255. static void addStackToArmy(IObjectInfo::CArmyStructure & army, const CCreature * crea, si32 amount)
  256. {
  257. army.totalStrength += crea->fightValue * amount;
  258. bool walker = true;
  259. if (crea->hasBonusOfType(Bonus::SHOOTER))
  260. {
  261. army.shootersStrength += crea->fightValue * amount;
  262. walker = false;
  263. }
  264. if (crea->hasBonusOfType(Bonus::FLYING))
  265. {
  266. army.flyersStrength += crea->fightValue * amount;
  267. walker = false;
  268. }
  269. if (walker)
  270. army.walkersStrength += crea->fightValue * amount;
  271. }
  272. IObjectInfo::CArmyStructure CBankInfo::minGuards() const
  273. {
  274. std::vector<IObjectInfo::CArmyStructure> armies;
  275. for (auto configEntry : config)
  276. {
  277. auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]);
  278. IObjectInfo::CArmyStructure army;
  279. for (auto & stack : stacks)
  280. {
  281. assert(!stack.allowedCreatures.empty());
  282. auto weakest = boost::range::min_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b)
  283. {
  284. return a->fightValue < b->fightValue;
  285. });
  286. addStackToArmy(army, *weakest, stack.minAmount);
  287. }
  288. armies.push_back(army);
  289. }
  290. return *boost::range::min_element(armies);
  291. }
  292. IObjectInfo::CArmyStructure CBankInfo::maxGuards() const
  293. {
  294. std::vector<IObjectInfo::CArmyStructure> armies;
  295. for (auto configEntry : config)
  296. {
  297. auto stacks = JsonRandom::evaluateCreatures(configEntry["guards"]);
  298. IObjectInfo::CArmyStructure army;
  299. for (auto & stack : stacks)
  300. {
  301. assert(!stack.allowedCreatures.empty());
  302. auto strongest = boost::range::max_element(stack.allowedCreatures, [](const CCreature * a, const CCreature * b)
  303. {
  304. return a->fightValue < b->fightValue;
  305. });
  306. addStackToArmy(army, *strongest, stack.maxAmount);
  307. }
  308. armies.push_back(army);
  309. }
  310. return *boost::range::max_element(armies);
  311. }
  312. bool CBankInfo::givesResources() const
  313. {
  314. for (const JsonNode & node : config)
  315. if (!node["reward"]["resources"].isNull())
  316. return true;
  317. return false;
  318. }
  319. bool CBankInfo::givesArtifacts() const
  320. {
  321. for (const JsonNode & node : config)
  322. if (!node["reward"]["artifacts"].isNull())
  323. return true;
  324. return false;
  325. }
  326. bool CBankInfo::givesCreatures() const
  327. {
  328. for (const JsonNode & node : config)
  329. if (!node["reward"]["creatures"].isNull())
  330. return true;
  331. return false;
  332. }
  333. bool CBankInfo::givesSpells() const
  334. {
  335. for (const JsonNode & node : config)
  336. if (!node["reward"]["spells"].isNull())
  337. return true;
  338. return false;
  339. }
  340. std::unique_ptr<IObjectInfo> CBankInstanceConstructor::getObjectInfo(ObjectTemplate tmpl) const
  341. {
  342. return std::unique_ptr<IObjectInfo>(new CBankInfo(levels));
  343. }