2011-12-13 21:23:17 +00:00
|
|
|
#pragma once
|
|
|
|
|
2012-11-13 11:52:23 +00:00
|
|
|
#include "GameConstants.h"
|
2010-12-19 14:39:56 +00:00
|
|
|
#include "../lib/ConstTransitivePtr.h"
|
2014-05-24 01:56:51 +03:00
|
|
|
#include "IHandlerBase.h"
|
2009-04-16 11:14:13 +00:00
|
|
|
|
2009-04-15 14:03:31 +00:00
|
|
|
/*
|
2014-05-24 02:07:54 +03:00
|
|
|
* CObjectClassesHandler.h, part of VCMI engine
|
2009-04-15 14:03:31 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2009-04-16 11:14:13 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-02 23:48:38 +00:00
|
|
|
class CBinaryReader;
|
|
|
|
class CLegacyConfigParser;
|
|
|
|
class JsonNode;
|
2014-05-17 17:50:11 +03:00
|
|
|
class CRandomGenerator;
|
2014-01-02 23:48:38 +00:00
|
|
|
|
|
|
|
class DLL_LINKAGE ObjectTemplate
|
2009-04-16 11:14:13 +00:00
|
|
|
{
|
2014-01-02 23:48:38 +00:00
|
|
|
enum EBlockMapBits
|
|
|
|
{
|
|
|
|
VISIBLE = 1,
|
|
|
|
VISITABLE = 2,
|
|
|
|
BLOCKED = 4
|
|
|
|
};
|
2009-04-16 11:14:13 +00:00
|
|
|
|
2014-01-02 23:48:38 +00:00
|
|
|
/// tiles that are covered by this object, uses EBlockMapBits enum as flags
|
|
|
|
std::vector<std::vector<ui8>> usedTiles;
|
|
|
|
/// directions from which object can be entered, format same as for moveDir in CGHeroInstance(but 0 - 7)
|
|
|
|
ui8 visitDir;
|
|
|
|
/// list of terrains on which this object can be placed
|
|
|
|
std::set<ETerrainType> allowedTerrains;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// H3 ID/subID of this object
|
2013-02-10 23:24:57 +00:00
|
|
|
Obj id;
|
2014-01-02 23:48:38 +00:00
|
|
|
si32 subid;
|
|
|
|
/// print priority, objects with higher priority will be print first, below everything else
|
2009-04-16 11:14:13 +00:00
|
|
|
si32 printPriority;
|
2014-01-02 23:48:38 +00:00
|
|
|
/// animation file that should be used to display object
|
|
|
|
std::string animationFile;
|
|
|
|
|
2014-03-23 20:26:18 +00:00
|
|
|
/// string ID, equals to def base name for h3m files (lower case, no extension) or specified in mod data
|
|
|
|
std::string stringID;
|
|
|
|
|
2014-01-02 23:48:38 +00:00
|
|
|
ui32 getWidth() const;
|
|
|
|
ui32 getHeight() const;
|
|
|
|
void setSize(ui32 width, ui32 height);
|
|
|
|
|
2009-07-25 16:35:47 +00:00
|
|
|
bool isVisitable() const;
|
2014-01-02 23:48:38 +00:00
|
|
|
|
|
|
|
// Checks object used tiles
|
|
|
|
// Position is relative to bottom-right corner of the object, can not be negative
|
|
|
|
bool isWithin(si32 X, si32 Y) const;
|
|
|
|
bool isVisitableAt(si32 X, si32 Y) const;
|
|
|
|
bool isVisibleAt(si32 X, si32 Y) const;
|
|
|
|
bool isBlockedAt(si32 X, si32 Y) const;
|
|
|
|
|
|
|
|
// Checks if object is visitable from certain direction. X and Y must be between -1..+1
|
|
|
|
bool isVisitableFrom(si8 X, si8 Y) const;
|
|
|
|
|
|
|
|
// Checks if object can be placed on specific terrain
|
|
|
|
bool canBePlacedAt(ETerrainType terrain) const;
|
|
|
|
|
|
|
|
ObjectTemplate();
|
|
|
|
|
|
|
|
void readTxt(CLegacyConfigParser & parser);
|
|
|
|
void readMsk();
|
|
|
|
void readMap(CBinaryReader & reader);
|
|
|
|
void readJson(const JsonNode & node);
|
|
|
|
|
2009-04-16 11:14:13 +00:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
2014-03-23 20:26:18 +00:00
|
|
|
h & usedTiles & allowedTerrains & animationFile & stringID;
|
2014-01-02 23:48:38 +00:00
|
|
|
h & id & subid & printPriority & visitDir;
|
2009-04-16 11:14:13 +00:00
|
|
|
}
|
|
|
|
};
|
2014-01-02 23:48:38 +00:00
|
|
|
|
2014-04-25 17:59:05 +03:00
|
|
|
class IObjectInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool givesResources() const = 0;
|
|
|
|
|
|
|
|
virtual bool givesExperience() const = 0;
|
|
|
|
virtual bool givesMana() const = 0;
|
|
|
|
virtual bool givesMovement() const = 0;
|
|
|
|
|
|
|
|
virtual bool givesPrimarySkills() const = 0;
|
|
|
|
virtual bool givesSecondarySkills() const = 0;
|
|
|
|
|
|
|
|
virtual bool givesArtifacts() const = 0;
|
|
|
|
virtual bool givesCreatures() const = 0;
|
|
|
|
virtual bool givesSpells() const = 0;
|
|
|
|
|
|
|
|
virtual bool givesBonuses() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CGObjectInstance;
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
class AObjectTypeHandler
|
2014-04-25 17:59:05 +03:00
|
|
|
{
|
2014-05-16 23:50:02 +03:00
|
|
|
si32 type;
|
|
|
|
si32 subtype;
|
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
JsonNode base; /// describes base template
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
std::vector<ObjectTemplate> templates;
|
|
|
|
protected:
|
2014-05-24 01:56:51 +03:00
|
|
|
void setType(si32 type, si32 subtype);
|
2014-05-16 23:50:02 +03:00
|
|
|
|
|
|
|
virtual bool objectFilter(const CGObjectInstance *, const ObjectTemplate &) const;
|
2014-04-25 17:59:05 +03:00
|
|
|
public:
|
2014-05-24 01:56:51 +03:00
|
|
|
/// loads templates from Json structure using fields "base" and "templates"
|
|
|
|
virtual void init(const JsonNode & input);
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
void addTemplate(const ObjectTemplate & templ);
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
/// returns all templates, without any filters
|
|
|
|
std::vector<ObjectTemplate> getTemplates() const;
|
|
|
|
|
|
|
|
/// returns all templates that can be placed on specific terrain type
|
|
|
|
std::vector<ObjectTemplate> getTemplates(si32 terrainType) const;
|
2014-04-27 20:37:53 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
/// returns preferred template for this object, if present (e.g. one of 3 possible templates for town - village, fort and castle)
|
|
|
|
/// note that appearance will not be changed - this must be done separately (either by assignment or via pack from server)
|
|
|
|
boost::optional<ObjectTemplate> getOverride(si32 terrainType, const CGObjectInstance * object) const;
|
2014-05-16 23:50:02 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
/// Creates object and set up core properties (like ID/subID). Object is NOT initialized
|
|
|
|
/// to allow creating objects before game start (e.g. map loading)
|
2014-05-16 23:50:02 +03:00
|
|
|
virtual CGObjectInstance * create(ObjectTemplate tmpl) const = 0;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
/// Configures object properties. Should be re-entrable, resetting state of the object if necessarily
|
2014-05-17 17:50:11 +03:00
|
|
|
virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const = 0;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
/// Returns object configuration, if available. Othervice returns NULL
|
2014-04-27 20:37:53 +03:00
|
|
|
virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const = 0;
|
2014-05-24 01:56:51 +03:00
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & type & subtype & templates;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class ObjectType>
|
|
|
|
class CDefaultObjectTypeHandler : public AObjectTypeHandler
|
|
|
|
{
|
|
|
|
CGObjectInstance * create(ObjectTemplate tmpl) const
|
|
|
|
{
|
|
|
|
auto obj = new ObjectType();
|
|
|
|
obj->ID = tmpl.id;
|
|
|
|
obj->subID = tmpl.subid;
|
|
|
|
obj->appearance = tmpl;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void configureObject(CGObjectInstance * object, CRandomGenerator & rng) const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const IObjectInfo * getObjectInfo(ObjectTemplate tmpl) const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-25 17:59:05 +03:00
|
|
|
};
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
typedef std::shared_ptr<AObjectTypeHandler> TObjectTypeHandler;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
|
2014-04-25 17:59:05 +03:00
|
|
|
{
|
2014-05-24 01:56:51 +03:00
|
|
|
/// Small internal structure that contains information on specific group of objects
|
|
|
|
/// (creating separate entity is overcomplicating at least at this point)
|
|
|
|
struct ObjectContainter
|
|
|
|
{
|
|
|
|
si32 id;
|
|
|
|
|
|
|
|
std::string name; // human-readable name
|
|
|
|
std::string handlerName; // ID of handler that controls this object, shoul be determined using hadlerConstructor map
|
|
|
|
|
|
|
|
JsonNode base;
|
|
|
|
std::map<si32, TObjectTypeHandler> objects;
|
|
|
|
|
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
|
|
|
{
|
|
|
|
h & base & objects;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
/// list of object handlers, each of them handles only one type
|
2014-05-24 01:56:51 +03:00
|
|
|
std::map<si32, ObjectContainter * > objects;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
/// map that is filled during contruction with all known handlers. Not serializeable
|
|
|
|
std::map<std::string, std::function<TObjectTypeHandler()> > handlerConstructors;
|
|
|
|
|
|
|
|
ObjectContainter * loadFromJson(const JsonNode & json);
|
2014-04-25 17:59:05 +03:00
|
|
|
public:
|
2014-05-24 01:56:51 +03:00
|
|
|
CObjectClassesHandler();
|
|
|
|
|
|
|
|
virtual std::vector<JsonNode> loadLegacyData(size_t dataSize);
|
|
|
|
|
|
|
|
virtual void loadObject(std::string scope, std::string name, const JsonNode & data);
|
|
|
|
virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index);
|
|
|
|
|
|
|
|
virtual void afterLoadFinalization(){};
|
|
|
|
|
|
|
|
virtual std::vector<bool> getDefaultAllowed() const;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
/// returns handler for specified object (ID-based). ObjectHandler keeps ownership
|
|
|
|
TObjectTypeHandler getHandlerFor(si32 type, si32 subtype) const;
|
2014-04-25 17:59:05 +03:00
|
|
|
|
2014-05-24 01:56:51 +03:00
|
|
|
std::string getObjectName(si32 type) const;
|
|
|
|
|
2014-05-16 23:50:02 +03:00
|
|
|
template <typename Handler> void serialize(Handler &h, const int version)
|
2014-04-25 17:59:05 +03:00
|
|
|
{
|
2014-05-24 01:56:51 +03:00
|
|
|
h & objects;
|
2014-04-25 17:59:05 +03:00
|
|
|
}
|
2014-04-27 20:37:53 +03:00
|
|
|
};
|