/*
0         1         2         3         4         5         6         7
01234567890123456789012345678901234567890123456789012345678901234567890123456789
--------------------------------------------------------------------------------
-- Description : Particles system
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
#ifndef D_THROW_PRT
 
#include "acp_base.h"
#include "MTH.h"
#include "RND.h"
#include "MEC.h"
#include "GMT.h"
#include "GEO.h"
#include "GLI.h"
#include "COL.h"
 
#include "PRT/SegmtPrt.h"
 
/*
--------------------------------------------------------------------------------
-- Description : Global variables
--------------------------------------------------------------------------------
*/
 
/*
--------------------------------------------------------------------------------
-- Description : Segment particles creation
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vCreateSegmentParticles ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                      GMT_tdxHandleToGameMaterial _hMaterial )
{
  ACP_tdxHandleOfElement hElementSegment;
  ACP_tdxIndex xSegmentIndex;
  GEO_tdstDoubledIndex stSegmentPoints;
  GEO_tdstColor stColor;
  PRT_tdstSegmentCommonData *p_stSegmentCommonData;
 
  _p_stParticlesSystem->xParticlesType = PRT_C_xSegmentParticles;
 
  /* Table de vitesse de particules */
  GEO_M_CPAMalloc ( _p_stParticlesSystem->d_stSpeed,
                    MTH3D_tdstVector *,
                    sizeof ( MTH3D_tdstVector ) * _p_stParticlesSystem->xNbParticles,
                    E_uwGEONotEnoughtMemory );
 
  GEO_M_CPAMalloc ( _p_stParticlesSystem->d_stListOfParticles,
                    void *,
                    sizeof ( PRT_tdstSegmentParticle ) * _p_stParticlesSystem->xNbParticles,
                    E_uwGEONotEnoughtMemory );
 
  GEO_M_CPAMalloc ( _p_stParticlesSystem->p_stCommonData,
                    void *,
                    sizeof ( PRT_tdstSegmentCommonData ),
                    E_uwGEONotEnoughtMemory );
 
  stColor.xR = 1.0f;
  stColor.xG = 1.0f;
  stColor.xB = 1.0f;
  stColor.xA = 1.0f;
 
  GEO_vCreateGeometricObject ( &(_p_stParticlesSystem->hParticlesSystemObject), (ACP_tdxIndex) (2*_p_stParticlesSystem->xNbParticles), (ACP_tdxIndex)1 );
  GEO_xCreateElementLines ( _p_stParticlesSystem->hParticlesSystemObject, &hElementSegment, _p_stParticlesSystem->xNbParticles );
 
  for ( xSegmentIndex = 0 ; xSegmentIndex < _p_stParticlesSystem->xNbParticles ; xSegmentIndex ++ )
  {
    stSegmentPoints.a2_xIndex[0] = 2*xSegmentIndex;
    stSegmentPoints.a2_xIndex[1] = 2*xSegmentIndex + 1;
    GEO_xSetIndexOfElementLines ( _p_stParticlesSystem->hParticlesSystemObject, hElementSegment, xSegmentIndex, &stSegmentPoints );
    GEO_xSetGameMaterialOfElementLines ( _p_stParticlesSystem->hParticlesSystemObject, hElementSegment, xSegmentIndex, _hMaterial );
  }
  GEO_vEndCreateObject ( _p_stParticlesSystem->hParticlesSystemObject );
 
  p_stSegmentCommonData = (PRT_tdstSegmentCommonData *)(_p_stParticlesSystem->p_stCommonData);
  p_stSegmentCommonData->hMaterial = _hMaterial;
}
 
