CMapOperation.h 4.6 KB

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