CMapOperation.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * CMapOperation.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../int3.h"
  12. #include "MapEditUtils.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class CGObjectInstance;
  15. class CMap;
  16. namespace vstd
  17. {
  18. class RNG;
  19. }
  20. /// The abstract base class CMapOperation defines an operation that can be executed, undone and redone.
  21. class DLL_LINKAGE CMapOperation : public boost::noncopyable
  22. {
  23. public:
  24. explicit CMapOperation(CMap * map);
  25. virtual ~CMapOperation() = default;
  26. virtual void execute() = 0;
  27. virtual void undo() = 0;
  28. virtual void redo() = 0;
  29. virtual std::string getLabel() const = 0; /// Returns a operation display name.
  30. static const int FLIP_PATTERN_HORIZONTAL = 1;
  31. static const int FLIP_PATTERN_VERTICAL = 2;
  32. static const int FLIP_PATTERN_BOTH = 3;
  33. protected:
  34. MapRect extendTileAround(const int3 & centerPos) const;
  35. MapRect extendTileAroundSafely(const int3 & centerPos) const; /// doesn't exceed map size
  36. CMap * map;
  37. };
  38. /// The CComposedOperation is an operation which consists of several operations.
  39. class DLL_LINKAGE CComposedOperation : public CMapOperation
  40. {
  41. public:
  42. CComposedOperation(CMap * map);
  43. void execute() override;
  44. void undo() override;
  45. void redo() override;
  46. std::string getLabel() const override;
  47. void addOperation(std::unique_ptr<CMapOperation> && operation);
  48. private:
  49. std::list<std::unique_ptr<CMapOperation> > operations;
  50. };
  51. /// The CDrawTerrainOperation class draws a terrain area on the map.
  52. class CDrawTerrainOperation : public CMapOperation
  53. {
  54. public:
  55. CDrawTerrainOperation(CMap * map, CTerrainSelection terrainSel, TerrainId terType, int decorationsPercentage, vstd::RNG * gen);
  56. void execute() override;
  57. void undo() override;
  58. void redo() override;
  59. std::string getLabel() const override;
  60. private:
  61. struct ValidationResult
  62. {
  63. ValidationResult(bool result, std::string transitionReplacement = "");
  64. bool result;
  65. /// The replacement of a T rule, either D or S.
  66. std::string transitionReplacement;
  67. int flip;
  68. };
  69. struct InvalidTiles
  70. {
  71. std::set<int3> foreignTiles;
  72. std::set<int3> nativeTiles;
  73. bool centerPosValid;
  74. InvalidTiles() : centerPosValid(false) { }
  75. };
  76. void updateTerrainTypes();
  77. void invalidateTerrainViews(const int3 & centerPos);
  78. InvalidTiles getInvalidTiles(const int3 & centerPos) const;
  79. void updateTerrainViews();
  80. /// Validates the terrain view of the given position and with the given pattern. The first method wraps the
  81. /// second method to validate the terrain view with the given pattern in all four flip directions(horizontal, vertical).
  82. ValidationResult validateTerrainView(const int3 & pos, const std::vector<TerrainViewPattern> * pattern, int recDepth = 0) const;
  83. ValidationResult validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  84. CTerrainSelection terrainSel;
  85. TerrainId terType;
  86. int decorationsPercentage;
  87. vstd::RNG* gen;
  88. std::set<int3> invalidatedTerViews;
  89. };
  90. /// The CClearTerrainOperation clears+initializes the terrain.
  91. class CClearTerrainOperation : public CComposedOperation
  92. {
  93. public:
  94. CClearTerrainOperation(CMap * map, vstd::RNG * gen);
  95. std::string getLabel() const override;
  96. };
  97. /// The CInsertObjectOperation class inserts an object to the map.
  98. class CInsertObjectOperation : public CMapOperation
  99. {
  100. public:
  101. CInsertObjectOperation(CMap * map, CGObjectInstance * obj);
  102. void execute() override;
  103. void undo() override;
  104. void redo() override;
  105. std::string getLabel() const override;
  106. private:
  107. CGObjectInstance * obj;
  108. };
  109. /// The CMoveObjectOperation class moves object to another position
  110. class CMoveObjectOperation : public CMapOperation
  111. {
  112. public:
  113. CMoveObjectOperation(CMap * map, CGObjectInstance * obj, const int3 & targetPosition);
  114. void execute() override;
  115. void undo() override;
  116. void redo() override;
  117. std::string getLabel() const override;
  118. private:
  119. CGObjectInstance * obj;
  120. int3 initialPos;
  121. int3 targetPos;
  122. };
  123. /// The CRemoveObjectOperation class removes object from the map
  124. class CRemoveObjectOperation : public CMapOperation
  125. {
  126. public:
  127. CRemoveObjectOperation(CMap* map, CGObjectInstance * obj);
  128. ~CRemoveObjectOperation();
  129. void execute() override;
  130. void undo() override;
  131. void redo() override;
  132. std::string getLabel() const override;
  133. private:
  134. CGObjectInstance* obj;
  135. };
  136. VCMI_LIB_NAMESPACE_END