/*##########################################################################################*/
/*## Demos.c :                                                                            ##*/
/*##                                                                                      ##*/
/*## Version              1.0                                                             ##*/
/*## Creation date        16/04/98			Sebastien DAVID			symbol : DVD (Gizmo)  ##*/
/*##                                                                                      ##*/
/*## That file needs to be compatible for all platforms.                                  ##*/
/*##                                                                                      ##*/
/*## (c) Ubi Studios 1998                                                                 ##*/
/*##########################################################################################*/
 
/* ANNECY AV {*/
/*#define GIZ_DEBUG_DEMO_MEMORY*/
/* END ANNECY AV }*/
 
 
#define D_3dData_StructureDefine
#define D_State_Define 
 
 
/*#define D_GIZMO_TEST		/* maj DVD (Gizmo)   */
 
#include <direct.h>
#include "TOOLSCPA.h"
#include "cpa_std.h"
 
/*#include "POS\PosMatri.h"*/
 
#include "Actions/AllActs.h"
 
#include "Structur/ErrGame.h"
#include "Structur/MemGame.h"
#include "STRUCTUR/Objects.h"
#include "STRUCTUR/Anim_s.h"
#include "Structur/StdObjSt.h"
#include "Structur/EngMode.h"
#include "Structur/State.h"
#include "MainChar.h"
#include "Family.h"
#include "PlayAnim/PlayAnim.h"
#include "PlayAnim/PlayEvts.h"
#include "ZeMem.h"
#include "Physicol.h"
#include "PO.h"  
#include "Structur/ObjsTbls.h"
#include "ia_dnm.h"
#include "ToolCam.h"
/* ANNECY AV {*/
/*#include "Commands.h"*/
/* END ANNECY AV }*/
 
#include "ChanList.h"
#include "LipsSync.h"
#include "Basic.h"
/* Add on for father son links management*/
#include "GameEng.h"
#include "Initeng.h"
#include "Always.h"
 
/* ANNECY AV {*/
#include "Specif/Demos.h"
/*#include "POS\PosQuatn.h"*/
/*#include "DspText.h"*/
/*#include "DspFix.h"*/
/*#include "lives.h"*/
/*#include "GamInven.h"*/
#include "MTH.h"
extern BOOL g_bAnimConstantSpeed;
/* END ANNECY AV }*/
 
#include "SCT\Sector.h"
#include "AGO.h"
 
extern void fn_vRegisterTextToDraw (char * _szTextToDraw , long _lX , long _lY);
extern int  IPT_g_bAtLeastOneActionIsValidated;
 
 
void fn_vTreatCharactersInSector(HIE_tdxHandleToSuperObject hCharacter, ACP_tdxBool bUseSector);
 
/* ******************* */
/* variables globales */
/* ******************* */
 
unsigned long gl_DEMO_ConstantSpeed=FALSE;
 
char * gp_DEMO_AllocatedMemory=NULL;
char * gp_DEMO_CurrentPosInMemory=NULL;
unsigned long gl_DEMO_NumberOfFrames=0L;
unsigned long gl_DEMO_CurrentFrame=0L;
unsigned long g_ulDEMO_CurrentTimerCount=0L;
unsigned long g_ulDEMO_StartDEMO_EngineTimerCount;
 
#ifndef RETAIL
    #define D_DEMO_USE_VCR    /* Don't use VCR in the retail version*/
#endif
 
#ifdef D_DEMO_USE_VCR
 
    typedef struct stBeginningOfCharacterFrame_
    {
        long lCurrentIndex;
        long lCurrentIndexForVCR;
    } tdstBeginningOfCharacterFrame;
 
    tdstBeginningOfCharacterFrame * gp_DEMO_BeginningOfCharacterFrame = NULL; /* The position in the gp_DEMO_AllocatedMemory array of the beginning of each character for each frame*/
    char * gp_DEMO_AllocatedMemoryForVCR    = NULL;    /* To save infos which are not in the gp_DEMO_AllocatedMemory block*/
    char * gp_DEMO_CurrentPosInMemoryForVCR = NULL;
    char * gp_DEMO_SaveCurrentPosInMemory   = NULL;
    int    g_b_DEMO_VCRIsUsable             = FALSE;   /* Is the VCR available (if there is no .vcr file, it won't)*/
    int    g_bVCRMode                       = FALSE;   /* Does the user want to be in VCR mode ?*/
    int    g_bVCR_DemoIsPaused              = FALSE;
    int    g_bVCR_ReverseMode               = FALSE;   /* When playing the demo in reverse mode*/
    int    g_bDontTreatCharacter            = FALSE;   /* To NOT treat a character (because it still isn't created in reverse mode)*/
    long   g_lNumberOfCharacterFrame        = 0;
 
    #define M_DEMO_ENTER_VCR_MODE()                                             \
        {                                                                       \
            gp_DEMO_SaveCurrentPosInMemory = gp_DEMO_CurrentPosInMemory;        \
            gp_DEMO_CurrentPosInMemory     = gp_DEMO_CurrentPosInMemoryForVCR;  \
        }
 
    #define M_DEMO_EXIT_VCR_MODE()                                              \
        {                                                                       \
            gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_CurrentPosInMemory;      \
            gp_DEMO_CurrentPosInMemory       = gp_DEMO_SaveCurrentPosInMemory;  \
        }
 
    #define M_DEMO_WRITE_VCR(info,type)                                         \
        {                                                                       \
/*long lTest=0xAAAAAAAA;*/                                                          \
            M_DEMO_ENTER_VCR_MODE ();                                           \
/*M_DEMO_WRITE (& lTest,lTest);*/                                                   \
            M_DEMO_WRITE          (info,type);                                  \
            M_DEMO_EXIT_VCR_MODE  ();                                           \
        }
 
    #define M_DEMO_READ_VCR(info,type)                                          \
        {                                                                       \
/*long lTest;*/                                                                     \
            M_DEMO_ENTER_VCR_MODE ();                                           \
/*M_DEMO_READ (& lTest,lTest);*/                                                    \
/*assert (lTest == 0xAAAAAAAA);*/                                                   \
            M_DEMO_READ           (info,type);                                  \
            M_DEMO_EXIT_VCR_MODE  ();                                           \
        }
#else
 
    #define M_DEMO_WRITE_VCR(a,b)
    #define M_DEMO_READ_VCR(a,b)
 
#endif
 
float gf_CompressionRate0,gf_CompressionRate1,gf_CompressionRate2;
 
DEMO_tdxHandleToDemoSOList	g_hDemoSOList=NULL;
int							g_iFirstAlwaysNumber = 0;
DEMO_tdxHandleToDemoGMTList g_hDemoGMTFixList=NULL;
DEMO_tdxHandleToDemoGMTList g_hDemoGMTLevelList=NULL;
 
/*HIE_tdxHandleToSuperObject gSO_UniverseSector;*/
 
unsigned short g_DEMO_KeybTest=0;
int            g_iNbFramesToWaitBeforePlaying=0;
unsigned long g_ulFlagFrameNumber=0;
unsigned long g_ulNbPOS,g_ulNbALW,g_ulNbSCT,g_ulNbACT,g_ulNbCAN;
 
unsigned short g_DEMO_FirstInitFlag=FALSE;
unsigned short g_DEMO_AutoPlay=FALSE;
 
/*FILE *gp_stFileDEMO_DEBUG;*/
 
int    g_bTestInputDuringDemo            = FALSE;
char   g_szMapNameToLoadWhenDemoFinished [20];
 
#ifndef RETAIL
    char g_szDemoMode    [20];
    char g_szVCRMode     [20];
    char g_szFrameNumber [20];
#endif
 
#define D_DEMO_STATISTIC
 
#ifdef  D_DEMO_STATISTIC
 
    typedef struct DEMO_stStatistic_
    {
        long lNumberOfAllocations;
        long lNumberOfBytes;
    } DEMO_tdstStatistic;
 
    #define  DEMO_C_NumberOfStatistic  22
 
    DEMO_tdstStatistic DEMO_g_a_stStatistic [DEMO_C_NumberOfStatistic];
 
    char * DEMO_g_a_szStatisticName [DEMO_C_NumberOfStatistic] =
        { "General flag" , "Actor number" , "Compressed actor number" , "Frame number" ,
          "Complete translation" , "Delta translation" ,
          "Complete rotation" , "Delta rotation" , "State change" , "Frame change" ,
          "Transparency change" , "Activate always" , "Desactivate always" ,
          "Activate / desactivate canal" , "Activation / Desactivation" ,
          "Debug" , "Change viewport size" , "Set module transparency value" ,
          "Activate module transparency" , "Fix values change" , "Sector change" ,
          "Move module" };
 
    #define DEMO_M_AddStatistic(lType,lSize)                                \
        {                                                                   \
            DEMO_g_a_stStatistic [lType] . lNumberOfAllocations ++;         \
            DEMO_g_a_stStatistic [lType] . lNumberOfBytes += lSize;         \
        }
 
#else
 
    #define DEMO_M_AddStatistic(a,b)
 
#endif
 
#define  DEMO_C_STAT_GeneralFlag                  0
#define  DEMO_C_STAT_ActorNumber                  1
#define  DEMO_C_STAT_CompressedActorNumber        2
#define  DEMO_C_STAT_FrameNumber                  3
#define  DEMO_C_STAT_CompleteTranslation          4
#define  DEMO_C_STAT_DeltaTranslation             5
#define  DEMO_C_STAT_CompleteRotation             6
#define  DEMO_C_STAT_DeltaRotation                7
#define  DEMO_C_STAT_StateChange                  8
#define  DEMO_C_STAT_FrameChange                  9
#define  DEMO_C_STAT_TransparencyChange          10
 
#define  DEMO_C_STAT_ActivateAlways              11
#define  DEMO_C_STAT_DesactivateAlways           12
#define  DEMO_C_STAT_ActivateDesactivateCanal    13
#define  DEMO_C_STAT_ActivationDesactivation     14
#define  DEMO_C_STAT_Debug                       15
#define  DEMO_C_STAT_ChangeViewportSize          16
#define  DEMO_C_STAT_SetModuleTransparencyValue  17
#define  DEMO_C_STAT_ActivateModuleTransparency  18
#define  DEMO_C_STAT_FixValuesChange             19
#define  DEMO_C_STAT_SectorChange                20
#define  DEMO_C_STAT_MoveModule                  21
#define  DEMO_C_STAT_GenerateAGO				 22
	
/* ANNECY AV {*/
 
/* Compression of a float into a short : if -2 < x < 2 ==> store x * 4096 : -8192 <= result <= 8192*/
/*                                       if |x| >= 2   ==> store 16384 + x * 128 : 8704 <= result <= 24064 (one must have x <= 60)*/
INLINE
short fn_sCompressNumber (float x)
{
    return (short) (fabs (x) < 2.0f ? x * 4096 : 16384.0f + x * 128.0f);
}
 
INLINE
float fn_fUncompressNumber (short n)
{
    return n < 8700 ? ((float) n) / 4096.0f : ((float) (n - 16384)) / 128.0f;
}
 
/*******************************************************************************/
/**  fn_vCompressMatrix                                                       **/
/**  Compress a 3x3 float matrix into a 9 short array                         **/
/*******************************************************************************/
void fn_vCompressMatrix (tdstDemoCompressedMatrix * _p_stCompressedMatrix , MTH3D_tdstMatrix * _p_stMatrix)
{
    short            * p = _p_stCompressedMatrix -> a9_sCoef;
    MTH3D_tdstVector * p_stVector;
 
    p_stVector = & _p_stMatrix -> stCol_0;
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetXofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetYofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetZofVector (p_stVector));
 
    p_stVector = & _p_stMatrix -> stCol_1;
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetXofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetYofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetZofVector (p_stVector));
 
    p_stVector = & _p_stMatrix -> stCol_2;
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetXofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetYofVector (p_stVector));
    * (p ++) = fn_sCompressNumber (MTH3D_M_xGetZofVector (p_stVector));
}
 
/*******************************************************************************/
/**  fn_bCompressedMatrixTestIfEqual                                          **/
/**  Test if two compressed matrixes are equal                                **/
/*******************************************************************************/
int fn_bCompressedMatrixTestIfEqual (tdstDemoCompressedMatrix * _p_stMatrix1 , tdstDemoCompressedMatrix * _p_stMatrix2)
{
    return memcmp (_p_stMatrix1 , _p_stMatrix2 , sizeof (tdstDemoCompressedMatrix)) == 0;
}
 
/*******************************************************************************/
/**  fn_vDecompressMatrix                                                     **/
/**  Decompress a matrix                                                      **/
/*******************************************************************************/
void fn_vDecompressMatrix (MTH3D_tdstMatrix * _p_stMatrix , tdstDemoCompressedMatrix * _p_stCompressedMatrix)
{
    short            * p = _p_stCompressedMatrix -> a9_sCoef;
    MTH3D_tdstVector * p_stVector;
 
    p_stVector = & _p_stMatrix -> stCol_0;
    MTH3D_M_vSetVectorElements (p_stVector , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)));
    p_stVector = & _p_stMatrix -> stCol_1;
    MTH3D_M_vSetVectorElements (p_stVector , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)));
    p_stVector = & _p_stMatrix -> stCol_2;
    MTH3D_M_vSetVectorElements (p_stVector , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)) , fn_fUncompressNumber (* (p ++)));
}
 
/*******************************************************************************/
/**  fn_vComputeRotationMatrixFromTransformMatrix                             **/
/**                                                                           **/
/*******************************************************************************/
void fn_vComputeRotationMatrixFromTransformMatrix (MTH3D_tdstMatrix * _p_stRotationMatrix , MTH3D_tdstMatrix * _p_stTransformMatrix)
{
    MTH_tdxReal xInvertScaleX , xInvertScaleY , xInvertScaleZ;
 
    /* ??? In reverse mode, it is possible to have a null matrix*/
    if (_p_stTransformMatrix -> stCol_0 . xX == 0.0 && _p_stTransformMatrix -> stCol_0 . xY == 0.0 && _p_stTransformMatrix -> stCol_0 . xZ == 0.0)
        MTH3D_M_vSetIdentityMatrix (_p_stTransformMatrix);
 
    xInvertScaleX = MTH_M_xInvSqrt (MTH_M_xAdd (MTH_M_xAdd (MTH_M_xSqr (_p_stTransformMatrix -> stCol_0.xX) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_1.xX)) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_2.xX)));
 
    xInvertScaleY = MTH_M_xInvSqrt (MTH_M_xAdd (MTH_M_xAdd (MTH_M_xSqr (_p_stTransformMatrix -> stCol_0.xY) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_1.xY)) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_2.xY)));
 
    xInvertScaleZ = MTH_M_xInvSqrt (MTH_M_xAdd (MTH_M_xAdd (MTH_M_xSqr (_p_stTransformMatrix -> stCol_0.xZ) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_1.xZ)) ,
                                                            MTH_M_xSqr (_p_stTransformMatrix -> stCol_2.xZ)));
 
    _p_stRotationMatrix -> stCol_0 . xX = MTH_M_xMul (_p_stTransformMatrix -> stCol_0 . xX , xInvertScaleX);
    _p_stRotationMatrix -> stCol_0 . xY = MTH_M_xMul (_p_stTransformMatrix -> stCol_0 . xY , xInvertScaleY);
    _p_stRotationMatrix -> stCol_0 . xZ = MTH_M_xMul (_p_stTransformMatrix -> stCol_0 . xZ , xInvertScaleZ);
    _p_stRotationMatrix -> stCol_1 . xX = MTH_M_xMul (_p_stTransformMatrix -> stCol_1 . xX , xInvertScaleX);
    _p_stRotationMatrix -> stCol_1 . xY = MTH_M_xMul (_p_stTransformMatrix -> stCol_1 . xY , xInvertScaleY);
    _p_stRotationMatrix -> stCol_1 . xZ = MTH_M_xMul (_p_stTransformMatrix -> stCol_1 . xZ , xInvertScaleZ);
    _p_stRotationMatrix -> stCol_2 . xX = MTH_M_xMul (_p_stTransformMatrix -> stCol_2 . xX , xInvertScaleX);
    _p_stRotationMatrix -> stCol_2 . xY = MTH_M_xMul (_p_stTransformMatrix -> stCol_2 . xY , xInvertScaleY);
    _p_stRotationMatrix -> stCol_2 . xZ = MTH_M_xMul (_p_stTransformMatrix -> stCol_2 . xZ , xInvertScaleZ);
}
 
