CMapOperation.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, const CTerrainSelection & terrainSel, TerrainId terType, 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, const 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, nativeTiles;
  69. bool centerPosValid;
  70. InvalidTiles() : centerPosValid(false) { }
  71. };
  72. void updateTerrainTypes();
  73. void invalidateTerrainViews(const int3 & centerPos);
  74. InvalidTiles getInvalidTiles(const int3 & centerPos) const;
  75. void updateTerrainViews();
  76. /// Validates the terrain view of the given position and with the given pattern. The first method wraps the
  77. /// second method to validate the terrain view with the given pattern in all four flip directions(horizontal, vertical).
  78. ValidationResult validateTerrainView(const int3 & pos, const std::vector<TerrainViewPattern> * pattern, int recDepth = 0) const;
  79. ValidationResult validateTerrainViewInner(const int3 & pos, const TerrainViewPattern & pattern, int recDepth = 0) const;
  80. CTerrainSelection terrainSel;
  81. TerrainId terType;
  82. CRandomGenerator* gen;
  83. std::set<int3> invalidatedTerViews;
  84. };
  85. /// The CClearTerrainOperation clears+initializes the terrain.
  86. class CClearTerrainOperation : public CComposedOperation
  87. {
  88. public:
  89. CClearTerrainOperation(CMap * map, CRandomGenerator * gen);
  90. std::string getLabel() const override;
  91. };
  92. /// The CInsertObjectOperation class inserts an object to the map.
  93. class CInsertObjectOperation : public CMapOperation
  94. {
  95. public:
  96. CInsertObjectOperation(CMap * map, CGObjectInstance * obj);
  97. void execute() override;
  98. void undo() override;
  99. void redo() override;
  100. std::string getLabel() const override;
  101. private:
  102. CGObjectInstance * obj;
  103. };
  104. /// The CMoveObjectOperation class moves object to another position
  105. class CMoveObjectOperation : public CMapOperation
  106. {
  107. public:
  108. CMoveObjectOperation(CMap * map, CGObjectInstance * obj, const int3 & targetPosition);
  109. void execute() override;
  110. void undo() override;
  111. void redo() override;
  112. std::string getLabel() const override;
  113. private:
  114. CGObjectInstance * obj;
  115. int3 initialPos;
  116. int3 targetPos;
  117. };
  118. /// The CRemoveObjectOperation class removes object from the map
  119. class CRemoveObjectOperation : public CMapOperation
  120. {
  121. public:
  122. CRemoveObjectOperation(CMap* map, CGObjectInstance * obj);
  123. ~CRemoveObjectOperation();
  124. void execute() override;
  125. void undo() override;
  126. void redo() override;
  127. std::string getLabel() const override;
  128. private:
  129. CGObjectInstance* obj;
  130. };
  131. VCMI_LIB_NAMESPACE_END