2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-06-09 16:28:03 +03:00
|
|
|
#include "CSpellHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
#include "CGeneralTextHandler.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "filesystem/Filesystem.h"
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
#include "JsonNode.h"
|
2009-04-14 17:48:57 +03:00
|
|
|
#include <cctype>
|
2012-05-18 23:50:16 +03:00
|
|
|
#include "BattleHex.h"
|
2012-12-18 13:32:11 +03:00
|
|
|
#include "CModHandler.h"
|
2014-03-07 16:21:09 +03:00
|
|
|
#include "StringConstants.h"
|
2009-04-14 17:48:57 +03:00
|
|
|
|
2014-11-12 05:59:53 +02:00
|
|
|
#include "mapObjects/CGHeroInstance.h"
|
|
|
|
#include "BattleState.h"
|
2014-11-13 03:53:25 +02:00
|
|
|
#include "CBattleCallback.h"
|
2014-11-12 05:59:53 +02:00
|
|
|
|
2014-11-12 10:36:34 +02:00
|
|
|
#include "SpellMechanics.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-04-15 17:03:31 +03: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
|
|
|
|
*
|
|
|
|
*/
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
namespace SpellConfig
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
static const std::string LEVEL_NAMES[] = {"none", "basic", "advanced", "expert"};
|
2014-11-24 18:14:10 +02:00
|
|
|
|
|
|
|
static const SpellSchoolInfo SCHOOL[4] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
|
2014-11-12 05:59:53 +02:00
|
|
|
///CSpell::LevelInfo
|
2014-03-17 16:11:10 +03:00
|
|
|
CSpell::LevelInfo::LevelInfo()
|
2014-11-13 12:37:42 +02:00
|
|
|
:description(""),cost(0),power(0),AIValue(0),smartTarget(true), clearTarget(false), clearAffected(false), range("0")
|
2014-03-17 16:11:10 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CSpell::LevelInfo::~LevelInfo()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-12 05:59:53 +02:00
|
|
|
///CSpell
|
2014-03-07 16:21:09 +03:00
|
|
|
CSpell::CSpell():
|
2014-03-09 02:18:51 +03:00
|
|
|
id(SpellID::NONE), level(0),
|
|
|
|
earth(false), water(false), fire(false), air(false),
|
|
|
|
combatSpell(false), creatureAbility(false),
|
|
|
|
positiveness(ESpellPositiveness::NEUTRAL),
|
|
|
|
mainEffectAnim(-1),
|
|
|
|
defaultProbability(0),
|
|
|
|
isRising(false), isDamage(false), isOffensive(false),
|
2014-11-12 05:59:53 +02:00
|
|
|
targetType(ETargetType::NO_TARGET),
|
|
|
|
mechanics(nullptr)
|
2013-01-15 17:20:48 +03:00
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
levels.resize(GameConstants::SPELL_SCHOOL_LEVELS);
|
2013-01-15 17:20:48 +03:00
|
|
|
}
|
|
|
|
|
2013-04-21 19:38:31 +03:00
|
|
|
CSpell::~CSpell()
|
|
|
|
{
|
2014-11-12 05:59:53 +02:00
|
|
|
delete mechanics;
|
2013-04-21 19:38:31 +03:00
|
|
|
}
|
|
|
|
|
2014-11-13 16:57:35 +02:00
|
|
|
bool CSpell::isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const
|
|
|
|
{
|
|
|
|
if(!hasSpellBook)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const bool inSpellBook = vstd::contains(spellBook, id);
|
|
|
|
const bool isBonus = caster->hasBonusOfType(Bonus::SPELL, id);
|
|
|
|
|
|
|
|
bool inTome = false;
|
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
2014-11-13 16:57:35 +02:00
|
|
|
{
|
|
|
|
if(school.at(cnf.id) && caster->hasBonusOfType(cnf.knoledgeBonus))
|
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
inTome = stop = true;
|
2014-11-13 16:57:35 +02:00
|
|
|
}
|
2014-11-24 18:14:10 +02:00
|
|
|
});
|
2014-11-13 16:57:35 +02:00
|
|
|
|
|
|
|
if (isSpecialSpell())
|
|
|
|
{
|
|
|
|
if (inSpellBook)
|
|
|
|
{//hero has this spell in spellbook
|
|
|
|
logGlobal->errorStream() << "Special spell in spellbook "<<name;
|
|
|
|
}
|
|
|
|
return isBonus;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return inSpellBook || inTome || isBonus || caster->hasBonusOfType(Bonus::SPELLS_OF_LEVEL, level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
const CSpell::LevelInfo & CSpell::getLevelInfo(const int level) const
|
|
|
|
{
|
|
|
|
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
logGlobal->errorStream() << __FUNCTION__ << " invalid school level " << level;
|
|
|
|
throw new std::runtime_error("Invalid school level");
|
2014-03-17 16:11:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return levels.at(level);
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:02:27 +02:00
|
|
|
ui32 CSpell::calculateBonus(ui32 baseDamage, const CGHeroInstance* caster, const CStack* affectedCreature) const
|
|
|
|
{
|
|
|
|
ui32 ret = baseDamage;
|
|
|
|
//applying sorcery secondary skill
|
|
|
|
if(caster)
|
|
|
|
{
|
|
|
|
ret *= (100.0 + caster->valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, SecondarySkill::SORCERY)) / 100.0;
|
|
|
|
ret *= (100.0 + caster->valOfBonuses(Bonus::SPELL_DAMAGE) + caster->valOfBonuses(Bonus::SPECIFIC_SPELL_DAMAGE, id.toEnum())) / 100.0;
|
2014-11-12 10:03:55 +02:00
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
2014-11-12 10:03:55 +02:00
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
ret *= (100.0 + caster->valOfBonuses(cnf.damagePremyBonus)) / 100.0;
|
|
|
|
stop = true; //only bonus from one school is used
|
|
|
|
});
|
2014-11-12 07:02:27 +02:00
|
|
|
|
|
|
|
if (affectedCreature && affectedCreature->getCreature()->level) //Hero specials like Solmyr, Deemer
|
|
|
|
ret *= (100. + ((caster->valOfBonuses(Bonus::SPECIAL_SPELL_LEV, id.toEnum()) * caster->level) / affectedCreature->getCreature()->level)) / 100.0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-12 08:52:11 +02:00
|
|
|
ui32 CSpell::calculateDamage(const CGHeroInstance * caster, const CStack * affectedCreature, int spellSchoolLevel, int usedSpellPower) const
|
|
|
|
{
|
|
|
|
ui32 ret = 0; //value to return
|
|
|
|
|
|
|
|
//check if spell really does damage - if not, return 0
|
|
|
|
if(!isDamageSpell())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = usedSpellPower * power;
|
|
|
|
ret += getPower(spellSchoolLevel);
|
|
|
|
|
|
|
|
//affected creature-specific part
|
|
|
|
if(nullptr != affectedCreature)
|
|
|
|
{
|
|
|
|
//applying protections - when spell has more then one elements, only one protection should be applied (I think)
|
2014-11-12 10:03:55 +02:00
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
2014-11-12 08:52:11 +02:00
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
if(affectedCreature->hasBonusOfType(Bonus::SPELL_DAMAGE_REDUCTION, (ui8)cnf.id))
|
2014-11-12 10:03:55 +02:00
|
|
|
{
|
|
|
|
ret *= affectedCreature->valOfBonuses(Bonus::SPELL_DAMAGE_REDUCTION, (ui8)cnf.id);
|
|
|
|
ret /= 100;
|
2014-11-24 18:14:10 +02:00
|
|
|
stop = true;//only bonus from one school is used
|
2014-11-12 10:03:55 +02:00
|
|
|
}
|
2014-11-24 18:14:10 +02:00
|
|
|
});
|
|
|
|
|
2014-11-12 08:52:11 +02:00
|
|
|
//general spell dmg reduction
|
2014-11-13 14:07:39 +02:00
|
|
|
if(affectedCreature->hasBonusOfType(Bonus::SPELL_DAMAGE_REDUCTION, -1))
|
2014-11-12 08:52:11 +02:00
|
|
|
{
|
|
|
|
ret *= affectedCreature->valOfBonuses(Bonus::SPELL_DAMAGE_REDUCTION, -1);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = calculateBonus(ret, caster, affectedCreature);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-12 07:34:43 +02:00
|
|
|
ui32 CSpell::calculateHealedHP(const CGHeroInstance* caster, const CStack* stack, const CStack* sacrificedStack) const
|
|
|
|
{
|
|
|
|
//todo: use Mechanics class
|
|
|
|
int healedHealth;
|
|
|
|
|
|
|
|
if(!isHealingSpell())
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << "calculateHealedHP called for nonhealing spell "<< name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int spellPowerSkill = caster->getPrimSkillLevel(PrimarySkill::SPELL_POWER);
|
|
|
|
const int levelPower = getPower(caster->getSpellSchoolLevel(this));
|
|
|
|
|
|
|
|
if (id == SpellID::SACRIFICE && sacrificedStack)
|
|
|
|
healedHealth = (spellPowerSkill + sacrificedStack->MaxHealth() + levelPower) * sacrificedStack->count;
|
|
|
|
else
|
2014-11-12 08:20:20 +02:00
|
|
|
healedHealth = spellPowerSkill * power + levelPower; //???
|
2014-11-12 07:34:43 +02:00
|
|
|
healedHealth = calculateBonus(healedHealth, caster, stack);
|
|
|
|
return std::min<ui32>(healedHealth, stack->MaxHealth() - stack->firstHPleft + (isRisingSpell() ? stack->baseAmount * stack->MaxHealth() : 0));
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2012-05-18 23:50:16 +03:00
|
|
|
std::vector<BattleHex> CSpell::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes) const
|
2009-04-14 17:48:57 +03:00
|
|
|
{
|
2014-11-13 06:34:20 +02:00
|
|
|
return mechanics->rangeInHexes(centralHex,schoolLvl,side,outDroppedHexes);
|
2009-04-14 17:48:57 +03:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:53:25 +02:00
|
|
|
std::set<const CStack* > CSpell::getAffectedStacks(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, PlayerColor casterColor, int spellLvl, BattleHex destination, const CGHeroInstance * caster) const
|
|
|
|
{
|
2014-11-13 11:22:20 +02:00
|
|
|
ISpellMechanics::SpellTargetingContext ctx(this, cb,mode,casterColor,spellLvl,destination);
|
2014-11-13 03:53:25 +02:00
|
|
|
|
2014-11-13 11:22:20 +02:00
|
|
|
std::set<const CStack* > attackedCres = mechanics->getAffectedStacks(ctx);
|
2014-11-13 03:53:25 +02:00
|
|
|
|
|
|
|
//now handle immunities
|
|
|
|
auto predicate = [&, this](const CStack * s)->bool
|
|
|
|
{
|
2014-11-13 11:22:20 +02:00
|
|
|
bool hitDirectly = ctx.ti.alwaysHitDirectly && s->coversPos(destination);
|
2014-11-13 03:53:25 +02:00
|
|
|
bool notImmune = (ESpellCastProblem::OK == isImmuneByStack(caster, s));
|
|
|
|
|
|
|
|
return !(hitDirectly || notImmune);
|
2014-11-13 11:22:20 +02:00
|
|
|
};
|
2014-11-13 03:53:25 +02:00
|
|
|
vstd::erase_if(attackedCres, predicate);
|
|
|
|
|
|
|
|
return attackedCres;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
CSpell::ETargetType CSpell::getTargetType() const
|
2011-02-21 18:53:23 +02:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
return targetType;
|
2011-02-21 18:53:23 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:53:25 +02:00
|
|
|
CSpell::TargetInfo CSpell::getTargetInfo(const int level) const
|
2014-03-17 16:11:10 +03:00
|
|
|
{
|
2014-11-13 05:29:41 +02:00
|
|
|
TargetInfo info(this, level);
|
2014-05-01 01:48:33 +03:00
|
|
|
return info;
|
2014-03-17 16:11:10 +03:00
|
|
|
}
|
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
void CSpell::forEachSchool(const std::function<void(const SpellSchoolInfo &, bool &)>& cb) const
|
|
|
|
{
|
|
|
|
bool stop = false;
|
|
|
|
for(const SpellSchoolInfo & cnf : SpellConfig::SCHOOL)
|
|
|
|
{
|
|
|
|
if(school.at(cnf.id))
|
|
|
|
{
|
|
|
|
cb(cnf, stop);
|
|
|
|
|
|
|
|
if(stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-09 02:18:51 +03: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;
|
|
|
|
}
|
|
|
|
|
2014-05-17 13:36:49 +03:00
|
|
|
bool CSpell::isNeutral() const
|
|
|
|
{
|
|
|
|
return positiveness == NEUTRAL;
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:34:43 +02:00
|
|
|
bool CSpell::isHealingSpell() const
|
|
|
|
{
|
|
|
|
return isRisingSpell() || (id == SpellID::CURE);
|
|
|
|
}
|
2014-05-17 13:36:49 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
bool CSpell::isRisingSpell() const
|
|
|
|
{
|
|
|
|
return isRising;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isDamageSpell() const
|
|
|
|
{
|
|
|
|
return isDamage;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isOffensiveSpell() const
|
|
|
|
{
|
|
|
|
return isOffensive;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::isSpecialSpell() const
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
return isSpecial;
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSpell::hasEffects() const
|
|
|
|
{
|
2014-03-17 16:11:10 +03:00
|
|
|
return !levels[0].effects.empty();
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& CSpell::getIconImmune() const
|
|
|
|
{
|
|
|
|
return iconImmune;
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:00:58 +03:00
|
|
|
const std::string& CSpell::getCastSound() const
|
|
|
|
{
|
|
|
|
return castSound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
si32 CSpell::getCost(const int skillLevel) const
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
return getLevelInfo(skillLevel).cost;
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si32 CSpell::getPower(const int skillLevel) const
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
return getLevelInfo(skillLevel).power;
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//si32 CSpell::calculatePower(const int skillLevel) const
|
|
|
|
//{
|
|
|
|
// return power + getPower(skillLevel);
|
|
|
|
//}
|
|
|
|
|
|
|
|
si32 CSpell::getProbability(const TFaction factionId) const
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
if(!vstd::contains(probabilities,factionId))
|
|
|
|
{
|
|
|
|
return defaultProbability;
|
|
|
|
}
|
|
|
|
return probabilities.at(factionId);
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
|
|
|
|
void CSpell::getEffects(std::vector<Bonus>& lst, const int level) const
|
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)
|
2013-01-15 17:20:48 +03:00
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
logGlobal->errorStream() << __FUNCTION__ << " invalid school level " << level;
|
2013-01-16 14:19:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
const std::vector<Bonus> & effects = levels[level].effects;
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
if(effects.empty())
|
2014-05-01 01:48:33 +03:00
|
|
|
{
|
2014-03-07 16:21:09 +03:00
|
|
|
logGlobal->errorStream() << __FUNCTION__ << " This spell (" + name + ") has no effects for level " << level;
|
|
|
|
return;
|
2014-05-01 01:48:33 +03:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
lst.reserve(lst.size() + effects.size());
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
for(const Bonus & b : effects)
|
2013-01-16 14:19:04 +03:00
|
|
|
{
|
2014-03-17 16:11:10 +03:00
|
|
|
lst.push_back(Bonus(b));
|
2013-01-15 17:20:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:50:51 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::isImmuneBy(const IBonusBearer* obj) const
|
2014-11-13 04:40:52 +02:00
|
|
|
{
|
2013-02-13 22:35:43 +03:00
|
|
|
//todo: use new bonus API
|
2014-05-18 16:55:26 +03:00
|
|
|
//1. Check absolute limiters
|
|
|
|
for(auto b : absoluteLimiters)
|
2013-01-16 14:19:04 +03:00
|
|
|
{
|
|
|
|
if (!obj->hasBonusOfType(b))
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2013-01-16 14:19:04 +03:00
|
|
|
}
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
//2. Check absolute immunities
|
2014-05-01 01:48:33 +03:00
|
|
|
for(auto b : absoluteImmunities)
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
|
|
|
if (obj->hasBonusOfType(b))
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
2014-11-13 04:40:52 +02:00
|
|
|
|
|
|
|
//check receptivity
|
|
|
|
if (isPositive() && obj->hasBonusOfType(Bonus::RECEPTIVE)) //accept all positive spells
|
|
|
|
return ESpellCastProblem::OK;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
//3. Check negation
|
2014-05-18 16:55:26 +03:00
|
|
|
//FIXME: Orb of vulnerability mechanics is not such trivial
|
2014-03-09 02:18:51 +03:00
|
|
|
if(obj->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES)) //Orb of vulnerability
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::NOT_DECIDED;
|
2014-05-18 16:55:26 +03:00
|
|
|
|
|
|
|
//4. Check negatable limit
|
|
|
|
for(auto b : limiters)
|
|
|
|
{
|
|
|
|
if (!obj->hasBonusOfType(b))
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2014-05-18 16:55:26 +03:00
|
|
|
}
|
|
|
|
|
2013-07-28 13:05:37 +03:00
|
|
|
|
2014-05-18 16:55:26 +03:00
|
|
|
//5. Check negatable immunities
|
2013-06-29 16:05:48 +03:00
|
|
|
for(auto b : immunities)
|
2013-01-16 14:19:04 +03:00
|
|
|
{
|
|
|
|
if (obj->hasBonusOfType(b))
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2013-01-16 14:19:04 +03:00
|
|
|
}
|
|
|
|
|
2014-05-18 16:55:26 +03:00
|
|
|
//6. Check elemental immunities
|
2014-11-12 10:03:55 +02:00
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem tmp = ESpellCastProblem::NOT_DECIDED;
|
|
|
|
|
|
|
|
forEachSchool([&](const SpellSchoolInfo & cnf, bool & stop)
|
2013-01-16 14:19:04 +03:00
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
auto element = cnf.immunityBonus;
|
|
|
|
|
|
|
|
if(obj->hasBonusOfType(element, 0)) //always resist if immune to all spells altogether
|
2014-11-13 13:10:25 +02:00
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
tmp = ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
else if(!isPositive()) //negative or indifferent
|
|
|
|
{
|
|
|
|
if((isDamageSpell() && obj->hasBonusOfType(element, 2)) || obj->hasBonusOfType(element, 1))
|
2014-11-13 13:10:25 +02:00
|
|
|
{
|
2014-11-24 18:14:10 +02:00
|
|
|
tmp = ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(tmp != ESpellCastProblem::NOT_DECIDED)
|
|
|
|
return tmp;
|
|
|
|
|
2013-02-13 22:35:43 +03:00
|
|
|
TBonusListPtr levelImmunities = obj->getBonuses(Selector::type(Bonus::LEVEL_SPELL_IMMUNITY));
|
2013-01-16 14:19:04 +03:00
|
|
|
|
|
|
|
if(obj->hasBonusOfType(Bonus::SPELL_IMMUNITY, id)
|
2013-02-13 22:35:43 +03:00
|
|
|
|| ( levelImmunities->size() > 0 && levelImmunities->totalValue() >= level && level))
|
2013-01-16 14:19:04 +03:00
|
|
|
{
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::STACK_IMMUNE_TO_SPELL;
|
2013-01-16 14:19:04 +03:00
|
|
|
}
|
|
|
|
|
2014-11-12 04:50:51 +02:00
|
|
|
return ESpellCastProblem::NOT_DECIDED;
|
2013-01-16 14:19:04 +03:00
|
|
|
}
|
|
|
|
|
2014-11-13 03:53:25 +02:00
|
|
|
ESpellCastProblem::ESpellCastProblem CSpell::isImmuneByStack(const CGHeroInstance* caster, const CStack* obj) const
|
2014-11-12 05:59:53 +02:00
|
|
|
{
|
2014-11-13 03:53:25 +02:00
|
|
|
const auto immuneResult = mechanics->isImmuneByStack(caster,obj);
|
2014-11-12 08:20:20 +02:00
|
|
|
|
|
|
|
if (ESpellCastProblem::NOT_DECIDED != immuneResult)
|
|
|
|
return immuneResult;
|
|
|
|
return ESpellCastProblem::OK;
|
2014-11-12 05:59:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
void CSpell::setIsOffensive(const bool val)
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
isOffensive = val;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
positiveness = CSpell::NEGATIVE;
|
|
|
|
isDamage = true;
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSpell::setIsRising(const bool val)
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
isRising = val;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
if(val)
|
|
|
|
{
|
|
|
|
positiveness = CSpell::POSITIVE;
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
|
2014-11-12 10:03:55 +02:00
|
|
|
void CSpell::setup()
|
|
|
|
{
|
|
|
|
setupMechanics();
|
|
|
|
|
2014-11-13 15:57:49 +02:00
|
|
|
air = school[ESpellSchool::AIR];
|
|
|
|
fire = school[ESpellSchool::FIRE];
|
|
|
|
water = school[ESpellSchool::WATER];
|
|
|
|
earth = school[ESpellSchool::EARTH];
|
2014-11-12 10:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-12 05:59:53 +02:00
|
|
|
void CSpell::setupMechanics()
|
|
|
|
{
|
|
|
|
if(nullptr != mechanics)
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << "Spell " << this->name << " mechanics already set";
|
|
|
|
delete mechanics;
|
|
|
|
mechanics = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case SpellID::CLONE:
|
2014-11-13 03:53:25 +02:00
|
|
|
mechanics = new CloneMechanics(this);
|
2014-11-12 05:59:53 +02:00
|
|
|
break;
|
|
|
|
case SpellID::DISPEL_HELPFUL_SPELLS:
|
|
|
|
mechanics = new DispellHelpfulMechanics(this);
|
2014-11-12 07:34:43 +02:00
|
|
|
break;
|
|
|
|
case SpellID::SACRIFICE:
|
|
|
|
mechanics = new SacrificeMechanics(this);
|
2014-11-12 08:20:20 +02:00
|
|
|
break;
|
2014-11-13 04:40:52 +02:00
|
|
|
case SpellID::CHAIN_LIGHTNING:
|
|
|
|
mechanics = new ChainLightningMechanics(this);
|
2014-11-13 06:34:20 +02:00
|
|
|
break;
|
|
|
|
case SpellID::FIRE_WALL:
|
|
|
|
case SpellID::FORCE_FIELD:
|
|
|
|
mechanics = new WallMechanics(this);
|
|
|
|
break;
|
2014-11-12 07:34:43 +02:00
|
|
|
default:
|
|
|
|
if(isRisingSpell())
|
|
|
|
mechanics = new SpecialRisingSpellMechanics(this);
|
|
|
|
else
|
2014-11-12 10:36:34 +02:00
|
|
|
mechanics = new DefaultSpellMechanics(this);
|
2014-11-12 05:59:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-13 05:29:41 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2014-11-13 14:00:45 +02:00
|
|
|
alwaysHitDirectly = false;
|
|
|
|
clearAffected = levelInfo.clearAffected;
|
|
|
|
clearTarget = levelInfo.clearTarget;
|
2014-11-13 05:29:41 +02:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2013-01-15 17:20:48 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
bool DLL_LINKAGE isInScreenRange(const int3 ¢er, const int3 &pos)
|
2010-03-21 00:17:19 +02:00
|
|
|
{
|
|
|
|
int3 diff = pos - center;
|
|
|
|
if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-12 05:59:53 +02:00
|
|
|
///CSpellHandler
|
2014-03-07 16:21:09 +03:00
|
|
|
CSpellHandler::CSpellHandler()
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
std::vector<JsonNode> CSpellHandler::loadLegacyData(size_t dataSize)
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
using namespace SpellConfig;
|
|
|
|
std::vector<JsonNode> legacyData;
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
CLegacyConfigParser parser("DATA/SPTRAITS.TXT");
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
auto readSchool = [&](JsonMap& schools, const std::string& name)
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
if (parser.readString() == "x")
|
|
|
|
{
|
|
|
|
schools[name].Bool() = true;
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
};
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
auto read = [&,this](bool combat, bool ability)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
JsonNode lineNode(JsonNode::DATA_STRUCT);
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
const si32 id = legacyData.size();
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
lineNode["index"].Float() = id;
|
|
|
|
lineNode["type"].String() = ability ? "ability" : (combat ? "combat" : "adventure");
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
lineNode["name"].String() = parser.readString();
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
parser.readString(); //ignored unused abbreviated name
|
|
|
|
lineNode["level"].Float() = parser.readNumber();
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto& schools = lineNode["school"].Struct();
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
readSchool(schools, "earth");
|
|
|
|
readSchool(schools, "water");
|
|
|
|
readSchool(schools, "fire");
|
|
|
|
readSchool(schools, "air");
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto& levels = lineNode["levels"].Struct();
|
2013-02-13 22:35:43 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto getLevel = [&](const size_t idx)->JsonMap&
|
|
|
|
{
|
|
|
|
assert(idx < GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
return levels[LEVEL_NAMES[idx]].Struct();
|
|
|
|
};
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto costs = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
|
|
|
lineNode["power"].Float() = parser.readNumber();
|
|
|
|
auto powers = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto& chances = lineNode["gainChance"].Struct();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
for(size_t i = 0; i < GameConstants::F_NUMBER ; i++){
|
|
|
|
chances[ETownType::names[i]].Float() = parser.readNumber();
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
auto AIVals = parser.readNumArray<si32>(GameConstants::SPELL_SCHOOL_LEVELS);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
std::vector<std::string> descriptions;
|
|
|
|
for(size_t i = 0; i < GameConstants::SPELL_SCHOOL_LEVELS ; i++)
|
|
|
|
descriptions.push_back(parser.readString());
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-13 12:37:42 +02:00
|
|
|
parser.readString(); //ignore attributes. All data present in JSON
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
//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];
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
legacyData.push_back(lineNode);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
}
|
|
|
|
while (parser.endLine() && !parser.isNextEntryEmpty());
|
|
|
|
};
|
2007-06-09 16:28:03 +03:00
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
auto skip = [&](int cnt)
|
2012-08-25 11:44:51 +03:00
|
|
|
{
|
2013-01-16 14:19:04 +03:00
|
|
|
for(int i=0; i<cnt; i++)
|
|
|
|
parser.endLine();
|
|
|
|
};
|
2007-06-09 16:28:03 +03:00
|
|
|
|
2013-01-16 14:19:04 +03:00
|
|
|
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
|
2012-08-25 11:44:51 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
//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;
|
2014-05-01 01:48:33 +03:00
|
|
|
legacyData.push_back(temp);
|
2011-09-04 05:26:56 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
objects.resize(legacyData.size());
|
2011-09-04 05:26:56 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
return legacyData;
|
|
|
|
}
|
2011-09-04 05:26:56 +03:00
|
|
|
|
2014-03-08 22:02:47 +03:00
|
|
|
const std::string CSpellHandler::getTypeName() const
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
return "spell";
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
2012-09-14 02:41:03 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
CSpell * CSpellHandler::loadFromJson(const JsonNode& json)
|
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
using namespace SpellConfig;
|
2013-01-15 17:20:48 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
CSpell * spell = new CSpell();
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
const auto type = json["type"].String();
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
if(type == "ability")
|
|
|
|
{
|
|
|
|
spell->creatureAbility = true;
|
|
|
|
spell->combatSpell = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
spell->creatureAbility = false;
|
|
|
|
spell->combatSpell = type == "combat";
|
|
|
|
}
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->name = json["name"].String();
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
logGlobal->traceStream() << __FUNCTION__ << ": loading spell " << spell->name;
|
2013-01-15 17:20:48 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
const auto schoolNames = json["school"];
|
2014-11-13 15:57:49 +02:00
|
|
|
|
2014-11-24 18:14:10 +02:00
|
|
|
for(const SpellSchoolInfo & info : SpellConfig::SCHOOL)
|
2014-11-13 15:57:49 +02:00
|
|
|
{
|
|
|
|
spell->school[info.id] = schoolNames[info.jsonName].Bool();
|
|
|
|
}
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->level = json["level"].Float();
|
|
|
|
spell->power = json["power"].Float();
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->defaultProbability = json["defaultGainChance"].Float();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
for(const auto & node : json["gainChance"].Struct())
|
2014-03-07 16:21:09 +03:00
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
const int chance = node.second.Float();
|
2013-01-16 14:19:04 +03:00
|
|
|
|
2014-03-07 16:21:09 +03:00
|
|
|
VLC->modh->identifiers.requestIdentifier(node.second.meta, "faction",node.first, [=](si32 factionID)
|
2013-02-13 22:35:43 +03:00
|
|
|
{
|
2014-03-07 16:21:09 +03:00
|
|
|
spell->probabilities[factionID] = chance;
|
|
|
|
});
|
2008-12-20 19:08:51 +02:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
auto targetType = json["targetType"].String();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
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;
|
2014-11-13 12:37:42 +02:00
|
|
|
else if(targetType == "LOCATION")
|
|
|
|
spell->targetType = CSpell::LOCATION;
|
2014-11-12 02:27:45 +02:00
|
|
|
else
|
|
|
|
logGlobal->warnStream() << "Spell " << spell->name << ". Target type " << (targetType.empty() ? "empty" : "unknown ("+targetType+")") << ". Assumed NO_TARGET.";
|
2014-03-07 16:21:09 +03:00
|
|
|
|
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->mainEffectAnim = json["anim"].Float();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
for(const auto & counteredSpell: json["counters"].Struct())
|
|
|
|
if (counteredSpell.second.Bool())
|
|
|
|
{
|
2014-11-12 04:50:51 +02:00
|
|
|
VLC->modh->identifiers.requestIdentifier(json.meta, counteredSpell.first, [=](si32 id)
|
2014-11-12 02:27:45 +02:00
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->counteredSpells.push_back(SpellID(id));
|
|
|
|
});
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
//TODO: more error checking - f.e. conflicting flags
|
|
|
|
const auto flags = json["flags"];
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
//by default all flags are set to false in constructor
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
spell->isDamage = flags["damage"].Bool(); //do this before "offensive"
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
if(flags["offensive"].Bool())
|
|
|
|
{
|
|
|
|
spell->setIsOffensive(true);
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
if(flags["rising"].Bool())
|
|
|
|
{
|
|
|
|
spell->setIsRising(true);
|
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
const bool implicitPositiveness = spell->isOffensive || spell->isRising; //(!) "damage" does not mean NEGATIVE --AVS
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
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
|
2014-11-14 02:29:11 +02:00
|
|
|
logGlobal->errorStream() << "No positiveness specified, assumed NEUTRAL";
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
spell->isSpecial = flags["special"].Bool();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
auto findBonus = [&](std::string name, std::vector<Bonus::BonusType> &vec)
|
2014-05-01 01:48:33 +03:00
|
|
|
{
|
|
|
|
auto it = bonusNameMap.find(name);
|
|
|
|
if(it == bonusNameMap.end())
|
|
|
|
{
|
|
|
|
logGlobal->errorStream() << spell->name << ": invalid bonus name" << name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vec.push_back((Bonus::BonusType)it->second);
|
|
|
|
}
|
|
|
|
};
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
auto readBonusStruct = [&](std::string name, std::vector<Bonus::BonusType> &vec)
|
2014-05-01 01:48:33 +03:00
|
|
|
{
|
2014-03-09 02:18:51 +03:00
|
|
|
for(auto bonusData: json[name].Struct())
|
|
|
|
{
|
|
|
|
const std::string bonusId = bonusData.first;
|
|
|
|
const bool flag = bonusData.second.Bool();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
if(flag)
|
|
|
|
findBonus(bonusId, vec);
|
|
|
|
}
|
2014-05-01 01:48:33 +03:00
|
|
|
};
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
readBonusStruct("immunity", spell->immunities);
|
|
|
|
readBonusStruct("absoluteImmunity", spell->absoluteImmunities);
|
2014-05-18 16:55:26 +03:00
|
|
|
readBonusStruct("limit", spell->limiters);
|
|
|
|
readBonusStruct("absoluteLimit", spell->absoluteLimiters);
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
const JsonNode & graphicsNode = json["graphics"];
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
spell->iconImmune = graphicsNode["iconImmune"].String();
|
|
|
|
spell->iconBook = graphicsNode["iconBook"].String();
|
|
|
|
spell->iconEffect = graphicsNode["iconEffect"].String();
|
2014-03-10 19:00:58 +03:00
|
|
|
spell->iconScenarioBonus = graphicsNode["iconScenarioBonus"].String();
|
2014-11-14 02:29:11 +02:00
|
|
|
spell->iconScroll = graphicsNode["iconScroll"].String();
|
|
|
|
|
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-03-10 19:00:58 +03:00
|
|
|
const JsonNode & soundsNode = json["sounds"];
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-03-10 19:00:58 +03:00
|
|
|
spell->castSound = soundsNode["cast"].String();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-11-14 02:29:11 +02:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
//load level attributes
|
2014-11-14 02:29:11 +02:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
const int levelsCount = GameConstants::SPELL_SCHOOL_LEVELS;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
for(int levelIndex = 0; levelIndex < levelsCount; levelIndex++)
|
|
|
|
{
|
|
|
|
const JsonNode & levelNode = json["levels"][LEVEL_NAMES[levelIndex]];
|
2014-03-17 16:11:10 +03:00
|
|
|
|
|
|
|
CSpell::LevelInfo & levelObject = spell->levels[levelIndex];
|
|
|
|
|
2014-11-13 12:37:42 +02:00
|
|
|
const si32 levelPower = levelObject.power = levelNode["power"].Float();
|
|
|
|
|
|
|
|
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();
|
|
|
|
levelObject.clearAffected = levelNode["targetModifier"]["clearAffected"].Bool();
|
|
|
|
levelObject.range = levelNode["range"].String();
|
2014-03-17 16:11:10 +03:00
|
|
|
|
|
|
|
for(const auto & elem : levelNode["effects"].Struct())
|
2014-03-09 02:18:51 +03:00
|
|
|
{
|
2014-03-17 16:11:10 +03:00
|
|
|
const JsonNode & bonusNode = elem.second;
|
|
|
|
Bonus * b = JsonUtils::parseBonus(bonusNode);
|
|
|
|
const bool usePowerAsValue = bonusNode["val"].isNull();
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
//TODO: make this work. see CSpellHandler::afterLoadFinalization()
|
|
|
|
//b->sid = spell->id; //for all
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
b->source = Bonus::SPELL_EFFECT;//for all
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
if(usePowerAsValue)
|
|
|
|
b->val = levelPower;
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-03-17 16:11:10 +03:00
|
|
|
levelObject.effects.push_back(*b);
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
2014-03-17 16:11:10 +03:00
|
|
|
|
2014-03-09 02:18:51 +03:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2014-05-01 01:48:33 +03:00
|
|
|
return spell;
|
2014-03-07 16:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSpellHandler::afterLoadFinalization()
|
|
|
|
{
|
2014-05-01 01:48:33 +03:00
|
|
|
//FIXME: it is a bad place for this code, should refactor loadFromJson to know object id during loading
|
2014-03-09 02:18:51 +03:00
|
|
|
for(auto spell: objects)
|
2014-11-12 05:59:53 +02:00
|
|
|
{
|
2014-03-17 16:11:10 +03:00
|
|
|
for(auto & level: spell->levels)
|
|
|
|
for(auto & bonus: level.effects)
|
|
|
|
bonus.sid = spell->id;
|
2014-11-12 10:03:55 +02:00
|
|
|
spell->setup();
|
2014-11-12 05:59:53 +02:00
|
|
|
}
|
2008-12-20 19:08:51 +02:00
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-05-18 17:47:18 +03:00
|
|
|
void CSpellHandler::beforeValidate(JsonNode & object)
|
|
|
|
{
|
|
|
|
//handle "base" level info
|
|
|
|
|
2014-05-18 19:27:12 +03:00
|
|
|
JsonNode& levels = object["levels"];
|
|
|
|
JsonNode& base = levels["base"];
|
2014-05-18 17:47:18 +03:00
|
|
|
|
2014-05-19 01:28:44 +03:00
|
|
|
auto inheritNode = [&](const std::string & name){
|
|
|
|
JsonUtils::inherit(levels[name],base);
|
2014-05-18 19:27:12 +03:00
|
|
|
};
|
2014-05-18 17:47:18 +03:00
|
|
|
|
2014-05-19 01:28:44 +03:00
|
|
|
inheritNode("none");
|
|
|
|
inheritNode("basic");
|
|
|
|
inheritNode("advanced");
|
|
|
|
inheritNode("expert");
|
2014-11-14 02:29:11 +02:00
|
|
|
|
2014-05-18 17:47:18 +03:00
|
|
|
}
|
2014-03-07 16:21:09 +03:00
|
|
|
|
|
|
|
|
2013-04-21 19:38:31 +03:00
|
|
|
CSpellHandler::~CSpellHandler()
|
|
|
|
{
|
2014-03-07 16:21:09 +03:00
|
|
|
|
2013-04-21 19:38:31 +03:00
|
|
|
}
|
|
|
|
|
2013-04-21 15:49:26 +03:00
|
|
|
std::vector<bool> CSpellHandler::getDefaultAllowed() const
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-02-05 00:58:42 +03:00
|
|
|
std::vector<bool> allowedSpells;
|
|
|
|
allowedSpells.resize(GameConstants::SPELLS_QUANTITY, true);
|
2013-01-06 22:30:12 +03:00
|
|
|
return allowedSpells;
|
|
|
|
}
|