2014-06-23 20:58:19 +03:00
|
|
|
/*
|
2014-06-05 20:26:50 +03:00
|
|
|
* CGPandoraBox.cpp, part of VCMI engine
|
2014-06-05 19:52:14 +03:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "CGPandoraBox.h"
|
|
|
|
|
2014-06-05 20:26:50 +03:00
|
|
|
#include "../NetPacks.h"
|
2014-06-05 23:51:24 +03:00
|
|
|
#include "../CSoundBase.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2015-02-02 10:25:26 +02:00
|
|
|
#include "../spells/CSpellHandler.h"
|
2014-06-25 17:11:07 +03:00
|
|
|
#include "../StartInfo.h"
|
|
|
|
#include "../IGameCallback.h"
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
///helpers
|
|
|
|
static void showInfoDialog(const PlayerColor playerID, const ui32 txtID, const ui16 soundID)
|
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.soundID = soundID;
|
|
|
|
iw.player = playerID;
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT,txtID);
|
|
|
|
IObjectInterface::cb->sendAndApply(&iw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showInfoDialog(const CGHeroInstance* h, const ui32 txtID, const ui16 soundID)
|
|
|
|
{
|
|
|
|
const PlayerColor playerID = h->getOwner();
|
|
|
|
showInfoDialog(playerID,txtID,soundID);
|
|
|
|
}
|
|
|
|
|
2016-09-09 19:30:36 +02:00
|
|
|
void CGPandoraBox::initObj(CRandomGenerator & rand)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
blockVisit = (ID==Obj::PANDORAS_BOX); //block only if it's really pandora's box (events also derive from that class)
|
|
|
|
hasGuardians = stacks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
BlockingDialog bd (true, false);
|
|
|
|
bd.player = h->getOwner();
|
|
|
|
bd.soundID = soundBase::QUEST;
|
|
|
|
bd.text.addTxt (MetaString::ADVOB_TXT, 14);
|
|
|
|
cb->showBlockingDialog (&bd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::giveContentsUpToExp(const CGHeroInstance *h) const
|
|
|
|
{
|
2015-11-28 22:03:26 +02:00
|
|
|
afterSuccessfulVisit();
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->getOwner();
|
|
|
|
|
|
|
|
bool changesPrimSkill = false;
|
|
|
|
for (auto & elem : primskills)
|
|
|
|
{
|
|
|
|
if(elem)
|
|
|
|
{
|
|
|
|
changesPrimSkill = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-09 12:38:54 +02:00
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
std::vector<std::pair<SecondarySkill, ui8>> unpossessedAbilities; //ability + ability level
|
|
|
|
int abilitiesRequiringSlot = 0;
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
//filter out unnecessary secondary skills
|
|
|
|
for (int i = 0; i < abilities.size(); i++)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2016-09-20 12:40:58 +02:00
|
|
|
int curLev = h->getSecSkillLevel(abilities[i]);
|
|
|
|
bool abilityCanUseSlot = !curLev && ((h->secSkills.size() + abilitiesRequiringSlot) < GameConstants::SKILL_PER_HERO); //limit new abilities to number of slots
|
2016-09-19 20:05:57 +02:00
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
if (abilityCanUseSlot)
|
|
|
|
abilitiesRequiringSlot++;
|
2016-09-19 20:05:57 +02:00
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
if ((curLev && curLev < abilityLevels[i]) || abilityCanUseSlot)
|
|
|
|
{
|
|
|
|
unpossessedAbilities.push_back({ abilities[i], abilityLevels[i] });
|
2016-09-19 20:05:57 +02:00
|
|
|
}
|
2016-09-20 12:40:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(gainedExp || changesPrimSkill || unpossessedAbilities.size())
|
|
|
|
{
|
2014-06-05 19:52:14 +03:00
|
|
|
TExpType expVal = h->calculateXp(gainedExp);
|
|
|
|
//getText(iw,afterBattle,175,h); //wtf?
|
2016-09-20 12:40:58 +02:00
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 175); //%s learns something
|
|
|
|
iw.text.addReplacement(h->name);
|
2016-10-09 12:38:54 +02:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
if(expVal)
|
|
|
|
iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
|
2016-10-09 12:38:54 +02:00
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
for(int i=0; i<primskills.size(); i++)
|
|
|
|
if(primskills[i])
|
|
|
|
iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
|
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
for(auto abilityData : unpossessedAbilities)
|
|
|
|
iw.components.push_back(Component(Component::SEC_SKILL, abilityData.first, abilityData.second, 0));
|
2016-10-09 12:38:54 +02:00
|
|
|
|
2016-09-20 12:40:58 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
//give sec skills
|
2016-09-20 12:40:58 +02:00
|
|
|
for (auto abilityData : unpossessedAbilities)
|
|
|
|
cb->changeSecSkill(h, abilityData.first, abilityData.second, true);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
2016-09-19 20:05:57 +02:00
|
|
|
assert(h->secSkills.size() <= GameConstants::SKILL_PER_HERO);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
//give prim skills
|
|
|
|
for(int i=0; i<primskills.size(); i++)
|
|
|
|
if(primskills[i])
|
|
|
|
cb->changePrimSkill(h,static_cast<PrimarySkill::PrimarySkill>(i),primskills[i],false);
|
|
|
|
|
|
|
|
assert(!cb->isVisitCoveredByAnotherQuery(this, h));
|
|
|
|
|
|
|
|
//give exp
|
|
|
|
if(expVal)
|
|
|
|
cb->changePrimSkill(h, PrimarySkill::EXPERIENCE, expVal, false);
|
|
|
|
}
|
2016-09-20 12:40:58 +02:00
|
|
|
//else { } //TODO:Create information that box was empty for now, and deliver to CGPandoraBox::giveContentsAfterExp or refactor
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
if(!cb->isVisitCoveredByAnotherQuery(this, h))
|
|
|
|
giveContentsAfterExp(h);
|
|
|
|
//Otherwise continuation occurs via post-level-up callback.
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::giveContentsAfterExp(const CGHeroInstance *h) const
|
|
|
|
{
|
|
|
|
bool hadGuardians = hasGuardians; //copy, because flag will be emptied after issuing first post-battle message
|
|
|
|
|
|
|
|
std::string msg = message; //in case box is removed in the meantime
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->getOwner();
|
|
|
|
|
2016-06-06 09:30:45 +02:00
|
|
|
//TODO: reuse this code for Scholar skill
|
2014-06-05 19:52:14 +03:00
|
|
|
if(spells.size())
|
|
|
|
{
|
2016-10-09 12:38:54 +02:00
|
|
|
std::set<SpellID> spellsToGive;
|
2016-06-06 09:30:45 +02:00
|
|
|
|
|
|
|
auto i = spells.cbegin();
|
|
|
|
while (i != spells.cend())
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2016-06-06 09:30:45 +02:00
|
|
|
iw.components.clear();
|
|
|
|
iw.text.clear();
|
|
|
|
spellsToGive.clear();
|
|
|
|
|
|
|
|
for (; i != spells.cend(); i++)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2016-10-09 12:38:54 +02:00
|
|
|
const CSpell * sp = (*i).toSpell();
|
|
|
|
if(h->canLearnSpell(sp))
|
2016-06-06 09:30:45 +02:00
|
|
|
{
|
|
|
|
iw.components.push_back(Component(Component::SPELL, *i, 0, 0));
|
|
|
|
spellsToGive.insert(*i);
|
|
|
|
}
|
2016-10-09 12:38:54 +02:00
|
|
|
if(spellsToGive.size() == 8) //display up to 8 spells at once
|
2016-06-06 09:30:45 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!spellsToGive.empty())
|
|
|
|
{
|
|
|
|
if (spellsToGive.size() > 1)
|
|
|
|
{
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 188); //%s learns spells
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 184); //%s learns a spell
|
|
|
|
}
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
cb->changeSpells(h, true, spellsToGive);
|
|
|
|
cb->showInfoDialog(&iw);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(manaDiff)
|
|
|
|
{
|
|
|
|
getText(iw,hadGuardians,manaDiff,176,177,h);
|
|
|
|
iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
cb->setManaPoints(h->id, h->mana + manaDiff);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(moraleDiff)
|
|
|
|
{
|
|
|
|
getText(iw,hadGuardians,moraleDiff,178,179,h);
|
|
|
|
iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
GiveBonus gb;
|
|
|
|
gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id.getNum(),"");
|
|
|
|
gb.id = h->id.getNum();
|
|
|
|
cb->giveHeroBonus(&gb);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(luckDiff)
|
|
|
|
{
|
|
|
|
getText(iw,hadGuardians,luckDiff,180,181,h);
|
|
|
|
iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
GiveBonus gb;
|
|
|
|
gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id.getNum(),"");
|
|
|
|
gb.id = h->id.getNum();
|
|
|
|
cb->giveHeroBonus(&gb);
|
|
|
|
}
|
|
|
|
|
|
|
|
iw.components.clear();
|
|
|
|
iw.text.clear();
|
|
|
|
for(int i=0; i<resources.size(); i++)
|
|
|
|
{
|
|
|
|
if(resources[i] < 0)
|
|
|
|
iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
|
|
|
|
}
|
|
|
|
if(iw.components.size())
|
|
|
|
{
|
|
|
|
getText(iw,hadGuardians,182,h);
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
|
|
|
|
iw.components.clear();
|
|
|
|
iw.text.clear();
|
|
|
|
for(int i=0; i<resources.size(); i++)
|
|
|
|
{
|
|
|
|
if(resources[i] > 0)
|
|
|
|
iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
|
|
|
|
}
|
|
|
|
if(iw.components.size())
|
|
|
|
{
|
|
|
|
getText(iw,hadGuardians,183,h);
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
|
|
|
|
iw.components.clear();
|
|
|
|
// getText(iw,afterBattle,183,h);
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
for(auto & elem : artifacts)
|
|
|
|
{
|
|
|
|
iw.components.push_back(Component(Component::ARTIFACT,elem,0,0));
|
|
|
|
if(iw.components.size() >= 14)
|
|
|
|
{
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
iw.components.clear();
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure - once more?
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(iw.components.size())
|
|
|
|
{
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
|
2016-11-26 14:14:43 +02:00
|
|
|
cb->giveResources(h->getOwner(), resources);
|
2014-06-05 19:52:14 +03:00
|
|
|
|
|
|
|
for(auto & elem : artifacts)
|
|
|
|
cb->giveHeroNewArtifact(h, VLC->arth->artifacts[elem],ArtifactPosition::FIRST_AVAILABLE);
|
|
|
|
|
|
|
|
iw.components.clear();
|
|
|
|
iw.text.clear();
|
|
|
|
|
2015-12-24 20:30:57 +02:00
|
|
|
if(creatures.stacksCount())
|
2014-06-05 19:52:14 +03:00
|
|
|
{ //this part is taken straight from creature bank
|
|
|
|
MetaString loot;
|
|
|
|
for(auto & elem : creatures.Slots())
|
|
|
|
{ //build list of joined creatures
|
|
|
|
iw.components.push_back(Component(*elem.second));
|
|
|
|
loot << "%s";
|
|
|
|
loot.addReplacement(*elem.second);
|
|
|
|
}
|
|
|
|
|
2015-12-24 20:30:57 +02:00
|
|
|
if(creatures.stacksCount() == 1 && creatures.Slots().begin()->second->count == 1)
|
2014-06-05 19:52:14 +03:00
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 185);
|
|
|
|
else
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 186);
|
|
|
|
|
|
|
|
iw.text.addReplacement(loot.buildList());
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
|
|
|
|
cb->showInfoDialog(&iw);
|
2016-02-17 10:28:54 +02:00
|
|
|
cb->giveCreatures(this, h, creatures, false);
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
if(!hasGuardians && msg.size())
|
|
|
|
{
|
|
|
|
iw.text << msg;
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
|
|
|
|
{
|
|
|
|
if(afterBattle || !message.size())
|
|
|
|
{
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iw.text << message;
|
|
|
|
afterBattle = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
|
|
|
|
{
|
|
|
|
iw.components.clear();
|
|
|
|
iw.text.clear();
|
|
|
|
if(afterBattle || !message.size())
|
|
|
|
{
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
|
|
|
|
iw.text.addReplacement(h->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iw.text << message;
|
|
|
|
afterBattle = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
|
|
|
|
{
|
2016-01-26 07:41:09 +02:00
|
|
|
if(result.winner == 0)
|
|
|
|
{
|
|
|
|
giveContentsUpToExp(hero);
|
|
|
|
}
|
2014-06-05 19:52:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
|
|
|
|
{
|
2015-12-24 20:30:57 +02:00
|
|
|
if(answer)
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
2015-12-24 20:30:57 +02:00
|
|
|
if(stacksCount() > 0) //if pandora's box is protected by army
|
2014-06-05 19:52:14 +03:00
|
|
|
{
|
|
|
|
showInfoDialog(hero,16,0);
|
|
|
|
cb->startBattleI(hero, this); //grants things after battle
|
|
|
|
}
|
2015-12-24 20:30:57 +02:00
|
|
|
else if(message.size() == 0 && resources.size() == 0
|
2014-06-05 19:52:14 +03:00
|
|
|
&& primskills.size() == 0 && abilities.size() == 0
|
2015-12-24 20:30:57 +02:00
|
|
|
&& abilityLevels.size() == 0 && artifacts.size() == 0
|
|
|
|
&& spells.size() == 0 && creatures.stacksCount() > 0
|
2014-06-05 19:52:14 +03:00
|
|
|
&& gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
|
|
|
|
{
|
|
|
|
showInfoDialog(hero,15,0);
|
|
|
|
cb->removeObject(this);
|
|
|
|
}
|
|
|
|
else //if it gives something without battle
|
|
|
|
{
|
|
|
|
giveContentsUpToExp(hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGPandoraBox::heroLevelUpDone(const CGHeroInstance *hero) const
|
|
|
|
{
|
|
|
|
giveContentsAfterExp(hero);
|
|
|
|
}
|
|
|
|
|
2015-11-28 22:03:26 +02:00
|
|
|
void CGPandoraBox::afterSuccessfulVisit() const
|
|
|
|
{
|
|
|
|
cb->removeAfterVisit(this);
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:52:14 +03:00
|
|
|
void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
|
|
|
|
{
|
|
|
|
if(!(availableFor & (1 << h->tempOwner.getNum())))
|
|
|
|
return;
|
|
|
|
if(cb->getPlayerSettings(h->tempOwner)->playerID)
|
|
|
|
{
|
|
|
|
if(humanActivate)
|
|
|
|
activated(h);
|
|
|
|
}
|
|
|
|
else if(computerActivate)
|
|
|
|
activated(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGEvent::activated( const CGHeroInstance * h ) const
|
|
|
|
{
|
|
|
|
if(stacksCount() > 0)
|
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
|
|
|
if(message.size())
|
|
|
|
iw.text << message;
|
|
|
|
else
|
|
|
|
iw.text.addTxt(MetaString::ADVOB_TXT, 16);
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
cb->startBattleI(h, this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
giveContentsUpToExp(h);
|
|
|
|
}
|
|
|
|
}
|
2015-11-28 22:03:26 +02:00
|
|
|
|
|
|
|
void CGEvent::afterSuccessfulVisit() const
|
|
|
|
{
|
|
|
|
if(removeAfterVisit)
|
|
|
|
{
|
|
|
|
cb->removeAfterVisit(this);
|
|
|
|
}
|
|
|
|
else if(hasGuardians)
|
|
|
|
hasGuardians = false;
|
|
|
|
}
|