/*******************************************************************************/
/**  fn_bCanUltraCompressMatrix                                               **/
/**                                                                           **/
/*******************************************************************************/
int fn_bCanUltraCompressMatrix (tdstDemoCompressedMatrix * p_stMatrixToCompress , tdstDemoCompressedMatrix * p_stReferenceMatrix ,
                                tdstDemoUltraCompressedMatrix * p_stUltraCompressedMatrix)
{
    int     i , j;
    short * p , * r;
 
    p = p_stMatrixToCompress -> a9_sCoef;
    r = p_stReferenceMatrix  -> a9_sCoef;
 
/*
    // To be ultra-compressed, the matrix must be antisymetric :
    if (p [1] != - p [3] || r [1] != - r [3])  return FALSE;
    if (p [2] != - p [6] || r [2] != - r [6])  return FALSE;
    if (p [5] != - p [7] || r [5] != - r [7])  return FALSE;
*/
 
    for (i = 0 , j = 0 ; i < 9 ; i ++ , p ++ , r ++)
    {
/*        if (i == 3 || i == 6 || i == 7)  continue;    // coef 3, 6 and 7 are unnecessary*/
 
        if (abs (* p) >= 8192 || abs (* r) >= 8192)  return FALSE;
        if (abs (* p - * r) >= 128)                  return FALSE;
 
        p_stUltraCompressedMatrix -> a9_cCoef [j ++] = (char) (* p - * r);
    }
 
    return TRUE;
}
 
/*******************************************************************************/
/**  fn_vUltraDecompressMatrix                                                **/
/**                                                                           **/
/*******************************************************************************/
void fn_vUltraDecompressMatrix (MTH3D_tdstMatrix * p_stMatrix , tdstDemoUltraCompressedMatrix * p_stUltraCompressedMatrix ,
                                tdstDemoCompressedMatrix * p_stReferenceMatrix)
{
    int     i;
    char  * p = p_stUltraCompressedMatrix -> a9_cCoef;
    short * r = p_stReferenceMatrix -> a9_sCoef;
 
    for (i = 0 ; i < 9 ; i ++ , r ++)
    {
/*
        if (i == 3)  * r = (short) - r [-2];    // coef 3 = - coef 1
        else
        if (i == 6)  * r = (short) - r [-4];    // coef 6 = - coef 2
        else
        if (i == 7)  * r = (short) - r [-2];    // coef 7 = - coef 5
        else
*/
#pragma warning (disable:4244)
               * r += (short) * (p ++);
#pragma warning (default:4244)
    }
 
    fn_vDecompressMatrix (p_stMatrix , p_stReferenceMatrix);
}
 
/*******************************************************************************/
/**  fn_bCanCompressVector                                                    **/
/**                                                                           **/
/*******************************************************************************/
int fn_bCanCompressVector (MTH3D_tdstVector * _p_stVector , MTH3D_tdstVector * _p_stPreviousVector)
{
    MTH3D_tdstVector stDelta;
 
    MTH3D_M_vSubVector (& stDelta , _p_stVector , _p_stPreviousVector);
 
    return fabs (stDelta . xX * 128.0) <= 127 &&
           fabs (stDelta . xY * 128.0) <= 127 &&
           fabs (stDelta . xZ * 128.0) <= 127;
}
 
/*******************************************************************************/
/**  fn_vCompressVector                                                       **/
/**                                                                           **/
/*******************************************************************************/
void fn_vCompressVector (char * _p_cCompressedVector , MTH3D_tdstVector * _p_stVector , MTH3D_tdstVector * _p_stPreviousVector)
{
    MTH3D_tdstVector stDelta;
 
    MTH3D_M_vSubVector (& stDelta , _p_stVector , _p_stPreviousVector);
 
    if (stDelta . xX >= 0)
         _p_cCompressedVector [0] =          (char) (  stDelta . xX * 128.0 + 0.5);
    else _p_cCompressedVector [0] = (char) - (char) (- stDelta . xX * 128.0 + 0.5);
 
    if (stDelta . xY >= 0)
         _p_cCompressedVector [1] =          (char) (  stDelta . xY * 128.0 + 0.5);
    else _p_cCompressedVector [1] = (char) - (char) (- stDelta . xY * 128.0 + 0.5);
 
    if (stDelta . xZ >= 0)
         _p_cCompressedVector [2] =          (char) (  stDelta . xZ * 128.0 + 0.5);
    else _p_cCompressedVector [2] = (char) - (char) (- stDelta . xZ * 128.0 + 0.5);
}
 
/*******************************************************************************/
/**  fn_vDecompressVector                                                     **/
/**                                                                           **/
/*******************************************************************************/
void fn_vDecompressVector (MTH3D_tdstVector * _p_stVector , char * _p_cCompressedVector , MTH3D_tdstVector * _p_stPreviousVector)
{
    _p_stVector -> xX = _p_stPreviousVector -> xX + _p_cCompressedVector [0] / 128.0f;
    _p_stVector -> xY = _p_stPreviousVector -> xY + _p_cCompressedVector [1] / 128.0f;
    _p_stVector -> xZ = _p_stPreviousVector -> xZ + _p_cCompressedVector [2] / 128.0f;
}
 
#ifdef D_DEMO_USE_VCR
 
/*******************************************************************************/
/**  fn_vSaveCurrentPositionForVCR                                            **/
/**                                                                           **/
/*******************************************************************************/
void fn_vSaveCurrentPositionForVCR (void)
{
    gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndex       = gp_DEMO_CurrentPosInMemory       - gp_DEMO_AllocatedMemory;
    gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndexForVCR = gp_DEMO_CurrentPosInMemoryForVCR - gp_DEMO_AllocatedMemoryForVCR;
    g_lNumberOfCharacterFrame ++;
}
 
