Tube.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <vector>
  18. #include <string>
  19. #include <functional>
  20. #include "Structs.h"
  21. enum class ETubeDirection : char
  22. {
  23. Undefined = -1,
  24. Top=0,
  25. TopRight=1,
  26. Right=2,
  27. BottomRight=3,
  28. Bottom=4,
  29. BottomLeft=5,
  30. Left=6,
  31. TopLeft=7,
  32. };
  33. inline ETubeDirection ToTubeDirection(const std::underlying_type_t<ETubeDirection> dir)
  34. {
  35. if (dir > 7 || dir < -1)
  36. return ETubeDirection::Undefined;
  37. return static_cast<ETubeDirection>(dir);
  38. }
  39. inline auto to_int(const ETubeDirection dir)
  40. {
  41. return static_cast<std::underlying_type_t<ETubeDirection>>(dir);
  42. }
  43. ETubeDirection opposite_dir(ETubeDirection dir);
  44. bool dir_to_xy(ETubeDirection dir, MapVec& y);
  45. class CTube
  46. {
  47. public:
  48. struct WalkInfo
  49. {
  50. MapCoords pos;
  51. ETubeDirection direction = ETubeDirection::Undefined;
  52. MapCoords next_pos;
  53. };
  54. public:
  55. CTube() = default;
  56. CTube(std::uint16_t tubeId, const std::string& s);
  57. CTube(
  58. std::uint16_t tubeID,
  59. std::uint16_t startX,
  60. std::uint16_t startY,
  61. ETubeDirection direction,
  62. std::uint16_t endX,
  63. std::uint16_t endY,
  64. const std::vector<ETubeDirection>& tubeParts
  65. );
  66. CTube(
  67. std::uint16_t startX,
  68. std::uint16_t startY,
  69. ETubeDirection direction,
  70. std::uint16_t endX,
  71. std::uint16_t endY,
  72. const std::vector<ETubeDirection>& tubeParts
  73. );
  74. CTube reverse(std::uint16_t newTubeID=0xFFFF) const;
  75. // full equivalency including id
  76. bool operator==(const CTube& r) const = default;
  77. bool isEqual(const CTube& r, bool ignoreId) const;
  78. // Create tube with simple algorithm
  79. static CTube autocreate(std::uint16_t startX, std::uint16_t startY, std::uint16_t endX, std::uint16_t endY, int straightStartParts=1);
  80. const std::vector<ETubeDirection>& GetTubeParts() const
  81. {
  82. return m_tubeParts;
  83. }
  84. void setId(std::uint16_t id)
  85. {
  86. m_tubeId = id;
  87. }
  88. bool isCounterpart(const CTube& other) const
  89. {
  90. return getStartCoords() == other.getEndCoords() && getEndCoords() == other.getStartCoords();
  91. }
  92. bool hasId() const
  93. {
  94. return m_tubeId != 0xFFFF;
  95. }
  96. auto getId() const { return m_tubeId; }
  97. auto getStartX() const { return m_startX; }
  98. auto getStartY() const { return m_startY; }
  99. MapCoords getStartCoords() const { return MapCoords(m_startX, m_startY); }
  100. auto getInitialDirection() const { return m_direction; }
  101. ETubeDirection getLastDirection() const;
  102. auto getEndX() const { return m_endX; }
  103. auto getEndY() const { return m_endY; }
  104. MapCoords getEndCoords() const { return MapCoords(m_endX, m_endY); }
  105. bool touches(const MapCoords& mc) const;
  106. std::string toString() const;
  107. bool append(std::uint16_t endX, std::uint16_t endY, int forceStraightParts=-1);
  108. bool walk(const std::function<bool(const WalkInfo&)>& walker) const;
  109. bool isValid() const;
  110. private:
  111. std::uint16_t m_tubeId = 0xFFFF; // ID of tube
  112. std::uint16_t m_startX = 0;
  113. std::uint16_t m_startY = 0;
  114. ETubeDirection m_direction = ETubeDirection::Undefined;
  115. std::uint16_t m_endX = 0;
  116. std::uint16_t m_endY = 0;
  117. std::vector<ETubeDirection> m_tubeParts;
  118. };