Guest User

[FS] Cruise Control/Flying Car Mode

a guest
Nov 19th, 2011
1,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. #include <a_samp>
  2. #include <sscanf2> //Credits to Y_Less (http://forum.sa-mp.com/showthread.php?t=120356)
  3. #include <zcmd> //Credits to Zeex (http://forum.sa-mp.com/showthread.php?t=91354)
  4. #define COLOR_RED 0xAA3333AA
  5. #define COLOR_YELLOW 0xFFFF00AA
  6. #undef MAX_PLAYERS
  7. #define MAX_PLAYERS 100 //number of server slots
  8. #define UPDATE_RATE 50 //define update rate of vehicle speed (milliseconds); lower values might cause lag; higher values causes not constant vehicle speed
  9. #define CCKey KEY_ACTION //define key for CC mode
  10. #define JumpKey KEY_CROUCH //define key for vehicle jump
  11. #define JumpSize 0.2 //define vehicle jump size when press JumpKey
  12. #define AutoRepair 1 //autorepair vehicle while using CC mode: on - 1; off - 0
  13. //Speedboost functions were found at http://forum.sa-mp.com/showthread.php?t=199628 tutorial by The_Gangstas
  14.  
  15. new bool: cc[MAX_PLAYERS char];
  16. new Float: ccspeed[MAX_PLAYERS];
  17.  
  18. public OnFilterScriptInit()
  19. {
  20. for(new i=0; i<MAX_PLAYERS; i++) if(IsPlayerConnected(i)) OnPlayerConnect(i);
  21. return print("Cruise Control/Flying Car Mode by Riccor loaded!");
  22. }
  23.  
  24. public OnPlayerConnect(playerid)
  25. {
  26. cc{playerid} = false;
  27. ccspeed[playerid] = 0.1;
  28. return 1;
  29. }
  30.  
  31. CMD:cc(playerid,params[])
  32. {
  33. new Float: value;
  34. if(!sscanf(params,"f", value))
  35. {
  36. if(value < 0.05 || value > 3.0) return SendClientMessage(playerid, COLOR_RED, "[ERROR:] Invalid Speed Value! [0.05-3.0]");
  37. ccspeed[playerid] = value;
  38. new string[128];
  39. format(string,sizeof(string),":: You setted CC Speed Value to %.2f!", ccspeed[playerid]);
  40. return SendClientMessage(playerid, COLOR_YELLOW, string);
  41. }
  42. if(cc{playerid})
  43. {
  44. cc{playerid} = false;
  45. return SendClientMessage(playerid, COLOR_RED, ":: You disabled CC mode!");
  46. }
  47. else
  48. {
  49. cc{playerid} = true;
  50. return SendClientMessage(playerid, COLOR_YELLOW, ":: You enabled CC mode! Press and hold CTRL key while driving to use it. Use /cc [value] to change CC speed.");
  51. }
  52. }
  53.  
  54. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  55. {
  56. if(cc{playerid})
  57. {
  58. if(newkeys & CCKey) ccf(playerid);
  59. else if(newkeys & JumpKey)
  60. {
  61. new Float: x, Float:y, Float:z;
  62. GetVehicleVelocity(GetPlayerVehicleID(playerid), x, y, z);
  63. SetVehicleVelocity(GetPlayerVehicleID(playerid),x, y, z + JumpSize);
  64. }
  65. }
  66. return 1;
  67. }
  68.  
  69. forward ccf(playerid);
  70. public ccf(playerid)
  71. {
  72. new keys, ud, lr;
  73. GetPlayerKeys(playerid, keys, ud, lr);
  74. if((keys & CCKey))
  75. {
  76. new Float: adirection[3];
  77. new vehid = GetPlayerVehicleID(playerid);
  78. new Float:Quaternion[4];
  79. new Float:transformationmatrix[4][4];
  80.  
  81. GetVehicleRotationQuat(vehid, Quaternion[0], Quaternion[1], Quaternion[2], Quaternion[3]);
  82.  
  83. new Float:xx = Quaternion[0] * Quaternion[0];
  84. new Float:xy = Quaternion[0] * Quaternion[1];
  85. new Float:xz = Quaternion[0] * Quaternion[2];
  86. new Float:xw = Quaternion[0] * Quaternion[3];
  87. new Float:yy = Quaternion[1] * Quaternion[1];
  88. new Float:yz = Quaternion[1] * Quaternion[2];
  89. new Float:yw = Quaternion[1] * Quaternion[3];
  90. new Float:zz = Quaternion[2] * Quaternion[2];
  91. new Float:zw = Quaternion[2] * Quaternion[3];
  92.  
  93. transformationmatrix[0][0] = 1 - 2 * ( yy + zz );
  94. transformationmatrix[0][1] = 2 * ( xy - zw );
  95. transformationmatrix[0][2] = 2 * ( xz + yw );
  96. transformationmatrix[0][3] = 0.0;
  97.  
  98. transformationmatrix[1][0] = 2 * ( xy + zw );
  99. transformationmatrix[1][1] = 1 - 2 * ( xx + zz );
  100. transformationmatrix[1][2] = 2 * ( yz - xw );
  101. transformationmatrix[1][3] = 0.0;
  102.  
  103. transformationmatrix[2][0] = 2 * ( xz - yw );
  104. transformationmatrix[2][1] = 2 * ( yz + xw );
  105. transformationmatrix[2][2] = 1 - 2 * ( xx + yy );
  106. transformationmatrix[2][3] = 0;
  107.  
  108. transformationmatrix[3][0] = 0;
  109. transformationmatrix[3][1] = 0;
  110. transformationmatrix[3][2] = 0;
  111. transformationmatrix[3][3] = 1;
  112.  
  113. adirection[2] = -1 * transformationmatrix[0][1] + transformationmatrix[0][3];
  114. adirection[1] = -1 * transformationmatrix[1][1] + transformationmatrix[1][3];
  115. adirection[0] = -(-1 * transformationmatrix[2][1] + transformationmatrix[2][3]);
  116.  
  117. #if AutoRepair == 1
  118. RepairVehicle(vehid);
  119. #endif
  120.  
  121. SetVehicleVelocity(vehid, adirection[0]*ccspeed[playerid], adirection[1]*ccspeed[playerid], adirection[2]*ccspeed[playerid]);
  122. SetTimerEx("ccf", UPDATE_RATE, false, "i", playerid);
  123. }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment