CommonConstructors.cpp 9.6 KB

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