2
0

CModHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "StdInc.h"
  2. #include "CModHandler.h"
  3. #include "CDefObjInfoHandler.h"
  4. #include "JsonNode.h"
  5. #include "Filesystem/CResourceLoader.h"
  6. #include "Filesystem/ISimpleResourceLoader.h"
  7. /*
  8. * CModHandler.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class CArtHandler;
  17. class CHeroHandler;
  18. class CCreatureHandler;
  19. class CSpellHandler;
  20. class CBuildingHandler;
  21. class CObjectHandler;
  22. class CDefObjInfoHandler;
  23. class CTownHandler;
  24. class CGeneralTextHandler;
  25. class ResourceLocator;
  26. CModHandler::CModHandler()
  27. {
  28. VLC->modh = this;
  29. loadConfigFromFile ("defaultMods");
  30. //CResourceHandler::loadModsFilesystems(); //scan for all mods
  31. //TODO: mod filesystem is already initialized at LibClasses launch
  32. //TODO: load default (last?) config
  33. }
  34. artID CModHandler::addNewArtifact (CArtifact * art)
  35. {
  36. int id = artifacts.size();
  37. artifacts.push_back (art);
  38. return id;
  39. }
  40. creID CModHandler::addNewCreature (CCreature * cre)
  41. {
  42. int id = creatures.size();
  43. creatures.push_back (cre);
  44. return id;
  45. }
  46. void CModHandler::loadConfigFromFile (std::string name)
  47. {
  48. const JsonNode config(ResourceID("config/" + name + ".json"));
  49. const JsonNode & hardcodedFeatures = config["hardcodedFeatures"];
  50. settings.CREEP_SIZE = hardcodedFeatures["CREEP_SIZE"].Float();
  51. settings.WEEKLY_GROWTH = hardcodedFeatures["WEEKLY_GROWTH_PERCENT"].Float();
  52. settings.NEUTRAL_STACK_EXP = hardcodedFeatures["NEUTRAL_STACK_EXP_DAILY"].Float();
  53. settings.MAX_BUILDING_PER_TURN = hardcodedFeatures["MAX_BUILDING_PER_TURN"].Float();
  54. settings.DWELLINGS_ACCUMULATE_CREATURES = hardcodedFeatures["DWELLINGS_ACCUMULATE_CREATURES"].Bool();
  55. settings.ALL_CREATURES_GET_DOUBLE_MONTHS = hardcodedFeatures["ALL_CREATURES_GET_DOUBLE_MONTHS"].Bool();
  56. const JsonNode & gameModules = config["modules"];
  57. modules.STACK_EXP = gameModules["STACK_EXPERIENCE"].Bool();
  58. modules.STACK_ARTIFACT = gameModules["STACK_ARTIFACTS"].Bool();
  59. modules.COMMANDERS = gameModules["COMMANDERS"].Bool();
  60. modules.MITHRIL = gameModules["MITHRIL"].Bool();
  61. //TODO: load only mods from the list
  62. //TODO: read mods from Mods/ folder
  63. auto & configList = CResourceHandler::get()->getResourcesWithName (ResourceID("CONFIG/mod.json"));
  64. BOOST_FOREACH(auto & entry, configList)
  65. {
  66. auto stream = entry.getLoader()->load (entry.getResourceName());
  67. std::unique_ptr<ui8[]> textData (new ui8[stream->getSize()]);
  68. stream->read (textData.get(), stream->getSize());
  69. tlog3 << "\t\tFound mod file: " << entry.getResourceName() << "\n";
  70. const JsonNode config ((char*)textData.get(), stream->getSize());
  71. const JsonNode *value = &config["creatures"];
  72. BOOST_FOREACH (auto creature, value->Vector())
  73. {
  74. auto cre = loadCreature (creature); //create and push back creature
  75. }
  76. }
  77. }
  78. void CModHandler::saveConfigToFile (std::string name)
  79. {
  80. //JsonNode savedConf = config;
  81. //JsonNode schema(ResourceID("config/defaultSettings.json"));
  82. //savedConf.Struct().erase("session");
  83. //savedConf.minimize(schema);
  84. CResourceHandler::get()->createResource("config/" + name +".json");
  85. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
  86. //file << savedConf;
  87. }
  88. CCreature * CModHandler::loadCreature (const JsonNode &node)
  89. {
  90. CCreature * cre = new CCreature();
  91. const JsonNode *value; //optional value
  92. //TODO: ref name?
  93. const JsonNode & name = node["name"];
  94. cre->nameSing = name["singular"].String();
  95. cre->namePl = name["plural"].String();
  96. cre->nameRef = cre->nameSing;
  97. //TODO: export resource set to separate function?
  98. const JsonNode & cost = node["cost"];
  99. if (cost.getType() == JsonNode::DATA_FLOAT) //gold
  100. {
  101. cre->cost[Res::GOLD] = cost.Float();
  102. }
  103. else if (cost.getType() == JsonNode::DATA_VECTOR)
  104. {
  105. int i = 0;
  106. BOOST_FOREACH (auto & val, cost.Vector())
  107. {
  108. cre->cost[i++] = val.Float();
  109. }
  110. }
  111. else //damn you...
  112. {
  113. value = &cost["gold"];
  114. if (!value->isNull())
  115. cre->cost[Res::GOLD] = value->Float();
  116. value = &cost["gems"];
  117. if (!value->isNull())
  118. cre->cost[Res::GEMS] = value->Float();
  119. value = &cost["crystal"];
  120. if (!value->isNull())
  121. cre->cost[Res::CRYSTAL] = value->Float();
  122. value = &cost["mercury"];
  123. if (!value->isNull())
  124. cre->cost[Res::MERCURY] = value->Float();
  125. value = &cost["sulfur"];
  126. if (!value->isNull())
  127. cre->cost[Res::SULFUR] = value->Float();
  128. value = &cost["ore"];
  129. if (!value->isNull())
  130. cre->cost[Res::ORE] = value->Float();
  131. value = &cost["wood"];
  132. if (!value->isNull())
  133. cre->cost[Res::WOOD] = value->Float();
  134. value = &cost["mithril"];
  135. if (!value->isNull())
  136. cre->cost[Res::MITHRIL] = value->Float();
  137. }
  138. cre->level = node["level"].Float();
  139. cre->faction = -1; //neutral
  140. //TODO: node["faction"].String() to id or just node["faction"].Float();
  141. cre->fightValue = node["fightValue"].Float();
  142. cre->AIValue = node["aiValue"].Float();
  143. cre->growth = node["growth"].Float();
  144. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  145. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  146. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  147. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  148. const JsonNode & vec = node["damage"];
  149. cre->addBonus(vec["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
  150. cre->addBonus(vec["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
  151. auto & amounts = node ["advMapAmount"];
  152. cre->ammMin = amounts["min"].Float();
  153. cre->ammMax = amounts["max"].Float();
  154. //optional
  155. value = &node["upgrades"];
  156. if (!value->isNull())
  157. {
  158. BOOST_FOREACH (auto & str, value->Vector())
  159. {
  160. cre->upgradeNames.insert (str.String());
  161. }
  162. }
  163. value = &node["shots"];
  164. if (!value->isNull())
  165. cre->addBonus(value->Float(), Bonus::SHOTS);
  166. value = &node["spellPoints"];
  167. if (!value->isNull())
  168. cre->addBonus(value->Float(), Bonus::CASTS);
  169. value = &node["doubleWide"];
  170. if (!value->isNull())
  171. cre->doubleWide = value->Bool();
  172. else
  173. cre->doubleWide = false;
  174. value = &node["abilities"];
  175. if (!value->isNull())
  176. {
  177. BOOST_FOREACH (const JsonNode &bonus, value->Vector())
  178. {
  179. cre->addNewBonus(ParseBonus(bonus));
  180. }
  181. }
  182. //graphics
  183. const JsonNode & graphics = node["graphics"];
  184. cre->animDefName = graphics["animation"].String();
  185. cre->timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  186. cre->troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  187. cre->attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  188. const JsonNode & animationTime = graphics["animationTime"];
  189. cre->walkAnimationTime = animationTime["walk"].Float();
  190. cre->attackAnimationTime = animationTime["attack"].Float();
  191. cre->flightAnimationDistance = animationTime["flight"].Float(); //?
  192. //TODO: background?
  193. const JsonNode & missle = graphics["missle"];
  194. const JsonNode & offsets = missle["offset"];
  195. cre->upperRightMissleOffsetX = offsets["upperX"].Float();
  196. cre->upperRightMissleOffsetY = offsets["upperY"].Float();
  197. cre->rightMissleOffsetX = offsets["middleX"].Float();
  198. cre->rightMissleOffsetY = offsets["middleY"].Float();
  199. cre->lowerRightMissleOffsetX = offsets["lowerX"].Float();
  200. cre->lowerRightMissleOffsetY = offsets["lowerY"].Float();
  201. int i = 0;
  202. BOOST_FOREACH (auto & angle, missle["frameAngles"].Vector())
  203. {
  204. cre->missleFrameAngles[i++] = angle.Float();
  205. }
  206. cre->advMapDef = graphics["map"].String();
  207. //TODO: parse
  208. cre->projectile = "PLCBOWX.DEF";
  209. cre->projectileSpin = false;
  210. const JsonNode & sounds = node["sound"];
  211. #define GET_SOUND_VALUE(value_name) do { value = &sounds[#value_name]; if (!value->isNull()) cre->sounds.value_name = value->String(); } while(0)
  212. GET_SOUND_VALUE(attack);
  213. GET_SOUND_VALUE(defend);
  214. GET_SOUND_VALUE(killed);
  215. GET_SOUND_VALUE(move);
  216. GET_SOUND_VALUE(shoot);
  217. GET_SOUND_VALUE(wince);
  218. GET_SOUND_VALUE(ext1);
  219. GET_SOUND_VALUE(ext2);
  220. GET_SOUND_VALUE(startMoving);
  221. GET_SOUND_VALUE(endMoving);
  222. #undef GET_SOUND_VALUE
  223. creatures.push_back(cre);
  224. return cre;
  225. }
  226. void CModHandler::recreateAdvMapDefs()
  227. {
  228. BOOST_FOREACH (auto creature, creatures)
  229. {
  230. //generate adventure map object info & graphics
  231. CGDefInfo* nobj = new CGDefInfo (*VLC->dobjinfo->gobjs[GameConstants::CREI_TYPE][0]);//copy all typical properties
  232. nobj->name = creature->advMapDef; //change only def name (?)
  233. VLC->dobjinfo->gobjs[GameConstants::CREI_TYPE][creature->idNumber] = nobj;
  234. }
  235. }
  236. void CModHandler::recreateHandlers()
  237. {
  238. //TODO: consider some template magic to unify all handlers?
  239. //VLC->arth->artifacts.clear();
  240. //VLC->creh->creatures.clear(); //TODO: what about items from original game?
  241. BOOST_FOREACH (auto creature, creatures)
  242. {
  243. creature->idNumber = VLC->creh->creatures.size(); //calculate next index for every used creature
  244. VLC->creh->creatures.push_back (creature);
  245. //TODO: use refName?
  246. //if (creature->nameRef.size())
  247. // VLC->creh->nameToID[creature->nameRef] = creature->idNumber;
  248. VLC->creh->nameToID[creature->nameSing] = creature->idNumber;
  249. }
  250. recreateAdvMapDefs();
  251. BOOST_FOREACH (auto creature, VLC->creh->creatures) //populate upgrades described with string
  252. {
  253. BOOST_FOREACH (auto upgradeName, creature->upgradeNames)
  254. {
  255. auto it = VLC->creh->nameToID.find(upgradeName);
  256. if (it != VLC->creh->nameToID.end())
  257. {
  258. creature->upgrades.insert (it->second);
  259. }
  260. }
  261. }
  262. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  263. BOOST_FOREACH (auto mod, activeMods) //inactive part
  264. {
  265. BOOST_FOREACH (auto art, allMods[mod].artifacts)
  266. {
  267. VLC->arth->artifacts.push_back (artifacts[art]);
  268. //TODO: recreate types / limiters based on string id
  269. }
  270. BOOST_FOREACH (auto creature, allMods[mod].creatures)
  271. {
  272. VLC->creh->creatures.push_back (creatures[creature]);
  273. //TODO VLC->creh->notUsedMonster.push_back (creatures[creature]);
  274. //TODO: recreate upgrades and other properties based on string id
  275. }
  276. }
  277. }
  278. CModHandler::~CModHandler()
  279. {
  280. }