Advertisement
Guest User

raw

a guest
Jul 15th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. /* ========================================================================= */
  2. /* INCLUDES */
  3. /* ========================================================================= */
  4.  
  5. #include <sourcemod>
  6. #include <sdktools>
  7. #include <sdkhooks>
  8. #include <cstrike>
  9.  
  10. #pragma semicolon 1
  11. #pragma newdecls required
  12.  
  13. /* ========================================================================= */
  14. /* DEFINES */
  15. /* ========================================================================= */
  16.  
  17. /* Plugin version */
  18. #define C_PLUGIN_VERSION "3.0.0"
  19.  
  20. /* ------------------------------------------------------------------------- */
  21.  
  22. /* Knockback weapon property */
  23. #define C_WEAPON_PROPERTY_KNOCKBACK (0)
  24. /* Velocity weapon property */
  25. #define C_WEAPON_PROPERTY_VELOCITY (1)
  26. /* Ground weapon property */
  27. #define C_WEAPON_PROPERTY_GROUND (2)
  28. /* Maximum weapon property */
  29. #define C_WEAPON_PROPERTY_MAXIMUM (3)
  30.  
  31. /* ========================================================================= */
  32. /* GLOBAL CONSTANTS */
  33. /* ========================================================================= */
  34.  
  35. /* Plugin information */
  36. public Plugin myinfo =
  37. {
  38. name = "Weapon Jumping",
  39. author = "Nyuu",
  40. description = "Provide the ability to weapon jump",
  41. version = C_PLUGIN_VERSION,
  42. url = "https://forums.alliedmods.net/showthread.php?t=292151"
  43. }
  44.  
  45. /* ========================================================================= */
  46. /* GLOBAL VARIABLES */
  47. /* ========================================================================= */
  48.  
  49. /* Players weapon jumping */
  50. bool gl_bPlayerWeaponJumping [MAXPLAYERS + 1];
  51. /* Players weapon jumping velocity */
  52. float gl_vPlayerWeaponJumpingVelocity[MAXPLAYERS + 1][3];
  53.  
  54. /* Weapon properties stringmap */
  55. StringMap gl_hMapWeaponProperties;
  56.  
  57. /* Weapon jumping enabled cvar */
  58. ConVar gl_hCvarWeaponJumpingEnabled;
  59.  
  60. /* ========================================================================= */
  61. /* FUNCTIONS */
  62. /* ========================================================================= */
  63.  
  64. /* ------------------------------------------------------------------------- */
  65. /* Plugin */
  66. /* ------------------------------------------------------------------------- */
  67.  
  68. public void OnPluginStart()
  69. {
  70. // Create the version cvar
  71. CreateConVar("sm_weapon_jumping_version", C_PLUGIN_VERSION, "Display the plugin version", FCVAR_DONTRECORD | FCVAR_NOTIFY | FCVAR_REPLICATED | FCVAR_SPONLY);
  72.  
  73. // Create the plugin cvars
  74. gl_hCvarWeaponJumpingEnabled = CreateConVar("sm_weapon_jumping_enabled", "1", "Enable weapon jumping", FCVAR_NONE, true, 0.0, true, 1.0);
  75.  
  76. // Create the weapon properties stringmap
  77. gl_hMapWeaponProperties = new StringMap();
  78.  
  79. // Hook the weapon fire event
  80. HookEvent("weapon_fire", OnWeaponFirePost, EventHookMode_Post);
  81. }
  82.  
  83. public void OnConfigsExecuted()
  84. {
  85. char szConfigFile[PLATFORM_MAX_PATH];
  86.  
  87. // Create the configuration keyvalues
  88. KeyValues kvConfig = new KeyValues("weapons");
  89.  
  90. // Clear the weapon properties stringmap
  91. gl_hMapWeaponProperties.Clear();
  92.  
  93. // Build the path of the configuration file
  94. BuildPath(Path_SM, szConfigFile, sizeof(szConfigFile), "configs/weapon_jumping.cfg");
  95.  
  96. // Import the configuration file
  97. if (kvConfig.ImportFromFile(szConfigFile))
  98. {
  99. LogMessage("Start to read the configuration file...");
  100.  
  101. // Go to the first weapon properties section
  102. if (kvConfig.GotoFirstSubKey())
  103. {
  104. char szWeaponName[32];
  105. int arrWeaponProperty[C_WEAPON_PROPERTY_MAXIMUM];
  106.  
  107. do
  108. {
  109. // Read the weapon name
  110. if (kvConfig.GetSectionName(szWeaponName, sizeof(szWeaponName)))
  111. {
  112. // Get the weapon properties
  113. float flKnockback = kvConfig.GetFloat("knockback", 0.00);
  114. float flVelocity = kvConfig.GetFloat("velocity", 0.00);
  115. int iGround = kvConfig.GetNum ("ground", 0);
  116. bool bGround = false;
  117.  
  118. // Check & clamp the weapon properties
  119. if (flVelocity < 0.00)
  120. {
  121. flVelocity = 0.00;
  122. }
  123. else if (flVelocity > 1.00)
  124. {
  125. flVelocity = 1.00;
  126. }
  127.  
  128. if (iGround)
  129. {
  130. bGround = true;
  131. }
  132.  
  133. // Convert the weapon properties
  134. arrWeaponProperty[C_WEAPON_PROPERTY_KNOCKBACK] = view_as<int>(flKnockback);
  135. arrWeaponProperty[C_WEAPON_PROPERTY_VELOCITY] = view_as<int>(flVelocity);
  136. arrWeaponProperty[C_WEAPON_PROPERTY_GROUND] = view_as<int>(bGround);
  137.  
  138. // Push the weapon properties in the stringmap
  139. gl_hMapWeaponProperties.SetArray(szWeaponName, arrWeaponProperty, C_WEAPON_PROPERTY_MAXIMUM);
  140.  
  141. LogMessage("Read \"%s\" (Knockback: %0.2f | Velocity: %0.2f | Ground: %d).",
  142. szWeaponName, flKnockback, flVelocity, bGround);
  143. }
  144.  
  145. // Go to the next weapon properties section
  146. } while (kvConfig.GotoNextKey());
  147. }
  148.  
  149. LogMessage("Finish to read the configuration file (%d weapons read) !", gl_hMapWeaponProperties.Size);
  150. }
  151. else
  152. {
  153. LogError("Can't import the configuration file !");
  154. LogError("> Path: %s", szConfigFile);
  155. }
  156.  
  157. delete kvConfig;
  158. }
  159.  
  160. /* ------------------------------------------------------------------------- */
  161. /* Client */
  162. /* ------------------------------------------------------------------------- */
  163.  
  164. public void OnClientConnected(int iClient)
  165. {
  166. // Initialize the client data
  167. gl_bPlayerWeaponJumping [iClient] = false;
  168. gl_vPlayerWeaponJumpingVelocity[iClient][0] = 0.0;
  169. gl_vPlayerWeaponJumpingVelocity[iClient][1] = 0.0;
  170. gl_vPlayerWeaponJumpingVelocity[iClient][2] = 0.0;
  171. }
  172.  
  173. public void OnClientPutInServer(int iClient)
  174. {
  175. // Hook the client postthink function
  176. SDKHook(iClient, SDKHook_PostThinkPost, OnPlayerPostThinkPost);
  177. }
  178.  
  179. public void OnClientDisconnect(int iClient)
  180. {
  181. // Clear the client data
  182. gl_bPlayerWeaponJumping [iClient] = false;
  183. gl_vPlayerWeaponJumpingVelocity[iClient][0] = 0.0;
  184. gl_vPlayerWeaponJumpingVelocity[iClient][1] = 0.0;
  185. gl_vPlayerWeaponJumpingVelocity[iClient][2] = 0.0;
  186. }
  187.  
  188. /* ------------------------------------------------------------------------- */
  189. /* Weapon */
  190. /* ------------------------------------------------------------------------- */
  191.  
  192. public void OnWeaponFirePost(Event hEvent, const char[] szName, bool bDontBroadcast)
  193. {
  194. // Check if the plugin is enabled
  195. if (gl_hCvarWeaponJumpingEnabled.BoolValue)
  196. {
  197. // Get the player
  198. int iPlayer = GetClientOfUserId(hEvent.GetInt("userid"));
  199.  
  200. // Check if the player is valid
  201. if (1 <= iPlayer <= MaxClients)
  202. {
  203. // Get the player active weapon
  204. int iWeapon = GetEntPropEnt(iPlayer, Prop_Send, "m_hActiveWeapon");
  205.  
  206. // Check the current number of ammo in the loader
  207. if (GetEntProp(iWeapon, Prop_Send, "m_iClip1") > 0)
  208. {
  209. char szWeaponName[32];
  210. int arrWeaponProperty[C_WEAPON_PROPERTY_MAXIMUM];
  211.  
  212. // Get the weapon name
  213. hEvent.GetString("weapon", szWeaponName, sizeof(szWeaponName));
  214.  
  215. // Check if the weapon name is present in the weapon properties stringmap
  216. if (gl_hMapWeaponProperties.GetArray(szWeaponName, arrWeaponProperty, C_WEAPON_PROPERTY_MAXIMUM))
  217. {
  218. // Convert the weapon properties
  219. float flKnockback = view_as<float>(arrWeaponProperty[C_WEAPON_PROPERTY_KNOCKBACK]);
  220. float flVelocity = view_as<float>(arrWeaponProperty[C_WEAPON_PROPERTY_VELOCITY]);
  221. bool bGround = view_as<bool> (arrWeaponProperty[C_WEAPON_PROPERTY_GROUND]);
  222.  
  223. // Check if the player can weapon jump on ground
  224. if (!(GetEntityFlags(iPlayer) & FL_ONGROUND) || bGround)
  225. {
  226. float vPlayerVelocity[3];
  227. float vPlayerEyeAngles[3];
  228. float vPlayerForward[3];
  229.  
  230. // Get the player velocity
  231. GetEntPropVector(iPlayer, Prop_Data, "m_vecVelocity", vPlayerVelocity);
  232.  
  233. // Get the player forward direction
  234. GetClientEyeAngles(iPlayer, vPlayerEyeAngles);
  235. GetAngleVectors(vPlayerEyeAngles, vPlayerForward, NULL_VECTOR, NULL_VECTOR);
  236.  
  237. // Compute the player weapon jumping velocity
  238. gl_vPlayerWeaponJumpingVelocity[iPlayer][0] = vPlayerVelocity[0] * flVelocity - vPlayerForward[0] * flKnockback;
  239. gl_vPlayerWeaponJumpingVelocity[iPlayer][1] = vPlayerVelocity[1] * flVelocity - vPlayerForward[1] * flKnockback;
  240. gl_vPlayerWeaponJumpingVelocity[iPlayer][2] = vPlayerVelocity[2] * flVelocity - vPlayerForward[2] * flKnockback;
  241.  
  242. // Set the player weapon jumping
  243. gl_bPlayerWeaponJumping[iPlayer] = true;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250.  
  251. /* ------------------------------------------------------------------------- */
  252. /* Player */
  253. /* ------------------------------------------------------------------------- */
  254.  
  255. public void OnPlayerPostThinkPost(int iPlayer)
  256. {
  257. // Check if the player must weapon jump
  258. if (gl_bPlayerWeaponJumping[iPlayer])
  259. {
  260. // Check if the player is still alive
  261. if (IsPlayerAlive(iPlayer))
  262. {
  263. // Expel the player
  264. TeleportEntity(iPlayer, NULL_VECTOR, NULL_VECTOR, gl_vPlayerWeaponJumpingVelocity[iPlayer]);
  265. }
  266.  
  267. // Reset the player weapon jumping
  268. gl_bPlayerWeaponJumping[iPlayer] = false;
  269. }
  270. }
  271.  
  272. /* ========================================================================= */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement