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
|
/*
* CGObjectInstance.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 "IObjectInterface.h"
#include "../bonuses/BonusEnum.h"
#include "../constants/EntityIdentifiers.h"
#include "../filesystem/ResourcePath.h"
#include "../int3.h"
VCMI_LIB_NAMESPACE_BEGIN
struct Component;
class JsonSerializeFormat;
class ObjectTemplate;
class CMap;
class AObjectTypeHandler;
using TObjectTypeHandler = std::shared_ptr<AObjectTypeHandler>;
class DLL_LINKAGE CGObjectInstance : public IObjectInterface
{
public:
/// Type of object, e.g. town, hero, creature.
MapObjectID ID;
/// Subtype of object, depends on type
MapObjectSubID subID;
/// Current owner of an object (when below PLAYER_LIMIT)
PlayerColor tempOwner;
/// Index of object in map's list of objects
ObjectInstanceID id;
/// Defines appearance of object on map (animation, blocked tiles, blit order, etc)
std::shared_ptr<const ObjectTemplate> appearance;
/// Position of bottom-right corner of object on map
int3 pos;
std::string instanceName;
CGObjectInstance(IGameCallback *cb);
~CGObjectInstance() override;
MapObjectID getObjGroupIndex() const override;
MapObjectSubID getObjTypeIndex() const override;
std::string getTypeName() const;
std::string getSubtypeName() const;
/// "center" tile from which the sight distance is calculated
int3 getSightCenter() const;
/// If true hero can visit this object only from neighbouring tiles and can't stand on this object
bool blockVisit;
bool removable;
PlayerColor getOwner() const override
{
return this->tempOwner;
}
void setOwner(const PlayerColor & ow);
void setAnchorPos(int3 pos);
/** APPEARANCE ACCESSORS **/
int getWidth() const; //returns width of object graphic in tiles
int getHeight() const; //returns height of object graphic in tiles
int3 visitablePos() const override;
int3 anchorPos() const override;
int3 getTopVisiblePos() const;
bool visitableAt(const int3 & pos) const; //returns true if object is visitable at location
bool blockingAt (const int3 & pos) const; //returns true if object is blocking location
bool coveringAt (const int3 & pos) const; //returns true if object covers with picture location
std::set<int3> getBlockedPos() const; //returns set of positions blocked by this object
const std::set<int3> & getBlockedOffsets() const; //returns set of relative positions blocked by this object
/// returns true if object is visitable
bool isVisitable() const;
/// If true hero can visit this object only from neighbouring tiles and can't stand on this object
virtual bool isBlockedVisitable() const;
// If true, can be possibly removed from the map
virtual bool isRemovable() const;
/// If true this object can be visited by hero standing on the coast
virtual bool isCoastVisitable() const;
virtual BattleField getBattlefield() const;
virtual bool isTile2Terrain() const { return false; }
std::optional<AudioPath> getAmbientSound(vstd::RNG & rng) const;
std::optional<AudioPath> getVisitSound(vstd::RNG & rng) const;
std::optional<AudioPath> getRemovalSound(vstd::RNG & rng) const;
virtual TObjectTypeHandler getObjectHandler() const;
/** VIRTUAL METHODS **/
/// 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()
virtual int getSightRadius() const;
/// returns (x,y,0) offset to a visitable tile of object
virtual int3 getVisitableOffset() const;
/// 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;
virtual std::string getPopupText(PlayerColor player) const;
virtual std::string getPopupText(const CGHeroInstance * hero) const;
virtual std::vector<Component> getPopupComponents(PlayerColor player) const;
virtual std::vector<Component> getPopupComponents(const CGHeroInstance * hero) const;
const IOwnableObject * asOwnable() const override;
/** OVERRIDES OF IObjectInterface **/
void initObj(vstd::RNG & rand) override;
void pickRandomObject(vstd::RNG & rand) override;
void onHeroVisit(const CGHeroInstance * h) const override;
/// method for synchronous update. Note: For new properties classes should override setPropertyDer instead
void setProperty(ObjProperty what, ObjPropertyID identifier) final;
virtual void afterAddToMap(CMap * map);
virtual void afterRemoveFromMap(CMap * map);
///Entry point of binary (de-)serialization
template <typename Handler> void serialize(Handler &h)
{
h & instanceName;
if (h.version < Handler::Version::REMOVE_OBJECT_TYPENAME)
{
std::string unused;
h & unused;
h & unused;
}
h & pos;
h & ID;
subID.serializeIdentifier(h, ID);
h & id;
h & tempOwner;
h & blockVisit;
h & removable;
h & appearance;
//definfo is handled by map serializer
}
///Entry point of Json (de-)serialization
void serializeJson(JsonSerializeFormat & handler);
virtual void updateFrom(const JsonNode & data);
protected:
/// virtual method that allows synchronously update object state on server and all clients
virtual void setPropertyDer(ObjProperty what, ObjPropertyID identifier);
/// Called mostly during map randomization to turn random object into a regular one (e.g. "Random Monster" into "Pikeman")
void setType(MapObjectID ID, MapObjectSubID subID);
/// Gives dummy bonus from this object to hero. Can be used to track visited state
void giveDummyBonus(const ObjectInstanceID & heroID, BonusDuration::Type duration = BonusDuration::ONE_DAY) const;
///Serialize object-type specific options
virtual void serializeJsonOptions(JsonSerializeFormat & handler);
void serializeJsonOwner(JsonSerializeFormat & handler);
};
VCMI_LIB_NAMESPACE_END
|