#endif
/* END ANNECY AV }*/
 
 
/*            ######################################################################## */
/*        ############################################################################ */
/*      ########################## MANAGMENT of Demo Memory ########################## */
/*     ############################################################################### */
/*     ############################################################################### */
/*     #########                                                                       */
/* #################                                                                   */
/*   #############                                                                     */
/*     #########                                                                       */
/*       #####                                                                         */
/*         #                                                                           */
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vAllocateMemory(void)
{
#define DEMO_ALLOCATED_MEMORY 2000L    /* 2000 Ko to store the demo*/
 
  gp_DEMO_AllocatedMemory = (char *)TMP_M_p_Malloc(DEMO_ALLOCATED_MEMORY*1024L);
  if(gp_DEMO_AllocatedMemory == NULL)
  {
  	__asm int 3h
  }
 
#ifdef D_DEMO_USE_VCR
    #define DEMO_MAX_NB_CHARACTER_FRAME  250000L
    gp_DEMO_BeginningOfCharacterFrame = (tdstBeginningOfCharacterFrame *) TMP_M_p_Malloc (DEMO_MAX_NB_CHARACTER_FRAME * sizeof (tdstBeginningOfCharacterFrame));
    if (gp_DEMO_BeginningOfCharacterFrame == NULL)
    {
        __asm int 3h
    }
 
    #define DEMO_ALLOCATED_MEMORY_FOR_VCR  1000L    /* 1000 Ko to store informations for VCR*/
    gp_DEMO_AllocatedMemoryForVCR = (char *) TMP_M_p_Malloc (DEMO_ALLOCATED_MEMORY_FOR_VCR*1024L);
    if (gp_DEMO_AllocatedMemoryForVCR == NULL)
    {
        __asm int 3h
    }
#endif
 
  * g_szMapNameToLoadWhenDemoFinished = 0;
 
#if !defined(RETAIL) && !defined(PRESS_DEMO)
    * g_szDemoMode    = 0;
    * g_szVCRMode     = 0;
    * g_szFrameNumber = 0;
  
    fn_vRegisterTextToDraw (g_szDemoMode    , 400 , 0);
    fn_vRegisterTextToDraw (g_szVCRMode     , 400 , 16);
    fn_vRegisterTextToDraw (g_szFrameNumber , 400 , 32);
#endif
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vPrepareForReadNow (void)
{
/*  gSO_UniverseSector = HIE_fn_hGetChildNumber(SECT_hFatherSector,HIE_fn_lGetNumberOfChildren(SECT_hFatherSector)-1);*/
 
  gp_DEMO_CurrentPosInMemory = gp_DEMO_AllocatedMemory;
  gl_DEMO_CurrentFrame       = 0;
#ifdef D_DEMO_USE_VCR
  gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_AllocatedMemoryForVCR;
  g_lNumberOfCharacterFrame        = 0;
#endif
/*  fn_vAddCommandLineAnswerWithScroll("PLAY DEMO");*/
/*  OutputDebugString("PLAY DEMO\n");*/
#ifndef RETAIL
    strcpy (g_szDemoMode , "PLAY");
#endif
/*  gp_stFileDEMO_DEBUG = fopen("DEMO_DBG.LOG","at"); */
  g_ulDEMO_CurrentTimerCount =0;
  g_ulDEMO_StartDEMO_EngineTimerCount = g_stEngineStructure.stEngineTimer.ulCurrentTimerCount;
}
 
void DEMO_fn_vPrepareForRead (int _iNbFramesToWait)
{
    g_DEMO_KeybTest = DEMO_PLAY;
    if (_iNbFramesToWait == 0)  DEMO_fn_vPrepareForReadNow ();
    else g_iNbFramesToWaitBeforePlaying = _iNbFramesToWait;
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vPrepareForWrite(void)
{
  g_DEMO_KeybTest = DEMO_RECORD;
/*  gSO_UniverseSector = HIE_fn_hGetChildNumber(SECT_hFatherSector,HIE_fn_lGetNumberOfChildren(SECT_hFatherSector)-1);*/
  gp_DEMO_CurrentPosInMemory = gp_DEMO_AllocatedMemory;
#ifdef D_DEMO_USE_VCR
  gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_AllocatedMemoryForVCR;
  g_b_DEMO_VCRIsUsable             = TRUE;
  g_lNumberOfCharacterFrame        = 0L;
#endif
  gl_DEMO_NumberOfFrames    = 0L;
  g_DEMO_FirstInitFlag=TRUE;
  DEMO_fn_vReInitSOList();
/*  fn_vAddCommandLineAnswerWithScroll("RECORD DEMO");*/
#ifndef RETAIL
  strcpy (g_szDemoMode , "RECORD");
#endif
 
#ifdef  D_DEMO_STATISTIC
  memset (DEMO_g_a_stStatistic , 0 , sizeof (DEMO_g_a_stStatistic));
#endif
 
/*  OutputDebugString("RECORD DEMO\n");*/
  
  g_ulNbPOS=0;  g_ulNbALW=0;  g_ulNbSCT=0;  g_ulNbACT=0;  g_ulNbCAN=0;
  g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vCloseFirstInit(void)
{
  g_DEMO_FirstInitFlag=FALSE;
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveToFile(void)
{
  FILE *p_xFile;
/* ANNECY AV {*/
  char szFilename[_MAX_PATH]/*,szMessage[80]*/;
/* END ANNECY AV }*/
  long i;
  long lSubMapNumber;
  char *p_cFilename;
 
  lSubMapNumber = GAM_M_GetSubMap(fn_lGetSubMapNumber());
  if(lSubMapNumber == 0)
  {
	  sprintf (szFilename , "Demos\\%s" , fn_p_szGetLevelName());
  }
  else
  {
	  sprintf (szFilename , "Demos\\%s\\%02d" , fn_p_szGetLevelName() , lSubMapNumber);
  }
  p_cFilename = szFilename + strlen (szFilename);
 
  FIL_fn_bValidatePath(szFilename,"Demo000.sav");
/*  mkdir("Demos");*/
 
  
  sprintf(p_cFilename,"\\Demo%03d.sav",0);
  for (i=0;i<100&&FIL_fn_bIsFileExist(szFilename);i++)
  {
   sprintf(p_cFilename,"\\Demo%03d.sav",i+1);
  }
 
/*
  i=0;
  do
  {
    sprintf(szFilename,"Demos\\demo%03d.sav",i);
    p_xFile=fopen(szFilename,"rb");
    i++;
  }
  while ( p_xFile!=NULL && i<100);
*/
 
  if(i<100)
  {
    unsigned long Length = (unsigned long)gp_DEMO_CurrentPosInMemory-(unsigned long)gp_DEMO_AllocatedMemory;
 
    p_xFile=fopen(szFilename,"wb");
    if(p_xFile!=NULL)
    {
      fwrite(&gl_DEMO_NumberOfFrames,sizeof(unsigned long),1,p_xFile);
      fwrite(&Length,sizeof(unsigned long),1,p_xFile);
	     fwrite(gp_DEMO_AllocatedMemory,Length,1,p_xFile);
	     fclose(p_xFile);
/* ANNECY AV {*/
/*      sprintf(szMessage,"SAVE DEMO  in Demos\\demo%03d.sav",i);*/
/*      fn_vAddCommandLineAnswerWithScroll(szMessage);*/
    }
/*    else fn_vAddCommandLineAnswerWithScroll("FILE ERROR IN SAVING DEMO");*/
  }
/*  else fn_vAddCommandLineAnswerWithScroll("!!! DEMO NOT SAVED !!!  NO MORE PLACE");*/
/* END ANNECY AV }*/
 
#ifdef D_DEMO_USE_VCR
  strncpy (szFilename + strlen (szFilename) - 3 , "vcr" , 3);
  p_xFile = fopen (szFilename , "wb");
  if (p_xFile)
  {
      unsigned long Length;
      fwrite (& g_lNumberOfCharacterFrame , sizeof (g_lNumberOfCharacterFrame) , 1 , p_xFile);
      fwrite (gp_DEMO_BeginningOfCharacterFrame , g_lNumberOfCharacterFrame , sizeof (tdstBeginningOfCharacterFrame) , p_xFile);
      Length = (unsigned long) gp_DEMO_CurrentPosInMemoryForVCR - (unsigned long) gp_DEMO_AllocatedMemoryForVCR;
      fwrite(&Length,sizeof(unsigned long),1,p_xFile);
      if (Length)
          fwrite (gp_DEMO_AllocatedMemoryForVCR,Length,1,p_xFile);
      fclose (p_xFile);
  }
#endif
 
#ifndef RETAIL
    strcpy (g_szDemoMode , "");
#endif
 
#ifdef D_DEMO_STATISTIC
  {
      int                  i;
      DEMO_tdstStatistic * p;
      long                 lTotalSize = 0;
      FILE *               pFile = fopen ("Demos.log" , "wt");
      if (! pFile)  return;
 
      for (i = 0 , p = DEMO_g_a_stStatistic ; i < DEMO_C_NumberOfStatistic ; i ++ , p ++)  
      {
          lTotalSize += p -> lNumberOfBytes;
      }
 
      fprintf (pFile , "Total size : %i\n\n" , lTotalSize);
 
      for (i = 0 , p = DEMO_g_a_stStatistic ; i < DEMO_C_NumberOfStatistic ; i ++ , p ++)
      {
          fprintf (pFile , "%35s : %4i blocs %6i bytes - %2i %% (1 bloc = %2i bytes)\n" , DEMO_g_a_szStatisticName [i] , p -> lNumberOfAllocations ,
                   p -> lNumberOfBytes , (p -> lNumberOfBytes * 100 + lTotalSize / 2) / lTotalSize , p -> lNumberOfBytes / max (p -> lNumberOfAllocations , 1));
      }
 
      fclose (pFile);
  }
#endif
}
#endif /* RETAIL */
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vLoadFromFile(char *szFileName)
{
  FILE *p_xFile;
 
  p_xFile=fopen(szFileName,"rb");
  if(p_xFile!=NULL)
  {
      unsigned long Length;
      fread(&gl_DEMO_NumberOfFrames,sizeof(long),1,p_xFile);
      fread(&Length,sizeof(unsigned long),1,p_xFile);
	  fread(gp_DEMO_AllocatedMemory,Length,1,p_xFile);
	  fclose(p_xFile);
/*      fn_vAddCommandLineAnswerWithScroll("LOAD DEMO");*/
/*  else fn_vAddCommandLineAnswerWithScroll("DEMO NOT LOADED  --- FILE NOT FOUND");*/
 
#ifdef D_DEMO_USE_VCR
    {
      char szVCRFileName [256];
      strcpy (szVCRFileName , szFileName);
      strncpy (szVCRFileName + strlen (szVCRFileName) - 3 , "vcr" , 3);
 
      g_b_DEMO_VCRIsUsable = FALSE;
      p_xFile = fopen (szVCRFileName , "rb");
      if (p_xFile)
      {
          if (fread (& g_lNumberOfCharacterFrame , sizeof (g_lNumberOfCharacterFrame) , 1 , p_xFile))
              if (g_lNumberOfCharacterFrame)
                  if (fread (gp_DEMO_BeginningOfCharacterFrame , sizeof (tdstBeginningOfCharacterFrame) ,
                             g_lNumberOfCharacterFrame , p_xFile) == (unsigned int) g_lNumberOfCharacterFrame)
                      if (fread (&Length,sizeof(unsigned long),1,p_xFile))
                          if (fread (gp_DEMO_AllocatedMemoryForVCR,1,Length,p_xFile) == Length)
                              g_b_DEMO_VCRIsUsable = TRUE;
          fclose (p_xFile);
      }
    }
#endif
  }
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vAutoPlayDemoFromFile(char *szFileName)
{
  DEMO_fn_vLoadFromFile(szFileName);
  DEMO_fn_vPrepareForRead(0);
  g_DEMO_AutoPlay=TRUE;
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vStopAutoPlay(void)
{
  g_DEMO_AutoPlay = FALSE;
  /*DFM changeEngineFlag*/
	 fn_vChangeEngineMode(EM_ModeStoppingProgram);
}
 
 
void DEMO_fn_vSwitchVCRMode ()
{
#ifdef D_DEMO_USE_VCR
    if (g_DEMO_KeybTest == DEMO_PLAY)    /* One can change the VCR mode only when playing the demo*/
    {
        g_bVCRMode = ! g_bVCRMode;
        g_bVCR_DemoIsPaused = FALSE;
    }
#endif
}
 
void DEMO_fn_vSetTestInputDuringDemo (int _bTestInput)
{
    g_bTestInputDuringDemo = _bTestInput;
}
 
void DEMO_fn_vSetMapToLoadWhenDemoFinished (char * _szMapName)
{
    strncpy (g_szMapNameToLoadWhenDemoFinished , _szMapName , 20);
    g_szMapNameToLoadWhenDemoFinished [19] = 0;
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vTransformPositionInfos(HIE_tdxHandleToSuperObject hSO)
{
 
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL

void DEMO_fn_vSaveAPositionInMemory(HIE_tdxHandleToSuperObject hSO)
{
}
#endif /* RETAIL */

/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/****WRITE SECTION*******************************************************************************************************************/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
/* ANNECY AV {*/
/*
void DEMO_fn_vSaveSuperImposedTextActivationDesactivation(int iIndex,FON_tdxHandleOfString hText,FON_tdxHandleOfFont hFont,MTH_tdxReal xPosX,MTH_tdxReal xPosY,long lLiveTime,int Activate)
{
  DEMO_FLAG_CAST  Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_EXTENDED;
  
  char Tmp_iIndex = (char)iIndex;
  char Tmp_hText = (char)hText;
  char Tmp_lLiveTime = (char)lLiveTime;
  char Tmp_hFont;
 
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
 
 
  Tmp_hFont = (char)FON_fn_wGetFontNumberInList(hFont);
 
  if(Activate) Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_ACTIVE_DESACTIVE;
 
  Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_SUPERIMPOSED_TEXT;
 
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {// debug
    long L=0xABABABAB;
    M_DEMO_WRITE(&L,long);
  }// debug
  #endif
 
  M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_WRITE(&Tmp_iIndex,char);
  if(Activate)
  {
    M_DEMO_WRITE(&Tmp_hText,char);
    M_DEMO_WRITE(&Tmp_hFont,char);
    M_DEMO_WRITE(&xPosX,MTH_tdxReal);
    M_DEMO_WRITE(&xPosY,MTH_tdxReal);
    M_DEMO_WRITE(&Tmp_lLiveTime,char);
  }  
}
*/
/* END ANNECY AV }*/
 
/* ANNECY AV {*/
#ifndef RETAIL
void DEMO_fn_vSaveChangeViewportSize (unsigned char _ucParam1 , unsigned char _ucParam2 , unsigned char _ucParam3 ,
                                      unsigned char _ucParam4 , unsigned char _ucParam5 , unsigned char _ucParam6 ,
                                      unsigned char _ucParam7 , unsigned char _ucParam8 , unsigned char _ucParam9 , unsigned char _ucParam10)
{
    DEMO_FLAG_CAST  Demo_Flag;
    unsigned char a10_ucParam [10];
 
    if(g_DEMO_KeybTest != DEMO_RECORD)  return;
 
    Demo_Flag = (DEMO_FLAG_CAST) (DEMO_FLAG_EXTENDED | DEMO_FLAG_CHANGE_VIEWPORT_SIZE);
 
    a10_ucParam [0] = _ucParam1;
    a10_ucParam [1] = _ucParam2;
    a10_ucParam [2] = _ucParam3;
    a10_ucParam [3] = _ucParam4;
    a10_ucParam [4] = _ucParam5;
    a10_ucParam [5] = _ucParam6;
    a10_ucParam [6] = _ucParam7;
    a10_ucParam [7] = _ucParam8;
    a10_ucParam [8] = _ucParam9;
    a10_ucParam [9] = _ucParam10;
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_ChangeViewportSize , sizeof (DEMO_FLAG_CAST) + sizeof (a10_ucParam));
    M_DEMO_WRITE (& Demo_Flag , DEMO_FLAG_CAST);
    M_DEMO_WRITE (a10_ucParam , a10_ucParam);
}
#endif /* RETAIL */


#ifndef REATAIL 
void DEMO_fn_vSaveModuleTransparencyValue (HIE_tdxHandleToSuperObject hSO , unsigned char ucChannelNumber , MTH_tdxReal xTransparencyValue)
{
    DEMO_FLAG_CAST  Demo_Flag;
    unsigned short  wSO_Number;
    unsigned char   ucTransparencyValue;
 
    if (g_DEMO_KeybTest != DEMO_RECORD)  return;
 
    Demo_Flag           = (DEMO_FLAG_CAST) (DEMO_FLAG_EXTENDED | DEMO_FLAG_MODULE_TRANSPARENCY);
    wSO_Number          = DEMO_fn_wGetNumberFromSO (hSO);  
    ucTransparencyValue = (unsigned char) MTH_M_xRealToLong (xTransparencyValue);
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_SetModuleTransparencyValue , sizeof (DEMO_FLAG_CAST) + 4);
    M_DEMO_WRITE (& Demo_Flag           , DEMO_FLAG_CAST);
    M_DEMO_WRITE (& wSO_Number          , unsigned short);
    M_DEMO_WRITE (& ucChannelNumber     , unsigned char);
    M_DEMO_WRITE (& ucTransparencyValue , unsigned char);
}
#endif /* RETAIL */


#ifndef RETAIL 
void DEMO_fn_vSaveActivateModuleTransparency (HIE_tdxHandleToSuperObject hSO , unsigned char ucChannelNumber , int bTransparencyActive)
{
    DEMO_FLAG_CAST  Demo_Flag;
    unsigned short  wSO_Number;
 
    if (g_DEMO_KeybTest != DEMO_RECORD)  return;
 
    Demo_Flag           = (DEMO_FLAG_CAST) (bTransparencyActive ? DEMO_FLAG_EXTENDED | DEMO_FLAG_ACTIVATE_TRANSPARENCY
                                                                : DEMO_FLAG_EXTENDED | DEMO_FLAG_DESACTIVATE_TRANSPARENCY);
    wSO_Number          = DEMO_fn_wGetNumberFromSO (hSO);
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_ActivateModuleTransparency , sizeof (DEMO_FLAG_CAST) + 3);
    M_DEMO_WRITE (& Demo_Flag       , DEMO_FLAG_CAST);
    M_DEMO_WRITE (& wSO_Number      , unsigned short);
    M_DEMO_WRITE (& ucChannelNumber , unsigned char);
}
#endif /* RETAIL */

#ifndef RETAIL 
void DEMO_fn_vSaveModuleMove (HIE_tdxHandleToSuperObject hSO , unsigned char ucChannelNumber , int bSaveTransformMatrix)
{
    DEMO_FLAG_CAST             Demo_Flag;
    unsigned short             wSO_Number;
    HIE_tdxHandleToSuperObject hModuleSO;
/*    POS_tdstCompletePosition * hModuleGlobalMatrix;*/
    POS_tdstCompletePosition * hModuleLocaleMatrix;
    tdstDemoCompressedMatrix   stCompressedMatrix;
 
    if (g_DEMO_KeybTest != DEMO_RECORD)  return;
 
    hModuleSO           = fn_hGetSuperObjectInChannel (fn_h3dDataGetChannelSOList (M_GetMSHandle (hSO , 3dData)) , ucChannelNumber);
/*    hModuleGlobalMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (hModuleSO);*/
/*    fn_vCompressMatrix (& stCompressedMatrix , & hModuleGlobalMatrix -> stTransformMatrix);*/
    hModuleLocaleMatrix = HIE_fn_hGetSuperObjectMatrix (hModuleSO);
    if (bSaveTransformMatrix)
        fn_vCompressMatrix (& stCompressedMatrix , & hModuleLocaleMatrix -> stTransformMatrix);
 
    Demo_Flag  = (DEMO_FLAG_CAST) (DEMO_FLAG_EXTENDED | DEMO_FLAG_MOVE_MODULE);
    if (bSaveTransformMatrix)  Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_ROTATE_MODULE;
    wSO_Number = DEMO_fn_wGetNumberFromSO (hSO);
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_MoveModule , sizeof (DEMO_FLAG_CAST) + 3 + sizeof (MTH3D_tdstVector) +
                                                  (bSaveTransformMatrix ? sizeof (tdstDemoCompressedMatrix) : 0));
    M_DEMO_WRITE (& Demo_Flag          , DEMO_FLAG_CAST);
    M_DEMO_WRITE (& wSO_Number         , unsigned short);
    M_DEMO_WRITE (& ucChannelNumber    , unsigned char);
/*    M_DEMO_WRITE (& hModuleGlobalMatrix -> stTranslationVector , MTH3D_tdstVector);*/
    M_DEMO_WRITE (& hModuleLocaleMatrix -> stTranslationVector , MTH3D_tdstVector);
    if (bSaveTransformMatrix)  M_DEMO_WRITE (& stCompressedMatrix , tdstDemoCompressedMatrix);
}
#endif /* RETAIL */
/* END ANNECY AV }*/
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveFixValuesChange(unsigned char FieldFlag,unsigned char ucNewValue)
{
  DEMO_FLAG_CAST  Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_EXTENDED;
 
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
 
  Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_FIX_CHANGE;
 
#ifdef D_DEMO_USE_VCR
  fn_vSaveCurrentPositionForVCR ();
#endif
 
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L=0xCBCBCBCB;
    DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
    M_DEMO_WRITE(&L,long);
  }/* debug*/
  #endif
 
  DEMO_M_AddStatistic (DEMO_C_STAT_FixValuesChange , sizeof (DEMO_FLAG_CAST) + 2);
  M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_WRITE(&FieldFlag,unsigned char);
  M_DEMO_WRITE(&ucNewValue,unsigned char);
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveAlwaysActivationDesactivation(HIE_tdxHandleToSuperObject hSO,MTH3D_tdstVector * p_stTempVector,int Activate)
{
	tdstDemoSOList *p_stListElement;
	unsigned short wSO_Number=0;
	DEMO_FLAG_CAST  Demo_Flag=0;
	tdObjectType otObjectModelType;
	
	if(g_DEMO_KeybTest != DEMO_RECORD) return;
	
    p_stListElement = DEMO_fn_p_stGetInfosPointerFromSO(hSO);
    if(p_stListElement->ucActivate == Activate ) return;
	p_stListElement->ucActivate = (unsigned char)Activate;

	wSO_Number = DEMO_fn_wGetNumberFromSO(hSO);  
	otObjectModelType = M_ObjectGetModelType(M_GetEngineObject(hSO));
	
	if(Activate) 
		Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_ACTIVATE;
	else
	{
		Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_DESACTIVATE;
		p_stListElement->ucReInit = TRUE;
	}
	
	Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_IS_AN_ALWAYS;
	
#ifdef D_DEMO_USE_VCR
	fn_vSaveCurrentPositionForVCR ();
#endif
	
#ifdef GIZ_DEBUG_DEMO_MEMORY
	{/* debug*/
		long L=0xDDDDDDDD;
		DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
		M_DEMO_WRITE(&L,long);
	}/* debug*/
#endif
	
	DEMO_M_AddStatistic (Activate ? DEMO_C_STAT_ActivateAlways : DEMO_C_STAT_DesactivateAlways ,
		sizeof (DEMO_FLAG_CAST) + 2 + (Activate ? sizeof (tdObjectType) + sizeof (MTH3D_tdstVector) : 0));
	M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
	M_DEMO_WRITE(&wSO_Number,unsigned short);
	g_ulNbALW++;
	if(Activate)
	{
		M_DEMO_WRITE(&otObjectModelType,tdObjectType);
		M_DEMO_WRITE(&p_stTempVector,MTH3D_tdstVector);
	}
#ifdef D_DEMO_USE_VCR
	if (! Activate)
	{
		MS_tdxHandleTo3dData  h_Current3dData   = M_GetMSHandle(hSO,3dData);
		struct tdstState_ *  p_stCurrentState   = h_Current3dData -> h_CurrentState;
		tdxHandleToFamilyList p_stCurrentFamily = h_Current3dData -> h_Family;
		struct tdstState_ *   p_stTmpState;
		long                  tmp;
		unsigned short        wStateNumber;
		
		LST3_M_StaticWhatElementNumber (p_stCurrentState , & p_stCurrentFamily -> hForStateArray , p_stTmpState , tmp);
		wStateNumber = (unsigned short)tmp;
		
		M_DEMO_WRITE_VCR (& otObjectModelType , tdObjectType);
		M_DEMO_WRITE_VCR (& hSO -> hGlobalMatrix -> stTranslationVector , MTH3D_tdstVector);
		M_DEMO_WRITE_VCR (& wStateNumber , unsigned short);
		M_DEMO_WRITE_VCR (& h_Current3dData -> uwCurrentFrame , unsigned short);
	}
#endif
	/*  M_DEMO_WRITE(&ucChannelNumber,unsigned char);*/
#ifdef _DEBUG
	{   /*???*/
		char szText [128];
		sprintf (szText , "%s of actor '%s' with index %i\n" , Activate ? "Activation" : "Desactivation" ,
			hSO -> hLinkedObject . p_stCharacter -> h_3dData -> h_InitialState -> szStateName ,
			hSO - g_stAlways . d_stAlwaysSuperObject);
		OutputDebugString (szText);
	}
#endif
}
#endif /* RETAIL */

/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveCanalActivationDesactivation(HIE_tdxHandleToSuperObject hSO, unsigned char ucChannelNumber,int Activate)
{
/*  tdstDemoSOList *p_stListElement;*/
  unsigned short wSO_Number=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
 
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
 
  wSO_Number = DEMO_fn_wGetNumberFromSO(hSO);  
 
  if(Activate) 
    Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_ACTIVATE_CANAL;
  else
    Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_DESACTIVATE_CANAL;
 
#ifdef D_DEMO_USE_VCR
  fn_vSaveCurrentPositionForVCR ();
#endif
 
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L=0xCCCCCCCC;
    DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
    M_DEMO_WRITE(&L,long);
  }/* debug*/
  #endif
 
  DEMO_M_AddStatistic (DEMO_C_STAT_ActivateDesactivateCanal , sizeof (DEMO_FLAG_CAST) + 3);
  M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_WRITE(&wSO_Number,unsigned short);
  M_DEMO_WRITE(&ucChannelNumber,unsigned char);
  g_ulNbCAN++;
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
/*
void DEMO_fn_vSaveSectorChange(HIE_tdxHandleToSuperObject hSO, HIE_tdxHandleToSuperObject hSectorNew)
{
//  tdstDemoSOList *p_stListElement;
  unsigned short wSO_Number=0;
  unsigned char ucSectorNumber=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
  SECT_tdxHandleOfElementLstGraphicInteraction _hCollNode;
  long Index;
  SECT_tdxHandleOfSectorObject  hSectorUniverse_LinkedObject;
 
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
 
  wSO_Number = DEMO_fn_wGetNumberFromSO(hSO);  
 
  hSectorUniverse_LinkedObject = (SECT_tdxHandleOfSectorObject)HIE_fn_hGetSuperObjectObject(gSO_UniverseSector);
  LST2_M_StaticForEachElementOf(&hSectorUniverse_LinkedObject->stListOfSectorsInGraphicInteraction,_hCollNode,Index)
  {
    if(_hCollNode->hPointerOfSectorSO == hSectorNew)
    {
      ucSectorNumber = (unsigned char)Index;
      goto Label_continue;
    }
  }
  // error all sectors must be in universe sector graphical list
  OutputDebugString("\nerror in Universe sector Demos.c\n");
  
 
Label_continue :
 
  Demo_Flag = (DEMO_FLAG_CAST) DEMO_FLAG_SECTOR_CHANGE;
 
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {// debug
    long L=0xEEEEEEEE;
    DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
    M_DEMO_WRITE(&L,long);
  }// debug
  #endif
 
  DEMO_M_AddStatistic (DEMO_C_STAT_SectorChange , sizeof (DEMO_FLAG_CAST) + 3);
  M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_WRITE(&wSO_Number,unsigned short);
  M_DEMO_WRITE(&ucSectorNumber,unsigned char);
  g_ulNbSCT++;
}
*/
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vTestAndSaveActivationDesactivation(HIE_tdxHandleToSuperObject hSO, int Active)
{
  tdstDemoSOList *p_stListElement;
  unsigned short wSO_Number=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
 
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
  /* ********************** init values ***************************/
  p_stListElement = DEMO_fn_p_stGetInfosPointerFromSO(hSO);
  if(p_stListElement->ucActivate == Active ) return;
 
  p_stListElement->ucActivate = (unsigned char)Active;
  wSO_Number = DEMO_fn_wGetNumberFromSO(hSO);  
 
  /*if(gl_DEMO_NumberOfFrames > 0)*/
  {
    Demo_Flag = (DEMO_FLAG_CAST) 0;
 
 /*   if( g_stEngineStructure.stEngineTimer.ulTrameNumber > g_ulFlagFrameNumber)
    {
      Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_NEW_ENGINE_FRAME;
      g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;
    }
   */ 
    if(Active) 
    {
      Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_ACTIVATE;
      p_stListElement->ucReInit = TRUE;
    }
    else
    {
      Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_DESACTIVATE;
    }
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    #ifdef GIZ_DEBUG_DEMO_MEMORY
    {/* debug*/
      long L=0xBBBBBBBB;
      DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
      M_DEMO_WRITE(&L,long);
    }/* debug*/
    #endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_ActivationDesactivation , sizeof (DEMO_FLAG_CAST) + 2);
    M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
    M_DEMO_WRITE(&wSO_Number,unsigned short);
    g_ulNbACT++;
  }
 
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveACompleteCharacterPosition(HIE_tdxHandleToSuperObject hSO)
{
	tdstDemoSOList *p_stListElement;
	MS_tdxHandleTo3dData h_Current3dData = M_GetMSHandle(hSO,3dData);
	struct tdstState_  *p_stCurrentState = h_Current3dData->h_CurrentState;
	/* ANNECY MT {*/
	tdxHandleToFamilyList p_stCurrentFamily = h_Current3dData->h_Family;
	/* END ANNECY MT }*/
	struct tdstState_  *p_stTmpState;
	GEO_tdxHandleToMatrix	 hSupObjMatrix;
	MTH3D_tdstVector _stTrs;
	/* ANNECY AV {*/
	/*  POS_tdstShortQuaternion _stQuaternion;*/
	tdstDemoCompressedMatrix stCompressedMatrix;
	tdstDemoUltraCompressedMatrix stUltraCompressedMatrix;
	/* END ANNECY AV }*/
	unsigned short wStateNumber=0, wSO_Number=0;
	DEMO_FLAG_CAST  Demo_Flag=0;
	long tmp;
	float f_Transparence;
	
#ifdef D_DEMO_USE_VCR
	unsigned short           wPreviousStateNumber;
	unsigned short           wPreviousFrame;
	MTH3D_tdstVector         stPreviousTranslation;
	tdstDemoCompressedMatrix stPreviousCompressedMatrix;
#endif
	
	if(g_DEMO_KeybTest != DEMO_RECORD) return;
	
    /* ********************** init values ***************************/
    p_stListElement = DEMO_fn_p_stGetInfosPointerFromSO(hSO);
	
#ifdef D_DEMO_USE_VCR
    wPreviousStateNumber       = p_stListElement -> wStateNumber;
    wPreviousFrame             = p_stListElement -> uwCurrentFrame;
    stPreviousTranslation      = p_stListElement -> _stTrs;
    stPreviousCompressedMatrix = p_stListElement -> stCompressedMatrix;
#endif
	
	/* ANNECY AV {*/
	/*  if(p_stListElement->ucActivate == FALSE ) return;*/
    if (p_stListElement->ucActivate == FALSE && ! CAM_fn_bSuperObjectIsACamera (hSO))  return;
	/* END ANNECY AV }*/
	
    wSO_Number = DEMO_fn_wGetNumberFromSO(hSO);  
	
	/* ANNECY MT {*/
    LST3_M_StaticWhatElementNumber(p_stCurrentState,&p_stCurrentFamily->hForStateArray,p_stTmpState,tmp);
	/* END ANNECY MT }*/
    wStateNumber = (unsigned short)tmp; /* macro de (tûûû..ûûût)*/
	
    hSupObjMatrix = HIE_fn_hGetSuperObjectGlobalMatrix(hSO);
    POS_fn_vGetTranslationVector(hSupObjMatrix , &_stTrs );
	/* ANNECY AV {*/
	/*  POS_fn_vCompressRotation(&_stQuaternion, &hSupObjMatrix->stRotationMatrix);*/
    fn_vCompressMatrix (& stCompressedMatrix , & hSupObjMatrix -> stTransformMatrix);
	/* END ANNECY AV }*/
	/* *************************************************************  */
	
	
	/* ********************** compress data *************************/
	if(p_stListElement->ucReInit == FALSE)
	{
		Demo_Flag = (DEMO_FLAG_CAST) 0;
		
		if( g_stEngineStructure.stEngineTimer.ulTrameNumber > g_ulFlagFrameNumber)
		{
			Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_NEW_ENGINE_FRAME;
			g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;
			{
				char s[200];
				sprintf(s,"Frame = %ld :  \t\tPOS%ld \t\tALW%ld \t\tSCT%ld \t\tACT%ld \t\tCAN%ld  \n",g_ulFlagFrameNumber,g_ulNbPOS,g_ulNbALW,g_ulNbSCT,g_ulNbACT,g_ulNbCAN);
				OutputDebugString(s);
			}
			g_ulNbPOS=0;  g_ulNbALW=0;  g_ulNbSCT=0;  g_ulNbACT=0;  g_ulNbCAN=0;
		}
		
		if (MTH_M_bEqualWithEpsilon (p_stListElement->_stTrs.xX, _stTrs.xX, 1E-4) == FALSE ||
			MTH_M_bEqualWithEpsilon (p_stListElement->_stTrs.xY, _stTrs.xY, 1E-4) == FALSE ||
			MTH_M_bEqualWithEpsilon (p_stListElement->_stTrs.xZ, _stTrs.xZ, 1E-4) == FALSE)
		{
			if(p_stListElement->ucDeltaCounter != 0 && fn_bCanCompressVector (& _stTrs , & p_stListElement->_stTrs))
			{
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_TRANSLATION_DELTA;
			}
			else
			{
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_TRANSLATION_COMPLETE;
				MTH3D_M_vCopyVector(&p_stListElement->_stTrs,&_stTrs);
			}
		}
		
		/*    if( !POS_fn_bQuaternionTestIfEqual(&p_stListElement->_stQuaternion,&_stQuaternion) )*/
		if (! fn_bCompressedMatrixTestIfEqual (& p_stListElement -> stCompressedMatrix , & stCompressedMatrix))
		{
			if (fn_bCanUltraCompressMatrix (& stCompressedMatrix , & p_stListElement -> stCompressedMatrix , & stUltraCompressedMatrix))
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_ROTATION_DELTA;
			else Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_ROTATION_COMPLETE;
			
			/*      memcpy(&p_stListElement->_stQuaternion,  &_stQuaternion,  sizeof(POS_tdstShortQuaternion));*/
			memcpy (& p_stListElement -> stCompressedMatrix , & stCompressedMatrix , sizeof (tdstDemoCompressedMatrix));
		}
		
		if (!CAM_fn_bSuperObjectIsACamera(hSO))
		{
			if( p_stListElement->wStateNumber !=wStateNumber )
			{
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_STATE_CHANGE;
				p_stListElement->wStateNumber = wStateNumber;
			}
			if( p_stListElement->uwCurrentFrame != h_Current3dData->uwCurrentFrame )
			{
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_FRAME_CHANGE;
				p_stListElement->uwCurrentFrame = h_Current3dData->uwCurrentFrame;
			}
			
			f_Transparence = HIE_fn_fGetSuperObjectTransparenceLevel(hSO);
			/* ANNECY AV {*/
			if (HIE_fn_lGetSuperObjectDrawMask (hSO) & GLI_C_lIsNotGrided)  f_Transparence = 255.0f;
			/* END ANNECY AV }*/
			if( ((float)p_stListElement->ucTransparence) != f_Transparence )
			{
				Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_TRANSPARENCY_CHANGE;
				p_stListElement->ucTransparence = (unsigned char)f_Transparence;
			}
		}
	}
	else
	{
		/* ANNECY AV {*/
		Demo_Flag = (DEMO_FLAG_CAST) (DEMO_FLAG_TRANSLATION_COMPLETE | DEMO_FLAG_ROTATION_COMPLETE);
		/*    g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;*/
		MTH3D_M_vCopyVector(&p_stListElement->_stTrs,&_stTrs);
		/*    memcpy(&p_stListElement->_stQuaternion,  &_stQuaternion,  sizeof(POS_tdstShortQuaternion));*/
		memcpy (& p_stListElement -> stCompressedMatrix , & stCompressedMatrix , sizeof (tdstDemoCompressedMatrix));
		
		if( g_stEngineStructure.stEngineTimer.ulTrameNumber > g_ulFlagFrameNumber)
		{
			Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_NEW_ENGINE_FRAME;
			g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;
		}
		
		/* END ANNECY AV }*/
		if (!CAM_fn_bSuperObjectIsACamera(hSO))
		{
			Demo_Flag |= (DEMO_FLAG_CAST) (  DEMO_FLAG_FRAME_CHANGE
				| DEMO_FLAG_STATE_CHANGE
				| DEMO_FLAG_TRANSPARENCY_CHANGE );
		}                            
		
		if (!CAM_fn_bSuperObjectIsACamera(hSO))
		{
			p_stListElement->wStateNumber = wStateNumber;
			p_stListElement->uwCurrentFrame = h_Current3dData->uwCurrentFrame;
			/* ANNECY AV {*/
			p_stListElement->ucTransparence = (unsigned char) ((HIE_fn_lGetSuperObjectDrawMask (hSO) & GLI_C_lIsNotGrided) == 0 ?
				HIE_fn_fGetSuperObjectTransparenceLevel (hSO) : 255);
			/* END ANNECY AV }*/
		}
		p_stListElement->ucReInit = FALSE;
		/*    g_ulFlagFrameNumber = g_stEngineStructure.stEngineTimer.ulTrameNumber;*/
	}
	/* **********************************************************************/
	
	if(Demo_Flag == (DEMO_FLAG_CAST) 0)  return;
	
#ifdef D_DEMO_USE_VCR
	fn_vSaveCurrentPositionForVCR ();
#endif
	
	/* *********************** save in Memory ****************************** */
#ifdef GIZ_DEBUG_DEMO_MEMORY
	{/* debug*/
		long L=0xAAAAAAAA;
		DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
		M_DEMO_WRITE(&L,long);
	}/* debug*/
#endif
	
	if (HIE_fn_SO_bIsSuperimposed (hSO))  wSO_Number += 16384;
	else if (wSO_Number <= 255)  Demo_Flag |= (DEMO_FLAG_CAST) DEMO_FLAG_COMPRESS_ACTOR_NUMBER;
	
	DEMO_M_AddStatistic (DEMO_C_STAT_GeneralFlag , sizeof (DEMO_FLAG_CAST));
	M_DEMO_WRITE(&Demo_Flag,DEMO_FLAG_CAST);
	
	if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_COMPRESS_ACTOR_NUMBER))
	{
		unsigned char ucActorNumber = (unsigned char) wSO_Number;
		DEMO_M_AddStatistic (DEMO_C_STAT_CompressedActorNumber , 1);
		M_DEMO_WRITE (& ucActorNumber , unsigned char);
	}
	else
	{
		DEMO_M_AddStatistic (DEMO_C_STAT_ActorNumber , 2);
		M_DEMO_WRITE(&wSO_Number,short);
	}
	
	g_ulNbPOS++;
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_NEW_ENGINE_FRAME) )
	{
#ifdef GIZ_DEBUG_DEMO_MEMORY
		{/* debug*/
			long L=0xFFFFFFFF;
			DEMO_M_AddStatistic (DEMO_C_STAT_Debug , 4);
			M_DEMO_WRITE(&L,long);
		}/* debug*/
#endif
		{
			unsigned short  ulDeltaTimerCount = (unsigned short)g_stEngineStructure.stEngineTimer.ulDeltaTimerCount;
			DEMO_M_AddStatistic (DEMO_C_STAT_FrameNumber , 2);
			M_DEMO_WRITE(&ulDeltaTimerCount,unsigned short);
		}
		gl_DEMO_NumberOfFrames ++;
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSLATION_COMPLETE) )
	{
		/*    p_stListElement->ucDeltaCounter++; if(p_stListElement->ucDeltaCounter == DEMO_DELTACOUNT) p_stListElement->ucDeltaCounter=0;*/
		p_stListElement->ucDeltaCounter = 1;
		DEMO_M_AddStatistic (DEMO_C_STAT_CompleteTranslation , sizeof (MTH3D_tdstVector));
		M_DEMO_WRITE(&_stTrs,MTH3D_tdstVector);
		M_DEMO_WRITE_VCR (& stPreviousTranslation , MTH3D_tdstVector);
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSLATION_DELTA) )
	{
		/*    MTH3D_tdstVector _stDelta;*/
		/*    MTH3D_M_vSubVector ( &_stDelta, &_stTrs, &p_stListElement->_stTrs );*/
		char a3_cCoef [3];
		fn_vCompressVector (a3_cCoef , & _stTrs , & p_stListElement->_stTrs);
		
		p_stListElement->ucDeltaCounter++; if(p_stListElement->ucDeltaCounter == DEMO_DELTACOUNT) p_stListElement->ucDeltaCounter=0;
		
		DEMO_M_AddStatistic (DEMO_C_STAT_DeltaTranslation , 3);
		/*    M_DEMO_WRITE(&_stDelta,MTH3D_tdstVector);*/
		M_DEMO_WRITE (a3_cCoef , a3_cCoef);
		
		MTH3D_M_vCopyVector(&p_stListElement->_stTrs,&_stTrs);
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ROTATION_COMPLETE) )
	{
		/* ANNECY AV {*/
		/*    M_DEMO_WRITE(&_stQuaternion,POS_tdstShortQuaternion);*/
		DEMO_M_AddStatistic (DEMO_C_STAT_CompleteRotation , sizeof (tdstDemoCompressedMatrix));
		M_DEMO_WRITE (& stCompressedMatrix , tdstDemoCompressedMatrix);
		M_DEMO_WRITE_VCR (& stPreviousCompressedMatrix , tdstDemoCompressedMatrix);
		/* END ANNECY AV }*/
	}
	
	if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ROTATION_DELTA))
	{
		DEMO_M_AddStatistic (DEMO_C_STAT_DeltaRotation , sizeof (tdstDemoUltraCompressedMatrix));
		M_DEMO_WRITE (& stUltraCompressedMatrix , tdstDemoUltraCompressedMatrix);
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_STATE_CHANGE) )
	{
		DEMO_M_AddStatistic (DEMO_C_STAT_StateChange , 2);
		M_DEMO_WRITE(&wStateNumber,unsigned short);
		M_DEMO_WRITE_VCR (& wPreviousStateNumber , unsigned short);
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_FRAME_CHANGE) )
	{
		DEMO_M_AddStatistic (DEMO_C_STAT_FrameChange , 2);
		M_DEMO_WRITE(&h_Current3dData->uwCurrentFrame,unsigned short);
		M_DEMO_WRITE_VCR (& wPreviousFrame , unsigned short);
	}
	
	if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSPARENCY_CHANGE) )
	{
		DEMO_M_AddStatistic (DEMO_C_STAT_TransparencyChange , 1);
		M_DEMO_WRITE(&p_stListElement->ucTransparence,unsigned char);
	}
	/* ***********************************************************************/
	
	
	gf_CompressionRate0 = (float)((long)gp_DEMO_CurrentPosInMemory-(long)gp_DEMO_AllocatedMemory)/(float)gl_DEMO_NumberOfFrames;
	
	if( ((long)gp_DEMO_CurrentPosInMemory-(long)gp_DEMO_AllocatedMemory)>DEMO_ALLOCATED_MEMORY*1024L )
	{
		g_DEMO_KeybTest = DEMO_NOTHING;
		DEMO_fn_vSaveToFile();
		/* ANNECY AV {*/
		/*    fn_vAddCommandLineAnswerWithScroll("DEMO RECORD STOP    DEMO MEMORY FULL   SAVE AT THIS POSITION");*/
		/* END ANNECY AV }*/
	}
}
#endif /* RETAIL */
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_fn_vSaveGenerateAGO (unsigned long PartType, MTH3D_tdstVector *p_stPosition, MTH3D_tdstVector *p_stDirection, GMT_tdxHandleToGameMaterial	hGameMaterial, float parameter)
{
    DEMO_FLAG_CAST  Demo_Flag;
    unsigned short uwGMTIndexInGMTList;
 
    if(g_DEMO_KeybTest != DEMO_RECORD)  return;

	if (hGameMaterial)
	{
		uwGMTIndexInGMTList = DEMO_fn_wGetNumberFromGMT(hGameMaterial);
	}
	else
	{
		uwGMTIndexInGMTList = 0xffff;
	}
    Demo_Flag = (DEMO_FLAG_CAST) (DEMO_FLAG_EXTENDED | DEMO_FLAG_GENERATE_AGO);
 
#ifdef D_DEMO_USE_VCR
    fn_vSaveCurrentPositionForVCR ();
#endif
 
    DEMO_M_AddStatistic (DEMO_C_STAT_GenerateAGO , sizeof (DEMO_FLAG_CAST) + sizeof (unsigned long) + sizeof(MTH3D_tdstVector) + sizeof(MTH3D_tdstVector) + sizeof(unsigned short) + sizeof(float));
    M_DEMO_WRITE (& Demo_Flag , DEMO_FLAG_CAST);
    M_DEMO_WRITE (&PartType , unsigned long);
    M_DEMO_WRITE (p_stPosition , MTH3D_tdstVector);
    M_DEMO_WRITE (p_stDirection , MTH3D_tdstVector);
    M_DEMO_WRITE (&uwGMTIndexInGMTList , unsigned short);
    M_DEMO_WRITE (&parameter , float);
}
#endif /* RETAIL */
 /*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
 
 
 
 
 
 
 
 
 
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/************************************************************************************************************************************/
/****READ SECTION********************************************************************************************************************/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vReadAndTreatAllExtendedFlag(DEMO_FLAG_CAST Demo_Flag)
{
  if(g_DEMO_KeybTest != DEMO_PLAY) return;
 
  /* ********************** init values ***************************/
  /* *********************** read in Memory *******************************/
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L;
    M_DEMO_READ(&L,long);
  }/* debug*/
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
 
  /* ANNECY AV {*/
  /*
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_SUPERIMPOSED_TEXT))
  {
  char Tmp_iIndex;
  char Tmp_hText;
  char Tmp_lLiveTime;
  char Tmp_hFont;
  MTH_tdxReal xPosX,xPosY;
  FON_tdxHandleOfFont hFont;
  
    M_DEMO_READ(&Tmp_iIndex,char);
    if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVE_DESACTIVE))
    {
	M_DEMO_READ(&Tmp_hText,char);
	M_DEMO_READ(&Tmp_hFont,char);
	M_DEMO_READ(&xPosX,MTH_tdxReal);
	M_DEMO_READ(&xPosY,MTH_tdxReal);
	M_DEMO_READ(&Tmp_lLiveTime,char);
	
      hFont = FON_fn_wGetFontHandleByNumber((unsigned long)Tmp_hFont);
      fn_vAddSuperimposedText((int)Tmp_iIndex,(FON_tdxHandleOfString)Tmp_hText,hFont,xPosX,xPosY,(long) Tmp_lLiveTime);
	  }  
	  else
	  {
      fn_vRemoveSuperimposedText((int)Tmp_iIndex);
	  }
	  }
  */
  
  /* ANNECY AV {*/
  /*
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_FIX_CHANGE))
  {
  unsigned char ucValue;
  unsigned char FieldFlag;
  HIE_tdxHandleToSuperObject hSO;
  
    M_DEMO_READ(&FieldFlag,unsigned char);
    M_DEMO_READ(&ucValue,unsigned char);
	
	  hSO = MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode());
	  switch(FieldFlag)
	  {
      case DEMO_FIX_HIT_POINTS : fn_vStandardGameSetHitPoints(M_GetMSHandle(hSO,StandardGame),ucValue); break;
      case DEMO_FIX_HIT_POINTS_MAX : fn_vStandardGameSetHitPointsMax(M_GetMSHandle(hSO,StandardGame),ucValue); break;
      case DEMO_FIX_SILVER_SPADE : fn_vStandardGameSetHitPoints(M_GetMSHandle(g_stEngineStructure.h_WorldCaracter,StandardGame),ucValue); break;
      case DEMO_FIX_SILVER_SPADE_MAX : fn_vStandardGameSetHitPointsMax(M_GetMSHandle(g_stEngineStructure.h_WorldCaracter,StandardGame),ucValue);
	  g_stFixStructure.ucCurrentNumberOfSilverSpade = 255;   break;
      case DEMO_FIX_LIVES : fn_vSetLives(ucValue); break;
      case DEMO_FIX_TINGS : fn_vWorldSetNumberOfTingsPickedUp(M_GetMSHandle(g_stEngineStructure.h_WorldCaracter,World),ucValue); break;
      case DEMO_FIX_BEES : fn_xSetNumberOfItemsWithId(0,ucValue); break;
	  }
	  }
  */
  /* END ANNECY AV }*/
  
  /* ANNECY AV {*/
  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_CHANGE_VIEWPORT_SIZE))
  {
      unsigned char a10_ucParam [10];
	  
      M_DEMO_READ (a10_ucParam , a10_ucParam);
      fn_vChangeViewPortPercent (a10_ucParam [0] , a10_ucParam [1] , a10_ucParam [2] , a10_ucParam [3] , a10_ucParam [4] ,
		  a10_ucParam [5] , a10_ucParam [6] , a10_ucParam [7] , (signed char)a10_ucParam [8] , (signed char)a10_ucParam [9]);
  }
  else if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_MODULE_TRANSPARENCY))
  {
	  unsigned short wSO_Number;
	  unsigned char  ucChannelNumber;
	  unsigned char  ucTransparencyValue;
	  tdstDemoSOList *            p_stListElement;
	  int                         i;
	  CHN_tdxHandleToChannelArray hChannelArray;
	  HIE_tdxHandleToSuperObject  hModuleSuperObject;
	  
	  /* Read the parameters*/
	  M_DEMO_READ (& wSO_Number          , unsigned short);
	  M_DEMO_READ (& ucChannelNumber     , unsigned char);
	  M_DEMO_READ (& ucTransparencyValue , unsigned char);
	  
	  /* Find the actor super object*/
	  LST2_M_StaticGetElementNumber (g_hDemoSOList , p_stListElement , wSO_Number , i);
	  
	  hChannelArray = fn_h3dDataGetChannelSOList (M_GetMSHandle (p_stListElement -> hCharacterSupObject , 3dData));
	  if (hChannelArray)
	  {
		  hModuleSuperObject = fn_hGetSuperObjectInChannel (hChannelArray , ucChannelNumber);
		  if (hModuleSuperObject)
		  {
			  HIE_fn_SO_vSetFlags(hModuleSuperObject , HIE_fn_SO_ulGetFlags(hModuleSuperObject) | HIE_C_Flag_ModuleTransparency);
			  HIE_fn_vSetSuperObjectTransparenceLevel (hModuleSuperObject , (float) ucTransparencyValue);
		  }
	  }
  }
  else if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ACTIVATE_TRANSPARENCY) ||
	  DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_DESACTIVATE_TRANSPARENCY))
  {
	  unsigned short wSO_Number;
	  unsigned char  ucChannelNumber;
	  tdstDemoSOList *            p_stListElement;
	  int                         i;
	  CHN_tdxHandleToChannelArray hChannelArray;
	  HIE_tdxHandleToSuperObject  hModuleSuperObject;
	  
	  /* Read the parameters*/
	  M_DEMO_READ (& wSO_Number      , unsigned short);
	  M_DEMO_READ (& ucChannelNumber , unsigned char);
	  
	  /* Find the actor super object*/
	  LST2_M_StaticGetElementNumber (g_hDemoSOList , p_stListElement , wSO_Number , i);
	  
	  hChannelArray = fn_h3dDataGetChannelSOList (M_GetMSHandle (p_stListElement -> hCharacterSupObject , 3dData));
	  if (hChannelArray)
	  {
		  hModuleSuperObject = fn_hGetSuperObjectInChannel (hChannelArray , ucChannelNumber);
		  if (hModuleSuperObject)
		  {
			  long lValue = HIE_fn_lGetSuperObjectDrawMask (hModuleSuperObject);
			  
			  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ACTIVATE_TRANSPARENCY))
				  lValue = lValue & ~ GLI_C_lIsNotGrided;
			  else
			  {
				  lValue = lValue | GLI_C_lIsNotGrided;
				  HIE_fn_SO_vSetFlags (hModuleSuperObject , HIE_fn_SO_ulGetFlags (hModuleSuperObject) & ~ HIE_C_Flag_ModuleTransparency);
			  }
			  
			  HIE_fn_vSetSuperObjectDrawMask (hModuleSuperObject , lValue);
		  }
	  }
  }
  else if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_MOVE_MODULE))
  {
	  unsigned short              wSO_Number;
	  unsigned char               ucChannelNumber;
	  tdstDemoSOList *            p_stListElement;
	  int                         i;
	  CHN_tdxHandleToChannelArray hChannelArray;
	  HIE_tdxHandleToSuperObject  hModuleSuperObject;
	  /*      POS_tdstCompletePosition *  hModuleGlobalMatrix;*/
	  POS_tdstCompletePosition *  hModuleLocaleMatrix;
	  tdstDemoCompressedMatrix    stCompressedMatrix;
	  MTH3D_tdstVector            stTranslationVector;
	  
	  /* Read the parameters*/
	  M_DEMO_READ (& wSO_Number          , unsigned short);
	  M_DEMO_READ (& ucChannelNumber     , unsigned char);
	  M_DEMO_READ (& stTranslationVector , MTH3D_tdstVector);
	  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ROTATE_MODULE))
		  M_DEMO_READ (& stCompressedMatrix  , tdstDemoCompressedMatrix);
	  
	  /* Find the actor super object*/
	  LST2_M_StaticGetElementNumber (g_hDemoSOList , p_stListElement , wSO_Number , i);
	  
	  hChannelArray = fn_h3dDataGetChannelSOList (M_GetMSHandle (p_stListElement -> hCharacterSupObject , 3dData));
	  if (hChannelArray)
	  {
		  hModuleSuperObject = fn_hGetSuperObjectInChannel (hChannelArray , ucChannelNumber);
		  if (hModuleSuperObject)
		  {
		  /*
		  hModuleGlobalMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (hModuleSuperObject);
		  fn_vDecompressMatrix (& hModuleGlobalMatrix -> stTransformMatrix , & stCompressedMatrix);
		  fn_vComputeRotationMatrixFromTransformMatrix (& hModuleGlobalMatrix -> stRotationMatrix , & hModuleGlobalMatrix -> stTransformMatrix);
		  MTH3D_M_vCopyVector  (& hModuleGlobalMatrix -> stTranslationVector , & stTranslationVector);
		  POS_fn_vUpdateMatrixType         (hModuleGlobalMatrix);
		  HIE_fn_vValidateGlobalMatrix     (hModuleSuperObject);
		  HIE_fn_vComputeNewRelativeMatrix (hModuleSuperObject);
			  */
			  
			  hModuleLocaleMatrix = HIE_fn_hGetSuperObjectMatrix (hModuleSuperObject);
			  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ROTATE_MODULE))
			  {
				  fn_vDecompressMatrix (& hModuleLocaleMatrix -> stTransformMatrix , & stCompressedMatrix);
				  fn_vComputeRotationMatrixFromTransformMatrix (& hModuleLocaleMatrix -> stRotationMatrix , & hModuleLocaleMatrix -> stTransformMatrix);
			  }
			  MTH3D_M_vCopyVector  (& hModuleLocaleMatrix -> stTranslationVector , & stTranslationVector);
			  hModuleLocaleMatrix -> ulType = POS_C_ulCompleteMatrixFlag;
			  POS_fn_vUpdateMatrixType          (hModuleLocaleMatrix);
			  HIE_fn_vInvalidateOneGlobalMatrix (hModuleSuperObject);
		  }
	  }
  }
  else if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_GENERATE_AGO))
  {
	  unsigned long PartType;
	  MTH3D_tdstVector stPosition;
	  MTH3D_tdstVector stDirection;
	  unsigned short uwGMTIndexInGMTList;
	  float parameter;
	  GMT_tdxHandleToGameMaterial	hGameMaterial;
	  ACP_tdxHandleOfMaterial		hVisualMaterial;	  

	  M_DEMO_READ (&PartType , unsigned long);
	  M_DEMO_READ (&stPosition , MTH3D_tdstVector);
	  M_DEMO_READ (&stDirection , MTH3D_tdstVector);
	  M_DEMO_READ (&uwGMTIndexInGMTList , unsigned short);
	  M_DEMO_READ (&parameter , float);

	  if (uwGMTIndexInGMTList != 0xffff)
	  {
		hGameMaterial   = DEMO_fn_hGetGMTFromList   (uwGMTIndexInGMTList);
		hVisualMaterial = GMT_fn_hGetVisualMaterial (hGameMaterial);
	  }
	  else
	  {
		  hVisualMaterial = NULL;
	  }

	  AGO_vAddParticle (PartType , &stPosition , &stDirection , hVisualMaterial , parameter);
  }
  /* END ANNECY AV }*/
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vReadAndChangeActivationDesactivation(HIE_tdxHandleToSuperObject hSO)
{
  unsigned short wSO_Number=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
  tdstEngineObject * p_stObj;
  unsigned char ucChannelNumber;
  HIE_tdxHandleToSuperObject hSOChannel;
  unsigned long ulSpoFlags;
 
  if(g_DEMO_KeybTest != DEMO_PLAY) return;
 
  /* ********************** init values ***************************/
  /* *********************** read in Memory *******************************/
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L;
    M_DEMO_READ(&L,long);
  }/* debug*/
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_READ(&wSO_Number,unsigned short);
 
  p_stObj=(tdstEngineObject*)HIE_fn_hGetSuperObjectObject(hSO);
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVATE))
  {
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        p_stObj = (tdstEngineObject *) HIE_fn_hGetSuperObjectObject (hSO);
        M_UnactivateObject (p_stObj);
        PLA_fn_vDesInitAllChildOfCharacter (hSO , p_stObj);
    }
    else
