home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / WD_SRC.ZIP / SOURCE / BSP.CPP < prev    next >
C/C++ Source or Header  |  1995-01-07  |  5KB  |  233 lines

  1. #include "..\Source\LastWolf.hpp"
  2.  
  3.  
  4. BOOL bsp_TraverseAndDrawTree( CLine *pRootLine )
  5. {
  6.     // Get which side the player is on of pRootLine.
  7.     // Visit that side, pRootLine, then the opposite side.
  8.     CLine *pFirst, *pSecond;
  9.     CLine *pLeft, *pRight;
  10.     CLine *pDrawRoot;
  11.     SideDir pruneVal, viewSide;
  12.  
  13.  
  14.     // Test if it's possible to prune off a whole subtree (by not calling
  15.     // bsp_TraverseAndDrawTree()).
  16.     pruneVal = dr_PruneTree(pRootLine);
  17.     switch( pruneVal )
  18.     {
  19.         case LeftSide:
  20.             pLeft = NULL;
  21.             pRight = pRootLine->pRight;
  22.             break;
  23.  
  24.         case RightSide:
  25.             pLeft = pRootLine->pLeft;
  26.             pRight = NULL;
  27.             break;
  28.  
  29.         case Intersect:
  30.             pLeft = pRootLine->pLeft;
  31.             pRight = pRootLine->pRight;
  32.             break;
  33.     }
  34.  
  35.     
  36.     // Determine the order to draw the subtrees based on which side of
  37.     // pRootLine the player is on.
  38.     viewSide = bsp_PlayerSide(&player, pRootLine);
  39.     if( viewSide == LeftSide )
  40.     {
  41.         pFirst = pLeft;
  42.         pSecond = pRight;
  43.     
  44.         pDrawRoot = pRootLine;
  45.     }
  46.     else
  47.     {
  48.         pFirst = pRight;
  49.         pSecond = pLeft;
  50.     
  51.         pDrawRoot = pRootLine;
  52.     }
  53.  
  54.  
  55.     
  56.     // Do the drawing.
  57.     if( pFirst != NULL )
  58.         if( bsp_TraverseAndDrawTree( pFirst ) == FALSE )
  59.             return FALSE;
  60.  
  61.     if( pDrawRoot != NULL )
  62.         if( dr_DrawWall( pDrawRoot, viewSide ) == ScreenCompletelyDrawn )
  63.             return FALSE;
  64.  
  65.     if( pSecond != NULL )
  66.         if(    bsp_TraverseAndDrawTree( pSecond ) == FALSE )
  67.             return FALSE;
  68.  
  69.  
  70.     return TRUE;
  71. }
  72.  
  73.  
  74. SideDir bsp_PlayerSide( Player *pPlayer, CLine *pLine )
  75. {
  76.     Angle lineAngle;
  77.     Fixed rotatedPlayerY;
  78.  
  79.  
  80.     lineAngle = GetAngle( pLine->pPoint1->localX, pLine->pPoint1->localY,
  81.                           pLine->pPoint2->localX, pLine->pPoint2->localY );
  82.  
  83.     lineAngle = (ANGLE_RES - lineAngle) & ANGLE_MASK;
  84.  
  85.     // Rotate all of pToSplit's vertices *around* pMainLine's first Point.
  86.     RotatePointYOnly( pLine->pPoint1->localX, pLine->pPoint1->localY,
  87.                 lineAngle, pPlayer->xPos, pPlayer->yPos,
  88.                 &rotatedPlayerY );
  89.  
  90.     if( UNFIX(rotatedPlayerY) >= pLine->pPoint1->localY )
  91.         return LeftSide;
  92.     else
  93.         return RightSide;
  94. }
  95.  
  96.  
  97.  
  98. void RotatePointYOnly( DWORD xOrigin, DWORD yOrigin, Angle rotateAngle, DWORD pointX, DWORD pointY, Fixed *pDestY )
  99. {
  100.     Fixed x, y;
  101.     
  102.     x = pointX - xOrigin;
  103.     y = pointY - yOrigin;
  104.  
  105.     *pDestY = (SAFE_SIN(rotateAngle) * x) + (SAFE_COS(rotateAngle) * y);
  106.     *pDestY += FIX(yOrigin);    
  107. }
  108.  
  109.  
  110. void RotatePoint( DWORD xOrigin, DWORD yOrigin, Angle rotateAngle, DWORD pointX, DWORD pointY, Fixed *pDestX, Fixed *pDestY )
  111. {
  112.     Fixed x, y;
  113.     
  114.     x = pointX - xOrigin;
  115.     y = pointY - yOrigin;
  116.  
  117.     *pDestX = (SAFE_COS(rotateAngle) * x) - (SAFE_SIN(rotateAngle) * y);
  118.     *pDestX += FIX(xOrigin);
  119.  
  120.     *pDestY = (SAFE_SIN(rotateAngle) * x) + (SAFE_COS(rotateAngle) * y);
  121.     *pDestY += FIX(yOrigin);    
  122. }
  123.  
  124.  
  125. void RotatePointFixed( Fixed xOrigin, Fixed yOrigin, Angle rotateAngle, Fixed pointX, Fixed pointY, Fixed *pDestX, Fixed *pDestY )
  126. {
  127.     Fixed x, y;
  128.     
  129.     x = pointX - xOrigin;
  130.     y = pointY - yOrigin;
  131.  
  132.     *pDestX = FMul(SAFE_COS(rotateAngle), x) - FMul(SAFE_SIN(rotateAngle), y);
  133.     *pDestX += FIX(xOrigin);
  134.  
  135.     *pDestY = FMul(SAFE_SIN(rotateAngle), x) + FMul(SAFE_COS(rotateAngle), y);
  136.     *pDestY += FIX(yOrigin);    
  137. }
  138.  
  139.  
  140. Angle GetAngle( DWORD x1, DWORD y1, DWORD x2, DWORD y2 )
  141. {
  142.     Fixed slope, slopeLookup;
  143.     Angle lookupAngle;
  144.     DWORD wholeX, wholeY;
  145.     BOOL yOnTop;
  146.  
  147.     wholeX = x2 - x1;
  148.     wholeY = y2 - y1;
  149.     
  150.     if( 0 == wholeX )
  151.     {
  152.         if( wholeY > 0 )
  153.             return HALF_PI;
  154.         else
  155.             return HALF_PI + PI;
  156.     }
  157.     else if( 0 == wholeY )
  158.     {
  159.         if( wholeX > 0 )
  160.             return 0;
  161.         else
  162.             return PI;
  163.     }
  164.     
  165.     
  166.     if( ABS(wholeX) > ABS(wholeY) )
  167.         yOnTop = TRUE;
  168.     else
  169.         yOnTop = FALSE;
  170.  
  171.     if( yOnTop )
  172.         slope = FDiv( FIX(wholeY), FIX(wholeX) );
  173.     else
  174.         slope = FDiv( FIX(wholeX), FIX(wholeY) );
  175.  
  176.     
  177.     //slopeLookup = R_UNFIX( FMul(slope, FIX(ANGLE_RES-1)) );
  178.     slopeLookup = W_UNFIX( slope * (ANGLE_RES-1) );
  179.     
  180.     if( slopeLookup < 0 )
  181.         lookupAngle = ATAN_TABLE[-slopeLookup];
  182.     else
  183.         lookupAngle = ATAN_TABLE[slopeLookup];
  184.  
  185.     if( !yOnTop )
  186.         lookupAngle = (Angle)(HALF_PI - lookupAngle);
  187.         
  188.     if( wholeX > 0 && wholeY < 0 )
  189.         return (CIRCLE - lookupAngle);
  190.     else if( wholeX < 0 && wholeY < 0 )
  191.         return (Angle)(PI + lookupAngle);
  192.     else if( wholeX < 0 && wholeY > 0 )
  193.         return (Angle)(PI - lookupAngle);
  194.     else if( wholeX > 0 && wholeY > 0 )
  195.         return (Angle)lookupAngle;
  196.   
  197.     return lookupAngle;
  198. }
  199.  
  200.  
  201. SideDir bsp_PruneTree( CLine *pLine )
  202. {
  203.     pLine=pLine;
  204.  
  205.     return Intersect;
  206. }
  207.  
  208.  
  209. CLine *bsp_GetParentFromLine( CLine *pRootLine, CLine *pChildLine )
  210. {
  211.     CLine *pRetVal;
  212.     
  213.     if( pRootLine->pLeft == pChildLine || pRootLine->pRight == pChildLine )
  214.         return pRootLine;
  215.     else
  216.     {
  217.         if( pRootLine->pLeft != NULL )
  218.         {
  219.              pRetVal = bsp_GetParentFromLine( pRootLine->pLeft, pChildLine );
  220.              if( pRetVal != NULL )
  221.                  return pRetVal;
  222.         }
  223.         
  224.         if( pRootLine->pRight != NULL )
  225.         {
  226.              pRetVal = bsp_GetParentFromLine( pRootLine->pRight, pChildLine );
  227.              if( pRetVal != NULL )
  228.                  return pRetVal;
  229.         }
  230.     }
  231.     
  232.     return NULL;
  233. }