CommonConstructors.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "StdInc.h"
  2. #include "CommonConstructors.h"
  3. #include "CGTownInstance.h"
  4. #include "CGHeroInstance.h"
  5. #include "../mapping/CMap.h"
  6. #include "../CHeroHandler.h"
  7. #include "../CCreatureHandler.h"
  8. /*
  9. * CommonConstructors.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. CObstacleConstructor::CObstacleConstructor()
  18. {
  19. }
  20. bool CObstacleConstructor::isStaticObject()
  21. {
  22. return true;
  23. }
  24. CTownInstanceConstructor::CTownInstanceConstructor()
  25. {
  26. }
  27. void CTownInstanceConstructor::initTypeData(const JsonNode & input)
  28. {
  29. VLC->modh->identifiers.requestIdentifier("faction", input["faction"],
  30. [&](si32 index) { faction = VLC->townh->factions[index]; });
  31. filtersJson = input["filters"];
  32. }
  33. void CTownInstanceConstructor::afterLoadFinalization()
  34. {
  35. for (auto entry : filtersJson.Struct())
  36. {
  37. filters[entry.first] = LogicalExpression<BuildingID>(entry.second, [&](const JsonNode & node)
  38. {
  39. return BuildingID(VLC->modh->identifiers.getIdentifier("building." + faction->identifier, node.Vector()[0]).get());
  40. });
  41. }
  42. }
  43. bool CTownInstanceConstructor::objectFilter(const CGObjectInstance * object, const ObjectTemplate & templ) const
  44. {
  45. auto town = dynamic_cast<const CGTownInstance *>(object);
  46. auto buildTest = [&](const BuildingID & id)
  47. {
  48. return town->hasBuilt(id);
  49. };
  50. if (filters.count(templ.stringID))
  51. return filters.at(templ.stringID).test(buildTest);
  52. return false;
  53. }
  54. CGObjectInstance * CTownInstanceConstructor::create(ObjectTemplate tmpl) const
  55. {
  56. CGTownInstance * obj = createTyped(tmpl);
  57. obj->town = faction->town;
  58. obj->tempOwner = PlayerColor::NEUTRAL;
  59. return obj;
  60. }
  61. void CTownInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  62. {
  63. auto templ = getOverride(object->cb->getTile(object->pos)->terType, object);
  64. if (templ)
  65. object->appearance = templ.get();
  66. }
  67. CHeroInstanceConstructor::CHeroInstanceConstructor()
  68. {
  69. }
  70. void CHeroInstanceConstructor::initTypeData(const JsonNode & input)
  71. {
  72. VLC->modh->identifiers.requestIdentifier("heroClass", input["heroClass"],
  73. [&](si32 index) { heroClass = VLC->heroh->classes.heroClasses[index]; });
  74. }
  75. bool CHeroInstanceConstructor::objectFilter(const CGObjectInstance * object, const ObjectTemplate & templ) const
  76. {
  77. return false;
  78. }
  79. CGObjectInstance * CHeroInstanceConstructor::create(ObjectTemplate tmpl) const
  80. {
  81. CGHeroInstance * obj = createTyped(tmpl);
  82. obj->type = nullptr; //FIXME: set to valid value. somehow.
  83. return obj;
  84. }
  85. void CHeroInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  86. {
  87. }
  88. CDwellingInstanceConstructor::CDwellingInstanceConstructor()
  89. {
  90. }
  91. void CDwellingInstanceConstructor::initTypeData(const JsonNode & input)
  92. {
  93. const JsonVector & levels = input["creatures"].Vector();
  94. availableCreatures.resize(levels.size());
  95. for (size_t i=0; i<levels.size(); i++)
  96. {
  97. const JsonVector & creatures = levels[i].Vector();
  98. availableCreatures[i].resize(creatures.size());
  99. for (size_t j=0; j<creatures.size(); j++)
  100. {
  101. VLC->modh->identifiers.requestIdentifier("creature", creatures[j], [&] (si32 index)
  102. {
  103. availableCreatures[i][j] = VLC->creh->creatures[index];
  104. });
  105. }
  106. }
  107. }
  108. bool CDwellingInstanceConstructor::objectFilter(const CGObjectInstance *, const ObjectTemplate &) const
  109. {
  110. return false;
  111. }
  112. CGObjectInstance * CDwellingInstanceConstructor::create(ObjectTemplate tmpl) const
  113. {
  114. CGDwelling * obj = createTyped(tmpl);
  115. for (auto entry : availableCreatures)
  116. {
  117. obj->creatures.resize(obj->creatures.size()+1);
  118. for (const CCreature * cre : entry)
  119. obj->creatures.back().second.push_back(cre->idNumber);
  120. }
  121. return obj;
  122. }
  123. void CDwellingInstanceConstructor::configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
  124. {
  125. }