#endif
    {
        M_ActivateObject(p_stObj);
/* ANNECY AV {*/
        PLA_fn_bDoAnimPlayerForCharacter(hSO,FALSE,FALSE);
/* END ANNECY AV }*/
    }
  }
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_DESACTIVATE))
  {
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        MS_tdxHandleTo3dData h_Current3dData = M_GetMSHandle(hSO,3dData);
        struct tdstAnim3d_ * p_stAnim        = fn_h3dDataGetCurrentState (h_Current3dData) -> p_stAnim;
 
        M_ActivateObject ((tdstEngineObject *) HIE_fn_hGetSuperObjectObject (hSO));
 
        /* ???*/
        /* To be sure that the frame number is correct*/
        if (p_stAnim && fn_uw3dDataGetCurrentFrame (h_Current3dData) >= p_stAnim -> uwNumberOfFrames)
            fn_v3dDataSetCurrentFrame  (h_Current3dData , (unsigned short) (p_stAnim -> uwNumberOfFrames - 1));
 
        PLA_fn_bDoAnimPlayerForCharacter (hSO,FALSE,FALSE);
    }
    else
#endif
    {
        M_UnactivateObject(p_stObj);
        PLA_fn_vDesInitAllChildOfCharacter(hSO,p_stObj);
    }
  }
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVATE_CANAL))
  {
    M_DEMO_READ(&ucChannelNumber,unsigned char);
    hSOChannel = fn_hGetSuperObjectInChannel(fn_h3dDataGetChannelSOList(M_GetMSHandle(hSO,3dData)),ucChannelNumber);
	if (hSOChannel != NULL)
	{
		ulSpoFlags = HIE_M_xGetSuperObjectMember(hSOChannel , ulFlags);
		/* it becomes pickable, visible and shadowable */
		HIE_M_vSetSuperObjectMember(hSOChannel , ulFlags, ulSpoFlags & ~ ( HIE_C_Flag_ulNotPickable | HIE_C_Flag_ulHidden | HIE_C_Flag_ulNoShadowOnMe ) ) ;
	}
  }
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_DESACTIVATE_CANAL))
  {
    M_DEMO_READ(&ucChannelNumber,unsigned char);
    hSOChannel = fn_hGetSuperObjectInChannel(fn_h3dDataGetChannelSOList(M_GetMSHandle(hSO,3dData)),ucChannelNumber);
	if (hSOChannel != NULL)
	{
		ulSpoFlags = HIE_M_xGetSuperObjectMember(hSOChannel , ulFlags);
		/* it becomes not pickable, visible or shadowable */
		HIE_M_vSetSuperObjectMember(hSOChannel , ulFlags, ulSpoFlags | HIE_C_Flag_ulNotPickable | HIE_C_Flag_ulHidden | HIE_C_Flag_ulNoShadowOnMe ) ;
	}
  }
 
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
/*
void DEMO_fn_vReadAndDoSectorChange(HIE_tdxHandleToSuperObject hSO)
{
  unsigned short wSO_Number=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
  unsigned char ucSectorNumber=0;
  SECT_tdxHandleOfElementLstGraphicInteraction _hCollNode;
  long Index;
  SECT_tdxHandleOfSectorObject  hSectorUniverse_LinkedObject;
 
  if(g_DEMO_KeybTest != DEMO_PLAY) return;
 
  // ********************** init values **************************
  // *********************** read in Memory ******************************
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {// debug
    long L;
    M_DEMO_READ(&L,long);
  }// debug
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_READ(&wSO_Number,unsigned short);
  M_DEMO_READ(&ucSectorNumber,unsigned char);
 
  hSectorUniverse_LinkedObject = (SECT_tdxHandleOfSectorObject)HIE_fn_hGetSuperObjectObject(gSO_UniverseSector);
 
  LST2_M_StaticGetElementNumber(&hSectorUniverse_LinkedObject->stListOfSectorsInGraphicInteraction,_hCollNode,ucSectorNumber,Index);
 
  GAM_fn_vSetCurrentSector(hSO,_hCollNode->hPointerOfSectorSO);
  fn_vSectInfoLaunchSectorSoundEvents(hSO);
  fn_vInsertObjectInSectorList(GAM_fn_hGetCurrentSector(hSO),hSO);
  if( hSO==MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode()) )
  {
    fn_vInitObjectsWhenPlayerChangeSector(hSO);
  }
  GAM_fn_vSetPreviousSector(hSO,GAM_fn_hGetCurrentSector(hSO));
}
*/
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vReadAlwaysActivationDesactivation(HIE_tdxHandleToSuperObject hSO)
{
  unsigned short wSO_Number=0,wFatherSO_Number=0;
  DEMO_FLAG_CAST  Demo_Flag=0;
  /*tdstEngineObject * p_stObj;*/
  tdObjectType otObjectModelType;
  MTH3D_tdstVector  stTempVector;
  POS_tdstCompletePosition p_stPosMatrix;
 
  POS_fn_vSetIdentityMatrix(&p_stPosMatrix);
 
 
  if(g_DEMO_KeybTest != DEMO_PLAY) return;
  /* ********************** init values ***************************/
  /* *********************** read in Memory *******************************/
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L;
    M_DEMO_READ(&L,long);
  }/* debug*/
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_READ(&wSO_Number,unsigned short);
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVATE))
  {
    M_DEMO_READ(&otObjectModelType,tdObjectType);
    M_DEMO_READ(&stTempVector,MTH3D_tdstVector);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {   /* In reverse mode, desactivate always instead of activating it*/
        fn_vKillEngineObjectOrAlwaysByPointer ((tdstEngineObject *) HIE_fn_hGetSuperObjectObject (hSO));
    }
    else
#endif
    {
        POS_fn_vSetTranslationVector (&p_stPosMatrix,&stTempVector);
 
        /* The generator of all always is the main character*/
        fn_p_stAllocateAlways(otObjectModelType, gp_stDynamicWorld, MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode()) , 0, &p_stPosMatrix);
        fn_vStandardGameSetIsActivable(M_GetMSHandle(hSO,StandardGame),TRUE);
    }
  }
 
  if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_DESACTIVATE))
  {
#ifdef D_DEMO_USE_VCR
      if (g_bVCR_ReverseMode)
      {
          int i , iAlwaysNumber;
          unsigned short wStateNumber , wFrameNumber;
          MS_tdxHandleTo3dData h_Current3dData;
          struct tdstState_ *  p_stTmpState;
 
          M_DEMO_READ_VCR (& otObjectModelType , tdObjectType);
          M_DEMO_READ_VCR (& stTempVector , MTH3D_tdstVector);
          M_DEMO_READ_VCR (& wStateNumber , unsigned short);
          M_DEMO_READ_VCR (& wFrameNumber , unsigned short);
 
          POS_fn_vSetTranslationVector (&p_stPosMatrix,&stTempVector);
 
          /* A little trick : to be sure that the always will be allocated at the good place, we fill all the previous places*/
          iAlwaysNumber = hSO - g_stAlways.d_stAlwaysSuperObject;
          for (i = 0 ; i < iAlwaysNumber ; i ++)
              if (g_stAlways.d_hAlwaysGenerator [i] == NULL)  g_stAlways.d_hAlwaysGenerator [i] = (void *) 0xFFFFFFFF;
 
          fn_p_stAllocateAlways(otObjectModelType, gp_stDynamicWorld, MC_fn_hGetCharacterInMainCharacterNode( MC_fn_hGetFirstMainCharNode()) , 0, &p_stPosMatrix);
 
          for (i = 0 ; i < iAlwaysNumber ; i ++)
              if (g_stAlways.d_hAlwaysGenerator [i] == (void *) 0xFFFFFFFF)  g_stAlways.d_hAlwaysGenerator [i] = NULL;
 
          fn_vStandardGameSetIsActivable(M_GetMSHandle(hSO,StandardGame),TRUE);
 
          h_Current3dData = M_GetMSHandle (hSO , 3dData);
          LST2_M_StaticGetElementNumber( & h_Current3dData -> h_Family -> hForStateArray , p_stTmpState , wStateNumber , i);
          PLA_fn_bSetNewState  (hSO , p_stTmpState , FALSE , FALSE);
          PLA_fn_vSetCurrFrame (hSO , wFrameNumber);
      }
      else
#endif
/* ANNECY AV {*/
      {
/*        fn_vKillAlwaysByPersonalType(wSO_Number+C_AlwaysObjectType);*/
/*        fn_vKillAlwaysByPersonalType (wSO_Number - g_iFirstAlwaysNumber + C_AlwaysObjectType);*/
        fn_vKillEngineObjectOrAlwaysByPointer ((tdstEngineObject *) HIE_fn_hGetSuperObjectObject (hSO));
      }
/* END ANNECY AV */
  }
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vGetACompleteCharacterPosition(HIE_tdxHandleToSuperObject hSO)
{
  tdstDemoSOList *p_stListElement;
  MS_tdxHandleTo3dData h_Current3dData = M_GetMSHandle(hSO,3dData);
  struct tdstState_  *p_stTmpState;
  GEO_tdxHandleToMatrix	 hSupObjMatrix;
/*  MTH3D_tdstVector _stTrs;*/
/* ANNECY AV {*/
/*  POS_tdstShortQuaternion _stQuaternion;*/
  tdstDemoCompressedMatrix stCompressedMatrix;
  tdstDemoUltraCompressedMatrix stUltraCompressedMatrix;
/* END ANNECY AV }*/
  unsigned short wStateNumber=0, wSO_Number=0;
  unsigned short uwForcedFrame;
  long i;
  DEMO_FLAG_CAST  Demo_Flag=0;
  /*MTH_tdxReal X, Y, Z;*/
  
  /* We have to test g_iNbFramesToWaitBeforePlaying*/
  if(g_DEMO_KeybTest != DEMO_PLAY || g_iNbFramesToWaitBeforePlaying != 0) return;
 
  /* ********************** init values ***************************/
  p_stListElement = DEMO_fn_p_stGetInfosPointerFromSO(hSO);
  hSupObjMatrix = HIE_fn_hGetSuperObjectMatrix(hSO);
/* ANNECY AV {*/
/*  POS_M_vSetType(hSupObjMatrix,0x01b0);*/
/* END ANNECY AV }*/
  /* ************************************************************* */
 
  /* *********************** read in Memory *******************************/
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {/* debug*/
    long L;
    M_DEMO_READ(&L,long);
  }/* debug*/
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
 
  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_COMPRESS_ACTOR_NUMBER))
  {
      unsigned char ucSO_Number;
      M_DEMO_READ (& ucSO_Number , unsigned char);
      wSO_Number= ucSO_Number;
  }
  else
  {
      M_DEMO_READ(&wSO_Number,short);
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_NEW_ENGINE_FRAME) )
  {
    #ifdef GIZ_DEBUG_DEMO_MEMORY
    {/* debug*/
      long L;
      M_DEMO_READ(&L,long);
    }/* debug*/
    #endif
    {
      unsigned short  ulDeltaTimerCount;
      M_DEMO_READ(&ulDeltaTimerCount,unsigned short);
      g_ulDEMO_CurrentTimerCount += ulDeltaTimerCount;
     /*  {
        char s[200];
        sprintf(s,"Demo : MEM read ulDeltaTimerCount %ld\n",ulDeltaTimerCount);
        OutputDebugString(s);
       }*/
    }
#ifdef D_DEMO_USE_VCR
    if (! g_bVCR_ReverseMode)
#endif
        gl_DEMO_CurrentFrame++;
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSLATION_COMPLETE) )
  {
    M_DEMO_READ(&p_stListElement->_stTrs,MTH3D_tdstVector);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        M_DEMO_READ_VCR (& p_stListElement->_stTrs , MTH3D_tdstVector);
    }
