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

993 lines
25 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 "../NetPacks.h"
#include "../CSoundBase.h"
#include "../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 "../CSkillHandler.h"
#include "../mapping/CMap.h"
#include "../modding/ModScope.h"
#include "../modding/ModUtility.h"
VCMI_LIB_NAMESPACE_BEGIN
std::map <PlayerColor, std::set <ui8> > CGKeys::playerKeyMap;
2023-02-12 22:39:17 +02:00
//TODO: Remove constructor
CQuest::CQuest():
qid(-1),
missionType(MISSION_NONE),
progress(NOT_ACTIVE),
lastDay(-1),
2023-10-09 05:24:40 +02:00
killTarget(-1),
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),
repeatedQuest(false)
{
}
static std::string visitedTxt(const bool visited)
{
int id = visited ? 352 : 353;
return VLC->generaltexth->allTexts[id];
}
const std::string & CQuest::missionName(CQuest::Emission mission)
{
static const std::array<std::string, 11> names = {
"empty",
"heroLevel",
"primarySkill",
"killHero",
"killCreature",
"bringArt",
"bringCreature",
"bringResources",
"bringHero",
"bringPlayer",
2022-12-28 00:47:13 +02:00
"keymaster"
};
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-09 05:24:40 +02:00
for(cre = q->creatures.begin(); cre != q->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-09 05:24:40 +02:00
if(!heroAllowed(h))
return false;
if(killTarget >= 0)
{
2023-10-09 05:24:40 +02:00
if(!CGHeroInstance::cb->getObjByQuestIdentifier(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
{
2023-10-09 05:24:40 +02:00
for(auto & elem : 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, h->getArtPos(elem, false)));
}
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, 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-09 05:24:40 +02:00
cb->takeCreatures(h->id, creatures);
cb->giveResources(h->getOwner(), resources);
2023-10-09 02:19:58 +02:00
}
2016-01-29 08:28:37 +02:00
void CQuest::getVisitText(MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
{
2023-09-27 23:49:27 +02:00
MetaString text;
bool failRequirements = (h ? !checkQuest(h) : true);
2023-10-09 05:24:40 +02:00
loadComponents(components, h);
2016-01-29 08:28:37 +02:00
if(firstVisit)
{
isCustom = isCustomFirst;
text = firstVisitText;
2023-09-27 23:49:27 +02:00
iwText.appendRawString(text.toString());
}
2016-01-29 08:28:37 +02:00
else if(failRequirements)
{
isCustom = isCustomNext;
text = nextVisitText;
2023-09-27 23:49:27 +02:00
iwText.appendRawString(text.toString());
}
switch (missionType)
{
case MISSION_LEVEL:
2016-01-29 08:28:37 +02:00
if(!isCustom)
2023-10-09 05:24:40 +02:00
iwText.replaceNumber(heroLevel); //TODO: heroLevel
break;
case MISSION_PRIMARY_STAT:
{
MetaString loot;
2016-01-29 08:28:37 +02:00
for(int i = 0; i < 4; ++i)
{
2023-10-09 05:24:40 +02:00
if(primary[i])
{
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(primary[i]);
loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
}
}
if (!isCustom)
iwText.replaceRawString(loot.buildList());
}
break;
case MISSION_KILL_HERO:
components.emplace_back(Component::EComponentType::HERO_PORTRAIT, heroPortrait, 0, 0);
2016-01-29 08:28:37 +02:00
if(!isCustom)
2023-09-27 23:49:27 +02:00
addReplacements(iwText, text.toString());
break;
case MISSION_HERO:
2023-10-09 05:24:40 +02:00
if(!isCustom && !heroes.empty())
iwText.replaceRawString(VLC->heroh->getById(heroes.front())->getNameTranslated());
break;
case MISSION_KILL_CREATURE:
{
2023-02-12 22:39:17 +02:00
components.emplace_back(stackToKill);
2016-01-29 08:28:37 +02:00
if(!isCustom)
{
2023-09-27 23:49:27 +02:00
addReplacements(iwText, text.toString());
}
}
break;
case MISSION_ART:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : artifacts)
{
loot.appendRawString("%s");
loot.replaceLocalString(EMetaText::ART_NAMES, elem);
}
2016-01-29 08:28:37 +02:00
if(!isCustom)
iwText.replaceRawString(loot.buildList());
}
break;
case MISSION_ARMY:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : creatures)
{
loot.appendRawString("%s");
loot.replaceCreatureName(elem);
}
2016-01-29 08:28:37 +02:00
if(!isCustom)
iwText.replaceRawString(loot.buildList());
}
break;
case MISSION_RESOURCES:
{
MetaString loot;
2016-01-29 08:28:37 +02:00
for(int i = 0; i < 7; ++i)
{
2023-10-09 05:24:40 +02:00
if(resources[i])
{
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(resources[i]);
loot.replaceLocalString(EMetaText::RES_NAMES, i);
}
}
2016-01-29 08:28:37 +02:00
if(!isCustom)
iwText.replaceRawString(loot.buildList());
}
break;
case MISSION_PLAYER:
2023-10-09 05:24:40 +02:00
if(!isCustom && !players.empty())
iwText.replaceLocalString(EMetaText::COLOR, players.front());
break;
}
}
2016-01-29 08:28:37 +02:00
void CQuest::getRolloverText(MetaString &ms, bool onHover) const
{
// Quests with MISSION_NONE type don't have a text for them
assert(missionType != MISSION_NONE);
2016-01-29 08:28:37 +02:00
if(onHover)
ms.appendRawString("\n\n");
2023-04-13 18:06:44 +02:00
std::string questName = missionName(missionType);
std::string questState = missionState(onHover ? 3 : 4);
ms.appendRawString(VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption));
2016-01-29 08:28:37 +02:00
switch(missionType)
{
case MISSION_LEVEL:
2023-10-09 05:24:40 +02:00
ms.replaceNumber(heroLevel);
break;
case MISSION_PRIMARY_STAT:
{
MetaString loot;
for (int i = 0; i < 4; ++i)
{
2023-10-09 05:24:40 +02:00
if (primary[i])
{
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(primary[i]);
loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
}
}
ms.replaceRawString(loot.buildList());
}
break;
case MISSION_KILL_HERO:
ms.replaceRawString(heroName);
break;
case MISSION_KILL_CREATURE:
ms.replaceCreatureName(stackToKill);
break;
case MISSION_ART:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : artifacts)
{
loot.appendRawString("%s");
loot.replaceLocalString(EMetaText::ART_NAMES, elem);
}
ms.replaceRawString(loot.buildList());
}
break;
case MISSION_ARMY:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : creatures)
{
loot.appendRawString("%s");
loot.replaceCreatureName(elem);
}
ms.replaceRawString(loot.buildList());
}
break;
case MISSION_RESOURCES:
{
MetaString loot;
for (int i = 0; i < 7; ++i)
{
2023-10-09 05:24:40 +02:00
if (resources[i])
{
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(resources[i]);
loot.replaceLocalString(EMetaText::RES_NAMES, i);
}
}
ms.replaceRawString(loot.buildList());
}
break;
case MISSION_HERO:
2023-10-09 05:24:40 +02:00
ms.replaceRawString(VLC->heroh->getById(heroes.front())->getNameTranslated());
break;
case MISSION_PLAYER:
2023-10-09 05:24:40 +02:00
ms.replaceRawString(VLC->generaltexth->colors[players.front()]);
break;
default:
break;
}
}
2023-09-13 01:40:07 +02:00
void CQuest::getCompletionText(MetaString &iwText) const
{
2023-09-27 23:49:27 +02:00
iwText.appendRawString(completedText.toString());
2016-01-29 08:28:37 +02:00
switch(missionType)
{
case CQuest::MISSION_LEVEL:
if (!isCustomComplete)
2023-10-09 05:24:40 +02:00
iwText.replaceNumber(heroLevel);
break;
case CQuest::MISSION_PRIMARY_STAT:
2023-09-27 23:49:27 +02:00
{
MetaString loot;
2023-10-09 05:24:40 +02:00
assert(primary.size() <= 4);
for (int i = 0; i < primary.size(); ++i)
{
2023-10-09 05:24:40 +02:00
if (primary[i])
{
2023-09-27 23:49:27 +02:00
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(primary[i]);
2023-09-27 23:49:27 +02:00
loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
}
}
2023-09-27 23:49:27 +02:00
if (!isCustomComplete)
iwText.replaceRawString(loot.buildList());
break;
2023-09-27 23:49:27 +02:00
}
case CQuest::MISSION_ART:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : artifacts)
{
loot.appendRawString("%s");
loot.replaceLocalString(EMetaText::ART_NAMES, elem);
}
if (!isCustomComplete)
iwText.replaceRawString(loot.buildList());
}
break;
case CQuest::MISSION_ARMY:
{
MetaString loot;
2023-10-09 05:24:40 +02:00
for(const auto & elem : creatures)
{
loot.appendRawString("%s");
loot.replaceCreatureName(elem);
}
if (!isCustomComplete)
iwText.replaceRawString(loot.buildList());
}
break;
case CQuest::MISSION_RESOURCES:
{
MetaString loot;
for (int i = 0; i < 7; ++i)
{
2023-10-09 05:24:40 +02:00
if (resources[i])
{
loot.appendRawString("%d %s");
2023-10-09 05:24:40 +02:00
loot.replaceNumber(resources[i]);
loot.replaceLocalString(EMetaText::RES_NAMES, i);
}
}
if (!isCustomComplete)
iwText.replaceRawString(loot.buildList());
}
break;
case MISSION_KILL_HERO:
case MISSION_KILL_CREATURE:
if (!isCustomComplete)
2023-09-27 23:49:27 +02:00
addReplacements(iwText, completedText.toString());
break;
case MISSION_HERO:
2023-10-09 05:24:40 +02:00
if (!isCustomComplete && !heroes.empty())
iwText.replaceRawString(VLC->heroh->getById(heroes.front())->getNameTranslated());
break;
case MISSION_PLAYER:
2023-10-09 05:24:40 +02:00
if (!isCustomComplete && !players.empty())
iwText.replaceRawString(VLC->generaltexth->colors[players.front()]);
break;
}
}
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
Rewardable::Limiter::serializeJson(handler);
static const std::vector<std::string> MISSION_TYPE_JSON =
{
"None", "Level", "PrimaryStat", "KillHero", "KillCreature", "Artifact", "Army", "Resources", "Hero", "Player"
};
handler.serializeEnum("missionType", missionType, Emission::MISSION_NONE, MISSION_TYPE_JSON);
handler.serializeInt("timeLimit", lastDay, -1);
2023-10-09 05:24:40 +02:00
handler.serializeInstance<int>("killTarget", killTarget, -1);
2023-10-09 05:24:40 +02:00
if(!handler.saving)
{
2023-10-09 05:24:40 +02:00
switch (missionType)
2023-04-13 18:06:44 +02:00
{
2023-10-09 05:24:40 +02:00
case MISSION_NONE:
break;
case MISSION_LEVEL:
handler.serializeInt("heroLevel", heroLevel, -1);
break;
case MISSION_PRIMARY_STAT:
{
auto primarySkills = handler.enterStruct("primarySkills");
for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
handler.serializeInt(NPrimarySkill::names[i], primary[i], 0);
}
break;
case MISSION_KILL_HERO:
case MISSION_KILL_CREATURE:
break;
case MISSION_ART:
handler.serializeIdArray<ArtifactID>("artifacts", artifacts);
break;
case MISSION_ARMY:
{
auto a = handler.enterArray("creatures");
a.serializeStruct(creatures);
}
break;
case MISSION_RESOURCES:
{
auto r = handler.enterStruct("resources");
2023-10-09 05:24:40 +02:00
for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
{
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], resources[idx], 0);
}
}
break;
case MISSION_HERO:
{
ui32 temp;
handler.serializeId<ui32, ui32, HeroTypeID>("hero", temp, 0);
heroes.emplace_back(temp);
}
break;
case MISSION_PLAYER:
{
2023-10-09 05:24:40 +02:00
ui32 temp;
handler.serializeId<ui32, ui32, PlayerColor>("player", temp, PlayerColor::NEUTRAL);
players.emplace_back(temp);
}
2023-10-09 05:24:40 +02:00
break;
default:
logGlobal->error("Invalid quest mission type");
break;
2023-04-13 18:06:44 +02:00
}
}
}
void CGSeerHut::setObjToKill()
{
2016-01-29 08:28:37 +02:00
if(quest->missionType == CQuest::MISSION_KILL_CREATURE)
{
quest->stackToKill = getCreatureToKill(false)->getStack(SlotID(0)); //FIXME: stacks tend to disappear (desync?) on server :?
assert(quest->stackToKill.type);
quest->stackToKill.count = 0; //no count in info window
quest->stackDirection = checkDirection();
}
2016-01-29 08:28:37 +02:00
else if(quest->missionType == CQuest::MISSION_KILL_HERO)
{
quest->heroName = getHeroToKill(false)->getNameTranslated();
quest->heroPortrait = getHeroToKill(false)->getPortraitSource();
}
quest->getCompletionText(configuration.onSelect);
for(auto & i : configuration.info)
quest->getCompletionText(i.message);
}
void CGSeerHut::init(CRandomGenerator & 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(CRandomGenerator & rand)
{
init(rand);
2023-09-13 01:40:07 +02:00
CRewardableObject::initObj(rand);
quest->progress = CQuest::NOT_ACTIVE;
2016-01-29 08:28:37 +02:00
if(quest->missionType)
{
2023-01-10 16:24:59 +02:00
std::string questName = quest->missionName(quest->missionType);
2016-01-29 08:28:37 +02:00
if(!quest->isCustomFirst)
2023-09-27 23:49:27 +02:00
quest->firstVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, quest->missionState(0), quest->textOption).get());
2016-01-29 08:28:37 +02:00
if(!quest->isCustomNext)
2023-10-09 02:19:58 +02:00
quest->nextVisitText.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, quest->missionState(1), quest->textOption).get());
2016-01-29 08:28:37 +02:00
if(!quest->isCustomComplete)
2023-10-09 02:19:58 +02:00
quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, quest->missionState(2), quest->textOption).get());
}
else
{
quest->progress = CQuest::COMPLETE;
2023-09-27 23:49:27 +02:00
quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
}
}
2016-01-29 08:28:37 +02:00
void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
{
quest->getRolloverText (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();
2016-01-29 08:28:37 +02:00
if(ID == Obj::SEER_HUT && quest->progress != CQuest::NOT_ACTIVE)
{
hoverName = VLC->generaltexth->allTexts[347];
2016-01-29 08:28:37 +02:00
boost::algorithm::replace_first(hoverName, "%s", seerName);
}
2016-01-29 08:28:37 +02:00
if(quest->progress & quest->missionType) //rollover when the quest is active
{
MetaString ms;
getRolloverText (ms, true);
hoverName += ms.toString();
}
return hoverName;
}
void CQuest::addReplacements(MetaString &out, const std::string &base) const
{
switch(missionType)
{
case MISSION_KILL_CREATURE:
2023-09-28 02:51:58 +02:00
if(stackToKill.type)
{
2023-09-28 02:51:58 +02:00
out.replaceCreatureName(stackToKill);
if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
{
out.replaceRawString(VLC->generaltexth->arraytxt[147+stackDirection]);
}
}
break;
case MISSION_KILL_HERO:
2023-09-27 23:49:27 +02:00
out.replaceTextID(heroName);
break;
}
}
bool IQuestObject::checkQuest(const CGHeroInstance* h) const
{
return quest->checkQuest(h);
}
void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
{
quest->getVisitText (text,components, isCustom, FirstVisit, h);
}
2023-02-12 22:39:17 +02:00
void IQuestObject::afterAddToMapCommon(CMap * map) const
{
map->addNewQuestInstance(quest);
}
void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
{
2016-01-29 08:28:37 +02:00
switch(what)
{
case 10:
quest->progress = static_cast<CQuest::Eprogress>(val);
break;
}
}
void CGSeerHut::newTurn(CRandomGenerator & 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->setObjProperty (id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE);
}
}
2016-01-29 08:28:37 +02:00
void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
{
InfoWindow iw;
iw.player = h->getOwner();
2016-01-29 08:28:37 +02:00
if(quest->progress < CQuest::COMPLETE)
{
bool firstVisit = !quest->progress;
bool failRequirements = !checkQuest(h);
2016-01-29 08:28:37 +02:00
bool isCustom = false;
2016-01-29 08:28:37 +02:00
if(firstVisit)
{
isCustom = quest->isCustomFirst;
2016-01-29 08:28:37 +02:00
cb->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::IN_PROGRESS);
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
else if(failRequirements)
{
isCustom = quest->isCustomNext;
}
2016-01-29 08:28:37 +02:00
if(firstVisit || failRequirements)
{
getVisitText (iw.text, iw.components, isCustom, 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()->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
{
2023-10-09 05:24:40 +02:00
const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
if(allowNull && !o)
return nullptr;
assert(o && (o->ID == Obj::HERO || o->ID == Obj::PRISON));
2023-02-12 22:39:17 +02:00
return dynamic_cast<const CGHeroInstance *>(o);
}
const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
{
2023-10-09 05:24:40 +02:00
const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->killTarget);
if(allowNull && !o)
return nullptr;
assert(o && o->ID == Obj::MONSTER);
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->setObjProperty(id, CGSeerHut::OBJPROP_VISITED, CQuest::COMPLETE); //mission complete
}
}
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();
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, val, id);
if(metaTypeName == "luck")
reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT, val, id);
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(CRandomGenerator & 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;
configuration.info.back().reward.removeObject = true;
2023-09-13 01:40:07 +02:00
configuration.canRefuse = true;
}
void CGQuestGuard::serializeJsonOptions(JsonSerializeFormat & handler)
{
//quest only, do not call base class
quest->serializeJson(handler, "quest");
}
void CGKeys::reset()
{
playerKeyMap.clear();
}
void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
{
if (what >= 101 && what <= (100 + PlayerColor::PLAYER_LIMIT_I))
{
PlayerColor player(what-101);
2023-02-12 22:39:17 +02:00
playerKeyMap[player].insert(static_cast<ui8>(val));
}
else
2023-02-12 22:39:17 +02:00
logGlobal->error("Unexpected properties requested to set: what=%d, val=%d", static_cast<int>(what), val);
}
2023-02-12 22:39:17 +02:00
bool CGKeys::wasMyColorVisited(const PlayerColor & player) const
{
2023-02-12 22:39:17 +02:00
return playerKeyMap.count(player) && vstd::contains(playerKeyMap[player], subID);
}
std::string CGKeys::getHoverText(PlayerColor player) const
{
return getObjectName() + "\n" + visitedTxt(wasMyColorVisited(player));
}
std::string CGKeys::getObjectName() const
{
return VLC->generaltexth->tentColors[subID] + " " + 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()) )
{
cb->setObjProperty(id, h->tempOwner.getNum()+101, subID);
txt_id=19;
}
else
txt_id=20;
h->showInfoDialog(txt_id);
}
void CGBorderGuard::initObj(CRandomGenerator & rand)
{
blockVisit = true;
}
void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
{
text.appendLocalString(EMetaText::ADVOB_TXT,18);
}
void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
{
if (!onHover)
{
text.appendRawString(VLC->generaltexth->tentColors[subID]);
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