Heightmap.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:19k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // Heightmap.cpp: implementation of the CHeightmap class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Heightmap.h"
  6. #include "stdio.h"
  7. #include "GameSetting.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. MOVE_VERTEX    * CHeightmap::m_pMovemap=NULL;
  12. unsigned char  * CHeightmap::m_pTmap=NULL;
  13. CFrustumCull     CHeightmap::m_cFrustumCull=CFrustumCull();
  14. unsigned char  * CHeightmap::m_pCovermap=NULL;
  15. GRID_BIAS      * CHeightmap::m_pCoverBias=NULL;
  16. VERTEX           CHeightmap::m_ViewPos=VERTEX(0,0,0);
  17. float            CHeightmap::m_ViewRotX=0;
  18. float            CHeightmap::m_ViewRotY=0;
  19. int              CHeightmap::m_numUser=0;
  20. int              CHeightmap::m_ViewOffsetX=0;
  21. int              CHeightmap::m_ViewOffsetZ=0;
  22. //////////////focus
  23. double           CHeightmap::m_focusPosX=0;
  24. double           CHeightmap::m_focusPosY=0;
  25. double           CHeightmap::m_focusPosZ=0;
  26. double           CHeightmap::m_distFromFocus=0;
  27. int              CHeightmap::m_focusState=FOCUS_GROUND;
  28. bool             CHeightmap::m_bAttack=false;
  29. float            CHeightmap::m_myHealth=1;
  30. //////////building
  31. int              CHeightmap::m_numBuilding=0;
  32. VERTEX          *CHeightmap::m_buildingPos=NULL;
  33. BOUNDARY_3D    **CHeightmap::m_bigBndyBuilding=NULL;
  34. BOUNDARY_3D    **CHeightmap::m_smallBndyBuilding=NULL;
  35. /////////////sprite
  36. int              CHeightmap::m_numSoldier=0;
  37. VERTEX          *CHeightmap::m_soldierPos=NULL;
  38. float           *CHeightmap::m_soldierRot=NULL;
  39. float           *CHeightmap::m_soldierHealth=NULL;
  40. int              CHeightmap::m_numTriangles=0;
  41. CHeightmap::CHeightmap()
  42. {
  43. if(m_numUser==0)
  44. {
  45. /////////read terrain map
  46.      if(!ReadTerrainmapFile())
  47. {
  48.     MessageBox(0, "read heightmap failed", "Error", MB_OK | MB_ICONERROR);
  49.     return ;
  50. }
  51.      if(!ReadPlantmapFile())
  52. {
  53.     MessageBox(0, "read plantmap failed", "Error", MB_OK | MB_ICONERROR);
  54.     return ;
  55. }
  56. GetMissionSetting();
  57. m_pMovemap =new MOVE_VERTEX [256*256];
  58. }
  59. m_numUser++;
  60. }
  61. CHeightmap::~CHeightmap()
  62. {
  63. m_numUser--;
  64. if(m_numUser==0)
  65. {
  66. if(m_pTmap!=NULL)
  67. {
  68.     delete [] m_pTmap;
  69.      m_pTmap=NULL;
  70. }
  71. if(m_pCovermap!=NULL)
  72. {
  73. delete [] m_pCovermap;
  74.      m_pCovermap=NULL;
  75. }
  76. if(m_pCoverBias!=NULL)
  77. {
  78. delete [] m_pCoverBias;
  79.             m_pCoverBias=NULL;
  80. }
  81. if(m_pMovemap!=NULL)
  82. {
  83. delete [] m_pMovemap;
  84.      m_pMovemap=NULL;
  85. }
  86. if(m_bigBndyBuilding!=NULL)
  87. {
  88. ///// only release pointers
  89. delete [] m_bigBndyBuilding;
  90.      m_bigBndyBuilding=NULL;
  91. }
  92. if(m_smallBndyBuilding!=NULL)
  93. {
  94. ///// only release pointers
  95. delete [] m_smallBndyBuilding;
  96.      m_smallBndyBuilding=NULL;
  97. }
  98. if(m_buildingPos!=NULL)
  99. {
  100. ///// only release pointers
  101. delete [] m_buildingPos;
  102.      m_buildingPos=NULL;
  103. }
  104. if(m_soldierPos!=NULL)
  105. {
  106. ///// only release pointers
  107. delete [] m_soldierPos;
  108.      m_soldierPos=NULL;
  109. }
  110. if(m_soldierRot!=NULL)
  111. {
  112. ///// only release pointers
  113. delete [] m_soldierRot;
  114.      m_soldierRot=NULL;
  115. }
  116. if(m_soldierHealth!=NULL)
  117. {
  118. ///// only release pointers
  119. delete [] m_soldierHealth;
  120.      m_soldierHealth=NULL;
  121. }
  122. }
  123. }
  124. bool CHeightmap::GetMissionSetting()
  125. {
  126. char missionName[64];
  127. char sINIPath[MAX_PATH];
  128. wsprintf(missionName,"mission/mission%d.ini",CGameSetting::m_iMissionSelect);
  129. MakeModulePath(missionName, sINIPath);
  130. m_numBuilding = GetPrivateProfileInt("Building", "HouseNumber", 1, sINIPath);
  131.     m_buildingPos = new VERTEX[m_numBuilding];
  132.     m_bigBndyBuilding   = new BOUNDARY_3D* [m_numBuilding];
  133.     m_smallBndyBuilding = new BOUNDARY_3D* [m_numBuilding];
  134. char keyName[64];
  135. for(int i=0;i<m_numBuilding;i++)
  136. {
  137. wsprintf(keyName,"House%dX",i+1);
  138. m_buildingPos[i].xpos=(float)GetPrivateProfileInt("Building", keyName, 1000, sINIPath);
  139. wsprintf(keyName,"House%dZ",i+1);
  140. m_buildingPos[i].zpos=(float)GetPrivateProfileInt("Building", keyName, 1000, sINIPath);
  141. }
  142. m_numSoldier = GetPrivateProfileInt("Sprite", "SpriteNumber", 1, sINIPath);
  143.     m_soldierPos = new VERTEX[m_numSoldier];
  144.     m_soldierRot = new float [m_numSoldier];
  145. m_soldierHealth=new float [m_numSoldier];
  146. for( i=0;i<m_numSoldier;i++)
  147. {
  148. wsprintf(keyName,"Sprite%dX",i+1);
  149. m_soldierPos[i].xpos=(float)GetPrivateProfileInt("Sprite", keyName, 1000, sINIPath);
  150. wsprintf(keyName,"Sprite%dZ",i+1);
  151. m_soldierPos[i].zpos=(float)GetPrivateProfileInt("Sprite", keyName, 1000, sINIPath);
  152. wsprintf(keyName,"Sprite%dRot",i+1);
  153. m_soldierRot[i]     =(float)GetPrivateProfileInt("Sprite", keyName, 0, sINIPath);
  154.         m_soldierHealth[i]=1;
  155. }
  156. return true;
  157. }
  158. void CHeightmap::UpdateHeightmap()
  159. {
  160. m_cFrustumCull.m_ViewerRotate=m_ViewRotY;
  161. m_cFrustumCull.UpdateFrustumCull();
  162.      
  163. /*  m_rect.minz=m_cFrustumCull.m_pos[m_upPos].y-2;
  164.     m_rect.maxz=m_cFrustumCull.m_pos[m_downPos].y+2;
  165.     m_rect.minx=m_cFrustumCull.m_pos[minx].x-2;
  166.     m_rect.maxx=m_cFrustumCull.m_pos[maxx].x+2;
  167. */
  168. int offsetx,offsetz;
  169. offsetx=int(m_ViewPos.xpos*0.025f);
  170. if(offsetx<0)offsetx-=1;
  171. offsetz=int(m_ViewPos.zpos*0.025f);
  172. if(offsetz<0)offsetz-=1;
  173. ////////////////////////////////////////////////
  174. int x,z;
  175. int startx;
  176. int xpos,zpos;
  177. int uppos=m_cFrustumCull.m_upPos;
  178. int downpos=m_cFrustumCull.m_downPos;
  179. for(z=(m_cFrustumCull.m_pos[uppos].y-2);z<(m_cFrustumCull.m_pos[downpos].y+3);z++)
  180. {
  181.     zpos=((z-128+offsetz)*4)%256;
  182.     if(zpos<0)zpos=256+zpos;
  183.         startx=((  (m_cFrustumCull.m_LeftArray[z]-2)  -128+offsetx)*4)%256;
  184.     if(startx<0)startx=256+startx;
  185. xpos=startx;
  186. for(x=(m_cFrustumCull.m_LeftArray[z]-2);x<(m_cFrustumCull.m_RightArray[z]+3);x++)
  187. {
  188. m_pMovemap[z*256+x].xpos=xpos;
  189. m_pMovemap[z*256+x].zpos=zpos;
  190.     
  191. xpos+=4;
  192. if(xpos>252)xpos=0;
  193. }
  194. }
  195.     ////////////////////////////////////////////////
  196. m_ViewOffsetX=offsetx*40;
  197. m_ViewOffsetZ=offsetz*40;
  198.     m_numTriangles=0;
  199. }
  200. float CHeightmap::GetHeight(VERTEX *checked)
  201. {
  202. POINT point=ConvertToHeightmap(checked);
  203. float Xpos,Zpos;
  204. if(checked->xpos<0)
  205.     Xpos=1+checked->xpos*0.1f-int(checked->xpos*0.1f);
  206. else
  207.     Xpos=checked->xpos*0.1f-int(checked->xpos*0.1f);
  208. if(checked->zpos<0)
  209.     Zpos=1+checked->zpos*0.1f-int(checked->zpos*0.1f);
  210. else 
  211. Zpos=checked->zpos*0.1f-int(checked->zpos*0.1f);
  212.     ///////////////////////
  213. float ypos;
  214. float h11,h12,h22,h21;
  215. float ha,hb;
  216. //////////check Xpos and Zpos
  217. ////////////////////////////
  218. int addx=point.x+1;
  219. int addy=point.y+1;
  220. if(addx==256)addx=0;
  221. if(addy==256)addy=0;
  222.     h11=float(m_pTmap[256*(point.y+0) + (point.x+0)]);
  223.     h12=float(m_pTmap[256*(addy) + (point.x+0)]);
  224.     h21=float(m_pTmap[256*(point.y+0) + addx]);
  225.     h22=float(m_pTmap[256*(addy) + addx]);
  226. ha=h11+Xpos*(h21-h11);
  227. hb=h12+Xpos*(h22-h12);
  228.     ypos=ha +Zpos*(hb-ha);
  229. return ypos;
  230. }
  231. float CHeightmap::GetHeight(float xpos,float zpos)
  232. {
  233.    VERTEX temp=VERTEX(xpos,0,zpos);
  234.    float ypos=GetHeight(&temp);
  235.    return ypos;
  236. }
  237. bool CHeightmap::CollideCheck(VERTEX startPos,VERTEX endPos,float step)
  238. {
  239.     if(CheckTerrainCollide(startPos,endPos,step))return true;
  240.     if(CheckBuildingCollide(startPos,endPos))return true;
  241.     if(CheckTreeCollide(startPos,endPos))return true;
  242. return false;
  243. }
  244. bool CHeightmap::CheckTerrainCollide(VERTEX viewPos,VERTEX objPos,float step)
  245. {
  246. if(step<1)step=1;
  247. float dy=viewPos.ypos-objPos.ypos;
  248. float dx=viewPos.xpos-objPos.xpos;
  249. float dz=viewPos.zpos-objPos.zpos;
  250. int signx,signy,signz;
  251. if(dx<0)
  252. {dx=-dx; signx=-1;}
  253. else
  254.         signx=1;
  255. if(dy<0)
  256. {dy=-dy; signy=-1;}
  257. else
  258.         signy=1;
  259. if(dz<0)
  260. {dz=-dz; signz=-1;}
  261. else
  262.         signz=1;
  263. float terrainHeight=GetHeight(&viewPos);
  264. float CurLineHeight=viewPos.ypos;
  265. float CurZ=viewPos.zpos;
  266. float CurX=viewPos.xpos;
  267.     VERTEX vertCur;
  268.   if(dz>dx)
  269. {
  270. int times=int(dz/step);
  271. float deltaH=dy/times;
  272. float deltax=signx*dx/times;
  273. times++;
  274.         for(int i=0;i<times;i++)
  275. {
  276. vertCur.xpos=CurX;
  277. vertCur.zpos=CurZ;
  278. vertCur.ypos=CurLineHeight;
  279. if(vertCur.ypos<GetHeight(vertCur.xpos,vertCur.zpos) ) return true;
  280.             CurLineHeight -= signy*deltaH;
  281. CurX-=deltax;
  282. CurZ-=signz*step;
  283. }
  284. }
  285. else
  286. {
  287. int times=int(dx/step);
  288. float deltaH=dy/times;
  289. float deltaz=signz*dz/times;
  290. times++;
  291.         for(int i=0;i<times;i++)
  292. {
  293. vertCur.xpos=CurX;
  294. vertCur.zpos=CurZ;
  295. vertCur.ypos=CurLineHeight;
  296. if(vertCur.ypos<GetHeight(vertCur.xpos,vertCur.zpos) ) return true;
  297.             CurLineHeight -= signy*deltaH;
  298. CurZ-=deltaz;
  299. CurX-=signx*step;
  300. }
  301. }
  302.     return false;
  303. }
  304. bool CHeightmap::CheckTreeCollide(VERTEX viewPos,VERTEX objPos)
  305. {
  306. float dy=viewPos.ypos-objPos.ypos;
  307. float dx=viewPos.xpos-objPos.xpos;
  308. float dz=viewPos.zpos-objPos.zpos;
  309. int signx,signy,signz;
  310. if(dx<0)
  311. {dx=-dx; signx=-1;}
  312. else
  313.         signx=1;
  314. if(dy<0)
  315. {dy=-dy; signy=-1;}
  316. else
  317.         signy=1;
  318. if(dz<0)
  319. {dz=-dz; signz=-1;}
  320. else
  321.         signz=1;
  322.     POINT point;
  323. int   type;
  324. float distance;
  325.   if(dz>dx)
  326. {
  327. float CurZ=float(int(viewPos.zpos)/40)*40;
  328.      if(viewPos.zpos<0)CurZ -=40;
  329.     float CurX=viewPos.xpos+(CurZ-viewPos.zpos)*(viewPos.xpos-objPos.xpos)/(viewPos.zpos-objPos.zpos);
  330.     float gridX;
  331. float deltax=signx*dx*40/dz;
  332. do
  333. {
  334. point=ConvertToPlantmap(CurX,CurZ);
  335. type=GetPlantType(point.x,point.y);
  336. if(type>64)
  337. {
  338. gridX=float(int(CurX)/40)*40;
  339. if(CurX<0)gridX-=40;
  340. distance=m_cMath.GetPointToLineDist2D(viewPos,objPos,
  341.                     VERTEX(gridX+m_pCoverBias[64*point.y+point.x].xbias,0,
  342. CurZ+m_pCoverBias[64*point.y+point.x].zbias));
  343.                 if(type==128 && distance<2)return true;
  344.                 if(type==192 && distance<1.2f)return true;
  345.                 if(type==255 && distance<1)return true;
  346. }
  347.      CurX-=deltax;
  348.     CurZ-=signz*40;
  349. if(signz>0 )
  350. {
  351. if(CurZ<objPos.zpos)return false;
  352. }
  353. else
  354. {
  355. if(CurZ>objPos.zpos)return false;
  356. }
  357. }while(1);
  358. }
  359. else
  360. {
  361. float CurX=float(int(viewPos.xpos)/40)*40;
  362.      if(viewPos.xpos<0)CurX -=40;
  363.     float CurZ=viewPos.zpos+(CurX-viewPos.xpos)*(viewPos.zpos-objPos.zpos)/(viewPos.xpos-objPos.xpos);
  364.     float gridZ;
  365. float deltaz=signz*dz*40/dx;
  366. do
  367. {
  368. point=ConvertToPlantmap(CurX,CurZ);
  369. type=GetPlantType(point.x,point.y);
  370. if(type>64)
  371. {
  372. gridZ=float(int(CurZ)/40)*40;
  373. if(CurZ<0)gridZ-=40;
  374. distance=m_cMath.GetPointToLineDist2D(viewPos,objPos,
  375.                     VERTEX(CurX+m_pCoverBias[64*point.y+point.x].xbias,0,
  376. gridZ+m_pCoverBias[64*point.y+point.x].zbias));
  377. if(type==128 && distance<2)return true;
  378.                 if(type==192 && distance<1.2f)return true;
  379.                 if(type==255 && distance<1)return true;
  380. }
  381.      CurZ-=deltaz;
  382.     CurX-=signx*40;
  383. if(signx>0 )
  384. {
  385. if(CurX<objPos.xpos)return false;
  386. }
  387. else
  388. {
  389. if(CurX>objPos.xpos)return false;
  390. }
  391. }while(1);
  392. }
  393.     return false;
  394. }
  395. bool CHeightmap::CheckBuildingCollide(VERTEX startPos,VERTEX endPos)
  396. {
  397. for(int i=0;i<m_numBuilding;i++)
  398. {
  399. if(m_cMath.IsLineSegmentCutBy3DBoundary(&startPos,&endPos,m_smallBndyBuilding[i]))
  400. return true;
  401. }
  402. return false;
  403. }
  404. bool CHeightmap::CollideTreeBody(VERTEX *check)
  405. {
  406. POINT point=ConvertToMovemap(*check);
  407. int tx,tz;
  408. tx=m_pMovemap[point.y*256+point.x].xpos;
  409. tz=m_pMovemap[point.y*256+point.x].zpos;
  410. if(m_pCovermap[tz*16+tx/4]<65)return false;
  411. float treex,treez;
  412. treex=float(m_ViewOffsetX + (point.x-128)*40 + m_pCoverBias[tz*16+tx/4].xbias);
  413. treez=float(m_ViewOffsetZ + (point.y-128)*40 + m_pCoverBias[tz*16+tx/4].zbias);
  414. float deltaX=(treex>check->xpos)?(treex-check->xpos):(check->xpos-treex);
  415. float deltaZ=(treez>check->zpos)?(treez-check->zpos):(check->zpos-treez);
  416. if(deltaX<4 && deltaZ<4)
  417. {
  418. if(deltaX>deltaZ)
  419. {
  420. if(treex>check->xpos)check->xpos=treex-4;
  421. else check->xpos=treex+4;
  422. }
  423. else
  424. {
  425. if(treez>check->zpos)check->zpos=treez-4;
  426. else check->zpos=treez+4;
  427. }
  428. return true;
  429. }
  430. return false;
  431. }
  432. bool CHeightmap::CollideBuilding(VERTEX *oldPos,VERTEX *newPos)
  433. {
  434. for(int i=0;i<m_numBuilding;i++)
  435. {
  436. if(IsInBoundary3D(newPos,m_bigBndyBuilding[i]))
  437. {
  438. VERTEX temp=VERTEX(newPos->xpos,newPos->ypos,newPos->zpos);
  439. newPos->zpos=oldPos->zpos;
  440. if(!IsInBoundary3D(newPos,m_bigBndyBuilding[i]))
  441.                 return true;
  442. newPos->zpos=temp.zpos;
  443. newPos->xpos=oldPos->xpos;
  444. if(!IsInBoundary3D(newPos,m_bigBndyBuilding[i]))
  445.                 return true;
  446. oldPos->zpos=oldPos->zpos;
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. bool CHeightmap::IsInFrustum(VERTEX *checked)
  453. {
  454. POINT point=ConvertToMovemap(*checked);
  455. return m_cFrustumCull.IsInFrustum(point.x,point.y);
  456. }
  457. bool CHeightmap::IsInFrustum(float x,float z)
  458. {
  459. POINT point=ConvertToMovemap(VERTEX(x,255,z));
  460. return m_cFrustumCull.IsInFrustum(point.x,point.y);
  461. }
  462. bool CHeightmap::IsInFrustum(BOUNDARY_3D *pBoundary)
  463. {
  464. if(IsInFrustum(pBoundary->minx,pBoundary->minz))return true;
  465. if(IsInFrustum(pBoundary->maxx,pBoundary->minz))return true;
  466. if(IsInFrustum(pBoundary->maxx,pBoundary->maxz))return true;
  467. if(IsInFrustum(pBoundary->minx,pBoundary->maxz))return true;
  468. return false;
  469. }
  470. VERTEX CHeightmap::ConvertToWorld(int movemapXPos,int movemapZPos)
  471. {
  472. VERTEX position;
  473. position.xpos=float(m_ViewOffsetX + (movemapXPos-128)*40);
  474. position.zpos=float(m_ViewOffsetZ + (movemapZPos-128)*40);
  475. POINT point ; //point in terrain map
  476. point.x=m_pMovemap[movemapZPos*256+movemapXPos].xpos;
  477. point.y=m_pMovemap[movemapZPos*256+movemapXPos].zpos;
  478. position.ypos=m_pTmap[256*point.y + point.x];
  479. return position;
  480. }
  481. POINT CHeightmap::ConvertToHeightmap(VERTEX *pos)
  482. {
  483. POINT point;
  484. if(pos->xpos<0)
  485.     point.x=255+int(pos->xpos*0.1f)%256;
  486. else
  487. point.x=int(pos->xpos*0.1f)%256;
  488. if(pos->zpos<0)
  489.     point.y=255+int(pos->zpos*0.1f)%256;
  490. else 
  491. point.y=int(pos->zpos*0.1f)%256;
  492. return point;
  493. }
  494. POINT CHeightmap::ConvertToMovemap(VERTEX pos)
  495. {
  496.   POINT point;
  497.     if(pos.xpos < m_ViewOffsetX)
  498.     point.x=int((pos.xpos-m_ViewOffsetX)*0.025f)+127;
  499. else
  500.     point.x=int((pos.xpos-m_ViewOffsetX)*0.025f)+128;
  501. if(pos.zpos<m_ViewOffsetZ)
  502.     point.y=int((pos.zpos-m_ViewOffsetZ)*0.025f)+127;
  503. else
  504.         point.y=int((pos.zpos-m_ViewOffsetZ)*0.025f)+128;
  505. if(point.x<0 || point.x>255 )
  506. {   
  507. POINT p;
  508.     p.x=p.y=0;
  509. return p;
  510. }
  511. if(point.y<0 || point.y>255 )
  512. {   
  513. POINT p;
  514.     p.x=p.y=0;
  515. return p;
  516. }
  517.  
  518. return point;
  519. }
  520. POINT CHeightmap::ConvertToPlantmap(float worldX,float worldZ)
  521. {
  522. POINT point;
  523. if(worldX<0)
  524.     point.x=63+int(worldX*0.025f)%64;
  525. else
  526. point.x=int(worldX*0.025f)%64;
  527. if(worldZ<0)
  528.     point.y=63+int(worldZ*0.025f)%64;
  529. else 
  530. point.y=int(worldZ*0.025f)%64;
  531.     
  532.     return point;
  533. }
  534. void CHeightmap::GetFocusPos()
  535. {
  536.     //////////////// calcau
  537. float depth[2];
  538.     int xpos=CGameSetting::m_iScrWidth/2;
  539. int ypos=CGameSetting::m_iScrHeight/2;
  540. glReadPixels(xpos,ypos,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,depth);
  541.     GLdouble ModelMatrix[16], ProjMatrix[16];
  542.     glGetDoublev(GL_MODELVIEW_MATRIX,  ModelMatrix);
  543.     glGetDoublev(GL_PROJECTION_MATRIX, ProjMatrix);
  544.     GLint    Viewport[4]={0,0,CGameSetting::m_iScrWidth,CGameSetting::m_iScrHeight};
  545.     gluUnProject(xpos,ypos, depth[0], 
  546.          ModelMatrix, ProjMatrix, Viewport,
  547.  &m_focusPosX, &m_focusPosY, &m_focusPosZ);
  548. double dx=m_ViewPos.xpos-m_focusPosX;
  549. double dy=m_ViewPos.ypos-m_focusPosY;
  550. double dz=m_ViewPos.zpos-m_focusPosZ;
  551. m_distFromFocus= dx*dx+dy*dy+dz*dz;
  552. }
  553. bool CHeightmap::IsInBoundary3D(VERTEX *pos, BOUNDARY_3D *boundary)
  554. {
  555. if(pos->xpos > boundary->maxx || pos->xpos < boundary->minx )return false;
  556. if(pos->ypos > boundary->maxy || pos->ypos < boundary->miny )return false;
  557. if(pos->zpos > boundary->maxz || pos->zpos < boundary->minz )return false;
  558. return true;
  559. }
  560. ///////////////////////////////////////////////
  561. //////////////////////////////////////////////
  562. bool CHeightmap::ReadTerrainmapFile()
  563. {
  564.     m_pTmap=new unsigned char[256*256];
  565. if(m_pTmap==NULL)return false;
  566. FILE *fp;
  567. fp=fopen(CGameSetting::m_strHeightmap,"rb");
  568. if(!fp)return false;
  569. BITMAPFILEHEADER fileheader;
  570. fread(&fileheader,sizeof(BITMAPFILEHEADER),1,fp);
  571. if(fileheader.bfType!=19778)return false;
  572.     fseek(fp,1078,SEEK_SET);
  573. for(int z=255;z>-1;z--)
  574. for(int x=0;x<256;x++)
  575. {
  576. fread(&m_pTmap[256*z+x],sizeof(unsigned char),1,fp);
  577. }
  578. fclose(fp);
  579.     return true;
  580. }
  581. bool CHeightmap::ReadPlantmapFile()
  582. {
  583.     m_pCovermap =new unsigned char[64*64];
  584. m_pCoverBias=new GRID_BIAS[64*64];
  585. if(m_pCovermap==NULL)return false;
  586. if(m_pCoverBias==NULL)return false;
  587. FILE *fp;
  588. fp=fopen(CGameSetting::m_strPlantIndex,"rb");
  589. if(!fp)return false;
  590. BITMAPFILEHEADER fileheader;
  591. fread(&fileheader,sizeof(BITMAPFILEHEADER),1,fp);
  592. if(fileheader.bfType!=19778)return false;
  593.     fseek(fp,1078,SEEK_SET);
  594. for(int z=63;z>-1;z--)
  595. for(int x=0;x<64;x++)
  596. {
  597. fread(&m_pCovermap[64*z+x],sizeof(unsigned char),1,fp);
  598. }
  599. fclose(fp);
  600. /////construct plant position
  601. for( z=0;z<64;z++)
  602. for(int x=0;x<64;x++)
  603. {
  604. m_pCoverBias[64*z+x].xbias=5+(rand()%30);
  605. m_pCoverBias[64*z+x].zbias=5+(rand()%30);
  606. }
  607. /////////////////////////////
  608.     return true;
  609. }
  610. int  CHeightmap::GetPosInMovemap(int x,int z)
  611. {
  612.     return m_pMovemap[(128+z)*256+128+x].zpos*256+m_pMovemap[(128+z)*256+128+x].xpos;
  613. }
  614. int CHeightmap::GetPlantType(int mapx,int mapz)
  615. {
  616.     return m_pCovermap[mapz*64+mapx];
  617. }
  618. void CHeightmap::MakeModulePath(char filename[], char sINIPath[])
  619. {
  620. // Add the path of the executable to the passed INI filename
  621. // Buffer to hold the filename
  622. char sModuleFileName[MAX_PATH];
  623. // Pass NULL to get the handle of the current module
  624. HMODULE hModule = GetModuleHandle(NULL);
  625. // Fill the path into sModuleFileName and get the number of returned chars
  626. int iNumChars = GetModuleFileName(hModule, sModuleFileName, MAX_PATH);
  627. // If the path was received
  628. if (iNumChars)
  629. {
  630. // Loop from the last to the first char
  631. for (int iCurChar=iNumChars-1; iCurChar>=0; iCurChar--)
  632. {
  633. // If the current char is a backslash
  634. if (sModuleFileName[iCurChar] == char('\'))
  635. {
  636. // Terminate the the string behind the backslash and
  637. sModuleFileName[iCurChar+1] = char('');
  638. break;
  639. }
  640. }
  641. // Append INI filename at the end of the path
  642. strcat(sModuleFileName, filename);
  643. // Copy the full path into the destibation
  644. strcpy(sINIPath, sModuleFileName);
  645. }
  646. }