2015-02-26 19:59:18 +02:00
|
|
|
/*
|
|
|
|
* CSpellHandler.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
#include "CSpellHandler.h"
|
|
|
|
|
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#include "../filesystem/Filesystem.h"
|
|
|
|
|
|
|
|
#include "../JsonNode.h"
|
|
|
|
|
|
|
|
#include "../CModHandler.h"
|
|
|
|
#include "../StringConstants.h"
|
|
|
|
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "../CStack.h"
|
2017-06-24 16:42:05 +02:00
|
|
|
#include "../battle/BattleInfo.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
#include "../battle/CBattleInfoCallback.h"
|
2015-09-17 07:42:30 +02:00
|
|
|
#include "../CGameState.h" //todo: remove
|
|
|
|
|
|
|
|
#include "../NetPacks.h" //todo: remove
|
2015-02-02 10:25:26 +02:00
|
|
|
|
2015-03-10 22:14:58 +02:00
|
|
|
#include "ISpellMechanics.h"
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
namespace SpellConfig
|
|
|
|
{
|
|
|
|
static const std::string LEVEL_NAMES[] = {"none", "basic", "advanced", "expert"};
|
2015-02-26 19:59:18 +02:00
|
|
|
|
|
|
|
static const SpellSchoolInfo SCHOOL[4] =
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
ESpellSchool::AIR,
|
|
|
|
Bonus::AIR_SPELL_DMG_PREMY,
|
|
|
|
Bonus::AIR_IMMUNITY,
|
|
|
|
"air",
|
|
|
|
SecondarySkill::AIR_MAGIC,
|
|
|
|
Bonus::AIR_SPELLS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ESpellSchool::FIRE,
|
|
|
|
Bonus::FIRE_SPELL_DMG_PREMY,
|
|
|
|
Bonus::FIRE_IMMUNITY,
|
|
|
|
"fire",
|
|
|
|
SecondarySkill::FIRE_MAGIC,
|
|
|
|
Bonus::FIRE_SPELLS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ESpellSchool::WATER,
|
|
|
|
Bonus::WATER_SPELL_DMG_PREMY,
|
|
|
|
Bonus::WATER_IMMUNITY,
|
|
|
|
"water",
|
|
|
|
SecondarySkill::WATER_MAGIC,
|
|
|
|
Bonus::WATER_SPELLS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ESpellSchool::EARTH,
|
|
|
|
Bonus::EARTH_SPELL_DMG_PREMY,
|
|
|
|
Bonus::EARTH_IMMUNITY,
|
|
|
|
"earth",
|
|
|
|
SecondarySkill::EARTH_MAGIC,
|
|
|
|
Bonus::EARTH_SPELLS
|
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
};
|
2016-10-17 02:33:29 +02:00
|
|
|
|
|
|
|
//order as described in http://bugs.vcmi.eu/view.php?id=91
|
|
|
|
static const ESpellSchool SCHOOL_ORDER[4] =
|
|
|
|
{
|
|
|
|
ESpellSchool::AIR, //=0
|
|
|
|
ESpellSchool::FIRE, //=1
|
|
|
|
ESpellSchool::EARTH,//=3(!)
|
|
|
|
ESpellSchool::WATER //=2(!)
|
|
|
|
};
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///CSpell::LevelInfo
|
|
|
|
CSpell::LevelInfo::LevelInfo()
|
|
|
|
:description(""),cost(0),power(0),AIValue(0),smartTarget(true), clearTarget(false), clearAffected(false), range("0")
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::LevelInfo::~LevelInfo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
///CSpell
|
|
|
|
CSpell::CSpell():
|
|
|
|
id(SpellID::NONE), level(0),
|
2016-11-27 21:37:41 +02:00
|
|
|
power(0),
|
2015-02-02 10:25:26 +02:00
|
|
|
combatSpell(false), creatureAbility(false),
|
|
|
|
positiveness(ESpellPositiveness::NEUTRAL),
|
|
|
|
defaultProbability(0),
|
2016-11-27 21:37:41 +02:00
|
|
|
isRising(false), isDamage(false), isOffensive(false), isSpecial(true),
|
2015-02-02 10:25:26 +02:00
|
|
|
targetType(ETargetType::NO_TARGET),
|
2016-09-04 07:19:28 +02:00
|
|
|
mechanics(),
|
|
|
|
adventureMechanics()
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
levels.resize(GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::~CSpell()
|
|
|
|
{
|
2016-09-04 07:19:28 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const
|
|
|
|
{
|
|
|
|
mechanics->applyBattle(battle, packet);
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:09:27 +02:00
|
|
|
bool CSpell::adventureCast(const SpellCastEnvironment * env, const AdventureSpellCastParameters & parameters) const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
assert(env);
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2016-09-04 07:19:28 +02:00
|
|
|
if(!adventureMechanics.get())
|
|
|
|
{
|
|
|
|
env->complain("Invalid adventure spell cast attempt!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return adventureMechanics->adventureCast(env, parameters);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-06 05:40:23 +02:00
|
|
|
void CSpell::battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const
|
2016-02-13 18:41:28 +02:00
|
|
|
{
|
2015-02-02 10:25:26 +02:00
|
|
|
assert(env);
|
|
|
|
mechanics->battleCast(env, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CSpell::LevelInfo & CSpell::getLevelInfo(const int level) const
|
|
|
|
{
|
|
|
|
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
|
|
|
{
|
2016-08-12 18:20:57 +02:00
|
|
|
logGlobal->error("CSpell::getLevelInfo invalid school level %d", level);
|
2015-02-02 10:25:26 +02:00
|
|
|
throw new std::runtime_error("Invalid school level");
|
|
|
|
}
|
|
|
|
|
|
|
|
return levels.at(level);
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:22:52 +02:00
|
|
|
ui32 CSpell::calculateDamage(const ISpellCaster * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
//check if spell really does damage - if not, return 0
|
|
|
|
if(!isDamageSpell())
|
|
|
|
return 0;
|
2016-02-13 18:41:28 +02:00
|
|
|
return adjustRawDamage(caster, affectedCreature, calculateRawEffectValue(spellSchoolLevel, usedSpellPower));
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-05 10:36:25 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::canBeCast(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
|
2015-03-18 15:39:07 +02:00
|
|
|
{
|
2017-06-05 22:46:55 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem genProblem = cb->battleCanCastSpell(caster, mode);
|
|
|
|
if(genProblem != ESpellCastProblem::OK)
|
|
|
|
return genProblem;
|
|
|
|
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case ECastingMode::HERO_CASTING:
|
|
|
|
{
|
|
|
|
const CGHeroInstance * castingHero = dynamic_cast<const CGHeroInstance *>(caster);//todo: unify hero|creature spell cost
|
|
|
|
if(!castingHero)
|
|
|
|
{
|
|
|
|
logGlobal->debug("CSpell::canBeCast: invalid caster");
|
|
|
|
return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
|
|
|
|
return ESpellCastProblem::NO_SPELLBOOK;
|
|
|
|
if(!castingHero->canCastThisSpell(this))
|
|
|
|
return ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
|
|
|
|
if(castingHero->mana < cb->battleGetSpellCost(this, castingHero)) //not enough mana
|
|
|
|
return ESpellCastProblem::NOT_ENOUGH_MANA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-05 20:30:25 +02:00
|
|
|
if(!isCombatSpell())
|
|
|
|
return ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL;
|
2016-09-05 10:36:25 +02:00
|
|
|
|
2017-06-05 20:30:25 +02:00
|
|
|
const PlayerColor player = caster->getOwner();
|
2017-07-01 10:34:00 +02:00
|
|
|
const auto side = cb->playerToSide(player);
|
2017-06-05 20:30:25 +02:00
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
if(!side)
|
2017-06-05 20:30:25 +02:00
|
|
|
return ESpellCastProblem::INVALID;
|
|
|
|
|
|
|
|
//effect like Recanter's Cloak. Blocks also passive casting.
|
|
|
|
//TODO: check creature abilities to block
|
|
|
|
//TODO: check any possible caster
|
2017-09-06 00:03:32 +02:00
|
|
|
if(cb->battleMaxSpellLevel(side.get()) < level || cb->battleMinSpellLevel(side.get()) > level)
|
2017-06-05 20:30:25 +02:00
|
|
|
return ESpellCastProblem::SPELL_LEVEL_LIMIT_EXCEEDED;
|
|
|
|
|
|
|
|
const ESpellCastProblem::ESpellCastProblem specificProblem = mechanics->canBeCast(cb, mode, caster);
|
|
|
|
|
|
|
|
if(specificProblem != ESpellCastProblem::OK)
|
|
|
|
return specificProblem;
|
2016-09-05 10:36:25 +02:00
|
|
|
|
|
|
|
//check for creature target existence
|
2016-10-02 15:38:30 +02:00
|
|
|
//allow to cast spell if there is at least one smart target
|
2016-09-05 10:36:25 +02:00
|
|
|
if(mechanics->requiresCreatureTarget())
|
|
|
|
{
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case ECastingMode::HERO_CASTING:
|
|
|
|
case ECastingMode::CREATURE_ACTIVE_CASTING:
|
|
|
|
case ECastingMode::ENCHANTER_CASTING:
|
|
|
|
case ECastingMode::PASSIVE_CASTING:
|
|
|
|
{
|
|
|
|
TargetInfo tinfo(this, caster->getSpellSchoolLevel(this), mode);
|
|
|
|
|
|
|
|
bool targetExists = false;
|
|
|
|
|
|
|
|
for(const CStack * stack : cb->battleGetAllStacks())
|
|
|
|
{
|
2017-07-09 15:55:10 +02:00
|
|
|
const bool immune = !(stack->isValidTarget(!tinfo.onlyAlive) && ESpellCastProblem::OK == isImmuneByStack(caster, stack));
|
|
|
|
const bool ownerMatches = cb->battleMatchOwner(caster->getOwner(), stack, getPositiveness());
|
|
|
|
targetExists = !immune && ownerMatches;
|
2016-09-05 10:36:25 +02:00
|
|
|
if(targetExists)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!targetExists)
|
|
|
|
{
|
|
|
|
return ESpellCastProblem::NO_APPROPRIATE_TARGET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESpellCastProblem::OK;
|
2015-03-18 15:39:07 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
std::vector<BattleHex> CSpell::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes) const
|
|
|
|
{
|
|
|
|
return mechanics->rangeInHexes(centralHex,schoolLvl,side,outDroppedHexes);
|
|
|
|
}
|
|
|
|
|
2016-09-06 05:40:23 +02:00
|
|
|
std::vector<const CStack *> CSpell::getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, const ISpellCaster * caster, int spellLvl, BattleHex destination) const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
2017-07-15 23:49:59 +02:00
|
|
|
return mechanics->getAffectedStacks(cb, mode, caster, spellLvl, destination);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::ETargetType CSpell::getTargetType() const
|
|
|
|
{
|
|
|
|
return targetType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::forEachSchool(const std::function<void(const SpellSchoolInfo &, bool &)>& cb) const
|
|
|
|
{
|
|
|
|
bool stop = false;
|
2016-10-17 02:33:29 +02:00
|
|
|
for(ESpellSchool iter : SpellConfig::SCHOOL_ORDER)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
2016-10-17 02:33:29 +02:00
|
|
|
const SpellSchoolInfo & cnf = SpellConfig::SCHOOL[(ui8)iter];
|
2015-02-02 10:25:26 +02:00
|
|
|
if(school.at(cnf.id))
|
|
|
|
{
|
|
|
|
cb(cnf, stop);
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
if(stop)
|
|
|
|
break;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isCombatSpell() const
|
|
|
|
{
|
|
|
|
return combatSpell;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isAdventureSpell() const
|
|
|
|
{
|
|
|
|
return !combatSpell;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isCreatureAbility() const
|
|
|
|
{
|
|
|
|
return creatureAbility;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isPositive() const
|
|
|
|
{
|
|
|
|
return positiveness == POSITIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isNegative() const
|
|
|
|
{
|
|
|
|
return positiveness == NEGATIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isNeutral() const
|
|
|
|
{
|
|
|
|
return positiveness == NEUTRAL;
|
|
|
|
}
|
|
|
|
|
2016-09-29 22:14:22 +02:00
|
|
|
boost::logic::tribool CSpell::getPositiveness() const
|
|
|
|
{
|
|
|
|
switch (positiveness)
|
|
|
|
{
|
|
|
|
case CSpell::POSITIVE:
|
|
|
|
return true;
|
|
|
|
case CSpell::NEGATIVE:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return boost::logic::indeterminate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
bool CSpell::isRisingSpell() const
|
|
|
|
{
|
|
|
|
return isRising;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isDamageSpell() const
|
|
|
|
{
|
|
|
|
return isDamage;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isOffensiveSpell() const
|
|
|
|
{
|
|
|
|
return isOffensive;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isSpecialSpell() const
|
|
|
|
{
|
|
|
|
return isSpecial;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::hasEffects() const
|
|
|
|
{
|
2016-11-02 19:11:01 +02:00
|
|
|
return !levels[0].effects.empty() || !levels[0].cumulativeEffects.empty();
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
const std::string & CSpell::getIconImmune() const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
return iconImmune;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
const std::string & CSpell::getCastSound() const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
return castSound;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CSpell::getCost(const int skillLevel) const
|
|
|
|
{
|
|
|
|
return getLevelInfo(skillLevel).cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CSpell::getPower(const int skillLevel) const
|
|
|
|
{
|
|
|
|
return getLevelInfo(skillLevel).power;
|
|
|
|
}
|
|
|
|
|
|
|
|
si32 CSpell::getProbability(const TFaction factionId) const
|
|
|
|
{
|
|
|
|
if(!vstd::contains(probabilities,factionId))
|
|
|
|
{
|
|
|
|
return defaultProbability;
|
|
|
|
}
|
|
|
|
return probabilities.at(factionId);
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
void CSpell::getEffects(std::vector<Bonus> & lst, const int level, const bool cumulative, const si32 duration, boost::optional<si32 *> maxDuration) const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("invalid school level %d", level);
|
2015-02-02 10:25:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
const auto & levelObject = levels.at(level);
|
2015-02-02 10:25:26 +02:00
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
if(levelObject.effects.empty() && levelObject.cumulativeEffects.empty())
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("This spell (%s) has no effects for level %d", name, level);
|
2015-02-02 10:25:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
const auto & effects = cumulative ? levelObject.cumulativeEffects : levelObject.effects;
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
lst.reserve(lst.size() + effects.size());
|
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
for(const auto b : effects)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
2016-11-02 19:11:01 +02:00
|
|
|
Bonus nb(*b);
|
|
|
|
|
|
|
|
//use configured duration if present
|
|
|
|
if(nb.turnsRemain == 0)
|
|
|
|
nb.turnsRemain = duration;
|
|
|
|
if(maxDuration)
|
|
|
|
vstd::amax(*(maxDuration.get()), nb.turnsRemain);
|
|
|
|
|
|
|
|
lst.push_back(nb);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 23:01:33 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::canBeCastAt(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, const ISpellCaster * caster, BattleHex destination) const
|
2016-03-01 08:50:04 +02:00
|
|
|
{
|
2017-06-05 22:46:55 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem problem = canBeCast(cb, mode, caster);
|
2017-06-05 22:25:48 +02:00
|
|
|
if(problem != ESpellCastProblem::OK)
|
|
|
|
return problem;
|
|
|
|
|
2017-07-15 23:49:59 +02:00
|
|
|
return mechanics->canBeCastAt(cb, mode, caster, destination);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 14:43:39 +02:00
|
|
|
int CSpell::adjustRawDamage(const ISpellCaster * caster, const CStack * affectedCreature, int rawDamage) const
|
|
|
|
{
|
|
|
|
int ret = rawDamage;
|
|
|
|
//affected creature-specific part
|
|
|
|
if(nullptr != affectedCreature)
|
|
|
|
{
|
|
|
|
//applying protections - when spell has more then one elements, only one protection should be applied (I think)
|
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
|
|
|
{
|
|
|
|
if(affectedCreature->hasBonusOfType(Bonus::SPELL_DAMAGE_REDUCTION, (ui8)cnf.id))
|
|
|
|
{
|
2017-08-01 15:59:45 +02:00
|
|
|
ret *= 100 - affectedCreature->valOfBonuses(Bonus::SPELL_DAMAGE_REDUCTION, (ui8)cnf.id);
|
2015-09-16 14:43:39 +02:00
|
|
|
ret /= 100;
|
|
|
|
stop = true;//only bonus from one school is used
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-01 15:59:45 +02:00
|
|
|
CSelector selector = Selector::type(Bonus::SPELL_DAMAGE_REDUCTION).And(Selector::subtype(-1));
|
|
|
|
|
2015-09-16 14:43:39 +02:00
|
|
|
//general spell dmg reduction
|
2017-08-01 15:59:45 +02:00
|
|
|
if(affectedCreature->hasBonus(selector))
|
2015-09-16 14:43:39 +02:00
|
|
|
{
|
2017-08-01 15:59:45 +02:00
|
|
|
ret *= 100 - affectedCreature->valOfBonuses(selector);
|
2015-09-16 14:43:39 +02:00
|
|
|
ret /= 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
//dmg increasing
|
|
|
|
if(affectedCreature->hasBonusOfType(Bonus::MORE_DAMAGE_FROM_SPELL, id))
|
|
|
|
{
|
|
|
|
ret *= 100 + affectedCreature->valOfBonuses(Bonus::MORE_DAMAGE_FROM_SPELL, id.toEnum());
|
|
|
|
ret /= 100;
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 15:06:26 +02:00
|
|
|
if(caster != nullptr)
|
|
|
|
ret = caster->getSpellBonus(this, ret, affectedCreature);
|
2015-09-16 14:43:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSpell::calculateRawEffectValue(int effectLevel, int effectPower) const
|
|
|
|
{
|
2016-02-13 18:41:28 +02:00
|
|
|
return effectPower * power + getPower(effectLevel);
|
2015-09-16 14:43:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 12:23:13 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::internalIsImmune(const ISpellCaster * caster, const CStack *obj) const
|
2015-02-26 19:59:18 +02:00
|
|
|
{
|
2015-02-02 10:25:26 +02:00
|
|
|
//todo: use new bonus API
|
|
|
|
//1. Check absolute limiters
|
|
|
|
for(auto b : absoluteLimiters)
|
|
|
|
{
|
|
|
|
if (!obj->hasBonusOfType(b))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//2. Check absolute immunities
|
|
|
|
for(auto b : absoluteImmunities)
|
|
|
|
{
|
|
|
|
if (obj->hasBonusOfType(b))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-09-18 06:54:28 +02:00
|
|
|
{
|
|
|
|
//spell-based spell immunity (only ANTIMAGIC in OH3) is treated as absolute
|
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << Bonus::LEVEL_SPELL_IMMUNITY << "source_" << Bonus::SPELL_EFFECT;
|
2015-04-11 14:53:32 +02:00
|
|
|
|
2016-02-13 18:41:28 +02:00
|
|
|
TBonusListPtr levelImmunitiesFromSpell = obj->getBonuses(Selector::type(Bonus::LEVEL_SPELL_IMMUNITY).And(Selector::sourceType(Bonus::SPELL_EFFECT)), cachingStr.str());
|
2015-04-11 14:53:32 +02:00
|
|
|
|
2015-09-18 06:54:28 +02:00
|
|
|
if(levelImmunitiesFromSpell->size() > 0 && levelImmunitiesFromSpell->totalValue() >= level && level)
|
|
|
|
{
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
2015-04-11 14:53:32 +02:00
|
|
|
}
|
2015-09-18 06:54:28 +02:00
|
|
|
{
|
|
|
|
//SPELL_IMMUNITY absolute case
|
|
|
|
std::stringstream cachingStr;
|
|
|
|
cachingStr << "type_" << Bonus::SPELL_IMMUNITY << "subtype_" << id.toEnum() << "addInfo_1";
|
|
|
|
if(obj->hasBonus(Selector::typeSubtypeInfo(Bonus::SPELL_IMMUNITY, id.toEnum(), 1), cachingStr.str()))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2016-02-13 18:41:28 +02:00
|
|
|
}
|
2015-04-11 14:53:32 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
//check receptivity
|
|
|
|
if (isPositive() && obj->hasBonusOfType(Bonus::RECEPTIVE)) //accept all positive spells
|
2015-02-26 19:59:18 +02:00
|
|
|
return ESpellCastProblem::OK;
|
2016-02-13 18:41:28 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
//3. Check negation
|
2015-09-17 12:23:13 +02:00
|
|
|
//Orb of vulnerability
|
|
|
|
//FIXME: Orb of vulnerability mechanics is not such trivial (issue 1791)
|
|
|
|
const bool battleWideNegation = obj->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES, 0);
|
|
|
|
const bool heroNegation = obj->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES, 1);
|
|
|
|
//anyone can cast on artifact holder`s stacks
|
2016-02-13 18:41:28 +02:00
|
|
|
if(heroNegation)
|
2015-02-02 10:25:26 +02:00
|
|
|
return ESpellCastProblem::NOT_DECIDED;
|
2015-09-17 12:23:13 +02:00
|
|
|
//this stack is from other player
|
|
|
|
//todo: check that caster is always present (not trivial is this case)
|
|
|
|
//todo: NEGATE_ALL_NATURAL_IMMUNITIES special cases: dispell, chain lightning
|
|
|
|
else if(battleWideNegation && caster)
|
|
|
|
{
|
|
|
|
if(obj->owner != caster->getOwner())
|
|
|
|
return ESpellCastProblem::NOT_DECIDED;
|
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
//4. Check negatable limit
|
|
|
|
for(auto b : limiters)
|
|
|
|
{
|
|
|
|
if (!obj->hasBonusOfType(b))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//5. Check negatable immunities
|
|
|
|
for(auto b : immunities)
|
|
|
|
{
|
|
|
|
if (obj->hasBonusOfType(b))
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//6. Check elemental immunities
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem tmp = ESpellCastProblem::NOT_DECIDED;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
|
|
|
{
|
|
|
|
auto element = cnf.immunityBonus;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
if(obj->hasBonusOfType(element, 0)) //always resist if immune to all spells altogether
|
|
|
|
{
|
|
|
|
tmp = ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
stop = true;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
else if(!isPositive()) //negative or indifferent
|
|
|
|
{
|
|
|
|
if((isDamageSpell() && obj->hasBonusOfType(element, 2)) || obj->hasBonusOfType(element, 1))
|
|
|
|
{
|
|
|
|
tmp = ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
stop = true;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
});
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
if(tmp != ESpellCastProblem::NOT_DECIDED)
|
|
|
|
return tmp;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
TBonusListPtr levelImmunities = obj->getBonuses(Selector::type(Bonus::LEVEL_SPELL_IMMUNITY));
|
|
|
|
|
|
|
|
if(obj->hasBonusOfType(Bonus::SPELL_IMMUNITY, id)
|
|
|
|
|| ( levelImmunities->size() > 0 && levelImmunities->totalValue() >= level && level))
|
|
|
|
{
|
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESpellCastProblem::NOT_DECIDED;
|
|
|
|
}
|
|
|
|
|
2015-09-26 19:09:54 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::isImmuneByStack(const ISpellCaster * caster, const CStack * obj) const
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
const auto immuneResult = mechanics->isImmuneByStack(caster,obj);
|
2015-02-26 19:59:18 +02:00
|
|
|
|
|
|
|
if (ESpellCastProblem::NOT_DECIDED != immuneResult)
|
2015-02-02 10:25:26 +02:00
|
|
|
return immuneResult;
|
2015-02-26 19:59:18 +02:00
|
|
|
return ESpellCastProblem::OK;
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::setIsOffensive(const bool val)
|
|
|
|
{
|
|
|
|
isOffensive = val;
|
|
|
|
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
positiveness = CSpell::NEGATIVE;
|
|
|
|
isDamage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::setIsRising(const bool val)
|
|
|
|
{
|
|
|
|
isRising = val;
|
|
|
|
|
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
positiveness = CSpell::POSITIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::setup()
|
|
|
|
{
|
|
|
|
setupMechanics();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::setupMechanics()
|
|
|
|
{
|
2015-02-26 19:59:18 +02:00
|
|
|
mechanics = ISpellMechanics::createMechanics(this);
|
2016-09-04 07:19:28 +02:00
|
|
|
adventureMechanics = IAdventureSpellMechanics::createMechanics(this);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
2015-09-12 22:52:04 +02:00
|
|
|
///CSpell::AnimationInfo
|
|
|
|
CSpell::AnimationItem::AnimationItem()
|
|
|
|
:resourceName(""),verticalPosition(VerticalPosition::TOP),pause(0)
|
|
|
|
{
|
2016-02-13 18:41:28 +02:00
|
|
|
|
2015-09-12 22:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
///CSpell::AnimationInfo
|
|
|
|
CSpell::AnimationInfo::AnimationInfo()
|
|
|
|
{
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::AnimationInfo::~AnimationInfo()
|
|
|
|
{
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSpell::AnimationInfo::selectProjectile(const double angle) const
|
2015-02-26 19:59:18 +02:00
|
|
|
{
|
|
|
|
std::string res;
|
2015-02-02 10:25:26 +02:00
|
|
|
double maximum = 0.0;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
for(const auto & info : projectile)
|
|
|
|
{
|
|
|
|
if(info.minimumAngle < angle && info.minimumAngle > maximum)
|
|
|
|
{
|
|
|
|
maximum = info.minimumAngle;
|
|
|
|
res = info.resourceName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:06:02 +02:00
|
|
|
return res;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
///CSpell::TargetInfo
|
|
|
|
CSpell::TargetInfo::TargetInfo(const CSpell * spell, const int level)
|
|
|
|
{
|
|
|
|
init(spell, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::TargetInfo::TargetInfo(const CSpell * spell, const int level, ECastingMode::ECastingMode mode)
|
|
|
|
{
|
|
|
|
init(spell, level);
|
|
|
|
if(mode == ECastingMode::ENCHANTER_CASTING)
|
|
|
|
{
|
|
|
|
smart = true; //FIXME: not sure about that, this makes all spells smart in this mode
|
|
|
|
massive = true;
|
|
|
|
}
|
|
|
|
else if(mode == ECastingMode::SPELL_LIKE_ATTACK)
|
|
|
|
{
|
|
|
|
alwaysHitDirectly = true;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2016-09-10 20:07:36 +02:00
|
|
|
else if(mode == ECastingMode::CREATURE_ACTIVE_CASTING)
|
|
|
|
{
|
|
|
|
massive = false;//FIXME: find better solution for Commander spells
|
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::TargetInfo::init(const CSpell * spell, const int level)
|
|
|
|
{
|
|
|
|
auto & levelInfo = spell->getLevelInfo(level);
|
|
|
|
|
|
|
|
type = spell->getTargetType();
|
|
|
|
smart = levelInfo.smartTarget;
|
|
|
|
massive = levelInfo.range == "X";
|
|
|
|
onlyAlive = !spell->isRisingSpell();
|
|
|
|
alwaysHitDirectly = false;
|
|
|
|
clearAffected = levelInfo.clearAffected;
|
|
|
|
clearTarget = levelInfo.clearTarget;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
bool DLL_LINKAGE isInScreenRange(const int3 & center, const int3 & pos)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
int3 diff = pos - center;
|
|
|
|
if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
///CSpellHandler
|
|
|
|
CSpellHandler::CSpellHandler()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<JsonNode> CSpellHandler::loadLegacyData(size_t dataSize)
|
|
|
|
{
|
|
|
|
using namespace SpellConfig;
|
|
|
|
std::vector<JsonNode> legacyData;
|
|
|
|
|
|
|
|
CLegacyConfigParser parser("DATA/SPTRAITS.TXT");
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
auto readSchool = [&](JsonMap & schools, const std::string & name)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
if (parser.readString() == "x")
|
|
|
|
{
|
|
|
|
schools[name].Bool() = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto read = [&,this](bool combat, bool ability)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2017-11-26 23:18:18 +02:00
|
|
|
JsonNode lineNode(JsonNode::JsonType::DATA_STRUCT);
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
const si32 id = legacyData.size();
|
|
|
|
|
|
|
|
lineNode["index"].Float() = id;
|
|
|
|
lineNode["type"].String() = ability ? "ability" : (combat ? "combat" : "adventure");
|
|
|
|
|
|
|
|
lineNode["name"].String() = parser.readString();
|
|
|
|
|
|
|
|
parser.readString(); //ignored unused abbreviated name
|
|
|
|
lineNode["level"].Float() = parser.readNumber();
|
|
|
|
|
|
|
|
auto& schools = lineNode["school"].Struct();
|
|
|
|
|
|
|
|
readSchool(schools, "earth");
|
|
|
|
readSchool(schools, "water");
|
|
|
|
readSchool(schools, "fire");
|
|
|
|
readSchool(schools, "air");
|
|
|
|
|
|
|
|
auto& levels = lineNode["levels"].Struct();
|
|
|
|
|
|
|
|
auto getLevel = [&](const size_t idx)->JsonMap&
|
|
|
|
{
|
|
|
|
assert(idx < GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
return levels[LEVEL_NAMES[idx]].Struct();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto costs = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
lineNode["power"].Float() = parser.readNumber();
|
|
|
|
auto powers = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
|
|
|
|
auto& chances = lineNode["gainChance"].Struct();
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
for(size_t i = 0; i < GameConstants::F_NUMBER; i++){
|
2015-02-02 10:25:26 +02:00
|
|
|
chances[ETownType::names[i]].Float() = parser.readNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto AIVals = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
|
|
|
|
std::vector<std::string> descriptions;
|
2015-02-26 19:59:18 +02:00
|
|
|
for(size_t i = 0; i < GameConstants::SPELL_SCHOOL_LEVELS; i++)
|
2015-02-02 10:25:26 +02:00
|
|
|
descriptions.push_back(parser.readString());
|
|
|
|
|
|
|
|
parser.readString(); //ignore attributes. All data present in JSON
|
|
|
|
|
|
|
|
//save parsed level specific data
|
|
|
|
for(size_t i = 0; i < GameConstants::SPELL_SCHOOL_LEVELS; i++)
|
|
|
|
{
|
|
|
|
auto& level = getLevel(i);
|
|
|
|
level["description"].String() = descriptions[i];
|
|
|
|
level["cost"].Float() = costs[i];
|
|
|
|
level["power"].Float() = powers[i];
|
|
|
|
level["aiValue"].Float() = AIVals[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
legacyData.push_back(lineNode);
|
|
|
|
}
|
|
|
|
while (parser.endLine() && !parser.isNextEntryEmpty());
|
|
|
|
};
|
|
|
|
|
|
|
|
auto skip = [&](int cnt)
|
|
|
|
{
|
|
|
|
for(int i=0; i<cnt; i++)
|
|
|
|
parser.endLine();
|
|
|
|
};
|
|
|
|
|
|
|
|
skip(5);// header
|
|
|
|
read(false,false); //read adventure map spells
|
|
|
|
skip(3);
|
|
|
|
read(true,false); //read battle spells
|
|
|
|
skip(3);
|
|
|
|
read(true,true);//read creature abilities
|
|
|
|
|
|
|
|
//TODO: maybe move to config
|
|
|
|
//clone Acid Breath attributes for Acid Breath damage effect
|
|
|
|
JsonNode temp = legacyData[SpellID::ACID_BREATH_DEFENSE];
|
|
|
|
temp["index"].Float() = SpellID::ACID_BREATH_DAMAGE;
|
|
|
|
legacyData.push_back(temp);
|
|
|
|
|
|
|
|
objects.resize(legacyData.size());
|
|
|
|
|
|
|
|
return legacyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string CSpellHandler::getTypeName() const
|
|
|
|
{
|
|
|
|
return "spell";
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:55:45 +02:00
|
|
|
CSpell * CSpellHandler::loadFromJson(const JsonNode & json, const std::string & identifier)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
using namespace SpellConfig;
|
|
|
|
|
|
|
|
CSpell * spell = new CSpell();
|
2016-02-13 18:41:28 +02:00
|
|
|
spell->identifier = identifier;
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
const auto type = json["type"].String();
|
|
|
|
|
|
|
|
if(type == "ability")
|
|
|
|
{
|
|
|
|
spell->creatureAbility = true;
|
|
|
|
spell->combatSpell = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spell->creatureAbility = false;
|
|
|
|
spell->combatSpell = type == "combat";
|
|
|
|
}
|
|
|
|
|
|
|
|
spell->name = json["name"].String();
|
|
|
|
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->trace("%s: loading spell %s", __FUNCTION__, spell->name);
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
const auto schoolNames = json["school"];
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
for(const SpellSchoolInfo & info : SpellConfig::SCHOOL)
|
|
|
|
{
|
|
|
|
spell->school[info.id] = schoolNames[info.jsonName].Bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
spell->level = json["level"].Float();
|
|
|
|
spell->power = json["power"].Float();
|
|
|
|
|
|
|
|
spell->defaultProbability = json["defaultGainChance"].Float();
|
|
|
|
|
|
|
|
for(const auto & node : json["gainChance"].Struct())
|
|
|
|
{
|
|
|
|
const int chance = node.second.Float();
|
|
|
|
|
|
|
|
VLC->modh->identifiers.requestIdentifier(node.second.meta, "faction",node.first, [=](si32 factionID)
|
|
|
|
{
|
|
|
|
spell->probabilities[factionID] = chance;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
auto targetType = json["targetType"].String();
|
|
|
|
|
|
|
|
if(targetType == "NO_TARGET")
|
|
|
|
spell->targetType = CSpell::NO_TARGET;
|
|
|
|
else if(targetType == "CREATURE")
|
|
|
|
spell->targetType = CSpell::CREATURE;
|
|
|
|
else if(targetType == "OBSTACLE")
|
|
|
|
spell->targetType = CSpell::OBSTACLE;
|
|
|
|
else if(targetType == "LOCATION")
|
|
|
|
spell->targetType = CSpell::LOCATION;
|
2015-02-26 19:59:18 +02:00
|
|
|
else
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->warn("Spell %s: target type %s - assumed NO_TARGET.", spell->name, (targetType.empty() ? "empty" : "unknown ("+targetType+")"));
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
for(const auto & counteredSpell: json["counters"].Struct())
|
|
|
|
if (counteredSpell.second.Bool())
|
|
|
|
{
|
|
|
|
VLC->modh->identifiers.requestIdentifier(json.meta, counteredSpell.first, [=](si32 id)
|
|
|
|
{
|
|
|
|
spell->counteredSpells.push_back(SpellID(id));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: more error checking - f.e. conflicting flags
|
|
|
|
const auto flags = json["flags"];
|
|
|
|
|
|
|
|
//by default all flags are set to false in constructor
|
|
|
|
|
|
|
|
spell->isDamage = flags["damage"].Bool(); //do this before "offensive"
|
|
|
|
|
|
|
|
if(flags["offensive"].Bool())
|
|
|
|
{
|
|
|
|
spell->setIsOffensive(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(flags["rising"].Bool())
|
|
|
|
{
|
|
|
|
spell->setIsRising(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool implicitPositiveness = spell->isOffensive || spell->isRising; //(!) "damage" does not mean NEGATIVE --AVS
|
|
|
|
|
|
|
|
if(flags["indifferent"].Bool())
|
|
|
|
{
|
|
|
|
spell->positiveness = CSpell::NEUTRAL;
|
|
|
|
}
|
|
|
|
else if(flags["negative"].Bool())
|
|
|
|
{
|
|
|
|
spell->positiveness = CSpell::NEGATIVE;
|
|
|
|
}
|
|
|
|
else if(flags["positive"].Bool())
|
|
|
|
{
|
|
|
|
spell->positiveness = CSpell::POSITIVE;
|
|
|
|
}
|
|
|
|
else if(!implicitPositiveness)
|
|
|
|
{
|
|
|
|
spell->positiveness = CSpell::NEUTRAL; //duplicates constructor but, just in case
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Spell %s: no positiveness specified, assumed NEUTRAL.", spell->name);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
spell->isSpecial = flags["special"].Bool();
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
auto findBonus = [&](std::string name, std::vector<Bonus::BonusType> & vec)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
auto it = bonusNameMap.find(name);
|
|
|
|
if(it == bonusNameMap.end())
|
|
|
|
{
|
2017-08-30 23:23:19 +02:00
|
|
|
logMod->error("Spell %s: invalid bonus name %s", spell->name, name);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vec.push_back((Bonus::BonusType)it->second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
auto readBonusStruct = [&](std::string name, std::vector<Bonus::BonusType> & vec)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
for(auto bonusData: json[name].Struct())
|
|
|
|
{
|
|
|
|
const std::string bonusId = bonusData.first;
|
|
|
|
const bool flag = bonusData.second.Bool();
|
|
|
|
|
|
|
|
if(flag)
|
|
|
|
findBonus(bonusId, vec);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
readBonusStruct("immunity", spell->immunities);
|
|
|
|
readBonusStruct("absoluteImmunity", spell->absoluteImmunities);
|
2015-02-26 19:59:18 +02:00
|
|
|
readBonusStruct("limit", spell->limiters);
|
2015-02-02 10:25:26 +02:00
|
|
|
readBonusStruct("absoluteLimit", spell->absoluteLimiters);
|
|
|
|
|
|
|
|
const JsonNode & graphicsNode = json["graphics"];
|
|
|
|
|
|
|
|
spell->iconImmune = graphicsNode["iconImmune"].String();
|
|
|
|
spell->iconBook = graphicsNode["iconBook"].String();
|
|
|
|
spell->iconEffect = graphicsNode["iconEffect"].String();
|
|
|
|
spell->iconScenarioBonus = graphicsNode["iconScenarioBonus"].String();
|
|
|
|
spell->iconScroll = graphicsNode["iconScroll"].String();
|
|
|
|
|
|
|
|
const JsonNode & animationNode = json["animation"];
|
2015-02-26 19:59:18 +02:00
|
|
|
|
|
|
|
auto loadAnimationQueue = [&](const std::string & jsonName, CSpell::TAnimationQueue & q)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
2015-02-26 19:59:18 +02:00
|
|
|
auto queueNode = animationNode[jsonName].Vector();
|
2015-02-02 10:25:26 +02:00
|
|
|
for(const JsonNode & item : queueNode)
|
2015-02-26 19:59:18 +02:00
|
|
|
{
|
2015-02-02 10:25:26 +02:00
|
|
|
CSpell::TAnimation newItem;
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2017-11-26 23:18:18 +02:00
|
|
|
if(item.getType() == JsonNode::JsonType::DATA_STRING)
|
2015-02-02 10:25:26 +02:00
|
|
|
newItem.resourceName = item.String();
|
2017-11-26 23:18:18 +02:00
|
|
|
else if(item.getType() == JsonNode::JsonType::DATA_STRUCT)
|
2015-02-02 10:25:26 +02:00
|
|
|
{
|
|
|
|
newItem.resourceName = item["defName"].String();
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
auto vPosStr = item["verticalPosition"].String();
|
|
|
|
if("bottom" == vPosStr)
|
|
|
|
newItem.verticalPosition = VerticalPosition::BOTTOM;
|
2015-02-26 19:59:18 +02:00
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
else if(item.isNumber())
|
2015-03-22 12:49:14 +02:00
|
|
|
{
|
|
|
|
newItem.pause = item.Float();
|
|
|
|
}
|
2016-02-13 18:41:28 +02:00
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
q.push_back(newItem);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
};
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
loadAnimationQueue("affect", spell->animationInfo.affect);
|
|
|
|
loadAnimationQueue("cast", spell->animationInfo.cast);
|
2015-02-26 19:59:18 +02:00
|
|
|
loadAnimationQueue("hit", spell->animationInfo.hit);
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
const JsonVector & projectile = animationNode["projectile"].Vector();
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
for(const JsonNode & item : projectile)
|
|
|
|
{
|
|
|
|
CSpell::ProjectileInfo info;
|
|
|
|
info.resourceName = item["defName"].String();
|
|
|
|
info.minimumAngle = item["minimumAngle"].Float();
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
spell->animationInfo.projectile.push_back(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
const JsonNode & soundsNode = json["sounds"];
|
|
|
|
spell->castSound = soundsNode["cast"].String();
|
|
|
|
|
|
|
|
//load level attributes
|
|
|
|
const int levelsCount = GameConstants::SPELL_SCHOOL_LEVELS;
|
|
|
|
|
|
|
|
for(int levelIndex = 0; levelIndex < levelsCount; levelIndex++)
|
|
|
|
{
|
|
|
|
const JsonNode & levelNode = json["levels"][LEVEL_NAMES[levelIndex]];
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
CSpell::LevelInfo & levelObject = spell->levels[levelIndex];
|
|
|
|
|
2015-02-26 19:59:18 +02:00
|
|
|
const si32 levelPower = levelObject.power = levelNode["power"].Float();
|
2015-02-02 10:25:26 +02:00
|
|
|
|
|
|
|
levelObject.description = levelNode["description"].String();
|
|
|
|
levelObject.cost = levelNode["cost"].Float();
|
|
|
|
levelObject.AIValue = levelNode["aiValue"].Float();
|
|
|
|
levelObject.smartTarget = levelNode["targetModifier"]["smart"].Bool();
|
|
|
|
levelObject.clearTarget = levelNode["targetModifier"]["clearTarget"].Bool();
|
2015-02-26 19:59:18 +02:00
|
|
|
levelObject.clearAffected = levelNode["targetModifier"]["clearAffected"].Bool();
|
2015-02-02 10:25:26 +02:00
|
|
|
levelObject.range = levelNode["range"].String();
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
for(const auto & elem : levelNode["effects"].Struct())
|
|
|
|
{
|
|
|
|
const JsonNode & bonusNode = elem.second;
|
2016-09-19 23:36:35 +02:00
|
|
|
auto b = JsonUtils::parseBonus(bonusNode);
|
2015-02-02 10:25:26 +02:00
|
|
|
const bool usePowerAsValue = bonusNode["val"].isNull();
|
|
|
|
|
|
|
|
//TODO: make this work. see CSpellHandler::afterLoadFinalization()
|
|
|
|
//b->sid = spell->id; //for all
|
|
|
|
|
|
|
|
b->source = Bonus::SPELL_EFFECT;//for all
|
|
|
|
|
|
|
|
if(usePowerAsValue)
|
|
|
|
b->val = levelPower;
|
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
levelObject.effects.push_back(b);
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2016-11-02 19:11:01 +02:00
|
|
|
for(const auto & elem : levelNode["cumulativeEffects"].Struct())
|
|
|
|
{
|
|
|
|
const JsonNode & bonusNode = elem.second;
|
|
|
|
auto b = JsonUtils::parseBonus(bonusNode);
|
|
|
|
const bool usePowerAsValue = bonusNode["val"].isNull();
|
|
|
|
|
|
|
|
//TODO: make this work. see CSpellHandler::afterLoadFinalization()
|
|
|
|
//b->sid = spell->id; //for all
|
|
|
|
|
|
|
|
b->source = Bonus::SPELL_EFFECT;//for all
|
|
|
|
|
|
|
|
if(usePowerAsValue)
|
|
|
|
b->val = levelPower;
|
|
|
|
|
|
|
|
levelObject.cumulativeEffects.push_back(b);
|
|
|
|
}
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return spell;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpellHandler::afterLoadFinalization()
|
|
|
|
{
|
|
|
|
//FIXME: it is a bad place for this code, should refactor loadFromJson to know object id during loading
|
|
|
|
for(auto spell: objects)
|
|
|
|
{
|
|
|
|
for(auto & level: spell->levels)
|
2015-04-11 12:14:26 +02:00
|
|
|
{
|
2015-02-02 10:25:26 +02:00
|
|
|
for(auto & bonus: level.effects)
|
2016-11-02 19:11:01 +02:00
|
|
|
bonus->sid = spell->id;
|
|
|
|
for(auto & bonus: level.cumulativeEffects)
|
|
|
|
bonus->sid = spell->id;
|
2015-04-11 12:14:26 +02:00
|
|
|
}
|
2016-02-13 18:41:28 +02:00
|
|
|
spell->setup();
|
2015-02-02 10:25:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSpellHandler::beforeValidate(JsonNode & object)
|
|
|
|
{
|
|
|
|
//handle "base" level info
|
2015-02-26 19:59:18 +02:00
|
|
|
|
|
|
|
JsonNode & levels = object["levels"];
|
|
|
|
JsonNode & base = levels["base"];
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
auto inheritNode = [&](const std::string & name){
|
|
|
|
JsonUtils::inherit(levels[name],base);
|
|
|
|
};
|
2015-02-26 19:59:18 +02:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
inheritNode("none");
|
|
|
|
inheritNode("basic");
|
|
|
|
inheritNode("advanced");
|
|
|
|
inheritNode("expert");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CSpellHandler::~CSpellHandler()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<bool> CSpellHandler::getDefaultAllowed() const
|
|
|
|
{
|
|
|
|
std::vector<bool> allowedSpells;
|
2016-02-21 19:58:09 +02:00
|
|
|
allowedSpells.reserve(objects.size());
|
|
|
|
|
|
|
|
for(const CSpell * s : objects)
|
|
|
|
{
|
|
|
|
allowedSpells.push_back( !(s->isSpecialSpell() || s->isCreatureAbility()));
|
|
|
|
}
|
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
return allowedSpells;
|
|
|
|
}
|
2016-02-21 19:58:09 +02:00
|
|
|
|
|
|
|
si32 CSpellHandler::decodeSpell(const std::string& identifier)
|
|
|
|
{
|
|
|
|
auto rawId = VLC->modh->identifiers.getIdentifier("core", "spell", identifier);
|
|
|
|
if(rawId)
|
|
|
|
return rawId.get();
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSpellHandler::encodeSpell(const si32 index)
|
|
|
|
{
|
|
|
|
return VLC->spellh->objects[index]->identifier;
|
|
|
|
}
|
|
|
|
|