2017-08-20 09:18:07 +02:00
|
|
|
/*
|
|
|
|
* CSkillHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
#include "CSkillHandler.h"
|
|
|
|
|
|
|
|
#include "CGeneralTextHandler.h"
|
|
|
|
#include "filesystem/Filesystem.h"
|
|
|
|
|
|
|
|
#include "JsonNode.h"
|
|
|
|
|
|
|
|
#include "CModHandler.h"
|
|
|
|
#include "StringConstants.h"
|
|
|
|
|
|
|
|
#include "CStack.h"
|
|
|
|
#include "battle/BattleInfo.h"
|
|
|
|
#include "battle/CBattleInfoCallback.h"
|
|
|
|
|
|
|
|
///CSkill
|
2017-08-25 03:35:02 +02:00
|
|
|
CSkill::LevelInfo::LevelInfo() : description("")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSkill::LevelInfo::~LevelInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-22 14:40:15 +02:00
|
|
|
CSkill::CSkill(SecondarySkill id) : id(id)
|
2017-08-20 09:18:07 +02:00
|
|
|
{
|
2017-08-22 14:40:15 +02:00
|
|
|
if(id == SecondarySkill::DEFAULT)
|
|
|
|
identifier = "default";
|
|
|
|
else
|
|
|
|
identifier = NSecondarySkill::names[id];
|
2017-08-25 03:35:02 +02:00
|
|
|
// init levels
|
|
|
|
LevelInfo emptyLevel;
|
|
|
|
for(int level = 1; level < NSecondarySkill::levels.size(); level++)
|
|
|
|
levels.push_back(emptyLevel);
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSkill::~CSkill()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSkill::addNewBonus(const std::shared_ptr<Bonus>& b, int level)
|
|
|
|
{
|
|
|
|
b->source = Bonus::SECONDARY_SKILL;
|
2017-08-27 07:40:52 +02:00
|
|
|
b->sid = id;
|
2017-08-20 09:18:07 +02:00
|
|
|
b->duration = Bonus::PERMANENT;
|
|
|
|
b->description = identifier;
|
2017-08-25 03:35:02 +02:00
|
|
|
levels[level-1].effects.push_back(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSkill::setDescription(const std::string & desc, int level)
|
|
|
|
{
|
|
|
|
levels[level-1].description = desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::shared_ptr<Bonus>> & CSkill::getBonus(int level) const
|
|
|
|
{
|
|
|
|
return levels[level-1].effects;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & CSkill::getDescription(int level) const
|
|
|
|
{
|
|
|
|
return levels[level-1].description;
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 03:35:02 +02:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const CSkill::LevelInfo &info)
|
2017-08-20 09:18:07 +02:00
|
|
|
{
|
2017-08-26 04:06:47 +02:00
|
|
|
out << "(\"" << info.description << "\", [";
|
|
|
|
for(int i=0; i < info.effects.size(); i++)
|
|
|
|
out << (i ? "," : "") << info.effects[i]->Description();
|
|
|
|
return out << "])";
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 00:19:30 +02:00
|
|
|
DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const CSkill &skill)
|
|
|
|
{
|
2017-08-26 04:06:47 +02:00
|
|
|
out << "Skill(" << (int)skill.id << "," << skill.identifier << "): [";
|
|
|
|
for(int i=0; i < skill.levels.size(); i++)
|
|
|
|
out << (i ? "," : "") << skill.levels[i];
|
|
|
|
return out << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSkill::toString() const
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << *this;
|
|
|
|
return ss.str();
|
2017-08-23 00:19:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 09:18:07 +02:00
|
|
|
///CSkillHandler
|
|
|
|
CSkillHandler::CSkillHandler()
|
|
|
|
{
|
2017-08-21 14:35:46 +02:00
|
|
|
for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
|
|
|
|
{
|
2017-08-22 14:40:15 +02:00
|
|
|
CSkill * skill = new CSkill(SecondarySkill(id));
|
|
|
|
for(int level = 1; level < NSecondarySkill::levels.size(); level++)
|
2017-08-27 07:40:52 +02:00
|
|
|
for (auto bonus : defaultBonus(SecondarySkill(id), level))
|
|
|
|
skill->addNewBonus(bonus, level);
|
2017-08-22 14:40:15 +02:00
|
|
|
objects.push_back(skill);
|
2017-08-21 14:35:46 +02:00
|
|
|
}
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<JsonNode> CSkillHandler::loadLegacyData(size_t dataSize)
|
|
|
|
{
|
|
|
|
// not supported - no legacy data to load
|
|
|
|
std::vector<JsonNode> legacyData;
|
|
|
|
return legacyData;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string CSkillHandler::getTypeName() const
|
|
|
|
{
|
2017-08-23 14:15:33 +02:00
|
|
|
return "skill";
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSkill * CSkillHandler::loadFromJson(const JsonNode & json, const std::string & identifier)
|
|
|
|
{
|
2017-08-22 14:40:15 +02:00
|
|
|
CSkill * skill = NULL;
|
2017-08-20 09:18:07 +02:00
|
|
|
|
|
|
|
for(int id = 0; id < GameConstants::SKILL_QUANTITY; id++)
|
|
|
|
{
|
|
|
|
if(NSecondarySkill::names[id].compare(identifier) == 0)
|
|
|
|
{
|
2017-08-22 14:40:15 +02:00
|
|
|
skill = new CSkill(SecondarySkill(id));
|
2017-08-20 09:18:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 14:40:15 +02:00
|
|
|
if(!skill)
|
|
|
|
{
|
2017-08-26 04:06:47 +02:00
|
|
|
logGlobal->error("unknown secondary skill %s", identifier);
|
2017-08-22 14:40:15 +02:00
|
|
|
throw std::runtime_error("invalid skill");
|
|
|
|
}
|
|
|
|
|
2017-08-20 09:18:07 +02:00
|
|
|
for(int level = 1; level < NSecondarySkill::levels.size(); level++)
|
|
|
|
{
|
|
|
|
const std::string & levelName = NSecondarySkill::levels[level]; // basic, advanced, expert
|
2017-08-25 03:35:02 +02:00
|
|
|
const JsonNode & levelNode = json[levelName];
|
|
|
|
// parse bonus effects
|
|
|
|
for(auto b : levelNode["effects"].Vector())
|
2017-08-20 09:18:07 +02:00
|
|
|
{
|
|
|
|
auto bonus = JsonUtils::parseBonus(b);
|
|
|
|
bonus->sid = skill->id;
|
|
|
|
skill->addNewBonus(bonus, level);
|
|
|
|
}
|
2017-08-25 03:35:02 +02:00
|
|
|
// parse skill description - tracked separately
|
|
|
|
if(vstd::contains(levelNode.Struct(), "description") && !levelNode["description"].isNull())
|
|
|
|
//CGI->generaltexth->skillInfoTexts[skill->id][level-1] = levelNode["description"].String();
|
|
|
|
skill->setDescription(levelNode["description"].String(), level);
|
2017-08-20 09:18:07 +02:00
|
|
|
}
|
2017-08-26 04:06:47 +02:00
|
|
|
logMod->debug("loaded secondary skill %s(%d)", identifier, (int)skill->id);
|
|
|
|
logMod->trace("%s", skill->toString());
|
2017-08-20 09:18:07 +02:00
|
|
|
|
|
|
|
return skill;
|
|
|
|
}
|
|
|
|
|
2017-08-25 07:17:14 +02:00
|
|
|
void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
|
|
|
|
{
|
|
|
|
auto type_name = getTypeName();
|
|
|
|
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
|
|
|
|
|
|
|
|
if(object->id == SecondarySkill::DEFAULT) // new skill - no index identified
|
|
|
|
{
|
|
|
|
object->id = SecondarySkill(objects.size());
|
|
|
|
objects.push_back(object);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
objects[object->id] = object;
|
|
|
|
|
|
|
|
registerObject(scope, type_name, name, object->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSkillHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
|
|
|
|
{
|
|
|
|
auto type_name = getTypeName();
|
|
|
|
auto object = loadFromJson(data, normalizeIdentifier(scope, "core", name));
|
|
|
|
|
|
|
|
assert(object->id == index);
|
|
|
|
objects[index] = object;
|
|
|
|
|
|
|
|
registerObject(scope,type_name, name, object->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-20 09:18:07 +02:00
|
|
|
void CSkillHandler::afterLoadFinalization()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSkillHandler::beforeValidate(JsonNode & object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSkillHandler::~CSkillHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<bool> CSkillHandler::getDefaultAllowed() const
|
|
|
|
{
|
|
|
|
std::vector<bool> allowedSkills(objects.size(), true);
|
|
|
|
return allowedSkills;
|
|
|
|
}
|
2017-08-21 14:35:46 +02:00
|
|
|
|
|
|
|
// HMM3 default bonus provided by secondary skill
|
2017-08-27 07:40:52 +02:00
|
|
|
std::vector<std::shared_ptr<Bonus>> CSkillHandler::defaultBonus(SecondarySkill skill, int level) const
|
2017-08-21 14:35:46 +02:00
|
|
|
{
|
2017-08-27 07:40:52 +02:00
|
|
|
std::vector<std::shared_ptr<Bonus>> result;
|
2017-08-21 14:35:46 +02:00
|
|
|
|
2017-08-27 07:40:52 +02:00
|
|
|
// add bonus based on current values - useful for adding multiple bonuses easily
|
2017-08-28 10:09:27 +02:00
|
|
|
auto addBonus = [=,&result](int bonusVal, Bonus::BonusType bonusType = Bonus::SECONDARY_SKILL_PREMY, int subtype = 0)
|
|
|
|
{
|
|
|
|
if(bonusType == Bonus::SECONDARY_SKILL_PREMY || bonusType == Bonus::SECONDARY_SKILL_VAL2)
|
|
|
|
subtype = skill;
|
2017-08-27 07:40:52 +02:00
|
|
|
result.push_back(std::make_shared<Bonus>(Bonus::PERMANENT, bonusType, Bonus::SECONDARY_SKILL, bonusVal, skill, subtype, Bonus::BASE_NUMBER));
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(skill)
|
2017-08-21 14:35:46 +02:00
|
|
|
{
|
2017-08-26 04:59:24 +02:00
|
|
|
case SecondarySkill::PATHFINDING:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(25 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::ARCHERY:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(5 + 5 * level * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::LOGISTICS:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(10 * level); break;
|
2017-08-26 07:33:00 +02:00
|
|
|
case SecondarySkill::SCOUTING:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(level, Bonus::SIGHT_RADIOUS); break;
|
2017-08-25 15:43:20 +02:00
|
|
|
case SecondarySkill::DIPLOMACY:
|
2017-08-28 13:33:19 +02:00
|
|
|
addBonus(level);
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(20 * level, Bonus::SURRENDER_DISCOUNT); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::NAVIGATION:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(50 * level); break;
|
2017-08-25 15:43:20 +02:00
|
|
|
case SecondarySkill::LEADERSHIP:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(level, Bonus::MORALE); break;
|
2017-08-25 15:43:20 +02:00
|
|
|
case SecondarySkill::LUCK:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(level, Bonus::LUCK); break;
|
2017-08-28 10:09:27 +02:00
|
|
|
case SecondarySkill::BALLISTICS:
|
|
|
|
addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::CATAPULT);
|
|
|
|
addBonus(level);
|
|
|
|
break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::EAGLE_EYE:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(30 + 10 * level);
|
|
|
|
addBonus(1 + level, Bonus::SECONDARY_SKILL_VAL2);
|
|
|
|
break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::NECROMANCY:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(10 * level); break;
|
2017-08-25 15:43:20 +02:00
|
|
|
case SecondarySkill::ESTATES:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(125 << (level-1)); break;
|
2017-08-27 09:37:54 +02:00
|
|
|
case SecondarySkill::FIRE_MAGIC:
|
|
|
|
case SecondarySkill::AIR_MAGIC:
|
|
|
|
case SecondarySkill::WATER_MAGIC:
|
|
|
|
case SecondarySkill::EARTH_MAGIC:
|
|
|
|
addBonus(level); break;
|
2017-08-27 10:10:25 +02:00
|
|
|
case SecondarySkill::SCHOLAR:
|
|
|
|
addBonus(1 + level); break;
|
2017-08-25 15:43:20 +02:00
|
|
|
case SecondarySkill::TACTICS:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(1 + 2 * level); break;
|
2017-08-27 11:05:17 +02:00
|
|
|
case SecondarySkill::ARTILLERY:
|
2017-08-28 10:09:27 +02:00
|
|
|
addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::BALLISTA);
|
|
|
|
addBonus(25 + 25 * level);
|
2017-08-27 11:05:17 +02:00
|
|
|
if(level > 1) // extra attack
|
|
|
|
addBonus(1, Bonus::SECONDARY_SKILL_VAL2);
|
2017-08-28 10:09:27 +02:00
|
|
|
break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::LEARNING:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(5 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::OFFENCE:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(10 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::ARMORER:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(5 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::INTELLIGENCE:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(25 << (level-1)); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::SORCERY:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(5 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::RESISTANCE:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(5 << (level-1)); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
case SecondarySkill::FIRST_AID:
|
2017-08-28 10:09:27 +02:00
|
|
|
addBonus(100, Bonus::MANUAL_CONTROL, CreatureID::FIRST_AID_TENT);
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(25 + 25 * level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
default:
|
2017-08-27 07:40:52 +02:00
|
|
|
addBonus(level); break;
|
2017-08-21 14:35:46 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 07:40:52 +02:00
|
|
|
return result;
|
2017-08-21 14:35:46 +02:00
|
|
|
}
|