/* * CHeroHandler.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once #include #include #include #include #include "../lib/ConstTransitivePtr.h" #include "GameConstants.h" #include "HeroBonus.h" #include "IHandlerBase.h" class CHeroClass; class CGameInfo; class CGHeroInstance; struct BattleHex; class JsonNode; class CRandomGenerator; class JsonSerializeFormat; class Terrain; struct SSpecialtyInfo { si32 type; si32 val; si32 subtype; si32 additionalinfo; template void serialize(Handler &h, const int version) { h & type; h & val; h & subtype; h & additionalinfo; } }; struct SSpecialtyBonus /// temporary hold { ui8 growsWithLevel; BonusList bonuses; template void serialize(Handler &h, const int version) { h & growsWithLevel; h & bonuses; } }; class DLL_LINKAGE CHero : public HeroType { public: struct InitialArmyStack { ui32 minAmount; ui32 maxAmount; CreatureID creature; template void serialize(Handler &h, const int version) { h & minAmount; h & maxAmount; h & creature; } }; std::string identifier; HeroTypeID ID; si32 imageIndex; std::vector initialArmy; CHeroClass * heroClass; std::vector > secSkillsInit; //initial secondary skills; first - ID of skill, second - level of skill (1 - basic, 2 - adv., 3 - expert) std::vector specDeprecated; std::vector specialtyDeprecated; BonusList specialty; std::set spells; bool haveSpellBook; bool special; // hero is special and won't be placed in game (unless preset on map), e.g. campaign heroes ui8 sex; // default sex: 0=male, 1=female /// Localized texts std::string name; //name of hero std::string biography; std::string specName; std::string specDescr; std::string specTooltip; /// Graphics std::string iconSpecSmall; std::string iconSpecLarge; std::string portraitSmall; std::string portraitLarge; std::string battleImage; CHero(); virtual ~CHero(); int32_t getIndex() const override; int32_t getIconIndex() const override; const std::string & getName() const override; const std::string & getJsonKey() const override; HeroTypeID getId() const override; void registerIcons(const IconRegistar & cb) const override; void updateFrom(const JsonNode & data); void serializeJson(JsonSerializeFormat & handler); template void serialize(Handler &h, const int version) { h & ID; h & imageIndex; h & initialArmy; h & heroClass; h & secSkillsInit; h & specialty; h & spells; h & haveSpellBook; h & sex; h & special; h & name; h & biography; h & specName; h & specDescr; h & specTooltip; h & iconSpecSmall; h & iconSpecLarge; h & portraitSmall; h & portraitLarge; h & identifier; h & battleImage; } }; // convert deprecated format std::vector> SpecialtyInfoToBonuses(const SSpecialtyInfo & spec, int sid = 0); std::vector> SpecialtyBonusToBonuses(const SSpecialtyBonus & spec, int sid = 0); class DLL_LINKAGE CHeroClass : public HeroClass { public: enum EClassAffinity { MIGHT, MAGIC }; std::string identifier; std::string name; // translatable //double aggression; // not used in vcmi. TFaction faction; HeroClassID id; ui8 affinity; // affinity, using EClassAffinity enum // default chance for hero of specific class to appear in tavern, if field "tavern" was not set // resulting chance = sqrt(town.chance * heroClass.chance) ui32 defaultTavernChance; CCreature * commander; std::vector primarySkillInitial; // initial primary skills std::vector primarySkillLowLevel; // probability (%) of getting point of primary skill when getting level std::vector primarySkillHighLevel;// same for high levels (> 10) std::vector secSkillProbability; //probabilities of gaining secondary skills (out of 112), in id order std::map selectionProbability; //probability of selection in towns std::string imageBattleMale; std::string imageBattleFemale; std::string imageMapMale; std::string imageMapFemale; CHeroClass(); int32_t getIndex() const override; int32_t getIconIndex() const override; const std::string & getName() const override; const std::string & getJsonKey() const override; HeroClassID getId() const override; void registerIcons(const IconRegistar & cb) const override; bool isMagicHero() const; SecondarySkill chooseSecSkill(const std::set & possibles, CRandomGenerator & rand) const; //picks secondary skill out from given possibilities void updateFrom(const JsonNode & data); void serializeJson(JsonSerializeFormat & handler); template void serialize(Handler & h, const int version) { h & identifier; h & name; h & faction; h & id; h & defaultTavernChance; h & primarySkillInitial; h & primarySkillLowLevel; h & primarySkillHighLevel; h & secSkillProbability; h & selectionProbability; h & affinity; h & commander; h & imageBattleMale; h & imageBattleFemale; h & imageMapMale; h & imageMapFemale; if(!h.saving) { for(auto i = 0; i < secSkillProbability.size(); i++) if(secSkillProbability[i] < 0) secSkillProbability[i] = 0; } } EAlignment::EAlignment getAlignment() const; }; struct DLL_LINKAGE CObstacleInfo { si32 ID; std::string defName; std::vector allowedTerrains; std::vector allowedSpecialBfields; ui8 isAbsoluteObstacle; //there may only one such obstacle in battle and its position is always the same si32 width, height; //how much space to the right and up is needed to place obstacle (affects only placement algorithm) std::vector blockedTiles; //offsets relative to obstacle position (that is its left bottom corner) std::vector getBlocked(BattleHex hex) const; //returns vector of hexes blocked by obstacle when it's placed on hex 'hex' bool isAppropriate(Terrain terrainType, int specialBattlefield = -1) const; template void serialize(Handler &h, const int version) { h & ID; h & defName; h & allowedTerrains; h & allowedSpecialBfields; h & isAbsoluteObstacle; h & width; h & height; h & blockedTiles; } }; class DLL_LINKAGE CHeroClassHandler : public CHandlerBase { void fillPrimarySkillData(const JsonNode & node, CHeroClass * heroClass, PrimarySkill::PrimarySkill pSkill); public: std::vector loadLegacyData(size_t dataSize) override; void afterLoadFinalization() override; std::vector getDefaultAllowed() const override; ~CHeroClassHandler(); template void serialize(Handler &h, const int version) { h & objects; } protected: const std::vector & getTypeNames() const override; CHeroClass * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; }; class DLL_LINKAGE CHeroHandler : public CHandlerBase { /// expPerLEvel[i] is amount of exp needed to reach level i; /// consists of 201 values. Any higher levels require experience larger that ui64 can hold std::vector expPerLevel; /// helpers for loading to avoid huge load functions void loadHeroArmy(CHero * hero, const JsonNode & node); void loadHeroSkills(CHero * hero, const JsonNode & node); void loadHeroSpecialty(CHero * hero, const JsonNode & node); void loadExperience(); void loadBallistics(); void loadTerrains(); void loadObstacles(); public: CHeroClassHandler classes; //default costs of going through terrains. -1 means terrain is impassable std::map terrCosts; struct SBallisticsLevelInfo { ui8 keep, tower, gate, wall; //chance to hit in percent (eg. 87 is 87%) ui8 shots; //how many shots we have ui8 noDmg, oneDmg, twoDmg; //chances for shot dealing certain dmg in percent (eg. 87 is 87%); must sum to 100 ui8 sum; //I don't know if it is useful for anything, but it's in config file template void serialize(Handler &h, const int version) { h & keep; h & tower; h & gate; h & wall; h & shots; h & noDmg; h & oneDmg; h & twoDmg; h & sum; } }; std::vector ballistics; //info about ballistics ability per level; [0] - none; [1] - basic; [2] - adv; [3] - expert std::map obstacles; //info about obstacles that may be placed on battlefield std::map absoluteObstacles; //info about obstacles that may be placed on battlefield ui32 level(ui64 experience) const; //calculates level corresponding to given experience amount ui64 reqExp(ui32 level) const; //calculates experience required for given level std::vector loadLegacyData(size_t dataSize) override; void beforeValidate(JsonNode & object) override; void loadObject(std::string scope, std::string name, const JsonNode & data) override; void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override; void afterLoadFinalization() override; CHeroHandler(); ~CHeroHandler(); std::vector getDefaultAllowed() const override; template void serialize(Handler &h, const int version) { h & classes; h & objects; h & expPerLevel; h & ballistics; h & terrCosts; h & obstacles; h & absoluteObstacles; } protected: const std::vector & getTypeNames() const override; CHero * loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index) override; };