Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cMapPatch.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cMapPatch_H
7 #define _INC_cMapPatch_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cMapBase.h"
13 #include "CMapChanSurface.h"
14 #include "CMapChanHeight.h"
18 
19 namespace GrayMapData
20 {
22  : public cXObjDef
23  {
30  //
31  typedef cXObjDef SUPER_t;
32  public:
33  cMapPatch(UID_t rid, cMapPatch* pParentLOD, MAP_METERI_t x, MAP_METERI_t y, MAP_LOD_TYPE eLod);
34  virtual ~cMapPatch(void);
35 
36  // Must be overloaded at higher level.
37  virtual HRESULT s_LoadStaticSection(cScriptSection* pScriptSection = nullptr) = 0; // load object or create default object.
38  virtual cMapBase* get_MapBase() const = 0;
39 
40  STDMETHOD_(XOBJTID_t, get_XObjTypeId)() const
41  {
42  // return a FOURCC code identifying the object type. ASSUME registered with cXObjMgr
43  return cFourCC('M', 'p', 'a', 't'); // RESC_Mpatch
44  }
45 
46  bool isChannelLoaded(MAP_CHANNEL_t wChannel) const
47  {
48  return((m_ChannelsLoaded & wChannel) ? true : false);
49  }
50  bool isChannelDirty(MAP_CHANNEL_t wChannel) const
51  {
52  return((m_ChannelsDirty & wChannel) ? true : false);
53  }
54  void addChannelLoaded(MAP_CHANNEL_t wChannel);
55  void addChannelDirty(MAP_CHANNEL_t wChannel);
57  {
58  m_ChannelsDirty &= ~wChannel;
59  }
60 
61  void addChannelDirtyR(MAP_CHANNEL_t wChannel);
62 
63  bool isSkirtChild() const;
64 
65  virtual HRESULT DisposeThis();
66  STDMETHOD_(cString, get_Name)() const;
67  StrLen_t GetNameFormat(char* pszName, bool bDir) const;
68  static StrLen_t GRAYCALL GetNameByCoord(char* pszName, MAP_METERI_t iCoordX, MAP_METERI_t iCoordY, MAP_METERI_t iSize, bool bDir);
69  cStringF GetFilePath(const FILECHAR_t* pszPostfix) const;
70 
71  cMapPatchPtr LoadPatchNeighbor(int dnx, int dny, MAP_CHANNEL_t uLoadChannels) const;
72 
73  // parents and children
75  {
76  // The size in meters of each height point. (MAP_PATCH_POINTS)
77  // MAP_SCALE(MAP_LOD_BLOCK)=1, MAP_SCALE(MAP_LOD_SECTOR)=8, MAP_SCALE(MAP_LOD_OCTANT)=64, Zone=
78  return MAP_SCALE(m_eLod);
79  }
81  {
84  return MAP_SIZE(m_eLod);
85  }
86 
87  // allow objects to overlap to next patch this far! else it must get kicked up to parent.
88  // PATCH_OVERLAP_MARGIN = 30 in the old code.
89 #define PATCH_OVERLAP_DIV 4
90 
92  {
94  return m_ptUR.x;
95  }
97  {
99  return m_ptUR.y;
100  }
101 
102  void GetURPoint(cPoint3f& ptw) const
103  {
104  // Get upper right point in world meters.
105  ptw.m_x = (D3DVALUE)m_ptUR.x;
106  ptw.m_y = (D3DVALUE)m_ptUR.y;
107  ptw.m_z = 0;
108  }
109  void GetCenterPoint(cPoint3f& ptw) const
110  {
111  MAP_METERI_t iSize2 = get_SizeXY() / 2;
112  ptw.m_x = (D3DVALUE)m_ptUR.x + iSize2;
113  ptw.m_y = (D3DVALUE)m_ptUR.y + iSize2;
114  ptw.m_z = 0;
115  }
116  void GetURPPoint(cPoint2i& pt) const
117  {
118  // Get Upper right X in patch coords.
119  // Get Upper right Y in patch coords.
120  const MAP_METERI_t size = get_SizeXY();
121  pt.x = m_ptUR.x / size;
122  pt.y = m_ptUR.y / size;
123  }
124  void GetURPoint(cVector3f& vPt) const
125  {
126  // Get the upper right point (origin) in D3D space.
127  vPt.x = (float)(m_ptUR.x) - g_Coords.m_d3dOffset.x;
128  vPt.y = 0; // height is not relevant.
129  vPt.z = (float)(m_ptUR.y) - g_Coords.m_d3dOffset.z;
130  }
131  void GetCenterPoint(cVector3f& vPt) const
132  {
133  // Get world meters.
134  GetURPoint(vPt);
135  MAP_METERI_t iSize2 = get_SizeXY() / 2;
136  vPt.x += iSize2;
137  vPt.z += iSize2;
138  }
139 
141  {
143  x -= get_URX();
144  y -= get_URY();
145  const MAP_METERI_t size = get_SizeXY();
146  if (IS_INDEX_BAD(x, size) || IS_INDEX_BAD(y, size))
147  return false;
148  return true;
149  }
150  bool isPointInPatch(const cVector3f& vPt) const;
151 
152  void GetPatchRect(cRectI& rect) const
153  {
154  // Get rectangle in meters
155  const MAP_METERI_t size = get_SizeXY();
156  rect.SetRectWH(m_ptUR.x, m_ptUR.y, size, size);
157  }
158  void GetPatchRect(cRectf& rect) const
159  {
160  const MAP_METERI_t size = get_SizeXY();
161  rect.SetRectWH((float)m_ptUR.x, (float)m_ptUR.y, (float)size, (float)size);
162  }
163 
164  void CvtPatchToWorld(const cVector3f& vIn, cPoint3f& ptWOut) const;
165 
167  {
168  return m_pParentPatch;
169  }
170  cMapPatchPtr GetChildPatch(int rx8, int ry8, MAP_CHANNEL_t uLoadFlags) const;
171 
172  // Height Map
174  {
177  ASSERT(isChannelLoaded(MAP_CHANNEL_Height));
178  //ASSERT(m_Height.m_pImage==nullptr);
179  return &m_Height.m_Grid[(x)+((y)*(MAP_PATCH_SIZE1))];
180  }
182  {
183  // get raw height at a point.
184  // ARGS: x,y = coords offset in the patch
187  ASSERT(isChannelLoaded(MAP_CHANNEL_Height));
188  //ASSERT(m_Height.m_pImage==nullptr);
189  return m_Height.m_Grid[(x)+((y)*(MAP_PATCH_SIZE1))];
190  }
192  {
193  ASSERT(isChannelLoaded(MAP_CHANNEL_Height));
194  //ASSERT(m_Height.m_pImage==nullptr);
197  if (m_Height.m_Grid[(x)+((y)*(MAP_PATCH_SIZE1))] == h)
198  return false;
199  m_Height.m_Grid[(x)+((y)*(MAP_PATCH_SIZE1))] = h;
200  return true;
201  }
202 
203  MAP_METERf_t GetHX(float x, float y);
204  bool SetHX(float x, float y, MAP_METERf_t fHeight);
205  MAP_METERf_t GetHeightX(float x, float y);
206  bool SetHeightX(float x, float y, MAP_METERf_t fHeight);
207 
208  float GetNormalP(float x, float y, cVector3f& vNorm);
209 
210  GINTERSECT_TYPE GetHeightRayContactSegment(const cPoint3f& ptW1, const cVector3f& vDir, cVector3f& vPos1, cVector3f& vPos2, float fMaxRange);
211  bool GetHeightRayContact2(cVector3f& vContact, const cVector3f& vPos1, const cVector3f& vPos2);
212 
214  {
215  return m_fWaterLevel;
216  }
217  virtual void put_WaterLevel(MAP_METERf_t fWaterLevel);
218  MAP_METERf_t GetWaterLevelX(float x, float y) const;
219 
220  static MAP_TERRAIN_TYPE GRAYCALL GetTerrainType(MAP_METERf_t z, MAP_METERf_t fGroundLevel, MAP_METERf_t fWaterLevel, MAP_METERf_t fCharHeight);
221  MAP_TERRAIN_TYPE GetTerrainType(const cPoint3f& pt, MAP_METERf_t fCharHeight, cVector3f* pvNorm,
222  MAP_METERf_t* pfGroundLevel = nullptr, MAP_METERf_t* pfWaterLevel = nullptr);
223 
224  // objects in the patch
225  HRESULT s_Invoke_Save(const cVariant& vArgs, cVariant& vValRet, cScriptContext* pContext);
226  HRESULT s_Invoke_Reload(const cVariant& vArgs, cVariant& vValRet, cScriptContext* pContext);
227  HRESULT s_Invoke_HeightUpdateSector(const cVariant& vArgs, cVariant& vValRet, cScriptContext* pContext);
228  HRESULT s_Invoke_HeightChange(const cVariant& vArgs, cVariant& vValRet, cScriptContext* pContext);
229  HRESULT s_Invoke_HeightUpdateSkirt(const cVariant& vArgs, cVariant& vValRet, cScriptContext* pContext);
230 
231  // Scripting the patch
232  STDMETHOD(s_PropPut)(const cScriptableProp* pProp, const cVariant& vVal);
233  STDMETHOD(s_PropGet)(const cScriptableProp* pProp, OUT cVariant& vValRet);
234  STDMETHOD(s_Invoke)(const cScriptableMethod* pMethod, const cVariant& vArgs, cVariant& vValRet);
235 
236  virtual HRESULT LoadXObject();
237  virtual void UnloadXObject();
238  virtual HRESULT ReloadXObject();
239  virtual HRESULT SaveXObject();
240 
241  // this does the actual loading/saving of the needed channels only
242  // only new channels here now, old can be moved here later
243  virtual HRESULT LoadChannels(MAP_CHANNEL_t uLoadFlags);
244  virtual void UnloadChannels(MAP_CHANNEL_t uLoadFlags);
245  virtual HRESULT WriteChannels(MAP_CHANNEL_t uWriteFlags, bool bCheckParent = true);
246 
247  // Heights in the patch.
248  static MAP_METERf_t HeightRandomFunc(const float x, const float y);
249 
250  HRESULT HeightsImageWrite(bool bCheckParent = true);
251  bool HeightsImageLoad();
252  bool HeightsImageLock(bool bWrite);
253  void HeightsImageUnlock();
254 
255  void HeightsAddRandom(float fScaler);
256  void HeightsGenerateRandom();
257  bool HeightsUpdateFromParent(bool bDoSkirt, bool bUpdate);
258  bool HeightsUpdateFromChildren(bool bDoSkirt, bool bUpdate);
259  bool HeightsUpdateChildren(bool bDoSkirt, bool bUpdate);
260  bool HeightsUpdateParent(bool bDoSkirt, bool bUpdate);
261 
262  bool HeightsMakeSectorFromBlocks(bool bDoSkirt = true, bool bUpdate = false);
263  bool HeightsMakeBlockFromSector(bool bDoSkirt = true, bool bUpdate = false);
264 
265  bool HeightsSkirtUpdate(bool bAllOrNothing, bool bChildren);
266  void HeightsSkirtCheck(bool bChanges);
267  void HeightsOffset(float fOffset, bool bUpdate);
268 
269  bool NormalMap_Build(D3DCOLOR normalMap[MAP_PATCH_SIZE1][MAP_PATCH_SIZE1]); // builds the normal map
270 
271  STDMETHOD(s_WriteProps)(IScriptableSetter& reg);
272  bool s_LoadPatch();
273 
275  {
276  return S_OK;
277  }
278 
280  // Surface Map
281 
282  HRESULT SurfaceMapLoad();
283  HRESULT SurfaceMapWrite();
284  size_t SurfaceAllocateMap(bool bInit); // returns linear size
285 
287  {
288  // get raw surface map id at a point.
289  // ARGS: x,y = coords offset in the patch
290  return m_Surface.GetSurf(px, py);
291  }
292 
293  bool SurfacePtNoUpdate(int px, int py, MAP_SURFACE_ID_t eSurfaceID);
294  bool SetSurfacePtInt(int px, int py, MAP_SURFACE_ID_t eSurfaceID);
295 
297  {
298  ASSERT(m_eLod == MAP_LOD_SECTOR);
299  x -= m_ptUR.x;
300  y -= m_ptUR.y;
303  return GetSurfacePtInt(x, y);
304  }
305 
307  {
308  ASSERT(m_eLod == MAP_LOD_SECTOR);
309  x -= m_ptUR.x;
310  y -= m_ptUR.y;
313  return SetSurfacePtInt(x, y, eSurfaceID);
314  }
315 
316  // coords conversion
317  template<class _TYPE> inline void WorldToPatch(_TYPE& x, _TYPE& y)
318  {
321  x -= m_ptUR.x;
322  y -= m_ptUR.y;
323  MAP_METERI_t iScale = get_ScaleXY();
324  if (iScale > 1)
325  {
326  x /= iScale;
327  y /= iScale;
328  }
329  }
330 
331  private:
332  HRESULT HeightsImageLoadJpg();
333  HRESULT HeightsImageWriteJpg();
334  HRESULT HeightsImageUpdateJpg();
335 
336  public:
339  enum P_TYPE_
340  {
341 #define cMapPatchProp(a,b,c,d,e) P_##a,
342 #include "cMapPatchProps.tbl"
343 #undef cMapPatchProp
345  };
346  static cScriptableProp sm_Props[P_QTY + 1];
347  enum M_TYPE_
348  {
349 #define cMapPatchMethod(a,b,c) M_##a,
350 #include "cMapPatchMethods.tbl"
351 #undef cMapPatchMethod
353  };
354  static cScriptableMethod sm_Methods[M_QTY + 1];
355 
356  int m_iChildPatchesLoaded; // how many of my children have been loaded into the cXObjMgr. <= 8x8 = MAP_LOD_RATIO^2
357  MAP_CHANNEL_t m_ChannelsToLoad; // What is requested to be loaded.
358  MAP_CHANNEL_t m_ChannelsLoaded; // What types of data are loaded for the patch now.
359  MAP_CHANNEL_t m_ChannelsDirty; // What types of data need to saved back to disk.
360 
361  MAP_CHANNEL_t m_ChannelsDerived; // Channels derived from random data or from parent or child. NOT saved.
362 
365 
367 
368  protected:
369  MAP_METERf_t m_fWaterLevel; // Default water level for the whole patch. MAP_HEIGHT_SEA. may also have 3d water objects above this.
370 
371  private:
372  cMapPatchPtr m_pParentPatch;
373  const cPoint2i m_ptUR;
374  const MAP_LOD_TYPE m_eLod;
375  };
376 };
377 
378 #endif
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYMAPDATA_LINK
Definition: GrayMapData.h:13
#define IS_INDEX_GOOD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:35
#define IS_INDEX_BAD(i, q)
cast the (likely) int to unsigned to check for negatives.
Definition: Index.h:34
float D3DVALUE
Definition: MathDX.h:18
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
DWORD D3DCOLOR
Stuff normally defined in windows.h or DirectX headers.
Definition: cColorRef.h:24
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define MAP_SCALE(lod)
integer Size of a patch's MAP_PATCH_POINTS (in meters (MAP_METERI_t))
Definition: cMapBase.h:45
#define MAP_SIZE(lod)
size of a patch (in meters (MAP_METERI_t))
Definition: cMapBase.h:46
#define MAP_PATCH_SIZE1
Definition: cMapChanHeight.h:40
cVector3f m_d3dOffset
offset for convert to d3d device.
Definition: cCoordSpace.h:28
Definition: cFourCC.h:96
Definition: cPoint2.h:20
Definition: cRectT.h:552
Definition: cRectI.h:22
Definition: cScriptContext.h:24
Definition: cScriptSection.h:25
Definition: cScriptableInterface.h:97
Definition: cVariant.h:26
TYPE x
Definition: cVecT.h:490
TYPE y
Definition: cVecT.h:490
TYPE m_z
Definition: cVecT.h:544
TYPE y
Definition: cVecT.h:545
TYPE z
Definition: cVecT.h:545
TYPE m_x
Definition: cVecT.h:544
TYPE m_y
Definition: cVecT.h:544
TYPE x
Definition: cVecT.h:545
Definition: cVector.h:94
Definition: cXObjDef.h:17
P_TYPE_
Definition: cXObjDef.h:30
M_TYPE_
Definition: cXObjDef.h:38
Definition: cXObject.h:78
Definition: cMapChanHeight.h:25
Definition: cMapChanSurface.h:17
Definition: cMapBase.h:188
Definition: cMapPatch.h:23
MAP_SURFACE_ID_t GetSurfacePt(MAP_METERI_t x, MAP_METERI_t y)
Definition: cMapPatch.h:296
void GetCenterPoint(cVector3f &vPt) const
Definition: cMapPatch.h:131
void WorldToPatch(_TYPE &x, _TYPE &y)
Definition: cMapPatch.h:317
bool isPointInPatch(MAP_METERI_t x, MAP_METERI_t y) const
Definition: cMapPatch.h:140
MAP_METERI_t get_SizeXY() const
Definition: cMapPatch.h:80
bool SetSurfacePt(MAP_METERI_t x, MAP_METERI_t y, MAP_SURFACE_ID_t eSurfaceID)
Definition: cMapPatch.h:306
MAP_CHANNEL_t m_ChannelsLoaded
Definition: cMapPatch.h:358
MAP_CHANNEL_t m_ChannelsDerived
Definition: cMapPatch.h:361
STDMETHOD_(cString, get_Name)() const
void clearChannelDirty(MAP_CHANNEL_t wChannel)
Definition: cMapPatch.h:56
MAP_HEIGHT_t GetH(MAP_PATCH_POINT_t x, MAP_PATCH_POINT_t y) const
Definition: cMapPatch.h:181
void GetURPoint(cVector3f &vPt) const
Definition: cMapPatch.h:124
void GetURPoint(cPoint3f &ptw) const
Definition: cMapPatch.h:102
MAP_SURFACE_ID_t GetSurfacePtInt(MAP_METERI_t px, MAP_METERI_t py)
Definition: cMapPatch.h:286
MAP_METERI_t get_ScaleXY() const
Definition: cMapPatch.h:74
CHEAPOBJECT_IMPL
Definition: cMapPatch.h:337
CMapChanHeight m_Height
MAP_CHANNEL_Height.
Definition: cMapPatch.h:363
cMapPatchPtr get_ParentPatch() const
Definition: cMapPatch.h:166
void GetPatchRect(cRectf &rect) const
Definition: cMapPatch.h:158
virtual HRESULT s_LoadStaticSection(cScriptSection *pScriptSection=nullptr)=0
MAP_HEIGHT_t * GetHPtr(MAP_PATCH_POINT_t x, MAP_PATCH_POINT_t y)
Definition: cMapPatch.h:173
@ M_QTY
Definition: cMapPatch.h:352
MAP_METERI_t get_URX() const
Definition: cMapPatch.h:91
@ P_QTY
Definition: cMapPatch.h:344
CSCRIPT_CLASS_DEF1(MapPatch, cXObjDef)
int m_iChildPatchesLoaded
Definition: cMapPatch.h:356
bool isChannelDirty(MAP_CHANNEL_t wChannel) const
Definition: cMapPatch.h:50
MAP_METERf_t get_WaterLevel() const
Definition: cMapPatch.h:213
MAP_METERf_t m_fWaterLevel
Definition: cMapPatch.h:369
bool isChannelLoaded(MAP_CHANNEL_t wChannel) const
Definition: cMapPatch.h:46
MAP_CHANNEL_t m_ChannelsDirty
Definition: cMapPatch.h:359
void GetURPPoint(cPoint2i &pt) const
Definition: cMapPatch.h:116
MAP_KEY_TYPE m_dwKeyCode
Set the map crypt key code for the octant given us by the server.
Definition: cMapPatch.h:366
void GetCenterPoint(cPoint3f &ptw) const
Definition: cMapPatch.h:109
bool SetH(MAP_PATCH_POINT_t x, MAP_PATCH_POINT_t y, MAP_HEIGHT_t h)
Definition: cMapPatch.h:191
virtual HRESULT UpdateChanges()
Definition: cMapPatch.h:274
virtual cMapBase * get_MapBase() const =0
CMapChanSurface m_Surface
MAP_CHANNEL_Surface.
Definition: cMapPatch.h:364
STDMETHOD_(XOBJTID_t, get_XObjTypeId)() const
Definition: cMapPatch.h:40
MAP_METERI_t get_URY() const
Definition: cMapPatch.h:96
void GetPatchRect(cRectI &rect) const
Definition: cMapPatch.h:152
MAP_CHANNEL_t m_ChannelsToLoad
Definition: cMapPatch.h:357
Definition: IScriptableObj.h:62
FOURCC XOBJTID_t
a unique FOURCC id for a type of XObject, 0=k_XOBJTID_NULL, MAKEFOURCC(a,b,c,d). Equiv to GUID for IU...
Definition: IXObject.h:42
interface const RECTQ_t & rect
Definition: cQuadtree.h:44
HASHCODE32_t UID_t
Sort of the same as a HASHCODE_t. not the same as UIDINDEX_t.
Definition: cUID.h:18
GINTERSECT_TYPE
Definition: cVecT.h:20
Definition: GrayMapData.cpp:12
WORD MAP_CHANNEL_t
Definition: cMapBase.h:103
int MAP_PATCH_POINT_t
this gives the number of samples in the heightmap file: 128 means a 129x129 file (include skirt pixel...
Definition: cMapBase.h:42
BYTE MAP_SURFACE_ID_t
Definition: cMapBase.h:114
@ MAP_CHANNEL_Height
the height map. MAP_HEIGHT_t
Definition: cMapBase.h:82
MAP_TERRAIN_TYPE
Definition: cMapBase.h:127
__DECL_IMPORT cCoordSpaceDX g_Coords
unsigned short MAP_HEIGHT_t
Definition: cMapBase.h:108
MAP_LOD_TYPE
Definition: cMapBase.h:27
@ MAP_LOD_SECTOR
Definition: cMapBase.h:33
UINT32 MAP_KEY_TYPE
Definition: cMapKeys.h:19
int MAP_METERI_t
integer meters for the location of a patch. MAP_METER_t
Definition: cMapKeys.h:16
float MAP_METERf_t
Height meters dont need double accuracy. MAP_METER_t.
Definition: cMapKeys.h:17
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
char FILECHAR_t
a UTF8 char in a file name. like TCHAR
Definition: FileName.h:22