1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/CSpellHandler.h

348 lines
9.8 KiB
C
Raw Normal View History

#pragma once
#include "IHandlerBase.h"
#include "../lib/ConstTransitivePtr.h"
#include "int3.h"
#include "GameConstants.h"
#include "BattleHex.h"
#include "HeroBonus.h"
2009-05-14 07:49:04 +03:00
/*
* CSpellHandler.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
*
*/
class CSpell;
class ISpellMechanics;
class CLegacyConfigParser;
class CGHeroInstance;
class CStack;
class CBattleInfoCallback;
struct CPackForClient;
2014-11-12 10:03:55 +02:00
struct SpellSchoolInfo
{
ESpellSchool id; //backlink
Bonus::BonusType damagePremyBonus;
Bonus::BonusType immunityBonus;
std::string jsonName;
SecondarySkill::ESecondarySkill skill;
Bonus::BonusType knoledgeBonus;
2014-11-12 10:03:55 +02:00
};
static const SpellSchoolInfo SPELL_SCHOOL_CONFIG[4] =
2014-11-12 10:03:55 +02:00
{
{
ESpellSchool::AIR,
Bonus::AIR_SPELL_DMG_PREMY,
Bonus::AIR_IMMUNITY,
"air",
SecondarySkill::AIR_MAGIC,
Bonus::AIR_SPELLS
2014-11-12 10:03:55 +02:00
},
{
ESpellSchool::FIRE,
Bonus::FIRE_SPELL_DMG_PREMY,
Bonus::FIRE_IMMUNITY,
"fire",
SecondarySkill::FIRE_MAGIC,
Bonus::FIRE_SPELLS
2014-11-12 10:03:55 +02:00
},
{
ESpellSchool::WATER,
Bonus::WATER_SPELL_DMG_PREMY,
Bonus::WATER_IMMUNITY,
"water",
SecondarySkill::WATER_MAGIC,
Bonus::WATER_SPELLS
2014-11-12 10:03:55 +02:00
},
{
ESpellSchool::EARTH,
Bonus::EARTH_SPELL_DMG_PREMY,
Bonus::EARTH_IMMUNITY,
"earth",
SecondarySkill::EARTH_MAGIC,
Bonus::EARTH_SPELLS
2014-11-12 10:03:55 +02:00
}
};
///callback to be provided by server
class DLL_LINKAGE SpellCastEnvironment
{
public:
virtual void sendAndApply(CPackForClient * info) = 0;
};
///helper struct
struct DLL_LINKAGE SpellCastContext
{
public:
2014-11-23 13:10:29 +02:00
const SpellCastEnvironment * env;
int spellLvl;
// BattleHex destination;
ui8 casterSide;
PlayerColor casterColor;
2014-11-23 13:10:29 +02:00
const CGHeroInstance * caster;
const CGHeroInstance * secHero;
int usedSpellPower;
ECastingMode::ECastingMode mode;
2014-11-23 13:10:29 +02:00
const CStack * targetStack;
const CStack * selectedStack;
};
class DLL_LINKAGE CSpell
{
public:
2014-05-01 01:48:33 +03:00
struct LevelInfo
{
std::string description; //descriptions of spell for skill level
si32 cost; //per skill level: 0 - none, 1 - basic, etc
si32 power; //per skill level: 0 - none, 1 - basic, etc
si32 AIValue; //AI values: per skill level: 0 - none, 1 - basic, etc
bool smartTarget;
bool clearTarget;
bool clearAffected;
2014-05-01 01:48:33 +03:00
std::string range;
std::vector<Bonus> effects;
LevelInfo();
~LevelInfo();
template <typename Handler> void serialize(Handler &h, const int version)
{
h & description & cost & power & AIValue & smartTarget & range & effects;
h & clearTarget & clearAffected;
2014-05-01 01:48:33 +03:00
}
};
/** \brief Low level accessor. Don`t use it if absolutely necessary
*
* \param level. spell school level
* \return Spell level info structure
*
*/
const CSpell::LevelInfo& getLevelInfo(const int level) const;
public:
enum ETargetType {NO_TARGET, CREATURE, OBSTACLE, LOCATION};
enum ESpellPositiveness {NEGATIVE = -1, NEUTRAL = 0, POSITIVE = 1};
struct TargetInfo
{
2014-05-01 01:48:33 +03:00
ETargetType type;
bool smart;
bool massive;
bool onlyAlive;
///no immunity on primary target (mostly spell-like attack)
bool alwaysHitDirectly;
2014-11-13 14:00:45 +02:00
bool clearTarget;
bool clearAffected;
TargetInfo(const CSpell * spell, const int level);
TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode);
private:
void init(const CSpell * spell, const int level);
};
SpellID id;
std::string identifier; //???
std::string name;
si32 level;
2014-11-12 10:03:55 +02:00
bool earth; //deprecated
bool water; //deprecated
bool fire; //deprecated
bool air; //deprecated
std::map<ESpellSchool, bool> school; //todo: use this instead of separate boolean fields
si32 power; //spell's power
std::map<TFaction, si32> probabilities; //% chance to gain for castles
bool combatSpell; //is this spell combat (true) or adventure (false)
bool creatureAbility; //if true, only creatures can use this spell
si8 positiveness; //1 if spell is positive for influenced stacks, 0 if it is indifferent, -1 if it's negative
std::vector<SpellID> counteredSpells; //spells that are removed when effect of this spell is placed on creature (for bless-curse, haste-slow, and similar pairs)
2012-09-14 02:41:03 +03:00
CSpell();
~CSpell();
bool isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const;
std::vector<BattleHex> rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes = nullptr ) const; //convert range to specific hexes; last optional out parameter is set to true, if spell would cover unavailable hexes (that are not included in ret)
si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)
ETargetType getTargetType() const; //deprecated
CSpell::TargetInfo getTargetInfo(const int level) const;
bool isCombatSpell() const;
bool isAdventureSpell() const;
bool isCreatureAbility() const;
2013-02-02 20:20:31 +03:00
bool isPositive() const;
bool isNegative() const;
2014-05-17 13:36:49 +03:00
bool isNeutral() const;
2013-02-02 20:20:31 +03:00
bool isDamageSpell() const;
bool isHealingSpell() const;
bool isRisingSpell() const;
bool isOffensiveSpell() const;
2013-02-02 20:20:31 +03:00
bool isSpecialSpell() const;
bool hasEffects() const;
void getEffects(std::vector<Bonus> &lst, const int level) const;
//internal, for use only by Mechanics classes
ESpellCastProblem::ESpellCastProblem isImmuneBy(const IBonusBearer *obj) const;
//checks for creature immunity / anything that prevent casting *at given hex* - doesn't take into acount general problems such as not having spellbook or mana points etc.
ESpellCastProblem::ESpellCastProblem isImmuneByStack(const CGHeroInstance * caster, const CStack * obj) const;
2014-11-12 07:02:27 +02:00
//internal, for use only by Mechanics classes. applying secondary skills
2014-11-12 07:02:27 +02:00
ui32 calculateBonus(ui32 baseDamage, const CGHeroInstance * caster, const CStack * affectedCreature) const;
///calculate spell damage on stack taking caster`s secondary skills and affectedCreature`s bonuses into account
ui32 calculateDamage(const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const;
///calculate healed HP for all spells casted by hero
ui32 calculateHealedHP(const CGHeroInstance * caster, const CStack * stack, const CStack * sacrificedStack = nullptr) const;
///selects from allStacks actually affected stacks
std::set<const CStack *> getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const CGHeroInstance * caster = nullptr) const;
si32 getCost(const int skillLevel) const;
2014-05-01 01:48:33 +03:00
/**
* Returns spell level power, base power ignored
*/
si32 getPower(const int skillLevel) const;
2014-05-01 01:48:33 +03:00
// /**
// * Returns spell power, taking base power into account
// */
// si32 calculatePower(const int skillLevel) const;
si32 getProbability(const TFaction factionId) const;
/**
2014-05-01 01:48:33 +03:00
* Returns resource name of icon for SPELL_IMMUNITY bonus
*/
const std::string& getIconImmune() const;
const std::string& getCastSound() const;
template <typename Handler> void serialize(Handler &h, const int version)
{
2014-11-13 15:57:49 +02:00
h & identifier & id & name & level & power
2014-05-01 01:48:33 +03:00
& probabilities & attributes & combatSpell & creatureAbility & positiveness & counteredSpells & mainEffectAnim;
h & isRising & isDamage & isOffensive;
h & targetType;
h & immunities & limiters & absoluteImmunities & absoluteLimiters;
h & iconImmune;
h & defaultProbability;
2014-05-01 01:48:33 +03:00
h & isSpecial;
2014-05-01 01:48:33 +03:00
h & castSound & iconBook & iconEffect & iconScenarioBonus & iconScroll;
2014-05-01 01:48:33 +03:00
h & levels;
2014-11-13 15:57:49 +02:00
h & school;
if(!h.saving)
2014-11-12 10:03:55 +02:00
setup();
}
friend class CSpellHandler;
friend class Graphics;
private:
2014-05-01 01:48:33 +03:00
void setIsOffensive(const bool val);
void setIsRising(const bool val);
//call this after load or deserialization. cant be done in constructor.
2014-11-12 10:03:55 +02:00
void setup();
void setupMechanics();
private:
2014-05-01 01:48:33 +03:00
si32 defaultProbability;
bool isRising;
bool isDamage;
bool isOffensive;
bool isSpecial;
std::string attributes; //reference only attributes //todo: remove or include in configuration format, currently unused
ETargetType targetType;
std::vector<Bonus::BonusType> immunities; //any of these grants immunity
std::vector<Bonus::BonusType> absoluteImmunities; //any of these grants immunity, can't be negated
std::vector<Bonus::BonusType> limiters; //all of them are required to be affected
std::vector<Bonus::BonusType> absoluteLimiters; //all of them are required to be affected, can't be negated
2013-02-02 20:20:31 +03:00
///graphics related stuff
std::string iconImmune;
std::string iconBook;
std::string iconEffect;
std::string iconScenarioBonus;
std::string iconScroll;
///sound related stuff
std::string castSound;
std::vector<LevelInfo> levels;
ISpellMechanics * mechanics;//(!) do not serialize
};
bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos); //for spells like Dimension Door
class DLL_LINKAGE CSpellHandler: public CHandlerBase<SpellID, CSpell>
{
public:
2009-09-25 06:21:56 +03:00
CSpellHandler();
virtual ~CSpellHandler();
2014-05-01 01:48:33 +03:00
///IHandler base
std::vector<JsonNode> loadLegacyData(size_t dataSize) override;
void afterLoadFinalization() override;
void beforeValidate(JsonNode & object) override;
/**
* Gets a list of default allowed spells. OH3 spells are all allowed by default.
*
* @return a list of allowed spells, the index is the spell id and the value either 0 for not allowed or 1 for allowed
*/
std::vector<bool> getDefaultAllowed() const override;
2014-03-08 22:02:47 +03:00
const std::string getTypeName() const override;
template <typename Handler> void serialize(Handler &h, const int version)
{
h & objects ;
}
protected:
2014-05-01 01:48:33 +03:00
CSpell * loadFromJson(const JsonNode & json) override;
};