CModHandler.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "StdInc.h"
  2. #include "CModHandler.h"
  3. #include "JsonNode.h"
  4. /*
  5. * CModHandler.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. class CArtHandler;
  14. class CHeroHandler;
  15. class CCreatureHandler;
  16. class CSpellHandler;
  17. class CBuildingHandler;
  18. class CObjectHandler;
  19. class CDefObjInfoHandler;
  20. class CTownHandler;
  21. class CGeneralTextHandler;
  22. CModHandler::CModHandler()
  23. {
  24. VLC->modh = this;
  25. loadConfigFromFile ("defaultMods");
  26. //CResourceHandler::loadModsFilesystems(); //scan for all mods
  27. //TODO: mod filesystem is already initialized at LibClasses launch
  28. //TODO: load default (last?) config
  29. }
  30. artID CModHandler::addNewArtifact (CArtifact * art)
  31. {
  32. int id = artifacts.size();
  33. artifacts.push_back (art);
  34. return id;
  35. }
  36. creID CModHandler::addNewCreature (CCreature * cre)
  37. {
  38. int id = creatures.size();
  39. creatures.push_back (cre);
  40. return id;
  41. }
  42. void CModHandler::loadConfigFromFile (std::string name)
  43. {
  44. const JsonNode config(ResourceID("config/" + name + ".json"));
  45. auto hardcodedFeatures = config["hardcodedFeatures"];
  46. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
  47. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
  48. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
  49. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
  50. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  51. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  52. auto gameModules = config["modules"];
  53. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  54. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  55. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  56. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  57. }
  58. void CModHandler::saveConfigToFile (std::string name)
  59. {
  60. //JsonNode savedConf = config;
  61. //JsonNode schema(ResourceID("config/defaultSettings.json"));
  62. //savedConf.Struct().erase("session");
  63. //savedConf.minimize(schema);
  64. CResourceHandler::get()->createResource("config/" + name +".json");
  65. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
  66. //file << savedConf;
  67. }
  68. CCreature * CModHandler::loadCreature (const JsonNode &node)
  69. {
  70. CCreature * cre = new CCreature();
  71. cre->idNumber = creatures.size();
  72. const JsonNode *value; //optional value
  73. //TODO: ref name
  74. auto name = node["name"];
  75. cre->nameSing = name["singular"].String();
  76. cre->namePl = name["plural"].String();
  77. //TODO: map name->id
  78. auto cost = node["cost"];
  79. if (cost.getType() == JsonNode::DATA_FLOAT) //gold
  80. {
  81. cre->cost[Res::GOLD] = cost.Float();
  82. }
  83. else
  84. {
  85. int i = 0;
  86. BOOST_FOREACH (auto val, cost.Vector())
  87. {
  88. cre->cost[i++] = val.Float();
  89. }
  90. }
  91. cre->level = node["level"].Float();
  92. cre->faction = -1; //TODO: node["faction"].String() to id or just node["faction"].Float();
  93. cre->fightValue = node["fightValue"].Float();
  94. cre->AIValue = node["aiValue"].Float();
  95. cre->growth = node["growth"].Float();
  96. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  97. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  98. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  99. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  100. auto vec = node["damage"].Vector();
  101. cre->addBonus(vec[0].Float(), Bonus::CREATURE_DAMAGE, 1);
  102. cre->addBonus(vec[1].Float(), Bonus::CREATURE_DAMAGE, 2);
  103. //optional
  104. value = &node["shots"];
  105. if (!value->isNull())
  106. cre->addBonus(value->Float(), Bonus::SHOTS);
  107. value = &node["spellPoints"];
  108. if (!value->isNull())
  109. cre->addBonus(value->Float(), Bonus::CASTS);
  110. cre->doubleWide = value->Bool();
  111. value = &node["abilities"];
  112. if (!value->isNull())
  113. {
  114. BOOST_FOREACH (const JsonNode &bonus, value->Vector())
  115. {
  116. cre->addNewBonus(ParseBonus(bonus));
  117. }
  118. }
  119. //graphics
  120. auto graphics = node["graphics"];
  121. cre->animDefName = graphics["animation"].String();
  122. cre->timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  123. cre->troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  124. cre->attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  125. auto animationTime = graphics["animationTime"];
  126. cre->walkAnimationTime = animationTime["walk"].Float();
  127. cre->attackAnimationTime = animationTime["attack"].Float();
  128. cre->flightAnimationDistance = animationTime["flight"].Float(); //?
  129. //TODO: background?
  130. auto missle = graphics["missle"];
  131. auto offsets = missle["offset"];
  132. cre->upperRightMissleOffsetX = offsets["upperX"].Float();
  133. cre->upperRightMissleOffsetY = offsets["upperY"].Float();
  134. cre->rightMissleOffsetX = offsets["middleX"].Float();
  135. cre->rightMissleOffsetY = offsets["middleY"].Float();
  136. cre->lowerRightMissleOffsetX = offsets["lowerX"].Float();
  137. cre->lowerRightMissleOffsetY = offsets["lowerY"].Float();
  138. int i = 0;
  139. BOOST_FOREACH (auto angle, missle["frameAngles"].Vector())
  140. {
  141. cre->missleFrameAngles[i++] = angle.Float();
  142. }
  143. //we need to know creature id to add it
  144. VLC->creh->idToProjectile[cre->idNumber] = value->String();
  145. auto sounds = node["sound"];
  146. #define GET_SOUND_VALUE(value_name) do { value = &node[#value_name]; if (!value->isNull()) cre->sounds.value_name = sounds[#value_name].String(); } while(0)
  147. GET_SOUND_VALUE(attack);
  148. GET_SOUND_VALUE(defend);
  149. GET_SOUND_VALUE(killed);
  150. GET_SOUND_VALUE(move);
  151. GET_SOUND_VALUE(shoot);
  152. GET_SOUND_VALUE(wince);
  153. GET_SOUND_VALUE(ext1);
  154. GET_SOUND_VALUE(ext2);
  155. GET_SOUND_VALUE(startMoving);
  156. GET_SOUND_VALUE(endMoving);
  157. #undef GET_SOUND_VALUE
  158. creatures.push_back(cre);
  159. return cre;
  160. }
  161. void CModHandler::recreateHandlers()
  162. {
  163. //TODO: consider some template magic to unify all handlers?
  164. VLC->arth->artifacts.clear();
  165. VLC->creh->creatures.clear(); //TODO: what about items from original game?
  166. BOOST_FOREACH (auto mod, activeMods)
  167. {
  168. BOOST_FOREACH (auto art, allMods[mod].artifacts)
  169. {
  170. VLC->arth->artifacts.push_back (artifacts[art]);
  171. //TODO: recreate types / limiters based on string id
  172. }
  173. BOOST_FOREACH (auto creature, allMods[mod].creatures)
  174. {
  175. VLC->creh->creatures.push_back (creatures[creature]);
  176. //TODO: recreate upgrades and other properties based on string id
  177. }
  178. }
  179. }
  180. CModHandler::~CModHandler()
  181. {
  182. }