2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-06-08 17:58:04 +03:00
|
|
|
#include "CHeroHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
#include "CGeneralTextHandler.h"
|
2012-08-01 15:02:54 +03:00
|
|
|
#include "Filesystem/CResourceLoader.h"
|
2012-08-25 11:44:51 +03:00
|
|
|
#include "VCMI_Lib.h"
|
|
|
|
#include "JsonNode.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "GameConstants.h"
|
2012-04-23 22:56:37 +03:00
|
|
|
#include "BattleHex.h"
|
2009-08-31 18:57:15 +03:00
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* CHeroHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-08-04 18:56:36 +03:00
|
|
|
CHeroClass::CHeroClass()
|
|
|
|
{
|
|
|
|
skillLimit = 8;
|
|
|
|
}
|
|
|
|
CHeroClass::~CHeroClass()
|
|
|
|
{
|
|
|
|
}
|
2008-09-29 14:03:30 +03:00
|
|
|
int CHeroClass::chooseSecSkill(const std::set<int> & possibles) const //picks secondary skill out from given possibilities
|
2008-08-04 18:56:36 +03:00
|
|
|
{
|
|
|
|
if(possibles.size()==1)
|
|
|
|
return *possibles.begin();
|
|
|
|
int totalProb = 0;
|
2008-09-29 14:03:30 +03:00
|
|
|
for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
|
2008-08-04 18:56:36 +03:00
|
|
|
{
|
|
|
|
totalProb += proSec[*i];
|
|
|
|
}
|
|
|
|
int ran = rand()%totalProb;
|
2008-09-29 14:03:30 +03:00
|
|
|
for(std::set<int>::const_iterator i=possibles.begin(); i!=possibles.end(); i++)
|
2008-08-04 18:56:36 +03:00
|
|
|
{
|
|
|
|
ran -= proSec[*i];
|
|
|
|
if(ran<0)
|
|
|
|
return *i;
|
|
|
|
}
|
2012-04-22 10:32:45 +03:00
|
|
|
throw std::runtime_error("Cannot pick secondary skill!");
|
2008-08-04 18:56:36 +03:00
|
|
|
}
|
|
|
|
|
2012-09-23 21:01:04 +03:00
|
|
|
EAlignment::EAlignment CHeroClass::getAlignment() const
|
2010-07-20 09:05:45 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
return (EAlignment::EAlignment)alignment;
|
2010-07-20 09:05:45 +03:00
|
|
|
}
|
|
|
|
|
2012-04-23 22:56:37 +03:00
|
|
|
std::vector<BattleHex> CObstacleInfo::getBlocked(BattleHex hex) const
|
2009-02-09 16:50:32 +02:00
|
|
|
{
|
2012-04-23 22:56:37 +03:00
|
|
|
std::vector<BattleHex> ret;
|
|
|
|
if(isAbsoluteObstacle)
|
2009-02-09 16:50:32 +02:00
|
|
|
{
|
2012-04-23 22:56:37 +03:00
|
|
|
assert(!hex.isValid());
|
|
|
|
range::copy(blockedTiles, std::back_inserter(ret));
|
|
|
|
return ret;
|
2009-02-09 16:50:32 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 22:56:37 +03:00
|
|
|
BOOST_FOREACH(int offset, blockedTiles)
|
2009-02-09 16:50:32 +02:00
|
|
|
{
|
2012-04-23 22:56:37 +03:00
|
|
|
BattleHex toBlock = hex + offset;
|
|
|
|
if((hex.getY() & 1) && !(toBlock.getY() & 1))
|
|
|
|
toBlock += BattleHex::LEFT;
|
|
|
|
|
|
|
|
if(!toBlock.isValid())
|
|
|
|
tlog1 << "Misplaced obstacle!\n";
|
|
|
|
else
|
|
|
|
ret.push_back(toBlock);
|
2009-02-09 16:50:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-23 22:56:37 +03:00
|
|
|
bool CObstacleInfo::isAppropriate(int terrainType, int specialBattlefield /*= -1*/) const
|
2009-09-25 14:38:18 +03:00
|
|
|
{
|
2012-04-23 22:56:37 +03:00
|
|
|
if(specialBattlefield != -1)
|
|
|
|
return vstd::contains(allowedSpecialBfields, specialBattlefield);
|
|
|
|
|
|
|
|
return vstd::contains(allowedTerrains, terrainType);
|
2009-09-25 14:38:18 +03:00
|
|
|
}
|
|
|
|
|
2007-07-20 02:38:11 +03:00
|
|
|
CHeroHandler::~CHeroHandler()
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < heroes.size(); i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
heroes[i].dellNull();
|
2009-01-11 00:08:18 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < heroClasses.size(); i++)
|
|
|
|
delete heroClasses[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
CHeroHandler::CHeroHandler()
|
2008-06-13 11:16:51 +03:00
|
|
|
{}
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2009-02-07 18:07:29 +02:00
|
|
|
void CHeroHandler::loadObstacles()
|
|
|
|
{
|
2012-04-23 22:56:37 +03:00
|
|
|
auto loadObstacles = [](const JsonNode &node, bool absolute, std::map<int, CObstacleInfo> &out)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(const JsonNode &obs, node.Vector())
|
|
|
|
{
|
|
|
|
int ID = obs["id"].Float();
|
|
|
|
CObstacleInfo & obi = out[ID];
|
|
|
|
obi.ID = ID;
|
|
|
|
obi.defName = obs["defname"].String();
|
|
|
|
obi.width = obs["width"].Float();
|
|
|
|
obi.height = obs["height"].Float();
|
|
|
|
obi.allowedTerrains = obs["allowedTerrain"].StdVector<ui8>();
|
|
|
|
obi.allowedSpecialBfields = obs["specialBattlefields"].StdVector<ui8>();
|
|
|
|
obi.blockedTiles = obs["blockedTiles"].StdVector<si16>();
|
|
|
|
obi.isAbsoluteObstacle = absolute;
|
|
|
|
}
|
|
|
|
};
|
2011-09-02 05:12:55 +03:00
|
|
|
|
2012-08-02 14:03:26 +03:00
|
|
|
const JsonNode config(ResourceID("config/obstacles.json"));
|
2012-04-23 22:56:37 +03:00
|
|
|
loadObstacles(config["obstacles"], false, obstacles);
|
|
|
|
loadObstacles(config["absoluteObstacles"], true, absoluteObstacles);
|
2012-05-18 23:50:16 +03:00
|
|
|
//loadObstacles(config["moats"], true, moats);
|
2009-02-07 18:07:29 +02:00
|
|
|
}
|
|
|
|
|
2007-06-08 17:58:04 +03:00
|
|
|
void CHeroHandler::loadHeroes()
|
|
|
|
{
|
2008-06-30 03:06:41 +03:00
|
|
|
VLC->heroh = this;
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser parser("DATA/HOTRAITS.TXT");
|
|
|
|
|
|
|
|
parser.endLine(); //ignore header
|
|
|
|
parser.endLine();
|
2007-08-04 22:01:22 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (int i=0; i<GameConstants::HEROES_QUANTITY; i++)
|
2007-06-08 17:58:04 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
CHero * hero = new CHero;
|
|
|
|
hero->name = parser.readString();
|
2007-08-04 22:01:22 +03:00
|
|
|
|
2008-05-03 18:30:11 +03:00
|
|
|
for(int x=0;x<3;x++)
|
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
hero->lowStack[x] = parser.readNumber();
|
|
|
|
hero->highStack[x] = parser.readNumber();
|
|
|
|
hero->refTypeStack[x] = parser.readString();
|
|
|
|
boost::algorithm::replace_all(hero->refTypeStack[x], " ", ""); //remove spaces
|
2008-05-03 18:30:11 +03:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
parser.endLine();
|
|
|
|
|
|
|
|
hero->ID = heroes.size();
|
|
|
|
heroes.push_back(hero);
|
2007-06-08 17:58:04 +03:00
|
|
|
}
|
2011-08-27 18:53:45 +03:00
|
|
|
|
|
|
|
// Load heroes information
|
2012-08-02 14:03:26 +03:00
|
|
|
const JsonNode config(ResourceID("config/heroes.json"));
|
2011-08-27 18:53:45 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &hero, config["heroes"].Vector()) {
|
|
|
|
int hid = hero["id"].Float();
|
2011-08-27 20:33:07 +03:00
|
|
|
const JsonNode *value;
|
2011-08-27 18:53:45 +03:00
|
|
|
|
2011-08-31 02:10:01 +03:00
|
|
|
// sex: 0=male, 1=female
|
|
|
|
heroes[hid]->sex = !!hero["female"].Bool();
|
2012-08-25 12:21:18 +03:00
|
|
|
heroes[hid]->heroType = CHero::EHeroClasses((int)hero["class"].Float());
|
2011-08-27 18:53:45 +03:00
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &set, hero["skill_set"].Vector()) {
|
2011-08-30 09:19:07 +03:00
|
|
|
heroes[hid]->secSkillsInit.push_back(std::make_pair(set["skill"].Float(), set["level"].Float()));
|
2008-09-29 14:03:30 +03:00
|
|
|
}
|
|
|
|
|
2011-08-30 09:19:07 +03:00
|
|
|
value = &hero["spell"];
|
|
|
|
if (!value->isNull()) {
|
|
|
|
heroes[hid]->startingSpell = value->Float();
|
|
|
|
}
|
2011-08-27 20:33:07 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &specialty, hero["specialties"].Vector())
|
|
|
|
{
|
|
|
|
SSpecialtyInfo dummy;
|
2011-08-27 20:33:07 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
dummy.type = specialty["type"].Float();
|
|
|
|
dummy.val = specialty["val"].Float();
|
|
|
|
dummy.subtype = specialty["subtype"].Float();
|
|
|
|
dummy.additionalinfo = specialty["info"].Float();
|
2011-08-27 20:33:07 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
heroes[hid]->spec.push_back(dummy); //put a copy of dummy
|
2011-08-27 20:33:07 +03:00
|
|
|
}
|
2009-07-03 21:40:36 +03:00
|
|
|
}
|
2011-08-27 19:13:44 +03:00
|
|
|
|
2007-08-04 22:01:22 +03:00
|
|
|
loadHeroClasses();
|
|
|
|
initHeroClasses();
|
2008-01-27 18:07:27 +02:00
|
|
|
expPerLevel.push_back(0);
|
|
|
|
expPerLevel.push_back(1000);
|
|
|
|
expPerLevel.push_back(2000);
|
|
|
|
expPerLevel.push_back(3200);
|
2009-07-12 17:07:36 +03:00
|
|
|
expPerLevel.push_back(4600);
|
|
|
|
expPerLevel.push_back(6200);
|
|
|
|
expPerLevel.push_back(8000);
|
|
|
|
expPerLevel.push_back(10000);
|
|
|
|
expPerLevel.push_back(12200);
|
|
|
|
expPerLevel.push_back(14700);
|
|
|
|
expPerLevel.push_back(17500);
|
|
|
|
expPerLevel.push_back(20600);
|
|
|
|
expPerLevel.push_back(24320);
|
|
|
|
expPerLevel.push_back(28784);
|
|
|
|
expPerLevel.push_back(34140);
|
2010-08-16 12:54:09 +03:00
|
|
|
while (expPerLevel[expPerLevel.size() - 1] > expPerLevel[expPerLevel.size() - 2])
|
|
|
|
{
|
|
|
|
int i = expPerLevel.size() - 1;
|
|
|
|
expPerLevel.push_back (expPerLevel[i] + (expPerLevel[i] - expPerLevel[i-1]) * 1.2);
|
|
|
|
}
|
2010-08-25 17:57:58 +03:00
|
|
|
expPerLevel.pop_back();//last value is broken
|
2007-07-17 23:51:49 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser ballParser("DATA/BALLIST.TXT");
|
|
|
|
|
|
|
|
ballParser.endLine(); //header
|
|
|
|
ballParser.endLine();
|
|
|
|
|
|
|
|
do
|
2009-02-06 16:15:45 +02:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
ballParser.readString();
|
|
|
|
ballParser.readString();
|
|
|
|
|
2009-02-06 16:15:45 +02:00
|
|
|
CHeroHandler::SBallisticsLevelInfo bli;
|
2012-08-25 11:44:51 +03:00
|
|
|
bli.keep = ballParser.readNumber();
|
|
|
|
bli.tower = ballParser.readNumber();
|
|
|
|
bli.gate = ballParser.readNumber();
|
|
|
|
bli.wall = ballParser.readNumber();
|
|
|
|
bli.shots = ballParser.readNumber();
|
|
|
|
bli.noDmg = ballParser.readNumber();
|
|
|
|
bli.oneDmg = ballParser.readNumber();
|
|
|
|
bli.twoDmg = ballParser.readNumber();
|
|
|
|
bli.sum = ballParser.readNumber();
|
2009-02-06 16:15:45 +02:00
|
|
|
ballistics.push_back(bli);
|
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (ballParser.endLine());
|
2007-06-09 16:28:03 +03:00
|
|
|
}
|
2011-08-27 20:33:07 +03:00
|
|
|
|
2007-06-18 13:49:06 +03:00
|
|
|
void CHeroHandler::loadHeroClasses()
|
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
CLegacyConfigParser parser("DATA/HCTRAITS.TXT");
|
2007-06-18 13:49:06 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
parser.endLine(); // header
|
|
|
|
parser.endLine();
|
2007-06-18 13:49:06 +03:00
|
|
|
|
2012-08-25 11:44:51 +03:00
|
|
|
do
|
2009-07-16 14:38:07 +03:00
|
|
|
{
|
|
|
|
CHeroClass * hc = new CHeroClass;
|
2012-08-25 11:44:51 +03:00
|
|
|
hc->alignment = heroClasses.size() / 6;
|
|
|
|
|
|
|
|
hc->name = parser.readString();
|
|
|
|
hc->aggression = parser.readNumber();
|
2012-09-23 21:01:04 +03:00
|
|
|
for(int g=0; g<GameConstants::PRIMARY_SKILLS; ++g)
|
|
|
|
{
|
|
|
|
hc->initialPrimSkills[g] = parser.readNumber();
|
|
|
|
}
|
2007-06-18 13:49:06 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
hc->primChance.resize(GameConstants::PRIMARY_SKILLS);
|
|
|
|
for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
|
2007-06-18 15:48:29 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
hc->primChance[x].first = parser.readNumber();
|
2007-06-18 15:48:29 +03:00
|
|
|
}
|
2011-12-14 00:23:17 +03:00
|
|
|
for(int x=0; x<GameConstants::PRIMARY_SKILLS; ++x)
|
2007-06-18 15:48:29 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
hc->primChance[x].second = parser.readNumber();
|
2007-06-18 15:48:29 +03:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
hc->proSec.resize(GameConstants::SKILL_QUANTITY);
|
|
|
|
for(int dd=0; dd<GameConstants::SKILL_QUANTITY; ++dd)
|
2007-06-18 15:48:29 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
hc->proSec[dd] = parser.readNumber();
|
2007-06-18 15:48:29 +03:00
|
|
|
}
|
|
|
|
|
2009-07-16 14:38:07 +03:00
|
|
|
for(int dd=0; dd<ARRAY_COUNT(hc->selectionProbability); ++dd)
|
2007-06-18 15:48:29 +03:00
|
|
|
{
|
2012-08-25 11:44:51 +03:00
|
|
|
hc->selectionProbability[dd] = parser.readNumber();
|
2007-06-18 15:48:29 +03:00
|
|
|
}
|
2009-07-16 14:38:07 +03:00
|
|
|
|
2007-06-18 15:48:29 +03:00
|
|
|
heroClasses.push_back(hc);
|
2007-06-18 13:49:06 +03:00
|
|
|
}
|
2012-08-25 11:44:51 +03:00
|
|
|
while (parser.endLine() && !parser.isNextEntryEmpty());
|
2007-06-18 13:49:06 +03:00
|
|
|
}
|
2007-07-16 13:03:54 +03:00
|
|
|
|
|
|
|
void CHeroHandler::initHeroClasses()
|
|
|
|
{
|
|
|
|
for(int gg=0; gg<heroes.size(); ++gg)
|
|
|
|
{
|
|
|
|
heroes[gg]->heroClass = heroClasses[heroes[gg]->heroType];
|
|
|
|
}
|
2011-09-02 06:39:49 +03:00
|
|
|
|
|
|
|
loadTerrains();
|
2007-07-16 13:03:54 +03:00
|
|
|
}
|
2007-08-12 20:48:05 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 CHeroHandler::level (ui64 experience) const
|
2007-08-12 20:48:05 +03:00
|
|
|
{
|
2009-07-20 06:30:48 +03:00
|
|
|
int i;
|
2009-08-16 18:39:18 +03:00
|
|
|
if (experience <= expPerLevel.back())
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2009-08-17 13:02:29 +03:00
|
|
|
for (i = expPerLevel.size()-1; experience < expPerLevel[i]; i--);
|
2009-07-20 06:30:48 +03:00
|
|
|
return i + 1;
|
2007-09-14 16:11:10 +03:00
|
|
|
}
|
2009-07-20 06:30:48 +03:00
|
|
|
else
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2009-08-17 13:02:29 +03:00
|
|
|
i = expPerLevel.size() - 1;
|
2009-08-16 18:39:18 +03:00
|
|
|
while (experience > reqExp (i))
|
|
|
|
i++;
|
|
|
|
return i;
|
2007-09-14 16:11:10 +03:00
|
|
|
}
|
2008-01-27 18:07:27 +02:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui64 CHeroHandler::reqExp (ui32 level) const
|
2008-01-27 18:07:27 +02:00
|
|
|
{
|
2009-07-20 06:30:48 +03:00
|
|
|
if(!level)
|
|
|
|
return 0;
|
|
|
|
|
2009-10-28 12:45:45 +02:00
|
|
|
if (level <= expPerLevel.size())
|
2009-07-20 06:30:48 +03:00
|
|
|
{
|
2009-10-28 12:45:45 +02:00
|
|
|
return expPerLevel[level-1];
|
2009-07-20 06:30:48 +03:00
|
|
|
}
|
2008-01-27 18:07:27 +02:00
|
|
|
else
|
2007-09-14 16:11:10 +03:00
|
|
|
{
|
2010-08-16 12:54:09 +03:00
|
|
|
tlog3 << "A hero has reached unsupported amount of experience\n";
|
|
|
|
return expPerLevel[expPerLevel.size()-1];
|
2007-09-14 16:11:10 +03:00
|
|
|
}
|
2007-08-12 20:48:05 +03:00
|
|
|
}
|
|
|
|
|
2011-09-02 06:39:49 +03:00
|
|
|
void CHeroHandler::loadTerrains()
|
2007-08-15 18:13:11 +03:00
|
|
|
{
|
2012-08-02 14:03:26 +03:00
|
|
|
const JsonNode config(ResourceID("config/terrains.json"));
|
2009-06-30 15:28:22 +03:00
|
|
|
|
2012-09-05 15:49:23 +03:00
|
|
|
terrCosts.reserve(GameConstants::TERRAIN_TYPES);
|
|
|
|
BOOST_FOREACH(const std::string & name, GameConstants::TERRAIN_NAMES)
|
|
|
|
terrCosts.push_back(config[name].Float());
|
2009-06-30 15:28:22 +03:00
|
|
|
}
|
2009-07-03 21:40:36 +03:00
|
|
|
|
|
|
|
CHero::CHero()
|
|
|
|
{
|
|
|
|
startingSpell = -1;
|
2011-06-03 06:23:50 +03:00
|
|
|
sex = 0xff;
|
2009-07-03 21:40:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CHero::~CHero()
|
|
|
|
{
|
|
|
|
|
2009-10-04 05:02:45 +03:00
|
|
|
}
|