#endif
    POS_fn_vSetTranslationVector(hSupObjMatrix , &p_stListElement->_stTrs );
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSLATION_DELTA) )
  {
/*    MTH3D_tdstVector _stDelta;*/
/*    M_DEMO_READ(&_stDelta,MTH3D_tdstVector);*/
/*    MTH3D_M_vAddVector ( &p_stListElement->_stTrs,&_stDelta, &p_stListElement->_stTrs );*/
    char a3_cCoef [3];
    M_DEMO_READ (a3_cCoef , a3_cCoef);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        a3_cCoef [0] = (char) - a3_cCoef [0];
        a3_cCoef [1] = (char) - a3_cCoef [1];
        a3_cCoef [2] = (char) - a3_cCoef [2];
    }
#endif
    fn_vDecompressVector (& p_stListElement->_stTrs , a3_cCoef , & p_stListElement -> _stTrs);
 
    POS_fn_vSetTranslationVector(hSupObjMatrix , &p_stListElement->_stTrs );
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ROTATION_COMPLETE) )
  {
/*    M_DEMO_READ(&_stQuaternion,POS_tdstShortQuaternion);*/
/*    POS_fn_vDecompressRotation(&hSupObjMatrix->stRotationMatrix,&_stQuaternion);*/
/*    MTH3D_M_vCopyMatrix( &hSupObjMatrix->stTransformMatrix, &hSupObjMatrix->stRotationMatrix );   */
    M_DEMO_READ (& stCompressedMatrix , tdstDemoCompressedMatrix);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        M_DEMO_READ_VCR (& stCompressedMatrix , tdstDemoCompressedMatrix);
    }
