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>
|
2010-05-02 21:20:26 +03:00
|
|
|
#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
|
|
|
|
{
|
|
|
|
TSlots::const_iterator i = slots.find(slot);
|
|
|
|
if (i != slots.end())
|
|
|
|
return i->second;
|
|
|
|
else
|
|
|
|
throw std::string("That slot is empty!");
|
|
|
|
}
|
|
|
|
|
|
|
|
const CCreature* CCreatureSet::getCreature(TSlot slot) const /*workaround of map issue */
|
|
|
|
{
|
|
|
|
TSlots::const_iterator i = slots.find(slot);
|
|
|
|
if (i != slots.end())
|
|
|
|
return i->second.type;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCreatureSet::setCreature(TSlot slot, TCreature type, TQuantity quantity) /*slots 0 to 6 */
|
|
|
|
{
|
|
|
|
slots[slot] = CStackInstance(type, quantity); //brutal force
|
|
|
|
if (quantity == 0)
|
|
|
|
slots.erase(slot);
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
if (slots.size() > ARMY_SIZE)
|
2010-04-02 05:23:22 +03:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSlot CCreatureSet::getSlotFor(TCreature creature, ui32 slotsAmount/*=7*/) const /*returns -1 if no slot available */
|
|
|
|
{
|
|
|
|
for(TSlots::const_iterator i=slots.begin(); i!=slots.end(); ++i)
|
|
|
|
{
|
|
|
|
if(i->second.type->idNumber == creature)
|
|
|
|
{
|
|
|
|
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::getAmount(TSlot slot) const
|
|
|
|
{
|
|
|
|
TSlots::const_iterator i = slots.find(slot);
|
|
|
|
if (i != slots.end())
|
|
|
|
return i->second.count;
|
|
|
|
else
|
|
|
|
return 0; //TODO? consider issuing a warning
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
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))
|
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
slots.erase(i);
|
|
|
|
sweep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
void CCreatureSet::addToSlot(TSlot slot, TCreature cre, TQuantity count, bool allowMerging/* = true*/)
|
2010-04-02 05:23:22 +03:00
|
|
|
{
|
|
|
|
assert(slot >= 0);
|
2010-05-02 21:20:26 +03:00
|
|
|
const CCreature *c = VLC->creh->creatures[cre];
|
2010-11-09 13:27:58 +02:00
|
|
|
assert(!vstd::contains(slots, slot) || (slots[slot].type == c && allowMerging)); //that slot was empty or contained same type creature
|
2010-04-02 05:23:22 +03:00
|
|
|
slots[slot].type = c;
|
|
|
|
slots[slot].count += count;
|
2010-05-02 21:20:26 +03:00
|
|
|
|
|
|
|
//TODO
|
|
|
|
const CArmedInstance *armedObj = dynamic_cast<const CArmedInstance *>(this);
|
|
|
|
if(armedObj && !slots[slot].armyObj)
|
|
|
|
slots[slot].armyObj = armedObj;
|
2010-04-02 05:23:22 +03:00
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
void CCreatureSet::addToSlot(TSlot slot, const CStackInstance &stack, bool allowMerging/* = true*/)
|
2010-04-02 05:23:22 +03:00
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
assert(stack.type == VLC->creh->creatures[stack.type->idNumber]);
|
|
|
|
addToSlot(slot, stack.type->idNumber, stack.count, allowMerging );
|
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)
|
|
|
|
{
|
|
|
|
bool isRand = (i->second.idRand != -1);
|
|
|
|
if(!isRand)
|
|
|
|
{
|
|
|
|
assert(i->second.type);
|
2010-05-02 21:20:26 +03:00
|
|
|
assert(i->second.type == VLC->creh->creatures[i->second.type->idNumber]);
|
2010-04-02 05:23:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(allowUnrandomized);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
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 * getAmount(slot);
|
|
|
|
}
|
|
|
|
std::string CCreatureSet::getRoughAmount (TSlot slot) const
|
|
|
|
{
|
|
|
|
return VLC->generaltexth->arraytxt[174 + 3*CCreature::getQuantityID(getAmount(slot))];
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCreatureSet::stacksCount() const
|
|
|
|
{
|
|
|
|
return slots.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureSet::addStack(TSlot slot, const CStackInstance &stack)
|
|
|
|
{
|
|
|
|
addToSlot(slot, stack, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureSet::setFormation(bool tight)
|
|
|
|
{
|
|
|
|
formation = tight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureSet::setStackCount(TSlot slot, TQuantity count)
|
|
|
|
{
|
|
|
|
assert(vstd::contains(slots, slot));
|
2010-05-26 12:47:53 +03:00
|
|
|
assert(count > 0);
|
2010-05-02 21:20:26 +03:00
|
|
|
slots[slot].count = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureSet::clear()
|
|
|
|
{
|
|
|
|
slots.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
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)
|
|
|
|
return i->first;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-02 05:23:22 +03:00
|
|
|
CStackInstance::CStackInstance()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2010-05-02 21:20:26 +03:00
|
|
|
CStackInstance::CStackInstance(TCreature id, TQuantity Count, const CArmedInstance *ArmyObj)
|
2010-04-02 05:23:22 +03:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
setType(id);
|
|
|
|
count = Count;
|
2010-05-02 21:20:26 +03:00
|
|
|
armyObj = ArmyObj;
|
2010-04-02 05:23:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CStackInstance::CStackInstance(const CCreature *cre, TQuantity Count)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
type = cre;
|
|
|
|
count = Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStackInstance::init()
|
|
|
|
{
|
|
|
|
experience = 0;
|
|
|
|
count = 0;
|
|
|
|
type = NULL;
|
|
|
|
idRand = -1;
|
2010-05-02 21:20:26 +03:00
|
|
|
armyObj = NULL;
|
2010-07-12 13:20:25 +03:00
|
|
|
nodeType = STACK;
|
2010-04-02 05:23:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CStackInstance::getQuantityID() const
|
|
|
|
{
|
|
|
|
return CCreature::getQuantityID(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStackInstance::setType(int creID)
|
|
|
|
{
|
2010-05-02 21:20:26 +03:00
|
|
|
type = VLC->creh->creatures[creID];
|
|
|
|
}
|
|
|
|
|
2010-11-13 22:26:15 +02:00
|
|
|
// 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);
|
|
|
|
// }
|
2010-07-16 09:18:41 +03:00
|
|
|
|
2010-07-31 03:26:34 +03:00
|
|
|
std::string CStackInstance::getQuantityTXT(bool capitalized /*= true*/) const
|
|
|
|
{
|
|
|
|
return VLC->generaltexth->arraytxt[174 + getQuantityID()*3 + 2 - capitalized];
|
2010-04-02 05:23:22 +03:00
|
|
|
}
|