1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Prepare battle log for spell-cast on server side.

This commit is contained in:
AlexVinS 2016-09-10 18:23:55 +03:00
parent f8767a6380
commit 62abde6c46
13 changed files with 132 additions and 118 deletions

View File

@ -1323,12 +1323,9 @@ void CBattleInterface::spellCast( const BattleSpellCast * sc )
} }
//displaying message in console //displaying message in console
std::vector<std::string> logLines; for(const auto & line : sc->battleLog)
if(!console->addText(line.toString()))
spell.prepareBattleLog(curInt->cb.get(), sc, logLines); logGlobal->warn("Too long battle log line");
for(auto line : logLines)
console->addText(line);
waitForAnims(); waitForAnims();
//mana absorption //mana absorption

View File

@ -1243,6 +1243,11 @@ const PlayerColor CStack::getOwner() const
return owner; return owner;
} }
void CStack::getCasterName(MetaString & text) const
{
//always plural name in case of spell cast.
text.addReplacement(MetaString::CRE_PL_NAMES, type->idNumber.num);
}
bool CMP_stack::operator()( const CStack* a, const CStack* b ) bool CMP_stack::operator()( const CStack* a, const CStack* b )
{ {

View File

@ -249,6 +249,8 @@ public:
const PlayerColor getOwner() const override; const PlayerColor getOwner() const override;
void getCasterName(MetaString & text) const override;
///stack will be ghost in next battle state update ///stack will be ghost in next battle state update
void makeGhost(); void makeGhost();

View File

@ -1492,7 +1492,6 @@ struct BattleSpellCast : public CPackForClient//3009
DLL_LINKAGE void applyGs(CGameState *gs); DLL_LINKAGE void applyGs(CGameState *gs);
void applyCl(CClient *cl); void applyCl(CClient *cl);
si32 dmgToDisplay; //this amount will be displayed as amount of damage dealt by spell
ui8 side; //which hero did cast spell: 0 - attacker, 1 - defender ui8 side; //which hero did cast spell: 0 - attacker, 1 - defender
ui32 id; //id of spell ui32 id; //id of spell
ui8 skill; //caster's skill level ui8 skill; //caster's skill level
@ -1502,9 +1501,12 @@ struct BattleSpellCast : public CPackForClient//3009
std::set<ui32> affectedCres; //ids of creatures affected by this spell, generally used if spell does not set any effect (like dispel or cure) std::set<ui32> affectedCres; //ids of creatures affected by this spell, generally used if spell does not set any effect (like dispel or cure)
si32 casterStack;// -1 if not cated by creature, >=0 caster stack ID si32 casterStack;// -1 if not cated by creature, >=0 caster stack ID
bool castByHero; //if true - spell has been cast by hero, otherwise by a creature bool castByHero; //if true - spell has been cast by hero, otherwise by a creature
std::vector<MetaString> battleLog;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
h & dmgToDisplay & side & id & skill & manaGained & tile & customEffects & affectedCres & casterStack & castByHero; h & side & id & skill & manaGained & tile & customEffects & affectedCres & casterStack & castByHero;
h & battleLog;
} }
}; };

View File