#endif
    memcpy (& p_stListElement -> stCompressedMatrix , & stCompressedMatrix , sizeof (tdstDemoCompressedMatrix));
    fn_vDecompressMatrix (& hSupObjMatrix -> stTransformMatrix , & stCompressedMatrix);
    fn_vComputeRotationMatrixFromTransformMatrix (& hSupObjMatrix -> stRotationMatrix , & hSupObjMatrix -> stTransformMatrix);
    hSupObjMatrix -> ulType = POS_C_ulCompleteMatrixFlag;
    POS_fn_vUpdateMatrixType (hSupObjMatrix);
  }
 
  if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_ROTATION_DELTA))
  {
    M_DEMO_READ (& stUltraCompressedMatrix , tdstDemoUltraCompressedMatrix);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        int    i;
        char * p = stUltraCompressedMatrix . a9_cCoef;
        for (i = 0 ; i < 9 ; i ++ , p ++)
            * p = (char) - * p;
    }
#endif
    fn_vUltraDecompressMatrix (& hSupObjMatrix -> stTransformMatrix , & stUltraCompressedMatrix , & p_stListElement -> stCompressedMatrix);
    fn_vComputeRotationMatrixFromTransformMatrix (& hSupObjMatrix -> stRotationMatrix , & hSupObjMatrix -> stTransformMatrix);
    hSupObjMatrix -> ulType = POS_C_ulCompleteMatrixFlag;
    POS_fn_vUpdateMatrixType (hSupObjMatrix);
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_STATE_CHANGE) )
  {
    M_DEMO_READ(&wStateNumber,unsigned short);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode) M_DEMO_READ_VCR (& wStateNumber , unsigned short);    /* If reverse mode, we read the previous state number*/
#endif
    p_stListElement->wStateNumber = wStateNumber; 
  }
  else
  {
    wStateNumber = p_stListElement->wStateNumber; 
  }
  
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_FRAME_CHANGE) )
  {
    M_DEMO_READ(&uwForcedFrame,unsigned short);
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)  M_DEMO_READ_VCR (& uwForcedFrame , unsigned short);    /* If reverse mode, we read the previous frame number*/
#endif
    p_stListElement->uwCurrentFrame = uwForcedFrame;
  }
  else
  {
    uwForcedFrame = p_stListElement->uwCurrentFrame;
  }
  
/* ANNECY AV {*/
/*  POS_fn_vCopyCompressedMatrix(HIE_fn_hGetSuperObjectGlobalMatrix(hSO),HIE_fn_hGetSuperObjectMatrix(hSO));*/
  POS_fn_vCopyMatrix (HIE_fn_hGetSuperObjectGlobalMatrix (hSO) , HIE_fn_hGetSuperObjectMatrix (hSO));
