Advertisement
Guest User

Some Source Engine RNG shit

a guest
Jun 6th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. // Here's some functions from Source Engine that synchronize RNG between client and server. This of course will have to be implemented differently in Zandronum, but it should perhaps give you guys some ideas on how to do it conceptually.
  2.  
  3. void CBaseEntity::SetPredictionRandomSeed( const CUserCmd *cmd ) // CBaseEntity is the base class for every entity. This means enemies, players, and everything alike.
  4. {                                                                // CUserCmd is basically the current command being executed by the player/entity. Pretty sure Zan has an analogue.
  5.     if ( !cmd )
  6.     {
  7.         m_nPredictionRandomSeed = -1;
  8. #ifdef SERVER_DLL
  9.         m_nPredictionRandomSeedServer = -1;
  10. #endif
  11.  
  12.         return;
  13.     }
  14.  
  15.     m_nPredictionRandomSeed = ( cmd->random_seed );
  16. #ifdef SERVER_DLL
  17.     m_nPredictionRandomSeedServer = ( cmd->server_random_seed );
  18. #endif
  19. }
  20.  
  21. inline int CBaseEntity::GetPredictionRandomSeed( bool bUseUnSyncedServerPlatTime ) // The bool here determines whether or not we're using the unsynched server seed, or the client/server synched seed.
  22. {
  23. #ifdef SERVER_DLL
  24.     return bUseUnSyncedServerPlatTime ? m_nPredictionRandomSeedServer : m_nPredictionRandomSeed;
  25. #else
  26.     return m_nPredictionRandomSeed;
  27. #endif
  28. }
  29.  
  30. ConVar sv_usercmd_custom_random_seed( "sv_usercmd_custom_random_seed", "1", FCVAR_CHEAT, "When enabled server will populate an additional random seed independent of the client" );
  31.  
  32. void CBasePlayer::ProcessUsercmds( CUserCmd *cmds, int numcmds, int totalcmds, int dropped_packets, bool paused ) // CBasePlayer is the base player pawn class, inherits from CBaseEntity. This is where the player's user commands (button presses, etc) are processed.
  33. {
  34. //*snip*
  35.  
  36.                 if ( sv_usercmd_custom_random_seed.GetBool() ) // Refer to the cvar description above.
  37.         {
  38.             float fltTimeNow = float( Plat_FloatTime() * 1000.0 ); // Based on current platform time.
  39.             pCmd->server_random_seed = *reinterpret_cast<int*>( (char*)&fltTimeNow );
  40.         }
  41.         else
  42.         {
  43.             pCmd->server_random_seed = pCmd->random_seed; // just copy the current synched RNG from CUserCmd.
  44.         }
  45. }
  46.  
  47. class CUserCmd // Base class for user commands
  48. {
  49. //*snip*
  50.  
  51.     int     random_seed;    // For shared random functions
  52. #ifdef SERVER_DLL
  53.     int     server_random_seed; // Only the server populates this seed
  54. #endif
  55. }
  56.  
  57. void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from ) // function that reads user commands that have been recieved on client and server alike, for entities and players.
  58. {
  59.   //*snip*
  60.  
  61.   move->random_seed = MD5_PseudoRandom( move->command_number ) & 0x7fffffff;
  62. }
  63.  
  64. // Now, here's an example of it's use in a function:
  65. void CBaseEntity::FireBullets( const FireBulletsInfo_t &info )
  66. {
  67.     //*snip*
  68.  
  69.         // Hitscan Prediction is only usable on players
  70.     int iSeed = 0;
  71.     if ( IsPlayer() )
  72.     {
  73.         iSeed = CBaseEntity::GetPredictionRandomSeed( info.m_bUseServerRandomSeed ) & 255;
  74.     }
  75.  
  76. //*snip*
  77.  
  78.         for (int iShot = 0; iShot < info.m_iShots; iShot++) // Use your imagination here, such as like for each pellet of a A_FireBullets shot.
  79.     {
  80.         //*snip*
  81.         // Prediction is only usable on players
  82.         if ( IsPlayer() )
  83.         {
  84.             RandomSeed( iSeed );    // init random system with this seed
  85.         }
  86.  
  87.                 // Get numbers from RNG, Trace an attack, etc...
  88.         }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement