[bb] SimpleREPLAY 1.1 by ToeB [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:43

Previous topic - Next topic

BlitzBot

Title : SimpleREPLAY 1.1
Author : ToeB
Posted : 1+ years ago

Description : This short library can record automaticly 3D positions, rotations and scales,  you only have to say which entity you want to record. furthermore you can record demos like in CS or cod, and you can replay ur record in variable timescale (negative timescale, too !)

You can download the full package with samples here :
<a href="http://www.toebproductions.bplaced.net/SimpleREPLAY.rar" target="_blank">http://www.toebproductions.bplaced.net/SimpleREPLAY.rar</a>

for the german article look here :
<a href="http://www.blitzforum.de/forum/viewtopic.php?t=35933" target="_blank">http://www.blitzforum.de/forum/viewtopic.php?t=35933</a>


Code :
Code: blitzbasic
;=======================+ 
;---- SimpleREPLAY -----| 
;-----------------------| 
;--------- by ----------| 
;-----------------------| 
;-------- ToeB ---------| 
;--- Tobias  Hendricks -| 
;=======================+ 

;---------------------------------------- 
; Globals 
;---------------------------------------- 
Global REPLAY_RecordTime 
Global REPLAY_RecordMs 
Global REPLAY_GlobalTime 
Global REPLAY_RecordOn 
Global REPLAY_RecordDataCount 

Global REPLAY_PlayOn 
Global REPLAY_PlayTime# 

Global REPLAY_FrameMS 
Global REPLAY_FrameTime 

Global REPLAY_DemoOn 
Global REPLAY_DemoStream 
Global REPLAY_DemoSeek 

Global REPLAY_EntityCount 

;---------------------------------------- 
; Types 
;---------------------------------------- 
Type REPLAY_Object 
   Field Entity 
   Field Animated 
   Field LastData.REPLAY_Data 
   Field ReplayBank 
   Field Name$, ID 
End Type 


Type REPLAY_Data 
   Field Info.REPLAY_Object 
   Field LastData.REPLAY_Data 
   Field NextData.REPLAY_Data 
   Field XPos#, YPos#, ZPos# 
   Field XRot#, YRot#, ZRot# 
   Field XScal#, YScal#, ZScal# 
   Field Time# 
End Type 

;---------------------------------------- 
; Functions 
;---------------------------------------- 
Function REPLAY_Init( tmpRecordTime=100 ) 

   REPLAY_RecordTime = tmpRecordTime 
   REPLAY_RecordMs = 0 
    
End Function 

  
Function REPLAY_AddEntity( tmpEntity ) 

   If tmpEntity = 0 Then Return 
    
   Local tmpObject.REPLAY_Object = New REPLAY_Object 
    
   tmpObjectEntity = tmpEntity 
   tmpObjectAnimated = tmpAnimated 
   tmpObjectReplayBank = CreateBank( 0 ) 
   tmpObjectName$ = EntityName( tmpEntity ) 
    
   REPLAY_EntityCount = REPLAY_EntityCount + 1 
    
   Return Handle( tmpObject ) 
    
End Function 



Function REPLAY_Record( ) 

   Local tmpTime = MilliSecs() 
   Local tmpData.REPLAY_Data 
    
   If REPLAY_PlayOn = 1 Then 
      REPLAY_PlayStop( ) 
   EndIf 
    
   If REPLAY_RecordOn = 0 Then 
      REPLAY_GlobalTime = tmpTime 
      REPLAY_RecordMs = 0 
      REPLAY_RecordOn = 1 
      Delete Each REPLAY_Data 
      REPLAY_RecordDataCount = 0 
   EndIf 
    
   If REPLAY_RecordOn = 1 Then 
      If REPLAY_RecordMs <= tmpTime Then 
         For tmpObject.REPLAY_Object = Each REPLAY_Object 
            tmpData = New REPLAY_Data 
            tmpDataInfo = tmpObject 
            tmpDataLastData = tmpObjectLastData 
            If tmpDataLastData <> Null Then 
               tmpDataLastDataNextData = tmpData 
            EndIf 
            tmpObjectLastData = tmpData    
            tmpDataTime = tmpTime - REPLAY_GlobalTime 
            tmpDataXPos = EntityX( tmpObjectEntity, 1 )    
            tmpDataYPos = EntityY( tmpObjectEntity, 1 )    
            tmpDataPos = EntityZ( tmpObjectEntity, 1 )    
            tmpDataXRot = EntityPitch( tmpObjectEntity, 1 )    
            tmpDataYRot = EntityYaw( tmpObjectEntity, 1 )    
            tmpDataRot = EntityRoll( tmpObjectEntity, 1 ) 
            tmpDataXScal = REPLAY_GetEntityScale( tmpObjectEntity, 0 ) 
            tmpDataYScal = REPLAY_GetEntityScale( tmpObjectEntity, 1 ) 
            tmpDataScal = REPLAY_GetEntityScale( tmpObjectEntity, 2 ) 
         Next 
         REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
         REPLAY_RecordMs = tmpTime + REPLAY_RecordTime 
      EndIf 
   EndIf 
    
End Function 


Function REPLAY_RecordDemo( tmpPath$ ) 
    
   Local tmpObject.REPLAY_Object 
   Local tmpTime = MilliSecs( ) 
   Local tmpID 
    
   If REPLAY_DemoOn = 0 Then 
      REPLAY_RecordStop( ) 
      REPLAY_PlayStop( ) 
      REPLAY_DemoStream = WriteFile( tmpPath$ ) : CloseFile( REPLAY_DemoStream ) 
      REPLAY_DemoStream = OpenFile( tmpPath$ ) 
      If REPLAY_DemoStream <> 0 Then 
         WriteInt( REPLAY_DemoStream, REPLAY_EntityCount ) 
         tmpID = 0 
         For tmpObject = Each REPLAY_Object 
            tmpID = tmpID + 1 : tmpObjectID = tmpID 
            WriteInt( REPLAY_DemoStream, tmpObjectID ) 
            WriteString( REPLAY_DemoStream, tmpObjectName$ ) 
         Next        
         REPLAY_DemoOn = 1 
         REPLAY_DemoSeek = 0 
         REPLAY_RecordOn = 1 
         REPLAY_GlobalTime = tmpTime 
      EndIf 
   EndIf 
    
   If REPLAY_DemoOn = 1 Then 
      If REPLAY_RecordMs <= tmpTime Then 
         For tmpObject.REPLAY_Object = Each REPLAY_Object 
            WriteInt( REPLAY_DemoStream, tmpObjectID ) 
            WriteInt( REPLAY_DemoStream, tmpTime - REPLAY_GlobalTime ) 
            WriteFloat( REPLAY_DemoStream, EntityX( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, EntityY( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, EntityZ( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, EntityPitch( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, EntityYaw( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, EntityRoll( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 0 ) ) 
            WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 1 ) ) 
            WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 2 ) ) 
         Next 
         REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
         REPLAY_RecordMs = tmpTime + REPLAY_RecordTime 
      EndIf 
   EndIf 
    
End Function 

Function REPLAY_RecordStop( ) 

   REPLAY_RecordOn = 0 
   REPLAY_DemoOn = 0 
   If REPLAY_DemoStream <> 0 Then 
      CloseFile( REPLAY_DemoStream ) 
      REPLAY_DemoStream = 0 
   EndIf 
    
End Function 


Function REPLAY_Play( tmpTimeScale#=1.0 ) 

   Local tmpObject.REPLAY_Object 
   Local tmpData.REPLAY_Data 
   Local tmpAnz, tmpBPos 
   Local tmpPos#, tmpPos1, tmpPos2 
   Local tmpTime = MilliSecs() 
   Local tmpRepTime, tmpInterpolFakt# 
   Local tmpXPos#[2], tmpYPos#[2], tmpZPos#[2] 
   Local tmpXRot#[2], tmpYRot#[2], tmpZRot#[2] 
   Local tmpXScal#[2], tmpYScal#[2], tmpZScal#[2] 
    
   REPLAY_FrameTime = tmpTime - REPLAY_FrameMs 
   REPLAY_FrameMs = tmpTime    
   If REPLAY_FrameTime = tmptime Then REPLAY_FrameTime = 16 
    
   If REPLAY_RecordOn = 1 Then 
      REPLAY_RecordStop( ) 
   EndIf 
    
   If REPLAY_PlayOn = 0 Then 
      tmpAnz = REPLAY_RecordDataCount * 40 
      For tmpObject = Each REPLAY_Object          
         ResizeBank( tmpObjectReplayBank, tmpAnz ) 
         tmpBPos = 0 
         For tmpData = Each REPLAY_Data 
            If tmpDataInfo = tmpObject Then 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 00, tmpDataXPos ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 04, tmpDataYPos ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 08, tmpDataPos ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 16, tmpDataXRot ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 20, tmpDataYRot ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 24, tmpDataRot ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 28, tmpDataXScal ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 32, tmpDataYScal ) 
               PokeFloat( tmpObjectReplayBank, tmpBPos + 36, tmpDataScal ) 
               tmpBPos = tmpBPos + 40 
            EndIf 
         Next 
      Next 
      REPLAY_PlayOn = 1 
      REPLAY_PlayTime = 1 
      REPLAY_Time = 0 
   EndIf 
    
   If REPLAY_PlayOn = 1 Then 
      tmpPos1 = Floor( REPLAY_PlayTime / REPLAY_RecordTime ) 
      tmpPos2 = Ceil( REPLAY_PlayTime / REPLAY_RecordTime ) 
      If tmpPos1 = tmpPos2 Then tmpPos2 = tmpPos2 + 1 
      If tmpPos1 > REPLAY_RecordDataCount Or tmpPos2 > REPLAY_RecordDataCount Then 
         tmpPos1 = REPLAY_RecordDataCount 
         tmpPos2 = REPLAY_RecordDataCount 
      EndIf 
      tmpPos = (REPLAY_PlayTime Mod REPLAY_RecordTime) / REPLAY_RecordTime 
      For tmpObject = Each REPLAY_Object 
         tmpXPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 00 ) 
         tmpYPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 04 ) 
         tmpZPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 08 ) 
         tmpXRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 16 ) 
         tmpYRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 20 ) 
         tmpZRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 24 ) 
         tmpXScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 28 ) 
         tmpYScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 32 ) 
         tmpZScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 36 ) 
          
         tmpXPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 00 ) 
         tmpYPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 04 ) 
         tmpZPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 08 ) 
         tmpXRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 16 ) 
         tmpYRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 20 ) 
         tmpZRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 24 ) 
         tmpXScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 28 ) 
         tmpYScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 32 ) 
         tmpZScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 36 )          
          
          
         tmpXPos[ 2 ] = tmpXPos[ 0 ] + ( tmpXPos[ 1 ] - tmpXPos[ 0 ] ) * tmpPos 
         tmpYPos[ 2 ] = tmpYPos[ 0 ] + ( tmpYPos[ 1 ] - tmpYPos[ 0 ] ) * tmpPos 
         tmpZPos[ 2 ] = tmpZPos[ 0 ] + ( tmpZPos[ 1 ] - tmpZPos[ 0 ] ) * tmpPos 
         tmpXRot[ 2 ] = tmpXRot[ 0 ] + ( tmpXRot[ 1 ] - tmpXRot[ 0 ] ) * tmpPos 
         tmpYRot[ 2 ] = tmpYRot[ 0 ] + ( tmpYRot[ 1 ] - tmpYRot[ 0 ] ) * tmpPos 
         tmpZRot[ 2 ] = tmpZRot[ 0 ] + ( tmpZRot[ 1 ] - tmpZRot[ 0 ] ) * tmpPos 
         tmpXScal[ 2 ] = tmpXScal[ 0 ] + ( tmpXScal[ 1 ] - tmpXScal[ 0 ] ) * tmpPos 
         tmpYScal[ 2 ] = tmpYScal[ 0 ] + ( tmpYScal[ 1 ] - tmpYScal[ 0 ] ) * tmpPos 
         tmpZScal[ 2 ] = tmpZScal[ 0 ] + ( tmpZScal[ 1 ] - tmpZScal[ 0 ] ) * tmpPos 
          
          
         PositionEntity tmpObjectEntity, tmpXPos[ 2 ], tmpYPos[ 2 ], tmpZPos[ 2 ], 1 
         RotateEntity tmpObjectEntity, tmpXRot[ 2 ], tmpYRot[ 2 ], tmpZRot[ 2 ], 1 
         ScaleEntity tmpObjectEntity, tmpXScal[ 2 ], tmpYScal[ 2 ], tmpXScal[ 2 ], 1 
      Next 
       
      tmpRepTime = REPLAY_RecordDataCount * REPLAY_RecordTime - REPLAY_RecordTime 
      REPLAY_PlayTime = ( REPLAY_PlayTime + Float( REPLAY_FrameTime ) * tmpTimeScale ) Mod tmpRepTime 
      If REPLAY_PlayTime < 0 Then REPLAY_PlayTime = tmpRepTime - REPLAY_RecordTime 

   EndIf 
    
End Function 

Function REPLAY_LoadDemo( tmpPath$ ) 

   Local tmpStream 
   Local tmpObjectCount 
   Local tmpID, tmpName$ 
   Local tmpObject.REPLAY_Object 
   Local tmpData.REPLAY_Data 
   Local tmpAktTime 
    
   Delete Each REPLAY_Data 
    
   tmpStream = ReadFile( tmpPath$ ) 
   If tmpStream = 0 Then Return 0 
    
   tmpObjectCount = ReadInt( tmpStream ) 
   For i = 1 To tmpObjectCount 
      tmpID = ReadInt( tmpStream ) 
      tmpName$ = ReadString( tmpStream ) 
      For tmpObject = Each REPLAY_Object 
         If tmpObjectName$ = tmpName$ Then 
            tmpObjectID = tmpID : Exit 
         EndIf 
      Next 
   Next 
   REPLAY_RecordDataCount = 0 
   tmpAktTime = -1 
   While Not Eof( tmpStream ) 
      tmpID = ReadInt( tmpStream ) 
      tmpData = New REPLAY_Data 
      tmpDataTime = ReadInt( tmpStream ) 
      tmpDataXPos = ReadFloat( tmpStream ) 
      tmpDataYPos = ReadFloat( tmpStream ) 
      tmpDataPos = ReadFloat( tmpStream ) 
      tmpDataXRot = ReadFloat( tmpStream ) 
      tmpDataYRot = ReadFloat( tmpStream ) 
      tmpDataRot = ReadFloat( tmpStream ) 
      tmpDataXScal = ReadFloat( tmpStream ) 
      tmpDataYScal = ReadFloat( tmpStream ) 
      tmpDataScal = ReadFloat( tmpStream ) 
      For tmpObject = Each REPLAY_Object 
         If tmpObjectID = tmpID Then 
            tmpDataInfo = tmpObject 
            tmpDataLastData = tmpObjectLastData 
            If tmpDataLastData <> Null Then 
               tmpDataLastDataNextData = tmpData 
            EndIf 
            tmpObjectLastData = tmpData    
            Exit 
         EndIf 
      Next 
      If tmpDataTime <> tmpAktTime Then 
         REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
         tmpAktTime = tmpDataTime 
      EndIf 
   Wend 
End Function 



Function REPLAY_PlayStop( ) 

   REPLAY_PlayOn = 0 
    
End Function  

Function REPLAY_GetEntityScale#( tmpEntity, tmpAxis=0 ) 
    
   If tmpEntity = 0 Then Return 0.0 

   Local tmpVX#, tmpVY#, tmpVZ# 
    
   If tmpAxis < 0 Then tmpAxis = 0 
   If tmpAxis > 2 Then tmpAxis = 2 
    
   tmpVX = GetMatElement( tmpEntity, tmpAxis, 0 ) 
   tmpVY = GetMatElement( tmpEntity, tmpAxis, 1 ) 
   tmpVZ = GetMatElement( tmpEntity, tmpAxis, 2 ) 
    
   Return ( Sqr( tmpVX*tmpVX + tmpVY*tmpVY + tmpVZ*tmpVZ ) ) 
    
End Function


Comments : none...