/* END ANNECY AV }*/
 
#ifdef D_DEMO_USE_VCR
  if (! g_bDontTreatCharacter)
#endif
  if(     DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_FRAME_CHANGE)
      ||  DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_STATE_CHANGE) )
  {
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode && wStateNumber >= LST2_M_StaticGetNumberOfElements (& h_Current3dData -> h_Family -> hForStateArray))
        wStateNumber = (unsigned short) (LST2_M_StaticGetNumberOfElements (& h_Current3dData -> h_Family -> hForStateArray) - 1);
#endif
 
    LST2_M_StaticGetElementNumber(&(h_Current3dData->h_Family->hForStateArray),p_stTmpState,wStateNumber,i);
 
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        struct tdstAnim3d_ * p_stAnim = fn_h3dDataGetCurrentState (h_Current3dData) -> p_stAnim;
 
        /* ???*/
        /* To be sure that the frame number is correct*/
        if (p_stAnim && uwForcedFrame >= p_stAnim -> uwNumberOfFrames)
            uwForcedFrame = (unsigned short) (p_stAnim -> uwNumberOfFrames - 1);
    }
#endif
 
/* ANNECY AV {*/
/*    PLA_fn_bSetForcedStateWithForcedFrame(hSO,p_stTmpState,uwForcedFrame);*/
    if (p_stTmpState != fn_h3dDataGetCurrentState (h_Current3dData) || uwForcedFrame == 0)
    {
#ifdef D_DEMO_USE_VCR
        if (g_bVCR_ReverseMode == FALSE || p_stTmpState -> p_stAnim != NULL)
#endif
        {
            PLA_fn_bSetNewState  (hSO , p_stTmpState , FALSE , FALSE );
            PLA_fn_vSetCurrFrame (hSO , uwForcedFrame);
        }
    }
    else
    {
        BOOL bSavedValue = g_bAnimConstantSpeed;
        fn_v3dDataSetCurrentFrame (h_Current3dData , (unsigned short) (uwForcedFrame - 1));
        g_bAnimConstantSpeed = TRUE;
        PLA_fn_bDoAnimPlayerForCharacter(hSO,FALSE,FALSE);
        g_bAnimConstantSpeed = bSavedValue;
    }
/* END ANNECY AV }*/
  }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSPARENCY_CHANGE) )
  {
    M_DEMO_READ(&p_stListElement->ucTransparence,unsigned char);
    HIE_fn_vSetSuperObjectTransparenceLevel(hSO,(float)p_stListElement->ucTransparence);
/* ANNECY AV {*/
    if (p_stListElement->ucTransparence == 255)
    { HIE_fn_vSetSuperObjectDrawMask (hSO , HIE_fn_lGetSuperObjectDrawMask (hSO) | GLI_C_lIsNotGrided);   }
    else
    { HIE_fn_vSetSuperObjectDrawMask (hSO , HIE_fn_lGetSuperObjectDrawMask (hSO) & ~ GLI_C_lIsNotGrided); }
/* END ANNECY AV }*/
  }
 
  
  /* {
      char s[200];
      sprintf(s,"Lecture : State n°%d  --  Frame n°%d\n",wStateNumber,ucForcedFrame);
      OutputDebugString(s);
  }*/
 
  
 
/*  DEMO_fn_vGoToTheNextInfoBlock();*/
 
#ifdef D_DEMO_USE_VCR
  g_bDontTreatCharacter = FALSE;
#endif
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vGoToTheNextInfoBlock(void)
{
 /*       !!!       plus à jour !!!
  DEMO_FLAG_CAST  Demo_Flag=0;
  unsigned short  wSO_Number=0;
 
  // *********************** read in Memory ******************************
  #ifdef GIZ_DEBUG_DEMO_MEMORY
  {// debug
    M_DEMO_MOVE(long);
  }// debug
  #endif
 
  M_DEMO_READ(&Demo_Flag,DEMO_FLAG_CAST);
  M_DEMO_READ(&wSO_Number,unsigned short);
 
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_NEW_ENGINE_FRAME) )
  {
    #ifdef GIZ_DEBUG_DEMO_MEMORY
    {// debug
      M_DEMO_MOVE(long);
    }// debug
    #endif
 
    M_DEMO_MOVE(unsigned short);
  }
 
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_TRANSLATION_COMPLETE) )    {  M_DEMO_MOVE(MTH3D_tdstVector); }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ROTATION_COMPLETE) )    {  M_DEMO_MOVE(POS_tdstShortQuaternion); }
 
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_STATE_CHANGE) )    {  M_DEMO_MOVE(unsigned short); }
  
  if( DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_FRAME_CHANGE) )    {  M_DEMO_MOVE(unsigned char); }
 */
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vInitSOList(void)
{ 
	g_hDemoSOList=(DEMO_tdxHandleToDemoSOList)M_p_GameMallocInHLM(sizeof(LST2_M_StaticAnchorDeclaration(DEMO_tdxHandleToDemoSOElement)));		
	LST2_M_StaticInitAnchor(g_hDemoSOList);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
tdstDemoSOList * DEMO_fn_p_stCreateSOListElement(void)
{
   tdstDemoSOList* p_stDemoSOList;
 
    unsigned char bIsInLevelMode = GAM_fn_bIsEngineInLevelMode ();
    GAM_fn_vSetEngineInLevelMode ();
 
    p_stDemoSOList=(tdstDemoSOList *)M_p_GameMallocInHLM(sizeof(tdstDemoSOList));
 
    if (! bIsInLevelMode)  GAM_fn_vSetEngineInFixMode ();
 
   p_stDemoSOList->hCharacterSupObject = NULL;
   p_stDemoSOList->ucActivate = FALSE;
   p_stDemoSOList->ucReInit = TRUE;
   LST2_M_StaticInitElement(p_stDemoSOList);
   return p_stDemoSOList;
}
 
void DEMO_fn_vStartOfAlwaysCreation (void)
{
    g_iFirstAlwaysNumber = LST2_M_StaticGetNumberOfElements (g_hDemoSOList);
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vPutCharacterInList(HIE_tdxHandleToSuperObject hSO)
{ 
  tdstDemoSOList *p_stListElement;
 
  p_stListElement = DEMO_fn_p_stCreateSOListElement();
  p_stListElement->hCharacterSupObject = hSO;
 
  LST2_M_StaticAddTail(g_hDemoSOList,p_stListElement);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
unsigned short DEMO_fn_wGetNumberFromSO(HIE_tdxHandleToSuperObject hSO)
{ 
  tdstDemoSOList *p_stListElement;
  unsigned short uwIndex;
 
  LST2_M_StaticForEachElementOf(g_hDemoSOList,p_stListElement,uwIndex)
  {
	if (p_stListElement->hCharacterSupObject==hSO)  return(uwIndex);
  }
  return((unsigned short)0xffff);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
tdstDemoSOList * DEMO_fn_p_stGetInfosPointerFromSO(HIE_tdxHandleToSuperObject hSO)
{ 
  tdstDemoSOList *p_stListElement;
  unsigned short uwIndex;
 
  LST2_M_StaticForEachElementOf(g_hDemoSOList,p_stListElement,uwIndex)
  {
	if (p_stListElement->hCharacterSupObject==hSO)  return(p_stListElement);
  }
  return(NULL);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vReInitSOList(void)
{ 
  tdstDemoSOList *p_stListElement;
  unsigned short uwIndex;
 
  LST2_M_StaticForEachElementOf(g_hDemoSOList,p_stListElement,uwIndex)
  {
   p_stListElement->ucActivate = FALSE;
   p_stListElement->ucReInit = TRUE;
   p_stListElement->ucDeltaCounter = 0;
  }
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
unsigned short DEMO_fn_vFindMainCharacterNumber(void)
{
  return (DEMO_fn_wGetNumberFromSO(MC_fn_hGetCharacterInMainCharacterNode(MC_fn_hGetFirstMainCharNode())));
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
unsigned short DEMO_fn_vFindMainCameraNumber(void)
{
  return (DEMO_fn_wGetNumberFromSO(CAM_fn_hGetActiveCamera(CAM_e_MainViewport)));
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
int DEMO_fn_vTestIfThisIsAGraphicCharacter(HIE_tdxHandleToSuperObject hSO)
{
  MS_tdxHandleTo3dData   h_Current3dData = M_GetMSHandle(hSO,3dData);
 
  if (h_Current3dData->h_CurrentState==NULL)   return FALSE;
  if (h_Current3dData->h_CurrentState->p_stAnim==NULL)  return FALSE;
 
  return TRUE;
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
#ifndef RETAIL
void DEMO_SaveInfo(HIE_tdxHandleToSuperObject hSO)
{
  unsigned short  wCurrentSO_Number;
  if(g_DEMO_KeybTest != DEMO_RECORD) return;
 
  wCurrentSO_Number = DEMO_fn_wGetNumberFromSO(hSO);
 
  if( DEMO_fn_vTestIfThisIsAGraphicCharacter(hSO) )
  {
/*    {
      char s[200];
      sprintf(s,"%ld S: %d\n",g_stEngineStructure.stEngineTimer.ulTrameNumber,wCurrentSO_Number);
      OutputDebugString(s);
    }
*/
    DEMO_fn_vSaveACompleteCharacterPosition(hSO); return;
  }
  /*
  if( DEMO_fn_wGetNumberFromSO(hSO) == DEMO_fn_vFindMainCharacterNumber() )
  {
    DEMO_fn_vSaveACompleteCharacterPosition(hSO); return;
  }*/
  if( DEMO_fn_wGetNumberFromSO(hSO) == DEMO_fn_vFindMainCameraNumber() )
  {
/*    {
      char s[200];
      sprintf(s,"%ld S: %d\n",g_stEngineStructure.stEngineTimer.ulTrameNumber,wCurrentSO_Number);
      OutputDebugString(s);
    }*/
    DEMO_fn_vSaveACompleteCharacterPosition(hSO); return;
  }
 
}
#endif /* RETAIL */

/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/

#if !defined(PRESS_DEMO)
 
#ifdef D_DEMO_USE_VCR
 
void DEMO_fn_vTestActionForVCR (void)
{
    static int bSpaceKeyWasPressed       = FALSE;
    static int bRightKeyWasPressed       = FALSE;
    static int bLeftKeyWasPressed        = FALSE;
    static int bPauseDemoNextFrame       = FALSE;
    static int bPauseDemoWhenKeyReleased = FALSE;
 
    /* ***** No VCR or end of VCR mode ******/
    if (g_bVCRMode == FALSE || g_b_DEMO_VCRIsUsable == FALSE)
    {
        bSpaceKeyWasPressed       = FALSE;
        bLeftKeyWasPressed        = FALSE;
        bRightKeyWasPressed       = FALSE;
        bPauseDemoNextFrame       = FALSE;
        bPauseDemoWhenKeyReleased = FALSE;
        return;
    }
 
    /* ***** Does the demo have to return to pause mode ? ******/
    if (bPauseDemoNextFrame)
    {
        g_bVCR_DemoIsPaused = TRUE;
        bPauseDemoNextFrame = FALSE;
    }
 
    /* ***** 'SPACE' key : pause or move one frame forward ******/
    if (GetAsyncKeyState (VK_SPACE) & 0x8000)
    {
        if (! bSpaceKeyWasPressed)
        {
            bSpaceKeyWasPressed = TRUE;
 
            if (g_bVCR_DemoIsPaused)
            {
                g_bVCR_DemoIsPaused = FALSE;    /* Press 'SPACE' when demo is in paused to move forward one frame*/
                bPauseDemoNextFrame = TRUE;
            }
            else g_bVCR_DemoIsPaused = TRUE;    /* Press 'SPACE' when demo is playing to pause the demo*/
        }
    }
    else bSpaceKeyWasPressed = FALSE;
 
    /* ***** 'RETURN' key : exit demo pause mode ******/
    if (GetAsyncKeyState (VK_RETURN) & 0x8000)  g_bVCR_DemoIsPaused = FALSE;
 
    /* ***** ''RIGHT' key : move forward faster ******/
    if (GetAsyncKeyState (VK_RIGHT) & 0x8000)
    {
        bRightKeyWasPressed = TRUE;
 
        if (g_bVCR_DemoIsPaused || bPauseDemoWhenKeyReleased)
        {                                                               /* Press 'RIGHT' when demo is paused to move forward*/
            int ShiftKeyPressed   = GetAsyncKeyState (VK_SHIFT)   & 0x8000;
            int ControlKeyPressed = GetAsyncKeyState (VK_CONTROL) & 0x8000;
 
            g_bVCR_DemoIsPaused       = FALSE;
            bPauseDemoWhenKeyReleased = TRUE;
 
            if (ShiftKeyPressed || ControlKeyPressed)                   /* Press 'Shift+RIGHT' when demo is paused to move forward  3 times faster*/
            {                                                           /* Press 'Ctrl+RIGHT'  when demo is paused to move forward 10 times faster*/
                g_ulDEMO_StartDEMO_EngineTimerCount -= g_stEngineStructure.stEngineTimer.ulUsefulDeltaTime *
                                                       (ControlKeyPressed ? 9 : 2);
            }
        }
        else
        {
            int  iSpeed = 1;                                            /* Press 'RIGHT' when demo is playing to move forward twice faster*/
            if (GetAsyncKeyState (VK_CONTROL) & 0x8000)  iSpeed = 9;    /* Press 'Ctrl+RIGHT' to move forward 10 times faster*/
            else
            if (GetAsyncKeyState (VK_SHIFT)   & 0x8000)  iSpeed = 2;    /* Press 'Shift+RIGHT' to move forward 3 times faster*/
 
            g_ulDEMO_StartDEMO_EngineTimerCount -= g_stEngineStructure.stEngineTimer.ulUsefulDeltaTime * iSpeed;
        }
    }
    else
    if (bRightKeyWasPressed)
    {
        bRightKeyWasPressed = FALSE;
 
        if (bPauseDemoWhenKeyReleased)
        {
            g_bVCR_DemoIsPaused       = TRUE;
            bPauseDemoWhenKeyReleased = FALSE;
        }
    }
 
    /* ***** 'LEFT' key : move backward ******/
    if ((GetAsyncKeyState (VK_LEFT) & 0x8000) && gl_DEMO_CurrentFrame >= 3)
    {
        bLeftKeyWasPressed = TRUE;
 
        if (g_bVCR_DemoIsPaused || bPauseDemoWhenKeyReleased)
        {
            int ShiftKeyPressed   = GetAsyncKeyState (VK_SHIFT)   & 0x8000;
            int ControlKeyPressed = GetAsyncKeyState (VK_CONTROL) & 0x8000;
 
            g_bVCR_DemoIsPaused       = FALSE;
            bPauseDemoWhenKeyReleased = TRUE;
 
            if (ShiftKeyPressed || ControlKeyPressed)                   /* Press 'Shift+LEFT' when demo is paused to move backward  3 times faster*/
            {                                                           /* Press 'Ctrl+LEFT'  when demo is paused to move backward 10 times faster*/
                g_ulDEMO_StartDEMO_EngineTimerCount -= g_stEngineStructure.stEngineTimer.ulUsefulDeltaTime *
                                                       (ControlKeyPressed ? 9 : 2);
            }
 
            gl_DEMO_CurrentFrame --;
            g_bVCR_ReverseMode = TRUE;
        }
        else
        {
            /*???*/
        }
    }
    else
    if (bLeftKeyWasPressed)
    {
        bLeftKeyWasPressed = FALSE;
 
        if (bPauseDemoWhenKeyReleased)
        {
            g_bVCR_DemoIsPaused       = TRUE;
            bPauseDemoWhenKeyReleased = FALSE;
        }
    }
}
#endif /* D_DEMO_USE_VCR */

#endif /* PRESS_DEMO */
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
ACP_tdxBool DEMO_fn_bUpdateDynamicHierarchy(void)
{
  long i;
  unsigned short  wCurrentSO_Number;
  tdstDemoSOList *p_stListElement;
  DEMO_FLAG_CAST  Demo_Flag=0;  
  unsigned long   ulDEMO_DeltaEngineTimerCount;
  int             bSuperimposedSO;

#if !defined(PRESS_DEMO)
#ifdef D_DEMO_USE_VCR
  int             bDontLookAtNewEngineFrame;
#endif
#endif /* PRESS_DEMO */
 
  /* Do we have to wait before playing the demo ?*/
  if (g_iNbFramesToWaitBeforePlaying)
  {
      if (-- g_iNbFramesToWaitBeforePlaying == 0)  DEMO_fn_vPrepareForReadNow ();
      return TRUE;
  }

#if !defined(PRESS_DEMO)
 
#ifdef D_DEMO_USE_VCR
  DEMO_fn_vTestActionForVCR ();
  if (g_bVCR_DemoIsPaused)
  {
      /* Increaze the 'start engine timer count' to be ok when the demo will continue*/
      /* ( + 10 to be sure to do only one frame when the demo will continue)*/
      g_ulDEMO_StartDEMO_EngineTimerCount = g_stEngineStructure.stEngineTimer.ulCurrentTimerCount - g_ulDEMO_CurrentTimerCount + 10;
 
      return TRUE;
  }
 
  bDontLookAtNewEngineFrame = g_bVCR_ReverseMode;    /* In reverse mode, we dont look at the NEW ENGINE FRAME flag for the first frame*/
#endif

#endif /* PRESS_DEMO */
 
  /* Stop the demo if we have reach the end or*/
  /* if a key or a button is pressed (only if we have been asked to test the input and after 60 frames (to let the user release the key to start the demo))*/
  if( gl_DEMO_CurrentFrame >= gl_DEMO_NumberOfFrames ||
      (g_bTestInputDuringDemo && gl_DEMO_CurrentFrame > 60 && IPT_g_bAtLeastOneActionIsValidated
#ifdef D_DEMO_USE_VCR
      && g_bVCRMode == FALSE    /* Don't test the input in VCR mode*/
#endif
      ))
  {
	DEMO_fn_vStopWhenPlaying ();
 
    /* Reinit the map even if another map will be loaded, because one more frame will be computed before the load*/
    fn_vReinitTheMap ();
 
    if (* g_szMapNameToLoadWhenDemoFinished != 0)
    {
         GAM_fn_vAskToChangeLevel (g_szMapNameToLoadWhenDemoFinished , FALSE);
         * g_szMapNameToLoadWhenDemoFinished = 0;
    }
    return FALSE;
  }
 
  ulDEMO_DeltaEngineTimerCount =    g_stEngineStructure.stEngineTimer.ulCurrentTimerCount
                                  - g_ulDEMO_StartDEMO_EngineTimerCount;
 
  if(!gl_DEMO_ConstantSpeed)
  {
    if(g_ulDEMO_CurrentTimerCount > ulDEMO_DeltaEngineTimerCount) TMR_fn_wWait(g_ulDEMO_CurrentTimerCount-ulDEMO_DeltaEngineTimerCount);
  }
 
  do
  {
#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode)
    {
        g_lNumberOfCharacterFrame --;
        gp_DEMO_CurrentPosInMemory       = gp_DEMO_AllocatedMemory       + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndex;
        gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_AllocatedMemoryForVCR + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndexForVCR;
    }
    else
    {
        assert (gp_DEMO_CurrentPosInMemory - gp_DEMO_AllocatedMemory == gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndex);
    }
#endif
 
/* ???
{
    char szText [128];
    sprintf (szText , "CurrentFrame = %4i   CharacterFrame = %4i\n" , gl_DEMO_CurrentFrame , g_lNumberOfCharacterFrame);
    OutputDebugString (szText);
}
*/
 
    #ifdef GIZ_DEBUG_DEMO_MEMORY
        M_DEMO_LOOK(&Demo_Flag,4,DEMO_FLAG_CAST);
    #else
        M_DEMO_LOOK(&Demo_Flag,0,DEMO_FLAG_CAST);
    #endif
 
#if !defined(PRESS_DEMO)

#ifdef D_DEMO_USE_VCR
    if (g_bVCR_ReverseMode && bDontLookAtNewEngineFrame == FALSE && DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_NEW_ENGINE_FRAME))
    {
        unsigned short usDeltaTimerCount;
 
        #ifdef GIZ_DEBUG_DEMO_MEMORY
            M_DEMO_LOOK (& usDeltaTimerCount , DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_COMPRESS_ACTOR_NUMBER) ? 11 : 12 , unsigned short);
        #else
            M_DEMO_LOOK (& usDeltaTimerCount , DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_COMPRESS_ACTOR_NUMBER) ?  3 :  4 , unsigned short);
        #endif
 
        /* A new frame begins : do we continue or not ?*/
        if (gl_DEMO_ConstantSpeed == FALSE && g_ulDEMO_CurrentTimerCount + usDeltaTimerCount < ulDEMO_DeltaEngineTimerCount && gl_DEMO_CurrentFrame > 2)
        {   /* We continue, so we decreaze the frame number*/
            gl_DEMO_CurrentFrame --;
        }
        else
        {   /* We don't continue : we advance to the beginning of the frame*/
            g_lNumberOfCharacterFrame ++;
            gp_DEMO_CurrentPosInMemory       = gp_DEMO_AllocatedMemory       + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndex;
            gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_AllocatedMemoryForVCR + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndexForVCR;
            break;
        }
    }
#endif

#endif /* PRESS_DEMO */
  
    if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_EXTENDED))
    {
      DEMO_fn_vReadAndTreatAllExtendedFlag(Demo_Flag);
    }
    else
    {
        if (DEMO_TEST_FLAG (Demo_Flag , DEMO_FLAG_COMPRESS_ACTOR_NUMBER))
        {
            unsigned char ucCurrentSO_Number;
            #ifdef GIZ_DEBUG_DEMO_MEMORY
                M_DEMO_LOOK (& ucCurrentSO_Number , 4 + sizeof (DEMO_FLAG_CAST) , unsigned char);
            #else
                M_DEMO_LOOK (& ucCurrentSO_Number , sizeof (DEMO_FLAG_CAST) , unsigned char);
            #endif
            wCurrentSO_Number = ucCurrentSO_Number;
        }
        else
        {
            #ifdef GIZ_DEBUG_DEMO_MEMORY
                M_DEMO_LOOK(&wCurrentSO_Number , 4 + sizeof(DEMO_FLAG_CAST),unsigned short);
            #else
                M_DEMO_LOOK(&wCurrentSO_Number , sizeof(DEMO_FLAG_CAST),unsigned short);
            #endif
        }
 
      /* Check if this is a superimposed super object*/
      if (wCurrentSO_Number < 16384)
      {
          bSuperimposedSO = FALSE;
      }
      else
      {
          bSuperimposedSO = TRUE;
          wCurrentSO_Number -= 16384;
      }
 
      LST2_M_StaticGetElementNumber(g_hDemoSOList,p_stListElement,wCurrentSO_Number,i);
 
      if (bSuperimposedSO)  HIE_fn_SO_vSetSuperimposedFlag (p_stListElement->hCharacterSupObject);
  
      if(     DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVATE)
          ||  DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_DESACTIVATE)
          ||  DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_ACTIVATE_CANAL)
          ||  DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_DESACTIVATE_CANAL)
/*          ||  DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_SECTOR_CHANGE) */
        )
      {
        if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_IS_AN_ALWAYS))
        {
          DEMO_fn_vReadAlwaysActivationDesactivation(p_stListElement->hCharacterSupObject);
        }
        else
        {
/*
          if(DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_SECTOR_CHANGE))
          {
            DEMO_fn_vReadAndDoSectorChange(p_stListElement->hCharacterSupObject);
          }
          else
*/
          {
            DEMO_fn_vReadAndChangeActivationDesactivation(p_stListElement->hCharacterSupObject); 
          }
        }
      }
      else
      {
		tdstEngineObject * p_stObject = (tdstEngineObject*)HIE_fn_hGetSuperObjectObject(p_stListElement->hCharacterSupObject);
 
#ifdef D_DEMO_USE_VCR
/* ???
        if (g_bVCR_ReverseMode)
        {
            int iAlwaysNumber = p_stListElement -> hCharacterSupObject - g_stAlways . d_stAlwaysSuperObject;
            if (iAlwaysNumber >= 0 && iAlwaysNumber < (int) g_stAlways . ulMaxNumberOfAlways)
            {
                // This is an always : let's see if it is created or not
                // (because in reverse mode, it can be created later in this frame)
                if (g_stAlways . d_hAlwaysGenerator [iAlwaysNumber] == NULL) g_bDontTreatCharacter = TRUE;
            }
        }
 
        if (g_bDontTreatCharacter)
        {
            DEMO_fn_vGetACompleteCharacterPosition (p_stListElement -> hCharacterSupObject);
        }
        else
*/
#endif
        {
            M_ActivateObject(p_stObject);
            fn_vStandardGameSetLastTrame(p_stObject->h_StandardGame,g_stEngineStructure.stEngineTimer.ulTrameNumber-1);
            fn_vTreatCharactersInSector(p_stListElement->hCharacterSupObject,FALSE);      
        }
      }
    }
 
/* ???
    {
      char s[200];
      sprintf(s,"Demo : CurrentFrame %ld \t\tNumberOfFrames %ld \t\tCurrentTimerCount %ld \t\tDeltaEngineTimerCount %ld\n",gl_DEMO_CurrentFrame,gl_DEMO_NumberOfFrames,g_ulDEMO_CurrentTimerCount,ulDEMO_DeltaEngineTimerCount);
      OutputDebugString(s);
    }
*/

#if !defined(PRESS_DEMO)

#ifdef D_DEMO_USE_VCR
 
    if (bDontLookAtNewEngineFrame)  Demo_Flag &= ~ DEMO_FLAG_NEW_ENGINE_FRAME;
    bDontLookAtNewEngineFrame = FALSE;    /* Only for one frame*/
 
    if (! g_bVCR_ReverseMode)
    {
        g_lNumberOfCharacterFrame ++;
    }
    else
    {
        /* Set the pointer to the correct values*/
        gp_DEMO_CurrentPosInMemory       = gp_DEMO_AllocatedMemory       + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndex;
        gp_DEMO_CurrentPosInMemoryForVCR = gp_DEMO_AllocatedMemoryForVCR + gp_DEMO_BeginningOfCharacterFrame [g_lNumberOfCharacterFrame] . lCurrentIndexForVCR;
    }
#endif

#endif /* PRESS_DEMO */
 
    if(gl_DEMO_CurrentFrame >= gl_DEMO_NumberOfFrames) break;
 
  } while( !DEMO_TEST_FLAG(Demo_Flag,DEMO_FLAG_NEW_ENGINE_FRAME)
            || (!gl_DEMO_ConstantSpeed && g_ulDEMO_CurrentTimerCount < ulDEMO_DeltaEngineTimerCount) );
 
