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"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../texts/CGeneralTextHandler.h"
|
2023-04-26 20:55:56 +02:00
|
|
|
#include "../IGameCallback.h"
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../gameState/CGameState.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../mapObjects/CGHeroInstance.h"
|
|
|
|
#include "../networkPacks/PacksForClient.h"
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../entities/building/CBuilding.h"
|
|
|
|
|
2023-04-26 20:55:56 +02:00
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
#include <vstd/RNG.h>
|
|
|
|
|
2023-04-26 20:55:56 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CGTownBuilding::CGTownBuilding(IGameCallback * cb)
|
|
|
|
: IObjectInterface(cb)
|
|
|
|
, town(nullptr)
|
|
|
|
{}
|
|
|
|
|
2024-08-16 14:57:38 +02:00
|
|
|
CGTownBuilding::CGTownBuilding(CGTownInstance * town, const BuildingID & index)
|
2024-01-01 16:37:48 +02:00
|
|
|
: IObjectInterface(town->cb)
|
|
|
|
, town(town)
|
2024-08-16 14:57:38 +02:00
|
|
|
, bID(index)
|
2024-01-01 16:37:48 +02:00
|
|
|
{}
|
|
|
|
|
2023-04-26 20:55:56 +02:00
|
|
|
PlayerColor CGTownBuilding::getOwner() const
|
|
|
|
{
|
|
|
|
return town->getOwner();
|
|
|
|
}
|
|
|
|
|
2023-10-24 16:11:25 +02:00
|
|
|
MapObjectID CGTownBuilding::getObjGroupIndex() const
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-10-24 16:11:25 +02:00
|
|
|
MapObjectSubID CGTownBuilding::getObjTypeIndex() const
|
2023-04-26 20:55:56 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGTownBuilding::visitablePos() const
|
|
|
|
{
|
|
|
|
return town->visitablePos();
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 CGTownBuilding::getPosition() const
|
|
|
|
{
|
|
|
|
return town->getPosition();
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:37:48 +02:00
|
|
|
CTownRewardableBuilding::CTownRewardableBuilding(IGameCallback *cb)
|
|
|
|
: CGTownBuilding(cb)
|
|
|
|
{}
|
|
|
|
|
2024-08-16 14:57:38 +02:00
|
|
|
CTownRewardableBuilding::CTownRewardableBuilding(CGTownInstance * town, const BuildingID & index, vstd::RNG & rand)
|
|
|
|
: CGTownBuilding(town, index)
|
2023-04-30 03:29:34 +02:00
|
|
|
{
|
2023-05-02 02:44:09 +02:00
|
|
|
initObj(rand);
|
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CTownRewardableBuilding::initObj(vstd::RNG & rand)
|
2023-05-02 02:44:09 +02:00
|
|
|
{
|
|
|
|
assert(town && town->town);
|
2024-07-12 17:10:03 +02:00
|
|
|
configuration = generateConfiguration(rand);
|
|
|
|
}
|
2023-07-19 21:25:52 +02:00
|
|
|
|
2024-07-12 17:10:03 +02:00
|
|
|
Rewardable::Configuration CTownRewardableBuilding::generateConfiguration(vstd::RNG & rand) const
|
|
|
|
{
|
|
|
|
Rewardable::Configuration result;
|
2024-08-16 14:57:38 +02:00
|
|
|
auto building = town->town->buildings.at(getBuildingType());
|
2023-07-19 21:25:52 +02:00
|
|
|
|
2024-07-12 17:10:03 +02:00
|
|
|
building->rewardableObjectInfo.configureObject(result, rand, cb);
|
|
|
|
for(auto & rewardInfo : result.info)
|
2023-05-02 02:44:09 +02:00
|
|
|
{
|
|
|
|
for (auto & bonus : rewardInfo.reward.bonuses)
|
|
|
|
{
|
2023-05-04 22:14:44 +02:00
|
|
|
bonus.source = BonusSource::TOWN_STRUCTURE;
|
2023-10-21 13:50:42 +02:00
|
|
|
bonus.sid = BonusSourceID(building->getUniqueTypeID());
|
2023-05-02 02:44:09 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-12 17:10:03 +02:00
|
|
|
return result;
|
2023-05-02 02:44:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-01 17:28:17 +02:00
|
|
|
void CTownRewardableBuilding::newTurn(vstd::RNG & rand) const
|
2023-05-02 02:44:09 +02:00
|
|
|
{
|
|
|
|
if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
|
|
|
|
{
|
2024-07-12 17:10:03 +02:00
|
|
|
auto newConfiguration = generateConfiguration(rand);
|
2024-08-16 14:57:38 +02:00
|
|
|
cb->setRewardableObjectConfiguration(town->id, getBuildingType(), newConfiguration);
|
2024-07-12 17:10:03 +02:00
|
|
|
|
2023-05-02 02:44:09 +02:00
|
|
|
if(configuration.resetParameters.visitors)
|
|
|
|
{
|
2024-08-16 14:57:38 +02:00
|
|
|
cb->setObjPropertyValue(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, getBuildingType());
|
2023-05-02 02:44:09 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-30 03:29:34 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 18:27:16 +02:00
|
|
|
void CTownRewardableBuilding::setProperty(ObjProperty what, ObjPropertyID identifier)
|
2023-04-30 03:29:34 +02:00
|
|
|
{
|
|
|
|
switch (what)
|
|
|
|
{
|
|
|
|
case ObjProperty::VISITORS:
|
2023-11-06 18:27:16 +02:00
|
|
|
visitors.insert(identifier.as<ObjectInstanceID>());
|
2023-04-30 03:29:34 +02:00
|
|
|
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_SELECT:
|
2023-11-06 18:27:16 +02:00
|
|
|
selectedReward = identifier.getNum();
|
2023-04-30 03:29:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 20:34:41 +02:00
|
|
|
void CTownRewardableBuilding::heroLevelUpDone(const CGHeroInstance *hero) const
|
|
|
|
{
|
|
|
|
grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero);
|
|
|
|
}
|
|
|
|
|
2024-08-09 00:28:28 +02:00
|
|
|
void CTownRewardableBuilding::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
|
2023-04-30 20:34:41 +02:00
|
|
|
{
|
2023-08-08 17:27:00 +02:00
|
|
|
if(answer == 0)
|
|
|
|
return; // player refused
|
|
|
|
|
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
|
|
|
|
{
|
2024-08-16 14:57:38 +02:00
|
|
|
town->addHeroToStructureVisitors(hero, getBuildingType());
|
2023-04-30 22:14:25 +02:00
|
|
|
|
2023-04-30 20:34:41 +02:00
|
|
|
grantRewardBeforeLevelup(cb, configuration.info.at(rewardID), hero);
|
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
// hero is not blocked by levelup dialog - grant remainder immediately
|
2023-04-30 20:34:41 +02:00
|
|
|
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-10-10 17:05:18 +02:00
|
|
|
{
|
2024-08-16 14:57:38 +02:00
|
|
|
const auto building = town->getTown()->buildings.at(getBuildingType());
|
2023-10-21 13:50:42 +02:00
|
|
|
return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID()));
|
2023-10-10 17:05:18 +02:00
|
|
|
}
|
2023-04-30 03:29:34 +02:00
|
|
|
case Rewardable::VISIT_HERO:
|
|
|
|
return visitors.find(contextHero->id) != visitors.end();
|
2023-09-30 20:22:31 +02:00
|
|
|
case Rewardable::VISIT_LIMITER:
|
|
|
|
return configuration.visitLimiter.heroAllowed(contextHero);
|
2023-04-30 03:29:34 +02:00
|
|
|
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
|
|
|
|
2024-08-16 14:57:38 +02:00
|
|
|
town->addHeroToStructureVisitors(h, getBuildingType()); //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);
|
|
|
|
};
|
|
|
|
|
2024-08-16 14:57:38 +02:00
|
|
|
assert(town->hasBuilt(getBuildingType()));
|
|
|
|
if(!town->hasBuilt(getBuildingType()))
|
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
|