bool CCreatureSet::canBeMergedWith(const CCreatureSet &cs, bool allowMergingStacks) const { if(!allowMergingStacks) { int freeSlots = stacksCount() - GameConstants::ARMY_SIZE; std::set<const CCreature*> cresToAdd; for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++) { SlotID dest = getSlotFor(i->second->type); if(!dest.validSlot() || hasStackAtSlot(dest)) cresToAdd.insert(i->second->type); } return cresToAdd.size() <= freeSlots; } else { CCreatureSet cres; //get types of creatures that need their own slot for(TSlots::const_iterator i = cs.stacks.begin(); i != cs.stacks.end(); i++) cres.addToSlot(i->first, i->second->type->idNumber, 1, true); SlotID j; for(TSlots::const_iterator i = stacks.begin(); i != stacks.end(); i++) { if ((j = cres.getSlotFor(i->second->type)).validSlot()) cres.addToSlot(j, i->second->type->idNumber, 1, true); //merge if possible else return false; //no place found } return true; //all stacks found their slots } }
void CGHeroInstance::initHero() { assert(validTypes(true)); if(!type) type = VLC->heroh->heroes[subID]; if (ID == Obj::HERO) appearance = VLC->objtypeh->getHandlerFor(Obj::HERO, type->heroClass->id)->getTemplates().front(); if(!vstd::contains(spells, SpellID::PRESET)) //hero starts with a spell { for(auto spellID : type->spells) spells.insert(spellID); } else //remove placeholder spells -= SpellID::PRESET; if(!getArt(ArtifactPosition::MACH4) && !getArt(ArtifactPosition::SPELLBOOK) && type->haveSpellBook) //no catapult means we haven't read pre-existent set -> use default rules for spellbook putArtifact(ArtifactPosition::SPELLBOOK, CArtifactInstance::createNewArtifactInstance(0)); if(!getArt(ArtifactPosition::MACH4)) putArtifact(ArtifactPosition::MACH4, CArtifactInstance::createNewArtifactInstance(3)); //everyone has a catapult if(portrait < 0 || portrait == 255) portrait = type->imageIndex; if(!hasBonus(Selector::sourceType(Bonus::HERO_BASE_SKILL))) { for(int g=0; g<GameConstants::PRIMARY_SKILLS; ++g) { pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(g), type->heroClass->primarySkillInitial[g]); } } if(secSkills.size() == 1 && secSkills[0] == std::pair<SecondarySkill,ui8>(SecondarySkill::DEFAULT, -1)) //set secondary skills to default secSkills = type->secSkillsInit; if (!name.length()) name = type->name; if (sex == 0xFF)//sex is default sex = type->sex; setFormation(false); if (!stacksCount()) //standard army//initial army { initArmy(); } assert(validTypes()); level = 1; if(exp == 0xffffffff) { initExp(); } else { levelUpAutomatically(); } if (VLC->modh->modules.COMMANDERS && !commander) { commander = new CCommanderInstance(type->heroClass->commander->idNumber); commander->setArmyObj (castToArmyObj()); //TODO: separate function for setting commanders commander->giveStackExp (exp); //after our exp is set } if (mana < 0) mana = manaLimit(); }