2013-01-06 22:30:12 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CMapEditManager.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 "../CRandomGenerator.h"
|
|
|
|
#include "CMap.h"
|
|
|
|
|
|
|
|
class CGObjectInstance;
|
2013-04-16 16:16:58 +03:00
|
|
|
class CTerrainViewPatternConfig;
|
2013-04-21 15:49:26 +03:00
|
|
|
struct TerrainViewPattern;
|
2013-01-06 22:30:12 +03:00
|
|
|
|
|
|
|
namespace ETerrainGroup
|
|
|
|
{
|
|
|
|
enum ETerrainGroup
|
|
|
|
{
|
|
|
|
NORMAL,
|
|
|
|
DIRT,
|
|
|
|
SAND,
|
|
|
|
WATER,
|
|
|
|
ROCK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Represents a map rectangle.
|
|
|
|
struct DLL_LINKAGE MapRect
|
|
|
|
{
|
|
|
|
MapRect(int3 pos, si32 width, si32 height) : pos(pos), width(width), height(height) { };
|
|
|
|
int3 pos;
|
|
|
|
si32 width, height;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The abstract base class CMapOperation defines an operation that can be executed, undone and redone.
|
|
|
|
class CMapOperation
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
2013-04-16 16:16:58 +03:00
|
|
|
public:
|
2013-04-19 14:43:11 +03:00
|
|
|
CMapOperation(CMap * map);
|
|
|
|
virtual ~CMapOperation() { };
|
2013-04-16 16:16:58 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
virtual void execute() = 0;
|
|
|
|
virtual void undo() = 0;
|
|
|
|
virtual void redo() = 0;
|
|
|
|
virtual std::string getLabel() const; /// Returns a display-able name of the operation.
|
2013-04-16 16:16:58 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
protected:
|
|
|
|
CMap * map;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The CMapUndoManager provides the functionality to save operations and undo/redo them.
|
2013-04-20 14:34:01 +03:00
|
|
|
class CMapUndoManager : boost::noncopyable
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CMapUndoManager();
|
|
|
|
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
void clearAll();
|
|
|
|
|
|
|
|
/// The undo redo limit is a number which says how many undo/redo items can be saved. The default
|
|
|
|
/// value is 10. If the value is 0, no undo/redo history will be maintained.
|
|
|
|
int getUndoRedoLimit() const;
|
|
|
|
void setUndoRedoLimit(int value);
|
|
|
|
|
|
|
|
const CMapOperation * peekRedo() const;
|
|
|
|
const CMapOperation * peekUndo() const;
|
|
|
|
|
|
|
|
void addOperation(unique_ptr<CMapOperation> && operation); /// Client code does not need to call this method.
|
2013-04-16 16:16:58 +03:00
|
|
|
|
|
|
|
private:
|
2013-04-19 14:43:11 +03:00
|
|
|
typedef std::list<unique_ptr<CMapOperation> > TStack;
|
2013-01-26 16:48:20 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
inline void doOperation(TStack & fromStack, TStack & toStack, bool doUndo);
|
|
|
|
inline const CMapOperation * peek(const TStack & stack) const;
|
2013-01-26 16:48:20 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
TStack undoStack;
|
|
|
|
TStack redoStack;
|
|
|
|
int undoRedoLimit;
|
|
|
|
};
|
2013-04-16 16:16:58 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// The map edit manager provides functionality for drawing terrain and placing
|
|
|
|
/// objects on the map.
|
2013-04-20 14:34:01 +03:00
|
|
|
class DLL_LINKAGE CMapEditManager : boost::noncopyable
|
2013-04-19 14:43:11 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CMapEditManager(CMap * map);
|
|
|
|
|
|
|
|
/// Clears the terrain. The free level is filled with water and the underground level with rock.
|
|
|
|
void clearTerrain(CRandomGenerator * gen);
|
|
|
|
|
|
|
|
void drawTerrain(const MapRect & rect, ETerrainType terType, CRandomGenerator * gen);
|
|
|
|
void insertObject(const int3 & pos, CGObjectInstance * obj);
|
|
|
|
|
|
|
|
CMapUndoManager & getUndoManager();
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void execute(unique_ptr<CMapOperation> && operation);
|
2013-04-16 16:16:58 +03:00
|
|
|
|
|
|
|
CMap * map;
|
2013-04-19 14:43:11 +03:00
|
|
|
CMapUndoManager undoManager;
|
2013-04-16 16:16:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------- */
|
|
|
|
/* Implementation/Detail classes, Private API */
|
|
|
|
/* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/// The terrain view pattern describes a specific composition of terrain tiles
|
|
|
|
/// in a 3x3 matrix and notes which terrain view frame numbers can be used.
|
|
|
|
struct TerrainViewPattern
|
|
|
|
{
|
|
|
|
struct WeightedRule
|
|
|
|
{
|
2013-01-26 16:48:20 +03:00
|
|
|
WeightedRule();
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Gets true if this rule is a standard rule which means that it has a value of one of the RULE_* constants.
|
2013-01-26 16:48:20 +03:00
|
|
|
bool isStandardRule() const;
|
2013-04-16 16:16:58 +03:00
|
|
|
|
|
|
|
/// The name of the rule. Can be any value of the RULE_* constants or a ID of a another pattern.
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
/// Optional. A rule can have points. Patterns may have a minimum count of points to reach to be successful.
|
|
|
|
int points;
|
2013-01-26 16:48:20 +03:00
|
|
|
};
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the flip mode same image. Pattern will be flipped and the same image will be used(which is given in the mapping).
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string FLIP_MODE_SAME_IMAGE;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the flip mode different images. Pattern will be flipped and different images will be used(mapping area is divided into 4 parts)
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string FLIP_MODE_DIFF_IMAGES;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the rule dirt, meaning a dirty border is required.
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string RULE_DIRT;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the rule sand, meaning a sandy border is required.
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string RULE_SAND;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the rule transition, meaning a dirty OR sandy border is required.
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string RULE_TRANSITION;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the rule native, meaning a native type is required.
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string RULE_NATIVE;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Constant for the rule any, meaning a native type, dirty OR sandy border is required.
|
2013-01-06 22:30:12 +03:00
|
|
|
static const std::string RULE_ANY;
|
|
|
|
|
|
|
|
TerrainViewPattern();
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// The pattern data can be visualized as a 3x3 matrix:
|
|
|
|
/// [ ][ ][ ]
|
|
|
|
/// [ ][ ][ ]
|
|
|
|
/// [ ][ ][ ]
|
|
|
|
///
|
|
|
|
/// The box in the center belongs always to the native terrain type and
|
|
|
|
/// is the point of origin. Depending on the terrain type different rules
|
|
|
|
/// can be used. Their meaning differs also from type to type.
|
|
|
|
///
|
|
|
|
/// std::vector -> several rules can be used in one cell
|
2013-01-26 16:48:20 +03:00
|
|
|
std::array<std::vector<WeightedRule>, 9> data;
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// The identifier of the pattern, if it's referenced from a another pattern.
|
2013-01-06 22:30:12 +03:00
|
|
|
std::string id;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// This describes the mapping between this pattern and the corresponding range of frames
|
|
|
|
/// which should be used for the ter view.
|
|
|
|
///
|
|
|
|
/// std::vector -> size=1: typical, size=2: if this pattern should map to two different types of borders
|
|
|
|
/// std::pair -> 1st value: lower range, 2nd value: upper range
|
2013-01-06 22:30:12 +03:00
|
|
|
std::vector<std::pair<int, int> > mapping;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// The minimum points to reach to to validate the pattern successfully.
|
2013-01-06 22:30:12 +03:00
|
|
|
int minPoints;
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// Describes if flipping is required and which mapping should be used.
|
2013-01-06 22:30:12 +03:00
|
|
|
std::string flipMode;
|
|
|
|
|
|
|
|
ETerrainGroup::ETerrainGroup terGroup;
|
|
|
|
};
|
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
/// The terrain view pattern config loads pattern data from the filesystem.
|
2013-04-19 14:43:11 +03:00
|
|
|
class CTerrainViewPatternConfig : public boost::noncopyable
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
public:
|
2013-04-16 16:16:58 +03:00
|
|
|
static CTerrainViewPatternConfig & get();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
|
|
|
const std::vector<TerrainViewPattern> & getPatternsForGroup(ETerrainGroup::ETerrainGroup terGroup) const;
|
2013-01-26 16:48:20 +03:00
|
|
|
const TerrainViewPattern & getPatternById(ETerrainGroup::ETerrainGroup terGroup, const std::string & id) const;
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
private:
|
2013-04-16 16:16:58 +03:00
|
|
|
CTerrainViewPatternConfig();
|
|
|
|
~CTerrainViewPatternConfig();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-16 16:16:58 +03:00
|
|
|
std::map<ETerrainGroup::ETerrainGroup, std::vector<TerrainViewPattern> > patterns;
|
|
|
|
static boost::mutex smx;
|
2013-01-06 22:30:12 +03:00
|
|
|
};
|
2013-04-19 14:43:11 +03:00
|
|
|
|
|
|
|
/// The DrawTerrainOperation class draws a terrain area on the map.
|
|
|
|
class DrawTerrainOperation : public CMapOperation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DrawTerrainOperation(CMap * map, const MapRect & rect, ETerrainType terType, CRandomGenerator * gen);
|
|
|
|
|
|
|
|
void execute();
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
std::string getLabel() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct ValidationResult
|
|
|
|
{
|
|
|
|
ValidationResult(bool result, const std::string & transitionReplacement = "");
|
|
|
|
|
|
|
|
bool result;
|
|
|
|
/// The replacement of a T rule, either D or S.
|
|
|
|
std::string transitionReplacement;
|
|
|
|
};
|
|
|
|
|
|
|
|
void updateTerrainViews(const MapRect & rect);
|
|
|
|
ETerrainGroup::ETerrainGroup getTerrainGroup(ETerrainType terType) const;
|
|
|
|
/// Validates the terrain view of the given position and with the given pattern.
|
|
|
|
ValidationResult validateTerrainView(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
|
|
|
|
/// Tests whether the given terrain type is a sand type. Sand types are: Water, Sand and Rock
|
|
|
|
bool isSandType(ETerrainType terType) const;
|
|
|
|
TerrainViewPattern getFlippedPattern(const TerrainViewPattern & pattern, int flip) const;
|
|
|
|
|
|
|
|
static const int FLIP_PATTERN_HORIZONTAL = 1;
|
|
|
|
static const int FLIP_PATTERN_VERTICAL = 2;
|
|
|
|
static const int FLIP_PATTERN_BOTH = 3;
|
|
|
|
|
|
|
|
MapRect rect;
|
|
|
|
ETerrainType terType;
|
|
|
|
CRandomGenerator * gen;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The InsertObjectOperation class inserts an object to the map.
|
|
|
|
class InsertObjectOperation : public CMapOperation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InsertObjectOperation(CMap * map, const int3 & pos, CGObjectInstance * obj);
|
|
|
|
|
|
|
|
void execute();
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
std::string getLabel() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int3 pos;
|
|
|
|
CGObjectInstance * obj;
|
|
|
|
};
|