WorldHeightMap.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  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. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // WorldHeightMap.h
  24. // Class to encapsulate height map.
  25. // Author: John Ahlquist, April 2001
  26. #pragma once
  27. #ifndef WorldHeightMap_H
  28. #define WorldHeightMap_H
  29. #include "Lib/BaseType.h"
  30. #include "WWLib/RefCount.h"
  31. #include "WWMath/Vector3.h"
  32. #include "W3DDevice/GameClient/TileData.h"
  33. #include "../../gameengine/include/common/MapObject.h"
  34. #include "Common/STLTypedefs.h"
  35. typedef std::vector<ICoord2D> VecICoord2D;
  36. /** MapObject class
  37. Not ref counted. Do not store pointers to this class. */
  38. #define K_MIN_HEIGHT 0
  39. #define K_MAX_HEIGHT 255
  40. #define NUM_SOURCE_TILES 1024
  41. #define NUM_BLEND_TILES 16192
  42. #define NUM_CLIFF_INFO 32384
  43. #define FLAG_VAL 0x7ADA0000
  44. // For backwards compatiblity.
  45. #define TEX_PATH_LEN 256
  46. /// Struct in memory.
  47. typedef struct {
  48. Int globalTextureClass;
  49. Int firstTile;
  50. Int numTiles;
  51. Int width;
  52. Int isBlendEdgeTile; ///< True if the texture contains blend edges.
  53. AsciiString name;
  54. ICoord2D positionInTexture;
  55. } TXTextureClass;
  56. typedef enum {POS_X, POS_Y, NEG_X, NEG_Y} TVDirection;
  57. /// Struct in memory.
  58. typedef struct {
  59. Real u0, v0; // Upper left uv
  60. Real u1, v1; // Lower left uv
  61. Real u2, v2; // Lower right uv
  62. Real u3, v3; // Upper right uv
  63. Bool flip;
  64. Bool mutant; // Mutant mapping needed to get this to fit.
  65. Short tileIndex; // Tile texture.
  66. } TCliffInfo;
  67. #define NUM_TEXTURE_CLASSES 256
  68. class TextureClass;
  69. class ChunkInputStream;
  70. class InputStream;
  71. class OutputStream;
  72. class DataChunkInput;
  73. struct DataChunkInfo;
  74. class AlphaEdgeTextureClass;
  75. class WorldHeightMap : public RefCountClass
  76. {
  77. friend class TerrainTextureClass;
  78. friend class AlphaTerrainTextureClass;
  79. friend class W3DCustomEdging;
  80. friend class AlphaEdgeTextureClass;
  81. #define NO_EVAL_TILING_MODES
  82. public:
  83. #ifdef EVAL_TILING_MODES
  84. enum {TILE_4x4, TILE_6x6, TILE_8x8} m_tileMode;
  85. #endif
  86. enum {
  87. NORMAL_DRAW_WIDTH = 129,
  88. NORMAL_DRAW_HEIGHT = 129,
  89. STRETCH_DRAW_WIDTH = 65,
  90. STRETCH_DRAW_HEIGHT = 65
  91. };
  92. protected:
  93. Int m_width; ///< Height map width.
  94. Int m_height; ///< Height map height (y size of array).
  95. Int m_borderSize; ///< Non-playable border area.
  96. VecICoord2D m_boundaries; ///< the in-game boundaries
  97. Int m_dataSize; ///< size of m_data.
  98. UnsignedByte *m_data; ///< array of z(height) values in the height map.
  99. UnsignedByte *m_cellFlipState; ///< array of bits to indicate the flip state of each cell.
  100. Int m_flipStateWidth; ///< with of the array holding cellFlipState
  101. UnsignedByte *m_cellCliffState; ///< array of bits to indicate the cliff state of each cell.
  102. /// Texture indices.
  103. Short *m_tileNdxes; ///< matches m_Data, indexes into m_SourceTiles.
  104. Short *m_blendTileNdxes; ///< matches m_Data, indexes into m_blendedTiles. 0 means no blend info.
  105. Short *m_cliffInfoNdxes; ///< matches m_Data, indexes into m_cliffInfo. 0 means no cliff info.
  106. Short *m_extraBlendTileNdxes; ///< matches m_Data, indexes into m_extraBlendedTiles. 0 means no blend info.
  107. Int m_numBitmapTiles; // Number of tiles initialized from bitmaps in m_SourceTiles.
  108. Int m_numEdgeTiles; // Number of tiles initialized from bitmaps in m_SourceTiles.
  109. Int m_numBlendedTiles; // Number of blended tiles created from bitmap tiles.
  110. TileData *m_sourceTiles[NUM_SOURCE_TILES]; ///< Tiles for m_textureClasses
  111. TileData *m_edgeTiles[NUM_SOURCE_TILES]; ///< Tiles for m_textureClasses
  112. TBlendTileInfo m_blendedTiles[NUM_BLEND_TILES];
  113. TBlendTileInfo m_extraBlendedTiles[NUM_BLEND_TILES];
  114. TCliffInfo m_cliffInfo[NUM_CLIFF_INFO];
  115. Int m_numCliffInfo; ///< Number of cliffInfo's used in m_cliffInfo.
  116. // Texture classes. There is one texture class for each bitmap read in.
  117. // A class may have more than one tile. For example, if the grass bitmap is
  118. // 128x128, it creates 4 64x64 tiles, so the grass texture class will have 4 tiles.
  119. int m_numTextureClasses;
  120. TXTextureClass m_textureClasses[NUM_TEXTURE_CLASSES];
  121. // Edge Texture classes. There is one texture class for each bitmap read in.
  122. // An edge class will normally have 4 tiles.
  123. int m_numEdgeTextureClasses;
  124. TXTextureClass m_edgeTextureClasses[NUM_TEXTURE_CLASSES];
  125. /** The actual texture used to render the 3d mesh. Note that it is
  126. basically m_SourceTiles laid out in rows, so by itself it is not useful.
  127. Use GetUVData to get the mapping info for height cells to map into the
  128. texture. */
  129. TerrainTextureClass *m_terrainTex;
  130. Int m_terrainTexHeight; /// Height of m_terrainTex allocated.
  131. /** The texture that contains the alpha edge tiles that get blended on
  132. top of the base texture. getAlphaUVData does the mapping. */
  133. AlphaTerrainTextureClass *m_alphaTerrainTex;
  134. Int m_alphaTexHeight; /// Height of m_alphaTerrainTex allocated.
  135. /** The texture that contains custom blend edge tiles. */
  136. AlphaEdgeTextureClass *m_alphaEdgeTex;
  137. Int m_alphaEdgeHeight; /// Height of m_alphaEdgeTex allocated.
  138. /// Drawing info - re the part of the map that is being drawn.
  139. Int m_drawOriginX;
  140. Int m_drawOriginY;
  141. Int m_drawWidthX;
  142. Int m_drawHeightY;
  143. protected:
  144. TileData *getSourceTile(UnsignedInt ndx) { if (ndx<NUM_SOURCE_TILES) return(m_sourceTiles[ndx]); return(NULL); };
  145. TileData *getEdgeTile(UnsignedInt ndx) { if (ndx<NUM_SOURCE_TILES) return(m_edgeTiles[ndx]); return(NULL); };
  146. static Bool readTiles(InputStream *pStrm, TileData **tiles, Int numRows);
  147. static Int countTiles(InputStream *pStrm);
  148. /// UV mapping data for a cell to map into the terrain texture.
  149. void getUVForNdx(Int ndx, float *minU, float *minV, float *maxU, float*maxV, Bool fullTile);
  150. Bool getUVForTileIndex(Int ndx, Short tileNdx, float U[4], float V[4], Bool fullTile);
  151. Int getTextureClassFromNdx(Int tileNdx);
  152. void readTexClass(TXTextureClass *texClass, TileData **tileData);
  153. Int updateTileTexturePositions(Int *edgeHeight); ///< Places each tile in the texture.
  154. void initCliffFlagsFromHeights(void);
  155. void setCellCliffFlagFromHeights(Int xIndex, Int yIndex);
  156. protected: // file reader callbacks.
  157. static Bool ParseHeightMapDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  158. Bool ParseHeightMapData(DataChunkInput &file, DataChunkInfo *info, void *userData);
  159. static Bool ParseSizeOnlyInChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  160. Bool ParseSizeOnly(DataChunkInput &file, DataChunkInfo *info, void *userData);
  161. static Bool ParseBlendTileDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  162. Bool ParseBlendTileData(DataChunkInput &file, DataChunkInfo *info, void *userData);
  163. static Bool ParseWorldDictDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  164. static Bool ParseObjectsDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  165. static Bool ParseObjectDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  166. Bool ParseObjectData(DataChunkInput &file, DataChunkInfo *info, void *userData, Bool readDict);
  167. static Bool ParseLightingDataChunk(DataChunkInput &file, DataChunkInfo *info, void *userData);
  168. protected:
  169. WorldHeightMap(void); ///< Simple constructor for WorldHeightMapEdit class.
  170. public: // constructors/destructors
  171. WorldHeightMap(ChunkInputStream *pFile, Bool bHMapOnly=false); // read from file.
  172. ~WorldHeightMap(void); // destroy.
  173. public: // Boundary info
  174. const VecICoord2D& getAllBoundaries(void) const { return m_boundaries; }
  175. public: // height map info.
  176. static Int getMinHeightValue(void) {return K_MIN_HEIGHT;}
  177. static Int getMaxHeightValue(void) {return K_MAX_HEIGHT;}
  178. UnsignedByte *getDataPtr(void) {return m_data;}
  179. Int getXExtent(void) {return m_width;} ///<number of vertices in x
  180. Int getYExtent(void) {return m_height;} ///<number of vertices in y
  181. inline Int getDrawOrgX(void) {return m_drawOriginX;}
  182. inline Int getDrawOrgY(void) {return m_drawOriginY;}
  183. inline Int getDrawWidth(void) {return m_drawWidthX;}
  184. inline Int getDrawHeight(void) {return m_drawHeightY;}
  185. inline void setDrawWidth(Int width) {m_drawWidthX = width; if (m_drawWidthX>m_width) m_drawWidthX = m_width;}
  186. inline void setDrawHeight(Int height) {m_drawHeightY = height; if (m_drawHeightY>m_height) m_drawHeightY = m_height;}
  187. inline Int getBorderSize(void) {return m_borderSize;}
  188. /// Get height with the offset that HeightMapRenderObjClass uses built in.
  189. inline UnsignedByte getDisplayHeight(Int x, Int y) { return m_data[x+m_drawOriginX+m_width*(y+m_drawOriginY)];}
  190. /// Get height in normal coordinates.
  191. inline UnsignedByte getHeight(Int xIndex, Int yIndex)
  192. {
  193. Int ndx = (yIndex*m_width)+xIndex;
  194. if ((ndx>=0) && (ndx<m_dataSize) && m_data)
  195. return(m_data[ndx]);
  196. else
  197. return(0);
  198. };
  199. void getUVForBlend(Int edgeClass, Region2D *range);
  200. Bool setDrawOrg(Int xOrg, Int yOrg);
  201. static void freeListOfMapObjects(void);
  202. Int getTextureClassNoBlend(Int xIndex, Int yIndex, Bool baseClass=false);
  203. Int getTextureClass(Int xIndex, Int yIndex, Bool baseClass=false);
  204. TXTextureClass getTextureFromIndex( Int textureIndex );
  205. public: // tile and texture info.
  206. TextureClass *getTerrainTexture(void); //< generates if needed and returns the terrain texture
  207. TextureClass *getAlphaTerrainTexture(void); //< generates if needed and returns alpha terrain texture
  208. TextureClass *getEdgeTerrainTexture(void); //< generates if needed and returns blend edge texture
  209. /// UV mapping data for a cell to map into the terrain texture. Returns true if the textures had to be stretched for cliffs.
  210. Bool getUVData(Int xIndex, Int yIndex, float U[4], float V[4], Bool fullTile);
  211. Bool getFlipState(Int xIndex, Int yIndex) const;
  212. Bool getCliffState(Int xIndex, Int yIndex) const;
  213. Bool getExtraAlphaUVData(Int xIndex, Int yIndex, float U[4], float V[4], UnsignedByte alpha[4], Bool *flip, Bool *cliff);
  214. /// UV mapping data for a cell to map into the alpha terrain texture.
  215. void getAlphaUVData(Int xIndex, Int yIndex, float U[4], float V[4], UnsignedByte alpha[4], Bool *flip, Bool fullTile);
  216. void getTerrainColorAt(Real x, Real y, RGBColor *pColor);
  217. AsciiString getTerrainNameAt(Real x, Real y);
  218. Bool isCliffMappedTexture(Int xIndex, Int yIndex);
  219. public: // modify height value
  220. void setRawHeight(Int xIndex, Int yIndex, UnsignedByte height) {
  221. Int ndx = (yIndex*m_width)+xIndex;
  222. if ((ndx>=0) && (ndx<m_dataSize) && m_data) m_data[ndx]=height;
  223. };
  224. protected:
  225. void setCliffState(Int xIndex, Int yIndex, Bool state);
  226. };
  227. #endif