1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/lib/mapObjects/TownBuildingInstance.cpp

285 lines
7.8 KiB
C++
Raw Normal View History

2023-04-26 20:55:56 +02:00
/*
* TownBuildingInstance.cpp, part of VCMI engine
2023-04-26 20:55:56 +02:00
*
* 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 "TownBuildingInstance.h"
2023-04-26 20:55:56 +02:00
#include "CGTownInstance.h"
#include "../texts/CGeneralTextHandler.h"
2023-04-26 20:55:56 +02:00
#include "../IGameCallback.h"
#include "../gameState/CGameState.h"
#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
#include <vstd/RNG.h>
2023-04-26 20:55:56 +02:00
VCMI_LIB_NAMESPACE_BEGIN
TownBuildingInstance::TownBuildingInstance(IGameCallback * cb)
2024-01-01 16:37:48 +02:00
: IObjectInterface(cb)
, town(nullptr)
{}
TownBuildingInstance::TownBuildingInstance(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
{}
PlayerColor TownBuildingInstance::getOwner() const
2023-04-26 20:55:56 +02:00
{
return town->getOwner();
}
MapObjectID TownBuildingInstance::getObjGroupIndex() const
2023-04-26 20:55:56 +02:00
{
return -1;
}
MapObjectSubID TownBuildingInstance::getObjTypeIndex() const
2023-04-26 20:55:56 +02:00
{
return 0;
}
const IOwnableObject * TownBuildingInstance::asOwnable() const
{
return nullptr;
}
int3 TownBuildingInstance::visitablePos() const
2023-04-26 20:55:56 +02:00
{
return town->visitablePos();
}
int3 TownBuildingInstance::getPosition() const
2023-04-26 20:55:56 +02:00
{
return town->getPosition();
}
TownRewardableBuildingInstance::TownRewardableBuildingInstance(IGameCallback *cb)
: TownBuildingInstance(cb)
2024-01-01 16:37:48 +02:00
{}
TownRewardableBuildingInstance::TownRewardableBuildingInstance(CGTownInstance * town, const BuildingID & index, vstd::RNG & rand)
: TownBuildingInstance(town, index)
{
initObj(rand);
}
void TownRewardableBuildingInstance::initObj(vstd::RNG & rand)
{
assert(town && town->town);
configuration = generateConfiguration(rand);
}
Rewardable::Configuration TownRewardableBuildingInstance::generateConfiguration(vstd::RNG & rand) const
{
Rewardable::Configuration result;
2024-08-16 14:57:38 +02:00
auto building = town->town->buildings.at(getBuildingType());
building->rewardableObjectInfo.configureObject(result, rand, cb);
for(auto & rewardInfo : result.info)
{
for (auto & bonus : rewardInfo.reward.bonuses)
{
2023-05-04 22:14:44 +02:00
bonus.source = BonusSource::TOWN_STRUCTURE;
bonus.sid = BonusSourceID(building->getUniqueTypeID());
}
}
return result;
}
void TownRewardableBuildingInstance::newTurn(vstd::RNG & rand) const
{
if (configuration.resetParameters.period != 0 && cb->getDate(Date::DAY) > 1 && ((cb->getDate(Date::DAY)-1) % configuration.resetParameters.period) == 0)
{
auto newConfiguration = generateConfiguration(rand);
2024-08-16 14:57:38 +02:00
cb->setRewardableObjectConfiguration(town->id, getBuildingType(), newConfiguration);
if(configuration.resetParameters.visitors)
{
2024-08-16 14:57:38 +02:00
cb->setObjPropertyValue(town->id, ObjProperty::STRUCTURE_CLEAR_VISITORS, getBuildingType());
}
}
}
void TownRewardableBuildingInstance::setProperty(ObjProperty what, ObjPropertyID identifier)
{
switch (what)
{
case ObjProperty::VISITORS:
visitors.insert(identifier.as<ObjectInstanceID>());
break;
case ObjProperty::STRUCTURE_CLEAR_VISITORS:
visitors.clear();
break;
case ObjProperty::REWARD_SELECT:
selectedReward = identifier.getNum();
break;
}
}
void TownRewardableBuildingInstance::heroLevelUpDone(const CGHeroInstance *hero) const
2023-04-30 20:34:41 +02:00
{
grantRewardAfterLevelup(cb, configuration.info.at(selectedReward), town, hero);
}
void TownRewardableBuildingInstance::blockingDialogAnswered(const CGHeroInstance *hero, int32_t answer) const
2023-04-30 20:34:41 +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 TownRewardableBuildingInstance::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
2023-04-30 20:34:41 +02:00
{
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);
// 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);
}
}
bool TownRewardableBuildingInstance::wasVisitedBefore(const CGHeroInstance * contextHero) const
{
2023-04-30 20:34:41 +02:00
switch (configuration.visitMode)
{
case Rewardable::VISIT_UNLIMITED:
return false;
case Rewardable::VISIT_ONCE:
2023-05-04 13:03:18 +02:00
return !visitors.empty();
case Rewardable::VISIT_PLAYER:
return false; //not supported
case Rewardable::VISIT_BONUS:
{
2024-08-16 14:57:38 +02:00
const auto building = town->getTown()->buildings.at(getBuildingType());
return contextHero->hasBonusFrom(BonusSource::TOWN_STRUCTURE, BonusSourceID(building->getUniqueTypeID()));
}
case Rewardable::VISIT_HERO:
return visitors.find(contextHero->id) != visitors.end();
case Rewardable::VISIT_LIMITER:
return configuration.visitLimiter.heroAllowed(contextHero);
default:
return false;
}
}
void TownRewardableBuildingInstance::onHeroVisit(const CGHeroInstance *h) const
{
auto grantRewardWithMessage = [&](int index) -> void
{
2023-04-30 20:34:41 +02:00
auto vi = configuration.info.at(index);
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
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;
if(!iw.components.empty() || !iw.text.toString().empty())
cb->showInfoDialog(&iw);
2023-04-30 22:14:25 +02:00
grantReward(index, h);
};
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);
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));
if (rewards.size() == 1)
2023-04-30 20:34:41 +02:00
configuration.info.at(rewards.front()).reward.loadComponents(sd.components, h);
cb->showBlockingDialog(&sd);
};
2024-08-16 14:57:38 +02:00
if(!town->hasBuilt(getBuildingType()))
return;
if(!wasVisitedBefore(h))
{
2023-04-30 20:34:41 +02:00
auto rewards = getAvailableRewards(h, Rewardable::EEventType::EVENT_FIRST_VISIT);
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);
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);
else
grantRewardWithMessage(rewards.front());
break;
}
default: // multiple rewards. Act according to select mode
{
2023-04-30 20:34:41 +02:00
switch (configuration.selectMode) {
case Rewardable::SELECT_PLAYER: // player must select
2023-04-30 20:34:41 +02:00
selectRewardsMessage(rewards, configuration.onSelect);
break;
case Rewardable::SELECT_FIRST: // give first available
grantRewardWithMessage(rewards.front());
break;
case Rewardable::SELECT_RANDOM: // give random
grantRewardWithMessage(*RandomGeneratorUtil::nextItem(rewards, cb->gameState()->getRandomGenerator()));
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);
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-26 20:55:56 +02:00
VCMI_LIB_NAMESPACE_END