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

1349 lines
35 KiB
C++
Raw Normal View History

#include "StdInc.h"
2009-05-20 13:08:56 +03:00
#include "NetPacks.h"
#include "CGeneralTextHandler.h"
#include "CDefObjInfoHandler.h"
#include "CArtHandler.h"
#include "CHeroHandler.h"
#include "CObjectHandler.h"
2009-05-20 13:08:56 +03:00
#include "VCMI_Lib.h"
#include "map.h"
#include "CSpellHandler.h"
#include "CCreatureHandler.h"
#include "CGameState.h"
#include "BattleState.h"
2009-03-07 17:55:56 +02:00
#undef min
#undef max
/*
* NetPacksLib.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
*
*/
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
DLL_LINKAGE void SetResource::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
assert(player < GameConstants::PLAYER_LIMIT);
vstd::amax(val, 0); //new value must be >= 0
gs->getPlayer(player)->resources[resid] = val;
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetResources::applyGs( CGameState *gs )
{
assert(player < GameConstants::PLAYER_LIMIT);
gs->getPlayer(player)->resources = res;
}
2009-03-07 17:55:56 +02:00
DLL_LINKAGE void SetPrimSkill::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *hero = gs->getHero(id);
assert(hero);
2009-03-07 17:55:56 +02:00
if(which <4)
{
Bonus *skill = hero->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && Selector::subtype(which) && Selector::sourceType(Bonus::HERO_BASE_SKILL));
assert(skill);
2009-03-07 17:55:56 +02:00
if(abs)
skill->val = val;
2009-03-07 17:55:56 +02:00
else
skill->val += val;
2009-03-07 17:55:56 +02:00
}
else if(which == 4) //XP
{
if(abs)
hero->exp = val;
else
hero->exp += val;
}
}
DLL_LINKAGE void SetSecSkill::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *hero = gs->getHero(id);
hero->setSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(which), val, abs);
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void HeroVisitCastle::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *h = gs->getHero(hid);
CGTownInstance *t = gs->getTown(tid);
2009-03-07 17:55:56 +02:00
if(start())
t->setVisitingHero(h);
2009-03-07 17:55:56 +02:00
else
t->setVisitingHero(NULL);
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void ChangeSpells::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *hero = gs->getHero(hid);
if(learn)
BOOST_FOREACH(ui32 sid, spells)
2011-01-22 05:43:20 +02:00
hero->spells.insert(sid);
2009-03-07 17:55:56 +02:00
else
BOOST_FOREACH(ui32 sid, spells)
2011-01-22 05:43:20 +02:00
hero->spells.erase(sid);
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetMana::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *hero = gs->getHero(hid);
vstd::amax(val, 0); //not less than 0
2009-03-07 17:55:56 +02:00
hero->mana = val;
}
DLL_LINKAGE void SetMovePoints::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *hero = gs->getHero(hid);
hero->movement = val;
}
DLL_LINKAGE void FoWChange::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
TeamState * team = gs->getPlayerTeam(player);
BOOST_FOREACH(int3 t, tiles)
team->fogOfWarMap[t.x][t.y][t.z] = mode;
2010-06-06 09:05:39 +03:00
if (mode == 0) //do not hide too much
{
boost::unordered_set<int3, ShashInt3> tilesRevealed;
2010-06-06 09:05:39 +03:00
for (size_t i = 0; i < gs->map->objects.size(); i++)
{
if (gs->map->objects[i])
2010-06-06 09:05:39 +03:00
{
switch(gs->map->objects[i]->ID)
{
case 34://hero
case 53://mine
case 98://town
case 220:
if(vstd::contains(team->players, gs->map->objects[i]->tempOwner)) //check owned observators
gs->map->objects[i]->getSightTiles(tilesRevealed);
2010-06-06 09:05:39 +03:00
break;
}
}
}
BOOST_FOREACH(int3 t, tilesRevealed) //probably not the most optimal solution ever
team->fogOfWarMap[t.x][t.y][t.z] = 1;
2010-06-06 09:05:39 +03:00
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetAvailableHeroes::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
PlayerState *p = gs->getPlayer(player);
p->availableHeroes.clear();
2009-03-07 17:55:56 +02:00
for (int i = 0; i < GameConstants::AVAILABLE_HEROES_PER_PLAYER; i++)
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *h = (hid[i]>=0 ? (CGHeroInstance*)gs->hpool.heroesPool[hid[i]] : NULL);
if(h && army[i])
h->setToArmy(army[i]);
p->availableHeroes.push_back(h);
2009-03-07 17:55:56 +02:00
}
}
DLL_LINKAGE void GiveBonus::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CBonusSystemNode *cbsn = NULL;
switch(who)
{
case HERO:
cbsn = gs->getHero(id);
break;
case PLAYER:
cbsn = gs->getPlayer(id);
break;
case TOWN:
cbsn = gs->getTown(id);
break;
}
assert(cbsn);
Bonus *b = new Bonus(bonus);
cbsn->addNewBonus(b);
std::string &descr = b->description;
if(!bdescr.message.size()
&& bonus.source == Bonus::OBJECT
&& (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE)
&& gs->map->objects[bonus.sid]->ID == GameConstants::EVENTI_TYPE) //it's morale/luck bonus from an event without description
{
descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
}
else
{
bdescr.toString(descr);
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void ChangeObjPos::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGObjectInstance *obj = gs->map->objects[objid];
if(!obj)
{
tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
return;
}
gs->map->removeBlockVisTiles(obj);
obj->pos = nPos;
gs->map->addBlockVisTiles(obj);
}
DLL_LINKAGE void PlayerEndsGame::applyGs( CGameState *gs )
{
PlayerState *p = gs->getPlayer(player);
p->status = victory ? 2 : 1;
}
DLL_LINKAGE void RemoveBonus::applyGs( CGameState *gs )
{
CBonusSystemNode *node;
if (who == HERO)
node = gs->getHero(whoID);
else
node = gs->getPlayer(whoID);
BonusList &bonuses = node->getBonusList();
for (int i = 0; i < bonuses.size(); i++)
{
Bonus *b = bonuses[i];
if(b->source == source && b->sid == id)
{
bonus = *b; //backup bonus (to show to interfaces later)
bonuses.erase(i);
break;
}
}
}
DLL_LINKAGE void RemoveObject::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGObjectInstance *obj = gs->map->objects[id];
2010-02-11 17:51:26 +02:00
//unblock tiles
if(obj->defInfo)
{
gs->map->removeBlockVisTiles(obj);
}
if(obj->ID==GameConstants::HEROI_TYPE)
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
PlayerState *p = gs->getPlayer(h->tempOwner);
gs->map->heroes -= h;
p->heroes -= h;
h->detachFrom(h->whereShouldBeAttached(gs));
h->tempOwner = 255; //no one owns beaten hero
if(h->visitedTown)
2009-03-07 17:55:56 +02:00
{
if(h->inTownGarrison)
h->visitedTown->garrisonHero = NULL;
2009-03-07 17:55:56 +02:00
else
h->visitedTown->visitingHero = NULL;
2009-03-07 17:55:56 +02:00
h->visitedTown = NULL;
}
//return hero to the pool, so he may reappear in tavern
gs->hpool.heroesPool[h->subID] = h;
if(!vstd::contains(gs->hpool.pavailable, h->subID))
gs->hpool.pavailable[h->subID] = 0xff;
2011-01-18 20:56:14 +02:00
gs->map->objects[id] = NULL;
2011-01-18 20:56:14 +02:00
return;
2009-03-07 17:55:56 +02:00
}
// else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
// {
// CGCreature *cre = static_cast<CGCreature*>(obj);
// gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
// }
gs->map->objects[id].dellNull();
2009-03-07 17:55:56 +02:00
}
static int getDir(int3 src, int3 dst)
{
int ret = -1;
if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
{
ret = 1;
}
else if(dst.x == src.x && dst.y+1 == src.y) //t
{
ret = 2;
}
else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
{
ret = 3;
}
else if(dst.x-1 == src.x && dst.y == src.y) //r
{
ret = 4;
}
else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
{
ret = 5;
}
else if(dst.x == src.x && dst.y-1 == src.y) //b
{
ret = 6;
}
else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
{
ret = 7;
}
else if(dst.x+1 == src.x && dst.y == src.y) //l
{
ret = 8;
}
return ret;
}
2009-03-07 17:55:56 +02:00
void TryMoveHero::applyGs( CGameState *gs )
{
CGHeroInstance *h = gs->getHero(id);
h->movement = movePoints;
if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end) {
h->moveDir = getDir(start,end);
}
if(result == EMBARK) //hero enters boat at dest tile
{
const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
2011-04-23 01:28:13 +03:00
assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == 8); //the only vis obj at dest is Boat
CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.back());
gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
h->boat = boat;
boat->hero = h;
}
else if(result == DISEMBARK) //hero leaves boat to dest tile
{
CGBoat *b = const_cast<CGBoat *>(h->boat);
b->direction = h->moveDir;
b->pos = start;
b->hero = NULL;
gs->map->addBlockVisTiles(b);
h->boat = NULL;
}
if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
2009-03-07 17:55:56 +02:00
{
gs->map->removeBlockVisTiles(h);
h->pos = end;
if(CGBoat *b = const_cast<CGBoat *>(h->boat))
b->pos = end;
2009-03-07 17:55:56 +02:00
gs->map->addBlockVisTiles(h);
}
2009-03-07 17:55:56 +02:00
BOOST_FOREACH(int3 t, fowRevealed)
gs->getPlayerTeam(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void NewStructures::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGTownInstance *t = gs->getTown(tid);
2009-03-07 17:55:56 +02:00
BOOST_FOREACH(si32 id,bid)
{
2009-03-07 17:55:56 +02:00
t->builtBuildings.insert(id);
}
2009-03-07 17:55:56 +02:00
t->builded = builded;
t->recreateBuildingsBonuses();
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void RazeStructures::applyGs( CGameState *gs )
{
CGTownInstance *t = gs->getTown(tid);
BOOST_FOREACH(si32 id,bid)
{
t->builtBuildings.erase(id);
}
t->destroyed = destroyed; //yeaha
t->recreateBuildingsBonuses();
}
DLL_LINKAGE void SetAvailableCreatures::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid].get());
assert(dw);
dw->creatures = creatures;
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetHeroesInTown::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGTownInstance *t = gs->getTown(tid);
CGHeroInstance *v = gs->getHero(visiting),
*g = gs->getHero(garrison);
bool newVisitorComesFromGarrison = v && v == t->garrisonHero;
bool newGarrisonComesFromVisiting = g && g == t->visitingHero;
if(newVisitorComesFromGarrison)
t->setGarrisonedHero(NULL);
if(newGarrisonComesFromVisiting)
t->setVisitingHero(NULL);
if(!newGarrisonComesFromVisiting || v)
t->setVisitingHero(v);
if(!newVisitorComesFromGarrison || g)
t->setGarrisonedHero(g);
2009-03-07 17:55:56 +02:00
if(v)
{
gs->map->addBlockVisTiles(v);
}
if(g)
{
gs->map->removeBlockVisTiles(g);
}
}
DLL_LINKAGE void HeroRecruited::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
assert(vstd::contains(gs->hpool.heroesPool, hid));
2009-03-07 17:55:56 +02:00
CGHeroInstance *h = gs->hpool.heroesPool[hid];
CGTownInstance *t = gs->getTown(tid);
PlayerState *p = gs->getPlayer(player);
2009-03-07 17:55:56 +02:00
h->setOwner(player);
h->pos = tile;
h->movement = h->maxMovePoints(true);
gs->hpool.heroesPool.erase(hid);
if(h->id < 0)
{
h->id = gs->map->objects.size();
gs->map->objects.push_back(h);
}
else
gs->map->objects[h->id] = h;
h->initHeroDefInfo();
gs->map->heroes.push_back(h);
p->heroes.push_back(h);
h->attachTo(p);
h->initObj();
2009-03-07 17:55:56 +02:00
gs->map->addBlockVisTiles(h);
2010-07-09 02:03:27 +03:00
if(t)
{
t->setVisitingHero(h);
2010-07-09 02:03:27 +03:00
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void GiveHero::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance *h = gs->getHero(id);
//bonus system
h->detachFrom(&gs->globalEffects);
h->attachTo(gs->getPlayer(player));
2009-03-07 17:55:56 +02:00
gs->map->removeBlockVisTiles(h,true);
h->setOwner(player);
h->movement = h->maxMovePoints(true);
h->initHeroDefInfo();
gs->map->heroes.push_back(h);
gs->getPlayer(h->getOwner())->heroes.push_back(h);
2009-03-07 17:55:56 +02:00
gs->map->addBlockVisTiles(h);
h->inTownGarrison = false;
}
DLL_LINKAGE void NewObject::applyGs( CGameState *gs )
{
CGObjectInstance *o = NULL;
switch(ID)
{
case 8:
o = new CGBoat();
break;
case 54: //probably more options will be needed
o = new CGCreature();
{
//CStackInstance hlp;
CGCreature *cre = static_cast<CGCreature*>(o);
//cre->slots[0] = hlp;
cre->notGrowingTeam = cre->neverFlees = 0;
cre->character = 2;
cre->gainedArtifact = -1;
cre->identifier = -1;
2011-06-25 17:22:19 +03:00
cre->addToSlot(0, new CStackInstance(subID, -1)); //add placeholder stack
}
break;
default:
o = new CGObjectInstance();
break;
}
o->ID = ID;
o->subID = subID;
o->pos = pos;
o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
id = o->id = gs->map->objects.size();
o->hoverName = VLC->generaltexth->names[ID];
switch(ID)
2010-02-21 17:03:30 +02:00
{
case 54: //cfreature
o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
assert(o->defInfo);
break;
case 124://hole
const TerrainTile &t = gs->map->getTile(pos);
o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
assert(o->defInfo);
break;
2010-02-21 17:03:30 +02:00
}
gs->map->objects.push_back(o);
gs->map->addBlockVisTiles(o);
o->initObj();
assert(o->defInfo);
}
DLL_LINKAGE void NewArtifact::applyGs( CGameState *gs )
{
assert(!vstd::contains(gs->map->artInstances, art));
2011-01-22 05:43:20 +02:00
gs->map->addNewArtifactInstance(art);
assert(!art->getParentNodes().size());
2011-01-18 20:56:14 +02:00
art->setType(art->artType);
if (CCombinedArtifactInstance* cart = dynamic_cast<CCombinedArtifactInstance*>(art.get()))
cart->createConstituents();
}
DLL_LINKAGE const CStackInstance * StackLocation::getStack()
{
if(!army->hasStackAtSlot(slot))
{
tlog2 << "Warning: " << army->nodeName() << " dont have a stack at slot " << slot << std::endl;
return NULL;
}
return &army->getStack(slot);
}
DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const
{
const ArtSlotInfo *s = getSlot();
2010-12-29 23:04:22 +02:00
if(s && s->artifact)
{
if(!s->locked)
return s->artifact;
else
{
tlog3 << "ArtifactLocation::getArt: That location is locked!\n";
return NULL;
}
}
return NULL;
}
DLL_LINKAGE CArtifactInstance *ArtifactLocation::getArt()
{
const ArtifactLocation *t = this;
return const_cast<CArtifactInstance*>(t->getArt());
}
DLL_LINKAGE const ArtSlotInfo *ArtifactLocation::getSlot() const
{
if (hero)
return hero->getSlot(slot);
if (stack)
return stack->getSlot(slot);
return NULL;
}
DLL_LINKAGE void ChangeStackCount::applyGs( CGameState *gs )
{
if(absoluteValue)
sl.army->setStackCount(sl.slot, count);
else
sl.army->changeStackCount(sl.slot, count);
}
DLL_LINKAGE void SetStackType::applyGs( CGameState *gs )
{
sl.army->setStackType(sl.slot, type);
}
DLL_LINKAGE void EraseStack::applyGs( CGameState *gs )
{
sl.army->eraseStack(sl.slot);
}
DLL_LINKAGE void SwapStacks::applyGs( CGameState *gs )
{
CStackInstance *s1 = sl1.army->detachStack(sl1.slot),
*s2 = sl2.army->detachStack(sl2.slot);
sl2.army->putStack(sl2.slot, s1);
sl1.army->putStack(sl1.slot, s2);
}
DLL_LINKAGE void InsertNewStack::applyGs( CGameState *gs )
{
CStackInstance *s = new CStackInstance(stack.type, stack.count);
sl.army->putStack(sl.slot, s);
}
DLL_LINKAGE void RebalanceStacks::applyGs( CGameState *gs )
{
const CCreature *srcType = src.army->getCreature(src.slot);
TQuantity srcCount = src.army->getStackCount(src.slot);
if(srcCount == count) //moving whole stack
{
if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
{
assert(c == srcType);
if (GameConstants::STACK_EXP)
{
ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
src.army->eraseStack(src.slot);
dst.army->changeStackCount(dst.slot, count);
dst.army->setStackExp(dst.slot, totalExp /(dst.army->getStackCount(dst.slot))); //mean
}
else
{
src.army->eraseStack(src.slot);
dst.army->changeStackCount(dst.slot, count);
}
}
else //move stack to an empty slot, no exp change needed
{
CStackInstance *stackDetached = src.army->detachStack(src.slot);
dst.army->putStack(dst.slot, stackDetached);
}
}
else
{
if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
{
assert(c == srcType);
if (GameConstants::STACK_EXP)
{
ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
src.army->changeStackCount(src.slot, -count);
dst.army->changeStackCount(dst.slot, count);
dst.army->setStackExp(dst.slot, totalExp /(src.army->getStackCount(src.slot) + dst.army->getStackCount(dst.slot))); //mean
}
else
{
src.army->changeStackCount(src.slot, -count);
dst.army->changeStackCount(dst.slot, count);
}
}
else //split stack to an empty slot
{
src.army->changeStackCount(src.slot, -count);
dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
if (GameConstants::STACK_EXP)
dst.army->setStackExp(dst.slot, src.army->getStackExperience(src.slot));
}
}
}
DLL_LINKAGE void PutArtifact::applyGs( CGameState *gs )
{
assert(art->canBePutAt(al));
2010-12-30 16:41:46 +02:00
al.hero->putArtifact(al.slot, art);
}
DLL_LINKAGE void EraseArtifact::applyGs( CGameState *gs )
{
CArtifactInstance *a = al.getArt();
assert(a);
a->removeFrom(al.hero, al.slot);
}
DLL_LINKAGE void MoveArtifact::applyGs( CGameState *gs )
{
CArtifactInstance *a = src.getArt();
if(dst.slot < GameConstants::BACKPACK_START)
assert(!dst.getArt());
2011-01-22 05:43:20 +02:00
a->move(src, dst);
2012-02-17 22:30:40 +03:00
//TODO what'll happen if Titan's thunder is equiped by pickin git up or the start of game?
if (a->artType->id == 135 && dst.hero && dst.slot == ArtifactPosition::RIGHT_HAND && !dst.hero->hasSpellbook()) //Titan's Thunder creates new spellbook on equip
gs->giveHeroArtifact(dst.hero, 0);
}
DLL_LINKAGE void AssembledArtifact::applyGs( CGameState *gs )
2011-01-22 05:43:20 +02:00
{
CGHeroInstance *h = al.hero;
const CArtifactInstance *transformedArt = al.getArt();
assert(transformedArt);
assert(vstd::contains(transformedArt->assemblyPossibilities(al.hero), builtArt));
CCombinedArtifactInstance *combinedArt = new CCombinedArtifactInstance(builtArt);
gs->map->addNewArtifactInstance(combinedArt);
//retrieve all constituents
2011-01-22 05:43:20 +02:00
BOOST_FOREACH(si32 constituentID, *builtArt->constituents)
{
int pos = h->getArtPos(constituentID);
assert(pos >= 0);
CArtifactInstance *constituentInstance = h->getArt(pos);
//move constituent from hero to be part of new, combined artifact
constituentInstance->removeFrom(h, pos);
combinedArt->addAsConstituent(constituentInstance, pos);
if(!vstd::contains(combinedArt->artType->possibleSlots, al.slot) && vstd::contains(combinedArt->artType->possibleSlots, pos))
al.slot = pos;
2011-01-22 05:43:20 +02:00
}
//put new combined artifacts
combinedArt->putAt(h, al.slot);
}
DLL_LINKAGE void DisassembledArtifact::applyGs( CGameState *gs )
2011-01-22 05:43:20 +02:00
{
CGHeroInstance *h = al.hero;
CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt());
assert(disassembled);
std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
disassembled->removeFrom(h, al.slot);
BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo &ci, constituents)
{
disassembled->detachFrom(ci.art);
ci.art->putAt(h, ci.slot >= 0 ? ci.slot : al.slot); //-1 is slot of main constituent -> it'll replace combined artifact in its pos
}
gs->map->eraseArtifactInstance(disassembled);
2011-01-22 05:43:20 +02:00
}
DLL_LINKAGE void HeroVisit::applyGs( CGameState *gs )
{
if(starting)
gs->ongoingVisits[hero] = obj;
else
gs->ongoingVisits.erase(hero);
}
DLL_LINKAGE void SetAvailableArtifacts::applyGs( CGameState *gs )
{
if(id >= 0)
{
if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
{
bm->artifacts = arts;
}
else
{
tlog1 << "Wrong black market id!" << std::endl;
}
}
else
{
CGTownInstance::merchantArtifacts = arts;
}
}
DLL_LINKAGE void NewTurn::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->day = day;
BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
{
CGHeroInstance *hero = gs->getHero(h.id);
hero->movement = h.move;
hero->mana = h.mana;
2009-03-07 17:55:56 +02:00
}
for(std::map<ui8, TResources>::iterator i = res.begin(); i != res.end(); i++)
{
assert(i->first < GameConstants::PLAYER_LIMIT);
gs->getPlayer(i->first)->resources = i->second;
}
2009-03-07 17:55:56 +02:00
BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
h.applyGs(gs);
gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
if(gs->getDate(1)) //new week
gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
2010-08-24 17:26:57 +03:00
//TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
2010-08-24 17:26:57 +03:00
//count days without town
for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
{
if(i->second.towns.size() || gs->day == 1)
i->second.daysWithoutCastle = 0;
else
i->second.daysWithoutCastle++;
}
BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
t->builded = 0;
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetObjectProperty::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGObjectInstance *obj = gs->map->objects[id];
if(!obj)
{
2009-03-07 17:55:56 +02:00
tlog1 << "Wrong object ID - property cannot be set!\n";
return;
}
2011-03-08 09:40:14 +02:00
CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj);
if(what == ObjProperty::OWNER && cai)
{
if(obj->ID == GameConstants::TOWNI_TYPE)
{
2011-03-08 09:40:14 +02:00
CGTownInstance *t = static_cast<CGTownInstance*>(obj);
if(t->tempOwner < GameConstants::PLAYER_LIMIT)
2011-03-08 09:40:14 +02:00
gs->getPlayer(t->tempOwner)->towns -= t;
if(val < GameConstants::PLAYER_LIMIT)
2011-03-08 09:40:14 +02:00
gs->getPlayer(val)->towns.push_back(t);
}
2011-03-08 09:40:14 +02:00
CBonusSystemNode *nodeToMove = cai->whatShouldBeAttached();
nodeToMove->detachFrom(cai->whereShouldBeAttached(gs));
obj->setProperty(what,val);
nodeToMove->attachTo(cai->whereShouldBeAttached(gs));
}
2011-03-08 09:40:14 +02:00
else //not an armed instance
{
obj->setProperty(what,val);
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void SetHoverName::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
name.toString(gs->map->objects[id]->hoverName);
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void HeroLevelUp::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CGHeroInstance* h = gs->getHero(heroid);
h->level = level;
//speciality
h->UpdateSpeciality();
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleStart::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->curB = info;
2010-12-23 02:33:48 +02:00
gs->curB->localInit();
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleNextRound::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
2009-05-12 06:35:51 +03:00
gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
2011-10-09 10:20:23 +03:00
for (int i = 0; i < 2; ++i)
{
vstd::amax(--gs->curB->enchanterCounter[i], 0);
2011-10-09 10:20:23 +03:00
}
2009-03-07 17:55:56 +02:00
gs->curB->round = round;
BOOST_FOREACH(CStack *s, gs->curB->stacks)
{
s->state -= EBattleStackState::DEFENDING;
s->state -= EBattleStackState::WAITING;
s->state -= EBattleStackState::MOVED;
s->state -= EBattleStackState::HAD_MORALE;
s->state -= EBattleStackState::FEAR;
s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
// new turn effects
s->battleTurnPassed();
2009-03-07 17:55:56 +02:00
}
}
DLL_LINKAGE void BattleSetActiveStack::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->curB->activeStack = stack;
CStack *st = gs->curB->getStack(stack);
//remove bonuses that last until when stack gets new turn
st->getBonusList().remove_if(Bonus::UntilGetsTurn);
if(vstd::contains(st->state,EBattleStackState::MOVED)) //if stack is moving second time this turn it must had a high morale bonus
st->state.insert(EBattleStackState::HAD_MORALE);
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleTriggerEffect::applyGs( CGameState *gs )
{
CStack *st = gs->curB->getStack(stackID);
switch (effect)
{
case Bonus::HP_REGENERATION:
st->firstHPleft += val;
vstd::amin (st->firstHPleft, (ui32)st->MaxHealth());
break;
case Bonus::MANA_DRAIN:
{
CGHeroInstance * h = gs->getHero(additionalInfo);
h->mana -= val;
vstd::amax(h->mana, 0);
break;
}
case Bonus::POISON:
{
Bonus * b = st->getBonus(Selector::source(Bonus::SPELL_EFFECT, 71) && Selector::type(Bonus::STACK_HEALTH));
if (b)
b->val = val;
break;
}
case Bonus::ENCHANTER:
break;
case Bonus::FEAR:
st->state.insert(EBattleStackState::FEAR);
break;
default:
tlog2 << "Unrecognized trigger effect type "<< type <<"\n";
}
}
void BattleResult::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
//stack with SUMMONED flag but coming from garrison -> most likely resurrected, needs to be removed
BOOST_FOREACH(CStack *s, gs->curB->stacks)
{
if(s->base && s->base->armyObj && vstd::contains(s->state, EBattleStackState::SUMMONED))
{
assert(&s->base->armyObj->getStack(s->slot) == s->base);
const_cast<CArmedInstance*>(s->base->armyObj)->eraseStack(s->slot);
}
}
2009-03-07 17:55:56 +02:00
for(unsigned i=0;i<gs->curB->stacks.size();i++)
delete gs->curB->stacks[i];
//remove any "until next battle" bonuses
CGHeroInstance *h;
h = gs->curB->heroes[0];
2009-03-07 17:55:56 +02:00
if(h)
h->getBonusList().remove_if(Bonus::OneBattle);
h = gs->curB->heroes[1];
2009-03-07 17:55:56 +02:00
if(h)
h->getBonusList().remove_if(Bonus::OneBattle);
2009-03-07 17:55:56 +02:00
if (GameConstants::STACK_EXP)
{
if (exp[0]) //checking local array is easier than dereferencing this crap twice
gs->curB->belligerents[0]->giveStackExp(exp[0]);
if (exp[1])
gs->curB->belligerents[1]->giveStackExp(exp[1]);
}
gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
gs->curB.dellNull();
2009-03-07 17:55:56 +02:00
}
void BattleStackMoved::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->curB->getStack(stack)->position = tilesToMove.back();
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleStackAttacked::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CStack * at = gs->curB->getStack(stackAttacked);
at->count = newAmount;
2009-03-07 17:55:56 +02:00
at->firstHPleft = newHP;
2009-03-07 17:55:56 +02:00
if(killed())
{
at->state -= EBattleStackState::ALIVE;
}
//life drain handling
for (int g=0; g<healedStacks.size(); ++g)
{
healedStacks[g].applyGs(gs);
}
if (willRebirth())
{
at->casts--;
at->state.insert(EBattleStackState::ALIVE); //hmm?
}
if (cloneKilled())
{
BattleStacksRemoved bsr; //remove body
bsr.stackIDs.insert(at->ID);
bsr.applyGs(gs);
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleAttack::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CStack *attacker = gs->curB->getStack(stackAttacking);
if(counter())
attacker->counterAttacks--;
2009-03-07 17:55:56 +02:00
if(shot())
{
//don't remove ammo if we have a working ammo cart
bool hasAmmoCart = false;
BOOST_FOREACH(const CStack * st, gs->curB->stacks)
{
2010-11-20 19:36:02 +02:00
if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
{
hasAmmoCart = true;
break;
}
}
if (!hasAmmoCart)
{
attacker->shots--;
}
}
BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
stackAttacked.applyGs(gs);
attacker->getBonusList().remove_if(Bonus::UntilAttack);
for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
{
CStack * stack = gs->curB->getStack(it->stackAttacked, false);
if (stack) //cloned stack is already gone
stack->getBonusList().remove_if(Bonus::UntilBeingAttacked);
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void StartAction::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
CStack *st = gs->curB->getStack(ba.stackNumber);
if(ba.actionType == BattleAction::END_TACTIC_PHASE)
{
gs->curB->tacticDistance = 0;
return;
}
2010-12-04 21:15:20 +02:00
if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
{
assert(st);
}
else
{
gs->curB->usedSpellsHistory[ba.side].push_back(VLC->spellh->spells[ba.additionalInfo]);
}
2009-03-07 17:55:56 +02:00
switch(ba.actionType)
{
2010-12-04 21:15:20 +02:00
case BattleAction::DEFEND:
st->state.insert(EBattleStackState::DEFENDING);
2009-03-07 17:55:56 +02:00
break;
2010-12-04 21:15:20 +02:00
case BattleAction::WAIT:
st->state.insert(EBattleStackState::WAITING);
2009-11-21 00:35:18 +02:00
return;
case BattleAction::HERO_SPELL: //no change in current stack state
return;
default: //any active stack action - attack, catapult, heal, spell...
st->state.insert(EBattleStackState::MOVED);
2009-03-07 17:55:56 +02:00
break;
}
2009-11-21 00:35:18 +02:00
if(st)
st->state -= EBattleStackState::WAITING; //if stack was waiting it has made move, so it won't be "waiting" anymore (if the action was WAIT, then we have returned)
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void BattleSpellCast::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
assert(gs->curB);
2011-10-09 14:23:24 +03:00
if (castedByHero)
2009-03-07 17:55:56 +02:00
{
2011-10-09 14:23:24 +03:00
CGHeroInstance * h = gs->curB->heroes[side];
CGHeroInstance * enemy = gs->curB->heroes[1-side];
h->mana -= spellCost;
vstd::amax(h->mana, 0);
2011-10-09 14:23:24 +03:00
if (enemy && manaGained)
enemy->mana += manaGained;
if (side >= 0 && side < 2)
{
2011-10-09 14:23:24 +03:00
gs->curB->castSpells[side]++;
}
2009-03-07 17:55:56 +02:00
}
if(id == 35 || id == 78) //dispel and dispel helpful spells
{
bool onlyHelpful = id == 78;
2009-08-04 20:05:49 +03:00
for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
{
2009-08-04 20:05:49 +03:00
CStack *s = gs->curB->getStack(*it);
if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
{
if(onlyHelpful)
s->popBonuses(Selector::positiveSpellEffects);
else
s->popBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
}
}
}
2009-03-07 17:55:56 +02:00
}
void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
{
//actualizing features vector
BOOST_FOREACH(const Bonus &fromEffect, ef)
{
BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
{
if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
{
stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
}
}
}
}
void actualizeEffect(CStack * s, const Bonus & ef)
{
BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
{
if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
{
stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
}
}
}
DLL_LINKAGE void SetStackEffect::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
int spellid = effect.begin()->sid; //effects' source ID
BOOST_FOREACH(ui32 id, stacks)
{
CStack *s = gs->curB->getStack(id);
if(s)
{
if(spellid == 47 || spellid == 80 || !s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid)))//disrupting ray or acid breath or not on the list - just add
{
BOOST_FOREACH(Bonus &fromEffect, effect)
{
s->addNewBonus( new Bonus(fromEffect));
}
}
else //just actualize
{
actualizeEffect(s, effect);
}
}
else
tlog1 << "Cannot find stack " << id << std::endl;
}
typedef std::pair<ui32, Bonus> p;
BOOST_FOREACH(p para, uniqueBonuses)
{
CStack *s = gs->curB->getStack(para.first);
if (s)
{
if (!s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid) && Selector::typeSubtype(para.second.type, para.second.subtype)))
s->addNewBonus(new Bonus(para.second));
else
actualizeEffect(s, effect);
}
else
tlog1 << "Cannot find stack " << para.first << std::endl;
}
2009-03-07 17:55:56 +02:00
}
DLL_LINKAGE void StacksInjured::applyGs( CGameState *gs )
{
BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
stackAttacked.applyGs(gs);
}
DLL_LINKAGE void StacksHealedOrResurrected::applyGs( CGameState *gs )
2009-08-04 20:05:49 +03:00
{
for(int g=0; g<healedStacks.size(); ++g)
{
2009-10-17 17:32:42 +03:00
CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
//checking if we resurrect a stack that is under a living stack
std::vector<BattleHex> access = gs->curB->getAccessibility(changedStack, true);
bool acc[GameConstants::BFIELD_SIZE];
for(int h=0; h<GameConstants::BFIELD_SIZE; ++h)
acc[h] = false;
for(int h=0; h<access.size(); ++h)
acc[access[h]] = true;
if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
changedStack->doubleWide(), changedStack->attackerOwned,
changedStack->hasBonusOfType(Bonus::FLYING), true))
return; //position is already occupied
//applying changes
2009-10-17 17:32:42 +03:00
bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
if(resurrected)
{
changedStack->state.insert(EBattleStackState::ALIVE);
2010-01-29 18:35:05 +02:00
if(healedStacks[g].lowLevelResurrection)
changedStack->state.insert(EBattleStackState::SUMMONED); //TODO: different counter for rised units
}
//int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
changedStack->count += res;
2009-10-17 17:32:42 +03:00
changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
if(changedStack->firstHPleft > changedStack->MaxHealth())
{
changedStack->firstHPleft -= changedStack->MaxHealth();
if(changedStack->baseAmount > changedStack->count)
2009-10-17 17:32:42 +03:00
{
changedStack->count += 1;
2009-10-17 17:32:42 +03:00
}
}
vstd::amin(changedStack->firstHPleft, changedStack->MaxHealth());
2009-08-04 20:05:49 +03:00
//removal of negative effects
2009-10-17 17:32:42 +03:00
if(resurrected)
2009-08-04 20:05:49 +03:00
{
// for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
// {
// if(VLC->spellh->spells[(*it)->sid]->positiveness < 0)
// {
// changedStack->bonuses.erase(it);
// }
// }
2009-08-04 20:05:49 +03:00
//removing all features from negative spells
const BonusList tmpFeatures = changedStack->getBonusList();
//changedStack->bonuses.clear();
BOOST_FOREACH(Bonus *b, tmpFeatures)
2009-08-04 20:05:49 +03:00
{
const CSpell *s = b->sourceSpell();
if(s && s->isNegative())
2009-08-04 20:05:49 +03:00
{
changedStack->removeBonus(b);
2009-08-04 20:05:49 +03:00
}
}
}
}
}
DLL_LINKAGE void ObstaclesRemoved::applyGs( CGameState *gs )
{
if(gs->curB) //if there is a battle
{
for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
{
for(int i=0; i<gs->curB->obstacles.size(); ++i)
{
if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
{
gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
break;
}
}
}
}
}
DLL_LINKAGE void CatapultAttack::applyGs( CGameState *gs )
2009-09-01 16:54:13 +03:00
{
if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
{
for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
{
gs->curB->si.wallState[it->first.first] =
std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
}
2009-09-01 16:54:13 +03:00
}
}
DLL_LINKAGE void BattleStacksRemoved::applyGs( CGameState *gs )
2009-09-05 17:10:26 +03:00
{
if(!gs->curB)
return;
for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
{
for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
{
if(gs->curB->stacks[b]->ID == *it) //if found
{
gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
break;
}
}
}
}
DLL_LINKAGE void BattleStackAdded::applyGs(CGameState *gs)
{
if (!BattleHex(pos).isValid())
{
tlog2 << "No place found for new stack!\n";
return;
}
CStackInstance *csi = new CStackInstance(creID, amount);
csi->setArmyObj(gs->curB->belligerents[attacker ? 0 : 1]);
CStack * summonedStack = gs->curB->generateNewStack(*csi, gs->curB->stacks.size(), attacker, 255, pos); //TODO: netpacks?
if (summoned)
summonedStack->state.insert(EBattleStackState::SUMMONED);
summonedStack->attachTo(csi);
summonedStack->postInit();
gs->curB->stacks.push_back(summonedStack); //the stack is not "SUMMONED", it is permanent
}
DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState *gs)
{
CStack * stack = gs->curB->getStack(stackID);
switch (which)
{
case CASTS:
{
if (absolute)
stack->casts = val;
else
stack->casts += val;
vstd::amax(stack->casts, 0);
break;
}
2011-10-09 10:20:23 +03:00
case ENCHANTER_COUNTER:
{
int side = gs->curB->whatSide(stack->owner);
if (absolute)
gs->curB->enchanterCounter[side] = val;
else
gs->curB->enchanterCounter[side] += val;
vstd::amax(gs->curB->enchanterCounter[side], 0);
2011-10-09 10:20:23 +03:00
break;
}
2011-10-17 11:24:51 +03:00
case UNBIND:
{
stack->popBonuses(Selector::type(Bonus::BIND_EFFECT));
break;
}
2012-02-10 16:13:24 +03:00
case CLONED:
{
stack->state.insert(EBattleStackState::CLONED);
break;
}
}
}
DLL_LINKAGE void YourTurn::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->currentPlayer = player;
}
DLL_LINKAGE void SetSelection::applyGs( CGameState *gs )
2009-03-07 17:55:56 +02:00
{
gs->getPlayer(player)->currentSelection = id;
}
DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack)
:id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
{
}