1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/lib/mapObjects/CQuest.cpp

898 lines
24 KiB
C++
Raw Normal View History

/*
* CQuest.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 "CQuest.h"
2021-02-15 14:03:32 +02:00
#include <vcmi/spells/Spell.h>
2023-05-17 15:52:16 +02:00
#include "../ArtifactUtils.h"
#include "../CSoundBase.h"
#include "../texts/CGeneralTextHandler.h"
#include "../CHeroHandler.h"
2023-06-17 14:21:42 +02:00
#include "CGCreature.h"
#include "../IGameCallback.h"
#include "../mapObjectConstructors/CObjectClassesHandler.h"
#include "../serializer/JsonSerializeFormat.h"
#include "../GameConstants.h"
#include "../constants/StringConstants.h"
#include "../CPlayerState.h"
#include "../CSkillHandler.h"
#include "../mapping/CMap.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../modding/ModScope.h"
#include "../modding/ModUtility.h"
#include "../networkPacks/PacksForClient.h"
2023-10-10 02:33:17 +02:00
#include "../spells/CSpellHandler.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
2023-02-12 22:39:17 +02:00
//TODO: Remove constructor
CQuest::CQuest():
qid(-1),
2023-10-11 21:10:42 +02:00
isCompleted(false),
2023-02-12 22:39:17 +02:00
lastDay(-1),
2023-10-11 00:47:19 +02:00
killTarget(ObjectInstanceID::NONE),
2023-02-12 22:39:17 +02:00
textOption(0),
completedOption(0),
stackDirection(0),
2023-04-13 18:06:44 +02:00
isCustomFirst(false),
2023-02-12 22:39:17 +02:00
isCustomNext(false),
2023-10-09 15:54:14 +02:00
isCustomComplete(false),
2023-10-09 19:15:34 +02:00
repeatedQuest(false),
questName(CQuest::missionName(EQuestMission::NONE))
{
}
static std::string visitedTxt(const bool visited)
{
int id = visited ? 352 : 353;
return VLC->generaltexth->allTexts[id];
}
const std::string & CQuest::missionName(EQuestMission mission)
{
static const std::array<std::string, 14> names = {
"empty",
"heroLevel",
"primarySkill",
"killHero",
"killCreature",
"bringArt",
"bringCreature",
"bringResources",
"bringHero",
"bringPlayer",
"hotaINVALID", // only used for h3m parsing
2023-10-13 12:52:45 +02:00
"keymaster",
"heroClass",
"reachDate"
};
2023-01-01 22:50:38 +02:00
if(static_cast<size_t>(mission) < names.size())
return names[static_cast<size_t>(mission)];
return names[0];
}
const std::string & CQuest::missionState(int state)
{
static const std::array<std::string, 5> states = {
"receive",
"visit",
"complete",
"hover",
"description",
};
2023-01-01 22:50:38 +02:00
if(state < states.size())
return states[state];
return states[0];
}
bool CQuest::checkMissionArmy(const CQuest * q, const CCreatureSet * army)
{
std::vector<CStackBasicDescriptor>::const_iterator cre;
TSlots::const_iterator it;
2023-02-12 22:39:17 +02:00
ui32 count = 0;
ui32 slotsCount = 0;
bool hasExtraCreatures = false;
2023-10-11 00:47:19 +02:00
for(cre = q->mission.creatures.begin(); cre != q->mission.creatures.end(); ++cre)
{
for(count = 0, it = army->Slots().begin(); it != army->Slots().end(); ++it)
{
if(it->second->type == cre->type)
{
count += it->second->count;
slotsCount++;
}
}
2023-02-12 22:39:17 +02:00
if(static_cast<TQuantity>(count) < cre->count) //not enough creatures of this kind
return false;
2023-02-12 22:39:17 +02:00
hasExtraCreatures |= static_cast<TQuantity>(count) > cre->count;
}
return hasExtraCreatures || slotsCount < army->Slots().size();
}
2016-01-29 08:28:37 +02:00
bool CQuest::checkQuest(const CGHeroInstance * h) const
{
2023-10-11 00:47:19 +02:00
if(!mission.heroAllowed(h))
2023-10-09 05:24:40 +02:00
return false;
if(killTarget.hasValue())
{
PlayerColor owner = h->getOwner();
if (!h->cb->getPlayerState(owner)->destroyedObjects.count(killTarget))
return false;
}
2023-10-09 05:24:40 +02:00
return true;
}
2023-10-09 02:19:58 +02:00
void CQuest::completeQuest(IGameCallback * cb, const CGHeroInstance *h) const
{
// FIXME: this should be part of 'reward', and not hacking into limiter state that should only limit access to such reward
2023-10-11 00:47:19 +02:00
for(auto & elem : mission.artifacts)
2023-10-09 02:19:58 +02:00
{
2023-10-09 05:24:40 +02:00
if(h->hasArt(elem))
{
cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(elem, false)));
2023-10-09 05:24:40 +02:00
}
else
{
const auto * assembly = h->getAssemblyByConstituent(elem);
assert(assembly);
auto parts = assembly->getPartsInfo();
2023-10-09 02:19:58 +02:00
2023-10-09 05:24:40 +02:00
// Remove the assembly
cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(assembly)));
2023-10-09 02:19:58 +02:00
2023-10-09 05:24:40 +02:00
// Disassemble this backpack artifact
for(const auto & ci : parts)
2023-10-09 02:19:58 +02:00
{
2023-10-09 05:24:40 +02:00
if(ci.art->getTypeId() != elem)
cb->giveHeroNewArtifact(h, ci.art->artType, ArtifactPosition::BACKPACK_START);
2023-10-09 02:19:58 +02:00
}
2023-10-09 05:24:40 +02:00
}
2023-10-09 02:19:58 +02:00
}
2023-10-11 00:47:19 +02:00
cb->takeCreatures(h->id, mission.creatures);
cb->giveResources(h->getOwner(), -mission.resources);
2023-10-09 02:19:58 +02:00
}
2024-01-01 16:37:48 +02:00
void CQuest::addTextReplacements(IGameCallback * cb, MetaString & text, std::vector<Component> & components) const
{
2023-10-11 00:47:19 +02:00
if(mission.heroLevel > 0)
text.replaceNumber(mission.heroLevel);
2023-10-09 19:15:34 +02:00
2023-10-11 00:47:19 +02:00
if(mission.heroExperience > 0)
text.replaceNumber(mission.heroExperience);
2023-10-10 02:33:17 +02:00
2023-10-09 19:15:34 +02:00
{ //primary skills
MetaString loot;
for(int i = 0; i < 4; ++i)
{
2023-10-11 00:47:19 +02:00
if(mission.primary[i])
2023-10-09 19:15:34 +02:00
{
loot.appendRawString("%d %s");
2023-10-11 00:47:19 +02:00
loot.replaceNumber(mission.primary[i]);
2023-10-09 19:15:34 +02:00
loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
}
}
2023-10-10 02:33:17 +02:00
2023-10-11 00:47:19 +02:00
for(auto & skill : mission.secondary)
2023-10-10 02:33:17 +02:00
{
loot.appendTextID(VLC->skillh->getById(skill.first)->getNameTextID());
}
2023-10-11 00:47:19 +02:00
for(auto & spell : mission.spells)
2023-10-10 02:33:17 +02:00
{
loot.appendTextID(VLC->spellh->getById(spell)->getNameTextID());
}
2023-10-09 19:15:34 +02:00
if(!loot.empty())
text.replaceRawString(loot.buildList());
}
2023-10-11 00:47:19 +02:00
if(killTarget != ObjectInstanceID::NONE && !heroName.empty())
{
components.emplace_back(ComponentType::HERO_PORTRAIT, heroPortrait);
2023-10-09 19:15:34 +02:00
addKillTargetReplacements(text);
}
2023-10-09 19:15:34 +02:00
if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE)
{
components.emplace_back(ComponentType::CREATURE, stackToKill);
2023-10-09 19:15:34 +02:00
addKillTargetReplacements(text);
}
2023-10-09 19:15:34 +02:00
2023-10-11 00:47:19 +02:00
if(!mission.heroes.empty())
text.replaceRawString(VLC->heroh->getById(mission.heroes.front())->getNameTranslated());
2023-10-09 19:15:34 +02:00
2023-10-11 00:47:19 +02:00
if(!mission.artifacts.empty())
{
2023-10-09 19:15:34 +02:00
MetaString loot;
2023-10-11 00:47:19 +02:00
for(const auto & elem : mission.artifacts)
{
2023-10-09 19:15:34 +02:00
loot.appendRawString("%s");
loot.replaceName(elem);
}
2023-10-09 19:15:34 +02:00
text.replaceRawString(loot.buildList());
}
2023-10-11 00:47:19 +02:00
if(!mission.creatures.empty())
2023-10-09 19:15:34 +02:00
{
MetaString loot;
2023-10-11 00:47:19 +02:00
for(const auto & elem : mission.creatures)
{
2023-10-09 19:15:34 +02:00
loot.appendRawString("%s");
loot.replaceName(elem);
}
2023-10-09 19:15:34 +02:00
text.replaceRawString(loot.buildList());
}
2023-10-11 00:47:19 +02:00
if(mission.resources.nonZero())
2023-10-09 19:15:34 +02:00
{
MetaString loot;
2023-11-08 16:13:08 +02:00
for(auto i : GameResID::ALL_RESOURCES())
{
2023-10-11 00:47:19 +02:00
if(mission.resources[i])
{
2023-10-09 19:15:34 +02:00
loot.appendRawString("%d %s");
2023-10-11 00:47:19 +02:00
loot.replaceNumber(mission.resources[i]);
2023-11-08 16:13:08 +02:00
loot.replaceName(i);
}
}
2023-10-09 19:15:34 +02:00
text.replaceRawString(loot.buildList());
}
2023-10-09 19:15:34 +02:00
2023-10-11 00:47:19 +02:00
if(!mission.players.empty())
2023-10-10 02:33:17 +02:00
{
MetaString loot;
2023-10-11 00:47:19 +02:00
for(auto & p : mission.players)
loot.appendName(p);
2023-10-10 02:33:17 +02:00
text.replaceRawString(loot.buildList());
}
2023-10-14 00:15:15 +02:00
if(lastDay >= 0)
2024-01-01 16:37:48 +02:00
text.replaceNumber(lastDay - cb->getDate(Date::DAY));
}
2024-01-01 16:37:48 +02:00
void CQuest::getVisitText(IGameCallback * cb, MetaString &iwText, std::vector<Component> &components, bool firstVisit, const CGHeroInstance * h) const
{
2023-10-09 19:15:34 +02:00
bool failRequirements = (h ? !checkQuest(h) : true);
2023-10-11 00:47:19 +02:00
mission.loadComponents(components, h);
2023-10-09 19:15:34 +02:00
if(firstVisit)
iwText.appendRawString(firstVisitText.toString());
else if(failRequirements)
iwText.appendRawString(nextVisitText.toString());
2023-10-14 00:15:15 +02:00
if(lastDay >= 0)
iwText.appendTextID(TextIdentifier("core", "seerhut", "time", textOption).get());
2024-01-01 16:37:48 +02:00
addTextReplacements(cb, iwText, components);
2023-10-09 19:15:34 +02:00
}
2024-01-01 16:37:48 +02:00
void CQuest::getRolloverText(IGameCallback * cb, MetaString &ms, bool onHover) const
2023-10-09 19:15:34 +02:00
{
2016-01-29 08:28:37 +02:00
if(onHover)
ms.appendRawString("\n\n");
std::string questState = missionState(onHover ? 3 : 4);
2023-10-14 00:15:15 +02:00
ms.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, questState, textOption).get());
2023-10-10 01:20:55 +02:00
std::vector<Component> components;
2024-01-01 16:37:48 +02:00
addTextReplacements(cb, ms, components);
}
2024-01-01 16:37:48 +02:00
void CQuest::getCompletionText(IGameCallback * cb, MetaString &iwText) const
{
2023-09-27 23:49:27 +02:00
iwText.appendRawString(completedText.toString());
2023-10-09 19:15:34 +02:00
2023-10-10 01:20:55 +02:00
std::vector<Component> components;
2024-01-01 16:37:48 +02:00
addTextReplacements(cb, iwText, components);
2023-10-09 19:15:34 +02:00
}
void CQuest::defineQuestName()
{
//standard quests
questName = CQuest::missionName(EQuestMission::NONE);
if(mission.heroLevel > 0) questName = CQuest::missionName(EQuestMission::LEVEL);
for(auto & s : mission.primary) if(s) questName = CQuest::missionName(EQuestMission::PRIMARY_SKILL);
if(killTarget != ObjectInstanceID::NONE && !heroName.empty()) questName = CQuest::missionName(EQuestMission::KILL_HERO);
if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE) questName = CQuest::missionName(EQuestMission::KILL_CREATURE);
if(!mission.artifacts.empty()) questName = CQuest::missionName(EQuestMission::ARTIFACT);
if(!mission.creatures.empty()) questName = CQuest::missionName(EQuestMission::ARMY);
if(mission.resources.nonZero()) questName = CQuest::missionName(EQuestMission::RESOURCES);
if(!mission.heroes.empty()) questName = CQuest::missionName(EQuestMission::HERO);
if(!mission.players.empty()) questName = CQuest::missionName(EQuestMission::PLAYER);
if(mission.daysPassed > 0) questName = CQuest::missionName(EQuestMission::HOTA_REACH_DATE);
if(!mission.heroClasses.empty()) questName = CQuest::missionName(EQuestMission::HOTA_HERO_CLASS);
2023-10-09 19:15:34 +02:00
}
void CQuest::addKillTargetReplacements(MetaString &out) const
{
if(!heroName.empty())
out.replaceRawString(heroName);
if(stackToKill != CreatureID::NONE)
{
out.replaceNamePlural(stackToKill);
2023-10-09 19:15:34 +02:00
out.replaceRawString(VLC->generaltexth->arraytxt[147+stackDirection]);
}
}
void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
{
auto q = handler.enterStruct(fieldName);
2023-09-27 23:49:27 +02:00
handler.serializeStruct("firstVisitText", firstVisitText);
handler.serializeStruct("nextVisitText", nextVisitText);
handler.serializeStruct("completedText", completedText);
2023-10-09 15:54:14 +02:00
handler.serializeBool("repeatedQuest", repeatedQuest, false);
if(!handler.saving)
{
2023-02-12 22:39:17 +02:00
isCustomFirst = !firstVisitText.empty();
isCustomNext = !nextVisitText.empty();
isCustomComplete = !completedText.empty();
}
2023-10-09 05:24:40 +02:00
handler.serializeInt("timeLimit", lastDay, -1);
2023-10-11 00:47:19 +02:00
handler.serializeStruct("limiter", mission);
handler.serializeInstance("killTarget", killTarget, ObjectInstanceID::NONE);
2023-10-09 19:15:34 +02:00
if(!handler.saving) //compatibility with legacy vmaps
{
2023-10-09 19:15:34 +02:00
std::string missionType = "None";
handler.serializeString("missionType", missionType);
if(missionType == "None")
return;
if(missionType == "Level")
2023-10-12 12:52:01 +02:00
handler.serializeInt("heroLevel", mission.heroLevel);
2023-10-09 19:15:34 +02:00
if(missionType == "PrimaryStat")
2023-04-13 18:06:44 +02:00
{
2023-10-09 19:15:34 +02:00
auto primarySkills = handler.enterStruct("primarySkills");
for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
2023-10-11 00:47:19 +02:00
handler.serializeInt(NPrimarySkill::names[i], mission.primary[i], 0);
2023-10-09 19:15:34 +02:00
}
if(missionType == "Artifact")
2023-10-11 00:47:19 +02:00
handler.serializeIdArray<ArtifactID>("artifacts", mission.artifacts);
2023-10-09 19:15:34 +02:00
if(missionType == "Army")
{
auto a = handler.enterArray("creatures");
2023-10-11 00:47:19 +02:00
a.serializeStruct(mission.creatures);
2023-10-09 19:15:34 +02:00
}
if(missionType == "Resources")
{
auto r = handler.enterStruct("resources");
2023-10-09 19:15:34 +02:00
for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
{
2023-10-11 00:47:19 +02:00
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mission.resources[idx], 0);
}
2023-10-09 19:15:34 +02:00
}
if(missionType == "Hero")
{
HeroTypeID temp;
handler.serializeId("hero", temp, HeroTypeID::NONE);
2023-10-11 00:47:19 +02:00
mission.heroes.emplace_back(temp);
2023-10-09 19:15:34 +02:00
}
if(missionType == "Player")
{
PlayerColor temp;
handler.serializeId("player", temp, PlayerColor::NEUTRAL);
2023-10-11 00:47:19 +02:00
mission.players.emplace_back(temp);
2023-04-13 18:06:44 +02:00
}
}
}
2023-10-09 19:15:34 +02:00
bool IQuestObject::checkQuest(const CGHeroInstance* h) const
{
return quest->checkQuest(h);
}
2024-01-01 16:37:48 +02:00
void CGSeerHut::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
2023-10-09 19:15:34 +02:00
{
2024-01-01 16:37:48 +02:00
quest->getVisitText(cb, text, components, FirstVisit, h);
2023-10-09 19:15:34 +02:00
}
void IQuestObject::afterAddToMapCommon(CMap * map) const
{
map->addNewQuestInstance(quest);
}
void CGSeerHut::setObjToKill()
{
2023-10-18 00:13:32 +02:00
if(quest->killTarget == ObjectInstanceID::NONE)
return;
2023-10-09 19:15:34 +02:00
if(getCreatureToKill(true))
{
quest->stackToKill = getCreatureToKill(false)->getCreature();
assert(quest->stackToKill != CreatureID::NONE);
quest->stackDirection = checkDirection();
}
2023-10-09 19:15:34 +02:00
else if(getHeroToKill(true))
{
quest->heroName = getHeroToKill(false)->getNameTranslated();
quest->heroPortrait = getHeroToKill(false)->getPortraitSource();
}
}
void CGSeerHut::init(vstd::RNG & rand)
{
auto names = VLC->generaltexth->findStringsWithPrefix("core.seerhut.names");
2023-04-13 18:06:44 +02:00
auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
seerName = VLC->generaltexth->translate(seerNameID);
quest->textOption = rand.nextInt(2);
quest->completedOption = rand.nextInt(1, 3);
2023-09-13 01:40:07 +02:00
configuration.canRefuse = true;
configuration.visitMode = Rewardable::EVisitMode::VISIT_ONCE;
configuration.selectMode = Rewardable::ESelectMode::SELECT_PLAYER;
}
void CGSeerHut::initObj(vstd::RNG & rand)
{
init(rand);
2023-09-13 01:40:07 +02:00
CRewardableObject::initObj(rand);
2023-10-09 19:15:34 +02:00
2023-10-10 01:53:25 +02:00
setObjToKill();
2023-10-09 19:15:34 +02:00
quest->defineQuestName();
2023-10-11 00:47:19 +02:00
if(quest->mission == Rewardable::Limiter{} && quest->killTarget == ObjectInstanceID::NONE)
2023-10-11 21:10:42 +02:00
quest->isCompleted = true;
2023-10-10 01:20:55 +02:00
if(quest->questName == quest->missionName(EQuestMission::NONE))
{
2023-10-09 19:15:34 +02:00
quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
}
else
{
2023-10-09 19:15:34 +02:00
if(!quest->isCustomFirst)
quest->firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(0), quest->textOption).get());
if(!quest->isCustomNext)
quest->nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest->questName, quest->missionState(1), quest->textOption).get());
if(!quest->isCustomComplete)
2023-10-10 01:53:25 +02:00
quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest-> questName, quest->missionState(2), quest->textOption).get());
}
2023-10-13 12:52:45 +02:00
2024-01-01 16:37:48 +02:00
quest->getCompletionText(cb, configuration.onSelect);
2023-10-13 12:52:45 +02:00
for(auto & i : configuration.info)
2024-01-01 16:37:48 +02:00
quest->getCompletionText(cb, i.message);
}
2016-01-29 08:28:37 +02:00
void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
{
2024-01-01 16:37:48 +02:00
quest->getRolloverText(cb, text, onHover);//TODO: simplify?
2016-01-29 08:28:37 +02:00
if(!onHover)
text.replaceRawString(seerName);
}
std::string CGSeerHut::getHoverText(PlayerColor player) const
{
std::string hoverName = getObjectName();
2023-10-11 21:10:42 +02:00
if(ID == Obj::SEER_HUT && quest->activeForPlayers.count(player))
{
hoverName = VLC->generaltexth->allTexts[347];
2016-01-29 08:28:37 +02:00
boost::algorithm::replace_first(hoverName, "%s", seerName);
}
2023-10-11 21:10:42 +02:00
if(quest->activeForPlayers.count(player)
&& (quest->mission != Rewardable::Limiter{}
|| quest->killTarget != ObjectInstanceID::NONE)) //rollover when the quest is active
{
MetaString ms;
getRolloverText (ms, true);
hoverName += ms.toString();
}
return hoverName;
}
std::string CGSeerHut::getHoverText(const CGHeroInstance * hero) const
{
return getHoverText(hero->getOwner());
}
std::string CGSeerHut::getPopupText(PlayerColor player) const
{
return getHoverText(player);
}
std::string CGSeerHut::getPopupText(const CGHeroInstance * hero) const
{
return getHoverText(hero->getOwner());
}
std::vector<Component> CGSeerHut::getPopupComponents(PlayerColor player) const
{
std::vector<Component> result;
if (quest->activeForPlayers.count(player))
quest->mission.loadComponents(result, nullptr);
return result;
}
std::vector<Component> CGSeerHut::getPopupComponents(const CGHeroInstance * hero) const
{
std::vector<Component> result;
if (quest->activeForPlayers.count(hero->getOwner()))
quest->mission.loadComponents(result, hero);
return result;
}
void CGSeerHut::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
{
2016-01-29 08:28:37 +02:00
switch(what)
{
case ObjProperty::SEERHUT_VISITED:
2023-10-11 21:10:42 +02:00
{
quest->activeForPlayers.emplace(identifier.as<PlayerColor>());
break;
2023-10-11 21:10:42 +02:00
}
case ObjProperty::SEERHUT_COMPLETE:
2023-10-11 21:10:42 +02:00
{
quest->isCompleted = identifier.getNum();
2023-10-11 21:10:42 +02:00
quest->activeForPlayers.clear();
break;
}
}
}
void CGSeerHut::newTurn(vstd::RNG & rand) const
{
2023-09-13 01:40:07 +02:00
CRewardableObject::newTurn(rand);
2016-01-29 08:28:37 +02:00
if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
{
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, true);
}
}
2016-01-29 08:28:37 +02:00
void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
{
InfoWindow iw;
iw.player = h->getOwner();
2023-10-11 21:10:42 +02:00
if(!quest->isCompleted)
{
2023-10-11 21:10:42 +02:00
bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
bool failRequirements = !checkQuest(h);
2016-01-29 08:28:37 +02:00
if(firstVisit)
{
cb->setObjPropertyID(id, ObjProperty::SEERHUT_VISITED, h->getOwner());
AddQuest aq;
aq.quest = QuestInfo (quest, this, visitablePos());
aq.player = h->tempOwner;
2016-01-29 08:28:37 +02:00
cb->sendAndApply(&aq); //TODO: merge with setObjProperty?
}
2016-01-29 08:28:37 +02:00
if(firstVisit || failRequirements)
{
2023-10-09 19:15:34 +02:00
getVisitText (iw.text, iw.components, firstVisit, h);
cb->showInfoDialog(&iw);
}
2016-01-29 08:28:37 +02:00
if(!failRequirements) // propose completion, also on first visit
{
2023-09-13 01:40:07 +02:00
CRewardableObject::onHeroVisit(h);
return;
}
}
else
{
iw.text.appendRawString(VLC->generaltexth->seerEmpty[quest->completedOption]);
if (ID == Obj::SEER_HUT)
iw.text.replaceRawString(seerName);
cb->showInfoDialog(&iw);
}
}
int CGSeerHut::checkDirection() const
{
int3 cord = getCreatureToKill(false)->pos;
2023-02-12 22:39:17 +02:00
if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
{
2023-02-12 22:39:17 +02:00
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
return 8;
2023-02-12 22:39:17 +02:00
else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
return 1;
else //northeast
return 2;
}
2023-02-12 22:39:17 +02:00
else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
{
2023-02-12 22:39:17 +02:00
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
return 7;
2023-02-12 22:39:17 +02:00
else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
return 9;
else //east
return 3;
}
else //south
{
2023-02-12 22:39:17 +02:00
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
return 6;
2023-02-12 22:39:17 +02:00
else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //south
return 5;
else //southeast
return 4;
}
}
const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
{
const CGObjectInstance *o = cb->getObj(quest->killTarget);
if(allowNull && !o)
return nullptr;
2023-02-12 22:39:17 +02:00
return dynamic_cast<const CGHeroInstance *>(o);
}
const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
{
const CGObjectInstance *o = cb->getObj(quest->killTarget);
if(allowNull && !o)
return nullptr;
2023-02-12 22:39:17 +02:00
return dynamic_cast<const CGCreature *>(o);
}
void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
{
2023-09-13 01:40:07 +02:00
CRewardableObject::blockingDialogAnswered(hero, answer);
if(answer)
2023-10-09 02:19:58 +02:00
{
quest->completeQuest(cb, hero);
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, !quest->repeatedQuest); //mission complete
2023-10-09 02:19:58 +02:00
}
}
void CGSeerHut::afterAddToMap(CMap* map)
{
IQuestObject::afterAddToMapCommon(map);
}
void CGSeerHut::serializeJsonOptions(JsonSerializeFormat & handler)
{
//quest and reward
CRewardableObject::serializeJsonOptions(handler);
quest->serializeJson(handler, "quest");
if(!handler.saving)
{
2023-09-17 22:19:45 +02:00
//backward compatibility for VCMI maps that use old SeerHut format
2023-09-13 02:14:35 +02:00
auto s = handler.enterStruct("reward");
2023-09-15 10:06:06 +02:00
const JsonNode & rewardsJson = handler.getCurrent();
2024-04-08 12:16:34 +02:00
if (rewardsJson.Struct().empty())
return;
2023-09-15 10:06:06 +02:00
2023-09-13 02:14:35 +02:00
std::string fullIdentifier;
std::string metaTypeName;
std::string scope;
std::string identifier;
2023-09-15 10:06:06 +02:00
auto iter = rewardsJson.Struct().begin();
fullIdentifier = iter->first;
ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
2023-09-13 02:14:35 +02:00
if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
return;
2023-09-15 10:06:06 +02:00
2023-09-13 02:14:35 +02:00
int val = 0;
handler.serializeInt(fullIdentifier, val);
Rewardable::VisitInfo vinfo;
auto & reward = vinfo.reward;
2023-09-13 02:14:35 +02:00
if(metaTypeName == "experience")
reward.heroExperience = val;
if(metaTypeName == "mana")
reward.manaDiff = val;
if(metaTypeName == "morale")
reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::MORALE, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
2023-09-13 02:14:35 +02:00
if(metaTypeName == "luck")
reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
2023-09-13 02:14:35 +02:00
if(metaTypeName == "resource")
{
2023-09-13 02:14:35 +02:00
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.resources[rawId] = val;
}
2023-09-13 02:14:35 +02:00
if(metaTypeName == "primarySkill")
{
2023-09-13 02:14:35 +02:00
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.primary.at(rawId) = val;
}
2023-09-13 02:14:35 +02:00
if(metaTypeName == "secondarySkill")
{
2023-09-13 02:14:35 +02:00
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.secondary[rawId] = val;
}
if(metaTypeName == "artifact")
{
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.artifacts.push_back(rawId);
}
if(metaTypeName == "spell")
{
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.spells.push_back(rawId);
}
2023-09-13 02:14:35 +02:00
if(metaTypeName == "creature")
{
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.creatures.emplace_back(rawId, val);
}
vinfo.visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
configuration.info.push_back(vinfo);
2023-09-13 02:14:35 +02:00
}
}
void CGQuestGuard::init(vstd::RNG & rand)
{
blockVisit = true;
quest->textOption = rand.nextInt(3, 5);
quest->completedOption = rand.nextInt(4, 5);
2023-09-13 01:40:07 +02:00
configuration.info.push_back({});
configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
2023-11-02 17:12:58 +02:00
configuration.info.back().reward.removeObject = subID.getNum() == 0 ? true : false;
2023-09-13 01:40:07 +02:00
configuration.canRefuse = true;
}
2023-10-11 23:15:24 +02:00
void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
{
if(!quest->isCompleted)
CGSeerHut::onHeroVisit(h);
else
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, false);
2023-10-11 23:15:24 +02:00
}
bool CGQuestGuard::passableFor(PlayerColor color) const
{
return quest->isCompleted;
}
void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
{
//quest only, do not call base class
quest->serializeJson(handler, "quest");
}
2023-02-12 22:39:17 +02:00
bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
{
return cb->getPlayerState(player)->visitedObjectsGlobal.count({Obj::KEYMASTER, subID}) != 0;
}
std::string CGKeys::getHoverText(PlayerColor player) const
{
return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
}
std::string CGKeys::getObjectName() const
{
return VLC->generaltexth->tentColors[subID.getNum()] + " " + CGObjectInstance::getObjectName();
}
bool CGKeymasterTent::wasVisited (PlayerColor player) const
{
return wasMyColorVisited (player);
}
void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
{
int txt_id;
if (!wasMyColorVisited (h->getOwner()) )
{
ChangeObjectVisitors cow;
cow.mode = ChangeObjectVisitors::VISITOR_GLOBAL;
cow.hero = h->id;
cow.object = id;
cb->sendAndApply(&cow);
txt_id=19;
}
else
txt_id=20;
h->showInfoDialog(txt_id);
}
void CGBorderGuard::initObj(vstd::RNG & rand)
{
blockVisit = true;
}
2023-10-09 19:15:34 +02:00
void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
{
2023-10-09 19:15:34 +02:00
text.appendLocalString(EMetaText::ADVOB_TXT, 18);
}
2023-10-09 19:15:34 +02:00
void CGBorderGuard::getRolloverText(MetaString &text, bool onHover) const
{
if (!onHover)
{
text.appendRawString(VLC->generaltexth->tentColors[subID.getNum()]);
text.appendRawString(" ");
text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
}
}
2016-01-29 08:28:37 +02:00
bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
{
return wasMyColorVisited (h->tempOwner);
}
2016-01-29 08:28:37 +02:00
void CGBorderGuard::onHeroVisit(const CGHeroInstance * h) const
{
if (wasMyColorVisited (h->getOwner()) )
{
BlockingDialog bd (true, false);
bd.player = h->getOwner();
bd.text.appendLocalString (EMetaText::ADVOB_TXT, 17);
cb->showBlockingDialog (&bd);
}
else
{
h->showInfoDialog(18);
AddQuest aq;
aq.quest = QuestInfo (quest, this, visitablePos());
aq.player = h->tempOwner;
cb->sendAndApply (&aq);
//TODO: add this quest only once OR check for multiple instances later
}
}
void CGBorderGuard::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
{
if (answer)
cb->removeObject(this, hero->getOwner());
}
void CGBorderGuard::afterAddToMap(CMap * map)
{
IQuestObject::afterAddToMapCommon(map);
}
2016-01-29 08:28:37 +02:00
void CGBorderGate::onHeroVisit(const CGHeroInstance * h) const //TODO: passability
{
if (!wasMyColorVisited (h->getOwner()) )
{
h->showInfoDialog(18);
AddQuest aq;
aq.quest = QuestInfo (quest, this, visitablePos());
aq.player = h->tempOwner;
cb->sendAndApply (&aq);
}
}
bool CGBorderGate::passableFor(PlayerColor color) const
{
return wasMyColorVisited(color);
}
VCMI_LIB_NAMESPACE_END