Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <sdktools_client>
  2.  
  3. public Plugin myinfo =
  4. {
  5. name = "TickRate Control",
  6. author = "Rostu",
  7. version = "1.1",
  8. url = "https://vk.com/rostu13 | Discord: Rostu#7917"
  9. };
  10.  
  11. #define PTR(%0) view_as<Address>(%0)
  12. #define nullptr Address_Null
  13.  
  14. int g_iDefaultTick;
  15. int g_iCurrentTick;
  16.  
  17. Address g_pTickInterval; // m_flTickInterval + SV_StartSound;
  18. Address g_pIntervalPerTick; // host_state_interval_per_tick + CBaseServer::SpawnServer
  19.  
  20. ConVar g_hTick;
  21. ConVar g_hForceRetry;
  22.  
  23. public APLRes AskPluginLoad2( Handle hPlugin, bool late, char[] sError, int error_len )
  24. {
  25. CreateNative("TickRate_GetCurrentTick", Native_GetCurrentTick);
  26. CreateNative("TickRate_GetDefaultTick", Native_GetDefaultTick);
  27. CreateNative("TickRate_SetTickRate", Native_SetTickRate);
  28. }
  29. public void OnPluginStart()
  30. {
  31. (g_hTick = CreateConVar("tickrate_value", "128.0", "Server TickRate", 0, true, 21.0, true, 128.0)).AddChangeHook(OnTickRateChange);
  32. g_hForceRetry = CreateConVar("tickrate_force_retry", "0", "1 - After changing the tickrate_value value - all players will be forced to log in to the server \n 0 - Nothing will happen ", 0, true, 0.0, true, 1.0)
  33. ReadGameData();
  34.  
  35. AutoExecConfig(true, "TickRateControl");
  36. }
  37. public void OnPluginEnd()
  38. {
  39. if(g_iDefaultTick != g_iCurrentTick)
  40. {
  41. SetTickRate(g_iDefaultTick);
  42. }
  43. }
  44. public void OnConfigsExecuted()
  45. {
  46. CheckToChangeTickRate(g_hTick.FloatValue);
  47. }
  48. public void OnTickRateChange (ConVar hCvar, const char[] sOldValue, const char[] sNewValue)
  49. {
  50. CheckToChangeTickRate(StringToFloat(sNewValue), g_hForceRetry.BoolValue);
  51. }
  52. void CheckToChangeTickRate(float fTick, bool bForceRetry = false)
  53. {
  54. float fNewTick = 1.0 / fTick;
  55.  
  56. if(fNewTick > 0.048828125 || fNewTick < 0.0078125)
  57. {
  58. fNewTick = view_as<float>(g_iDefaultTick);
  59. }
  60.  
  61. SetTickRate(view_as<int>(fNewTick));
  62.  
  63. if(bForceRetry)
  64. {
  65. for(int x = 1; x <= MaxClients; x++) if(IsClientInGame(x) && !IsFakeClient(x))
  66. {
  67. ReconnectClient(x);
  68. }
  69. }
  70. }
  71. void SetTickRate(int iTick)
  72. {
  73. StoreToAddress(g_pTickInterval, iTick, NumberType_Int32);
  74. StoreToAddress(g_pIntervalPerTick, iTick, NumberType_Int32);
  75.  
  76. static GlobalForward hForward;
  77.  
  78. if(hForward == null)
  79. hForward = new GlobalForward("TickRate_OnTickRateChanged", ET_Ignore, Param_Cell, Param_Cell); // public void TickRate_OnTickRateChanged(float fOldTick, float fNewTick)
  80.  
  81. Call_StartForward(hForward);
  82. Call_PushCell( 1.0 / view_as<float>(g_iCurrentTick));
  83. Call_PushCell( 1.0 / view_as<float>(iTick));
  84. Call_Finish();
  85.  
  86. g_iCurrentTick = iTick;
  87. }
  88. void ReadGameData()
  89. {
  90. GameData hGameData = new GameData("tickrate");
  91.  
  92. if(!hGameData)
  93. {
  94. SetFailState("Couldn't read gamedata/tickrate.txt");
  95. }
  96.  
  97. Address pStartSound = hGameData.GetAddress("sv_startsound");
  98. Address pSpawnServer = hGameData.GetAddress("spawnserver");
  99.  
  100. int iTickInterval = hGameData.GetOffset("m_flTickInterval");
  101. int iStateInterval = hGameData.GetOffset("host_state_interval");
  102.  
  103. if(pStartSound == nullptr || pSpawnServer == nullptr) SetFailState("Couldn't get Address sv_startsound/spawnserver :(");
  104. if(iTickInterval <= 0 || iStateInterval <= 0) SetFailState("Couldn't get Offset m_flTickInterval/host_state_interval :(");
  105.  
  106. g_pTickInterval = PTR(LoadFromAddress(pStartSound + PTR(iTickInterval), NumberType_Int32));
  107. g_pIntervalPerTick = PTR(LoadFromAddress(pSpawnServer + PTR(iStateInterval), NumberType_Int32));
  108.  
  109. g_iDefaultTick = LoadFromAddress(g_pTickInterval, NumberType_Int32);
  110.  
  111. delete hGameData;
  112. }
  113. public int Native_GetCurrentTick (Handle hPlugin, int iParams)
  114. {
  115. return view_as<int>(1.0 / view_as<float>(g_iCurrentTick));
  116. }
  117. public int Native_GetDefaultTick (Handle hPlugin, int iParams)
  118. {
  119. return view_as<int>(1.0 / view_as<float>(g_iDefaultTick));
  120. }
  121. public int Native_SetTickRate (Handle hPlugin, int iParams)
  122. {
  123. CheckToChangeTickRate(GetNativeCell(1), GetNativeCell(2));
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement