Ooops
January 15, 2021, 06:15:16 PM

Author Topic: [bb] SimpleREPLAY 1.1 by ToeB [ 1+ years ago ]  (Read 1486 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] SimpleREPLAY 1.1 by ToeB [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
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[/url]

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[/url]


Code :
Code: BlitzBasic
  1. ;=======================+
  2. ;---- SimpleREPLAY -----|
  3. ;-----------------------|
  4. ;--------- by ----------|
  5. ;-----------------------|
  6. ;-------- ToeB ---------|
  7. ;--- Tobias  Hendricks -|
  8. ;=======================+
  9.  
  10. ;----------------------------------------
  11. ; Globals
  12. ;----------------------------------------
  13. Global REPLAY_RecordTime
  14. Global REPLAY_RecordMs
  15. Global REPLAY_GlobalTime
  16. Global REPLAY_RecordOn
  17. Global REPLAY_RecordDataCount
  18.  
  19. Global REPLAY_PlayOn
  20. Global REPLAY_PlayTime#
  21.  
  22. Global REPLAY_FrameMS
  23. Global REPLAY_FrameTime
  24.  
  25. Global REPLAY_DemoOn
  26. Global REPLAY_DemoStream
  27. Global REPLAY_DemoSeek
  28.  
  29. Global REPLAY_EntityCount
  30.  
  31. ;----------------------------------------
  32. ; Types
  33. ;----------------------------------------
  34. Type REPLAY_Object
  35.    Field Entity
  36.    Field Animated
  37.    Field LastData.REPLAY_Data
  38.    Field ReplayBank
  39.    Field Name$, ID
  40. End Type
  41.  
  42.  
  43. Type REPLAY_Data
  44.    Field Info.REPLAY_Object
  45.    Field LastData.REPLAY_Data
  46.    Field NextData.REPLAY_Data
  47.    Field XPos#, YPos#, ZPos#
  48.    Field XRot#, YRot#, ZRot#
  49.    Field XScal#, YScal#, ZScal#
  50.    Field Time#
  51. End Type
  52.  
  53. ;----------------------------------------
  54. ; Functions
  55. ;----------------------------------------
  56. Function REPLAY_Init( tmpRecordTime=100 )
  57.  
  58.    REPLAY_RecordTime = tmpRecordTime
  59.    REPLAY_RecordMs = 0
  60.    
  61. End Function
  62.  
  63.  
  64. Function REPLAY_AddEntity( tmpEntity )
  65.  
  66.    If tmpEntity = 0 Then Return
  67.    
  68.    Local tmpObject.REPLAY_Object = New REPLAY_Object
  69.    
  70.    tmpObjectEntity = tmpEntity
  71.    tmpObjectAnimated = tmpAnimated
  72.    tmpObjectReplayBank = CreateBank( 0 )
  73.    tmpObjectName$ = EntityName( tmpEntity )
  74.    
  75.    REPLAY_EntityCount = REPLAY_EntityCount + 1
  76.    
  77.    Return Handle( tmpObject )
  78.    
  79. End Function
  80.  
  81.  
  82.  
  83. Function REPLAY_Record( )
  84.  
  85.    Local tmpTime = MilliSecs()
  86.    Local tmpData.REPLAY_Data
  87.    
  88.    If REPLAY_PlayOn = 1 Then
  89.       REPLAY_PlayStop( )
  90.    EndIf
  91.    
  92.    If REPLAY_RecordOn = 0 Then
  93.       REPLAY_GlobalTime = tmpTime
  94.       REPLAY_RecordMs = 0
  95.       REPLAY_RecordOn = 1
  96.       Delete Each REPLAY_Data
  97.       REPLAY_RecordDataCount = 0
  98.    EndIf
  99.    
  100.    If REPLAY_RecordOn = 1 Then
  101.       If REPLAY_RecordMs <= tmpTime Then
  102.          For tmpObject.REPLAY_Object = Each REPLAY_Object
  103.             tmpData = New REPLAY_Data
  104.             tmpDataInfo = tmpObject
  105.             tmpDataLastData = tmpObjectLastData
  106.             If tmpDataLastData <> Null Then
  107.                tmpDataLastDataNextData = tmpData
  108.             EndIf
  109.             tmpObjectLastData = tmpData    
  110.             tmpDataTime = tmpTime - REPLAY_GlobalTime
  111.             tmpDataXPos = EntityX( tmpObjectEntity, 1 )    
  112.             tmpDataYPos = EntityY( tmpObjectEntity, 1 )    
  113.             tmpDataPos = EntityZ( tmpObjectEntity, 1 )    
  114.             tmpDataXRot = EntityPitch( tmpObjectEntity, 1 )    
  115.             tmpDataYRot = EntityYaw( tmpObjectEntity, 1 )    
  116.             tmpDataRot = EntityRoll( tmpObjectEntity, 1 )
  117.             tmpDataXScal = REPLAY_GetEntityScale( tmpObjectEntity, 0 )
  118.             tmpDataYScal = REPLAY_GetEntityScale( tmpObjectEntity, 1 )
  119.             tmpDataScal = REPLAY_GetEntityScale( tmpObjectEntity, 2 )
  120.          Next
  121.          REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
  122.          REPLAY_RecordMs = tmpTime + REPLAY_RecordTime
  123.       EndIf
  124.    EndIf
  125.    
  126. End Function
  127.  
  128.  
  129. Function REPLAY_RecordDemo( tmpPath$ )
  130.    
  131.    Local tmpObject.REPLAY_Object
  132.    Local tmpTime = MilliSecs( )
  133.    Local tmpID
  134.    
  135.    If REPLAY_DemoOn = 0 Then
  136.       REPLAY_RecordStop( )
  137.       REPLAY_PlayStop( )
  138.       REPLAY_DemoStream = WriteFile( tmpPath$ ) : CloseFile( REPLAY_DemoStream )
  139.       REPLAY_DemoStream = OpenFile( tmpPath$ )
  140.       If REPLAY_DemoStream <> 0 Then
  141.          WriteInt( REPLAY_DemoStream, REPLAY_EntityCount )
  142.          tmpID = 0
  143.          For tmpObject = Each REPLAY_Object
  144.             tmpID = tmpID + 1 : tmpObjectID = tmpID
  145.             WriteInt( REPLAY_DemoStream, tmpObjectID )
  146.             WriteString( REPLAY_DemoStream, tmpObjectName$ )
  147.          Next        
  148.          REPLAY_DemoOn = 1
  149.          REPLAY_DemoSeek = 0
  150.          REPLAY_RecordOn = 1
  151.          REPLAY_GlobalTime = tmpTime
  152.       EndIf
  153.    EndIf
  154.    
  155.    If REPLAY_DemoOn = 1 Then
  156.       If REPLAY_RecordMs <= tmpTime Then
  157.          For tmpObject.REPLAY_Object = Each REPLAY_Object
  158.             WriteInt( REPLAY_DemoStream, tmpObjectID )
  159.             WriteInt( REPLAY_DemoStream, tmpTime - REPLAY_GlobalTime )
  160.             WriteFloat( REPLAY_DemoStream, EntityX( tmpObjectEntity, 1 ) )
  161.             WriteFloat( REPLAY_DemoStream, EntityY( tmpObjectEntity, 1 ) )
  162.             WriteFloat( REPLAY_DemoStream, EntityZ( tmpObjectEntity, 1 ) )
  163.             WriteFloat( REPLAY_DemoStream, EntityPitch( tmpObjectEntity, 1 ) )
  164.             WriteFloat( REPLAY_DemoStream, EntityYaw( tmpObjectEntity, 1 ) )
  165.             WriteFloat( REPLAY_DemoStream, EntityRoll( tmpObjectEntity, 1 ) )
  166.             WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 0 ) )
  167.             WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 1 ) )
  168.             WriteFloat( REPLAY_DemoStream, REPLAY_GetEntityScale( tmpObjectEntity, 2 ) )
  169.          Next
  170.          REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
  171.          REPLAY_RecordMs = tmpTime + REPLAY_RecordTime
  172.       EndIf
  173.    EndIf
  174.    
  175. End Function
  176.  
  177. Function REPLAY_RecordStop( )
  178.  
  179.    REPLAY_RecordOn = 0
  180.    REPLAY_DemoOn = 0
  181.    If REPLAY_DemoStream <> 0 Then
  182.       CloseFile( REPLAY_DemoStream )
  183.       REPLAY_DemoStream = 0
  184.    EndIf
  185.    
  186. End Function
  187.  
  188.  
  189. Function REPLAY_Play( tmpTimeScale#=1.0 )
  190.  
  191.    Local tmpObject.REPLAY_Object
  192.    Local tmpData.REPLAY_Data
  193.    Local tmpAnz, tmpBPos
  194.    Local tmpPos#, tmpPos1, tmpPos2
  195.    Local tmpTime = MilliSecs()
  196.    Local tmpRepTime, tmpInterpolFakt#
  197.    Local tmpXPos#[2], tmpYPos#[2], tmpZPos#[2]
  198.    Local tmpXRot#[2], tmpYRot#[2], tmpZRot#[2]
  199.    Local tmpXScal#[2], tmpYScal#[2], tmpZScal#[2]
  200.    
  201.    REPLAY_FrameTime = tmpTime - REPLAY_FrameMs
  202.    REPLAY_FrameMs = tmpTime    
  203.    If REPLAY_FrameTime = tmptime Then REPLAY_FrameTime = 16
  204.    
  205.    If REPLAY_RecordOn = 1 Then
  206.       REPLAY_RecordStop( )
  207.    EndIf
  208.    
  209.    If REPLAY_PlayOn = 0 Then
  210.       tmpAnz = REPLAY_RecordDataCount * 40
  211.       For tmpObject = Each REPLAY_Object          
  212.          ResizeBank( tmpObjectReplayBank, tmpAnz )
  213.          tmpBPos = 0
  214.          For tmpData = Each REPLAY_Data
  215.             If tmpDataInfo = tmpObject Then
  216.                PokeFloat( tmpObjectReplayBank, tmpBPos + 00, tmpDataXPos )
  217.                PokeFloat( tmpObjectReplayBank, tmpBPos + 04, tmpDataYPos )
  218.                PokeFloat( tmpObjectReplayBank, tmpBPos + 08, tmpDataPos )
  219.                PokeFloat( tmpObjectReplayBank, tmpBPos + 16, tmpDataXRot )
  220.                PokeFloat( tmpObjectReplayBank, tmpBPos + 20, tmpDataYRot )
  221.                PokeFloat( tmpObjectReplayBank, tmpBPos + 24, tmpDataRot )
  222.                PokeFloat( tmpObjectReplayBank, tmpBPos + 28, tmpDataXScal )
  223.                PokeFloat( tmpObjectReplayBank, tmpBPos + 32, tmpDataYScal )
  224.                PokeFloat( tmpObjectReplayBank, tmpBPos + 36, tmpDataScal )
  225.                tmpBPos = tmpBPos + 40
  226.             EndIf
  227.          Next
  228.       Next
  229.       REPLAY_PlayOn = 1
  230.       REPLAY_PlayTime = 1
  231.       REPLAY_Time = 0
  232.    EndIf
  233.    
  234.    If REPLAY_PlayOn = 1 Then
  235.       tmpPos1 = Floor( REPLAY_PlayTime / REPLAY_RecordTime )
  236.       tmpPos2 = Ceil( REPLAY_PlayTime / REPLAY_RecordTime )
  237.       If tmpPos1 = tmpPos2 Then tmpPos2 = tmpPos2 + 1
  238.       If tmpPos1 > REPLAY_RecordDataCount Or tmpPos2 > REPLAY_RecordDataCount Then
  239.          tmpPos1 = REPLAY_RecordDataCount
  240.          tmpPos2 = REPLAY_RecordDataCount
  241.       EndIf
  242.       tmpPos = (REPLAY_PlayTime Mod REPLAY_RecordTime) / REPLAY_RecordTime
  243.       For tmpObject = Each REPLAY_Object
  244.          tmpXPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 00 )
  245.          tmpYPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 04 )
  246.          tmpZPos[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 08 )
  247.          tmpXRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 16 )
  248.          tmpYRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 20 )
  249.          tmpZRot[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 24 )
  250.          tmpXScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 28 )
  251.          tmpYScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 32 )
  252.          tmpZScal[ 0 ] = PeekFloat( tmpObjectReplayBank, tmpPos1 * 40 + 36 )
  253.          
  254.          tmpXPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 00 )
  255.          tmpYPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 04 )
  256.          tmpZPos[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 08 )
  257.          tmpXRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 16 )
  258.          tmpYRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 20 )
  259.          tmpZRot[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 24 )
  260.          tmpXScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 28 )
  261.          tmpYScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 32 )
  262.          tmpZScal[ 1 ] = PeekFloat( tmpObjectReplayBank, tmpPos2 * 40 + 36 )          
  263.          
  264.          
  265.          tmpXPos[ 2 ] = tmpXPos[ 0 ] + ( tmpXPos[ 1 ] - tmpXPos[ 0 ] ) * tmpPos
  266.          tmpYPos[ 2 ] = tmpYPos[ 0 ] + ( tmpYPos[ 1 ] - tmpYPos[ 0 ] ) * tmpPos
  267.          tmpZPos[ 2 ] = tmpZPos[ 0 ] + ( tmpZPos[ 1 ] - tmpZPos[ 0 ] ) * tmpPos
  268.          tmpXRot[ 2 ] = tmpXRot[ 0 ] + ( tmpXRot[ 1 ] - tmpXRot[ 0 ] ) * tmpPos
  269.          tmpYRot[ 2 ] = tmpYRot[ 0 ] + ( tmpYRot[ 1 ] - tmpYRot[ 0 ] ) * tmpPos
  270.          tmpZRot[ 2 ] = tmpZRot[ 0 ] + ( tmpZRot[ 1 ] - tmpZRot[ 0 ] ) * tmpPos
  271.          tmpXScal[ 2 ] = tmpXScal[ 0 ] + ( tmpXScal[ 1 ] - tmpXScal[ 0 ] ) * tmpPos
  272.          tmpYScal[ 2 ] = tmpYScal[ 0 ] + ( tmpYScal[ 1 ] - tmpYScal[ 0 ] ) * tmpPos
  273.          tmpZScal[ 2 ] = tmpZScal[ 0 ] + ( tmpZScal[ 1 ] - tmpZScal[ 0 ] ) * tmpPos
  274.          
  275.          
  276.          PositionEntity tmpObjectEntity, tmpXPos[ 2 ], tmpYPos[ 2 ], tmpZPos[ 2 ], 1
  277.          RotateEntity tmpObjectEntity, tmpXRot[ 2 ], tmpYRot[ 2 ], tmpZRot[ 2 ], 1
  278.          ScaleEntity tmpObjectEntity, tmpXScal[ 2 ], tmpYScal[ 2 ], tmpXScal[ 2 ], 1
  279.       Next
  280.        
  281.       tmpRepTime = REPLAY_RecordDataCount * REPLAY_RecordTime - REPLAY_RecordTime
  282.       REPLAY_PlayTime = ( REPLAY_PlayTime + Float( REPLAY_FrameTime ) * tmpTimeScale ) Mod tmpRepTime
  283.       If REPLAY_PlayTime < 0 Then REPLAY_PlayTime = tmpRepTime - REPLAY_RecordTime
  284.  
  285.    EndIf
  286.    
  287. End Function
  288.  
  289. Function REPLAY_LoadDemo( tmpPath$ )
  290.  
  291.    Local tmpStream
  292.    Local tmpObjectCount
  293.    Local tmpID, tmpName$
  294.    Local tmpObject.REPLAY_Object
  295.    Local tmpData.REPLAY_Data
  296.    Local tmpAktTime
  297.    
  298.    Delete Each REPLAY_Data
  299.    
  300.    tmpStream = ReadFile( tmpPath$ )
  301.    If tmpStream = 0 Then Return 0
  302.    
  303.    tmpObjectCount = ReadInt( tmpStream )
  304.    For i = 1 To tmpObjectCount
  305.       tmpID = ReadInt( tmpStream )
  306.       tmpName$ = ReadString( tmpStream )
  307.       For tmpObject = Each REPLAY_Object
  308.          If tmpObjectName$ = tmpName$ Then
  309.             tmpObjectID = tmpID : Exit
  310.          EndIf
  311.       Next
  312.    Next
  313.    REPLAY_RecordDataCount = 0
  314.    tmpAktTime = -1
  315.    While Not Eof( tmpStream )
  316.       tmpID = ReadInt( tmpStream )
  317.       tmpData = New REPLAY_Data
  318.       tmpDataTime = ReadInt( tmpStream )
  319.       tmpDataXPos = ReadFloat( tmpStream )
  320.       tmpDataYPos = ReadFloat( tmpStream )
  321.       tmpDataPos = ReadFloat( tmpStream )
  322.       tmpDataXRot = ReadFloat( tmpStream )
  323.       tmpDataYRot = ReadFloat( tmpStream )
  324.       tmpDataRot = ReadFloat( tmpStream )
  325.       tmpDataXScal = ReadFloat( tmpStream )
  326.       tmpDataYScal = ReadFloat( tmpStream )
  327.       tmpDataScal = ReadFloat( tmpStream )
  328.       For tmpObject = Each REPLAY_Object
  329.          If tmpObjectID = tmpID Then
  330.             tmpDataInfo = tmpObject
  331.             tmpDataLastData = tmpObjectLastData
  332.             If tmpDataLastData <> Null Then
  333.                tmpDataLastDataNextData = tmpData
  334.             EndIf
  335.             tmpObjectLastData = tmpData    
  336.             Exit
  337.          EndIf
  338.       Next
  339.       If tmpDataTime <> tmpAktTime Then
  340.          REPLAY_RecordDataCount = REPLAY_RecordDataCount + 1    
  341.          tmpAktTime = tmpDataTime
  342.       EndIf
  343.    Wend
  344. End Function
  345.  
  346.  
  347.  
  348. Function REPLAY_PlayStop( )
  349.  
  350.    REPLAY_PlayOn = 0
  351.    
  352. End Function  
  353.  
  354. Function REPLAY_GetEntityScale#( tmpEntity, tmpAxis=0 )
  355.    
  356.    If tmpEntity = 0 Then Return 0.0
  357.  
  358.    Local tmpVX#, tmpVY#, tmpVZ#
  359.    
  360.    If tmpAxis < 0 Then tmpAxis = 0
  361.    If tmpAxis > 2 Then tmpAxis = 2
  362.    
  363.    tmpVX = GetMatElement( tmpEntity, tmpAxis, 0 )
  364.    tmpVY = GetMatElement( tmpEntity, tmpAxis, 1 )
  365.    tmpVZ = GetMatElement( tmpEntity, tmpAxis, 2 )
  366.    
  367.    Return ( Sqr( tmpVX*tmpVX + tmpVY*tmpVY + tmpVZ*tmpVZ ) )
  368.    
  369. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal