1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00
vcmi/lib/CCreatureSet.cpp

472 lines
10 KiB
C++
Raw Normal View History

2010-04-02 05:23:22 +03:00
#define VCMI_DLL
#include "CCreatureSet.h"
#include "../hch/CCreatureHandler.h"
#include "VCMI_Lib.h"
#include <assert.h>
#include "../hch/CObjectHandler.h"
#include "IGameCallback.h"
#include "CGameState.h"
#include "../hch/CGeneralTextHandler.h"
2010-04-02 05:23:22 +03:00
const CStackInstance &CCreatureSet::operator[](TSlot slot) const
2010-04-02 05:23:22 +03:00
{
TSlots::const_iterator i = slots.find(slot);
if (i != slots.end())
return *i->second;
2010-04-02 05:23:22 +03:00
else
throw std::string("That slot is empty!");
}
const CCreature* CCreatureSet::getCreature(TSlot slot) const
2010-04-02 05:23:22 +03:00
{
TSlots::const_iterator i = slots.find(slot);
if (i != slots.end())
return i->second->type;
2010-04-02 05:23:22 +03:00
else
return NULL;
}
bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
{
if(slot > 6 || slot < 0)
{
tlog1 << "Cannot set slot " << slot << std::endl;
2010-04-02 05:23:22 +03:00
return false;
}
if(!quantity)
{
tlog2 << "Using set creature to delete stack?\n";
eraseStack(slot);
return true;
}
if(vstd::contains(slots, slot)) //remove old creature
eraseStack(slot);
putStack(slot, new CStackInstance(type, quantity));
return true;
2010-04-02 05:23:22 +03:00
}
TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
{
return getSlotFor(VLC->creh->creatures[creature], slotsAmount);
}
TSlot CCreatureSet::getSlotFor(const CCreature *c, ui32 slotsAmount/*=ARMY_SIZE*/) const
{
assert(c->valid());
2010-04-02 05:23:22 +03:00
for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
{
assert(i->second->type->valid());
if(i->second->type == c)
2010-04-02 05:23:22 +03:00
{
return i->first; //if there is already such creature we return its slot id
}
}
for(ui32 i=0; i<slotsAmount; i++)
{
if(slots.find(i) == slots.end())
{
return i; //return first free slot
}
}
return -1; //no slot available
}
int CCreatureSet::getStackCount(TSlot slot) const
2010-04-02 05:23:22 +03:00
{
TSlots::const_iterator i = slots.find(slot);
if (i != slots.end())
return i->second->count;
2010-04-02 05:23:22 +03:00
else
return 0; //TODO? consider issuing a warning
}
bool CCreatureSet::mergableStacks(std::pair<TSlot, TSlot> &out, TSlot preferable /*= -1*/) 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
if(preferable >= 0 && vstd::contains(slots, preferable))
{
const CCreature *cr = slots.find(preferable)->second->type;
2010-04-02 05:23:22 +03:00
for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
{
if(cr == j->second->type && j->first != preferable)
2010-04-02 05:23:22 +03:00
{
out.first = preferable;
out.second = j->first;
return true;
}
}
}
for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
{
for(TSlots::const_iterator j=slots.begin(); j!=slots.end(); ++j)
{
if(i->second->type == j->second->type && i->first != j->first)
2010-04-02 05:23:22 +03:00
{
out.first = i->first;
out.second = j->first;
return true;
}
}
}
return false;
}
void CCreatureSet::sweep()
{
for(TSlots::iterator i=slots.begin(); i!=slots.end(); ++i)
{
if(!i->second->count)
2010-04-02 05:23:22 +03:00
{
slots.erase(i);
sweep();
break;
}
}
}
void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
2010-04-02 05:23:22 +03:00
{
const CCreature *c = VLC->creh->creatures[cre];
if(!vstd::contains(slots, 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
{
tlog1 << "Failed adding to slot!\n";
}
2010-04-02 05:23:22 +03:00
}
void CCreatureSet::addToSlot(TSlot slot, CStackInstance *stack, bool allowMerging/* = true*/)
2010-04-02 05:23:22 +03:00
{
assert(stack->valid(true));
if(!vstd::contains(slots, slot))
{
putStack(slot, stack);
}
else if(allowMerging && stack->type == getCreature(slot))
{
joinStack(slot, stack);
}
else
{
tlog1 << "Cannot add to slot " << slot << " stack " << *stack << std::endl;
}
2010-04-02 05:23:22 +03:00
}
bool CCreatureSet::validTypes(bool allowUnrandomized /*= false*/) const
{
for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
{
if(!i->second->valid(allowUnrandomized))
return false;
2010-04-02 05:23:22 +03:00
}
return true;
}
bool CCreatureSet::slotEmpty(TSlot slot) const
{
return !vstd::contains(slots, slot);
}
bool CCreatureSet::needsLastStack() const
{
return false;
}
int CCreatureSet::getArmyStrength() const
{
int ret = 0;
for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
ret += i->second->type->AIValue * i->second->count;
return ret;
}
ui64 CCreatureSet::getPower (TSlot slot) const
{
return getCreature(slot)->AIValue * getStackCount(slot);
}
std::string CCreatureSet::getRoughAmount (TSlot slot) const
{
return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getStackCount(slot))];
}
int CCreatureSet::stacksCount() const
{
return slots.size();
}
void CCreatureSet::setFormation(bool tight)
{
formation = tight;
}
void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
{
assert(vstd::contains(slots, slot));
assert(count > 0);
slots[slot]->count = count;
}
void CCreatureSet::clear()
{
while(!slots.empty())
{
eraseStack(slots.begin()->first);
}
}
const CStackInstance& CCreatureSet::getStack(TSlot slot) const
{
assert(vstd::contains(slots, slot));
return *slots.find(slot)->second;
}
void CCreatureSet::eraseStack(TSlot slot)
{
assert(vstd::contains(slots, slot));
delNull(slots[slot]);
slots.erase(slot);
}
bool CCreatureSet::contains(const CStackInstance *stack) const
{
if(!stack)
return false;
for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
if(i->second == stack)
return true;
return false;
}
2010-11-20 19:36:02 +02:00
TSlot CCreatureSet::findStack(const CStackInstance *stack) const
{
if(!stack)
return -1;
for(TSlots::const_iterator i = slots.begin(); i != slots.end(); ++i)
if(i->second == stack)
2010-11-20 19:36:02 +02:00
return i->first;
return -1;
}
CArmedInstance * CCreatureSet::castToArmyObj()
{
return dynamic_cast<CArmedInstance *>(this);
}
void CCreatureSet::putStack(TSlot slot, CStackInstance *stack)
{
assert(!vstd::contains(slots, slot));
slots[slot] = stack;
stack->setArmyObj(castToArmyObj());
}
void CCreatureSet::joinStack(TSlot slot, CStackInstance * stack)
{
const CCreature *c = getCreature(slot);
assert(c == stack->type);
assert(c);
//TODO move stuff
changeStackCount(slot, stack->count);
delNull(stack);
}
void CCreatureSet::changeStackCount(TSlot slot, TQuantity toAdd)
{
setStackCount(slot, getStackCount(slot) + toAdd);
}
CCreatureSet::CCreatureSet()
{
formation = false;
}
CCreatureSet::~CCreatureSet()
{
clear();
}
void CCreatureSet::setToArmy(CCreatureSet &src)
{
clear();
while(src)
{
TSlots::iterator i = src.slots.begin();
assert(i->second->type);
assert(i->second->valid(false));
assert(i->second->armyObj == NULL);
putStack(i->first, i->second);
src.slots.erase(i);
}
}
CStackInstance * CCreatureSet::detachStack(TSlot slot)
{
assert(vstd::contains(slots, slot));
CStackInstance *ret = slots[slot];
if(CArmedInstance *armedObj = castToArmyObj())
ret->detachFrom(armedObj);
slots.erase(slot);
return ret;
}
void CCreatureSet::setStackType(TSlot slot, const CCreature *type)
{
assert(vstd::contains(slots, slot));
CStackInstance *s = slots[slot];
s->setType(type->idNumber);
}
bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs) const
{
std::set<const CCreature*> cres;
//get types of creatures that need their own slot
for(TSlots::const_iterator i = cs.slots.begin(); i != cs.slots.end(); i++)
cres.insert(i->second->type);
for(TSlots::const_iterator i = slots.begin(); i != slots.end(); i++)
cres.insert(i->second->type);
return cres.size() <= ARMY_SIZE;
}
bool CCreatureSet::hasStackAtSlot(TSlot slot) const
{
return vstd::contains(slots, slot);
}
2010-04-02 05:23:22 +03:00
CStackInstance::CStackInstance()
: armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
}
CStackInstance::CStackInstance(TCreature id, TQuantity Count)
: armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
setType(id);
count = Count;
}
CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
: armyObj(_armyObj)
2010-04-02 05:23:22 +03:00
{
init();
type = cre;
count = Count;
}
void CStackInstance::init()
{
experience = 0;
count = 0;
type = NULL;
idRand = -1;
_armyObj = NULL;
nodeType = STACK;
2010-04-02 05:23:22 +03:00
}
int CStackInstance::getQuantityID() const
{
return CCreature::getQuantityID(count);
}
void CStackInstance::setType(int creID)
{
setType(VLC->creh->creatures[creID]);
}
void CStackInstance::setType(const CCreature *c)
2010-04-02 05:23:22 +03:00
{
if(type)
detachFrom(const_cast<CCreature*>(type));
type = c;
attachTo(const_cast<CCreature*>(type));
}
void CStackInstance::setArmyObj(const CArmedInstance *ArmyObj)
{
if(_armyObj)
detachFrom(const_cast<CArmedInstance*>(_armyObj));
if(ArmyObj)
{
_armyObj = ArmyObj;
attachTo(const_cast<CArmedInstance*>(_armyObj));
}
}
// void CStackInstance::getParents(TCNodes &out, const CBonusSystemNode *source /*= NULL*/) const
// {
// out.insert(type);
//
// if(source && source != this) //we should be root, if not - do not inherit anything
// return;
//
// if(armyObj)
// out.insert(armyObj);
// else
// out.insert(&IObjectInterface::cb->gameState()->globalEffects);
// }
std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
{
return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
}
bool CStackInstance::valid(bool allowUnrandomized) const
{
bool isRand = (idRand != -1);
if(!isRand)
{
return (type && type == VLC->creh->creatures[type->idNumber]);
}
else
return allowUnrandomized;
}
CStackBasicDescriptor::CStackBasicDescriptor()
{
type = NULL;
count = -1;
}
CStackBasicDescriptor::CStackBasicDescriptor(TCreature id, TQuantity Count)
: type (VLC->creh->creatures[id]), count(Count)
{
}
DLL_EXPORT std::ostream & operator<<(std::ostream & str, const CStackInstance & sth)
{
if(!sth.valid(true))
str << "an invalid stack!";
str << "stack with " << sth.count << " of ";
if(sth.type)
str << sth.type->namePl;
else
str << sth.idRand;
return str;
2010-04-02 05:23:22 +03:00
}