1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/lib/mapObjects/CGPandoraBox.cpp

354 lines
9.2 KiB
C++
Raw Normal View History

/*
* CGPandoraBox.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
*
*/
#include "StdInc.h"
#include "CGPandoraBox.h"
#include <vcmi/spells/Spell.h>
#include <vcmi/spells/Service.h>
#include "../CSoundBase.h"
#include "../CSkillHandler.h"
#include "../StartInfo.h"
#include "../IGameCallback.h"
#include "../constants/StringConstants.h"
#include "../networkPacks/PacksForClient.h"
#include "../networkPacks/PacksForClientBattle.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../serializer/JsonSerializeFormat.h"
VCMI_LIB_NAMESPACE_BEGIN
void CGPandoraBox::init()
{
blockVisit = true;
2023-09-17 22:19:45 +02:00
configuration.info.emplace_back();
configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
for(auto & i : configuration.info)
2023-09-17 22:19:45 +02:00
{
i.reward.removeObject = true;
2023-09-17 22:19:45 +02:00
if(!message.empty() && i.message.empty())
2023-09-27 23:11:11 +02:00
i.message = message;
2023-09-17 22:19:45 +02:00
}
}
void CGPandoraBox::initObj(vstd::RNG & rand)
{
init();
CRewardableObject::initObj(rand);
}
2023-09-17 18:02:24 +02:00
void CGPandoraBox::grantRewardWithMessage(const CGHeroInstance * h, int index, bool markAsVisit) const
{
2023-09-17 18:02:24 +02:00
auto vi = configuration.info.at(index);
if(!vi.message.empty())
{
CRewardableObject::grantRewardWithMessage(h, index, markAsVisit);
return;
}
//split reward message for pandora box
auto setText = [](bool cond, int posId, int negId, const CGHeroInstance * h)
{
2023-09-17 18:02:24 +02:00
MetaString text;
text.appendLocalString(EMetaText::ADVOB_TXT, cond ? posId : negId);
text.replaceRawString(h->getNameTranslated());
2023-09-17 18:02:24 +02:00
return text;
};
2024-01-01 16:37:48 +02:00
auto sendInfoWindow = [&](const MetaString & text, const Rewardable::Reward & reward)
{
2023-09-17 18:02:24 +02:00
InfoWindow iw;
iw.player = h->tempOwner;
iw.text = text;
reward.loadComponents(iw.components, h);
iw.type = EInfoWindowMode::MODAL;
if(!iw.components.empty())
cb->showInfoDialog(&iw);
};
Rewardable::Reward temp;
temp.spells = vi.reward.spells;
temp.heroExperience = vi.reward.heroExperience;
temp.heroLevel = vi.reward.heroLevel;
temp.primary = vi.reward.primary;
temp.secondary = vi.reward.secondary;
temp.bonuses = vi.reward.bonuses;
temp.manaDiff = vi.reward.manaDiff;
temp.manaPercentage = vi.reward.manaPercentage;
MetaString txt;
if(!vi.reward.spells.empty())
txt = setText(temp.spells.size() == 1, 184, 188, h);
if(vi.reward.heroExperience || vi.reward.heroLevel || !vi.reward.secondary.empty())
txt = setText(true, 175, 175, h);
for(int i : vi.reward.primary)
{
if(i)
{
2023-09-17 18:02:24 +02:00
txt = setText(true, 175, 175, h);
break;
}
2023-09-17 18:02:24 +02:00
}
2023-09-30 01:33:12 +02:00
if(vi.reward.manaDiff || vi.reward.manaPercentage >= 0)
2023-09-17 18:02:24 +02:00
txt = setText(temp.manaDiff > 0, 177, 176, h);
for(auto b : vi.reward.bonuses)
{
if(b.val && b.type == BonusType::MORALE)
txt = setText(b.val > 0, 179, 178, h);
if(b.val && b.type == BonusType::LUCK)
txt = setText(b.val > 0, 181, 180, h);
}
sendInfoWindow(txt, temp);
//resource message
temp = Rewardable::Reward{};
temp.resources = vi.reward.resources;
sendInfoWindow(setText(vi.reward.resources.marketValue() > 0, 183, 182, h), temp);
//artifacts message
temp = Rewardable::Reward{};
temp.artifacts = vi.reward.artifacts;
sendInfoWindow(setText(true, 183, 183, h), temp);
//creatures message
temp = Rewardable::Reward{};
temp.creatures = vi.reward.creatures;
txt.clear();
if(!vi.reward.creatures.empty())
{
MetaString loot;
for(auto c : vi.reward.creatures)
{
2023-09-17 18:02:24 +02:00
loot.appendRawString("%s");
loot.replaceName(c);
}
2023-09-17 18:02:24 +02:00
if(vi.reward.creatures.size() == 1 && vi.reward.creatures[0].count == 1)
txt.appendLocalString(EMetaText::ADVOB_TXT, 185);
else
txt.appendLocalString(EMetaText::ADVOB_TXT, 186);
2023-09-17 18:02:24 +02:00
txt.replaceRawString(loot.buildList());
txt.replaceRawString(h->getNameTranslated());
}
2023-09-17 18:02:24 +02:00
sendInfoWindow(txt, temp);
//everything else
temp = vi.reward;
temp.heroExperience = 0;
temp.heroLevel = 0;
temp.secondary.clear();
temp.primary.clear();
temp.resources.amin(0);
temp.resources.amax(0);
temp.manaDiff = 0;
2023-09-30 01:33:12 +02:00
temp.manaPercentage = -1;
2023-09-17 18:02:24 +02:00
temp.spells.clear();
temp.creatures.clear();
temp.bonuses.clear();
temp.artifacts.clear();
sendInfoWindow(setText(true, 175, 175, h), temp);
2023-09-17 18:02:24 +02:00
// grant reward afterwards. Note that it may remove object
if(markAsVisit)
markAsVisited(h);
grantReward(index, h);
}
void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
{
BlockingDialog bd (true, false);
bd.player = h->getOwner();
bd.text.appendLocalString(EMetaText::ADVOB_TXT, 14);
cb->showBlockingDialog(this, &bd);
}
void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
{
if(result.winner == BattleSide::ATTACKER)
{
CRewardableObject::onHeroVisit(hero);
}
}
void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
{
if(answer)
{
if(stacksCount() > 0) //if pandora's box is protected by army
{
hero->showInfoDialog(16, 0, EInfoWindowMode::MODAL);
cb->startBattle(hero, this); //grants things after battle
}
else if(getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT).empty())
{
hero->showInfoDialog(15);
cb->removeObject(this, hero->getOwner());
}
else //if it gives something without battle
{
CRewardableObject::onHeroVisit(hero);
}
}
}
void CGPandoraBox::serializeJsonOptions(JsonSerializeFormat & handler)
{
CRewardableObject::serializeJsonOptions(handler);
2023-09-17 22:19:45 +02:00
2023-09-27 23:11:11 +02:00
handler.serializeStruct("guardMessage", message);
if(!handler.saving)
{
2023-09-17 22:19:45 +02:00
//backward compatibility for VCMI maps that use old Pandora Box format
if(!handler.getCurrent()["guards"].Vector().empty())
CCreatureSet::serializeJson(handler, "guards", 7);
bool hasSomething = false;
Rewardable::VisitInfo vinfo;
2023-09-17 13:03:42 +02:00
vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
handler.serializeInt("experience", vinfo.reward.heroExperience, 0);
handler.serializeInt("mana", vinfo.reward.manaDiff, 0);
int val = 0;
handler.serializeInt("morale", val, 0);
if(val)
vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
handler.serializeInt("luck", val, 0);
if(val)
vinfo.reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
2023-09-17 13:03:42 +02:00
vinfo.reward.resources.serializeJson(handler, "resources");
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
{
auto s = handler.enterStruct("primarySkills");
2023-09-17 13:03:42 +02:00
for(int idx = 0; idx < vinfo.reward.primary.size(); idx ++)
{
2023-09-17 13:03:42 +02:00
handler.serializeInt(NPrimarySkill::names[idx], vinfo.reward.primary[idx], 0);
2023-09-17 22:19:45 +02:00
if(vinfo.reward.primary[idx])
hasSomething = true;
}
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
}
2023-09-17 13:03:42 +02:00
handler.serializeIdArray("artifacts", vinfo.reward.artifacts);
handler.serializeIdArray("spells", vinfo.reward.spells);
handler.enterArray("creatures").serializeStruct(vinfo.reward.creatures);
{
auto s = handler.enterStruct("secondarySkills");
for(const auto & p : handler.getCurrent().Struct())
{
const std::string skillName = p.first;
const std::string levelId = p.second.String();
2023-11-02 17:48:48 +02:00
const int rawId = SecondarySkill::decode(skillName);
if(rawId < 0)
{
logGlobal->error("Invalid secondary skill %s", skillName);
continue;
}
const int level = vstd::find_pos(NSecondarySkill::levels, levelId);
if(level < 0)
{
logGlobal->error("Invalid secondary skill level %s", levelId);
continue;
}
2023-09-17 13:03:42 +02:00
vinfo.reward.secondary[rawId] = level;
}
}
2023-09-17 22:19:45 +02:00
hasSomething = hasSomething
|| vinfo.reward.heroExperience
|| vinfo.reward.manaDiff
|| vinfo.reward.resources.nonZero()
|| !vinfo.reward.artifacts.empty()
|| !vinfo.reward.bonuses.empty()
|| !vinfo.reward.creatures.empty()
|| !vinfo.reward.secondary.empty();
2023-09-17 22:19:45 +02:00
if(hasSomething)
configuration.info.push_back(vinfo);
}
}
void CGEvent::init()
{
blockVisit = false;
2023-09-19 17:11:03 +02:00
configuration.infoWindowType = EInfoWindowMode::MODAL;
for(auto & i : configuration.info)
2023-09-17 18:02:24 +02:00
{
i.reward.removeObject = removeAfterVisit;
2023-09-17 18:02:24 +02:00
if(!message.empty() && i.message.empty())
2023-09-27 23:11:11 +02:00
i.message = message;
2023-09-17 18:02:24 +02:00
}
}
void CGEvent::grantRewardWithMessage(const CGHeroInstance * contextHero, int rewardIndex, bool markAsVisit) const
{
CRewardableObject::grantRewardWithMessage(contextHero, rewardIndex, markAsVisit);
}
void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
{
if(availableFor.count(h->tempOwner) == 0)
return;
if(cb->getPlayerSettings(h->tempOwner)->isControlledByHuman())
{
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;
2023-02-12 22:39:17 +02:00
if(!message.empty())
2023-09-27 23:11:11 +02:00
iw.text = message;
else
iw.text.appendLocalString(EMetaText::ADVOB_TXT, 16);
cb->showInfoDialog(&iw);
cb->startBattle(h, this);
}
else
{
CRewardableObject::onHeroVisit(h);
}
}
void CGEvent::serializeJsonOptions(JsonSerializeFormat & handler)
{
CGPandoraBox::serializeJsonOptions(handler);
handler.serializeBool("aIActivable", computerActivate, false);
handler.serializeBool("humanActivable", humanActivate, true);
handler.serializeBool("removeAfterVisit", removeAfterVisit, false);
handler.serializeIdArray("availableFor", availableFor);
}
VCMI_LIB_NAMESPACE_END