cm_landscape.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #if !defined(CM_LANDSCAPE_H_INC)
  2. #define CM_LANDSCAPE_H_INC
  3. #pragma warning (push, 3) //go back down to 3 for the stl include
  4. #include <list>
  5. #pragma warning (pop)
  6. using namespace std;
  7. // These are the root classes using data shared in both the server and the renderer.
  8. // This common data is also available to physics
  9. #define HEIGHT_RESOLUTION 256
  10. // Trying to make a guess at the optimal step through the patches
  11. // This is the average of 1 side and the diagonal presuming a square patch
  12. #define TERRAIN_STEP_MAGIC (1.0f / 1.2071f)
  13. #define MIN_TERXELS 2
  14. #define MAX_TERXELS 8
  15. // Defined as 1 << (sqrt(MAX_TERXELS) + 1)
  16. #define MAX_VARIANCE_SIZE 16
  17. // Maximum number of instances to pick from an instance file
  18. #define MAX_INSTANCE_TYPES 16
  19. // Types of areas
  20. typedef enum
  21. {
  22. AT_NONE,
  23. AT_FLAT,
  24. AT_BSP,
  25. AT_NPC,
  26. AT_GROUP,
  27. AT_RIVER,
  28. AT_OBJECTIVE,
  29. AT_PLAYER,
  30. } areaType_t;
  31. class CArea
  32. {
  33. private:
  34. vec3_t mPosition;
  35. float mRadius;
  36. float mAngle;
  37. float mAngleDiff;
  38. int mType;
  39. int mVillageID;
  40. public:
  41. CArea(void) {}
  42. ~CArea(void) {}
  43. void Init(vec3_t pos, float radius, float angle = 0.0f, int type = AT_NONE, float angleDiff = 0.0f, int villageID = 0)
  44. {
  45. VectorCopy(pos, mPosition);
  46. mRadius = radius;
  47. mAngle = angle;
  48. mAngleDiff = angleDiff;
  49. mType = type;
  50. mVillageID = villageID;
  51. }
  52. float GetRadius(void) const { return(mRadius); }
  53. float GetAngle(void) const { return(mAngle); }
  54. float GetAngleDiff(void) const { return(mAngleDiff); }
  55. vec3_t &GetPosition(void) { return(mPosition); }
  56. int GetType(void) const { return(mType); }
  57. int GetVillageID(void) const { return(mVillageID); }
  58. };
  59. typedef list<CArea*> areaList_t;
  60. typedef list<CArea*>::iterator areaIter_t;
  61. class CCMHeightDetails
  62. {
  63. private:
  64. int mContents;
  65. int mSurfaceFlags;
  66. public:
  67. CCMHeightDetails(void) {}
  68. ~CCMHeightDetails(void) {}
  69. // Accessors
  70. const int GetSurfaceFlags(void) const { return(mSurfaceFlags); }
  71. const int GetContents(void) const { return(mContents); }
  72. void SetFlags(const int con, const int sf) { mContents = con; mSurfaceFlags = sf; }
  73. };
  74. class CCMPatch
  75. {
  76. protected:
  77. class CCMLandScape *owner; // Owning landscape
  78. int mHx, mHy; // Terxel coords of patch
  79. byte *mHeightMap; // Pointer to height map to use
  80. byte mCornerHeights[4]; // Heights at the corners of the patch
  81. vec3_t mWorldCoords; // World coordinate offset of this patch.
  82. vec3pair_t mBounds; // mins and maxs of the patch for culling
  83. int mNumBrushes; // number of brushes to collide with in the patch
  84. struct cbrush_s *mPatchBrushData; // List of brushes that make up the patch
  85. int mSurfaceFlags; // surfaceflag of the heightshader
  86. int mContentFlags; // contents of the heightshader
  87. public:
  88. // Constructors
  89. CCMPatch(void) {}
  90. ~CCMPatch(void);
  91. // Accessors
  92. const vec3_t &GetWorld(void) const { return(mWorldCoords); }
  93. const vec3_t &GetMins(void) const { return(mBounds[0]); }
  94. const vec3_t &GetMaxs(void) const { return(mBounds[1]); }
  95. const vec3pair_t &GetBounds(void) const { return(mBounds); }
  96. const int GetHeightMapX(void) const { return(mHx); }
  97. const int GetHeightMapY(void) const { return(mHy); }
  98. const int GetHeight(int corner) const { return(mCornerHeights[corner]); }
  99. const int GetNumBrushes(void) const { return(mNumBrushes); }
  100. struct cbrush_s *GetCollisionData(void) const { return(mPatchBrushData); }
  101. void SetSurfaceFlags(const int in) { mSurfaceFlags = in; }
  102. const int GetSurfaceFlags(void) const { return(mSurfaceFlags); }
  103. void SetContents(const int in) { mContentFlags = in; }
  104. const int GetContents(void) const { return(mContentFlags); }
  105. // Prototypes
  106. void Init(CCMLandScape *ls, int heightX, int heightY, vec3_t world, byte *hMap, byte *patchBrushData);
  107. void InitPlane(struct cbrushside_s *side, cplane_t *plane, vec3_t p0, vec3_t p1, vec3_t p2);
  108. void CreatePatchPlaneData(void);
  109. void* GetAdjacentBrushX ( int x, int y );
  110. void* GetAdjacentBrushY ( int x, int y );
  111. };
  112. class CRandomTerrain;
  113. class CCMLandScape
  114. {
  115. private:
  116. int mRefCount; // Number of times this class is referenced
  117. thandle_t mTerrainHandle;
  118. byte *mHeightMap; // Pointer to byte array of height samples
  119. byte *mFlattenMap; // Pointer to byte array of flatten samples
  120. int mWidth, mHeight; // Width and height of heightMap excluding the 1 pixel edge
  121. int mTerxels; // Number of terxels per patch side
  122. vec3_t mTerxelSize; // Vector to scale heightMap samples to real world coords
  123. vec3pair_t mBounds; // Real world bounds of terrain brush
  124. vec3_t mSize; // Size of terrain brush in real world coords excluding 1 patch edge
  125. vec3_t mPatchSize; // Size of each patch in the x and y directions only
  126. float mPatchScalarSize; // Horizontal size of the patch
  127. int mBlockWidth, mBlockHeight; // Width and height of heightfield on blocks
  128. CCMPatch *mPatches;
  129. byte *mPatchBrushData; // Base memory from which the patch brush data is taken
  130. bool mHasPhysics; // Set to true unless disabled
  131. CRandomTerrain *mRandomTerrain;
  132. int mBaseWaterHeight; // Base water height in terxels
  133. float mWaterHeight; // Real world height of the water
  134. int mWaterContents; // Contents of the water shader
  135. int mWaterSurfaceFlags; // Surface flags of the water shader
  136. unsigned long holdrand;
  137. list<CArea *> mAreas; // List of flattened areas on this landscape
  138. list<CArea *>::iterator mAreasIt;
  139. CCMHeightDetails mHeightDetails[HEIGHT_RESOLUTION]; // Surfaceflags per height
  140. vec3_t *mCoords; // Temp storage for real world coords
  141. public:
  142. CCMLandScape(const char *configstring, bool server);
  143. ~CCMLandScape(void);
  144. CCMPatch *GetPatch(int x, int y);
  145. // Prototypes
  146. void PatchCollide(struct traceWork_s *tw, trace_t &trace, const vec3_t start, const vec3_t end, int checkcount);
  147. void TerrainPatchIterate(void (*IterateFunc)( CCMPatch *, void * ), void *userdata) const;
  148. float GetWorldHeight(vec3_t origin, const vec3pair_t bounds, bool aboveGround) const;
  149. float WaterCollide(const vec3_t begin, const vec3_t end, float fraction) const;
  150. void UpdatePatches(void);
  151. void GetTerxelLocalCoords ( int x, int y, vec3_t coords[8] );
  152. void LoadTerrainDef(const char *td);
  153. void SetShaders(int height, class CCMShader *shader);
  154. void FlattenArea(CArea *area, int height, bool save, bool forceHeight, bool smooth);
  155. void CarveLine ( vec3_t start, vec3_t end, int depth, int width );
  156. void CarveBezierCurve ( int numCtlPoints, vec3_t* ctlPoints, int steps, int depth, int size );
  157. void SaveArea(CArea *area);
  158. float FractionBelowLevel(CArea *area, int height);
  159. bool AreaCollision(CArea *area, int *areaTypes, int areaTypeCount);
  160. CArea *GetFirstArea(void);
  161. CArea *GetFirstObjectiveArea(void);
  162. CArea *GetPlayerArea(void);
  163. CArea *GetNextArea(void);
  164. CArea *GetNextObjectiveArea(void);
  165. // Accessors
  166. const int GetRefCount(void) const { return(mRefCount); }
  167. void IncreaseRefCount(void) { mRefCount++; }
  168. void DecreaseRefCount(void) { mRefCount--; }
  169. const vec3pair_t &GetBounds(void) const { return(mBounds); }
  170. const vec3_t &GetMins(void) const { return(mBounds[0]); }
  171. const vec3_t &GetMaxs(void) const { return(mBounds[1]); }
  172. const vec3_t &GetSize(void) const { return(mSize); }
  173. const vec3_t &GetTerxelSize(void) const { return(mTerxelSize); }
  174. const vec3_t &GetPatchSize(void) const { return(mPatchSize); }
  175. const float GetPatchWidth(void) const { return(mPatchSize[0]); }
  176. const float GetPatchHeight(void) const { return(mPatchSize[1]); }
  177. const float GetPatchScalarSize(void) const { return(mPatchScalarSize); }
  178. const int GetTerxels(void) const { return(mTerxels); }
  179. const int GetRealWidth(void) const { return(mWidth + 1); }
  180. const int GetRealHeight(void) const { return(mHeight + 1); }
  181. const int GetRealArea(void) const { return((mWidth + 1) * (mHeight + 1)); }
  182. const int GetWidth(void) const { return(mWidth); }
  183. const int GetHeight(void) const { return(mHeight); }
  184. const int GetArea(void) const { return(mWidth * mHeight); }
  185. const int GetBlockWidth(void) const { return(mBlockWidth); }
  186. const int GetBlockHeight(void) const { return(mBlockHeight); }
  187. const int GetBlockCount(void) const { return(mBlockWidth * mBlockHeight); }
  188. byte *GetHeightMap(void) const { return(mHeightMap); }
  189. byte *GetFlattenMap(void) const { return(mFlattenMap); }
  190. const thandle_t GetTerrainId(void) const { return(mTerrainHandle); }
  191. void SetTerrainId(const thandle_t terrainId) { mTerrainHandle = terrainId; }
  192. const float CalcWorldHeight(int height) const { return((height * mTerxelSize[2]) + mBounds[0][2]); }
  193. const bool GetHasPhysics(void) const { return(mHasPhysics); }
  194. const bool GetIsRandom(void) const { return(mRandomTerrain != 0); }
  195. const int GetSurfaceFlags(int height) const { return(mHeightDetails[height].GetSurfaceFlags()); }
  196. const int GetContentFlags(int height) const { return(mHeightDetails[height].GetContents()); }
  197. void CalcRealCoords(void);
  198. vec3_t *GetCoords(void) const { return(mCoords); }
  199. int GetBaseWaterHeight(void) const { return(mBaseWaterHeight); }
  200. void SetRealWaterHeight(int height) { mWaterHeight = height * mTerxelSize[2]; }
  201. float GetWaterHeight(void) const { return(mWaterHeight); }
  202. int GetWaterContents(void) const { return(mWaterContents); }
  203. int GetWaterSurfaceFlags(void) const { return(mWaterSurfaceFlags); }
  204. CRandomTerrain *GetRandomTerrain(void) { return mRandomTerrain; }
  205. void rand_seed(int seed);
  206. unsigned long get_rand_seed(void) { return holdrand; }
  207. float flrand(float min, float max);
  208. int irand(int min, int max);
  209. };
  210. void CM_TerrainPatchIterate(const class CCMLandScape *ls, void (*IterateFunc)( CCMPatch *, void * ), void *userdata);
  211. class CCMLandScape *CM_InitTerrain(const char *configstring, thandle_t terrainId, bool server);
  212. float CM_GetWorldHeight(const CCMLandScape *landscape, vec3_t origin, const vec3pair_t bounds, bool aboveGround);
  213. void CM_FlattenArea(CCMLandScape *landscape, CArea *area, int height, bool save, bool forceHeight, bool smooth);
  214. void CM_CarveBezierCurve (CCMLandScape *landscape, int numCtls, vec3_t* ctls, int steps, int depth, int size );
  215. void CM_SaveArea(CCMLandScape *landscape, CArea *area);
  216. float CM_FractionBelowLevel(CCMLandScape *landscape, CArea *area, int height);
  217. bool CM_AreaCollision(class CCMLandScape *landscape, class CArea *area, int *areaTypes, int areaTypeCount);
  218. CArea *CM_GetFirstArea(CCMLandScape *landscape);
  219. CArea *CM_GetFirstObjectiveArea(CCMLandScape *landscape);
  220. CArea *CM_GetPlayerArea(class CCMLandScape *common);
  221. CArea *CM_GetNextArea(CCMLandScape *landscape);
  222. CArea *CM_GetNextObjectiveArea(CCMLandScape *landscape);
  223. void CM_CircularIterate(byte *data, int width, int height, int xo, int yo, int insideRadius, int outsideRadius, int *user, void (*callback)(byte *, float, int *));
  224. CRandomTerrain *CreateRandomTerrain(const char *config, CCMLandScape *landscape, byte *heightmap, int width, int height);
  225. void SV_LoadMissionDef(const char *configstring, class CCMLandScape *landscape);
  226. void CL_CreateRandomTerrain(const char *config, class CCMLandScape *landscape, byte *image, int width, int height);
  227. void CL_LoadInstanceDef(const char *configstring, class CCMLandScape *landscape);
  228. void CL_LoadMissionDef(const char *configstring, class CCMLandScape *landscape);
  229. extern cvar_t *com_terrainPhysics;
  230. #endif
  231. // end