CModHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. VLC->townh->loadFactions(config["factions"]);
  72. const JsonNode *value = &config["creatures"];
  73. BOOST_FOREACH (auto creature, value->Vector())
  74. {
  75. loadCreature (creature);//create and push back creature
  76. }
  77. }
  78. }
  79. void CModHandler::saveConfigToFile (std::string name)
  80. {
  81. //JsonNode savedConf = config;
  82. //JsonNode schema(ResourceID("config/defaultSettings.json"));
  83. //savedConf.Struct().erase("session");
  84. //savedConf.minimize(schema);
  85. CResourceHandler::get()->createResource("config/" + name +".json");
  86. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/" + name +".json")), std::ofstream::trunc);
  87. //file << savedConf;
  88. }
  89. CCreature * CModHandler::loadCreature (const JsonNode &node)
  90. {
  91. CCreature * cre = new CCreature();
  92. const JsonNode *value; //optional value
  93. //TODO: ref name?
  94. const JsonNode & name = node["name"];
  95. cre->nameSing = name["singular"].String();
  96. cre->namePl = name["plural"].String();
  97. value = &name["reference"];
  98. if (!value->isNull())
  99. cre->nameRef = value->String();
  100. else
  101. cre->nameRef = cre->nameSing;
  102. cre->cost = Res::ResourceSet(node["cost"]);
  103. cre->level = node["level"].Float();
  104. cre->faction = -1; //neutral faction is 9 for now. Will be replaced by string -> id conversion
  105. //TODO: node["faction"].String() to id
  106. cre->fightValue = node["fightValue"].Float();
  107. cre->AIValue = node["aiValue"].Float();
  108. cre->growth = node["growth"].Float();
  109. cre->addBonus(node["hitPoints"].Float(), Bonus::STACK_HEALTH);
  110. cre->addBonus(node["speed"].Float(), Bonus::STACKS_SPEED);
  111. cre->addBonus(node["attack"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK);
  112. cre->addBonus(node["defense"].Float(), Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE);
  113. const JsonNode & vec = node["damage"];
  114. cre->addBonus(vec["min"].Float(), Bonus::CREATURE_DAMAGE, 1);
  115. cre->addBonus(vec["max"].Float(), Bonus::CREATURE_DAMAGE, 2);
  116. auto & amounts = node ["advMapAmount"];
  117. cre->ammMin = amounts["min"].Float();
  118. cre->ammMax = amounts["max"].Float();
  119. //optional
  120. BOOST_FOREACH (auto & str, node["upgrades"].Vector())
  121. {
  122. cre->upgradeNames.insert (str.String());
  123. }
  124. value = &node["shots"];
  125. if (!value->isNull())
  126. cre->addBonus(value->Float(), Bonus::SHOTS);
  127. value = &node["spellPoints"];
  128. if (!value->isNull())
  129. cre->addBonus(value->Float(), Bonus::CASTS);
  130. cre->doubleWide = node["doubleWide"].Bool();
  131. BOOST_FOREACH (const JsonNode &bonus, node["abilities"].Vector())
  132. {
  133. auto b = ParseBonus(bonus);
  134. b->source = Bonus::CREATURE_ABILITY;
  135. b->duration = Bonus::PERMANENT;
  136. cre->addNewBonus(b);
  137. }
  138. BOOST_FOREACH (const JsonNode &exp, node["stackExperience"].Vector())
  139. {
  140. auto bonus = ParseBonus (exp["bonus"]);
  141. bonus->source = Bonus::STACK_EXPERIENCE;
  142. bonus->duration = Bonus::PERMANENT;
  143. const JsonVector &values = exp["values"].Vector();
  144. int lowerLimit = 1;//, upperLimit = 255;
  145. if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
  146. {
  147. BOOST_FOREACH (const JsonNode &val, values)
  148. {
  149. if (val.Bool() == true)
  150. {
  151. bonus->limiter = make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
  152. cre->addNewBonus (new Bonus(*bonus)); //bonuses must be unique objects
  153. break; //TODO: allow bonuses to turn off?
  154. }
  155. ++lowerLimit;
  156. }
  157. }
  158. else
  159. {
  160. int lastVal = 0;
  161. BOOST_FOREACH (const JsonNode &val, values)
  162. {
  163. if (val.Float() != lastVal)
  164. {
  165. bonus->val = val.Float() - lastVal;
  166. bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
  167. cre->addNewBonus (new Bonus(*bonus));
  168. }
  169. lastVal = val.Float();
  170. ++lowerLimit;
  171. }
  172. }
  173. }
  174. //graphics
  175. const JsonNode & graphics = node["graphics"];
  176. cre->animDefName = graphics["animation"].String();
  177. cre->timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
  178. cre->troopCountLocationOffset = graphics["troopCountLocationOffset"].Float();
  179. cre->attackClimaxFrame = graphics["attackClimaxFrame"].Float();
  180. const JsonNode & animationTime = graphics["animationTime"];
  181. cre->walkAnimationTime = animationTime["walk"].Float();
  182. cre->attackAnimationTime = animationTime["attack"].Float();
  183. cre->flightAnimationDistance = animationTime["flight"].Float(); //?
  184. //TODO: background?
  185. const JsonNode & missle = graphics["missle"];
  186. //TODO: parse
  187. value = &missle["projectile"];
  188. if (value->isNull())
  189. cre->projectile = "PLCBOWX.DEF";
  190. else
  191. cre->projectile = value->String();
  192. value = &missle["spinning"];
  193. if (value->isNull())
  194. cre->projectileSpin = false; //no animation by default to avoid crash
  195. else
  196. cre->projectileSpin = value->Bool();
  197. const JsonNode & offsets = missle["offset"];
  198. cre->upperRightMissleOffsetX = offsets["upperX"].Float();
  199. cre->upperRightMissleOffsetY = offsets["upperY"].Float();
  200. cre->rightMissleOffsetX = offsets["middleX"].Float();
  201. cre->rightMissleOffsetY = offsets["middleY"].Float();
  202. cre->lowerRightMissleOffsetX = offsets["lowerX"].Float();
  203. cre->lowerRightMissleOffsetY = offsets["lowerY"].Float();
  204. int i = 0;
  205. BOOST_FOREACH (auto & angle, missle["frameAngles"].Vector())
  206. {
  207. cre->missleFrameAngles[i++] = angle.Float();
  208. }
  209. cre->advMapDef = graphics["map"].String();
  210. cre->iconIndex = graphics["iconIndex"].Float();
  211. const JsonNode & sounds = node["sound"];
  212. #define GET_SOUND_VALUE(value_name) do { cre->sounds.value_name = sounds[#value_name].String(); } while(0)
  213. GET_SOUND_VALUE(attack);
  214. GET_SOUND_VALUE(defend);
  215. GET_SOUND_VALUE(killed);
  216. GET_SOUND_VALUE(move);
  217. GET_SOUND_VALUE(shoot);
  218. GET_SOUND_VALUE(wince);
  219. GET_SOUND_VALUE(ext1);
  220. GET_SOUND_VALUE(ext2);
  221. GET_SOUND_VALUE(startMoving);
  222. GET_SOUND_VALUE(endMoving);
  223. #undef GET_SOUND_VALUE
  224. creatures.push_back(cre);
  225. tlog3 << "Added new creature " << cre->nameRef << "\n";
  226. return cre;
  227. }
  228. void CModHandler::recreateAdvMapDefs()
  229. {
  230. BOOST_FOREACH (auto creature, creatures)
  231. {
  232. //generate adventure map object info & graphics
  233. CGDefInfo* nobj = new CGDefInfo (*VLC->dobjinfo->gobjs[Obj::MONSTER][0]);//copy all typical properties
  234. nobj->name = creature->advMapDef; //change only def name (?)
  235. VLC->dobjinfo->gobjs[Obj::MONSTER][creature->idNumber] = nobj;
  236. }
  237. }
  238. void CModHandler::recreateHandlers()
  239. {
  240. //TODO: consider some template magic to unify all handlers?
  241. //VLC->arth->artifacts.clear();
  242. //VLC->creh->creatures.clear(); //TODO: what about items from original game?
  243. BOOST_FOREACH (auto creature, creatures)
  244. {
  245. creature->idNumber = VLC->creh->creatures.size(); //calculate next index for every used creature
  246. BOOST_FOREACH (auto bonus, creature->getBonusList())
  247. {
  248. bonus->sid = creature->idNumber;
  249. }
  250. VLC->creh->creatures.push_back (creature);
  251. //TODO: use refName?
  252. //if (creature->nameRef.size())
  253. // VLC->creh->nameToID[creature->nameRef] = creature->idNumber;
  254. VLC->creh->nameToID[creature->nameSing] = creature->idNumber;
  255. }
  256. recreateAdvMapDefs();
  257. BOOST_FOREACH (auto creature, VLC->creh->creatures) //populate upgrades described with string
  258. {
  259. BOOST_FOREACH (auto upgradeName, creature->upgradeNames)
  260. {
  261. auto it = VLC->creh->nameToID.find(upgradeName);
  262. if (it != VLC->creh->nameToID.end())
  263. {
  264. creature->upgrades.insert (it->second);
  265. }
  266. }
  267. }
  268. VLC->creh->buildBonusTreeForTiers(); //do that after all new creatures are loaded
  269. BOOST_FOREACH (auto mod, activeMods) //inactive part
  270. {
  271. BOOST_FOREACH (auto art, allMods[mod].artifacts)
  272. {
  273. VLC->arth->artifacts.push_back (artifacts[art]);
  274. //TODO: recreate types / limiters based on string id
  275. }
  276. BOOST_FOREACH (auto creature, allMods[mod].creatures)
  277. {
  278. VLC->creh->creatures.push_back (creatures[creature]);
  279. //TODO VLC->creh->notUsedMonster.push_back (creatures[creature]);
  280. //TODO: recreate upgrades and other properties based on string id
  281. }
  282. }
  283. }
  284. CModHandler::~CModHandler()
  285. {
  286. }