/*
--------------------------------------------------------------------------------
-- Description : Segment particles destruction
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
 
#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC */
void PRT_fn_vDestructSegmentParticles ( PRT_tdstParticlesSystem *_p_stParticlesSystem )
{
  _p_stParticlesSystem->xParticlesType = PRT_C_xNullParticles;
 
  /* Table de vitesse de particules */
  GEO_M_CPAFree ( _p_stParticlesSystem->d_stSpeed );
  _p_stParticlesSystem->d_stSpeed = NULL;
 
  GEO_M_CPAFree ( _p_stParticlesSystem->d_stListOfParticles );
  _p_stParticlesSystem->d_stListOfParticles = NULL;
 
  GEO_M_CPAFree ( _p_stParticlesSystem->p_stCommonData );
  _p_stParticlesSystem->p_stCommonData = NULL;
 
  GEO_vDeleteGeometricObject ( &(_p_stParticlesSystem->hParticlesSystemObject) );
  _p_stParticlesSystem->hParticlesSystemObject = NULL;
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vSetGameMaterialSegmentParticles ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                               GMT_tdxHandleToGameMaterial _hGameMaterial )
{
  PRT_tdstSegmentCommonData *p_stSegmentCommonData;
 
  p_stSegmentCommonData = (PRT_tdstSegmentCommonData *)(_p_stParticlesSystem->p_stCommonData);
  p_stSegmentCommonData->hMaterial = _hGameMaterial;
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vSetPointColorSegmentParticles ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                             GEO_tdstColor *_p_stColor )
{
  PRT_tdstSegmentCommonData *p_stSegmentCommonData;
 
  p_stSegmentCommonData = (PRT_tdstSegmentCommonData *)(_p_stParticlesSystem->p_stCommonData);
  p_stSegmentCommonData->stPointColor = *_p_stColor;
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vSetLastPointColorSegmentParticles ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                                 GEO_tdstColor *_p_stColor )
{
  PRT_tdstSegmentCommonData *p_stSegmentCommonData;
 
  p_stSegmentCommonData = (PRT_tdstSegmentCommonData *)(_p_stParticlesSystem->p_stCommonData);
  p_stSegmentCommonData->stLastPointColor = *_p_stColor;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC */
 
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vGenerateSegmentParticle ( MTH3D_tdstVector *_p_stParticlePosition,
                                       MTH3D_tdstVector *_p_stParticleSpeed,
                                       PRT_tdstSegmentParticle *_p_stSegmentParticle,
                                       PRT_tdstSegmentCommonData *_p_stSegmentCommonData,
                                       PRT_tdstParticlesSource *_p_stParticlesSource,
                                       POS_tdstCompletePosition *_p_stSourceMatrix,
                                       MTH_tdxReal _xTimeT )
{
  MTH3D_tdstVector stSpeedVariation;
  MTH_tdxReal xC1;
  MTH_tdxReal xC2;
  MTH_tdxReal xC3;
  MTH_tdxReal xC4;
  MTH3D_tdstVector stTrans;
 
  /* vecteur variation de vitesse */
  MTH3D_M_vSetVectorElements ( &stSpeedVariation,
    RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesSource->xSpeedRange ), _p_stParticlesSource->xSpeedRange ),
    RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesSource->xSpeedRange ), _p_stParticlesSource->xSpeedRange ),
    RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesSource->xSpeedRange ), _p_stParticlesSource->xSpeedRange ) );
 
  /* vitesse initiale */
  MTH3D_M_vMullAddVector ( _p_stParticleSpeed, _p_stParticlesSource->xSpeed, &(_p_stParticlesSource->stNormal), &stSpeedVariation );
  if ( (_p_stParticlesSource->ucLinkMode == PRT_C_ucRotationLink) || (_p_stParticlesSource->ucLinkMode == PRT_C_ucRotAndTransLink) )
  {
    POS_fn_vMulMatrixVector ( _p_stParticleSpeed, _p_stSourceMatrix, _p_stParticleSpeed );
  }
 
  /* position initiale */
  if ( _p_stParticlesSource->ucGeometryMode == PRT_C_ucPoint )
  {
    MTH3D_M_vCopyVector ( _p_stParticlePosition, &(_p_stParticlesSource->stVertex1) );
  }
  else
  if ( _p_stParticlesSource->ucGeometryMode == PRT_C_ucQuad )
  {
    /* genere les deux parametres */
    xC2 = RND_fn_xGetRealRandomValue ( 0, MTH_C_ZERO, MTH_C_ONE );
    xC3 = RND_fn_xGetRealRandomValue ( 0, MTH_C_ZERO, MTH_C_ONE );
    xC1 = MTH_M_xSub ( MTH_M_xSub ( MTH_C_ONE, xC2 ), xC3 );
    _p_stParticlePosition->xX = xC1*_p_stParticlesSource->stVertex1.xX + xC2*_p_stParticlesSource->stVertex2.xX + xC3*_p_stParticlesSource->stVertex3.xX;
    _p_stParticlePosition->xY = xC1*_p_stParticlesSource->stVertex1.xY + xC2*_p_stParticlesSource->stVertex2.xY + xC3*_p_stParticlesSource->stVertex3.xY;
    _p_stParticlePosition->xZ = xC1*_p_stParticlesSource->stVertex1.xZ + xC2*_p_stParticlesSource->stVertex2.xZ + xC3*_p_stParticlesSource->stVertex3.xZ;
  }
  else
  if ( _p_stParticlesSource->ucGeometryMode == PRT_C_ucParallelepipede )
  {
    /* genere les deux parametres */
    xC2 = RND_fn_xGetRealRandomValue ( 0, MTH_C_ZERO, MTH_C_ONE );
    xC3 = RND_fn_xGetRealRandomValue ( 0, MTH_C_ZERO, MTH_C_ONE );
    xC4 = RND_fn_xGetRealRandomValue ( 0, MTH_C_ZERO, MTH_C_ONE );
    xC1 = MTH_M_xSub ( MTH_M_xSub ( MTH_M_xSub ( MTH_C_ONE, xC2 ), xC3 ), xC4 );
    _p_stParticlePosition->xX = xC1*_p_stParticlesSource->stVertex1.xX + xC2*_p_stParticlesSource->stVertex2.xX + xC3*_p_stParticlesSource->stVertex3.xX + xC4*_p_stParticlesSource->stVertex4.xX;
    _p_stParticlePosition->xY = xC1*_p_stParticlesSource->stVertex1.xY + xC2*_p_stParticlesSource->stVertex2.xY + xC3*_p_stParticlesSource->stVertex3.xY + xC4*_p_stParticlesSource->stVertex4.xY;
    _p_stParticlePosition->xZ = xC1*_p_stParticlesSource->stVertex1.xZ + xC2*_p_stParticlesSource->stVertex2.xZ + xC3*_p_stParticlesSource->stVertex3.xZ + xC4*_p_stParticlesSource->stVertex4.xZ;
  }
 
  if ( _p_stParticlesSource->ucLinkMode == PRT_C_ucRotAndTransLink )
  {
    POS_fn_vMulMatrixVertex( _p_stParticlePosition, _p_stSourceMatrix, _p_stParticlePosition );
  }
  else
  if ( _p_stParticlesSource->ucLinkMode == PRT_C_ucTranslationLink )
  {
    POS_fn_vGetTranslationVector( _p_stSourceMatrix, &stTrans );
    MTH3D_M_vAddVector ( _p_stParticlePosition, &stTrans, _p_stParticlePosition );
  }
  else
  if ( _p_stParticlesSource->ucLinkMode == PRT_C_ucRotationLink )
  {
    POS_fn_vMulMatrixVector ( _p_stParticlePosition, _p_stSourceMatrix, _p_stParticlePosition );
  }
 
  /* LastPos */
  MTH3D_M_vCopyVector ( _p_stParticlePosition+1, _p_stParticlePosition );
 
  /* couleur du segment */
  _p_stSegmentParticle->stPointColor = _p_stSegmentCommonData->stPointColor;
  _p_stSegmentParticle->stLastPointColor = _p_stSegmentCommonData->stLastPointColor;
 
  /* DeathDate */
  if ( _p_stParticlesSource->ucLifeMode == PRT_C_ucNoLifeTime )
  {
    _p_stSegmentParticle->xDeathDate = MTH_C_InfinitPlus;
  }
  else
  if ( _p_stParticlesSource->ucLifeMode == PRT_C_ucConstantLifeTime )
  {
    _p_stSegmentParticle->xDeathDate = MTH_M_xAdd ( _xTimeT, _p_stParticlesSource->xMinTime );
  }
  else
  if ( _p_stParticlesSource->ucLifeMode == PRT_C_ucProbabilistLifeTime )
  {
    _p_stSegmentParticle->xDeathDate = MTH_M_xAdd ( _xTimeT, RND_fn_xGetRealRandomValue ( 0, _p_stParticlesSource->xMinTime, _p_stParticlesSource->xMaxTime ) );
  }
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vMecaSegmentParticlesSystem ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                          PRT_tdstParticlesEnvironment *_p_stParticlesEnvironment,
                                          MTH_tdxReal _xTimeT,
                                          MTH_tdxReal _xDeltaT )
{
  PRT_tdstSegmentParticle *p_stSegmentParticle;
  PRT_tdstSegmentParticle *p_stLastSegmentParticle;
  MTH3D_tdstVector *p_stParticlePosition;
  MTH3D_tdstVector *p_stParticleLastPosition;
  MTH3D_tdstVector *p_stLastParticlePosition;
  MTH3D_tdstVector *p_stLastParticleLastPosition;
  MTH3D_tdstVector *p_stParticleSpeed;
  MTH3D_tdstVector *p_stLastParticleSpeed;
  ACP_tdxBool bAlivedParticle;
  MTH3D_tdstVector stAccelerationVariation;
 
  p_stSegmentParticle = (PRT_tdstSegmentParticle *)(_p_stParticlesSystem->d_stListOfParticles);
  p_stLastSegmentParticle = p_stSegmentParticle + _p_stParticlesSystem->xNbActiveParticles;
  p_stParticlePosition = _p_stParticlesSystem->hParticlesSystemObject->d_stListOfPoints;
  p_stParticleLastPosition = p_stParticlePosition + 1;
  p_stLastParticlePosition = p_stParticlePosition + 2*_p_stParticlesSystem->xNbActiveParticles;
  p_stLastParticleLastPosition = p_stLastParticlePosition + 1;
  p_stParticleSpeed = _p_stParticlesSystem->d_stSpeed;
  p_stLastParticleSpeed = p_stParticleSpeed + _p_stParticlesSystem->xNbActiveParticles;
 
  while ( p_stSegmentParticle != p_stLastSegmentParticle )
  {
    /* LastPos */
    MTH3D_M_vCopyVector ( p_stParticleLastPosition, p_stParticlePosition );
 
    /* appliquer la meca */
    /* vecteur variation de l acceleration */
    MTH3D_M_vSetVectorElements ( &stAccelerationVariation,
      RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesEnvironment->xAccelerationRange ), _p_stParticlesEnvironment->xAccelerationRange ),
      RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesEnvironment->xAccelerationRange ), _p_stParticlesEnvironment->xAccelerationRange ),
      RND_fn_xGetRealRandomValue ( 0, MTH_M_xNeg ( _p_stParticlesEnvironment->xAccelerationRange ), _p_stParticlesEnvironment->xAccelerationRange ) );
 
    MTH3D_M_vAddVector ( &stAccelerationVariation, &(_p_stParticlesEnvironment->stAcceleration), &stAccelerationVariation );
 
    /* Vit t = f( Vit t-1, Pos t-1, Accel t ) */
    MTH3D_M_vMullAddVector ( p_stParticleSpeed, _xDeltaT, &stAccelerationVariation, p_stParticleSpeed );
    /* Pos t = f( Vit t, Pos t-1, Accel t ) */
    MTH3D_M_vMullAddVector ( p_stParticlePosition, _xDeltaT, p_stParticleSpeed, p_stParticlePosition );
 
    bAlivedParticle = MTH_M_bGreater ( p_stSegmentParticle->xDeathDate, _xTimeT );
    if ( bAlivedParticle )
    {
      bAlivedParticle = ( _p_stParticlesEnvironment->ulEnvType == PRT_C_ulAlignedBoxEnv ) ?
                        INT_fn_bGetInclusionPointInBox ( p_stParticlePosition,
                                                         &(_p_stParticlesEnvironment->stMinPoint),
                                                         &(_p_stParticlesEnvironment->stMaxPoint) ) :
                        INT_fn_bGetInclusionPointInSphere ( p_stParticlePosition,
                                                            &(_p_stParticlesEnvironment->stSphereCenter),
                                                            _p_stParticlesEnvironment->xRadius );
    }
 
    if ( bAlivedParticle )
    {
      p_stSegmentParticle ++;
      p_stParticlePosition ++;
      p_stParticlePosition ++;
      p_stParticleLastPosition ++;
      p_stParticleLastPosition ++;
      p_stParticleSpeed ++;
    }
    else
    {
      _p_stParticlesSystem->xNbActiveParticles --;
      p_stLastSegmentParticle --;
 
      if ( p_stSegmentParticle != p_stLastSegmentParticle )
      {
        *p_stSegmentParticle = *p_stLastSegmentParticle;
        p_stLastParticlePosition --;
        p_stLastParticlePosition --;
        *p_stParticlePosition = *p_stLastParticlePosition;
        p_stLastParticleLastPosition --;
        p_stLastParticleLastPosition --;
        *p_stParticleLastPosition = *p_stLastParticleLastPosition;
        p_stLastParticleSpeed --;
        *p_stParticleSpeed = *p_stLastParticleSpeed;
      }
    }
  }
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vSourceGenerateSegmentParticle ( PRT_tdstParticlesSystem *_p_stParticlesSystem,
                                             PRT_tdstParticlesSource *_p_stParticlesSource,
                                             POS_tdstCompletePosition *_p_stSourceMatrix,
                                             MTH_tdxReal _xTimeT )
{
  PRT_tdstSegmentParticle *p_stSegmentParticle;
  MTH3D_tdstVector *p_stParticlePosition;
  MTH3D_tdstVector *p_stParticleSpeed;
  long lNbParticlesToInject;
  ACP_tdxIndex xIndex;
 
  /* generation de particules ? */
  if ( PRT_fn_bComputeParticlesGeneration ( _p_stParticlesSource ) )
  {
    lNbParticlesToInject = MTH_M_xRealToLong ( MTH_M_xMul ( MTH_M_xLongToReal ( PRT_fn_xComputeNbParticlesToInject ( _p_stParticlesSource ) ), PRT_g_xNbParticlesFactor ) );
 
    if ( lNbParticlesToInject > 0 )
    {
      if ( _p_stParticlesSystem->xNbActiveParticles + lNbParticlesToInject > _p_stParticlesSystem->xNbParticles )
      {
        lNbParticlesToInject = _p_stParticlesSystem->xNbParticles - _p_stParticlesSystem->xNbActiveParticles;
      }
 
      p_stSegmentParticle = (PRT_tdstSegmentParticle *)(_p_stParticlesSystem->d_stListOfParticles) + _p_stParticlesSystem->xNbActiveParticles;
      p_stParticlePosition = _p_stParticlesSystem->hParticlesSystemObject->d_stListOfPoints + 2*_p_stParticlesSystem->xNbActiveParticles;
      p_stParticleSpeed = _p_stParticlesSystem->d_stSpeed + _p_stParticlesSystem->xNbActiveParticles;
      _p_stParticlesSystem->xNbActiveParticles += (short)lNbParticlesToInject;
 
      for ( xIndex = 0 ; xIndex < lNbParticlesToInject ; xIndex ++ )
      {
        /* reset meca */
        PRT_fn_vGenerateSegmentParticle ( p_stParticlePosition, p_stParticleSpeed,
                                          p_stSegmentParticle, (PRT_tdstSegmentCommonData *)(_p_stParticlesSystem->p_stCommonData),
                                          _p_stParticlesSource, _p_stSourceMatrix, _xTimeT );
 
        p_stSegmentParticle ++;
        p_stParticlePosition ++;
        p_stParticlePosition ++;
        p_stParticleSpeed ++;
      }
    }
  }
}
 
/*
--------------------------------------------------------------------------------
-- Description : 
--------------------------------------------------------------------------------
-- Creation date : 12 jun 1997               Author : FPI
--------------------------------------------------------------------------------
*/
 
void PRT_fn_vPreDrawSegmentParticlesSystem ( PRT_tdstParticlesSystem *_p_stParticlesSystem )
{
  _p_stParticlesSystem->hParticlesSystemObject->xNbPoints = 2*_p_stParticlesSystem->xNbActiveParticles;
  ((GEO_tdstElementLines *)(_p_stParticlesSystem->hParticlesSystemObject->d_stListOfElements[0]))->xNbLines = _p_stParticlesSystem->xNbActiveParticles;
}
 
#endif /* D_THROW_PRT */
