1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/lib/CCreatureSet.cpp

1066 lines
24 KiB
C++
Raw Normal View History

/*
* CCreatureSet.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"
2010-04-02 05:23:22 +03:00
#include "CCreatureSet.h"
#include "CConfigHandler.h"
#include "CCreatureHandler.h"
2010-04-02 05:23:22 +03:00
#include "VCMI_Lib.h"
2012-08-24 12:37:52 +03:00
#include "CModHandler.h"
2023-03-15 21:34:29 +02:00
#include "GameSettings.h"
#include "mapObjects/CGHeroInstance.h"
#include "IGameCallback.h"
#include "CGameState.h"
#include "CGeneralTextHandler.h"
#include "spells/CSpellHandler.h"
#include "CHeroHandler.h"
#include "IBonusTypeHandler.h"
#include "serializer/JsonSerializeFormat.h"
#include "NetPacksBase.h"
2010-04-02 05:23:22 +03:00
#include <vcmi/FactionService.h>
#include <vcmi/Faction.h>
VCMI_LIB_NAMESPACE_BEGIN
bool CreatureSlotComparer::operator()(const TPairCreatureSlot & lhs, const TPairCreatureSlot & rhs)
{
return lhs.first->getAIValue() < rhs.first->getAIValue(); // Descendant order sorting
}
2023-03-13 23:26:44 +02:00
const CStackInstance & CCreatureSet::operator[](const SlotID & slot) const
2010-04-02 05:23:22 +03:00
{
auto i = stacks.find(slot);
2011-01-21 04:36:30 +02:00
if (i != stacks.end())
return *i->second;
2010-04-02 05:23:22 +03:00
else
throw std::runtime_error("That slot is empty!");
2010-04-02 05:23:22 +03:00
}
2023-03-13 23:26:44 +02:00
const CCreature * CCreatureSet::getCreature(const SlotID & slot) const
2010-04-02 05:23:22 +03:00
{
auto i = stacks.find(slot);
2011-01-21 04:36:30 +02:00
if (i != stacks.end())
return i->second->type;
2010-04-02 05:23:22 +03:00
else
return nullptr;
2010-04-02 05:23:22 +03:00
}
2013-02-16 17:03:47 +03:00
bool CCreatureSet::setCreature(SlotID slot, CreatureID type, TQuantity quantity) /*slots 0 to 6 */
2010-04-02 05:23:22 +03:00
{
2013-02-16 17:03:47 +03:00
if(!slot.validSlot())
{
logGlobal->error("Cannot set slot %d", slot.getNum());
2010-04-02 05:23:22 +03:00
return false;
}
if(!quantity)
{
2017-08-10 18:39:27 +02:00
logGlobal->warn("Using set creature to delete stack?");
eraseStack(slot);
return true;
}
if(hasStackAtSlot(slot)) //remove old creature
eraseStack(slot);
2023-03-13 23:26:44 +02:00
auto * armyObj = castToArmyObj();
bool isHypotheticArmy = armyObj ? armyObj->isHypothetic() : false;
putStack(slot, new CStackInstance(type, quantity, isHypotheticArmy));
return true;
2010-04-02 05:23:22 +03:00
}
2023-03-13 23:26:44 +02:00
SlotID CCreatureSet::getSlotFor(const CreatureID & creature, ui32 slotsAmount) const /*returns -1 if no slot available */
2010-04-02 05:23:22 +03:00
{
return getSlotFor(VLC->creh->objects[creature], slotsAmount);
}
SlotID CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount) const
{
assert(c && c->valid());
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
2010-04-02 05:23:22 +03:00
{
assert(elem.second->type->valid());
if(elem.second->type == c)
2010-04-02 05:23:22 +03:00
{
return elem.first; //if there is already such creature we return its slot id
2010-04-02 05:23:22 +03:00
}
}
2013-02-16 17:03:47 +03:00
return getFreeSlot(slotsAmount);
2010-04-02 05:23:22 +03:00
}
2023-03-13 23:26:44 +02:00
bool CCreatureSet::hasCreatureSlots(const CCreature * c, const SlotID & exclude) const
{
assert(c && c->valid());
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks) // elem is const
{
if(elem.first == exclude) // Check slot
continue;
if(!elem.second || !elem.second->type) // Check creature
continue;
assert(elem.second->type->valid());
if(elem.second->type == c)
return true;
}
return false;
}
2023-03-13 23:26:44 +02:00
std::vector<SlotID> CCreatureSet::getCreatureSlots(const CCreature * c, const SlotID & exclude, TQuantity ignoreAmount) const
{
assert(c && c->valid());
std::vector<SlotID> result;
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
{
if(elem.first == exclude)
continue;
if(!elem.second || !elem.second->type || elem.second->type != c)
continue;
if(elem.second->count == ignoreAmount || elem.second->count < 1)
continue;
assert(elem.second->type->valid());
result.push_back(elem.first);
}
return result;
}
bool CCreatureSet::isCreatureBalanced(const CCreature * c, TQuantity ignoreAmount) const
{
assert(c && c->valid());
TQuantity max = 0;
TQuantity min = std::numeric_limits<TQuantity>::max();
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
{
if(!elem.second || !elem.second->type || elem.second->type != c)
continue;
const auto count = elem.second->count;
if(count == ignoreAmount || count < 1)
continue;
assert(elem.second->type->valid());
if(count > max)
max = count;
if(count < min)
min = count;
if(max - min > 1)
return false;
}
return true;
}
SlotID CCreatureSet::getFreeSlot(ui32 slotsAmount) const
{
2013-02-16 17:03:47 +03:00
for(ui32 i=0; i<slotsAmount; i++)
{
2013-02-16 17:03:47 +03:00
if(!vstd::contains(stacks, SlotID(i)))
{
2013-02-16 17:03:47 +03:00
return SlotID(i); //return first free slot
}
}
2013-02-16 17:03:47 +03:00
return SlotID(); //no slot available
}
std::vector<SlotID> CCreatureSet::getFreeSlots(ui32 slotsAmount) const
{
std::vector<SlotID> freeSlots;
for(ui32 i = 0; i < slotsAmount; i++)
{
auto slot = SlotID(i);
if(!vstd::contains(stacks, slot))
freeSlots.push_back(slot);
}
return freeSlots;
}
std::queue<SlotID> CCreatureSet::getFreeSlotsQueue(ui32 slotsAmount) const
{
std::queue<SlotID> freeSlots;
for (ui32 i = 0; i < slotsAmount; i++)
{
auto slot = SlotID(i);
if(!vstd::contains(stacks, slot))
freeSlots.push(slot);
}
return freeSlots;
}
TMapCreatureSlot CCreatureSet::getCreatureMap() const
{
TMapCreatureSlot creatureMap;
TMapCreatureSlot::key_compare keyComp = creatureMap.key_comp();
// https://stackoverflow.com/questions/97050/stdmap-insert-or-stdmap-find
// https://www.cplusplus.com/reference/map/map/key_comp/
2023-03-13 23:26:44 +02:00
for(const auto & pair : stacks)
{
2023-03-13 23:26:44 +02:00
const auto * creature = pair.second->type;
auto slot = pair.first;
2023-03-13 23:26:44 +02:00
auto lb = creatureMap.lower_bound(creature);
if(lb != creatureMap.end() && !(keyComp(creature, lb->first)))
continue;
creatureMap.insert(lb, TMapCreatureSlot::value_type(creature, slot));
}
return creatureMap;
}
2023-03-13 23:26:44 +02:00
TCreatureQueue CCreatureSet::getCreatureQueue(const SlotID & exclude) const
{
TCreatureQueue creatureQueue;
2023-03-13 23:26:44 +02:00
for(const auto & pair : stacks)
{
if(pair.first == exclude)
continue;
creatureQueue.push(std::make_pair(pair.second->type, pair.first));
}
return creatureQueue;
}
2023-03-13 23:26:44 +02:00
TQuantity CCreatureSet::getStackCount(const SlotID & slot) const
2010-04-02 05:23:22 +03:00
{
auto i = stacks.find(slot);
2011-01-21 04:36:30 +02:00
if (i != stacks.end())
return i->second->count;
2010-04-02 05:23:22 +03:00
else
return 0; //TODO? consider issuing a warning
}
2023-03-13 23:26:44 +02:00
TExpType CCreatureSet::getStackExperience(const SlotID & slot) const
{
auto i = stacks.find(slot);
if (i != stacks.end())
return i->second->experience;
else
return 0; //TODO? consider issuing a warning
}
2023-03-13 23:26:44 +02:00
bool CCreatureSet::mergableStacks(std::pair<SlotID, SlotID> & out, const SlotID & preferable) const /*looks for two same stacks, returns slot positions */
2010-04-02 05:23:22 +03:00
{
//try to match creature to our preferred stack
2013-02-16 17:03:47 +03:00
if(preferable.validSlot() && vstd::contains(stacks, preferable))
2010-04-02 05:23:22 +03:00
{
2011-01-21 04:36:30 +02:00
const CCreature *cr = stacks.find(preferable)->second->type;
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
2010-04-02 05:23:22 +03:00
{
if(cr == elem.second->type && elem.first != preferable)
2010-04-02 05:23:22 +03:00
{
out.first = preferable;
out.second = elem.first;
2010-04-02 05:23:22 +03:00
return true;
}
}
}
2023-03-13 23:26:44 +02:00
for(const auto & stack : stacks)
2010-04-02 05:23:22 +03:00
{
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
2010-04-02 05:23:22 +03:00
{
2023-03-13 23:26:44 +02:00
if(stack.second->type == elem.second->type && stack.first != elem.first)
2010-04-02 05:23:22 +03:00
{
2023-03-13 23:26:44 +02:00
out.first = stack.first;
out.second = elem.first;
2010-04-02 05:23:22 +03:00
return true;
}
}
}
return false;
}
void CCreatureSet::sweep()
{
for(auto i=stacks.begin(); i!=stacks.end(); ++i)
2010-04-02 05:23:22 +03:00
{
if(!i->second->count)
2010-04-02 05:23:22 +03:00
{
2011-01-21 04:36:30 +02:00
stacks.erase(i);
2010-04-02 05:23:22 +03:00
sweep();
break;
}
}
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::addToSlot(const SlotID & slot, const CreatureID & cre, TQuantity count, bool allowMerging)
2010-04-02 05:23:22 +03:00
{
const CCreature *c = VLC->creh->objects[cre];
if(!hasStackAtSlot(slot))
{
setCreature(slot, cre, count);
}
else if(getCreature(slot) == c && allowMerging) //that slot was empty or contained same type creature
{
setStackCount(slot, getStackCount(slot) + count);
}
else
{
2017-08-10 18:39:27 +02:00
logGlobal->error("Failed adding to slot!");
}
2010-04-02 05:23:22 +03:00
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::addToSlot(const SlotID & slot, CStackInstance * stack, bool allowMerging)
2010-04-02 05:23:22 +03:00
{
assert(stack->valid(true));
if(!hasStackAtSlot(slot))
{
putStack(slot, stack);
}
else if(allowMerging && stack->type == getCreature(slot))
{
joinStack(slot, stack);
}
else
{
logGlobal->error("Cannot add to slot %d stack %s", slot.getNum(), stack->nodeName());
}
2010-04-02 05:23:22 +03:00
}
bool CCreatureSet::validTypes(bool allowUnrandomized) const
2010-04-02 05:23:22 +03:00
{
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
2010-04-02 05:23:22 +03:00
{
if(!elem.second->valid(allowUnrandomized))
return false;
2010-04-02 05:23:22 +03:00
}
return true;
}
2023-03-13 23:26:44 +02:00
bool CCreatureSet::slotEmpty(const SlotID & slot) const
{
return !hasStackAtSlot(slot);
}
bool CCreatureSet::needsLastStack() const
{
return false;
}
ui64 CCreatureSet::getArmyStrength() const
{
ui64 ret = 0;
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
ret += elem.second->getPower();
return ret;
}
2023-03-13 23:26:44 +02:00
ui64 CCreatureSet::getPower(const SlotID & slot) const
{
return getStack(slot).getPower();
}
2023-03-13 23:26:44 +02:00
std::string CCreatureSet::getRoughAmount(const SlotID & slot, int mode) const
{
/// Mode represent return string format
/// "Pack" - 0, "A pack of" - 1, "a pack of" - 2
CCreature::CreatureQuantityId quantity = CCreature::getQuantityID(getStackCount(slot));
if((int)quantity)
{
2023-02-16 00:36:09 +02:00
if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
return CCreature::getQuantityRangeStringForId(quantity);
return VLC->generaltexth->arraytxt[(174 + mode) + 3*(int)quantity];
}
return "";
}
std::string CCreatureSet::getArmyDescription() const
{
std::string text;
std::vector<std::string> guards;
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
{
auto str = boost::str(boost::format("%s %s") % getRoughAmount(elem.first, 2) % getCreature(elem.first)->getNamePluralTranslated());
guards.push_back(str);
}
2023-03-13 23:26:44 +02:00
if(!guards.empty())
{
for(int i = 0; i < guards.size(); i++)
{
text += guards[i];
if(i + 2 < guards.size())
text += ", ";
else if(i + 2 == guards.size())
text += VLC->generaltexth->allTexts[237];
}
}
return text;
}
int CCreatureSet::stacksCount() const
{
return static_cast<int>(stacks.size());
}
void CCreatureSet::setFormation(bool tight)
{
if (tight)
formation = EArmyFormation::TIGHT;
else
2023-04-10 13:00:34 +02:00
formation = EArmyFormation::LOOSE;
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::setStackCount(const SlotID & slot, TQuantity count)
{
assert(hasStackAtSlot(slot));
assert(stacks[slot]->count + count > 0);
2023-03-15 21:34:29 +02:00
if (VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE) && count > stacks[slot]->count)
stacks[slot]->experience = static_cast<TExpType>(stacks[slot]->experience * (count / static_cast<double>(stacks[slot]->count)));
2011-01-21 04:36:30 +02:00
stacks[slot]->count = count;
armyChanged();
}
void CCreatureSet::giveStackExp(TExpType exp)
{
for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++)
i->second->giveStackExp(exp);
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::setStackExp(const SlotID & slot, TExpType exp)
{
assert(hasStackAtSlot(slot));
stacks[slot]->experience = exp;
}
void CCreatureSet::clear()
{
2011-01-21 04:36:30 +02:00
while(!stacks.empty())
{
2011-01-21 04:36:30 +02:00
eraseStack(stacks.begin()->first);
}
}
2023-03-13 23:26:44 +02:00
const CStackInstance & CCreatureSet::getStack(const SlotID & slot) const
{
assert(hasStackAtSlot(slot));
return *getStackPtr(slot);
}
2023-03-13 23:26:44 +02:00
const CStackInstance * CCreatureSet::getStackPtr(const SlotID & slot) const
{
if(hasStackAtSlot(slot))
return stacks.find(slot)->second;
else return nullptr;
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::eraseStack(const SlotID & slot)
{
assert(hasStackAtSlot(slot));
CStackInstance *toErase = detachStack(slot);
vstd::clear_pointer(toErase);
}
bool CCreatureSet::contains(const CStackInstance *stack) const
{
if(!stack)
return false;
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
if(elem.second == stack)
return true;
return false;
}
2013-02-16 17:03:47 +03:00
SlotID CCreatureSet::findStack(const CStackInstance *stack) const
2010-11-20 19:36:02 +02:00
{
2023-03-13 23:26:44 +02:00
const auto * h = dynamic_cast<const CGHeroInstance *>(this);
if (h && h->commander == stack)
2013-02-16 17:03:47 +03:00
return SlotID::COMMANDER_SLOT_PLACEHOLDER;
if(!stack)
2013-02-16 17:03:47 +03:00
return SlotID();
2010-11-20 19:36:02 +02:00
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
if(elem.second == stack)
return elem.first;
2010-11-20 19:36:02 +02:00
2013-02-16 17:03:47 +03:00
return SlotID();
2010-11-20 19:36:02 +02:00
}
CArmedInstance * CCreatureSet::castToArmyObj()
{
return dynamic_cast<CArmedInstance *>(this);
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::putStack(const SlotID & slot, CStackInstance * stack)
{
2013-02-16 17:03:47 +03:00
assert(slot.getNum() < GameConstants::ARMY_SIZE);
assert(!hasStackAtSlot(slot));
2011-01-21 04:36:30 +02:00
stacks[slot] = stack;
stack->setArmyObj(castToArmyObj());
armyChanged();
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::joinStack(const SlotID & slot, CStackInstance * stack)
{
[[maybe_unused]] const CCreature *c = getCreature(slot);
assert(c == stack->type);
assert(c);
//TODO move stuff
changeStackCount(slot, stack->count);
vstd::clear_pointer(stack);
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::changeStackCount(const SlotID & slot, TQuantity toAdd)
{
setStackCount(slot, getStackCount(slot) + toAdd);
}
CCreatureSet::~CCreatureSet()
{
clear();
}
void CCreatureSet::setToArmy(CSimpleArmy &src)
{
clear();
while(src)
{
auto i = src.army.begin();
2018-03-11 17:56:33 +02:00
putStack(i->first, new CStackInstance(i->second.first, i->second.second));
src.army.erase(i);
}
}
2023-03-13 23:26:44 +02:00
CStackInstance * CCreatureSet::detachStack(const SlotID & slot)
{
assert(hasStackAtSlot(slot));
2011-01-21 04:36:30 +02:00
CStackInstance *ret = stacks[slot];
//if(CArmedInstance *armedObj = castToArmyObj())
if(ret)
{
ret->setArmyObj(nullptr); //detaches from current armyobj
assert(!ret->armyObj); //we failed detaching?
}
2011-01-21 04:36:30 +02:00
stacks.erase(slot);
armyChanged();
return ret;
}
2023-03-13 23:26:44 +02:00
void CCreatureSet::setStackType(const SlotID & slot, const CreatureID & type)
{
assert(hasStackAtSlot(slot));
2011-01-21 04:36:30 +02:00
CStackInstance *s = stacks[slot];
2018-03-10 23:19:36 +02:00
s->setType(type);
armyChanged();
}
bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const
{
if(!allowMergingStacks)
{
int freeSlots = stacksCount() - GameConstants::ARMY_SIZE;
std::set<const CCreature*> cresToAdd;
2023-03-13 23:26:44 +02:00
for(const auto & elem : cs.stacks)
{
SlotID dest = getSlotFor(elem.second->type);
2013-02-16 17:03:47 +03:00
if(!dest.validSlot() || hasStackAtSlot(dest))
cresToAdd.insert(elem.second->type);
}
return cresToAdd.size() <= freeSlots;
}
else
{
CCreatureSet cres;
2013-09-28 15:31:06 +03:00
SlotID j;
//get types of creatures that need their own slot
2023-03-13 23:26:44 +02:00
for(const auto & elem : cs.stacks)
2013-09-28 15:31:06 +03:00
if ((j = cres.getSlotFor(elem.second->type)).validSlot())
2023-04-05 02:26:29 +02:00
cres.addToSlot(j, elem.second->type->getId(), 1, true); //merge if possible
//cres.addToSlot(elem.first, elem.second->type->getId(), 1, true);
2023-03-13 23:26:44 +02:00
for(const auto & elem : stacks)
{
if ((j = cres.getSlotFor(elem.second->type)).validSlot())
2023-04-05 02:26:29 +02:00
cres.addToSlot(j, elem.second->type->getId(), 1, true); //merge if possible
else
return false; //no place found
}
return true; //all stacks found their slots
}
}
2023-03-13 23:26:44 +02:00
bool CCreatureSet::hasStackAtSlot(const SlotID & slot) const
{
2011-01-21 04:36:30 +02:00
return vstd::contains(stacks, slot);
}
CCreatureSet & CCreatureSet::operator=(const CCreatureSet&cs)
{
assert(0);
2010-12-06 01:26:56 +02:00
return *this;
}
void CCreatureSet::armyChanged()
{
}
2023-04-16 19:42:56 +02:00
void CCreatureSet::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName, const std::optional<int> fixedSize)
2016-01-15 19:24:17 +02:00
{
if(handler.saving && stacks.empty())
return;
auto a = handler.enterArray(fieldName);
if(handler.saving)
2016-01-15 19:24:17 +02:00
{
size_t sz = 0;
for(const auto & p : stacks)
vstd::amax(sz, p.first.getNum()+1);
if(fixedSize)
2023-04-16 19:42:56 +02:00
vstd::amax(sz, fixedSize.value());
a.resize(sz, JsonNode::JsonType::DATA_STRUCT);
for(const auto & p : stacks)
{
auto s = a.enterStruct(p.first.getNum());
p.second->serializeJson(handler);
}
2016-01-15 19:24:17 +02:00
}
else
2016-01-15 19:24:17 +02:00
{
for(size_t idx = 0; idx < a.size(); idx++)
{
auto s = a.enterStruct(idx);
TQuantity amount = 0;
2016-01-15 19:24:17 +02:00
handler.serializeInt("amount", amount);
2016-01-15 19:24:17 +02:00
if(amount > 0)
{
2023-03-13 23:26:44 +02:00
auto * new_stack = new CStackInstance();
new_stack->serializeJson(handler);
2023-03-13 23:26:44 +02:00
putStack(SlotID(static_cast<si32>(idx)), new_stack);
2016-02-26 01:35:30 +02:00
}
}
2016-01-15 19:24:17 +02:00
}
}
2010-04-02 05:23:22 +03:00
CStackInstance::CStackInstance()
: armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
}
2023-03-13 23:26:44 +02:00
CStackInstance::CStackInstance(const CreatureID & id, TQuantity Count, bool isHypothetic):
CBonusSystemNode(isHypothetic), armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
setType(id);
count = Count;
}
CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count, bool isHypothetic)
: CBonusSystemNode(isHypothetic), armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
setType(cre);
2010-04-02 05:23:22 +03:00
count = Count;
}
void CStackInstance::init()
{
experience = 0;
count = 0;
type = nullptr;
_armyObj = nullptr;
setNodeType(STACK_INSTANCE);
2010-04-02 05:23:22 +03:00
}
CCreature::CreatureQuantityId CStackInstance::getQuantityID() const
2010-04-02 05:23:22 +03:00
{
return CCreature::getQuantityID(count);
}
int CStackInstance::getExpRank() const
{
2023-03-15 21:34:29 +02:00
if (!VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
return 0;
2023-04-05 02:26:29 +02:00
int tier = type->getLevel();
if (vstd::iswithin(tier, 1, 7))
{
2023-03-13 23:26:44 +02:00
for(int i = static_cast<int>(VLC->creh->expRanks[tier].size()) - 2; i > -1; --i) //sic!
{ //exp values vary from 1st level to max exp at 11th level
if (experience >= VLC->creh->expRanks[tier][i])
return ++i; //faster, but confusing - 0 index mean 1st level of experience
}
return 0;
}
else //higher tier
{
2023-03-13 23:26:44 +02:00
for(int i = static_cast<int>(VLC->creh->expRanks[0].size()) - 2; i > -1; --i)
{
if (experience >= VLC->creh->expRanks[0][i])
return ++i;
}
return 0;
}
}
int CStackInstance::getLevel() const
{
2023-04-05 02:26:29 +02:00
return std::max(1, static_cast<int>(type->getLevel()));
}
si32 CStackInstance::magicResistance() const
{
si32 val = valOfBonuses(Selector::type()(Bonus::MAGIC_RESISTANCE));
vstd::amin (val, 100);
return val;
}
void CStackInstance::giveStackExp(TExpType exp)
{
2023-04-05 02:26:29 +02:00
int level = type->getLevel();
if (!vstd::iswithin(level, 1, 7))
level = 0;
CCreatureHandler * creh = VLC->creh;
2011-02-13 21:46:16 +02:00
ui32 maxExp = creh->expRanks[level].back();
2023-03-13 23:26:44 +02:00
vstd::amin(exp, static_cast<TExpType>(maxExp)); //prevent exp overflow due to different types
vstd::amin(exp, (maxExp * creh->maxExpPerBattle[level])/100);
vstd::amin(experience += exp, maxExp); //can't get more exp than this limit
}
2023-03-13 23:26:44 +02:00
void CStackInstance::setType(const CreatureID & creID)
{
if(creID >= 0 && creID < VLC->creh->objects.size())
setType(VLC->creh->objects[creID]);
else
setType((const CCreature*)nullptr);
}
void CStackInstance::setType(const CCreature *c)
2010-04-02 05:23:22 +03:00
{
if(type)
{
detachFrom(const_cast<CCreature&>(*type));
2023-03-15 21:34:29 +02:00
if (type->isMyUpgrade(c) && VLC->settings()->getBoolean(EGameSettings::MODULE_STACK_EXPERIENCE))
experience = static_cast<TExpType>(experience * VLC->creh->expAfterUpgrade / 100.0);
}
CStackBasicDescriptor::setType(c);
if(type)
attachTo(const_cast<CCreature&>(*type));
}
std::string CStackInstance::bonusToString(const std::shared_ptr<Bonus>& bonus, bool description) const
{
return VLC->getBth()->bonusToString(bonus, this, description);
}
2023-03-13 23:26:44 +02:00
std::string CStackInstance::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
{
return VLC->getBth()->bonusToGraphics(bonus);
}
2021-03-23 16:47:07 +02:00
void CStackInstance::setArmyObj(const CArmedInstance * ArmyObj)
{
if(_armyObj)
detachFrom(const_cast<CArmedInstance&>(*_armyObj));
_armyObj = ArmyObj;
2021-03-23 16:47:07 +02:00
if(ArmyObj)
attachTo(const_cast<CArmedInstance&>(*_armyObj));
}
std::string CStackInstance::getQuantityTXT(bool capitalized) const
{
CCreature::CreatureQuantityId quantity = getQuantityID();
if ((int)quantity)
{
2023-02-16 00:36:09 +02:00
if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
return CCreature::getQuantityRangeStringForId(quantity);
return VLC->generaltexth->arraytxt[174 + (int)quantity*3 - 1 - capitalized];
}
else
return "";
}
bool CStackInstance::valid(bool allowUnrandomized) const
{
if(!randomStack)
{
2023-04-05 02:26:29 +02:00
return (type && type == VLC->creh->objects[type->getId()]);
}
else
return allowUnrandomized;
}
std::string CStackInstance::nodeName() const
{
std::ostringstream oss;
oss << "Stack of " << count << " of ";
if(type)
oss << type->getNamePluralTextID();
else
oss << "[UNDEFINED TYPE]";
return oss.str();
}
PlayerColor CStackInstance::getOwner() const
{
return _armyObj ? _armyObj->getOwner() : PlayerColor::NEUTRAL;
}
void CStackInstance::deserializationFix()
{
const CArmedInstance *armyBackup = _armyObj;
_armyObj = nullptr;
setArmyObj(armyBackup);
artDeserializationFix(this);
}
CreatureID CStackInstance::getCreatureID() const
{
if(type)
2023-04-05 02:26:29 +02:00
return type->getId();
else
return CreatureID::NONE;
}
std::string CStackInstance::getName() const
{
return (count > 1) ? type->getNamePluralTranslated() : type->getNameSingularTranslated();
}
ui64 CStackInstance::getPower() const
{
assert(type);
2023-04-05 02:26:29 +02:00
return type->getAIValue() * count;
}
ArtBearer::ArtBearer CStackInstance::bearerType() const
{
return ArtBearer::CREATURE;
}
void CStackInstance::putArtifact(ArtifactPosition pos, CArtifactInstance * art)
2016-01-15 19:24:17 +02:00
{
assert(!getArt(pos));
art->putAt(ArtifactLocation(this, pos));
2016-01-15 19:24:17 +02:00
}
void CStackInstance::serializeJson(JsonSerializeFormat & handler)
2016-01-15 19:24:17 +02:00
{
//todo: artifacts
CStackBasicDescriptor::serializeJson(handler);//must be first
if(handler.saving)
{
if(randomStack)
{
int level = randomStack->level;
int upgrade = randomStack->upgrade;
handler.serializeInt("level", level, 0);
handler.serializeInt("upgraded", upgrade, 0);
}
}
else
2016-01-15 19:24:17 +02:00
{
//type set by CStackBasicDescriptor::serializeJson
if(type == nullptr)
{
uint8_t level = 0;
uint8_t upgrade = 0;
handler.serializeInt("level", level, 0);
handler.serializeInt("upgrade", upgrade, 0);
randomStack = RandomStackInfo{ level, upgrade };
}
2016-01-15 19:24:17 +02:00
}
}
FactionID CStackInstance::getFaction() const
{
if(type)
return type->getFaction();
return FactionID::NEUTRAL;
}
const IBonusBearer* CStackInstance::getBonusBearer() const
{
return this;
}
CCommanderInstance::CCommanderInstance()
{
init();
}
2023-03-13 23:26:44 +02:00
CCommanderInstance::CCommanderInstance(const CreatureID & id): name("Commando")
{
init();
setType(id);
2023-03-13 23:26:44 +02:00
//TODO - parse them
}
void CCommanderInstance::init()
{
alive = true;
experience = 0;
level = 1;
count = 1;
type = nullptr;
_armyObj = nullptr;
setNodeType (CBonusSystemNode::COMMANDER);
secondarySkills.resize (ECommander::SPELL_POWER + 1);
}
void CCommanderInstance::setAlive (bool Alive)
{
//TODO: helm of immortality
alive = Alive;
if (!alive)
{
removeBonusesRecursive(Bonus::UntilCommanderKilled);
}
}
void CCommanderInstance::giveStackExp (TExpType exp)
{
if (alive)
experience += exp;
}
int CCommanderInstance::getExpRank() const
{
return VLC->heroh->level (experience);
}
int CCommanderInstance::getLevel() const
{
return std::max (1, getExpRank());
}
void CCommanderInstance::levelUp ()
{
level++;
2023-03-13 23:26:44 +02:00
for(const auto & bonus : VLC->creh->commanderLevelPremy)
{ //grant all regular level-up bonuses
accumulateBonus(bonus);
}
}
ArtBearer::ArtBearer CCommanderInstance::bearerType() const
{
return ArtBearer::COMMANDER;
}
bool CCommanderInstance::gainsLevel() const
{
2023-03-13 23:26:44 +02:00
return experience >= static_cast<TExpType>(VLC->heroh->reqExp(level + 1));
}
2023-03-13 23:26:44 +02:00
//This constructor should be placed here to avoid side effects
CStackBasicDescriptor::CStackBasicDescriptor() = default;
2023-03-13 23:26:44 +02:00
CStackBasicDescriptor::CStackBasicDescriptor(const CreatureID & id, TQuantity Count):
type(VLC->creh->objects[id]),
count(Count)
{
}
CStackBasicDescriptor::CStackBasicDescriptor(const CCreature *c, TQuantity Count)
: type(c), count(Count)
{
}
const Creature * CStackBasicDescriptor::getType() const
{
return type;
}
TQuantity CStackBasicDescriptor::getCount() const
{
return count;
}
void CStackBasicDescriptor::setType(const CCreature * c)
2016-01-15 19:24:17 +02:00
{
type = c;
2016-01-15 19:24:17 +02:00
}
void CStackBasicDescriptor::serializeJson(JsonSerializeFormat & handler)
2016-01-15 19:24:17 +02:00
{
handler.serializeInt("amount", count);
if(handler.saving)
{
if(type)
{
std::string typeName = type->getJsonKey();
handler.serializeString("type", typeName);
}
}
else
{
2023-03-13 23:26:44 +02:00
std::string typeName;
handler.serializeString("type", typeName);
if(!typeName.empty())
setType(VLC->creh->getCreature(CModHandler::scopeMap(), typeName));
}
2016-01-15 19:24:17 +02:00
}
void CSimpleArmy::clear()
{
army.clear();
}
CSimpleArmy::operator bool() const
{
2023-03-13 23:26:44 +02:00
return !army.empty();
}
2013-02-16 17:03:47 +03:00
bool CSimpleArmy::setCreature(SlotID slot, CreatureID cre, TQuantity count)
{
assert(!vstd::contains(army, slot));
2018-03-11 17:56:33 +02:00
army[slot] = std::make_pair(cre, count);
return true;
}
VCMI_LIB_NAMESPACE_END