@ -953,6 +953,13 @@ const PlayerColor CGHeroInstance::getOwner() const
return tempOwner; return tempOwner;
} }
void CGHeroInstance::getCasterName(MetaString & text) const
{
//FIXME: use local name, MetaString need access to gamestate as hero name is part of map object
text.addReplacement(name);
}
bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
{ {
if(nullptr == getArt(ArtifactPosition::SPELLBOOK)) if(nullptr == getArt(ArtifactPosition::SPELLBOOK))

View File

@ -246,6 +246,8 @@ public:
const PlayerColor getOwner() const override; const PlayerColor getOwner() const override;
void getCasterName(MetaString & text) const override;
void deserializationFix(); void deserializationFix();
void initObj() override; void initObj() override;

View File

@ -119,13 +119,12 @@ namespace SRSLPraserHelpers
} }
SpellCastContext::SpellCastContext(const DefaultSpellMechanics * mechanics_, const SpellCastEnvironment * env_, const BattleSpellCastParameters & parameters_): SpellCastContext::SpellCastContext(const DefaultSpellMechanics * mechanics_, const SpellCastEnvironment * env_, const BattleSpellCastParameters & parameters_):
mechanics(mechanics_), env(env_), attackedCres(), sc(), si(), parameters(parameters_), otherHero(nullptr), spellCost(0) mechanics(mechanics_), env(env_), attackedCres(), sc(), si(), parameters(parameters_), otherHero(nullptr), spellCost(0), damageToDisplay(0)
{ {
sc.side = parameters.casterSide; sc.side = parameters.casterSide;
sc.id = mechanics->owner->id; sc.id = mechanics->owner->id;
sc.skill = parameters.spellLvl; sc.skill = parameters.spellLvl;
sc.tile = parameters.getFirstDestinationHex(); sc.tile = parameters.getFirstDestinationHex();
sc.dmgToDisplay = 0;
sc.castByHero = parameters.mode == ECastingMode::HERO_CASTING; sc.castByHero = parameters.mode == ECastingMode::HERO_CASTING;
sc.casterStack = (parameters.casterStack ? parameters.casterStack->ID : -1); sc.casterStack = (parameters.casterStack ? parameters.casterStack->ID : -1);
sc.manaGained = 0; sc.manaGained = 0;
@ -146,22 +145,63 @@ SpellCastContext::~SpellCastContext()
void SpellCastContext::addDamageToDisplay(const si32 value) void SpellCastContext::addDamageToDisplay(const si32 value)
{ {
sc.dmgToDisplay += value; damageToDisplay += value;
} }
void SpellCastContext::setDamageToDisplay(const si32 value) void SpellCastContext::setDamageToDisplay(const si32 value)
{ {
sc.dmgToDisplay = value; damageToDisplay = value;
} }
void SpellCastContext::sendCastPacket() void SpellCastContext::prepareBattleLog()
{ {
for(auto sta : attackedCres) //todo: prepare battle log
bool displayDamage = true;
if(attackedCres.size() == 1)
{ {
sc.affectedCres.insert(sta->ID); const CStack * attackedStack = *attackedCres.begin();
switch(parameters.mode)
{
case ECastingMode::HERO_CASTING:
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, 195);
parameters.caster->getCasterName(line);
line.addReplacement(MetaString::SPELL_NAME, mechanics->owner->id.toEnum());
line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
}
break;
default:
{
mechanics->battleLogSingleTarget(sc.battleLog, parameters, attackedStack, damageToDisplay, displayDamage);
}
break;
}
}
else
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, 196);
parameters.caster->getCasterName(line);
line.addReplacement(MetaString::SPELL_NAME, mechanics->owner->id.toEnum());
sc.battleLog.push_back(line);
} }
env->sendAndApply(&sc); displayDamage = displayDamage && damageToDisplay > 0;
if(displayDamage)
{
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, 376);
line.addReplacement(MetaString::SPELL_NAME, mechanics->owner->id.toEnum());
line.addReplacement(damageToDisplay);
sc.battleLog.push_back(line);
}
} }
void SpellCastContext::beforeCast() void SpellCastContext::beforeCast()
@ -190,7 +230,14 @@ void SpellCastContext::beforeCast()
void SpellCastContext::afterCast() void SpellCastContext::afterCast()
{ {
sendCastPacket(); for(auto sta : attackedCres)
{
sc.affectedCres.insert(sta->ID);
}
prepareBattleLog();
env->sendAndApply(&sc);
if(parameters.mode == ECastingMode::HERO_CASTING) if(parameters.mode == ECastingMode::HERO_CASTING)
{ {
@ -312,33 +359,32 @@ void DefaultSpellMechanics::cast(const SpellCastEnvironment * env, const BattleS
ctx.afterCast(); ctx.afterCast();
} }
void DefaultSpellMechanics::battleLogSingleTarget(std::vector<std::string> & logLines, const BattleSpellCast * packet, void DefaultSpellMechanics::battleLogSingleTarget(std::vector<MetaString>& logLines, const BattleSpellCastParameters & parameters,
const std::string & casterName, const CStack * attackedStack, bool & displayDamage) const const CStack * attackedStack, const si32 damageToDisplay, bool & displayDamage) const
{ {
const std::string attackedName = attackedStack->getName(); auto getPluralFormat = [attackedStack](const int baseTextID) -> si32
const std::string attackedNameSing = attackedStack->getCreature()->nameSing;
const std::string attackedNamePl = attackedStack->getCreature()->namePl;
auto getPluralFormat = [attackedStack](const int baseTextID) -> boost::format
{ {
return boost::format(VLC->generaltexth->allTexts[(attackedStack->count > 1 ? baseTextID + 1 : baseTextID)]); return attackedStack->count > 1 ? baseTextID + 1 : baseTextID;
}; };
auto logSimple = [&logLines, getPluralFormat, attackedName](const int baseTextID) auto logSimple = [attackedStack, &logLines, getPluralFormat](const int baseTextID)
{ {
boost::format fmt = getPluralFormat(baseTextID); MetaString line;
fmt % attackedName; line.addTxt(MetaString::GENERAL_TXT, getPluralFormat(baseTextID));
logLines.push_back(fmt.str()); line.addReplacement(*attackedStack);
logLines.push_back(line);
}; };
auto logPlural = [&logLines, attackedNamePl](const int baseTextID) auto logPlural = [attackedStack, &logLines, getPluralFormat](const int baseTextID)
{ {
boost::format fmt(VLC->generaltexth->allTexts[baseTextID]); MetaString line;
fmt % attackedNamePl; line.addTxt(MetaString::GENERAL_TXT, baseTextID);
logLines.push_back(fmt.str()); line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
logLines.push_back(line);
}; };
displayDamage = false; //in most following cases damage info text is custom displayDamage = false; //in most following cases damage info text is custom
switch(owner->id) switch(owner->id)
{ {
case SpellID::STONE_GAZE: case SpellID::STONE_GAZE:
@ -358,52 +404,61 @@ void DefaultSpellMechanics::battleLogSingleTarget(std::vector<std::string> & log
break; break;
case SpellID::AGE: case SpellID::AGE:
{ {
boost::format text = getPluralFormat(551);
text % attackedName;
//The %s shrivel with age, and lose %d hit points." //The %s shrivel with age, and lose %d hit points."
MetaString line;
line.addTxt(MetaString::GENERAL_TXT, getPluralFormat(551));
line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
//todo: display effect from only this cast
TBonusListPtr bl = attackedStack->getBonuses(Selector::type(Bonus::STACK_HEALTH)); TBonusListPtr bl = attackedStack->getBonuses(Selector::type(Bonus::STACK_HEALTH));
const int fullHP = bl->totalValue(); const int fullHP = bl->totalValue();
bl->remove_if(Selector::source(Bonus::SPELL_EFFECT, SpellID::AGE)); bl->remove_if(Selector::source(Bonus::SPELL_EFFECT, SpellID::AGE));
text % (fullHP - bl->totalValue()); line.addReplacement(fullHP - bl->totalValue());
logLines.push_back(text.str()); logLines.push_back(line);
} }
break; break;
case SpellID::THUNDERBOLT: case SpellID::THUNDERBOLT:
{ {
logPlural(367); logPlural(367);
MetaString line;
//todo: handle newlines in metastring
std::string text = VLC->generaltexth->allTexts[343].substr(1, VLC->generaltexth->allTexts[343].size() - 1); //Does %d points of damage. std::string text = VLC->generaltexth->allTexts[343].substr(1, VLC->generaltexth->allTexts[343].size() - 1); //Does %d points of damage.
boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(packet->dmgToDisplay)); //no more text afterwards line << text;
logLines.push_back(text); line.addReplacement(damageToDisplay); //no more text afterwards
logLines.push_back(line);
} }
break; break;
case SpellID::DISPEL_HELPFUL_SPELLS: case SpellID::DISPEL_HELPFUL_SPELLS:
logPlural(555); logPlural(555);
break; break;
case SpellID::DEATH_STARE: case SpellID::DEATH_STARE:
if (packet->dmgToDisplay > 0) if (damageToDisplay > 0)
{ {
std::string text; MetaString line;
if (packet->dmgToDisplay > 1) if (damageToDisplay > 1)
{ {
text = VLC->generaltexth->allTexts[119]; //%d %s die under the terrible gaze of the %s. line.addTxt(MetaString::GENERAL_TXT, 119); //%d %s die under the terrible gaze of the %s.
boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(packet->dmgToDisplay)); line.addReplacement(damageToDisplay);
boost::algorithm::replace_first(text, "%s", attackedNamePl); line.addReplacement(MetaString::CRE_PL_NAMES, attackedStack->getCreature()->idNumber.num);
} }
else else
{ {
text = VLC->generaltexth->allTexts[118]; //One %s dies under the terrible gaze of the %s. line.addTxt(MetaString::GENERAL_TXT, 118); //One %s dies under the terrible gaze of the %s.
boost::algorithm::replace_first(text, "%s", attackedNameSing); line.addReplacement(MetaString::CRE_SING_NAMES, attackedStack->getCreature()->idNumber.num);
} }
boost::algorithm::replace_first(text, "%s", casterName); //casting stack parameters.caster->getCasterName(line);
logLines.push_back(text); logLines.push_back(line);
} }
break; break;
default: default:
{ {
boost::format text(VLC->generaltexth->allTexts[565]); //The %s casts %s MetaString line;
text % casterName % owner->name; line.addTxt(MetaString::GENERAL_TXT, 565);//The %s casts %s
//todo: use text 566 for single creature
parameters.caster->getCasterName(line);
line.addReplacement(MetaString::SPELL_NAME, owner->id.toEnum());
displayDamage = true; displayDamage = true;
logLines.push_back(text.str()); logLines.push_back(line);
} }
break; break;
} }

View File

@ -36,8 +36,9 @@ public:
private: private:
const CGHeroInstance * otherHero; const CGHeroInstance * otherHero;
int spellCost; int spellCost;
si32 damageToDisplay;
void sendCastPacket(); void prepareBattleLog();
}; };
///all combat spells ///all combat spells
@ -58,8 +59,8 @@ public:
void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const override final; void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const override final;
void battleLogSingleTarget(std::vector<std::string> & logLines, const BattleSpellCast * packet, void battleLogSingleTarget(std::vector<MetaString> & logLines, const BattleSpellCastParameters & parameters,
const std::string & casterName, const CStack * attackedStack, bool & displayDamage) const override; const CStack * attackedStack, const si32 damageToDisplay, bool & displayDamage) const;
bool requiresCreatureTarget() const override; bool requiresCreatureTarget() const override;
protected: protected:

View File

@ -586,59 +586,6 @@ ESpellCastProblem::ESpellCastProblem CSpell::isImmuneByStack(const ISpellCaster
return ESpellCastProblem::OK; return ESpellCastProblem::OK;
} }
void CSpell::prepareBattleLog(const CBattleInfoCallback * cb, const BattleSpellCast * packet, std::vector<std::string> & logLines) const
{
bool displayDamage = true;
std::string casterName("Something"); //todo: localize
if(packet->castByHero)
casterName = cb->battleGetHeroInfo(packet->side).name;
{
const auto casterStackID = packet->casterStack;
if(casterStackID > 0)
{
const CStack * casterStack = cb->battleGetStackByID(casterStackID);
if(casterStack != nullptr)
casterName = casterStack->type->namePl;
}
}
if(packet->affectedCres.size() == 1)
{
const CStack * attackedStack = cb->battleGetStackByID(*packet->affectedCres.begin(), false);
const std::string attackedNamePl = attackedStack->getCreature()->namePl;
if(packet->castByHero)
{
const std::string fmt = VLC->generaltexth->allTexts[195];
logLines.push_back(boost::to_string(boost::format(fmt) % casterName % this->name % attackedNamePl));
}
else
{
mechanics->battleLogSingleTarget(logLines, packet, casterName, attackedStack, displayDamage);
}
}
else
{
boost::format text(VLC->generaltexth->allTexts[196]);
text % casterName % this->name;
logLines.push_back(text.str());
}
if(packet->dmgToDisplay > 0 && displayDamage)
{
boost::format dmgInfo(VLC->generaltexth->allTexts[376]);
dmgInfo % this->name % packet->dmgToDisplay;
logLines.push_back(dmgInfo.str());
}
}
void CSpell::setIsOffensive(const bool val) void CSpell::setIsOffensive(const bool val)
{ {
isOffensive = val; isOffensive = val;

View File

@ -287,10 +287,6 @@ public:
///implementation of BattleSpellCast applying ///implementation of BattleSpellCast applying
void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const; void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const;
public://Client logic.
void prepareBattleLog(const CBattleInfoCallback * cb, const BattleSpellCast * packet, std::vector<std::string> & logLines) const;
public://internal, for use only by Mechanics classes public://internal, for use only by Mechanics classes
///applies caster`s secondary skills and affectedCreature`s to raw damage ///applies caster`s secondary skills and affectedCreature`s to raw damage
int adjustRawDamage(const ISpellCaster * caster, const CStack * affectedCreature, int rawDamage) const; int adjustRawDamage(const ISpellCaster * caster, const CStack * affectedCreature, int rawDamage) const;

View File

@ -58,12 +58,12 @@ ESpellCastProblem::ESpellCastProblem AcidBreathDamageMechanics::isImmuneByStack(
void DeathStareMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const void DeathStareMechanics::applyBattleEffects(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
{ {
//calculating dmg to display //calculating dmg to display
si32 dmgToDisplay = parameters.effectPower; si32 damageToDisplay = parameters.effectPower;
if(!ctx.attackedCres.empty()) if(!ctx.attackedCres.empty())
vstd::amin(dmgToDisplay, (*ctx.attackedCres.begin())->count); //stack is already reduced after attack vstd::amin(damageToDisplay, (*ctx.attackedCres.begin())->count); //stack is already reduced after attack
ctx.setDamageToDisplay(dmgToDisplay); ctx.setDamageToDisplay(damageToDisplay);
for(auto & attackedCre : ctx.attackedCres) for(auto & attackedCre : ctx.attackedCres)
{ {

View File

@ -114,9 +114,6 @@ public:
virtual void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const = 0; virtual void applyBattle(BattleInfo * battle, const BattleSpellCast * packet) const = 0;
virtual void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const = 0; virtual void battleCast(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const = 0;
virtual void battleLogSingleTarget(std::vector<std::string> & logLines, const BattleSpellCast * packet,
const std::string & casterName, const CStack * attackedStack, bool & displayDamage) const = 0;
//if true use generic algorithm for target existence check, see CSpell::canBeCast //if true use generic algorithm for target existence check, see CSpell::canBeCast
virtual bool requiresCreatureTarget() const = 0; virtual bool requiresCreatureTarget() const = 0;

View File

@ -18,6 +18,7 @@
class CSpell; class CSpell;
class CStack; class CStack;
class PlayerColor; class PlayerColor;
struct MetaString;
class DLL_LINKAGE ISpellCaster class DLL_LINKAGE ISpellCaster
{ {
@ -45,4 +46,6 @@ public:
virtual int getEffectValue(const CSpell * spell) const = 0; virtual int getEffectValue(const CSpell * spell) const = 0;
virtual const PlayerColor getOwner() const = 0; virtual const PlayerColor getOwner() const = 0;
virtual void getCasterName(MetaString & text) const = 0;
}; };