1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/lib/ArtifactUtils.cpp

287 lines
8.0 KiB
C++
Raw Normal View History

2023-05-17 15:52:16 +02:00
/*
2023-05-17 16:05:09 +02:00
* ArtifactUtils.cpp, part of VCMI engine
2023-05-17 15:52:16 +02:00
*
* 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
*
*/
2023-05-17 16:05:09 +02:00
#include "ArtifactUtils.h"
2023-05-17 15:52:16 +02:00
2023-05-17 16:05:09 +02:00
#include "CArtHandler.h"
#include "IGameSettings.h"
2023-06-20 19:42:43 +02:00
#include "spells/CSpellHandler.h"
2023-05-17 15:52:16 +02:00
#include "mapObjects/CGHeroInstance.h"
2023-05-17 15:52:16 +02:00
VCMI_LIB_NAMESPACE_BEGIN
DLL_LINKAGE bool ArtifactUtils::checkIfSlotValid(const CArtifactSet & artSet, const ArtifactPosition & slot)
{
if(artSet.bearerType() == ArtBearer::HERO)
{
if(isSlotEquipment(slot) || isSlotBackpack(slot) || slot == ArtifactPosition::TRANSITION_POS)
return true;
}
else if(artSet.bearerType() == ArtBearer::ALTAR)
{
if(isSlotBackpack(slot))
return true;
}
else if(artSet.bearerType() == ArtBearer::COMMANDER)
{
if(vstd::contains(commanderSlots(), slot))
return true;
}
else if(artSet.bearerType() == ArtBearer::CREATURE)
{
if(slot == ArtifactPosition::CREATURE_SLOT)
return true;
}
return false;
}
2023-05-17 15:52:16 +02:00
DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtAnyPosition(const CArtifactSet * target, const ArtifactID & aid)
{
if(auto targetSlot = getArtEquippedPosition(target, aid); targetSlot != ArtifactPosition::PRE_FIRST)
return targetSlot;
return getArtBackpackPosition(target, aid);
}
DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtEquippedPosition(const CArtifactSet * target, const ArtifactID & aid)
2023-05-17 15:52:16 +02:00
{
const auto * art = aid.toArtifact();
2023-07-03 22:11:56 +02:00
for(const auto & slot : art->getPossibleSlots().at(target->bearerType()))
2023-05-17 15:52:16 +02:00
{
if(art->canBePutAt(target, slot))
return slot;
}
return ArtifactPosition::PRE_FIRST;
2023-05-17 15:52:16 +02:00
}
DLL_LINKAGE ArtifactPosition ArtifactUtils::getArtBackpackPosition(const CArtifactSet * target, const ArtifactID & aid)
{
const auto * art = aid.toArtifact();
2023-11-08 19:54:02 +02:00
if(target->bearerType() == ArtBearer::HERO)
if(art->canBePutAt(target, ArtifactPosition::BACKPACK_START))
{
return ArtifactPosition::BACKPACK_START;
}
2023-05-17 15:52:16 +02:00
return ArtifactPosition::PRE_FIRST;
}
DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::unmovableSlots()
2023-05-17 15:52:16 +02:00
{
static const std::vector<ArtifactPosition> positions =
2023-05-17 15:52:16 +02:00
{
ArtifactPosition::SPELLBOOK,
ArtifactPosition::MACH4
};
return positions;
}
2024-04-19 16:14:41 +02:00
DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::commonWornSlots()
2023-05-17 15:52:16 +02:00
{
static const std::vector<ArtifactPosition> positions =
2023-05-17 15:52:16 +02:00
{
ArtifactPosition::HEAD,
ArtifactPosition::SHOULDERS,
ArtifactPosition::NECK,
ArtifactPosition::RIGHT_HAND,
ArtifactPosition::LEFT_HAND,
ArtifactPosition::TORSO,
ArtifactPosition::RIGHT_RING,
ArtifactPosition::LEFT_RING,
ArtifactPosition::FEET,
ArtifactPosition::MISC1,
ArtifactPosition::MISC2,
ArtifactPosition::MISC3,
ArtifactPosition::MISC4,
ArtifactPosition::MISC5,
};
return positions;
}
DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::allWornSlots()
{
static const std::vector<ArtifactPosition> positions =
{
ArtifactPosition::HEAD,
ArtifactPosition::SHOULDERS,
ArtifactPosition::NECK,
ArtifactPosition::RIGHT_HAND,
ArtifactPosition::LEFT_HAND,
ArtifactPosition::TORSO,
ArtifactPosition::RIGHT_RING,
ArtifactPosition::LEFT_RING,
ArtifactPosition::FEET,
ArtifactPosition::MISC1,
ArtifactPosition::MISC2,
ArtifactPosition::MISC3,
ArtifactPosition::MISC4,
ArtifactPosition::MISC5,
ArtifactPosition::MACH1,
ArtifactPosition::MACH2,
ArtifactPosition::MACH3,
ArtifactPosition::MACH4,
ArtifactPosition::SPELLBOOK
};
return positions;
}
DLL_LINKAGE const std::vector<ArtifactPosition> & ArtifactUtils::commanderSlots()
{
static const std::vector<ArtifactPosition> positions =
{
ArtifactPosition::COMMANDER1,
ArtifactPosition::COMMANDER2,
ArtifactPosition::COMMANDER3,
ArtifactPosition::COMMANDER4,
ArtifactPosition::COMMANDER5,
ArtifactPosition::COMMANDER6
};
return positions;
}
2023-05-17 15:52:16 +02:00
DLL_LINKAGE bool ArtifactUtils::isArtRemovable(const std::pair<ArtifactPosition, ArtSlotInfo> & slot)
{
return slot.second.artifact
&& !slot.second.locked
&& !vstd::contains(unmovableSlots(), slot.first);
}
DLL_LINKAGE bool ArtifactUtils::checkSpellbookIsNeeded(const CGHeroInstance * heroPtr, const ArtifactID & artID, const ArtifactPosition & slot)
{
// TODO what'll happen if Titan's thunder is equipped by pickin git up or the start of game?
// Titan's Thunder creates new spellbook on equip
if(artID == ArtifactID::TITANS_THUNDER && slot == ArtifactPosition::RIGHT_HAND)
{
if(heroPtr && !heroPtr->hasSpellbook())
2023-05-17 15:52:16 +02:00
{
return true;
2023-05-17 15:52:16 +02:00
}
}
return false;
}
DLL_LINKAGE bool ArtifactUtils::isSlotBackpack(const ArtifactPosition & slot)
{
return slot >= ArtifactPosition::BACKPACK_START;
2023-05-17 15:52:16 +02:00
}
DLL_LINKAGE bool ArtifactUtils::isSlotEquipment(const ArtifactPosition & slot)
{
return slot < ArtifactPosition::BACKPACK_START && slot >= ArtifactPosition(0);
2023-05-17 15:52:16 +02:00
}
DLL_LINKAGE bool ArtifactUtils::isBackpackFreeSlots(const CArtifactSet * target, const size_t reqSlots)
{
2024-01-12 23:57:19 +02:00
if(target->bearerType() == ArtBearer::HERO)
{
const auto backpackCap = VLC->engineSettings()->getInteger(EGameSettings::HEROES_BACKPACK_CAP);
2024-01-12 23:57:19 +02:00
if(backpackCap < 0)
return true;
else
return target->artifactsInBackpack.size() + reqSlots <= backpackCap;
}
2023-05-17 15:52:16 +02:00
else
2024-01-12 23:57:19 +02:00
return false;
2023-05-17 15:52:16 +02:00
}
DLL_LINKAGE std::vector<const CArtifact*> ArtifactUtils::assemblyPossibilities(
2024-06-22 18:29:39 +02:00
const CArtifactSet * artSet, const ArtifactID & aid, const bool onlyEquiped)
2023-05-17 15:52:16 +02:00
{
std::vector<const CArtifact*> arts;
const auto * art = aid.toArtifact();
2023-06-29 17:34:07 +02:00
if(art->isCombined())
2023-05-17 15:52:16 +02:00
return arts;
2024-10-12 20:20:29 +02:00
for(const auto combinedArt : art->getPartOf())
2023-05-17 15:52:16 +02:00
{
2024-10-12 20:20:29 +02:00
assert(combinedArt->isCombined());
2023-05-17 15:52:16 +02:00
bool possible = true;
2024-10-12 20:20:29 +02:00
CArtifactFittingSet fittingSet(*artSet);
for(const auto part : combinedArt->getConstituents()) // check if all constituents are available
2023-05-17 15:52:16 +02:00
{
2024-10-12 20:20:29 +02:00
const auto slot = fittingSet.getArtPos(part->getId(), onlyEquiped, false);
if(slot == ArtifactPosition::PRE_FIRST)
2023-05-17 15:52:16 +02:00
{
2023-09-12 17:30:48 +02:00
possible = false;
break;
2023-05-17 15:52:16 +02:00
}
2024-10-12 20:20:29 +02:00
fittingSet.lockSlot(slot);
2023-05-17 15:52:16 +02:00
}
if(possible)
2024-10-12 20:20:29 +02:00
arts.push_back(combinedArt);
2023-05-17 15:52:16 +02:00
}
return arts;
}
DLL_LINKAGE CArtifactInstance * ArtifactUtils::createScroll(const SpellID & spellId)
2023-05-17 15:52:16 +02:00
{
return ArtifactUtils::createArtifact(ArtifactID::SPELL_SCROLL, spellId);
2023-05-17 15:52:16 +02:00
}
DLL_LINKAGE CArtifactInstance * ArtifactUtils::createArtifact(const ArtifactID & artId, const SpellID & spellId)
2023-05-17 15:52:16 +02:00
{
const std::function<CArtifactInstance*(const CArtifact*)> createArtInst =
[&createArtInst, &spellId](const CArtifact * art) -> CArtifactInstance*
2023-05-17 15:52:16 +02:00
{
assert(art);
2023-05-17 15:52:16 +02:00
auto * artInst = new CArtifactInstance(art);
if(art->isCombined())
2023-05-17 15:52:16 +02:00
{
for(const auto & part : art->getConstituents())
artInst->addPart(createArtInst(part), ArtifactPosition::PRE_FIRST);
2023-05-17 15:52:16 +02:00
}
if(art->isGrowing())
{
auto bonus = std::make_shared<Bonus>();
bonus->type = BonusType::LEVEL_COUNTER;
bonus->val = 0;
artInst->addNewBonus(bonus);
}
if(art->isScroll())
2023-05-17 15:52:16 +02:00
{
artInst->addNewBonus(std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::SPELL,
BonusSource::ARTIFACT_INSTANCE, -1, BonusSourceID(ArtifactID(ArtifactID::SPELL_SCROLL)), BonusSubtypeID(spellId)));
2023-05-17 15:52:16 +02:00
}
return artInst;
};
if(artId.getNum() >= 0)
{
return createArtInst(artId.toArtifact());
2023-05-17 15:52:16 +02:00
}
2023-06-24 17:26:16 +02:00
else
2023-05-17 15:52:16 +02:00
{
2024-09-04 13:32:47 +02:00
return new CArtifactInstance(); // random, empty
2023-05-17 15:52:16 +02:00
}
}
2023-06-30 20:02:57 +02:00
DLL_LINKAGE void ArtifactUtils::insertScrrollSpellName(std::string & description, const SpellID & sid)
2023-06-20 19:42:43 +02:00
{
// We expect scroll description to be like this: This scroll contains the [spell name] spell which is added
// into spell book for as long as hero carries the scroll. So we want to replace text in [...] with a spell name.
// However other language versions don't have name placeholder at all, so we have to be careful
auto nameStart = description.find_first_of('[');
auto nameEnd = description.find_first_of(']', nameStart);
if(nameStart != std::string::npos && nameEnd != std::string::npos)
2023-06-20 19:42:43 +02:00
{
if(sid.getNum() >= 0)
description = description.replace(nameStart, nameEnd - nameStart + 1, sid.toEntity(VLC->spells())->getNameTranslated());
else
description = description.erase(nameStart, nameEnd - nameStart + 2); // erase "[spell name] " - including space
2023-06-20 19:42:43 +02:00
}
}
2023-05-17 15:52:16 +02:00
VCMI_LIB_NAMESPACE_END