1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
/*
* 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"
#include <vcmi/spells/Spell.h>
#include "../ArtifactUtils.h"
#include "../CSoundBase.h"
#include "../texts/CGeneralTextHandler.h"
#include "CGCreature.h"
#include "../IGameCallback.h"
#include "../entities/hero/CHeroHandler.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"
#include "../spells/CSpellHandler.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
//TODO: Remove constructor
CQuest::CQuest():
qid(-1),
isCompleted(false),
lastDay(-1),
killTarget(ObjectInstanceID::NONE),
textOption(0),
completedOption(0),
stackDirection(0),
isCustomFirst(false),
isCustomNext(false),
isCustomComplete(false),
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
"keymaster",
"heroClass",
"reachDate"
};
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",
};
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;
ui32 count = 0;
ui32 slotsCount = 0;
bool hasExtraCreatures = false;
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->getType() == cre->getType())
{
count += it->second->count;
slotsCount++;
}
}
if(static_cast<TQuantity>(count) < cre->count) //not enough creatures of this kind
return false;
hasExtraCreatures |= static_cast<TQuantity>(count) > cre->count;
}
return hasExtraCreatures || slotsCount < army->Slots().size();
}
bool CQuest::checkQuest(const CGHeroInstance * h) const
{
if(!mission.heroAllowed(h))
return false;
if(killTarget.hasValue())
{
PlayerColor owner = h->getOwner();
if (!h->cb->getPlayerState(owner)->destroyedObjects.count(killTarget))
return false;
}
return true;
}
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
for(auto & elem : mission.artifacts)
{
if(h->hasArt(elem))
{
cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(elem, false)));
continue;
}
// perhaps artifact is part of a combined artifact?
const auto * assembly = h->getCombinedArtWithPart(elem);
if (assembly)
{
auto parts = assembly->getPartsInfo(); // FIXME: causes crashes on Google Play
// Remove the assembly
cb->removeArtifact(ArtifactLocation(h->id, h->getArtPos(assembly)));
// Disassemble this backpack artifact
for(const auto & ci : parts)
{
if(ci.art->getTypeId() != elem)
cb->giveHeroNewArtifact(h, ci.art->getTypeId(), ArtifactPosition::BACKPACK_START);
}
continue;
}
logGlobal->error("Failed to find artifact %s in inventory of hero %s", elem.toEntity(VLC)->getJsonKey(), h->getHeroTypeID());
}
cb->takeCreatures(h->id, mission.creatures);
cb->giveResources(h->getOwner(), -mission.resources);
}
void CQuest::addTextReplacements(IGameCallback * cb, MetaString & text, std::vector<Component> & components) const
{
if(mission.heroLevel > 0)
text.replaceNumber(mission.heroLevel);
if(mission.heroExperience > 0)
text.replaceNumber(mission.heroExperience);
{ //primary skills
MetaString loot;
for(int i = 0; i < 4; ++i)
{
if(mission.primary[i])
{
loot.appendRawString("%d %s");
loot.replaceNumber(mission.primary[i]);
loot.replaceRawString(VLC->generaltexth->primarySkillNames[i]);
}
}
for(auto & skill : mission.secondary)
{
loot.appendTextID(VLC->skillh->getById(skill.first)->getNameTextID());
}
for(auto & spell : mission.spells)
{
loot.appendTextID(VLC->spellh->getById(spell)->getNameTextID());
}
if(!loot.empty())
text.replaceRawString(loot.buildList());
}
if(killTarget != ObjectInstanceID::NONE && !heroName.empty())
{
components.emplace_back(ComponentType::HERO_PORTRAIT, heroPortrait);
addKillTargetReplacements(text);
}
if(killTarget != ObjectInstanceID::NONE && stackToKill != CreatureID::NONE)
{
components.emplace_back(ComponentType::CREATURE, stackToKill);
addKillTargetReplacements(text);
}
if(!mission.heroes.empty())
text.replaceRawString(VLC->heroh->getById(mission.heroes.front())->getNameTranslated());
if(!mission.artifacts.empty())
{
MetaString loot;
for(const auto & elem : mission.artifacts)
{
loot.appendRawString("%s");
loot.replaceName(elem);
}
text.replaceRawString(loot.buildList());
}
if(!mission.creatures.empty())
{
MetaString loot;
for(const auto & elem : mission.creatures)
{
loot.appendRawString("%s");
loot.replaceName(elem);
}
text.replaceRawString(loot.buildList());
}
if(mission.resources.nonZero())
{
MetaString loot;
for(auto i : GameResID::ALL_RESOURCES())
{
if(mission.resources[i])
{
loot.appendRawString("%d %s");
loot.replaceNumber(mission.resources[i]);
loot.replaceName(i);
}
}
text.replaceRawString(loot.buildList());
}
if(!mission.players.empty())
{
MetaString loot;
for(auto & p : mission.players)
loot.appendName(p);
text.replaceRawString(loot.buildList());
}
if(lastDay >= 0)
text.replaceNumber(lastDay - cb->getDate(Date::DAY));
}
void CQuest::getVisitText(IGameCallback * cb, MetaString &iwText, std::vector<Component> &components, bool firstVisit, const CGHeroInstance * h) const
{
bool failRequirements = (h ? !checkQuest(h) : true);
mission.loadComponents(components, h);
if(firstVisit)
iwText.appendRawString(firstVisitText.toString());
else if(failRequirements)
iwText.appendRawString(nextVisitText.toString());
if(lastDay >= 0)
iwText.appendTextID(TextIdentifier("core", "seerhut", "time", textOption).get());
addTextReplacements(cb, iwText, components);
}
void CQuest::getRolloverText(IGameCallback * cb, MetaString &ms, bool onHover) const
{
if(onHover)
ms.appendRawString("\n\n");
std::string questState = missionState(onHover ? 3 : 4);
ms.appendTextID(TextIdentifier("core", "seerhut", "quest", questName, questState, textOption).get());
std::vector<Component> components;
addTextReplacements(cb, ms, components);
}
void CQuest::getCompletionText(IGameCallback * cb, MetaString &iwText) const
{
iwText.appendRawString(completedText.toString());
std::vector<Component> components;
addTextReplacements(cb, iwText, components);
}
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);
}
void CQuest::addKillTargetReplacements(MetaString &out) const
{
if(!heroName.empty())
out.replaceRawString(heroName);
if(stackToKill != CreatureID::NONE)
{
out.replaceNamePlural(stackToKill);
out.replaceRawString(VLC->generaltexth->arraytxt[147+stackDirection]);
}
}
void CQuest::serializeJson(JsonSerializeFormat & handler, const std::string & fieldName)
{
auto q = handler.enterStruct(fieldName);
handler.serializeStruct("firstVisitText", firstVisitText);
handler.serializeStruct("nextVisitText", nextVisitText);
handler.serializeStruct("completedText", completedText);
handler.serializeBool("repeatedQuest", repeatedQuest, false);
if(!handler.saving)
{
isCustomFirst = !firstVisitText.empty();
isCustomNext = !nextVisitText.empty();
isCustomComplete = !completedText.empty();
}
handler.serializeInt("timeLimit", lastDay, -1);
handler.serializeStruct("limiter", mission);
handler.serializeInstance("killTarget", killTarget, ObjectInstanceID::NONE);
if(!handler.saving) //compatibility with legacy vmaps
{
std::string missionType = "None";
handler.serializeString("missionType", missionType);
if(missionType == "None")
return;
if(missionType == "Level")
handler.serializeInt("heroLevel", mission.heroLevel);
if(missionType == "PrimaryStat")
{
auto primarySkills = handler.enterStruct("primarySkills");
for(int i = 0; i < GameConstants::PRIMARY_SKILLS; ++i)
handler.serializeInt(NPrimarySkill::names[i], mission.primary[i], 0);
}
if(missionType == "Artifact")
handler.serializeIdArray<ArtifactID>("artifacts", mission.artifacts);
if(missionType == "Army")
{
auto a = handler.enterArray("creatures");
a.serializeStruct(mission.creatures);
}
if(missionType == "Resources")
{
auto r = handler.enterStruct("resources");
for(size_t idx = 0; idx < (GameConstants::RESOURCE_QUANTITY - 1); idx++)
{
handler.serializeInt(GameConstants::RESOURCE_NAMES[idx], mission.resources[idx], 0);
}
}
if(missionType == "Hero")
{
HeroTypeID temp;
handler.serializeId("hero", temp, HeroTypeID::NONE);
mission.heroes.emplace_back(temp);
}
if(missionType == "Player")
{
PlayerColor temp;
handler.serializeId("player", temp, PlayerColor::NEUTRAL);
mission.players.emplace_back(temp);
}
}
}
bool IQuestObject::checkQuest(const CGHeroInstance* h) const
{
return quest->checkQuest(h);
}
void CGSeerHut::getVisitText(MetaString &text, std::vector<Component> &components, bool FirstVisit, const CGHeroInstance * h) const
{
quest->getVisitText(cb, text, components, FirstVisit, h);
}
void IQuestObject::afterAddToMapCommon(CMap * map) const
{
map->addNewQuestInstance(quest);
}
void CGSeerHut::setObjToKill()
{
if(quest->killTarget == ObjectInstanceID::NONE)
return;
if(getCreatureToKill(true))
{
quest->stackToKill = getCreatureToKill(false)->getCreatureID();
assert(quest->stackToKill != CreatureID::NONE);
quest->stackDirection = checkDirection();
}
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");
auto seerNameID = *RandomGeneratorUtil::nextItem(names, rand);
seerName = VLC->generaltexth->translate(seerNameID);
quest->textOption = rand.nextInt(2);
quest->completedOption = rand.nextInt(1, 3);
configuration.canRefuse = true;
configuration.visitMode = Rewardable::EVisitMode::VISIT_ONCE;
configuration.selectMode = Rewardable::ESelectMode::SELECT_PLAYER;
}
void CGSeerHut::initObj(vstd::RNG & rand)
{
init(rand);
CRewardableObject::initObj(rand);
setObjToKill();
quest->defineQuestName();
if(quest->mission == Rewardable::Limiter{} && quest->killTarget == ObjectInstanceID::NONE)
quest->isCompleted = true;
if(quest->questName == quest->missionName(EQuestMission::NONE))
{
quest->firstVisitText.appendTextID(TextIdentifier("core", "seehut", "empty", quest->completedOption).get());
}
else
{
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)
quest->completedText.appendTextID(TextIdentifier("core", "seerhut", "quest", quest-> questName, quest->missionState(2), quest->textOption).get());
}
quest->getCompletionText(cb, configuration.onSelect);
for(auto & i : configuration.info)
quest->getCompletionText(cb, i.message);
}
void CGSeerHut::getRolloverText(MetaString &text, bool onHover) const
{
quest->getRolloverText(cb, text, onHover);//TODO: simplify?
if(!onHover)
text.replaceRawString(seerName);
}
std::string CGSeerHut::getHoverText(PlayerColor player) const
{
std::string hoverName = getObjectName();
if(ID == Obj::SEER_HUT && quest->activeForPlayers.count(player))
{
hoverName = VLC->generaltexth->allTexts[347];
boost::algorithm::replace_first(hoverName, "%s", seerName);
}
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)
{
switch(what)
{
case ObjProperty::SEERHUT_VISITED:
{
quest->activeForPlayers.emplace(identifier.as<PlayerColor>());
break;
}
case ObjProperty::SEERHUT_COMPLETE:
{
quest->isCompleted = identifier.getNum();
quest->activeForPlayers.clear();
break;
}
}
}
void CGSeerHut::newTurn(vstd::RNG & rand) const
{
CRewardableObject::newTurn(rand);
if(quest->lastDay >= 0 && quest->lastDay <= cb->getDate() - 1) //time is up
{
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, true);
}
}
void CGSeerHut::onHeroVisit(const CGHeroInstance * h) const
{
InfoWindow iw;
iw.player = h->getOwner();
if(!quest->isCompleted)
{
bool firstVisit = !quest->activeForPlayers.count(h->getOwner());
bool failRequirements = !checkQuest(h);
if(firstVisit)
{
cb->setObjPropertyID(id, ObjProperty::SEERHUT_VISITED, h->getOwner());
AddQuest aq;
aq.quest = QuestInfo (quest, this, visitablePos());
aq.player = h->tempOwner;
cb->sendAndApply(aq); //TODO: merge with setObjProperty?
}
if(firstVisit || failRequirements)
{
getVisitText (iw.text, iw.components, firstVisit, h);
cb->showInfoDialog(&iw);
}
if(!failRequirements) // propose completion, also on first visit
{
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)->visitablePos();
if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.34) //north
{
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //northwest
return 8;
else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //north
return 1;
else //northeast
return 2;
}
else if(static_cast<double>(cord.x) / static_cast<double>(cb->getMapSize().x) < 0.67) //horizontal
{
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //west
return 7;
else if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.67) //central
return 9;
else //east
return 3;
}
else //south
{
if(static_cast<double>(cord.y) / static_cast<double>(cb->getMapSize().y) < 0.34) //southwest
return 6;
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;
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;
return dynamic_cast<const CGCreature *>(o);
}
void CGSeerHut::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
{
CRewardableObject::blockingDialogAnswered(hero, answer);
if(answer)
{
quest->completeQuest(cb, hero);
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, !quest->repeatedQuest); //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)
{
//backward compatibility for VCMI maps that use old SeerHut format
auto s = handler.enterStruct("reward");
const JsonNode & rewardsJson = handler.getCurrent();
if (rewardsJson.Struct().empty())
return;
std::string fullIdentifier;
std::string metaTypeName;
std::string scope;
std::string identifier;
auto iter = rewardsJson.Struct().begin();
fullIdentifier = iter->first;
ModUtility::parseIdentifier(fullIdentifier, scope, metaTypeName, identifier);
if(!std::set<std::string>{"resource", "primarySkill", "secondarySkill", "artifact", "spell", "creature", "experience", "mana", "morale", "luck"}.count(metaTypeName))
return;
int val = 0;
handler.serializeInt(fullIdentifier, val);
Rewardable::VisitInfo vinfo;
auto & reward = vinfo.reward;
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));
if(metaTypeName == "luck")
reward.bonuses.emplace_back(BonusDuration::ONE_BATTLE, BonusType::LUCK, BonusSource::OBJECT_INSTANCE, val, BonusSourceID(id));
if(metaTypeName == "resource")
{
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.resources[rawId] = val;
}
if(metaTypeName == "primarySkill")
{
auto rawId = *VLC->identifiers()->getIdentifier(ModScope::scopeMap(), fullIdentifier, false);
reward.primary.at(rawId) = val;
}
if(metaTypeName == "secondarySkill")
{
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);
}
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);
}
}
void CGQuestGuard::init(vstd::RNG & rand)
{
blockVisit = true;
quest->textOption = rand.nextInt(3, 5);
quest->completedOption = rand.nextInt(4, 5);
configuration.info.push_back({});
configuration.info.back().visitType = Rewardable::EEventType::EVENT_FIRST_VISIT;
configuration.info.back().reward.removeObject = subID.getNum() == 0 ? true : false;
configuration.canRefuse = true;
}
void CGQuestGuard::onHeroVisit(const CGHeroInstance * h) const
{
if(!quest->isCompleted)
CGSeerHut::onHeroVisit(h);
else
cb->setObjPropertyValue(id, ObjProperty::SEERHUT_COMPLETE, false);
}
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");
}
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();
}
std::string CGKeys::getObjectDescription(PlayerColor player) const
{
return visitedTxt(wasMyColorVisited(player));
}
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;
}
void CGBorderGuard::getVisitText(MetaString &text, std::vector<Component> &components, 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.getNum()]);
text.appendRawString(" ");
text.appendRawString(VLC->objtypeh->getObjectName(Obj::KEYMASTER, subID));
}
}
bool CGBorderGuard::checkQuest(const CGHeroInstance * h) const
{
return wasMyColorVisited (h->tempOwner);
}
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 (this, &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, int32_t answer) const
{
if (answer)
cb->removeObject(this, hero->getOwner());
}
void CGBorderGuard::afterAddToMap(CMap * map)
{
IQuestObject::afterAddToMapCommon(map);
}
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
|