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
|
/*
* CRewardableObject.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 "CRewardableObject.h"
#include "../CPlayerState.h"
#include "../IGameCallback.h"
#include "../IGameSettings.h"
#include "../battle/BattleLayout.h"
#include "../gameState/CGameState.h"
#include "../mapObjectConstructors/AObjectTypeHandler.h"
#include "../mapObjectConstructors/CRewardableConstructor.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../networkPacks/PacksForClient.h"
#include "../networkPacks/PacksForClientBattle.h"
#include "../serializer/JsonSerializeFormat.h"
#include <vstd/RNG.h>
VCMI_LIB_NAMESPACE_BEGIN
const IObjectInterface * CRewardableObject::getObject() const
{
return this;
}
void CRewardableObject::markAsScouted(const CGHeroInstance * hero) const
{
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_PLAYER, id, hero->id);
cb->sendAndApply(cov);
}
bool CRewardableObject::isGuarded() const
{
return stacksCount() > 0;
}
void CRewardableObject::onHeroVisit(const CGHeroInstance *hero) const
{
if(!wasScouted(hero->getOwner()))
{
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_SCOUTED, id, hero->id);
cb->sendAndApply(cov);
}
if (isGuarded())
{
auto guardedIndexes = getAvailableRewards(hero, Rewardable::EEventType::EVENT_GUARDED);
auto guardedReward = configuration.info.at(guardedIndexes.at(0));
// ask player to confirm attack
BlockingDialog bd(true, false);
bd.player = hero->getOwner();
bd.text = guardedReward.message;
bd.components = getPopupComponents(hero->getOwner());
cb->showBlockingDialog(this, &bd);
}
else
{
doHeroVisit(hero);
}
}
void CRewardableObject::heroLevelUpDone(const CGHeroInstance *hero) const
{
grantRewardAfterLevelup(configuration.info.at(selectedReward), this, hero);
}
void CRewardableObject::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const
{
if (result.winner == BattleSide::ATTACKER)
{
doHeroVisit(hero);
}
}
void CRewardableObject::blockingDialogAnswered(const CGHeroInstance * hero, int32_t answer) const
{
if(isGuarded())
{
if (answer)
{
auto layout = BattleLayout::createLayout(cb, configuration.guardsLayout, hero, this);
cb->startBattle(hero, this, visitablePos(), hero, nullptr, layout, nullptr);
}
}
else
{
onBlockingDialogAnswered(hero, answer);
}
}
void CRewardableObject::markAsVisited(const CGHeroInstance * hero) const
{
cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, true);
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_ADD_HERO, id, hero->id);
cb->sendAndApply(cov);
}
void CRewardableObject::grantReward(ui32 rewardID, const CGHeroInstance * hero) const
{
cb->setObjPropertyValue(id, ObjProperty::REWARD_SELECT, rewardID);
grantRewardBeforeLevelup(configuration.info.at(rewardID), hero);
// hero is not blocked by levelup dialog - grant remainder immediately
if(!cb->isVisitCoveredByAnotherQuery(this, hero))
{
grantRewardAfterLevelup(configuration.info.at(rewardID), this, hero);
}
}
bool CRewardableObject::wasVisitedBefore(const CGHeroInstance * contextHero) const
{
switch (configuration.visitMode)
{
case Rewardable::VISIT_UNLIMITED:
return false;
case Rewardable::VISIT_ONCE:
return onceVisitableObjectCleared;
case Rewardable::VISIT_PLAYER:
return vstd::contains(cb->getPlayerState(contextHero->getOwner())->visitedObjects, ObjectInstanceID(id));
case Rewardable::VISIT_BONUS:
return contextHero->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
case Rewardable::VISIT_HERO:
return contextHero->visitedObjects.count(ObjectInstanceID(id));
case Rewardable::VISIT_LIMITER:
return configuration.visitLimiter.heroAllowed(contextHero);
default:
return false;
}
}
bool CRewardableObject::wasVisited(PlayerColor player) const
{
switch (configuration.visitMode)
{
case Rewardable::VISIT_UNLIMITED:
case Rewardable::VISIT_BONUS:
case Rewardable::VISIT_HERO:
case Rewardable::VISIT_LIMITER:
return false;
case Rewardable::VISIT_ONCE:
case Rewardable::VISIT_PLAYER:
return vstd::contains(cb->getPlayerState(player)->visitedObjects, ObjectInstanceID(id));
default:
return false;
}
}
bool CRewardableObject::wasScouted(PlayerColor player) const
{
return vstd::contains(cb->getPlayerTeam(player)->scoutedObjects, ObjectInstanceID(id));
}
bool CRewardableObject::wasVisited(const CGHeroInstance * h) const
{
switch (configuration.visitMode)
{
case Rewardable::VISIT_BONUS:
return h->hasBonusFrom(BonusSource::OBJECT_TYPE, BonusSourceID(ID));
case Rewardable::VISIT_HERO:
return h->visitedObjects.count(ObjectInstanceID(id));
case Rewardable::VISIT_LIMITER:
return wasScouted(h->getOwner()) && configuration.visitLimiter.heroAllowed(h);
default:
return wasVisited(h->getOwner());
}
}
std::string CRewardableObject::getDisplayTextImpl(PlayerColor player, const CGHeroInstance * hero, bool includeDescription) const
{
std::string result = getObjectName();
if (includeDescription && !getDescriptionMessage(player, hero).empty())
result += "\n" + getDescriptionMessage(player, hero);
if (hero)
{
if(configuration.visitMode != Rewardable::VISIT_UNLIMITED)
{
if (wasVisited(hero))
result += "\n" + configuration.visitedTooltip.toString();
else
result += "\n " + configuration.notVisitedTooltip.toString();
}
}
else
{
if(configuration.visitMode == Rewardable::VISIT_PLAYER || configuration.visitMode == Rewardable::VISIT_ONCE)
{
if (wasVisited(player))
result += "\n" + configuration.visitedTooltip.toString();
else
result += "\n" + configuration.notVisitedTooltip.toString();
}
}
return result;
}
std::string CRewardableObject::getHoverText(PlayerColor player) const
{
return getDisplayTextImpl(player, nullptr, false);
}
std::string CRewardableObject::getHoverText(const CGHeroInstance * hero) const
{
return getDisplayTextImpl(hero->getOwner(), hero, false);
}
std::string CRewardableObject::getPopupText(PlayerColor player) const
{
return getDisplayTextImpl(player, nullptr, true);
}
std::string CRewardableObject::getPopupText(const CGHeroInstance * hero) const
{
return getDisplayTextImpl(hero->getOwner(), hero, true);
}
std::string CRewardableObject::getDescriptionMessage(PlayerColor player, const CGHeroInstance * hero) const
{
if (!wasScouted(player) || configuration.info.empty())
return configuration.description.toString();
auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
if (rewardIndices.empty() || !configuration.info[0].description.empty())
return configuration.info[0].description.toString();
if (!configuration.info[rewardIndices.front()].description.empty())
return configuration.info[rewardIndices.front()].description.toString();
return configuration.description.toString();
}
std::vector<Component> CRewardableObject::getPopupComponentsImpl(PlayerColor player, const CGHeroInstance * hero) const
{
if (!wasScouted(player))
return {};
if (isGuarded())
{
if (!cb->getSettings().getBoolean(EGameSettings::BANKS_SHOW_GUARDS_COMPOSITION))
return {};
std::map<CreatureID, int> guardsAmounts;
std::vector<Component> result;
for (auto const & slot : Slots())
if (slot.second)
guardsAmounts[slot.second->getCreatureID()] += slot.second->getCount();
for (auto const & guard : guardsAmounts)
{
Component comp(ComponentType::CREATURE, guard.first, guard.second);
result.push_back(comp);
}
return result;
}
else
{
if (!configuration.showScoutedPreview)
return {};
auto rewardIndices = getAvailableRewards(hero, Rewardable::EEventType::EVENT_FIRST_VISIT);
if (rewardIndices.empty() && !configuration.info.empty())
{
// Object has valid config, but current hero has no rewards that he can receive.
// Usually this happens if hero has already visited this object -> show reward using context without any hero
// since reward may be context-sensitive - e.g. Witch Hut that gives 1 skill, but always at basic level
return loadComponents(nullptr, {0});
}
if (rewardIndices.empty())
return {};
return loadComponents(hero, rewardIndices);
}
}
std::vector<Component> CRewardableObject::getPopupComponents(PlayerColor player) const
{
return getPopupComponentsImpl(player, nullptr);
}
std::vector<Component> CRewardableObject::getPopupComponents(const CGHeroInstance * hero) const
{
return getPopupComponentsImpl(hero->getOwner(), hero);
}
void CRewardableObject::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
{
switch (what)
{
case ObjProperty::REWARD_SELECT:
selectedReward = identifier.getNum();
break;
case ObjProperty::REWARD_CLEARED:
onceVisitableObjectCleared = identifier.getNum();
break;
}
}
void CRewardableObject::newTurn(vstd::RNG & 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)
{
auto handler = std::dynamic_pointer_cast<const CRewardableConstructor>(getObjectHandler());
auto newConfiguration = handler->generateConfiguration(cb, rand, ID, configuration.variables.preset);
cb->setRewardableObjectConfiguration(id, newConfiguration);
}
if (configuration.resetParameters.visitors)
{
cb->setObjPropertyValue(id, ObjProperty::REWARD_CLEARED, false);
ChangeObjectVisitors cov(ChangeObjectVisitors::VISITOR_CLEAR, id);
cb->sendAndApply(cov);
}
}
}
void CRewardableObject::initObj(vstd::RNG & rand)
{
getObjectHandler()->configureObject(this, rand);
}
CRewardableObject::CRewardableObject(IGameCallback *cb)
:CArmedInstance(cb)
{}
void CRewardableObject::serializeJsonOptions(JsonSerializeFormat & handler)
{
CArmedInstance::serializeJsonOptions(handler);
handler.serializeStruct("rewardable", static_cast<Rewardable::Interface&>(*this));
}
void CRewardableObject::initializeGuards()
{
clearSlots();
// Workaround for default creature banks strings that has placeholder for object name
// TODO: find better location for this code
for (auto & visitInfo : configuration.info)
visitInfo.message.replaceRawString(getObjectName());
for (auto const & visitInfo : configuration.info)
{
for (auto const & guard : visitInfo.reward.guards)
{
auto slotID = getFreeSlot();
if (!slotID.validSlot())
return;
putStack(slotID, new CStackInstance(guard.getId(), guard.getCount()));
}
}
}
bool CRewardableObject::isCoastVisitable() const
{
return configuration.coastVisitable;
}
VCMI_LIB_NAMESPACE_END
|