1
0

opsp.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /********************************************
  2. * OnPlayerShootPlayer! V5.0 *
  3. * Credits: wups, *
  4. * Double-O-Seven for his HS functions *
  5. ********************************************/
  6. // include
  7. #include <a_samp>
  8. #tryinclude <foreach>
  9. // defines
  10. #if defined OPSP
  11. #endinput
  12. #endif
  13. #define OPSP
  14. #if !defined foreach
  15. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  16. #define __SSCANF_FOREACH__
  17. #endif
  18. #if defined FILTERSCRIPT
  19. #error "OnPlayerShootPlayer ERROR: You must include it in your game mode, not in your filterscript!"
  20. #endif
  21. #if !defined PRESSED
  22. #define PRESSED(%0) \
  23. (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  24. #endif
  25. #if !defined RELEASED
  26. #define RELEASED(%0) \
  27. (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
  28. #endif
  29. // variables
  30. static
  31. Float:RL_phealth[MAX_PLAYERS],
  32. Float:RL_parmour[MAX_PLAYERS],
  33. bool:RL_Shooting[MAX_PLAYERS],
  34. bool:RL_UpdatedHealth[MAX_PLAYERS],
  35. bool:RL_OPUP,
  36. bool:RL_OPSC,
  37. bool:RL_OPKSC,
  38. bool:RL_OPC,
  39. RL_Released[MAX_PLAYERS];
  40. // forwards
  41. forward OnPlayerShootPlayer(Shooter,Target,Float:HealthLost,Float:ArmourLost);
  42. public OnPlayerUpdate(playerid)
  43. {
  44. static Float:RL_HP,
  45. Float:RL_Armour;
  46. GetPlayerHealth(playerid,RL_HP);
  47. GetPlayerArmour(playerid,RL_Armour);
  48. if(RL_HP < RL_phealth[playerid] || RL_Armour < RL_parmour[playerid])
  49. {
  50. if(RL_UpdatedHealth[playerid])
  51. RL_UpdatedHealth[playerid]=false;
  52. else
  53. {
  54. static
  55. Float:RL_PlayerPos[3],
  56. Float:RL_Distance,
  57. Float:RL_CameraPos[3],
  58. Float:RL_CameraVectors[3],
  59. RL_Tick
  60. ;
  61. RL_Tick = (GetTickCount()-1000);
  62. GetPlayerPos(playerid, RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  63. foreach(Player,i)
  64. {
  65. if(RL_Shooting[i] || RL_Tick < RL_Released[i])
  66. {
  67. if(i != playerid)
  68. {
  69. GetPlayerCameraFrontVector(i, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2]);
  70. GetPlayerCameraPos(i, RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2]);
  71. if(IsPlayerInRangeOfPoint(i,200.0,RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]))
  72. {
  73. 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]);
  74. if(RL_Distance < 2.5)
  75. {
  76. CallLocalFunction("OnPlayerShootPlayer","iiff",i,playerid,(RL_phealth[playerid]-RL_HP),(RL_parmour[playerid]-RL_Armour));
  77. break;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. RL_phealth[playerid]=RL_HP;
  86. RL_parmour[playerid]=RL_Armour;
  87. return (RL_OPUP)?CallLocalFunction("RL_OnPlayerUpdate","i",playerid):1;
  88. }
  89. // Functions
  90. stock crossp(Float:v1x, Float:v1y, Float:v1z, Float:v2x, Float:v2y, Float:v2z, &Float:output)
  91. {
  92. new
  93. Float:c1 = (v1y * v2z) - (v1z * v2y),
  94. Float:c2 = (v1z * v2x) - (v1x * v2z),
  95. Float:c3 = (v1x * v2y) - (v1y * v2x);
  96. output = floatsqroot ((c1 * c1) + (c2 * c2) + (c3 * c3));
  97. return 0;
  98. }
  99. 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)
  100. {
  101. //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).
  102. static Float:output;
  103. 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.
  104. distance = output / floatsqroot ((line_vector_x * line_vector_x) + (line_vector_y * line_vector_y) + (line_vector_z * line_vector_z));
  105. return 0;
  106. }
  107. // SetPlayerHealth
  108. stock SetPlayerHealthEx(playerid, Float:health)
  109. {
  110. RL_phealth[playerid]=health;
  111. RL_UpdatedHealth[playerid]=true;
  112. return SetPlayerHealth(playerid, health);
  113. }
  114. #define SetPlayerHealth SetPlayerHealthEx
  115. // SetPlayerArmour
  116. stock SetPlayerArmourEx(playerid, Float:armour)
  117. {
  118. RL_parmour[playerid]=armour;
  119. RL_UpdatedHealth[playerid]=true;
  120. return SetPlayerArmour(playerid, armour);
  121. }
  122. #define SetPlayerArmour SetPlayerArmourEx
  123. #if defined _ALS_OnPlayerUpdate
  124. #undef OnPlayerUpdate
  125. #else
  126. #define _ALS_OnPlayerUpdate
  127. #endif
  128. #define OnPlayerUpdate RL_OnPlayerUpdate
  129. forward RL_OnPlayerUpdate(playerid);
  130. // OnPlayerKeyStateChange
  131. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  132. {
  133. if(PRESSED(KEY_FIRE)) RL_Shooting[playerid]=true;
  134. else if(RELEASED(KEY_FIRE))
  135. {
  136. RL_Shooting[playerid]=false;
  137. RL_Released[playerid]=GetTickCount();
  138. }
  139. return (RL_OPKSC)?CallLocalFunction("RL_OnPlayerKeyStateChange","iii",playerid,newkeys,oldkeys):1;
  140. }
  141. #if defined _ALS_OnPlayerKeyStateChange
  142. #undef OnPlayerKeyStateChange
  143. #else
  144. #define _ALS_OnPlayerKeyStateChange
  145. #endif
  146. #define OnPlayerKeyStateChange RL_OnPlayerKeyStateChange
  147. forward RL_OnPlayerKeyStateChange(playerid,newkeys,oldkeys);
  148. // OnPlayerStateChange
  149. public OnPlayerStateChange(playerid, newstate, oldstate)
  150. {
  151. if(newstate == PLAYER_STATE_WASTED)
  152. {
  153. if(RL_UpdatedHealth[playerid])
  154. RL_UpdatedHealth[playerid]=false;
  155. else
  156. {
  157. static
  158. Float:RL_PlayerPos[3],
  159. Float:RL_Distance,
  160. Float:RL_CameraPos[3],
  161. Float:RL_CameraVectors[3],
  162. RL_Tick,
  163. Float:RL_HP,
  164. Float:RL_Armour
  165. ;
  166. RL_Tick = (GetTickCount()-1000);
  167. GetPlayerPos(playerid, RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]);
  168. foreach(Player,i)
  169. {
  170. if(RL_Shooting[i] || RL_Tick < RL_Released[i])
  171. {
  172. if(i != playerid)
  173. {
  174. GetPlayerCameraFrontVector(i, RL_CameraVectors[0], RL_CameraVectors[1], RL_CameraVectors[2]);
  175. GetPlayerCameraPos(i, RL_CameraPos[0], RL_CameraPos[1], RL_CameraPos[2]);
  176. if(IsPlayerInRangeOfPoint(i,200.0,RL_PlayerPos[0], RL_PlayerPos[1], RL_PlayerPos[2]))
  177. {
  178. 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]);
  179. if(RL_Distance < 2.5)
  180. {
  181. CallLocalFunction("OnPlayerShootPlayer","iiff",i,playerid,(RL_phealth[playerid]-RL_HP),(RL_parmour[playerid]-RL_Armour));
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. RL_Shooting[playerid]=false;
  191. RL_Released[playerid]=GetTickCount();
  192. return (RL_OPSC)?CallLocalFunction("RL_OnPlayerStateChange","iii",playerid,newstate,oldstate):1;
  193. }
  194. #if defined _ALS_OnPlayerStateChange
  195. #undef OnPlayerStateChange
  196. #else
  197. #define _ALS_OnPlayerStateChange
  198. #endif
  199. #define OnPlayerStateChange RL_OnPlayerStateChange
  200. forward RL_OnPlayerStateChange(playerid,newstate, oldstate);
  201. // OnPlayerConnect
  202. public OnPlayerConnect(playerid)
  203. {
  204. RL_Shooting[playerid]=false;
  205. RL_Released[playerid]=0;
  206. return (RL_OPC)?CallLocalFunction("RL_OnPlayerConnect","i",playerid):1;
  207. }
  208. #if defined _ALS_OnPlayerConnect
  209. #undef OnPlayerConnect
  210. #else
  211. #define _ALS_OnPlayerConnect
  212. #endif
  213. #define OnPlayerConnect RL_OnPlayerConnect
  214. forward RL_OnPlayerConnect(playerid);
  215. // OnGameModeInit
  216. public OnGameModeInit()
  217. {
  218. RL_OPUP = (funcidx("RL_OnPlayerUpdate") != -1);
  219. RL_OPSC = (funcidx("RL_OnPlayerStateChange") != -1);
  220. RL_OPKSC = (funcidx("RL_OnPlayerKeyStateChange") != -1);
  221. RL_OPC = (funcidx("RL_OnPlayerConnect") != -1);
  222. return (funcidx("RL_OnGameModeInit") != -1)?CallLocalFunction("RL_OnGameModeInit",""):1;
  223. }
  224. #if defined _ALS_OnGameModeInit
  225. #undef OnGameModeInit
  226. #else
  227. #define _ALS_OnGameModeInit
  228. #endif
  229. #define OnGameModeInit RL_OnGameModeInit
  230. forward RL_OnGameModeInit();
  231. // The end.