2023-04-26 20:55:56 +02:00
|
|
|
/*
|
|
|
|
* CGTownBuilding.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 "CGTownBuilding.h"
|
|
|
|
#include "CGTownInstance.h"
|
|
|
|
#include "../CGeneralTextHandler.h"
|
|
|
|
#include "../NetPacks.h"
|
|
|
|
#include "../IGameCallback.h"
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../gameState/CGameState.h"
|
2023-04-26 20:55:56 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
PlayerColor CGTownBuilding::getOwner() const
|
|
|
|
{
|
|
|
|
return town->getOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CGTownBuilding::getObjGroupIndex() const
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t CGTownBuilding::getObjTypeIndex() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGTownBuilding::visitablePos() const
|
|
|
|
{
|
|
|
|
return town->visitablePos();
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGTownBuilding::getPosition() const
|
|
|
|
{
|
|
|
|
return town->getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGTownBuilding::getVisitingBonusGreeting() const
|
|
|
|
{
|
|
|
|
auto bonusGreeting = town->getTown()->getGreeting(bType);
|
|
|
|
|
|
|
|
if(!bonusGreeting.empty())
|
|
|
|
return bonusGreeting;
|
|
|
|
|
|
|
|
switch(bType)
|
|
|
|
{
|
|
|
|
case BuildingSubID::MANA_VORTEX:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingManaVortex"));
|
|
|
|
break;
|
|
|
|
case BuildingSubID::KNOWLEDGE_VISITING_BONUS:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingKnowledge"));
|
|
|
|
break;
|
|
|
|
case BuildingSubID::SPELL_POWER_VISITING_BONUS:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingSpellPower"));
|
|
|
|
break;
|
|
|
|
case BuildingSubID::ATTACK_VISITING_BONUS:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingAttack"));
|
|
|
|
break;
|
|
|
|
case BuildingSubID::EXPERIENCE_VISITING_BONUS:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingExperience"));
|
|
|
|
break;
|
|
|
|
case BuildingSubID::DEFENSE_VISITING_BONUS:
|
|
|
|
bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingDefence"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
|
|
|
|
|
|
|
|
if(bonusGreeting.empty())
|
|
|
|
{
|
|
|
|
bonusGreeting = "Error: Bonus greeting for '%s' is not localized.";
|
|
|
|
logGlobal->error("'%s' building of '%s' faction has not localized bonus greeting.", buildingName, town->getTown()->faction->getNameTranslated());
|
|
|
|
}
|
|
|
|
boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
|
|
|
|
town->getTown()->setGreeting(bType, bonusGreeting);
|
|
|
|
return bonusGreeting;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CGTownBuilding::getCustomBonusGreeting(const Bonus & bonus) const
|
|
|
|
{
|
2023-05-01 00:20:01 +02:00
|
|
|
if(bonus.type == BonusType::TOWN_MAGIC_WELL)
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingInTownMagicWell"));
|
|
|
|
auto buildingName = town->getTown()->getSpecialBuilding(bType)->getNameTranslated();
|
|
|
|
boost::algorithm::replace_first(bonusGreeting, "%s", buildingName);
|
|
|
|
return bonusGreeting;
|
|
|
|
}
|
|
|
|
auto bonusGreeting = std::string(VLC->generaltexth->translate("vcmi.townHall.greetingCustomBonus")); //"%s gives you +%d %s%s"
|
|
|
|
std::string param;
|
|
|
|
std::string until;
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
if(bonus.type == BonusType::MORALE)
|
2023-04-26 20:55:56 +02:00
|
|
|
param = VLC->generaltexth->allTexts[384];
|
2023-05-01 00:20:01 +02:00
|
|
|
else if(bonus.type == BonusType::LUCK)
|
2023-04-26 20:55:56 +02:00
|
|
|
param = VLC->generaltexth->allTexts[385];
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
until = bonus.duration == BonusDuration::ONE_BATTLE
|
2023-04-26 20:55:56 +02:00
|
|
|
? VLC->generaltexth->translate("vcmi.townHall.greetingCustomUntil")
|
|
|
|
: ".";
|
|
|
|
|
|
|
|
boost::format fmt = boost::format(bonusGreeting) % bonus.description % bonus.val % param % until;
|
|
|
|
std::string greeting = fmt.str();
|
|
|
|
return greeting;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COPWBonus::COPWBonus(const BuildingID & bid, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
|
|
|
|
{
|
|
|
|
bID = bid;
|
|
|
|
bType = subId;
|
|
|
|
town = cgTown;
|
|
|
|
indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void COPWBonus::setProperty(ui8 what, ui32 val)
|
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case ObjProperty::VISITORS:
|
|
|
|
visitors.insert(val);
|
|
|
|
break;
|
|
|
|
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
|
|
|
|
visitors.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
ObjectInstanceID heroID = h->id;
|
|
|
|
if(town->hasBuilt(bID))
|
|
|
|
{
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
|
|
|
|
|
|
|
switch (this->bType)
|
|
|
|
{
|
|
|
|
case BuildingSubID::STABLES:
|
2023-05-01 00:20:01 +02:00
|
|
|
if(!h->hasBonusFrom(BonusSource::OBJECT, Obj::STABLES)) //does not stack with advMap Stables
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
GiveBonus gb;
|
2023-05-01 00:20:01 +02:00
|
|
|
gb.bonus = Bonus(BonusDuration::ONE_WEEK, BonusType::MOVEMENT, BonusSource::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100], 1);
|
2023-04-26 20:55:56 +02:00
|
|
|
gb.id = heroID.getNum();
|
|
|
|
cb->giveHeroBonus(&gb);
|
|
|
|
|
|
|
|
SetMovePoints mp;
|
|
|
|
mp.val = 600;
|
|
|
|
mp.absolute = false;
|
|
|
|
mp.hid = heroID;
|
|
|
|
cb->setMovePoints(&mp);
|
|
|
|
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(VLC->generaltexth->allTexts[580]);
|
2023-04-26 20:55:56 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::MANA_VORTEX:
|
|
|
|
if(visitors.empty())
|
|
|
|
{
|
|
|
|
if(h->mana < h->manaLimit() * 2)
|
|
|
|
cb->setManaPoints (heroID, 2 * h->manaLimit());
|
|
|
|
//TODO: investigate line below
|
|
|
|
//cb->setObjProperty (town->id, ObjProperty::VISITED, true);
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(getVisitingBonusGreeting());
|
2023-04-26 20:55:56 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
//extra visit penalty if hero alredy had double mana points (or even more?!)
|
|
|
|
town->addHeroToStructureVisitors(h, indexOnTV);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CTownBonus::CTownBonus(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown)
|
|
|
|
{
|
|
|
|
bID = index;
|
|
|
|
bType = subId;
|
|
|
|
town = cgTown;
|
|
|
|
indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownBonus::setProperty (ui8 what, ui32 val)
|
|
|
|
{
|
|
|
|
if(what == ObjProperty::VISITORS)
|
|
|
|
visitors.insert(ObjectInstanceID(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
ObjectInstanceID heroID = h->id;
|
|
|
|
if(town->hasBuilt(bID) && visitors.find(heroID) == visitors.end())
|
|
|
|
{
|
|
|
|
si64 val = 0;
|
|
|
|
InfoWindow iw;
|
|
|
|
PrimarySkill::PrimarySkill what = PrimarySkill::NONE;
|
|
|
|
|
|
|
|
switch(bType)
|
|
|
|
{
|
|
|
|
case BuildingSubID::KNOWLEDGE_VISITING_BONUS: //wall of knowledge
|
|
|
|
what = PrimarySkill::KNOWLEDGE;
|
|
|
|
val = 1;
|
|
|
|
iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 3, 1, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::SPELL_POWER_VISITING_BONUS: //order of fire
|
|
|
|
what = PrimarySkill::SPELL_POWER;
|
|
|
|
val = 1;
|
|
|
|
iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 2, 1, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::ATTACK_VISITING_BONUS: //hall of Valhalla
|
|
|
|
what = PrimarySkill::ATTACK;
|
|
|
|
val = 1;
|
|
|
|
iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 0, 1, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::EXPERIENCE_VISITING_BONUS: //academy of battle scholars
|
|
|
|
what = PrimarySkill::EXPERIENCE;
|
|
|
|
val = static_cast<int>(h->calculateXp(1000));
|
|
|
|
iw.components.emplace_back(Component::EComponentType::EXPERIENCE, 0, val, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::DEFENSE_VISITING_BONUS: //cage of warlords
|
|
|
|
what = PrimarySkill::DEFENSE;
|
|
|
|
val = 1;
|
|
|
|
iw.components.emplace_back(Component::EComponentType::PRIM_SKILL, 1, 1, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BuildingSubID::CUSTOM_VISITING_BONUS:
|
|
|
|
const auto building = town->getTown()->buildings.at(bID);
|
2023-05-01 00:20:01 +02:00
|
|
|
if(!h->hasBonusFrom(BonusSource::TOWN_STRUCTURE, Bonus::getSid32(building->town->faction->getIndex(), building->bid)))
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
const auto & bonuses = building->onVisitBonuses;
|
|
|
|
applyBonuses(const_cast<CGHeroInstance *>(h), bonuses);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(what != PrimarySkill::NONE)
|
|
|
|
{
|
|
|
|
iw.player = cb->getOwner(heroID);
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(getVisitingBonusGreeting());
|
2023-04-26 20:55:56 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
cb->changePrimSkill (cb->getHero(heroID), what, val);
|
|
|
|
town->addHeroToStructureVisitors(h, indexOnTV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownBonus::applyBonuses(CGHeroInstance * h, const BonusList & bonuses) const
|
|
|
|
{
|
|
|
|
auto addToVisitors = false;
|
|
|
|
|
|
|
|
for(const auto & bonus : bonuses)
|
|
|
|
{
|
|
|
|
GiveBonus gb;
|
|
|
|
InfoWindow iw;
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
if(bonus->type == BonusType::TOWN_MAGIC_WELL)
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
if(h->mana >= h->manaLimit())
|
|
|
|
return;
|
|
|
|
cb->setManaPoints(h->id, h->manaLimit());
|
2023-05-01 00:20:01 +02:00
|
|
|
bonus->duration = BonusDuration::ONE_DAY;
|
2023-04-26 20:55:56 +02:00
|
|
|
}
|
|
|
|
gb.bonus = * bonus;
|
|
|
|
gb.id = h->id.getNum();
|
|
|
|
cb->giveHeroBonus(&gb);
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
if(bonus->duration == BonusDuration::PERMANENT)
|
2023-04-26 20:55:56 +02:00
|
|
|
addToVisitors = true;
|
|
|
|
|
|
|
|
iw.player = cb->getOwner(h->id);
|
2023-06-18 11:18:25 +02:00
|
|
|
iw.text.appendRawString(getCustomBonusGreeting(gb.bonus));
|
2023-04-26 20:55:56 +02:00
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
}
|
|
|
|
if(addToVisitors)
|
|
|
|
town->addHeroToStructureVisitors(h, indexOnTV);
|
|
|
|
}
|
|
|
|
|
2023-05-02 02:44:09 +02:00
|
|
|
CTownRewardableBuilding::CTownRewardableBuilding(const BuildingID & index, BuildingSubID::EBuildingSubID subId, CGTownInstance * cgTown, CRandomGenerator & rand)
|
2023-04-30 03:29:34 +02:00
|
|
|
{
|
|
|
|
bID = index;
|
|
|
|
bType = subId;
|
|
|
|
town = cgTown;
|
|
|
|
indexOnTV = static_cast<si32>(town->bonusingBuildings.size());
|
2023-05-02 02:44:09 +02:00
|
|
|
initObj(rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::initObj(CRandomGenerator & rand)
|
|
|
|
{
|
|
|
|
assert(town && town->town);
|
2023-07-19 21:25:52 +02:00
|
|
|
|
|
|
|
auto building = town->town->buildings.at(bID);
|
|
|
|
|
|
|
|
building->rewardableObjectInfo.configureObject(configuration, rand);
|
2023-05-02 02:44:09 +02:00
|
|
|
for(auto & rewardInfo : configuration.info)
|
|
|
|
{
|
|
|
|
for (auto & bonus : rewardInfo.reward.bonuses)
|
|
|
|
{
|
2023-05-04 22:14:44 +02:00
|
|
|
bonus.source = BonusSource::TOWN_STRUCTURE;
|
2023-05-02 02:44:09 +02:00
|
|
|
bonus.sid = bID;
|
2023-05-04 22:14:44 +02:00
|
|
|
if (bonus.type == BonusType::MORALE)
|
2023-05-02 02:44:09 +02:00
|
|
|
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::MORALE, 0, bonus.val, 0);
|
2023-05-04 22:14:44 +02:00
|
|
|
if (bonus.type == BonusType::LUCK)
|
2023-05-02 02:44:09 +02:00
|
|
|
rewardInfo.reward.extraComponents.emplace_back(Component::EComponentType::LUCK, 0, bonus.val, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::newTurn(CRandomGenerator & rand) const
|
|
|
|
{
|
|
|
|
if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
|
|
|
|
{
|
|
|
|
if(configuration.resetParameters.rewards)
|
|
|
|
{
|
|
|
|
cb->setObjProperty(town->id, ObjProperty::REWARD_RANDOMIZE, indexOnTV);
|
|
|
|
}
|
|
|
|
if(configuration.resetParameters.visitors)
|
|
|
|
{
|
|
|
|
cb->setObjProperty(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, indexOnTV);
|
|
|
|
}
|
|
|
|
}
|
2023-04-30 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::setProperty(ui8 what, ui32 val)
|
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case ObjProperty::VISITORS:
|
|
|
|
visitors.insert(ObjectInstanceID(val));
|
|
|
|
break;
|
2023-05-02 02:44:09 +02:00
|
|
|
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
|
|
|
|
visitors.clear();
|
|
|
|
break;
|
2023-04-30 03:29:34 +02:00
|
|
|
case ObjProperty::REWARD_RANDOMIZE:
|
2023-05-02 02:44:09 +02:00
|
|
|
initObj(cb->gameState()->getRandomGenerator());
|
2023-04-30 03:29:34 +02:00
|
|
|
break;
|
|
|
|
case ObjProperty::REWARD_SELECT:
|
|
|
|
selectedReward = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 20:34:41 +02:00
|
|
|
void CTownRewardableBuilding::heroLevelUpDone(const CGHeroInstance *hero) const
|
|
|
|
{
|
|
|
|
grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const
|
|
|
|
{
|
2023-04-30 22:14:25 +02:00
|
|
|
if(visitors.find(hero->id) != visitors.end())
|
|
|
|
return; // query not for this building
|
2023-04-30 20:34:41 +02:00
|
|
|
|
|
|
|
if(answer > 0 && answer-1 < configuration.info.size())
|
|
|
|
{
|
|
|
|
auto list = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
|
|
|
grantReward(list[answer - 1], hero);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unhandled choice");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
|
|
|
|
{
|
2023-04-30 22:14:25 +02:00
|
|
|
town->addHeroToStructureVisitors(hero, indexOnTV);
|
|
|
|
|
2023-04-30 20:34:41 +02:00
|
|
|
grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
|
|
|
|
|
|
|
|
// hero is not blocked by levelup dialog - grant remainer immediately
|
|
|
|
if(!cb->isVisitCoveredByAnotherQuery(town, hero))
|
|
|
|
{
|
|
|
|
grantRewardAfterLevelup(cb, configuration.info.at(rewardID), town, hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 03:29:34 +02:00
|
|
|
bool CTownRewardableBuilding::wasVisitedBefore(const CGHeroInstance * contextHero) const
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
switch (configuration.visitMode)
|
2023-04-30 03:29:34 +02:00
|
|
|
{
|
|
|
|
case Rewardable::VISIT_UNLIMITED:
|
|
|
|
return false;
|
|
|
|
case Rewardable::VISIT_ONCE:
|
2023-05-04 13:03:18 +02:00
|
|
|
return !visitors.empty();
|
2023-04-30 03:29:34 +02:00
|
|
|
case Rewardable::VISIT_PLAYER:
|
|
|
|
return false; //not supported
|
|
|
|
case Rewardable::VISIT_BONUS:
|
2023-05-04 22:14:44 +02:00
|
|
|
return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, Bonus::getSid32(town->town->faction->getIndex(), bID));
|
2023-04-30 03:29:34 +02:00
|
|
|
case Rewardable::VISIT_HERO:
|
|
|
|
return visitors.find(contextHero->id) != visitors.end();
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CTownRewardableBuilding::onHeroVisit(const CGHeroInstance *h) const
|
|
|
|
{
|
|
|
|
auto grantRewardWithMessage = [&](int index) -> void
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
auto vi = configuration.info.at(index);
|
2023-04-30 03:29:34 +02:00
|
|
|
logGlobal->debug("Granting reward %d. Message says: %s", index, vi.message.toString());
|
2023-04-30 22:14:25 +02:00
|
|
|
|
|
|
|
town->addHeroToStructureVisitors(h, indexOnTV); //adding to visitors
|
2023-04-30 03:29:34 +02:00
|
|
|
|
|
|
|
InfoWindow iw;
|
|
|
|
iw.player = h->tempOwner;
|
|
|
|
iw.text = vi.message;
|
|
|
|
vi.reward.loadComponents(iw.components, h);
|
2023-05-07 01:19:18 +02:00
|
|
|
iw.type = EInfoWindowMode::MODAL;
|
2023-04-30 03:29:34 +02:00
|
|
|
if(!iw.components.empty() || !iw.text.toString().empty())
|
|
|
|
cb->showInfoDialog(&iw);
|
|
|
|
|
2023-04-30 22:14:25 +02:00
|
|
|
grantReward(index, h);
|
2023-04-30 03:29:34 +02:00
|
|
|
};
|
|
|
|
auto selectRewardsMessage = [&](const std::vector<ui32> & rewards, const MetaString & dialog) -> void
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
BlockingDialog sd(configuration.canRefuse, rewards.size() > 1);
|
2023-04-30 03:29:34 +02:00
|
|
|
sd.player = h->tempOwner;
|
|
|
|
sd.text = dialog;
|
|
|
|
|
|
|
|
if (rewards.size() > 1)
|
|
|
|
for (auto index : rewards)
|
2023-04-30 20:34:41 +02:00
|
|
|
sd.components.push_back(configuration.info.at(index).reward.getDisplayedComponent(h));
|
2023-04-30 03:29:34 +02:00
|
|
|
|
|
|
|
if (rewards.size() == 1)
|
2023-04-30 20:34:41 +02:00
|
|
|
configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h);
|
2023-04-30 03:29:34 +02:00
|
|
|
|
|
|
|
cb->showBlockingDialog(&sd);
|
|
|
|
};
|
|
|
|
|
2023-05-02 13:09:02 +02:00
|
|
|
if(!town->hasBuilt(bID) || cb->isVisitCoveredByAnotherQuery(town, h))
|
2023-04-30 03:29:34 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(!wasVisitedBefore(h))
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
|
2023-04-30 03:29:34 +02:00
|
|
|
|
|
|
|
logGlobal->debug("Visiting object with %d possible rewards", rewards.size());
|
|
|
|
switch (rewards.size())
|
|
|
|
{
|
|
|
|
case 0: // no available rewards, e.g. visiting School of War without gold
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
auto emptyRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_NOT_AVAILABLE);
|
2023-04-30 03:29:34 +02:00
|
|
|
if (!emptyRewards.empty())
|
|
|
|
grantRewardWithMessage(emptyRewards[0]);
|
|
|
|
else
|
|
|
|
logMod->warn("No applicable message for visiting empty object!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: // one reward. Just give it with message
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
if (configuration.canRefuse)
|
|
|
|
selectRewardsMessage(rewards, configuration.info.at(rewards.front()).message);
|
2023-04-30 03:29:34 +02:00
|
|
|
else
|
|
|
|
grantRewardWithMessage(rewards.front());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: // multiple rewards. Act according to select mode
|
|
|
|
{
|
2023-04-30 20:34:41 +02:00
|
|
|
switch (configuration.selectMode) {
|
2023-04-30 03:29:34 +02:00
|
|
|
case Rewardable::SELECT_PLAYER: // player must select
|
2023-04-30 20:34:41 +02:00
|
|
|
selectRewardsMessage(rewards, configuration.onSelect);
|
2023-04-30 03:29:34 +02:00
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_FIRST: // give first available
|
|
|
|
grantRewardWithMessage(rewards.front());
|
|
|
|
break;
|
|
|
|
case Rewardable::SELECT_RANDOM: // give random
|
2023-05-02 02:44:09 +02:00
|
|
|
grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
|
2023-04-30 03:29:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logGlobal->debug("Revisiting already visited object");
|
|
|
|
|
2023-04-30 20:34:41 +02:00
|
|
|
auto visitedRewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_ALREADY_VISITED);
|
2023-04-30 03:29:34 +02:00
|
|
|
if (!visitedRewards.empty())
|
|
|
|
grantRewardWithMessage(visitedRewards[0]);
|
|
|
|
else
|
2023-05-02 13:09:02 +02:00
|
|
|
logMod->debug("No applicable message for visiting already visited object!");
|
2023-04-30 03:29:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 20:55:56 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|