December 04, 2020, 11:27:07 AM

Author Topic: [bb] Replaying user input using a collection of Types by matt! [ 1+ years ago ]  (Read 372 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Replaying user input using a collection of Types
Author : matt!
Posted : 1+ years ago

Description : 23/24 June 2002, version 0.10, by Matt Sephton (matt@...)
26/27 June 2002, version 0.20, tracks multiple computer controlled elements

to do: enable early quit of replay to go back to game

*** feel free to use this method as long as you give me credit

this method can be used to replay user input. this simple demo records the player control of a dot. we choose to do the correct branch of the main loop (moving or drawing) depending on if we are recording (game) or replaying (watching). this could be extended to keep track of multiple items, such as computer/AI controlled characters.

:) matt


Code :
Code: BlitzBasic
  1. ;
  2. ; REPLAYING USER INPUT USING A COLLECTION OF TYPES
  3. ; 23/24 June 2002, version 0.10, tracks user controlled element
  4. ; 26/27 June 2002, version 0.20, tracks multiple computer controlled elements
  5. ;
  6. ; to do: enable early quit of replay to go back to game
  7. ;
  8. ; *** by Matt Sephton (matt@ewtoo.org)
  9. ; *** feel free to use this method as long as you give me credit
  10. ;
  11. ; this method can be used to replay user input. this simple demo records the player control of a dot.
  12. ;
  13. ; we choose to do the correct branch of the main loop (moving or drawing) depending on if we are
  14. ; recording (game) or replaying (watching).
  15. ;
  16. ; this could be extended To keep track of multiple items, such as computer/AI controlled characters.
  17. ;
  18.  
  19. AppTitle "cursors to move, R to replay"
  20.  
  21. Type coord              ;standard element type
  22.         Field x,y
  23. End Type
  24.  
  25. Type complex    ;complex element type
  26.         Field x,y
  27.         Field r         ;has additional properties
  28. End Type
  29.  
  30. Type replay             ;replay data type (notice all tracked properties are combined in this one type)
  31.         Field x,y
  32.         Field r
  33. End Type
  34.  
  35. Const W = 320, H = 200          ;screen dimensions
  36.  
  37. ball.coord = New coord          ;position player controlled element at screen centre
  38. ballx = W/2
  39. bally = H/2
  40.  
  41. ai.complex = New complex        ;ai "player" #1
  42. aix = 180
  43. aiy = 180
  44. ai
  45.  = 4
  46.  
  47. ai2.complex = New complex       ;ai "player" #2
  48. ai2x = 0
  49. ai2y = 0
  50. ai2
  51.  = 4
  52.  
  53. Graphics W,H,16,2               ;gfx mode, small 16-bit window
  54. SetBuffer BackBuffer()  ;set backbuffer
  55. ClsColor 31,127,255             ;cls blue
  56.  
  57. Global replay_counter = 0               ;current buffer size (number of types in collection)
  58. Global playback_pointer = 0             ;where we are in the buffer at playback
  59. Global replaying = False                ;are we replaying data or not?
  60.  
  61. Const target_fps = 24                   ;so we can reuse this constant
  62.  
  63. Const total_objects = 3                 ;total number of objects to track
  64. Const replay_length = 5                 ;
  65.  
  66. ;max buffer size, limited only by memory size
  67. Const max_buffer_size = total_objects * target_fps * replay_length
  68.  
  69. fps = CreateTimer(target_fps)   ;limit drawing speed to 25fps
  70.  
  71. While Not KeyDown(1)
  72.         Cls                                             ;clear screen
  73.         Color 255,255,255                       ;draw white
  74.  
  75.         If KeyDown(19) Then                     ;R = replay mode on
  76.                 Delay 200       ;pause
  77.                 replaying = True                        ;flag that we're want to go through the main loop in replay mode
  78.         EndIf
  79.        
  80.         If (replaying = False)                  ;record mode
  81.                 ;move ball with cursor keys
  82.                 ballx = ballx - KeyDown(203) + KeyDown(205)     ;left/right
  83.                 bally = bally - KeyDown(200) + KeyDown(208)     ;up/down
  84.  
  85.                 ;ai
  86.                 For team.complex = Each complex
  87.                         teamx = (teamx - 2) Mod 360
  88.                         teamy = (teamy - 2) Mod 360
  89.                 Next
  90.  
  91.                 ;limit replay data to max_buffer_size by deletingg oldest data item
  92.                 If (replay_counter >= max_buffer_size)
  93.                         Delete First replay
  94.                         Delete First replay
  95.                         Delete First replay
  96.                         replay_counter = replay_counter - total_objects         ;less data used
  97.                 EndIf
  98.  
  99.                 ;record new data item (player)
  100.                 replay.replay = New replay
  101.                 replayx = ballx
  102.                 replayy = bally
  103.                 replay_counter = replay_counter + 1             ;more data
  104.  
  105.                 ;record new data item (multiple ai)
  106.                 For team.complex = Each complex
  107.                         replay.replay = New replay
  108.                         replayx = teamx
  109.                         replayy = teamy
  110.                         replay_counter = replay_counter + 1             ;more data
  111.                 Next
  112.        
  113.                 Text 10,10,"R"+ replay_counter
  114.  
  115.         Else                            ;replay mode
  116.                 If (playback_pointer = 0) Then rd.replay = First replay ;rd = start of replay collection
  117.  
  118.                 ;draw replay cursor
  119.                 If (playback_pointer Mod target_fps > target_fps/2) Then Text W-20,10,"R"
  120.                
  121.                 If (playback_pointer < replay_counter) Then                     ;still in replay data
  122.                         ;set player draw position to be replay data
  123.                         ballx = rdx
  124.                         bally = rdy
  125.                         rd = After rd                   ;go to next replay data item in collection
  126.                         playback_pointer = playback_pointer + 1
  127.  
  128.                         For team.complex = Each complex
  129.                                 teamx = rdx
  130.                                 teamy = rdy
  131.                                 rd = After rd                                                   ;go to next replay data item in collection
  132.                                 playback_pointer = playback_pointer + 1 ;move forward through replay data
  133.                         Next
  134.  
  135.                 Else                                                                                            ;end of the replay data
  136.                         playback_pointer = 0    ;rewind pointer to beginning of replay data
  137.                         replaying = False               ;turn off replay
  138.                        
  139.                         Delay 200
  140.                         SetBuffer FrontBuffer() ;show a message to make obvious the transition from replay to game
  141.                         Cls
  142.                         Text W/2,(H/2)-10,"RESUMING GAME",True,True
  143.                         Delay 800
  144.                         SetBuffer BackBuffer()
  145.                 EndIf
  146.  
  147.                 Text 10,10,"@" + playback_pointer + "/" + max_buffer_size
  148.  
  149.                 Delay 75        ;slow motion replay!
  150.         EndIf
  151.  
  152.         Rect ballx, bally, 4,4,True
  153.  
  154.         Color 196,196,196                               ;draw paler grey
  155.         Oval (W/2)-48, (H/2)-48, 100,100,False
  156.  
  157.         Color 255,255,0                         ;draw yellow
  158.         For team.complex = Each complex
  159.                 Oval Sin(teamx)*50+(W/2), Cos(teamy)*50+(H/2), team
  160. ,team
  161. ,True
  162.         Next
  163.  
  164.         k = WaitTimer(fps)                      ;limit drawing speed
  165.        
  166.         Flip
  167. Wend
  168.  
  169. End
  170. ;


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal