OPSP.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /********************************************
  2. * OnPlayerShootPlayer! V8.3 *
  3. * Credits: wups *
  4. * Double-O-Seven for CrossProduct system *
  5. * Nero3D for GetPlayerCameraWeaponVector *
  6. ********************************************/
  7. // include
  8. #include <a_samp>
  9. #tryinclude <foreach>
  10. // defines
  11. #if defined OPSP
  12. #endinput
  13. #endif
  14. #define OPSP
  15. #if !defined foreach
  16. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  17. #define __SSCANF_FOREACH__
  18. #endif
  19. #if !defined PRESSED
  20. #define PRESSED(%0) \
  21. (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  22. #endif
  23. #if !defined RELEASED
  24. #define RELEASED(%0) \
  25. (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
  26. #endif
  27. // variables
  28. static
  29. Float:RL_phealth[MAX_PLAYERS],
  30. Float:RL_parmour[MAX_PLAYERS],
  31. bool:RL_Shooting[MAX_PLAYERS],
  32. bool:RL_UpdatedHealth[MAX_PLAYERS],
  33. bool:RL_OPUP,
  34. bool:RL_OPSC,
  35. bool:RL_OPKSC,
  36. bool:RL_OPC,
  37. RL_Released[MAX_PLAYERS];
  38. // forwards
  39. forward OnPlayerShootPlayer(Shooter,Target,Float:HealthLost,Float:ArmourLost);
  40. /*--------------------------------------------------------------------------------------------------
  41. Function:
  42. AdjustVector
  43. Params:
  44. & Float: vX - x Vector to adjust
  45. & Float: vY - y Vector to adjust
  46. & Float: vZ - z Vector to adjust
  47. Float: oX - x Offset Vector
  48. Float: oY - y Offset Vector
  49. const Float: oZ - z Offset Vector
  50. Return:
  51. -
  52. Notes:
  53. Adjust the vector with the offset
  54. --------------------------------------------------------------------------------------------------*/
  55. stock AdjustVector(& Float: vX, & Float: vY, & Float: vZ, Float: oX, Float: oY, const Float: oZ) { // Credits Nero_3D
  56. static
  57. Float: Angle;
  58. Angle = -atan2(vX, vY);
  59. if(45.0 < Angle) {
  60. oX ^= oY;
  61. oY ^= oX;
  62. oX ^= oY;
  63. if(90.0 < Angle) {
  64. oX *= -1;
  65. if(135.0 < Angle) {
  66. oX *= -1;
  67. oX ^= oY;
  68. oY ^= oX;
  69. oX ^= oY;
  70. oX *= -1;
  71. }
  72. }
  73. } else if(Angle < 0.0) {
  74. oY *= -1;
  75. if(Angle < -45.0) {
  76. oX *= -1;
  77. oX ^= oY;
  78. oY ^= oX;
  79. oX ^= oY;
  80. oX *= -1;
  81. if(Angle < -90.0) {
  82. oX *= -1;
  83. if(Angle < -135.0) {
  84. oX ^= oY;
  85. oY ^= oX;
  86. oX ^= oY;
  87. }
  88. }
  89. }
  90. }
  91. vX += oX,
  92. vY += oY;
  93. vZ += oZ;
  94. return false;
  95. }
  96. /*--------------------------------------------------------------------------------------------------
  97. Function:
  98. GetPlayerCameraWeaponVector
  99. Params:
  100. playerid - Player to get the weapon vector of
  101. & Float: vX - x Vector variable
  102. & Float: vY - y Vector variable
  103. & Float: vZ - z Vector variable
  104. Return:
  105. If the player is connected
  106. Notes:
  107. Gets the weapon vector of the player
  108. native GetPlayerCameraWeaponVector(playerid, & Float: vX, & Float: vY, & Float: vZ);
  109. --------------------------------------------------------------------------------------------------*/
  110. stock GetPlayerCameraWeaponVector(playerid, & Float: vX, & Float: vY, & Float: vZ) { // Credits Nero_3D
  111. static
  112. weapon;
  113. if(21 < (weapon = GetPlayerWeapon(playerid)) < 39) {
  114. GetPlayerCameraFrontVector(playerid, vX, vY, vZ);
  115. switch(weapon) {
  116. case WEAPON_SNIPER, WEAPON_ROCKETLAUNCHER, WEAPON_HEATSEEKER: {}
  117. case WEAPON_RIFLE: {
  118. AdjustVector(vX, vY, vZ, 0.016204, 0.009899, 0.047177);
  119. }
  120. case WEAPON_AK47, WEAPON_M4: {
  121. AdjustVector(vX, vY, vZ, 0.026461, 0.013070, 0.069079);
  122. }
  123. default: {
  124. AdjustVector(vX, vY, vZ, 0.043949, 0.015922, 0.103412);
  125. }
  126. }
  127. return true;
  128. }
  129. else
  130. GetPlayerCameraFrontVector(playerid, vX, vY, vZ);
  131. return false;
  132. }
  133. stock crossp(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z, &Float:output)
  134. {
  135. new
  136. Float:c1 = (v1y * v2z) - (v1z * v2y),
  137. Float:c2 = (v1z * v2x) - (v1x * v2z),
  138. Float:c3 = (v1x * v2y) - (v1y * v2x);
  139. output = floatsqroot ((c1 * c1) + (c2 * c2) + (c3 * c3));
  140. return 0;
  141. }
  142. stock GetDistanceFromPointToLine(&Float:distance, Float:line_vector_x, Float:line_vector_y, Float:line_vector_z, Float:line_x, Float:line_y, Float:line_z, Float:point_x, Float:point_y, Float:point_z)
  143. {
  144. //A line is defined by a point (which is on the line (line_x/y/z)) and a vector which defines the direction (line_vector_x/y/z).
  145. static Float:output;
  146. crossp(line_vector_x, line_vector_y, line_vector_z, point_x - line_x, point_y - line_y, point_z - line_z, output);//Cross product of 2 vectors.
  147. distance = output / floatsqroot ((line_vector_x * line_vector_x) + (line_vector_y * line_vector_y) + (line_vector_z * line_vector_z));
  148. return 0;
  149. }
  150. public OnPlayerUpdate(playerid)
  151. {
  152. static Float:RL_HP,
  153. Float:RL_Armour;
  154. GetPlayerHealth(playerid,RL_HP);
  155. GetPlayerArmour(playerid,RL_Armour);
  156. if(RL_HP != RL_phealth[playerid] || RL_Armour != RL_parmour[playerid])
  157. {
  158. if(RL_UpdatedHealth[playerid])
  159. RL_UpdatedHealth[playerid]=false;
  160. else
  161. {
  162. new
  163. Float:RL_PlayerPos[3],
  164. Float:RL_Distance,
  165. Float:RL_CameraPos[3],
  166. Float:RL_CameraVectors[3],
  167. RL_Tick
  168. ;
  169. RL_Tick = (GetTickCount()-1000);
  170. GetPlayerPos(playerid, RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  171. foreach(Player,i)
  172. {
  173. if(RL_Shooting[i] || RL_Tick < RL_Released[i])
  174. {
  175. if(i != playerid)
  176. {
  177. if(IsPlayerInRangeOfPoint(i,200.0,RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]))
  178. {
  179. GetPlayerCameraWeaponVector(i, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2]);
  180. GetPlayerCameraPos(i, RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2]);
  181. GetDistanceFromPointToLine(RL_Distance, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2], RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2], RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  182. if(RL_Distance < 2.5)
  183. {
  184. CallLocalFunction("OnPlayerShootPlayer","iiff",i,playerid,(RL_phealth[playerid]-RL_HP),(RL_parmour[playerid]-RL_Armour));
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. RL_phealth[playerid]=RL_HP;
  194. RL_parmour[playerid]=RL_Armour;
  195. return (RL_OPUP)?CallLocalFunction("RL_OnPlayerUpdate","i",playerid):1;
  196. }
  197. // Functions
  198. // SetPlayerHealth
  199. stock SetPlayerHealthEx(playerid, Float:health)
  200. {
  201. // RL_phealth[playerid]=health;
  202. RL_UpdatedHealth[playerid]=true;
  203. return SetPlayerHealth(playerid, health);
  204. }
  205. #define SetPlayerHealth SetPlayerHealthEx
  206. // SetPlayerArmour
  207. stock SetPlayerArmourEx(playerid, Float:armour)
  208. {
  209. // RL_parmour[playerid]=armour;
  210. RL_UpdatedHealth[playerid]=true;
  211. return SetPlayerArmour(playerid, armour);
  212. }
  213. #define SetPlayerArmour SetPlayerArmourEx
  214. #if defined _ALS_OnPlayerUpdate
  215. #undef OnPlayerUpdate
  216. #else
  217. #define _ALS_OnPlayerUpdate
  218. #endif
  219. #define OnPlayerUpdate RL_OnPlayerUpdate
  220. forward RL_OnPlayerUpdate(playerid);
  221. // OnPlayerKeyStateChange
  222. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  223. {
  224. if(PRESSED(KEY_FIRE)) RL_Shooting[playerid]=true;
  225. else if(RELEASED(KEY_FIRE))
  226. {
  227. RL_Shooting[playerid]=false;
  228. RL_Released[playerid]=GetTickCount();
  229. }
  230. return (RL_OPKSC)?CallLocalFunction("RL_OnPlayerKeyStateChange","iii",playerid,newkeys,oldkeys):1;
  231. }
  232. #if defined _ALS_OnPlayerKeyStateChange
  233. #undef OnPlayerKeyStateChange
  234. #else
  235. #define _ALS_OnPlayerKeyStateChange
  236. #endif
  237. #define OnPlayerKeyStateChange RL_OnPlayerKeyStateChange
  238. forward RL_OnPlayerKeyStateChange(playerid,newkeys,oldkeys);
  239. // OnPlayerStateChange
  240. public OnPlayerStateChange(playerid, newstate, oldstate)
  241. {
  242. if(newstate == PLAYER_STATE_WASTED)
  243. {
  244. if(RL_UpdatedHealth[playerid])
  245. RL_UpdatedHealth[playerid]=false;
  246. else
  247. {
  248. new
  249. Float:RL_PlayerPos[3],
  250. Float:RL_Distance,
  251. Float:RL_CameraPos[3],
  252. Float:RL_CameraVectors[3],
  253. RL_Tick,
  254. Float:RL_HP,
  255. Float:RL_Armour
  256. ;
  257. GetPlayerHealth(playerid,RL_HP);
  258. GetPlayerArmour(playerid,RL_Armour);
  259. RL_Tick = (GetTickCount()-1000);
  260. GetPlayerPos(playerid, RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  261. foreach(Player,i)
  262. {
  263. if(RL_Shooting[i] || RL_Tick < RL_Released[i])
  264. {
  265. if(i != playerid)
  266. {
  267. GetPlayerCameraWeaponVector(i, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2]);
  268. GetPlayerCameraPos(i, RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2]);
  269. if(IsPlayerInRangeOfPoint(i,200.0,RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]))
  270. {
  271. GetDistanceFromPointToLine(RL_Distance, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2], RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2], RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  272. if(RL_Distance < 2.5)
  273. {
  274. CallLocalFunction("OnPlayerShootPlayer","iiff",i,playerid,(RL_phealth[playerid]-RL_HP),(RL_parmour[playerid]-RL_Armour));
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. RL_Shooting[playerid]=false;
  284. RL_Released[playerid]=GetTickCount();
  285. return (RL_OPSC)?CallLocalFunction("RL_OnPlayerStateChange","iii",playerid,newstate,oldstate):1;
  286. }
  287. #if defined _ALS_OnPlayerStateChange
  288. #undef OnPlayerStateChange
  289. #else
  290. #define _ALS_OnPlayerStateChange
  291. #endif
  292. #define OnPlayerStateChange RL_OnPlayerStateChange
  293. forward RL_OnPlayerStateChange(playerid,newstate, oldstate);
  294. // OnPlayerConnect
  295. public OnPlayerConnect(playerid)
  296. {
  297. RL_Shooting[playerid]=false;
  298. RL_Released[playerid]=0;
  299. return (RL_OPC)?CallLocalFunction("RL_OnPlayerConnect","i",playerid):1;
  300. }
  301. #if defined _ALS_OnPlayerConnect
  302. #undef OnPlayerConnect
  303. #else
  304. #define _ALS_OnPlayerConnect
  305. #endif
  306. #define OnPlayerConnect RL_OnPlayerConnect
  307. forward RL_OnPlayerConnect(playerid);
  308. #if !defined FILTERSCRIPT
  309. // OnGameModeInit
  310. public OnGameModeInit()
  311. {
  312. RL_OPUP = (funcidx("RL_OnPlayerUpdate") != -1);
  313. RL_OPSC = (funcidx("RL_OnPlayerStateChange") != -1);
  314. RL_OPKSC = (funcidx("RL_OnPlayerKeyStateChange") != -1);
  315. RL_OPC = (funcidx("RL_OnPlayerConnect") != -1);
  316. return (funcidx("RL_OnGameModeInit") != -1)?CallLocalFunction("RL_OnGameModeInit",""):1;
  317. }
  318. #if defined _ALS_OnGameModeInit
  319. #undef OnGameModeInit
  320. #else
  321. #define _ALS_OnGameModeInit
  322. #endif
  323. #define OnGameModeInit RL_OnGameModeInit
  324. forward RL_OnGameModeInit();
  325. #else
  326. // OnFilterScriptInit
  327. public OnFilterScriptInit()
  328. {
  329. RL_OPUP = (funcidx("RL_OnPlayerUpdate") != -1);
  330. RL_OPSC = (funcidx("RL_OnPlayerStateChange") != -1);
  331. RL_OPKSC = (funcidx("RL_OnPlayerKeyStateChange") != -1);
  332. RL_OPC = (funcidx("RL_OnPlayerConnect") != -1);
  333. return (funcidx("RL_OnFilterScriptInit") != -1)?CallLocalFunction("RL_OnFilterScriptInit",""):1;
  334. }
  335. #if defined _ALS_OnFilterScriptInit
  336. #undef OnFilterScriptInit
  337. #else
  338. #define _ALS_OnFilterScriptInit
  339. #endif
  340. #define OnFilterScriptInit RL_OnFilterScriptInit.
  341. forward RL_OnFilterScriptInit();
  342. #endif
  343. // The end.