attachments.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. attachments.inc
  3. - This include fixes the attachments bug, which is expierenced while zooming with snipers, camera, RPGs..
  4. - The attached object info now can be retrieved from GetPlayerAttachedObject
  5. - This include also restores player attachments on Spawn, so they will remain!
  6. - Disconnect fix; Destroys the attachments on Disconnect.
  7. Author:
  8. - Gammix
  9. (c) Copyright 2015
  10. - This file is provided as is (no warranties).
  11. */
  12. /*
  13. native SetPlayerAttachedObject(playerid, index, modelid, bone, Float:fOffsetX = 0.0, Float:fOffsetY = 0.0, Float:fOffsetZ = 0.0, Float:fRotX = 0.0, Float:fRotY = 0.0, Float:fRotZ = 0.0, Float:fScaleX = 1.0, Float:fScaleY = 1.0, Float:fScaleZ = 1.0, materialcolor1 = 0, materialcolor2 = 0);
  14. native GetPlayerAttachedObject(playerid, index, &modelid, &bone, &Float:fOffsetX, &Float:fOffsetY, &Float:fOffsetZ, &Float:fRotX, &Float:fRotY, &Float:fRotZ, &Float:fScaleX, &Float:fScaleY, &Float:fScaleZ, &materialcolor1, &materialcolor2);
  15. native RemovePlayerAttachedObject(playerid, index);
  16. */
  17. #define att_modelid (0)
  18. #define att_bone (1)
  19. #define att_offset_x (2)
  20. #define att_offset_y (3)
  21. #define att_offset_z (4)
  22. #define att_rot_x (5)
  23. #define att_rot_y (6)
  24. #define att_rot_z (7)
  25. #define att_scale_x (8)
  26. #define att_scale_y (9)
  27. #define att_scale_z (10)
  28. #define att_color_1 (11)
  29. #define att_color_2 (12)
  30. #define att_used (13)
  31. new
  32. g_AttachmentData[][45] =
  33. {
  34. "att_modelid",
  35. "att_bone",
  36. "att_offset_x",
  37. "att_offset_y",
  38. "att_offset_z",
  39. "att_rot_x",
  40. "att_rot_y",
  41. "att_rot_z",
  42. "att_scale_x",
  43. "att_scale_y",
  44. "att_scale_z",
  45. "att_color_1",
  46. "att_color_2",
  47. "att_used"
  48. };
  49. stock static ATT_SetInt(playerid, index, type, val)
  50. {
  51. new
  52. s_String[45]
  53. ;
  54. format(s_String, sizeof(s_String), "%i_%s", index, g_AttachmentData[type]);
  55. return SetPVarInt(playerid, s_String, val);
  56. }
  57. stock static ATT_GetInt(playerid, index, type)
  58. {
  59. new
  60. s_String[45]
  61. ;
  62. format(s_String, sizeof(s_String), "%i_%s", index, g_AttachmentData[type]);
  63. return GetPVarInt(playerid, s_String);
  64. }
  65. stock static ATT_SetFloat(playerid, index, type, Float:val)
  66. {
  67. new
  68. s_String[45]
  69. ;
  70. format(s_String, sizeof(s_String), "%i_%s", index, g_AttachmentData[type]);
  71. return SetPVarFloat(playerid, s_String, val);
  72. }
  73. stock static Float:ATT_GetFloat(playerid, index, type)
  74. {
  75. new
  76. s_String[45]
  77. ;
  78. format(s_String, sizeof(s_String), "%i_%s", index, g_AttachmentData[type]);
  79. return GetPVarFloat(playerid, s_String);
  80. }
  81. public OnPlayerSpawn(playerid)
  82. {
  83. for(new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
  84. {
  85. if(ATT_GetInt(playerid, i, att_used))
  86. {
  87. SetPlayerAttachedObject( playerid,
  88. i,
  89. ATT_GetInt(playerid, i, att_modelid),
  90. ATT_GetInt(playerid, i, att_bone),
  91. ATT_GetFloat(playerid, i, att_offset_x),
  92. ATT_GetFloat(playerid, i, att_offset_y),
  93. ATT_GetFloat(playerid, i, att_offset_z),
  94. ATT_GetFloat(playerid, i, att_rot_x),
  95. ATT_GetFloat(playerid, i, att_rot_y),
  96. ATT_GetFloat(playerid, i, att_rot_z),
  97. ATT_GetFloat(playerid, i, att_scale_x),
  98. ATT_GetFloat(playerid, i, att_scale_y),
  99. ATT_GetFloat(playerid, i, att_scale_z),
  100. ATT_GetInt(playerid, i, att_color_1),
  101. ATT_GetInt(playerid, i, att_color_2)
  102. );
  103. }
  104. }
  105. #if defined ATT_OnPlayerSpawn
  106. return ATT_OnPlayerSpawn(playerid);
  107. #else
  108. return 1;
  109. #endif
  110. }
  111. #if defined _ALS_OnPlayerSpawn
  112. #undef OnPlayerSpawn
  113. #else
  114. #define _ALS_OnPlayerSpawn
  115. #endif
  116. #define OnPlayerSpawn ATT_OnPlayerSpawn
  117. #if defined ATT_OnPlayerSpawn
  118. forward ATT_OnPlayerSpawn(playerid);
  119. #endif
  120. public OnPlayerConnect(playerid)
  121. {
  122. for(new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
  123. {
  124. ATT_SetInt(playerid, i, att_used, false);
  125. }
  126. #if defined ATT_OnPlayerConnect
  127. return ATT_OnPlayerConnect(playerid);
  128. #else
  129. return 1;
  130. #endif
  131. }
  132. #if defined _ALS_OnPlayerConnect
  133. #undef OnPlayerConnect
  134. #else
  135. #define _ALS_OnPlayerConnect
  136. #endif
  137. #define OnPlayerConnect ATT_OnPlayerConnect
  138. #if defined ATT_OnPlayerConnect
  139. forward ATT_OnPlayerConnect(playerid);
  140. #endif
  141. public OnPlayerDisconnect(playerid, reason)
  142. {
  143. for(new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
  144. {
  145. ATT_SetInt(playerid, i, att_used, false);
  146. if(IsPlayerAttachedObjectSlotUsed(playerid, i))
  147. {
  148. RemovePlayerAttachedObject(playerid, i);
  149. }
  150. }
  151. #if defined ATT_OnPlayerDisconnect
  152. return ATT_OnPlayerDisconnect(playerid, reason);
  153. #else
  154. return 1;
  155. #endif
  156. }
  157. #if defined _ALS_OnPlayerDisconnect
  158. #undef OnPlayerDisconnect
  159. #else
  160. #define _ALS_OnPlayerDisconnect
  161. #endif
  162. #define OnPlayerDisconnect ATT_OnPlayerDisconnect
  163. #if defined ATT_OnPlayerDisconnect
  164. forward ATT_OnPlayerDisconnect(playerid, reason);
  165. #endif
  166. public OnPlayerEditAttachedObject(playerid, response, index, modelid, boneid, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:fRotZ, Float:fScaleX, Float:fScaleY, Float:fScaleZ)
  167. {
  168. ATT_SetInt(playerid, index, att_modelid, modelid);
  169. ATT_SetInt(playerid, index, att_bone, boneid);
  170. ATT_SetFloat(playerid, index, att_offset_x, fOffsetX);
  171. ATT_SetFloat(playerid, index, att_offset_y, fOffsetY);
  172. ATT_SetFloat(playerid, index, att_offset_z, fOffsetZ);
  173. ATT_SetFloat(playerid, index, att_rot_x, fRotX);
  174. ATT_SetFloat(playerid, index, att_rot_y, fRotY);
  175. ATT_SetFloat(playerid, index, att_rot_z, fRotZ);
  176. ATT_SetFloat(playerid, index, att_scale_x, fScaleX);
  177. ATT_SetFloat(playerid, index, att_scale_y, fScaleY);
  178. ATT_SetFloat(playerid, index, att_scale_z, fScaleZ);
  179. #if defined ATT_OnPlayerEditAttachedObject
  180. return ATT_OnPlayerEditAttachedObject(playerid, response, index, modelid, boneid, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:fRotZ, Float:fScaleX, Float:fScaleY, Float:fScaleZ);
  181. #else
  182. return 1;
  183. #endif
  184. }
  185. #if defined _ALS_OnPlayerEditAttachedObject
  186. #undef OnPlayerEditAttachedObject
  187. #else
  188. #define _ALS_OnPlayerEditAttachedObject
  189. #endif
  190. #define OnPlayerEditAttachedObject ATT_OnPlayerEditAttachedObject
  191. #if defined ATT_OnPlayerEditAttachedObject
  192. forward ATT_OnPlayerEditAttachedObject(playerid, response, index, modelid, boneid, Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ, Float:fRotX, Float:fRotY, Float:fRotZ, Float:fScaleX, Float:fScaleY, Float:fScaleZ);
  193. #endif
  194. #if ! defined KEY_AIM
  195. #define KEY_AIM 128
  196. #endif
  197. #if ! defined HOLDING
  198. #define HOLDING(%0) ((newkeys & (%0)) == (%0))
  199. #endif
  200. #if ! defined RELEASED
  201. #define RELEASED(%0) (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
  202. #endif
  203. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  204. {
  205. if (HOLDING(KEY_AIM))
  206. {
  207. switch (GetPlayerWeapon(playerid))
  208. {
  209. case WEAPON_SNIPER, WEAPON_ROCKETLAUNCHER, WEAPON_HEATSEEKER, WEAPON_CAMERA:
  210. {
  211. for (new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
  212. {
  213. if (IsPlayerAttachedObjectSlotUsed(playerid, i) &&
  214. ATT_GetInt(playerid, i, att_used))
  215. {
  216. RemovePlayerAttachedObject(playerid, i);
  217. }
  218. }
  219. }
  220. }
  221. }
  222. if(RELEASED(KEY_AIM))
  223. {
  224. for (new i; i < MAX_PLAYER_ATTACHED_OBJECTS; i++)
  225. {
  226. if (! IsPlayerAttachedObjectSlotUsed(playerid, i) &&
  227. ATT_GetInt(playerid, i, att_used))
  228. {
  229. SetPlayerAttachedObject( playerid,
  230. i,
  231. ATT_GetInt(playerid, i, att_modelid),
  232. ATT_GetInt(playerid, i, att_bone),
  233. ATT_GetFloat(playerid, i, att_offset_x),
  234. ATT_GetFloat(playerid, i, att_offset_y),
  235. ATT_GetFloat(playerid, i, att_offset_z),
  236. ATT_GetFloat(playerid, i, att_rot_x),
  237. ATT_GetFloat(playerid, i, att_rot_y),
  238. ATT_GetFloat(playerid, i, att_rot_z),
  239. ATT_GetFloat(playerid, i, att_scale_x),
  240. ATT_GetFloat(playerid, i, att_scale_y),
  241. ATT_GetFloat(playerid, i, att_scale_z),
  242. ATT_GetInt(playerid, i, att_color_1),
  243. ATT_GetInt(playerid, i, att_color_2)
  244. );
  245. }
  246. }
  247. }
  248. #if defined ATT_OnPlayerKeyStateChange
  249. return ATT_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  250. #else
  251. return 1;
  252. #endif
  253. }
  254. #if defined _ALS_OnPlayerKeyStateChange
  255. #undef OnPlayerKeyStateChange
  256. #else
  257. #define _ALS_OnPlayerKeyStateChange
  258. #endif
  259. #define OnPlayerKeyStateChange ATT_OnPlayerKeyStateChange
  260. #if defined ATT_OnPlayerKeyStateChange
  261. forward ATT_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  262. #endif
  263. stock ATT_SetPlayerAttachedObject(playerid, index, modelid, bone, Float:fOffsetX = 0.0, Float:fOffsetY = 0.0, Float:fOffsetZ = 0.0, Float:fRotX = 0.0, Float:fRotY = 0.0, Float:fRotZ = 0.0, Float:fScaleX = 1.0, Float:fScaleY = 1.0, Float:fScaleZ = 1.0, materialcolor1 = 0, materialcolor2 = 0)
  264. {
  265. if(SetPlayerAttachedObject(playerid, index, modelid, bone, fOffsetX, fOffsetY, fOffsetZ, fRotX, fRotY, fRotZ, fScaleX, fScaleY, fScaleZ, materialcolor1, materialcolor2))
  266. {
  267. ATT_SetInt(playerid, index, att_modelid, modelid);
  268. ATT_SetInt(playerid, index, att_bone, bone);
  269. ATT_SetFloat(playerid, index, att_offset_x, fOffsetX);
  270. ATT_SetFloat(playerid, index, att_offset_y, fOffsetY);
  271. ATT_SetFloat(playerid, index, att_offset_z, fOffsetZ);
  272. ATT_SetFloat(playerid, index, att_rot_x, fRotX);
  273. ATT_SetFloat(playerid, index, att_rot_y, fRotY);
  274. ATT_SetFloat(playerid, index, att_rot_z, fRotZ);
  275. ATT_SetFloat(playerid, index, att_scale_x, fScaleX);
  276. ATT_SetFloat(playerid, index, att_scale_y, fScaleY);
  277. ATT_SetFloat(playerid, index, att_scale_z, fScaleZ);
  278. ATT_SetInt(playerid, index, att_color_1, materialcolor1);
  279. ATT_SetInt(playerid, index, att_color_2, materialcolor2);
  280. ATT_SetInt(playerid, index, att_used, true);
  281. return true;
  282. }
  283. return false;
  284. }
  285. #if defined _ALS_SetPlayerAttachedObject
  286. #undef SetPlayerAttachedObject
  287. #else
  288. #define _ALS_SetPlayerAttachedObject
  289. #endif
  290. #define SetPlayerAttachedObject ATT_SetPlayerAttachedObject
  291. stock GetPlayerAttachedObject(playerid, index, &modelid, &bone, &Float:fOffsetX, &Float:fOffsetY, &Float:fOffsetZ, &Float:fRotX, &Float:fRotY, &Float:fRotZ, &Float:fScaleX, &Float:fScaleY, &Float:fScaleZ, &materialcolor1, &materialcolor2)
  292. {
  293. if(ATT_GetInt(playerid, index, att_used))
  294. {
  295. modelid = ATT_GetInt(playerid, index, att_modelid);
  296. bone = ATT_GetInt(playerid, index, att_bone);
  297. fOffsetX = ATT_GetFloat(playerid, index, att_offset_x);
  298. fOffsetY = ATT_GetFloat(playerid, index, att_offset_y);
  299. fOffsetZ = ATT_GetFloat(playerid, index, att_offset_z);
  300. fRotX = ATT_GetFloat(playerid, index, att_rot_x);
  301. fRotY = ATT_GetFloat(playerid, index, att_rot_y);
  302. fRotZ = ATT_GetFloat(playerid, index, att_rot_z);
  303. fScaleX = ATT_GetFloat(playerid, index, att_scale_x);
  304. fScaleY = ATT_GetFloat(playerid, index, att_scale_y);
  305. fScaleZ = ATT_GetFloat(playerid, index, att_scale_z);
  306. materialcolor1 = ATT_GetInt(playerid, index, att_color_1);
  307. materialcolor2 = ATT_GetInt(playerid, index, att_color_2);
  308. return true;
  309. }
  310. return false;
  311. }
  312. stock ATT_RemovePlayerAttachedObject(playerid, index)
  313. {
  314. if(RemovePlayerAttachedObject(playerid, index))
  315. {
  316. ATT_SetInt(playerid, index, att_used, false);
  317. return true;
  318. }
  319. return false;
  320. }
  321. #if defined _ALS_RemovePlayerAttachedObject
  322. #undef RemovePlayerAttachedObject
  323. #else
  324. #define _ALS_RemovePlayerAttachedObject
  325. #endif
  326. #define RemovePlayerAttachedObject ATT_RemovePlayerAttachedObject
  327. #undef att_modelid
  328. #undef att_bone
  329. #undef att_offset_x
  330. #undef att_offset_y
  331. #undef att_offset_z
  332. #undef att_rot_x
  333. #undef att_rot_y
  334. #undef att_rot_z
  335. #undef att_scale_x
  336. #undef att_scale_y
  337. #undef att_scale_z
  338. #undef att_color_1
  339. #undef att_color_2
  340. #undef att_used