CMapOperation.h 4.4 KB

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