1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/lib/CCreatureHandler.cpp

1390 lines
41 KiB
C++
Raw Normal View History

/*
* CCreatureHandler.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 "CCreatureHandler.h"
#include "CGeneralTextHandler.h"
2023-03-17 01:19:04 +02:00
#include "ResourceSet.h"
#include "filesystem/Filesystem.h"
#include "VCMI_Lib.h"
#include "CTownHandler.h"
2023-03-15 21:34:29 +02:00
#include "GameSettings.h"
#include "constants/StringConstants.h"
2023-04-30 15:52:48 +02:00
#include "bonuses/Limiters.h"
#include "bonuses/Updaters.h"
#include "serializer/JsonDeserializer.h"
#include "serializer/JsonUpdater.h"
#include "mapObjectConstructors/AObjectTypeHandler.h"
#include "mapObjectConstructors/CObjectClassesHandler.h"
#include "modding/CModHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
const std::map<CCreature::CreatureQuantityId, std::string> CCreature::creatureQuantityRanges =
{
{CCreature::CreatureQuantityId::FEW, "1-4"},
{CCreature::CreatureQuantityId::SEVERAL, "5-9"},
{CCreature::CreatureQuantityId::PACK, "10-19"},
{CCreature::CreatureQuantityId::LOTS, "20-49"},
{CCreature::CreatureQuantityId::HORDE, "50-99"},
{CCreature::CreatureQuantityId::THRONG, "100-249"},
{CCreature::CreatureQuantityId::SWARM, "250-499"},
{CCreature::CreatureQuantityId::ZOUNDS, "500-999"},
{CCreature::CreatureQuantityId::LEGION, "1000+"}
};
int32_t CCreature::getIndex() const
{
return idNumber.toEnum();
}
int32_t CCreature::getIconIndex() const
{
return iconIndex;
}
std::string CCreature::getJsonKey() const
{
2023-01-18 23:57:49 +02:00
return modScope + ':' + identifier;
}
void CCreature::registerIcons(const IconRegistar & cb) const
{
cb(getIconIndex(), 0, "CPRSMALL", smallIconName);
cb(getIconIndex(), 0, "TWCRPORT", largeIconName);
}
CreatureID CCreature::getId() const
{
return idNumber;
}
2023-04-05 02:26:29 +02:00
const IBonusBearer * CCreature::getBonusBearer() const
{
return this;
}
int32_t CCreature::getAdvMapAmountMin() const
{
return ammMin;
}
int32_t CCreature::getAdvMapAmountMax() const
{
return ammMax;
}
int32_t CCreature::getAIValue() const
{
return AIValue;
}
int32_t CCreature::getFightValue() const
{
return fightValue;
}
int32_t CCreature::getLevel() const
{
return level;
}
int32_t CCreature::getGrowth() const
{
return growth;
}
int32_t CCreature::getHorde() const
{
return hordeGrowth;
}
FactionID CCreature::getFaction() const
{
return FactionID(faction);
}
int32_t CCreature::getBaseAttack() const
{
static const auto SELECTOR = Selector::typeSubtype(BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::ATTACK)).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseDefense() const
{
static const auto SELECTOR = Selector::typeSubtype(BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::DEFENSE)).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseDamageMin() const
{
static const auto SELECTOR = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 1).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseDamageMax() const
{
static const auto SELECTOR = Selector::typeSubtype(BonusType::CREATURE_DAMAGE, 2).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseHitPoints() const
{
static const auto SELECTOR = Selector::type()(BonusType::STACK_HEALTH).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseSpellPoints() const
{
static const auto SELECTOR = Selector::type()(BonusType::CASTS).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseSpeed() const
{
static const auto SELECTOR = Selector::type()(BonusType::STACKS_SPEED).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
int32_t CCreature::getBaseShots() const
{
static const auto SELECTOR = Selector::type()(BonusType::SHOTS).And(Selector::sourceTypeSel(BonusSource::CREATURE_ABILITY));
return getExportedBonusList().valOfBonuses(SELECTOR);
}
2023-04-05 02:26:29 +02:00
int32_t CCreature::getRecruitCost(GameResID resIndex) const
{
if(resIndex.getNum() >= 0 && resIndex.getNum() < cost.size())
return cost[resIndex];
else
return 0;
}
2023-04-05 02:26:29 +02:00
TResources CCreature::getFullRecruitCost() const
{
return cost;
}
bool CCreature::hasUpgrades() const
{
return !upgrades.empty();
}
std::string CCreature::getNameTranslated() const
{
return getNameSingularTranslated();
}
std::string CCreature::getNamePluralTranslated() const
{
return VLC->generaltexth->translate(getNamePluralTextID());
}
std::string CCreature::getNameSingularTranslated() const
{
return VLC->generaltexth->translate(getNameSingularTextID());
}
std::string CCreature::getNameTextID() const
{
return getNameSingularTextID();
}
std::string CCreature::getNamePluralTextID() const
{
return TextIdentifier("creatures", modScope, identifier, "name", "plural" ).get();
}
std::string CCreature::getNameSingularTextID() const
{
return TextIdentifier("creatures", modScope, identifier, "name", "singular" ).get();
}
CCreature::CreatureQuantityId CCreature::getQuantityID(const int & quantity)
{
if (quantity<5)
return CCreature::CreatureQuantityId::FEW;
if (quantity<10)
return CCreature::CreatureQuantityId::SEVERAL;
if (quantity<20)
return CCreature::CreatureQuantityId::PACK;
if (quantity<50)
return CCreature::CreatureQuantityId::LOTS;
if (quantity<100)
return CCreature::CreatureQuantityId::HORDE;
if (quantity<250)
return CCreature::CreatureQuantityId::THRONG;
if (quantity<500)
return CCreature::CreatureQuantityId::SWARM;
if (quantity<1000)
return CCreature::CreatureQuantityId::ZOUNDS;
return CCreature::CreatureQuantityId::LEGION;
}
std::string CCreature::getQuantityRangeStringForId(const CCreature::CreatureQuantityId & quantityId)
{
if(creatureQuantityRanges.find(quantityId) != creatureQuantityRanges.end())
return creatureQuantityRanges.at(quantityId);
logGlobal->error("Wrong quantityId: %d", (int)quantityId);
assert(0);
return "[ERROR]";
}
int CCreature::estimateCreatureCount(ui32 countID)
{
static const int creature_count[] = { 0, 3, 8, 15, 35, 75, 175, 375, 750, 2500 };
if(countID > 9)
{
logGlobal->error("Wrong countID %d!", countID);
return 0;
}
else
return creature_count[countID];
}
bool CCreature::isDoubleWide() const
{
return doubleWide;
}
/**
* Determines if the creature is of a good alignment.
* @return true if the creture is good, false otherwise.
*/
bool CCreature::isGood () const
{
2023-04-04 12:36:42 +02:00
return VLC->factions()->getByIndex(faction)->getAlignment() == EAlignment::GOOD;
}
/**
* Determines if the creature is of an evil alignment.
* @return true if the creature is evil, false otherwise.
*/
bool CCreature::isEvil () const
{
2023-04-04 12:36:42 +02:00
return VLC->factions()->getByIndex(faction)->getAlignment() == EAlignment::EVIL;
}
2023-03-17 01:19:04 +02:00
si32 CCreature::maxAmount(const TResources &res) const //how many creatures can be bought
{
int ret = 2147483645;
int resAmnt = static_cast<int>(std::min(res.size(),cost.size()));
for(int i=0;i<resAmnt;i++)
if(cost[i])
2023-03-13 23:26:44 +02:00
ret = std::min(ret, (res[i] / cost[i]));
return ret;
}
CCreature::CCreature()
{
setNodeType(CBonusSystemNode::CREATURE);
2016-11-27 21:07:01 +02:00
fightValue = AIValue = growth = hordeGrowth = ammMin = ammMax = 0;
}
2016-11-27 21:07:01 +02:00
void CCreature::addBonus(int val, BonusType type, int subtype)
{
auto selector = Selector::typeSubtype(type, subtype).And(Selector::source(BonusSource::CREATURE_ABILITY, getIndex()));
BonusList & exported = getExportedBonusList();
BonusList existing;
exported.getBonuses(existing, selector, Selector::all);
if(existing.empty())
{
auto added = std::make_shared<Bonus>(BonusDuration::PERMANENT, type, BonusSource::CREATURE_ABILITY, val, getIndex(), subtype, BonusValueType::BASE_NUMBER);
addNewBonus(added);
}
else
{
std::shared_ptr<Bonus> b = existing[0];
b->val = val;
}
}
bool CCreature::isMyUpgrade(const CCreature *anotherCre) const
{
//TODO upgrade of upgrade?
2023-04-05 02:26:29 +02:00
return vstd::contains(upgrades, anotherCre->getId());
}
bool CCreature::valid() const
{
return this == VLC->creh->objects[idNumber];
}
2010-12-29 23:04:22 +02:00
std::string CCreature::nodeName() const
{
return "\"" + getNamePluralTextID() + "\"";
2010-12-29 23:04:22 +02:00
}
void CCreature::updateFrom(const JsonNode & data)
{
JsonUpdater handler(nullptr, data);
{
auto configScope = handler.enterStruct("config");
const JsonNode & configNode = handler.getCurrent();
serializeJson(handler);
if(!configNode["hitPoints"].isNull())
addBonus(configNode["hitPoints"].Integer(), BonusType::STACK_HEALTH);
if(!configNode["speed"].isNull())
addBonus(configNode["speed"].Integer(), BonusType::STACKS_SPEED);
if(!configNode["attack"].isNull())
addBonus(configNode["attack"].Integer(), BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::ATTACK));
if(!configNode["defense"].isNull())
addBonus(configNode["defense"].Integer(), BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::DEFENSE));
if(!configNode["damage"]["min"].isNull())
addBonus(configNode["damage"]["min"].Integer(), BonusType::CREATURE_DAMAGE, 1);
if(!configNode["damage"]["max"].isNull())
addBonus(configNode["damage"]["max"].Integer(), BonusType::CREATURE_DAMAGE, 2);
if(!configNode["shots"].isNull())
addBonus(configNode["shots"].Integer(), BonusType::SHOTS);
if(!configNode["spellPoints"].isNull())
addBonus(configNode["spellPoints"].Integer(), BonusType::CASTS);
}
handler.serializeBonuses("bonuses", this);
}
void CCreature::serializeJson(JsonSerializeFormat & handler)
{
handler.serializeInt("fightValue", fightValue);
handler.serializeInt("aiValue", AIValue);
handler.serializeInt("growth", growth);
handler.serializeInt("horde", hordeGrowth);// Needed at least until configurable buildings
{
auto advMapNode = handler.enterStruct("advMapAmount");
handler.serializeInt("min", ammMin);
handler.serializeInt("max", ammMax);
}
if(handler.updating)
{
cost.serializeJson(handler, "cost");
handler.serializeInt("faction", faction);//TODO: unify with deferred resolve
}
handler.serializeInt("level", level);
handler.serializeBool("doubleWide", doubleWide);
if(!handler.saving)
{
if(ammMin > ammMax)
logMod->error("Invalid creature '%s' configuration, advMapAmount.min > advMapAmount.max", identifier);
}
}
CCreatureHandler::CCreatureHandler()
: expAfterUpgrade(0)
{
VLC->creh = this;
loadCommanders();
}
2016-01-15 19:24:17 +02:00
const CCreature * CCreatureHandler::getCreature(const std::string & scope, const std::string & identifier) const
{
std::optional<si32> index = VLC->identifiers()->getIdentifier(scope, "creature", identifier);
2016-01-15 19:24:17 +02:00
if(!index)
throw std::runtime_error("Creature not found "+identifier);
return objects[*index];
2016-01-15 19:24:17 +02:00
}
void CCreatureHandler::loadCommanders()
{
auto configResource = JsonPath::builtin("config/commanders.json");
std::string modSource = VLC->modh->findResourceOrigin(configResource);
JsonNode data(configResource);
data.setMeta(modSource);
const JsonNode & config = data; // switch to const data accessors
for (auto bonus : config["bonusPerLevel"].Vector())
{
commanderLevelPremy.push_back(JsonUtils::parseBonus(bonus.Vector()));
}
int i = 0;
for (auto skill : config["skillLevels"].Vector())
{
2023-03-13 23:26:44 +02:00
skillLevels.emplace_back();
for (auto skillLevel : skill["levels"].Vector())
{
2023-03-13 23:26:44 +02:00
skillLevels[i].push_back(static_cast<ui8>(skillLevel.Float()));
}
++i;
}
for (auto ability : config["abilityRequirements"].Vector())
{
std::pair <std::shared_ptr<Bonus>, std::pair <ui8, ui8> > a;
a.first = JsonUtils::parseBonus (ability["ability"].Vector());
a.second.first = static_cast<ui8>(ability["skills"].Vector()[0].Float());
a.second.second = static_cast<ui8>(ability["skills"].Vector()[1].Float());
skillRequirements.push_back (a);
}
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadBonuses(JsonNode & creature, std::string bonuses) const
{
2023-03-13 23:26:44 +02:00
auto makeBonusNode = [&](const std::string & type, double val = 0) -> JsonNode
{
JsonNode ret;
ret["type"].String() = type;
ret["val"].Float() = val;
return ret;
};
static const std::map<std::string, JsonNode> abilityMap =
2014-10-02 18:43:46 +03:00
{
{"FLYING_ARMY", makeBonusNode("FLYING")},
{"SHOOTING_ARMY", makeBonusNode("SHOOTER")},
{"SIEGE_WEAPON", makeBonusNode("SIEGE_WEAPON")},
{"const_free_attack", makeBonusNode("BLOCKS_RETALIATION")},
{"IS_UNDEAD", makeBonusNode("UNDEAD")},
{"const_no_melee_penalty", makeBonusNode("NO_MELEE_PENALTY")},
{"const_jousting", makeBonusNode("JOUSTING", 5)},
{"KING_1", makeBonusNode("KING")}, // Slayer with no expertise
{"KING_2", makeBonusNode("KING", 2)}, // Advanced Slayer or better
{"KING_3", makeBonusNode("KING", 3)}, // Expert Slayer only
2014-10-02 18:43:46 +03:00
{"const_no_wall_penalty", makeBonusNode("NO_WALL_PENALTY")},
{"MULTI_HEADED", makeBonusNode("ATTACKS_ALL_ADJACENT")},
{"IMMUNE_TO_MIND_SPELLS", makeBonusNode("MIND_IMMUNITY")},
{"HAS_EXTENDED_ATTACK", makeBonusNode("TWO_HEX_ATTACK_BREATH")}
};
2023-03-13 23:26:44 +02:00
auto hasAbility = [&](const std::string & name) -> bool
{
return boost::algorithm::find_first(bonuses, name);
};
2023-03-13 23:26:44 +02:00
for(const auto & a : abilityMap)
{
if(hasAbility(a.first))
creature["abilities"][a.first] = a.second;
}
if(hasAbility("DOUBLE_WIDE"))
creature["doubleWide"].Bool() = true;
if(hasAbility("const_raises_morale"))
{
JsonNode node = makeBonusNode("MORALE");
node["val"].Float() = 1;
node["propagator"].String() = "HERO";
creature["abilities"]["const_raises_morale"] = node;
}
}
2023-03-15 21:34:29 +02:00
std::vector<JsonNode> CCreatureHandler::loadLegacyData()
{
2023-03-15 21:34:29 +02:00
size_t dataSize = VLC->settings()->getInteger(EGameSettings::TEXTS_CREATURE);
objects.resize(dataSize);
std::vector<JsonNode> h3Data;
h3Data.reserve(dataSize);
CLegacyConfigParser parser(TextPath::builtin("DATA/CRTRAITS.TXT"));
parser.endLine(); // header
// this file is a bit different in some of Russian localisations:
//ENG: Singular Plural Wood ...
//RUS: Singular Plural Plural2 Wood ...
// Try to detect which version this is by header
// TODO: use 3rd name? Stand for "whose", e.g. pikemans'
2023-03-13 23:26:44 +02:00
size_t namesCount = 2;
{
if ( parser.readString() != "Singular" || parser.readString() != "Plural" )
throw std::runtime_error("Incorrect format of CrTraits.txt");
if (parser.readString() == "Plural2")
namesCount = 3;
parser.endLine();
}
for (size_t i=0; i<dataSize; i++)
{
//loop till non-empty line
while (parser.isNextEntryEmpty())
parser.endLine();
JsonNode data;
data["name"]["singular"].String() = parser.readString();
if (namesCount == 3)
parser.readString();
data["name"]["plural"].String() = parser.readString();
for(int v=0; v<7; ++v)
data["cost"][GameConstants::RESOURCE_NAMES[v]].Float() = parser.readNumber();
data["fightValue"].Float() = parser.readNumber();
data["aiValue"].Float() = parser.readNumber();
data["growth"].Float() = parser.readNumber();
data["horde"].Float() = parser.readNumber();
data["hitPoints"].Float() = parser.readNumber();
data["speed"].Float() = parser.readNumber();
data["attack"].Float() = parser.readNumber();
data["defense"].Float() = parser.readNumber();
data["damage"]["min"].Float() = parser.readNumber();
data["damage"]["max"].Float() = parser.readNumber();
if (float shots = parser.readNumber())
data["shots"].Float() = shots;
if (float spells = parser.readNumber())
data["spellPoints"].Float() = spells;
data["advMapAmount"]["min"].Float() = parser.readNumber();
data["advMapAmount"]["max"].Float() = parser.readNumber();
// unused - ability text, not used since we no longer have original creature window
parser.readString();
loadBonuses(data, parser.readString()); //Attributes
h3Data.push_back(data);
}
loadAnimationInfo(h3Data);
return h3Data;
}
CCreature * CCreatureHandler::loadFromJson(const std::string & scope, const JsonNode & node, const std::string & identifier, size_t index)
{
assert(identifier.find(':') == std::string::npos);
assert(!scope.empty());
2023-03-13 23:26:44 +02:00
auto * cre = new CCreature();
if(node["hasDoubleWeek"].Bool())
{
doubledCreatures.insert(CreatureID(index));
}
cre->idNumber = CreatureID(index);
cre->iconIndex = cre->getIndex() + 2;
cre->identifier = identifier;
cre->modScope = scope;
JsonDeserializer handler(nullptr, node);
cre->serializeJson(handler);
2023-04-05 02:26:29 +02:00
cre->cost = ResourceSet(node["cost"]);
VLC->generaltexth->registerString(scope, cre->getNameSingularTextID(), node["name"]["singular"].String());
VLC->generaltexth->registerString(scope, cre->getNamePluralTextID(), node["name"]["plural"].String());
cre->addBonus(node["hitPoints"].Integer(), BonusType::STACK_HEALTH);
cre->addBonus(node["speed"].Integer(), BonusType::STACKS_SPEED);
cre->addBonus(node["attack"].Integer(), BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::ATTACK));
cre->addBonus(node["defense"].Integer(), BonusType::PRIMARY_SKILL, static_cast<int>(PrimarySkill::DEFENSE));
cre->addBonus(node["damage"]["min"].Integer(), BonusType::CREATURE_DAMAGE, 1);
cre->addBonus(node["damage"]["max"].Integer(), BonusType::CREATURE_DAMAGE, 2);
assert(node["damage"]["min"].Integer() <= node["damage"]["max"].Integer());
if(!node["shots"].isNull())
cre->addBonus(node["shots"].Integer(), BonusType::SHOTS);
loadStackExperience(cre, node["stackExperience"]);
loadJsonAnimation(cre, node["graphics"]);
loadCreatureJson(cre, node);
2023-03-13 23:26:44 +02:00
for(const auto & extraName : node["extraNames"].Vector())
{
2023-03-13 23:26:44 +02:00
for(const auto & type_name : getTypeNames())
registerObject(scope, type_name, extraName.String(), cre->getIndex());
}
JsonNode advMapFile = node["graphics"]["map"];
JsonNode advMapMask = node["graphics"]["mapMask"];
VLC->identifiers()->requestIdentifier(scope, "object", "monster", [=](si32 index)
{
JsonNode conf;
conf.setMeta(scope);
2023-04-05 02:26:29 +02:00
VLC->objtypeh->loadSubObject(cre->identifier, conf, Obj::MONSTER, cre->getId().num);
if (!advMapFile.isNull())
{
JsonNode templ;
templ["animation"] = advMapFile;
if (!advMapMask.isNull())
templ["mask"] = advMapMask;
templ.setMeta(scope);
// if creature has custom advMapFile, reset any potentially imported H3M templates and use provided file instead
VLC->objtypeh->getHandlerFor(Obj::MONSTER, cre->getId().num)->clearTemplates();
2023-04-05 02:26:29 +02:00
VLC->objtypeh->getHandlerFor(Obj::MONSTER, cre->getId().num)->addTemplate(templ);
}
// object does not have any templates - this is not usable object (e.g. pseudo-creature like Arrow Tower)
2023-04-05 02:26:29 +02:00
if (VLC->objtypeh->getHandlerFor(Obj::MONSTER, cre->getId().num)->getTemplates().empty())
VLC->objtypeh->removeSubObject(Obj::MONSTER, cre->getId().num);
});
return cre;
}
const std::vector<std::string> & CCreatureHandler::getTypeNames() const
{
static const std::vector<std::string> typeNames = { "creature" };
return typeNames;
}
2009-09-05 17:10:26 +03:00
std::vector<bool> CCreatureHandler::getDefaultAllowed() const
{
std::vector<bool> ret;
2023-03-13 23:26:44 +02:00
ret.reserve(objects.size());
for(const CCreature * crea : objects)
{
ret.push_back(crea ? !crea->special : false);
}
return ret;
}
2023-04-11 17:16:18 +02:00
void CCreatureHandler::loadCrExpMod()
{
if (VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE)) //reading default stack experience values
{
//Calculate rank exp values, formula appears complicated bu no parsing needed
expRanks.resize(8);
int dif = 0;
int it = 8000; //ignore name of this variable
expRanks[0].push_back(it);
for (int j = 1; j < 10; ++j) //used for tiers 8-10, and all other probably
{
expRanks[0].push_back(expRanks[0][j-1] + it + dif);
dif += it/5;
}
for (int i = 1; i < 8; ++i) //used for tiers 1-7
{
dif = 0;
it = 1000 * i;
expRanks[i].push_back(it);
for (int j = 1; j < 10; ++j)
{
expRanks[i].push_back(expRanks[i][j-1] + it + dif);
dif += it/5;
}
}
CLegacyConfigParser expBonParser(TextPath::builtin("DATA/CREXPMOD.TXT"));
2023-04-11 17:16:18 +02:00
expBonParser.endLine(); //header
maxExpPerBattle.resize(8);
for (int i = 1; i < 8; ++i)
{
expBonParser.readString(); //index
expBonParser.readString(); //float multiplier -> hardcoded
expBonParser.readString(); //ignore upgrade mod? ->hardcoded
expBonParser.readString(); //already calculated
maxExpPerBattle[i] = static_cast<ui32>(expBonParser.readNumber());
expRanks[i].push_back(expRanks[i].back() + static_cast<ui32>(expBonParser.readNumber()));
expBonParser.endLine();
}
//skeleton gets exp penalty
objects[56].get()->addBonus(-50, BonusType::EXP_MULTIPLIER, -1);
objects[57].get()->addBonus(-50, BonusType::EXP_MULTIPLIER, -1);
2023-04-11 17:16:18 +02:00
//exp for tier >7, rank 11
expRanks[0].push_back(147000);
expAfterUpgrade = 75; //percent
maxExpPerBattle[0] = maxExpPerBattle[7];
}
}
void CCreatureHandler::loadCrExpBon(CBonusSystemNode & globalEffects)
{
2023-03-15 21:34:29 +02:00
if (VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE)) //reading default stack experience bonuses
{
logGlobal->debug("\tLoading stack experience bonuses");
auto addBonusForAllCreatures = [&](std::shared_ptr<Bonus> b) {
auto limiter = std::make_shared<CreatureLevelLimiter>();
b->addLimiter(limiter);
globalEffects.addNewBonus(b);
};
auto addBonusForTier = [&](int tier, std::shared_ptr<Bonus> b) {
assert(vstd::iswithin(tier, 1, 7));
//bonuses from level 7 are given to high-level creatures too
auto max = tier == GameConstants::CREATURES_PER_TOWN ? std::numeric_limits<int>::max() : tier + 1;
auto limiter = std::make_shared<CreatureLevelLimiter>(tier, max);
b->addLimiter(limiter);
globalEffects.addNewBonus(b);
};
2023-04-11 17:16:18 +02:00
CLegacyConfigParser parser(TextPath::builtin("DATA/CREXPBON.TXT"));
Bonus b; //prototype with some default properties
b.source = BonusSource::STACK_EXPERIENCE;
b.duration = BonusDuration::PERMANENT;
b.valType = BonusValueType::ADDITIVE_VALUE;
b.effectRange = BonusLimitEffect::NO_LIMIT;
2011-02-08 13:34:34 +02:00
b.additionalInfo = 0;
b.turnsRemain = 0;
BonusList bl;
parser.endLine();
parser.readString(); //ignore index
loadStackExp(b, bl, parser);
2023-03-13 23:26:44 +02:00
for(const auto & b : bl)
addBonusForAllCreatures(b); //health bonus is common for all
parser.endLine();
for (int i = 1; i < 7; ++i)
{
for (int j = 0; j < 4; ++j) //four modifiers common for tiers
{
parser.readString(); //ignore index
bl.clear();
loadStackExp(b, bl, parser);
2023-03-13 23:26:44 +02:00
for(const auto & b : bl)
addBonusForTier(i, b);
parser.endLine();
}
}
2011-02-13 21:46:16 +02:00
for (int j = 0; j < 4; ++j) //tier 7
{
parser.readString(); //ignore index
2011-02-13 21:46:16 +02:00
bl.clear();
loadStackExp(b, bl, parser);
2023-03-13 23:26:44 +02:00
for(const auto & b : bl)
2011-02-13 21:46:16 +02:00
addBonusForTier(7, b);
parser.endLine();
2011-02-13 21:46:16 +02:00
}
do //parse everything that's left
{
auto sid = static_cast<ui32>(parser.readNumber()); //id = this particular creature ID
b.sid = sid;
bl.clear();
loadStackExp(b, bl, parser);
2023-03-13 23:26:44 +02:00
for(const auto & b : bl)
{
objects[sid]->addNewBonus(b); //add directly to CCreature Node
}
}
while (parser.endLine());
2011-02-06 10:32:05 +02:00
}//end of Stack Experience
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadAnimationInfo(std::vector<JsonNode> &h3Data) const
{
CLegacyConfigParser parser(TextPath::builtin("DATA/CRANIM.TXT"));
parser.endLine(); // header
parser.endLine();
2009-06-28 17:57:00 +03:00
2023-03-15 21:34:29 +02:00
for(int dd = 0; dd < VLC->settings()->getInteger(EGameSettings::TEXTS_CREATURE); ++dd)
{
while (parser.isNextEntryEmpty() && parser.endLine()) // skip empty lines
;
loadUnitAnimInfo(h3Data[dd]["graphics"], parser);
parser.endLine();
}
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadUnitAnimInfo(JsonNode & graphics, CLegacyConfigParser & parser) const
{
graphics["timeBetweenFidgets"].Float() = parser.readNumber();
JsonNode & animationTime = graphics["animationTime"];
animationTime["walk"].Float() = parser.readNumber();
animationTime["attack"].Float() = parser.readNumber();
parser.readNumber(); // unused value "Flight animation time" - H3 actually uses "Walk animation time" even for flying creatures
animationTime["idle"].Float() = 10.0;
JsonNode & missile = graphics["missile"];
JsonNode & offsets = missile["offset"];
offsets["upperX"].Float() = parser.readNumber();
offsets["upperY"].Float() = parser.readNumber();
offsets["middleX"].Float() = parser.readNumber();
offsets["middleY"].Float() = parser.readNumber();
offsets["lowerX"].Float() = parser.readNumber();
offsets["lowerY"].Float() = parser.readNumber();
for(int i=0; i<12; i++)
{
JsonNode entry;
entry.Float() = parser.readNumber();
missile["frameAngles"].Vector().push_back(entry);
}
graphics["troopCountLocationOffset"].Float() = parser.readNumber();
missile["attackClimaxFrame"].Float() = parser.readNumber();
// assume that creature is not a shooter and should not have whole missile field
if (missile["frameAngles"].Vector()[0].Float() == 0 &&
missile["attackClimaxFrame"].Float() == 0)
graphics.Struct().erase("missile");
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadJsonAnimation(CCreature * cre, const JsonNode & graphics) const
{
cre->animation.timeBetweenFidgets = graphics["timeBetweenFidgets"].Float();
cre->animation.troopCountLocationOffset = static_cast<int>(graphics["troopCountLocationOffset"].Float());
const JsonNode & animationTime = graphics["animationTime"];
cre->animation.walkAnimationTime = animationTime["walk"].Float();
cre->animation.idleAnimationTime = animationTime["idle"].Float();
cre->animation.attackAnimationTime = animationTime["attack"].Float();
const JsonNode & missile = graphics["missile"];
const JsonNode & offsets = missile["offset"];
cre->animation.upperRightMissleOffsetX = static_cast<int>(offsets["upperX"].Float());
cre->animation.upperRightMissleOffsetY = static_cast<int>(offsets["upperY"].Float());
cre->animation.rightMissleOffsetX = static_cast<int>(offsets["middleX"].Float());
cre->animation.rightMissleOffsetY = static_cast<int>(offsets["middleY"].Float());
cre->animation.lowerRightMissleOffsetX = static_cast<int>(offsets["lowerX"].Float());
cre->animation.lowerRightMissleOffsetY = static_cast<int>(offsets["lowerY"].Float());
cre->animation.attackClimaxFrame = static_cast<int>(missile["attackClimaxFrame"].Float());
cre->animation.missleFrameAngles = missile["frameAngles"].convertTo<std::vector<double> >();
cre->smallIconName = graphics["iconSmall"].String();
cre->largeIconName = graphics["iconLarge"].String();
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & config) const
{
creature->animDefName = AnimationPath::fromJson(config["graphics"]["animation"]);
//FIXME: MOD COMPATIBILITY
if (config["abilities"].getType() == JsonNode::JsonType::DATA_STRUCT)
{
2023-03-13 23:26:44 +02:00
for(const auto & ability : config["abilities"].Struct())
{
if (!ability.second.isNull())
{
auto b = JsonUtils::parseBonus(ability.second);
b->source = BonusSource::CREATURE_ABILITY;
b->sid = creature->getIndex();
b->duration = BonusDuration::PERMANENT;
creature->addNewBonus(b);
}
}
}
else
{
for(const JsonNode &ability : config["abilities"].Vector())
{
if(ability.getType() == JsonNode::JsonType::DATA_VECTOR)
{
logMod->error("Ignored outdated creature ability format in %s", creature->getJsonKey());
}
else
{
auto b = JsonUtils::parseBonus(ability);
b->source = BonusSource::CREATURE_ABILITY;
b->sid = creature->getIndex();
b->duration = BonusDuration::PERMANENT;
creature->addNewBonus(b);
}
}
}
VLC->identifiers()->requestIdentifier("faction", config["faction"], [=](si32 faction)
{
creature->faction = FactionID(faction);
});
for(const JsonNode &value : config["upgrades"].Vector())
{
VLC->identifiers()->requestIdentifier("creature", value, [=](si32 identifier)
{
creature->upgrades.insert(CreatureID(identifier));
});
}
creature->animation.projectileImageName = AnimationPath::fromJson(config["graphics"]["missile"]["projectile"]);
for(const JsonNode & value : config["graphics"]["missile"]["ray"].Vector())
{
CCreature::CreatureAnimation::RayColor color;
2023-01-30 00:12:43 +02:00
color.start.r = value["start"].Vector()[0].Integer();
color.start.g = value["start"].Vector()[1].Integer();
color.start.b = value["start"].Vector()[2].Integer();
color.start.a = value["start"].Vector()[3].Integer();
color.end.r = value["end"].Vector()[0].Integer();
color.end.g = value["end"].Vector()[1].Integer();
color.end.b = value["end"].Vector()[2].Integer();
color.end.a = value["end"].Vector()[3].Integer();
creature->animation.projectileRay.push_back(color);
}
creature->special = config["special"].Bool() || config["disabled"].Bool();
const JsonNode & sounds = config["sound"];
2023-09-04 12:03:15 +02:00
creature->sounds.attack = AudioPath::fromJson(sounds["attack"]);
creature->sounds.defend = AudioPath::fromJson(sounds["defend"]);
creature->sounds.killed = AudioPath::fromJson(sounds["killed"]);
creature->sounds.move = AudioPath::fromJson(sounds["move"]);
creature->sounds.shoot = AudioPath::fromJson(sounds["shoot"]);
creature->sounds.wince = AudioPath::fromJson(sounds["wince"]);
creature->sounds.startMoving = AudioPath::fromJson(sounds["startMoving"]);
creature->sounds.endMoving = AudioPath::fromJson(sounds["endMoving"]);
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode & input) const
{
for (const JsonNode &exp : input.Vector())
{
const JsonVector &values = exp["values"].Vector();
int lowerLimit = 1;//, upperLimit = 255;
if (values[0].getType() == JsonNode::JsonType::DATA_BOOL)
{
for (const JsonNode &val : values)
{
2023-03-13 23:26:44 +02:00
if(val.Bool())
{
// parse each bonus separately
// we can not create copies since identifiers resolution does not tracks copies
// leading to unset identifier values in copies
auto bonus = JsonUtils::parseBonus (exp["bonus"]);
bonus->source = BonusSource::STACK_EXPERIENCE;
bonus->duration = BonusDuration::PERMANENT;
bonus->limiter = std::make_shared<RankRangeLimiter>(RankRangeLimiter(lowerLimit));
creature->addNewBonus (bonus);
break; //TODO: allow bonuses to turn off?
}
++lowerLimit;
}
}
else
{
int lastVal = 0;
for (const JsonNode &val : values)
{
if (val.Float() != lastVal)
{
JsonNode bonusInput = exp["bonus"];
bonusInput["val"].Float() = static_cast<int>(val.Float()) - lastVal;
auto bonus = JsonUtils::parseBonus (bonusInput);
bonus->source = BonusSource::STACK_EXPERIENCE;
bonus->duration = BonusDuration::PERMANENT;
bonus->limiter.reset (new RankRangeLimiter(lowerLimit));
creature->addNewBonus (bonus);
}
lastVal = static_cast<int>(val.Float());
++lowerLimit;
}
}
}
}
2023-03-13 23:26:44 +02:00
void CCreatureHandler::loadStackExp(Bonus & b, BonusList & bl, CLegacyConfigParser & parser) const//help function for parsing CREXPBON.txt
2011-02-13 21:46:16 +02:00
{
bool enable = false; //some bonuses are activated with values 2 or 1
std::string buf = parser.readString();
std::string mod = parser.readString();
switch (buf[0])
{
2011-02-06 10:32:05 +02:00
case 'H':
b.type = BonusType::STACK_HEALTH;
b.valType = BonusValueType::PERCENT_TO_BASE;
2011-02-06 10:32:05 +02:00
break;
case 'A':
b.type = BonusType::PRIMARY_SKILL;
b.subtype = static_cast<int>(PrimarySkill::ATTACK);
2011-02-06 10:32:05 +02:00
break;
case 'D':
b.type = BonusType::PRIMARY_SKILL;
b.subtype = static_cast<int>(PrimarySkill::DEFENSE);
2011-02-06 10:32:05 +02:00
break;
case 'M': //Max damage
b.type = BonusType::CREATURE_DAMAGE;
2011-02-06 10:32:05 +02:00
b.subtype = 2;
break;
case 'm': //Min damage
b.type = BonusType::CREATURE_DAMAGE;
2011-02-06 10:32:05 +02:00
b.subtype = 1;
break;
2011-02-08 13:34:34 +02:00
case 'S':
b.type = BonusType::STACKS_SPEED; break;
case 'O':
b.type = BonusType::SHOTS; break;
2011-02-08 13:34:34 +02:00
case 'b':
b.type = BonusType::ENEMY_DEFENCE_REDUCTION; break;
2011-02-08 13:34:34 +02:00
case 'C':
b.type = BonusType::CHANGES_SPELL_COST_FOR_ALLY; break;
2011-07-16 19:40:38 +03:00
case 'd':
b.type = BonusType::DEFENSIVE_STANCE; break;
2011-02-08 13:34:34 +02:00
case 'e':
b.type = BonusType::DOUBLE_DAMAGE_CHANCE;
2012-09-26 22:02:44 +03:00
b.subtype = 0;
break;
case 'E':
b.type = BonusType::DEATH_STARE;
b.subtype = 0; //Gorgon
break;
case 'F':
b.type = BonusType::FEAR; break;
2011-02-08 13:34:34 +02:00
case 'g':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::ANY);
2011-03-08 09:40:14 +02:00
break;
case 'P':
b.type = BonusType::CASTS; break;
case 'R':
b.type = BonusType::ADDITIONAL_RETALIATION; break;
case 'W':
b.type = BonusType::MAGIC_RESISTANCE;
b.subtype = 0; //otherwise creature window goes crazy
break;
2011-02-08 13:34:34 +02:00
case 'f': //on-off skill
enable = true; //sometimes format is: 2 -> 0, 1 -> 1
2011-02-08 13:34:34 +02:00
switch (mod[0])
{
case 'A':
b.type = BonusType::ATTACKS_ALL_ADJACENT; break;
2011-02-08 13:34:34 +02:00
case 'b':
b.type = BonusType::RETURN_AFTER_STRIKE; break;
2011-02-08 13:34:34 +02:00
case 'B':
b.type = BonusType::TWO_HEX_ATTACK_BREATH; break;
2011-02-08 13:34:34 +02:00
case 'c':
b.type = BonusType::JOUSTING;
b.val = 5;
break;
2011-02-08 13:34:34 +02:00
case 'D':
b.type = BonusType::ADDITIONAL_ATTACK; break;
2011-02-08 13:34:34 +02:00
case 'f':
b.type = BonusType::FEARLESS; break;
2011-02-08 13:34:34 +02:00
case 'F':
b.type = BonusType::FLYING; break;
2011-02-08 13:34:34 +02:00
case 'm':
2023-07-14 10:33:21 +02:00
b.type = BonusType::MORALE;
b.val = 1;
b.valType = BonusValueType::INDEPENDENT_MAX;
break;
2011-02-08 13:34:34 +02:00
case 'M':
b.type = BonusType::NO_MORALE; break;
2011-02-08 13:34:34 +02:00
case 'p': //Mind spells
case 'P':
b.type = BonusType::MIND_IMMUNITY; break;
2011-07-10 12:03:46 +03:00
case 'r':
b.type = BonusType::REBIRTH; //on/off? makes sense?
2011-07-10 12:03:46 +03:00
b.subtype = 0;
b.val = 20; //arbitrary value
2011-02-08 13:34:34 +02:00
break;
case 'R':
b.type = BonusType::BLOCKS_RETALIATION; break;
2011-02-08 13:34:34 +02:00
case 's':
b.type = BonusType::FREE_SHOOTING; break;
2011-02-08 13:34:34 +02:00
case 'u':
b.type = BonusType::SPELL_RESISTANCE_AURA; break;
2011-02-08 13:34:34 +02:00
case 'U':
b.type = BonusType::UNDEAD; break;
2011-02-08 13:34:34 +02:00
default:
2017-08-12 13:36:04 +02:00
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
2011-02-08 13:34:34 +02:00
break;
}
break;
case 'w': //specific spell immunities, enabled/disabled
enable = true;
switch (mod[0])
{
case 'B': //Blind
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::BLIND;
b.additionalInfo = 0;//normal immunity
break;
case 'H': //Hypnotize
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::HYPNOTIZE;
b.additionalInfo = 0;//normal immunity
break;
case 'I': //Implosion
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::IMPLOSION;
b.additionalInfo = 0;//normal immunity
break;
case 'K': //Berserk
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::BERSERK;
b.additionalInfo = 0;//normal immunity
break;
case 'M': //Meteor Shower
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::METEOR_SHOWER;
b.additionalInfo = 0;//normal immunity
break;
2011-05-28 08:34:40 +03:00
case 'N': //dispell beneficial spells
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::DISPEL_HELPFUL_SPELLS;
b.additionalInfo = 0;//normal immunity
2011-05-28 08:34:40 +03:00
break;
case 'R': //Armageddon
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::ARMAGEDDON;
b.additionalInfo = 0;//normal immunity
break;
case 'S': //Slow
b.type = BonusType::SPELL_IMMUNITY;
b.subtype = SpellID::SLOW;
b.additionalInfo = 0;//normal immunity
break;
2011-02-13 21:46:16 +02:00
case '6':
case '7':
case '8':
case '9':
b.type = BonusType::LEVEL_SPELL_IMMUNITY;
b.val = std::atoi(mod.c_str()) - 5;
break;
case ':':
b.type = BonusType::LEVEL_SPELL_IMMUNITY;
b.val = GameConstants::SPELL_LEVELS; //in case someone adds higher level spells?
break;
case 'F':
b.type = BonusType::NEGATIVE_EFFECTS_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::FIRE);
break;
case 'O':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::FIRE);
b.val = 100; //Full damage immunity
break;
case 'f':
b.type = BonusType::SPELL_SCHOOL_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::FIRE);
break;
case 'C':
b.type = BonusType::NEGATIVE_EFFECTS_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::WATER);
break;
case 'W':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::WATER);
b.val = 100; //Full damage immunity
break;
case 'w':
b.type = BonusType::SPELL_SCHOOL_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::WATER);
break;
case 'E':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::EARTH);
b.val = 100; //Full damage immunity
break;
case 'e':
b.type = BonusType::SPELL_SCHOOL_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::EARTH);
break;
case 'A':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::AIR);
b.val = 100; //Full damage immunity
break;
case 'a':
b.type = BonusType::SPELL_SCHOOL_IMMUNITY;
b.subtype = SpellSchool(ESpellSchool::AIR);
break;
case 'D':
b.type = BonusType::SPELL_DAMAGE_REDUCTION;
b.subtype = SpellSchool(ESpellSchool::ANY);
b.val = 100; //Full damage immunity
break;
case '0':
b.type = BonusType::RECEPTIVE;
break;
case 'm':
b.type = BonusType::MIND_IMMUNITY;
break;
default:
2017-08-12 13:36:04 +02:00
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
}
break;
2011-02-08 13:34:34 +02:00
case 'i':
enable = true;
b.type = BonusType::NO_DISTANCE_PENALTY;
2011-02-08 13:34:34 +02:00
break;
case 'o':
enable = true;
b.type = BonusType::NO_WALL_PENALTY;
2011-02-08 13:34:34 +02:00
break;
case 'a':
case 'c':
case 'K':
case 'k':
b.type = BonusType::SPELL_AFTER_ATTACK;
b.subtype = stringToNumber(mod);
break;
case 'h':
b.type = BonusType::HATE;
b.subtype = stringToNumber(mod);
break;
case 'p':
case 'J':
b.type = BonusType::SPELL_BEFORE_ATTACK;
b.subtype = stringToNumber(mod);
b.additionalInfo = 3; //always expert?
break;
case 'r':
b.type = BonusType::HP_REGENERATION;
b.val = stringToNumber(mod);
break;
case 's':
b.type = BonusType::ENCHANTED;
b.subtype = stringToNumber(mod);
b.valType = BonusValueType::INDEPENDENT_MAX;
break;
2011-02-08 13:34:34 +02:00
default:
2017-08-12 13:36:04 +02:00
logGlobal->trace("Not parsed bonus %s %s", buf, mod);
return;
2011-02-08 13:34:34 +02:00
break;
}
switch (mod[0])
{
case '+':
case '=': //should we allow percent values to stack or pick highest?
b.valType = BonusValueType::ADDITIVE_VALUE;
2011-02-08 13:34:34 +02:00
break;
}
//limiters, range
2023-03-13 23:26:44 +02:00
si32 lastVal;
si32 curVal;
si32 lastLev = 0;
if (enable) //0 and 2 means non-active, 1 - active
{
if (b.type != BonusType::REBIRTH)
2011-07-10 12:03:46 +03:00
b.val = 0; //on-off ability, no value specified
parser.readNumber(); // 0 level is never active
for (int i = 1; i < 11; ++i)
{
curVal = static_cast<si32>(parser.readNumber());
if (curVal == 1)
{
b.limiter.reset (new RankRangeLimiter(i));
bl.push_back(std::make_shared<Bonus>(b));
break; //never turned off it seems
}
}
}
else
2011-02-08 13:34:34 +02:00
{
lastVal = static_cast<si32>(parser.readNumber());
if (b.type == BonusType::HATE)
lastVal *= 10; //odd fix
//FIXME: value for zero level should be stored in our config files (independent of stack exp)
for (int i = 1; i < 11; ++i)
2011-02-08 13:34:34 +02:00
{
curVal = static_cast<si32>(parser.readNumber());
if (b.type == BonusType::HATE)
curVal *= 10; //odd fix
2011-02-13 15:16:41 +02:00
if (curVal > lastVal) //threshold, add new bonus
{
2011-02-13 15:16:41 +02:00
b.val = curVal - lastVal;
lastVal = curVal;
b.limiter.reset (new RankRangeLimiter(i));
bl.push_back(std::make_shared<Bonus>(b));
lastLev = i; //start new range from here, i = previous rank
}
else if (curVal < lastVal)
{
b.val = lastVal;
b.limiter.reset (new RankRangeLimiter(lastLev, i));
}
2011-02-08 13:34:34 +02:00
}
}
}
2023-03-13 23:26:44 +02:00
int CCreatureHandler::stringToNumber(std::string & s) const
{
boost::algorithm::replace_first(s,"#",""); //drop hash character
return std::atoi(s.c_str());
}
CCreatureHandler::~CCreatureHandler()
{
2016-08-22 12:27:25 +02:00
for(auto & p : skillRequirements)
p.first = nullptr;
}
2010-07-24 00:05:49 +03:00
CreatureID CCreatureHandler::pickRandomMonster(CRandomGenerator & rand, int tier) const
2010-07-24 00:05:49 +03:00
{
int r = 0;
if(tier == -1) //pick any allowed creature
2010-07-24 00:05:49 +03:00
{
do
{
2023-04-05 02:26:29 +02:00
r = (*RandomGeneratorUtil::nextItem(objects, rand))->getId();
} while (objects[r] && objects[r]->special); // find first "not special" creature
}
else
{
assert(vstd::iswithin(tier, 1, 7));
std::vector<CreatureID> allowed;
for(const auto & creature : objects)
{
if(!creature->special && creature->level == tier)
allowed.push_back(creature->getId());
}
2023-03-13 23:26:44 +02:00
if(allowed.empty())
{
logGlobal->warn("Cannot pick a random creature of tier %d!", tier);
return CreatureID::NONE;
}
2010-07-24 00:05:49 +03:00
return *RandomGeneratorUtil::nextItem(allowed, rand);
}
assert (r >= 0); //should always be, but it crashed
return CreatureID(r);
}
void CCreatureHandler::afterLoadFinalization()
{
}
VCMI_LIB_NAMESPACE_END