mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-25 22:42:04 +02:00
Lighthouse / flaggable objects now has dedicated bonus system node
this alows to remove multiple hacks from lighthouse logic
This commit is contained in:
@@ -12,11 +12,13 @@
|
||||
#include "FlaggableMapObject.h"
|
||||
|
||||
#include "CGHeroInstance.h"
|
||||
|
||||
#include "../CPlayerState.h"
|
||||
#include "../callback/CPrivilegedInfoCallback.h"
|
||||
#include "../callback/IGameEventCallback.h"
|
||||
#include "../networkPacks/PacksForClient.h"
|
||||
#include "../gameState/CGameState.h"
|
||||
#include "../mapObjectConstructors/FlaggableInstanceConstructor.h"
|
||||
#include "../gameState/GameStatePackVisitor.h"
|
||||
#include "../networkPacks/PacksForClient.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
@@ -40,32 +42,17 @@ void FlaggableMapObject::onHeroVisit(IGameEventCallback & gameEvents, const CGHe
|
||||
if (cb->getPlayerRelations(h->getOwner(), getOwner()) != PlayerRelations::ENEMIES)
|
||||
return; // H3 behavior - revisiting owned Lighthouse is a no-op
|
||||
|
||||
if (getOwner().isValidPlayer())
|
||||
takeBonusFrom(gameEvents, getOwner());
|
||||
|
||||
gameEvents.setOwner(this, h->getOwner()); //not ours? flag it!
|
||||
|
||||
InfoWindow iw;
|
||||
iw.player = h->getOwner();
|
||||
iw.text.appendTextID(getFlaggableHandler()->getVisitMessageTextID());
|
||||
gameEvents.showInfoDialog(&iw);
|
||||
|
||||
giveBonusTo(gameEvents, h->getOwner());
|
||||
}
|
||||
|
||||
void FlaggableMapObject::markAsDeleted() const
|
||||
{
|
||||
// if(getOwner().isValidPlayer())
|
||||
// takeBonusFrom(gameEvents, getOwner());
|
||||
}
|
||||
|
||||
void FlaggableMapObject::initObj(vstd::RNG & rand)
|
||||
{
|
||||
// if(getOwner().isValidPlayer())
|
||||
// {
|
||||
// // FIXME: This is dirty hack
|
||||
// giveBonusTo(gameEvents, getOwner(), true);
|
||||
// }
|
||||
initBonuses();
|
||||
}
|
||||
|
||||
std::shared_ptr<FlaggableInstanceConstructor> FlaggableMapObject::getFlaggableHandler() const
|
||||
@@ -73,39 +60,10 @@ std::shared_ptr<FlaggableInstanceConstructor> FlaggableMapObject::getFlaggableHa
|
||||
return std::dynamic_pointer_cast<FlaggableInstanceConstructor>(getObjectHandler());
|
||||
}
|
||||
|
||||
void FlaggableMapObject::giveBonusTo(IGameEventCallback & gameEvents, const PlayerColor & player, bool onInit) const
|
||||
void FlaggableMapObject::initBonuses()
|
||||
{
|
||||
for (auto const & bonus : getFlaggableHandler()->getProvidedBonuses())
|
||||
{
|
||||
GiveBonus gb(GiveBonus::ETarget::PLAYER);
|
||||
gb.id = player;
|
||||
gb.bonus = *bonus;
|
||||
|
||||
// FIXME: better place for this code?
|
||||
gb.bonus.duration = BonusDuration::PERMANENT;
|
||||
gb.bonus.source = BonusSource::OBJECT_INSTANCE;
|
||||
gb.bonus.sid = BonusSourceID(id);
|
||||
|
||||
// FIXME: This is really dirty hack
|
||||
// Proper fix would be to make FlaggableMapObject into bonus system node
|
||||
// Unfortunately this will cause saves breakage
|
||||
if(onInit)
|
||||
{
|
||||
GameStatePackVisitor visitor(cb->gameState());
|
||||
gb.visit(visitor);
|
||||
}
|
||||
else
|
||||
gameEvents.sendAndApply(gb);
|
||||
}
|
||||
}
|
||||
|
||||
void FlaggableMapObject::takeBonusFrom(IGameEventCallback & gameEvents, const PlayerColor & player) const
|
||||
{
|
||||
RemoveBonus rb(GiveBonus::ETarget::PLAYER);
|
||||
rb.whoID = player;
|
||||
rb.source = BonusSource::OBJECT_INSTANCE;
|
||||
rb.id = BonusSourceID(id);
|
||||
gameEvents.sendAndApply(rb);
|
||||
addNewBonus(bonus);
|
||||
}
|
||||
|
||||
void FlaggableMapObject::serializeJsonOptions(JsonSerializeFormat& handler)
|
||||
@@ -113,4 +71,21 @@ void FlaggableMapObject::serializeJsonOptions(JsonSerializeFormat& handler)
|
||||
serializeJsonOwner(handler);
|
||||
}
|
||||
|
||||
void FlaggableMapObject::attachToBonusSystem(CGameState & gs)
|
||||
{
|
||||
if (getOwner().isValidPlayer())
|
||||
attachTo(*gs.getPlayerState(getOwner()));
|
||||
}
|
||||
|
||||
void FlaggableMapObject::detachFromBonusSystem(CGameState & gs)
|
||||
{
|
||||
if (getOwner().isValidPlayer())
|
||||
detachFrom(*gs.getPlayerState(getOwner()));
|
||||
}
|
||||
|
||||
void FlaggableMapObject::restoreBonusSystem(CGameState & gs)
|
||||
{
|
||||
attachToBonusSystem(gs);
|
||||
}
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
||||
|
||||
@@ -11,32 +11,49 @@
|
||||
|
||||
#include "CGObjectInstance.h"
|
||||
#include "IOwnableObject.h"
|
||||
#include "../bonuses/CBonusSystemNode.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
struct Bonus;
|
||||
class FlaggableInstanceConstructor;
|
||||
|
||||
class DLL_LINKAGE FlaggableMapObject : public CGObjectInstance, public IOwnableObject
|
||||
class DLL_LINKAGE FlaggableMapObject final : public CGObjectInstance, public IOwnableObject, public CBonusSystemNode
|
||||
{
|
||||
std::shared_ptr<FlaggableInstanceConstructor> getFlaggableHandler() const;
|
||||
|
||||
void giveBonusTo(IGameEventCallback & gameEvents, const PlayerColor & player, bool onInit = false) const;
|
||||
void takeBonusFrom(IGameEventCallback & gameEvents, const PlayerColor & player) const;
|
||||
void initBonuses();
|
||||
|
||||
public:
|
||||
using CGObjectInstance::CGObjectInstance;
|
||||
|
||||
void onHeroVisit(IGameEventCallback & gameEvents, const CGHeroInstance * h) const override;
|
||||
void markAsDeleted() const;
|
||||
void initObj(vstd::RNG & rand) override;
|
||||
|
||||
const IOwnableObject * asOwnable() const final;
|
||||
ResourceSet dailyIncome() const override;
|
||||
std::vector<CreatureID> providedCreatures() const override;
|
||||
|
||||
protected:
|
||||
void attachToBonusSystem(CGameState & gs) override;
|
||||
void detachFromBonusSystem(CGameState & gs) override;
|
||||
void restoreBonusSystem(CGameState & gs) override;
|
||||
|
||||
PlayerColor getOwner() const override
|
||||
{
|
||||
return CGObjectInstance::getOwner();
|
||||
}
|
||||
|
||||
void serializeJsonOptions(JsonSerializeFormat & handler) override;
|
||||
|
||||
template <typename Handler> void serialize(Handler &h)
|
||||
{
|
||||
h & static_cast<CGObjectInstance&>(*this);
|
||||
|
||||
if (h.version >= Handler::Version::FLAGGABLE_BONUS_SYSTEM_NODE)
|
||||
h & static_cast<CBonusSystemNode&>(*this);
|
||||
else
|
||||
initBonuses();
|
||||
}
|
||||
};
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
||||
|
||||
Reference in New Issue
Block a user