CModHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  50. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  51. auto gameModules = config["modules"];
  52. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  53. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  54. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  55. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  56. }
  57. void CModHandler::saveConfigToFile (std::string name)
  58. {
  59. //JsonNode savedConf = config;
  60. //JsonNode schema(ResourceID("config/defaultSettings.json"));
  61. //savedConf.Struct().erase("session");
  62. //savedConf.minimize(schema);
  63. CResourceHandler::get()->createResource("config/" + name +".json");
  64. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
  65. //file << savedConf;
  66. }
  67. CCreature * CModHandler::loadCreature (const JsonNode &node)
  68. {
  69. CCreature * cre = new CCreature();
  70. cre->idNumber = creatures.size();
  71. const JsonNode *value; //optional value
  72. //TODO: ref name
  73. auto name = node["name"];
  74. cre->nameSing = name["singular"].String();
  75. cre->namePl = name["plural"].String();
  76. //TODO: map name->id
  77. auto cost = node["cost"];
  78. if (cost.getType() == JsonNode::DATA_FLOAT) //gold
  79. {
  80. cre->cost[Res::GOLD] = cost.Float();
  81. }
  82. else
  83. {
  84. int i = 0;
  85. BOOST_FOREACH (auto val, cost.Vector())
  86. {
  87. cre->cost[i++] = val.Float();
  88. }
  89. }
  90. cre->level = node["level"].Float();
  91. cre->faction = -1; //TODO: node["faction"].String() to id or just node["faction"].Float();
  92. cre->fightValue = node["fightValue"].Float();
  93. cre->AIValue = node["aiValue"].Float();
  94. cre->growth = node["growth"].Float();
  95. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  96. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  97. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  98. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  99. auto vec = node["damage"].Vector();
  100. cre->addBonus(vec[0].Float(), Bonus::CREATURE_DAMAGE, 1);
  101. cre->addBonus(vec[1].Float(), Bonus::CREATURE_DAMAGE, 2);
  102. //optional
  103. value = &node["shots"];
  104. if (!value->isNull())
  105. cre->addBonus(value->Float(), Bonus::SHOTS);
  106. value = &node["spellPoints"];
  107. if (!value->isNull())
  108. cre->addBonus(value->Float(), Bonus::CASTS);
  109. value = &node["doubleWide"];
  110. if (!value->isNull())
  111. cre->doubleWide = value->Bool();
  112. else
  113. cre->doubleWide = false;
  114. value = &node["abilities"];
  115. if (!value->isNull())
  116. {
  117. BOOST_FOREACH (const JsonNode &bonus, value->Vector())
  118. {
  119. cre->addNewBonus(ParseBonus(bonus));
  120. }
  121. }
  122. //graphics
  123. auto graphics = node["graphics"];
  124. cre->animDefName = graphics["animation"].String();
  125. cre->timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  126. cre->troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  127. cre->attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  128. auto animationTime = graphics["animationTime"];
  129. cre->walkAnimationTime = animationTime["walk"].Float();
  130. cre->attackAnimationTime = animationTime["attack"].Float();
  131. cre->flightAnimationDistance = animationTime["flight"].Float(); //?
  132. //TODO: background?
  133. auto missle = graphics["missle"];
  134. auto offsets = missle["offset"];
  135. cre->upperRightMissleOffsetX = offsets["upperX"].Float();
  136. cre->upperRightMissleOffsetY = offsets["upperY"].Float();
  137. cre->rightMissleOffsetX = offsets["middleX"].Float();
  138. cre->rightMissleOffsetY = offsets["middleY"].Float();
  139. cre->lowerRightMissleOffsetX = offsets["lowerX"].Float();
  140. cre->lowerRightMissleOffsetY = offsets["lowerY"].Float();
  141. int i = 0;
  142. BOOST_FOREACH (auto angle, missle["frameAngles"].Vector())
  143. {
  144. cre->missleFrameAngles[i++] = angle.Float();
  145. }
  146. //we need to know creature id to add it
  147. VLC->creh->idToProjectile[cre->idNumber] = value->String();
  148. //TODO: sounds
  149. //how to pass info to Client?
  150. auto sounds = node["sound"];
  151. //CreaturesBattleSounds cbs;
  152. //cbs.attack = sounds["attack"].String();
  153. //cbs.defend = sounds["defend"].String();
  154. //cbs.killed = sounds["killed"].String(); // was killed or died
  155. //cbs.move = sounds["move"].String();
  156. //cbs.shoot = sounds["shoot"].String(); // range attack
  157. //cbs.wince = sounds["wince"].String(); // attacked but did not die
  158. //cbs.ext1 = ""; // creature specific extension
  159. //cbs.ext2 = ""; // creature specific extension
  160. //cbs.startMoving = sounds["moveStart"].String(); // usually same as ext1
  161. //cbs.endMoving = sounds["moveEnd"].String(); // usually same as ext2
  162. //CCS->soundh->CBattleSounds.push_back(cbs);
  163. creatures.push_back(cre);
  164. return cre;
  165. }
  166. void CModHandler::recreateHandlers()
  167. {
  168. //TODO: consider some template magic to unify all handlers?
  169. VLC->arth->artifacts.clear();
  170. VLC->creh->creatures.clear(); //TODO: what about items from original game?
  171. BOOST_FOREACH (auto mod, activeMods)
  172. {
  173. BOOST_FOREACH (auto art, allMods[mod].artifacts)
  174. {
  175. VLC->arth->artifacts.push_back (artifacts[art]);
  176. //TODO: recreate types / limiters based on string id
  177. }
  178. BOOST_FOREACH (auto creature, allMods[mod].creatures)
  179. {
  180. VLC->creh->creatures.push_back (creatures[creature]);
  181. //TODO: recreate upgrades and other properties based on string id
  182. }
  183. }
  184. }
  185. CModHandler::~CModHandler()
  186. {
  187. }