1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-21 17:17:06 +02:00

Basic support for configurable flaggable objects

Converted CGLighthouse class into FlaggableMapObject

Bonus provided by Lighthouse/Flaggable is now defined in config and is
accessible to mods.
This commit is contained in:
Ivan Savenko 2024-10-25 16:48:10 +00:00
parent 1826b5bbdf
commit 8a5ac73438
17 changed files with 296 additions and 128 deletions

View File

@ -53,6 +53,7 @@
"config/objects/creatureBanks.json",
"config/objects/dwellings.json",
"config/objects/generic.json",
"config/objects/lighthouse.json",
"config/objects/magicSpring.json",
"config/objects/magicWell.json",
"config/objects/moddables.json",

View File

@ -270,23 +270,6 @@
}
}
},
"lighthouse" : {
"index" :42,
"handler" : "lighthouse",
"base" : {
"sounds" : {
"visit" : ["LIGHTHOUSE"]
}
},
"types" : {
"object" : {
"index" : 0,
"aiValue" : 500,
"rmg" : {
}
}
}
},
"obelisk" : {
"index" :57,
"handler" : "obelisk",

View File

@ -0,0 +1,30 @@
{
"lighthouse" : {
"index" :42,
"handler" : "flaggable",
"base" : {
"sounds" : {
"visit" : ["LIGHTHOUSE"]
}
},
"types" : {
"lighthouse" : {
"compatibilityIdentifiers" : [ "object" ],
"index" : 0,
"aiValue" : 500,
"rmg" : {
}
"message" : "@core.advevent.69",
"bonuses" : {
"seaMovement" : {
"type" : "MOVEMENT",
"subtype" : "heroMovementSea",
"val" : 500
}
}
}
}
}
}

View File

@ -116,6 +116,7 @@ set(lib_MAIN_SRCS
mapObjectConstructors/CommonConstructors.cpp
mapObjectConstructors/CRewardableConstructor.cpp
mapObjectConstructors/DwellingInstanceConstructor.cpp
mapObjectConstructors/FlaggableInstanceConstructor.cpp
mapObjectConstructors/HillFortInstanceConstructor.cpp
mapObjectConstructors/ShipyardInstanceConstructor.cpp
@ -132,6 +133,7 @@ set(lib_MAIN_SRCS
mapObjects/CObjectHandler.cpp
mapObjects/CQuest.cpp
mapObjects/CRewardableObject.cpp
mapObjects/FlaggableMapObject.cpp
mapObjects/IMarket.cpp
mapObjects/IObjectInterface.cpp
mapObjects/MiscObjects.cpp
@ -497,6 +499,7 @@ set(lib_MAIN_HEADERS
mapObjectConstructors/CRewardableConstructor.h
mapObjectConstructors/DwellingInstanceConstructor.h
mapObjectConstructors/HillFortInstanceConstructor.h
mapObjectConstructors/FlaggableInstanceConstructor.h
mapObjectConstructors/IObjectInfo.h
mapObjectConstructors/RandomMapInfo.h
mapObjectConstructors/ShipyardInstanceConstructor.h
@ -515,6 +518,7 @@ set(lib_MAIN_HEADERS
mapObjects/CObjectHandler.h
mapObjects/CQuest.h
mapObjects/CRewardableObject.h
mapObjects/FlaggableMapObject.h
mapObjects/IMarket.h
mapObjects/IObjectInterface.h
mapObjects/IOwnableObject.h

View File

@ -23,17 +23,21 @@
#include "../mapObjectConstructors/CRewardableConstructor.h"
#include "../mapObjectConstructors/CommonConstructors.h"
#include "../mapObjectConstructors/DwellingInstanceConstructor.h"
#include "../mapObjectConstructors/FlaggableInstanceConstructor.h"
#include "../mapObjectConstructors/HillFortInstanceConstructor.h"
#include "../mapObjectConstructors/ShipyardInstanceConstructor.h"
#include "../mapObjects/CGCreature.h"
#include "../mapObjects/CGPandoraBox.h"
#include "../mapObjects/CQuest.h"
#include "../mapObjects/ObjectTemplate.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/CGPandoraBox.h"
#include "../mapObjects/CGTownInstance.h"
#include "../mapObjects/CQuest.h"
#include "../mapObjects/FlaggableMapObject.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/ObjectTemplate.h"
#include "../mapObjects/ObstacleSetHandler.h"
#include "../modding/IdentifierStorage.h"
#include "../modding/CModHandler.h"
#include "../modding/ModScope.h"
@ -57,6 +61,7 @@ CObjectClassesHandler::CObjectClassesHandler()
SET_HANDLER_CLASS("town", CTownInstanceConstructor);
SET_HANDLER_CLASS("bank", CBankInstanceConstructor);
SET_HANDLER_CLASS("boat", BoatInstanceConstructor);
SET_HANDLER_CLASS("flaggable", FlaggableInstanceConstructor);
SET_HANDLER_CLASS("market", MarketInstanceConstructor);
SET_HANDLER_CLASS("hillFort", HillFortInstanceConstructor);
SET_HANDLER_CLASS("shipyard", ShipyardInstanceConstructor);
@ -82,7 +87,6 @@ CObjectClassesHandler::CObjectClassesHandler()
SET_HANDLER("garrison", CGGarrison);
SET_HANDLER("heroPlaceholder", CGHeroPlaceholder);
SET_HANDLER("keymaster", CGKeymasterTent);
SET_HANDLER("lighthouse", CGLighthouse);
SET_HANDLER("magi", CGMagi);
SET_HANDLER("mine", CGMine);
SET_HANDLER("obelisk", CGObelisk);

View File

@ -0,0 +1,53 @@
/*
* FlaggableInstanceConstructor.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 "FlaggableInstanceConstructor.h"
#include "../json/JsonBonus.h"
#include "../texts/CGeneralTextHandler.h"
#include "../VCMI_Lib.h"
VCMI_LIB_NAMESPACE_BEGIN
void FlaggableInstanceConstructor::initTypeData(const JsonNode & config)
{
for (const auto & bonusJson : config["bonuses"].Struct())
providedBonuses.push_back(JsonUtils::parseBonus(bonusJson.second));
if (!config["message"].isNull())
{
std::string message = config["message"].String();
if (!message.empty() && message.at(0) == '@')
{
visitMessageTextID = message.substr(1);
}
else
{
visitMessageTextID = TextIdentifier(getBaseTextID(), "onVisit").get();
VLC->generaltexth->registerString( config.getModScope(), visitMessageTextID, config["message"]);
}
}
}
void FlaggableInstanceConstructor::initializeObject(FlaggableMapObject * flaggable) const
{
}
const std::string & FlaggableInstanceConstructor::getVisitMessageTextID() const
{
return visitMessageTextID;
}
const std::vector<std::shared_ptr<Bonus>> & FlaggableInstanceConstructor::getProvidedBonuses() const
{
return providedBonuses;
}
VCMI_LIB_NAMESPACE_END

View File

@ -0,0 +1,34 @@
/*
* FlaggableInstanceConstructor.h, 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
*
*/
#pragma once
#include "CDefaultObjectTypeHandler.h"
#include "../bonuses/Bonus.h"
#include "../mapObjects/FlaggableMapObject.h"
VCMI_LIB_NAMESPACE_BEGIN
class FlaggableInstanceConstructor final : public CDefaultObjectTypeHandler<FlaggableMapObject>
{
std::vector<std::shared_ptr<Bonus>> providedBonuses;
/// ID of message to show on hero visit
std::string visitMessageTextID;
protected:
void initTypeData(const JsonNode & config) override;
void initializeObject(FlaggableMapObject * object) const override;
public:
const std::string & getVisitMessageTextID() const;
const std::vector<std::shared_ptr<Bonus>> & getProvidedBonuses() const;
};
VCMI_LIB_NAMESPACE_END

View File

@ -0,0 +1,105 @@
/*
* FlaggableMapObject.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 "FlaggableMapObject.h"
#include "../IGameCallback.h"
#include "CGHeroInstance.h"
#include "../networkPacks/PacksForClient.h"
#include "../mapObjectConstructors/FlaggableInstanceConstructor.h"
VCMI_LIB_NAMESPACE_BEGIN
const IOwnableObject * FlaggableMapObject::asOwnable() const
{
return this;
}
ResourceSet FlaggableMapObject::dailyIncome() const
{
return {};
}
std::vector<CreatureID> FlaggableMapObject::providedCreatures() const
{
return {};
}
void FlaggableMapObject::onHeroVisit( const CGHeroInstance * h ) const
{
if (cb->getPlayerRelations(h->getOwner(), getOwner()) != PlayerRelations::ENEMIES)
return; // H3 behavior - revisiting owned Lighthouse is a no-op
if (getOwner().isValidPlayer())
takeBonusFrom(getOwner());
cb->setOwner(this, h->getOwner()); //not ours? flag it!
InfoWindow iw;
iw.player = h->getOwner();
iw.text.appendTextID(getFlaggableHandler()->getVisitMessageTextID());
cb->showInfoDialog(&iw);
giveBonusTo(h->getOwner());
}
void FlaggableMapObject::initObj(vstd::RNG & rand)
{
if(getOwner().isValidPlayer())
{
// FIXME: This is dirty hack
giveBonusTo(getOwner(), true);
}
}
std::shared_ptr<FlaggableInstanceConstructor> FlaggableMapObject::getFlaggableHandler() const
{
return std::dynamic_pointer_cast<FlaggableInstanceConstructor>(getObjectHandler());
}
void FlaggableMapObject::giveBonusTo(const PlayerColor & player, bool onInit) const
{
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)
gb.applyGs(cb->gameState());
else
cb->sendAndApply(gb);
}
}
void FlaggableMapObject::takeBonusFrom(const PlayerColor & player) const
{
RemoveBonus rb(GiveBonus::ETarget::PLAYER);
rb.whoID = player;
rb.source = BonusSource::OBJECT_INSTANCE;
rb.id = BonusSourceID(id);
cb->sendAndApply(rb);
}
void FlaggableMapObject::serializeJsonOptions(JsonSerializeFormat& handler)
{
serializeJsonOwner(handler);
}
VCMI_LIB_NAMESPACE_END

View File

@ -0,0 +1,41 @@
/*
* FlaggableMapObject.h, 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
*
*/
#pragma once
#include "CGObjectInstance.h"
#include "IOwnableObject.h"
VCMI_LIB_NAMESPACE_BEGIN
struct Bonus;
class FlaggableInstanceConstructor;
class DLL_LINKAGE FlaggableMapObject : public CGObjectInstance, public IOwnableObject
{
std::shared_ptr<FlaggableInstanceConstructor> getFlaggableHandler() const;
void giveBonusTo(const PlayerColor & player, bool onInit = false) const;
void takeBonusFrom(const PlayerColor & player) const;
public:
using CGObjectInstance::CGObjectInstance;
void onHeroVisit(const CGHeroInstance * h) const override;
void initObj(vstd::RNG & rand) override;
const IOwnableObject * asOwnable() const final;
ResourceSet dailyIncome() const override;
std::vector<CreatureID> providedCreatures() const override;
protected:
void serializeJsonOptions(JsonSerializeFormat & handler) override;
};
VCMI_LIB_NAMESPACE_END

View File

@ -1311,75 +1311,6 @@ void CGObelisk::setPropertyDer(ObjProperty what, ObjPropertyID identifier)
}
}
const IOwnableObject * CGLighthouse::asOwnable() const
{
return this;
}
ResourceSet CGLighthouse::dailyIncome() const
{
return {};
}
std::vector<CreatureID> CGLighthouse::providedCreatures() const
{
return {};
}
void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
{
if(h->tempOwner != tempOwner)
{
PlayerColor oldOwner = tempOwner;
cb->setOwner(this,h->tempOwner); //not ours? flag it!
h->showInfoDialog(69);
giveBonusTo(h->tempOwner);
if(oldOwner.isValidPlayer()) //remove bonus from old owner
{
RemoveBonus rb(GiveBonus::ETarget::PLAYER);
rb.whoID = oldOwner;
rb.source = BonusSource::OBJECT_INSTANCE;
rb.id = BonusSourceID(id);
cb->sendAndApply(rb);
}
}
}
void CGLighthouse::initObj(vstd::RNG & rand)
{
if(tempOwner.isValidPlayer())
{
// FIXME: This is dirty hack
giveBonusTo(tempOwner, true);
}
}
void CGLighthouse::giveBonusTo(const PlayerColor & player, bool onInit) const
{
GiveBonus gb(GiveBonus::ETarget::PLAYER);
gb.bonus.type = BonusType::MOVEMENT;
gb.bonus.val = 500;
gb.id = player;
gb.bonus.duration = BonusDuration::PERMANENT;
gb.bonus.source = BonusSource::OBJECT_INSTANCE;
gb.bonus.sid = BonusSourceID(id);
gb.bonus.subtype = BonusCustomSubtype::heroMovementSea;
// FIXME: This is really dirty hack
// Proper fix would be to make CGLighthouse into bonus system node
// Unfortunately this will cause saves breakage
if(onInit)
gb.applyGs(cb->gameState());
else
cb->sendAndApply(gb);
}
void CGLighthouse::serializeJsonOptions(JsonSerializeFormat& handler)
{
serializeJsonOwner(handler);
}
void HillFort::onHeroVisit(const CGHeroInstance * h) const
{
cb->showObjectWindow(this, EOpenWindowMode::HILL_FORT_WINDOW, h, false);

View File

@ -413,28 +413,6 @@ protected:
void setPropertyDer(ObjProperty what, ObjPropertyID identifier) override;
};
class DLL_LINKAGE CGLighthouse : public CGObjectInstance, public IOwnableObject
{
public:
using CGObjectInstance::CGObjectInstance;
void onHeroVisit(const CGHeroInstance * h) const override;
void initObj(vstd::RNG & rand) override;
const IOwnableObject * asOwnable() const final;
ResourceSet dailyIncome() const override;
std::vector<CreatureID> providedCreatures() const override;
template <typename Handler> void serialize(Handler &h)
{
h & static_cast<CGObjectInstance&>(*this);
}
void giveBonusTo(const PlayerColor & player, bool onInit = false) const;
protected:
void serializeJsonOptions(JsonSerializeFormat & handler) override;
};
class DLL_LINKAGE CGTerrainPatch : public CGObjectInstance
{
public:

View File

@ -1478,9 +1478,9 @@ CGObjectInstance * CMapLoaderH3M::readShipyard(const int3 & mapPosition, std::sh
return object;
}
CGObjectInstance * CMapLoaderH3M::readLighthouse(const int3 & mapPosition)
CGObjectInstance * CMapLoaderH3M::readLighthouse(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate)
{
auto * object = new CGLighthouse(map->cb);
auto * object = readGeneric(mapPosition, objectTemplate);
setOwnerAndValidate(mapPosition, object, reader->readPlayer32());
return object;
}
@ -1618,7 +1618,7 @@ CGObjectInstance * CMapLoaderH3M::readObject(std::shared_ptr<const ObjectTemplat
return readPyramid(mapPosition, objectTemplate);
case Obj::LIGHTHOUSE:
return readLighthouse(mapPosition);
return readLighthouse(mapPosition, objectTemplate);
case Obj::CREATURE_BANK:
case Obj::DERELICT_SHIP:

View File

@ -208,7 +208,7 @@ private:
CGObjectInstance * readPyramid(const int3 & position, std::shared_ptr<const ObjectTemplate> objTempl);
CGObjectInstance * readQuestGuard(const int3 & position);
CGObjectInstance * readShipyard(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate);
CGObjectInstance * readLighthouse(const int3 & mapPosition);
CGObjectInstance * readLighthouse(const int3 & mapPosition, std::shared_ptr<const ObjectTemplate> objectTemplate);
CGObjectInstance * readGeneric(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);
CGObjectInstance * readBank(const int3 & position, std::shared_ptr<const ObjectTemplate> objectTemplate);

View File

@ -20,14 +20,17 @@
#include "../gameState/CGameState.h"
#include "../gameState/CGameStateCampaign.h"
#include "../gameState/TavernHeroesPool.h"
#include "../mapObjects/CGCreature.h"
#include "../mapObjects/CGDwelling.h"
#include "../mapObjects/CGMarket.h"
#include "../mapObjects/CGPandoraBox.h"
#include "../mapObjects/CGTownInstance.h"
#include "../mapObjects/CQuest.h"
#include "../mapObjects/FlaggableMapObject.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapObjects/TownBuildingInstance.h"
#include "../mapping/CMap.h"
#include "../networkPacks/PacksForClient.h"
#include "../networkPacks/PacksForClientBattle.h"
@ -73,7 +76,7 @@ void registerTypes(Serializer &s)
s.template registerType<CGSirens>(15);
s.template registerType<CGShipyard>(16);
s.template registerType<CGDenOfthieves>(17);
s.template registerType<CGLighthouse>(18);
s.template registerType<FlaggableMapObject>(18);
s.template registerType<CGTerrainPatch>(19);
s.template registerType<HillFort>(20);
s.template registerType<CGMarket>(21);

View File

@ -60,7 +60,7 @@ Initializer::Initializer(CGObjectInstance * o, const PlayerColor & pl) : default
INIT_OBJ_TYPE(CGHeroPlaceholder);
INIT_OBJ_TYPE(CGHeroInstance);
INIT_OBJ_TYPE(CGSignBottle);
INIT_OBJ_TYPE(CGLighthouse);
INIT_OBJ_TYPE(FlaggableMapObject);
//INIT_OBJ_TYPE(CRewardableObject);
//INIT_OBJ_TYPE(CGPandoraBox);
//INIT_OBJ_TYPE(CGEvent);
@ -108,7 +108,7 @@ void Initializer::initialize(CGShipyard * o)
o->tempOwner = defaultPlayer;
}
void Initializer::initialize(CGLighthouse * o)
void Initializer::initialize(FlaggableMapObject * o)
{
if(!o) return;
@ -244,7 +244,7 @@ void Inspector::updateProperties(CGDwelling * o)
}
}
void Inspector::updateProperties(CGLighthouse * o)
void Inspector::updateProperties(FlaggableMapObject * o)
{
if(!o) return;
@ -492,7 +492,7 @@ void Inspector::updateProperties()
UPDATE_OBJ_PROPERTIES(CGHeroPlaceholder);
UPDATE_OBJ_PROPERTIES(CGHeroInstance);
UPDATE_OBJ_PROPERTIES(CGSignBottle);
UPDATE_OBJ_PROPERTIES(CGLighthouse);
UPDATE_OBJ_PROPERTIES(FlaggableMapObject);
UPDATE_OBJ_PROPERTIES(CRewardableObject);
UPDATE_OBJ_PROPERTIES(CGPandoraBox);
UPDATE_OBJ_PROPERTIES(CGEvent);
@ -540,7 +540,7 @@ void Inspector::setProperty(const QString & key, const QVariant & value)
SET_PROPERTIES(CGHeroInstance);
SET_PROPERTIES(CGShipyard);
SET_PROPERTIES(CGSignBottle);
SET_PROPERTIES(CGLighthouse);
SET_PROPERTIES(FlaggableMapObject);
SET_PROPERTIES(CRewardableObject);
SET_PROPERTIES(CGPandoraBox);
SET_PROPERTIES(CGEvent);
@ -553,7 +553,7 @@ void Inspector::setProperty(CArmedInstance * o, const QString & key, const QVari
if(!o) return;
}
void Inspector::setProperty(CGLighthouse * o, const QString & key, const QVariant & value)
void Inspector::setProperty(FlaggableMapObject * o, const QString & key, const QVariant & value)
{
if(!o) return;
}

View File

@ -17,6 +17,7 @@
#include "../lib/GameConstants.h"
#include "../lib/mapObjects/CGCreature.h"
#include "../lib/mapObjects/MapObjects.h"
#include "../lib/mapObjects/FlaggableMapObject.h"
#include "../lib/mapObjects/CRewardableObject.h"
#include "../lib/texts/CGeneralTextHandler.h"
#include "../lib/ResourceSet.h"
@ -48,7 +49,7 @@ public:
DECLARE_OBJ_TYPE(CGHeroInstance);
DECLARE_OBJ_TYPE(CGCreature);
DECLARE_OBJ_TYPE(CGSignBottle);
DECLARE_OBJ_TYPE(CGLighthouse);
DECLARE_OBJ_TYPE(FlaggableMapObject);
//DECLARE_OBJ_TYPE(CRewardableObject);
//DECLARE_OBJ_TYPE(CGEvent);
//DECLARE_OBJ_TYPE(CGPandoraBox);
@ -78,7 +79,7 @@ protected:
DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);
DECLARE_OBJ_PROPERTY_METHODS(CGLighthouse);
DECLARE_OBJ_PROPERTY_METHODS(FlaggableMapObject);
DECLARE_OBJ_PROPERTY_METHODS(CRewardableObject);
DECLARE_OBJ_PROPERTY_METHODS(CGPandoraBox);
DECLARE_OBJ_PROPERTY_METHODS(CGEvent);

View File

@ -121,7 +121,7 @@ void MapController::repairMap(CMap * map) const
dynamic_cast<CGTownInstance*>(obj.get()) ||
dynamic_cast<CGGarrison*>(obj.get()) ||
dynamic_cast<CGShipyard*>(obj.get()) ||
dynamic_cast<CGLighthouse*>(obj.get()) ||
dynamic_cast<FlaggableMapObject*>(obj.get()) ||
dynamic_cast<CGHeroInstance*>(obj.get()))
obj->tempOwner = PlayerColor::NEUTRAL;
}