#ifdef D_DEMO_USE_VCR
  g_bVCR_ReverseMode = FALSE;    /* This flag is only valid for one frame*/
 
  if (g_bVCRMode)
  {
      strcpy (g_szVCRMode , g_b_DEMO_VCRIsUsable ? "VCR" : "VCR not available");
      itoa (gl_DEMO_CurrentFrame , g_szFrameNumber , 10);    /* To display the frame number on the screen*/
  }
  else
  {
      strcpy (g_szVCRMode , "");
      * g_szFrameNumber = 0;
  }
#endif
 
  /*OutputDebugString("------------\n");*/
  return TRUE;
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
 
/* ANNECY MT - 04/12/98 {*/
void DEMO_fn_vStopWhenPlaying()
{
    g_DEMO_KeybTest = DEMO_NOTHING;
/*    fn_vAddCommandLineAnswerWithScroll("DEMO STOP    NORMAL PLAY");*/
/*    OutputDebugString("DEMO STOP    NORMAL PLAY\n");*/
#ifndef RETAIL
    strcpy (g_szDemoMode , "");
#endif
 
#ifdef D_DEMO_USE_VCR
    g_bVCRMode = FALSE;
    * g_szVCRMode     = 0;
    * g_szFrameNumber = 0;
#endif
 
    if(g_DEMO_AutoPlay==TRUE) DEMO_fn_vStopAutoPlay();
}
/* END ANNECY MT }*/
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vInitGMTFixList(void)
{ 
	g_hDemoGMTFixList=(DEMO_tdxHandleToDemoGMTList)M_p_GameMallocInHLM(sizeof(LST2_M_StaticAnchorDeclaration(DEMO_tdxHandleToDemoGMTElement)));		
	LST2_M_StaticInitAnchor(g_hDemoGMTFixList);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vInitGMTLevelList(void)
{ 
	g_hDemoGMTLevelList=(DEMO_tdxHandleToDemoGMTList)M_p_GameMallocInHLM(sizeof(LST2_M_StaticAnchorDeclaration(DEMO_tdxHandleToDemoGMTElement)));		
	LST2_M_StaticInitAnchor(g_hDemoGMTLevelList);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
tdstDemoGMTList * DEMO_fn_p_stCreateGMTListElement(void)
{
    tdstDemoGMTList* p_stDemoGMTList;
 
    p_stDemoGMTList=(tdstDemoGMTList *)M_p_GameMallocInHLM(sizeof(tdstDemoGMTList));
 
    p_stDemoGMTList->hGameMaterial = NULL;
    LST2_M_StaticInitElement(p_stDemoGMTList);
    return p_stDemoGMTList;
}
 
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vPutGMTInList(GMT_tdxHandleToGameMaterial _hGameMaterial)
{ 
  if (DEMO_fn_wGetNumberFromGMT (_hGameMaterial) == 0xffff)
  {
	tdstDemoGMTList *p_stListElement;
 
	p_stListElement = DEMO_fn_p_stCreateGMTListElement();
	p_stListElement->hGameMaterial = _hGameMaterial;
 
	if (GAM_fn_bIsEngineInFixMode ())
	{
		LST2_M_StaticAddTail(g_hDemoGMTFixList,p_stListElement);
	}
	else
	{
		LST2_M_StaticAddTail(g_hDemoGMTLevelList,p_stListElement);
	}
  }
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
 
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
unsigned short DEMO_fn_wGetNumberFromGMT(GMT_tdxHandleToGameMaterial _hGameMaterial)
{ 
  tdstDemoGMTList *p_stListElement;
  unsigned short uwIndex;

  /* search in Fix List */
  LST2_M_StaticForEachElementOf(g_hDemoGMTFixList,p_stListElement,uwIndex)
  {
	if (p_stListElement->hGameMaterial == _hGameMaterial)  return(uwIndex);
  }
  if (g_hDemoGMTLevelList)
  {
	  /* search in level list */
	  LST2_M_StaticForEachElementOf(g_hDemoGMTLevelList,p_stListElement,uwIndex)
	  {
		if (p_stListElement->hGameMaterial == _hGameMaterial)
			return ( unsigned short ) ( uwIndex + (unsigned short) LST2_M_StaticGetNumberOfElements ( g_hDemoGMTFixList ) );
	  }
  }
  return((unsigned short)0xffff);
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
 
/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
GMT_tdxHandleToGameMaterial DEMO_fn_hGetGMTFromList(unsigned short _uwIndex)
{
	unsigned short i;
	tdstDemoGMTList *p_stListElement = NULL;

	if (_uwIndex < LST2_M_StaticGetNumberOfElements(g_hDemoGMTFixList))
	{
		LST2_M_StaticGetElementNumber(g_hDemoGMTFixList,p_stListElement,_uwIndex,i);		
	}
	else
	{
		_uwIndex -= (unsigned short) LST2_M_StaticGetNumberOfElements ( g_hDemoGMTFixList ) ;
		LST2_M_StaticGetElementNumber(g_hDemoGMTLevelList,p_stListElement,_uwIndex,i);
	}
	return p_stListElement ? p_stListElement -> hGameMaterial : NULL;
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/

/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vDesInitLevel()
{
	g_hDemoSOList = NULL;
	g_hDemoGMTLevelList = NULL;
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/

/*.............############################################################.............*/
/*.....############################################################################.....*/
/*.####################################################################################.*/
void DEMO_fn_vDesInitGame()
{
	g_hDemoGMTFixList = NULL;
	DEMO_fn_vDesInitLevel ();
}
/*.####################################################################################.*/
/*.....############################################################################.....*/
/*.............############################################################.............*/
