2014-06-05 19:57:43 +03:00
|
|
|
/*
|
|
|
|
* CObjectHandler.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
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "ObjectTemplate.h"
|
|
|
|
|
|
|
|
#include "../int3.h"
|
|
|
|
#include "../HeroBonus.h"
|
2023-03-08 00:32:21 +02:00
|
|
|
#include "../NetPacksBase.h"
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
class CGHeroInstance;
|
2014-06-25 17:11:07 +03:00
|
|
|
class IGameCallback;
|
|
|
|
class CGObjectInstance;
|
2014-08-11 21:17:17 +03:00
|
|
|
struct MetaString;
|
2014-06-05 19:57:43 +03:00
|
|
|
struct BattleResult;
|
2016-02-22 01:37:19 +02:00
|
|
|
class JsonSerializeFormat;
|
2016-09-09 19:30:36 +02:00
|
|
|
class CRandomGenerator;
|
2017-05-28 15:23:42 +02:00
|
|
|
class CMap;
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
class JsonNode;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2015-12-05 00:14:03 +02:00
|
|
|
// This one teleport-specific, but has to be available everywhere in callbacks and netpacks
|
|
|
|
// For now it's will be there till teleports code refactored and moved into own file
|
2023-02-12 22:39:17 +02:00
|
|
|
using TTeleportExitsList = std::vector<std::pair<ObjectInstanceID, int3>>;
|
2015-12-05 00:14:03 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
class DLL_LINKAGE IObjectInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static IGameCallback *cb;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
virtual ~IObjectInterface() = default;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
virtual int32_t getObjGroupIndex() const = 0;
|
|
|
|
virtual int32_t getObjTypeIndex() const = 0;
|
|
|
|
|
|
|
|
virtual PlayerColor getOwner() const = 0;
|
|
|
|
virtual int3 visitablePos() const = 0;
|
|
|
|
virtual int3 getPosition() const = 0;
|
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
virtual void onHeroVisit(const CGHeroInstance * h) const;
|
|
|
|
virtual void onHeroLeave(const CGHeroInstance * h) const;
|
2016-09-09 19:30:36 +02:00
|
|
|
virtual void newTurn(CRandomGenerator & rand) const;
|
|
|
|
virtual void initObj(CRandomGenerator & rand); //synchr
|
2014-06-05 19:57:43 +03:00
|
|
|
virtual void setProperty(ui8 what, ui32 val);//synchr
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
//Called when queries created DURING HERO VISIT are resolved
|
|
|
|
//First parameter is always hero that visited object and triggered the query
|
|
|
|
virtual void battleFinished(const CGHeroInstance *hero, const BattleResult &result) const;
|
|
|
|
virtual void blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const;
|
|
|
|
virtual void garrisonDialogClosed(const CGHeroInstance *hero) const;
|
|
|
|
virtual void heroLevelUpDone(const CGHeroInstance *hero) const;
|
|
|
|
|
2023-03-08 00:32:21 +02:00
|
|
|
//unified helper to show info dialog for object owner
|
|
|
|
virtual void showInfoDialog(const ui32 txtID, const ui16 soundID = 0, EInfoWindowMode mode = EInfoWindowMode::AUTO) const;
|
|
|
|
|
|
|
|
//unified helper to show a specific window
|
|
|
|
static void openWindow(const EOpenWindowMode type, const int id1, const int id2 = -1);
|
|
|
|
|
|
|
|
//unified interface, AI helpers
|
2014-06-05 19:57:43 +03:00
|
|
|
virtual bool wasVisited (PlayerColor player) const;
|
|
|
|
virtual bool wasVisited (const CGHeroInstance * h) const;
|
|
|
|
|
|
|
|
static void preInit(); //called before objs receive their initObj
|
|
|
|
static void postInit();//called after objs receive their initObj
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("IObjectInterface serialized, unexpected, should not happen!");
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DLL_LINKAGE IBoatGenerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
const CGObjectInstance *o;
|
|
|
|
|
|
|
|
IBoatGenerator(const CGObjectInstance *O);
|
2023-02-12 22:39:17 +02:00
|
|
|
virtual ~IBoatGenerator() = default;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
|
|
|
virtual int getBoatType() const; //0 - evil (if a ship can be evil...?), 1 - good, 2 - neutral
|
|
|
|
virtual void getOutOffsets(std::vector<int3> &offsets) const =0; //offsets to obj pos when we boat can be placed
|
|
|
|
int3 bestLocation() const; //returns location when the boat should be placed
|
|
|
|
|
|
|
|
enum EGeneratorState {GOOD, BOAT_ALREADY_BUILT, TILE_BLOCKED, NO_WATER};
|
|
|
|
EGeneratorState shipyardStatus() const; //0 - can buid, 1 - there is already a boat at dest tile, 2 - dest tile is blocked, 3 - no water
|
|
|
|
void getProblemText(MetaString &out, const CGHeroInstance *visitor = nullptr) const;
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & o;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DLL_LINKAGE IShipyard : public IBoatGenerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IShipyard(const CGObjectInstance *O);
|
|
|
|
|
|
|
|
virtual void getBoatCost(std::vector<si32> &cost) const;
|
|
|
|
|
|
|
|
static const IShipyard *castFrom(const CGObjectInstance *obj);
|
|
|
|
static IShipyard *castFrom(CGObjectInstance *obj);
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & static_cast<IBoatGenerator&>(*this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DLL_LINKAGE CGObjectInstance : public IObjectInterface
|
|
|
|
{
|
|
|
|
public:
|
2014-06-24 02:26:36 +03:00
|
|
|
/// Position of bottom-right corner of object on map
|
|
|
|
int3 pos;
|
|
|
|
/// Type of object, e.g. town, hero, creature.
|
2014-06-05 19:57:43 +03:00
|
|
|
Obj ID;
|
2014-06-24 02:26:36 +03:00
|
|
|
/// Subtype of object, depends on type
|
|
|
|
si32 subID;
|
2021-09-12 13:30:54 +02:00
|
|
|
/// Current owner of an object (when below PLAYER_LIMIT)
|
|
|
|
PlayerColor tempOwner;
|
2014-06-24 02:26:36 +03:00
|
|
|
/// Index of object in map's list of objects
|
|
|
|
ObjectInstanceID id;
|
|
|
|
/// Defines appearance of object on map (animation, blocked tiles, blit order, etc)
|
2022-09-11 15:12:35 +02:00
|
|
|
std::shared_ptr<const ObjectTemplate> appearance;
|
2014-06-24 02:26:36 +03:00
|
|
|
/// If true hero can visit this object only from neighbouring tiles and can't stand on this object
|
|
|
|
bool blockVisit;
|
|
|
|
|
2016-02-22 18:26:42 +02:00
|
|
|
std::string instanceName;
|
2015-11-14 15:50:29 +02:00
|
|
|
std::string typeName;
|
|
|
|
std::string subTypeName;
|
|
|
|
|
2023-02-12 22:39:17 +02:00
|
|
|
CGObjectInstance(); //TODO: remove constructor
|
|
|
|
~CGObjectInstance() override;
|
2014-06-24 02:26:36 +03:00
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
int32_t getObjGroupIndex() const override;
|
|
|
|
int32_t getObjTypeIndex() const override;
|
|
|
|
|
2014-06-24 02:26:36 +03:00
|
|
|
/// "center" tile from which the sight distance is calculated
|
|
|
|
int3 getSightCenter() const;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2021-09-12 13:30:54 +02:00
|
|
|
PlayerColor getOwner() const override
|
|
|
|
{
|
|
|
|
return this->tempOwner;
|
|
|
|
}
|
2023-02-12 22:39:17 +02:00
|
|
|
void setOwner(const PlayerColor & ow);
|
2014-06-24 02:26:36 +03:00
|
|
|
|
2014-06-24 14:50:27 +03:00
|
|
|
/** APPEARANCE ACCESSORS **/
|
2014-06-24 02:26:36 +03:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
int getWidth() const; //returns width of object graphic in tiles
|
|
|
|
int getHeight() const; //returns height of object graphic in tiles
|
2014-06-24 02:26:36 +03:00
|
|
|
bool visitableAt(int x, int y) const; //returns true if object is visitable at location (x, y) (h3m pos)
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
int3 visitablePos() const override;
|
|
|
|
int3 getPosition() const override;
|
2014-06-05 19:57:43 +03:00
|
|
|
bool blockingAt(int x, int y) const; //returns true if object is blocking location (x, y) (h3m pos)
|
|
|
|
bool coveringAt(int x, int y) const; //returns true if object covers with picture location (x, y) (h3m pos)
|
|
|
|
std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
|
|
|
|
std::set<int3> getBlockedOffsets() const; //returns set of relative positions blocked by this object
|
|
|
|
bool isVisitable() const; //returns true if object is visitable
|
|
|
|
|
2022-07-09 18:00:03 +02:00
|
|
|
virtual BattleField getBattlefield() const;
|
2022-06-26 09:21:05 +02:00
|
|
|
|
|
|
|
virtual bool isTile2Terrain() const { return false; }
|
2021-11-05 23:08:48 +02:00
|
|
|
|
2017-09-13 02:35:58 +02:00
|
|
|
boost::optional<std::string> getAmbientSound() const;
|
|
|
|
boost::optional<std::string> getVisitSound() const;
|
|
|
|
boost::optional<std::string> getRemovalSound() const;
|
|
|
|
|
|
|
|
|
2014-06-24 14:50:27 +03:00
|
|
|
/** VIRTUAL METHODS **/
|
2014-06-24 02:26:36 +03:00
|
|
|
|
|
|
|
/// Returns true if player can pass through visitable tiles of this object
|
|
|
|
virtual bool passableFor(PlayerColor color) const;
|
|
|
|
/// Range of revealed map around this object, counting from getSightCenter()
|
2016-01-31 17:01:58 +02:00
|
|
|
virtual int getSightRadius() const;
|
2014-06-24 02:26:36 +03:00
|
|
|
/// returns (x,y,0) offset to a visitable tile of object
|
|
|
|
virtual int3 getVisitableOffset() const;
|
|
|
|
/// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
|
2014-06-16 19:27:26 +03:00
|
|
|
virtual void setType(si32 ID, si32 subID);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-24 20:39:36 +03:00
|
|
|
/// returns text visible in status bar with specific hero/player active.
|
|
|
|
|
|
|
|
/// Returns generic name of object, without any player-specific info
|
|
|
|
virtual std::string getObjectName() const;
|
|
|
|
|
|
|
|
/// Returns hover name for situation when there are no selected heroes. Default = object name
|
|
|
|
virtual std::string getHoverText(PlayerColor player) const;
|
|
|
|
/// Returns hero-specific hover name, including visited/not visited info. Default = player-specific name
|
|
|
|
virtual std::string getHoverText(const CGHeroInstance * hero) const;
|
|
|
|
|
2014-06-24 14:50:27 +03:00
|
|
|
/** OVERRIDES OF IObjectInterface **/
|
2014-06-24 02:26:36 +03:00
|
|
|
|
2016-09-09 19:30:36 +02:00
|
|
|
void initObj(CRandomGenerator & rand) override;
|
2014-06-05 19:57:43 +03:00
|
|
|
void onHeroVisit(const CGHeroInstance * h) const override;
|
2014-06-24 02:26:36 +03:00
|
|
|
/// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
|
2023-02-12 22:39:17 +02:00
|
|
|
void setProperty(ui8 what, ui32 val) final;
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2017-05-28 15:23:42 +02:00
|
|
|
virtual void afterAddToMap(CMap * map);
|
2022-09-17 13:04:01 +02:00
|
|
|
virtual void afterRemoveFromMap(CMap * map);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2015-11-13 16:47:47 +02:00
|
|
|
///Entry point of binary (de-)serialization
|
2014-06-05 19:57:43 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2022-06-20 16:39:50 +02:00
|
|
|
h & instanceName;
|
|
|
|
h & typeName;
|
|
|
|
h & subTypeName;
|
2017-07-31 15:35:42 +02:00
|
|
|
h & pos;
|
|
|
|
h & ID;
|
|
|
|
h & subID;
|
|
|
|
h & id;
|
|
|
|
h & tempOwner;
|
|
|
|
h & blockVisit;
|
|
|
|
h & appearance;
|
2016-02-22 18:26:42 +02:00
|
|
|
//definfo is handled by map serializer
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-02-22 03:53:14 +02:00
|
|
|
///Entry point of Json (de-)serialization
|
2016-02-22 01:37:19 +02:00
|
|
|
void serializeJson(JsonSerializeFormat & handler);
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
virtual void updateFrom(const JsonNode & data);
|
2021-09-12 13:30:54 +02:00
|
|
|
|
2014-06-05 19:57:43 +03:00
|
|
|
protected:
|
2014-06-24 02:26:36 +03:00
|
|
|
/// virtual method that allows synchronously update object state on server and all clients
|
|
|
|
virtual void setPropertyDer(ui8 what, ui32 val);
|
2014-06-05 19:57:43 +03:00
|
|
|
|
2014-06-24 02:26:36 +03:00
|
|
|
/// Gives dummy bonus from this object to hero. Can be used to track visited state
|
2023-02-12 22:39:17 +02:00
|
|
|
void giveDummyBonus(const ObjectInstanceID & heroID, ui8 duration = Bonus::ONE_DAY) const;
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-02-22 01:37:19 +02:00
|
|
|
///Serialize object-type specific options
|
|
|
|
virtual void serializeJsonOptions(JsonSerializeFormat & handler);
|
2015-11-13 16:47:47 +02:00
|
|
|
|
2016-02-22 01:37:19 +02:00
|
|
|
void serializeJsonOwner(JsonSerializeFormat & handler);
|
2014-06-05 19:57:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// function object which can be used to find an object with an specific sub ID
|
|
|
|
class CGObjectInstanceBySubIdFinder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CGObjectInstanceBySubIdFinder(CGObjectInstance * obj);
|
|
|
|
bool operator()(CGObjectInstance * obj) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CGObjectInstance * obj;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DLL_LINKAGE CObjectHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::vector<ui32> resVals; //default values of resources in gold
|
|
|
|
|
|
|
|
CObjectHandler();
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2014-06-22 13:39:40 +03:00
|
|
|
h & resVals;
|
2014-06-05 19:57:43 +03:00
|
|
|
}
|
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|