CMapOperation.h 4.3 KB

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