Advertisement
Gamer_Z

Untitled

Nov 19th, 2011
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.04 KB | None | 0 0
  1. //AutoPilot script by Gamer_Z v0.1
  2. #include <a_samp>//SA:MP Team
  3. #define FILTERSCRIPT
  4. #include <RouteConnector>//GPS Plugin
  5. #include <zcmd>//By Zeex
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //#define USE_TIMERS
  8. ////////////////////////////////////////////////////////////////////////////////
  9. new id[MAX_PLAYERS] = {-1,...};
  10. new CurrTarget[MAX_PLAYERS] = {0,...};
  11. new Timer[MAX_PLAYERS] = {-1,...};
  12. new Float:spd[MAX_PLAYERS] = {0.30,...};
  13. #if defined USE_TIMERS
  14. new interval[MAX_PLAYERS] = {250,...};
  15. #endif
  16.  
  17. //#define USE_SIDES//meh, does not always use side left, sometimes it switches... something with world rotation..
  18. //#define CONTROL_SIDES //
  19.  
  20. #if defined CONTROL_SIDES
  21. new Float:sideDist[MAX_PLAYERS];
  22. #else
  23. #define sideDist[%0] (2.5)
  24. #endif
  25. //#define USE_ANGULAR//Doesn't work? xD
  26. ////////////////////////////////////////////////////////////////////////////////
  27. enum location
  28. {
  29. name[16],
  30. Float:X,
  31. Float:Y,
  32. Float:Z
  33. }
  34.  
  35. #define PLACES (6)
  36. new places[PLACES][location] =
  37. {
  38. {"LV-c",2140.6675,993.1867,10.5248},//Las Venturas
  39. {"SF-c",-2261.2009,564.2894,34.7200},//San Frierro
  40. {"LS-c",2495.3755,-1669.4906,13.5163},//Los Santos
  41. {"LV-a",1686.3107,1609.5485,10.8203},//Las Venturas Airport
  42. {"SF-a",-1538.8635,-422.9142,5.8516},//San Frierro Airport
  43. {"LS-a",1953.5204,-2290.1130,13.5469}//Los Santos Airport
  44. };
  45. ////////////////////////////////////////////////////////////////////////////////
  46. CMD:autopilot(playerid,params[])
  47. {
  48. if(!IsPlayerInAnyVehicle(playerid))
  49. {
  50. SendClientMessage(playerid,-1,"AUTOPILOT YOU MUST BE IN A VEHICLE");
  51. return 1;
  52. }
  53. if(!strcmp(params,"stop",true))
  54. {
  55. if(Timer[playerid] != (-1))
  56. {
  57. #if defined USE_TIMERS
  58. KillTimer(Timer[playerid]);
  59. #endif
  60. Timer[playerid] = -1;
  61. DeleteArray(id[playerid]);
  62. id[playerid] = -1;
  63. CurrTarget[playerid] = 0;
  64. SendClientMessage(playerid,-1,"AUTOPILOT DISABLED");
  65. return 1;
  66. }
  67. SendClientMessage(playerid,-1,"AUTOPILOT COULDN'T BE DISABLED");
  68. return 1;
  69. }
  70. if(params[0] == 48 && params[1] == 46)
  71. {
  72. if(id[playerid] == -1)
  73. {
  74. new out[32];
  75. if(!isNumeric(params[2]))
  76. {
  77. SendClientMessage(playerid,-1,"AUTOPILOT SPEED MUST BE BETWEEN 0.01 and 0.80");
  78. return 1;
  79. }
  80. if(!isNumeric(params[3]))
  81. params[3] = 48;
  82. format(out,32,"%c%c",params[2],params[3]);
  83. new Float:tmpspd = floatdiv(strval(out),100.00);
  84. if(0.01 > tmpspd > 0.80)
  85. {
  86. SendClientMessage(playerid,-1,"AUTOPILOT SPEED MUST BE BETWEEN 0.01 and 0.80");
  87. return 1;
  88. }
  89. spd[playerid] = tmpspd;
  90. format(out,32,"AUTOPILOT SPEED SET TO: %.3f",spd[playerid]);
  91. SendClientMessage(playerid,-1,out);
  92. }
  93. else
  94. {
  95. SendClientMessage(playerid,-1,"AUTOPILOT CANNOT CHANGE SPEED WHILE DRIVING (bug prevention)");
  96. }
  97. return 1;
  98. }
  99.  
  100. if(id[playerid] == -1)
  101. {
  102. if (params[0] == 0 || (params[0] == 1 && params[1] == 0))
  103. {
  104. SendClientMessage(playerid,-1,"AUTOPILOT HAS NO TARGET");
  105. return 1;
  106. }
  107. new place = -1;
  108. for(new i = 0; i < PLACES; ++i)
  109. {
  110. if(!strcmp(places[i][name],params,true))
  111. {
  112. place = i;
  113. break;
  114. }
  115. }
  116. if(place == -1)
  117. {
  118. SendClientMessage(playerid,-1,"AUTOPILOT DOESN'T KNOW THAT PLACE");
  119. return 1;
  120. }
  121. new start = NearestPlayerNode(playerid,15.0);
  122. if(start == -1)
  123. {
  124. SendClientMessage(playerid,-1,"AUTOPILOT MALFUNCTION, PLEASE TRY AGAIN ON ANOTHER PLACE");
  125. return 1;
  126. }
  127. CalculatePath(start,NearestNodeFromPoint(places[place][X],places[place][Y],places[place][Z]),playerid);
  128. SendClientMessage(playerid,-1,"AUTOPILOT IS CALCULATING THE ROUTE FOR YOU..");
  129. return 1;
  130. }
  131. SendClientMessage(playerid,-1,"AUTOPILOT ALREADY TURNED ON");
  132. return 1;
  133. }
  134.  
  135. CMD:ap(playerid,params[])
  136. {
  137. return cmd_autopilot(playerid,params);
  138. }
  139.  
  140. stock isNumeric(const string[])
  141. {
  142. new length=strlen(string);
  143. if (length==0) return false;
  144. for (new i = 0; i < length; i++)
  145. {
  146. if (
  147. (string[i] > '9' || string[i] < '0' && string[i]!='-' && string[i]!='+') // Not a number,'+' or '-'
  148. || (string[i]=='-' && i!=0) // A '-' but not at first.
  149. || (string[i]=='+' && i!=0) // A '+' but not at first.
  150. ) return false;
  151. }
  152. if (length==1 && (string[0]=='-' || string[0]=='+')) return false;
  153. return true;
  154. }
  155.  
  156. #if defined USE_TIMERS
  157. CMD:interval(playerid,params[])
  158. {
  159. interval[playerid] = strval(params);
  160. format(string,128,"Interval: %d",interval);
  161. SendClientMessage(playerid,-1,string);
  162. return 1;
  163. }
  164. #endif
  165. ////////////////////////////////////////////////////////////////////////////////
  166. public GPS_WhenRouteIsCalculated(routeid,node_id_array[],amount_of_nodes,distance)
  167. {
  168. id[routeid] = StoreRouteArray(amount_of_nodes,node_id_array);
  169. #if defined USE_TIMERS
  170. Timer[routeid] = SetTimerEx("AutoPilot",interval[routeid],1,"i",routeid);
  171. #else
  172. Timer[routeid] = 1;
  173. #endif
  174. SendClientMessage(routeid,-1,".. AUTOPILOT WILL DRIVE YOU NOW TO YOUR DESTINATION");
  175. return 1;
  176. }
  177.  
  178. #if defined USE_TIMERS
  179.  
  180. #else
  181.  
  182. #if defined USE_SIDES
  183. #if defined CONTROL_SIDES
  184. new Keys,ud,lr;
  185. #endif
  186. #endif
  187. public OnPlayerUpdate(playerid)
  188. {
  189. if(Timer[playerid] == 1)
  190. {
  191. AutoPilot(playerid);
  192. }
  193. return 1;
  194. }
  195. #endif
  196.  
  197. public OnPlayerConnect(playerid)
  198. {
  199. Timer[playerid] = -1;
  200. id[playerid] = -1;
  201. CurrTarget[playerid] = 0;
  202. #if defined USE_TIMERS
  203. interval[playerid] = 250;
  204. #endif
  205. #if defined USE_SIDES
  206. #if defined CONTROL_SIDES
  207. sideDist[playerid] = 2.50;
  208. #endif
  209. #endif
  210. return 1;
  211. }
  212.  
  213. public OnPlayerDisconnect(playerid,reason)
  214. {
  215. if(Timer[playerid] != (-1))
  216. {
  217. #if defined USE_TIMERS
  218. KillTimer(Timer[playerid]);
  219. #endif
  220. Timer[playerid] = -1;
  221. DeleteArray(id[playerid]);
  222. id[playerid] = -1;
  223. CurrTarget[playerid] = 0;
  224. }
  225. return 1;
  226. }
  227.  
  228. forward AutoPilot(playerid);
  229. public AutoPilot(playerid)
  230. {
  231. #if defined USE_SIDES
  232. #if defined CONTROL_SIDES
  233. GetPlayerKeys(playerid,Keys,ud,lr);
  234. if(lr > 0)//right
  235. {
  236. sideDist[playerid] += 0.5;
  237. if(sideDist[playerid] >= 5.0)
  238. sideDist[playerid] = 5.0;
  239. }
  240. else if(lr < 0)//lrft
  241. {
  242. sideDist[playerid] -= 0.5;
  243. if(sideDist[playerid] <= -5.0)
  244. sideDist[playerid] = -5.0;
  245. }
  246. #endif
  247. #endif
  248. if(IsPlayerInAnyVehicle(playerid))
  249. {
  250. new Float:pos[2][3];
  251. new vehicleid = GetPlayerVehicleID(playerid);
  252. if(CurrTarget[playerid] == 0)
  253. {
  254. CurrTarget[playerid]++;
  255. GetNodePos(GetRouteAtPos(id[playerid],CurrTarget[playerid]),pos[0][0],pos[0][1],pos[0][2]);
  256. SetVehiclePos(vehicleid,pos[0][0],pos[0][1],pos[0][2]+2.0);
  257. return 1;
  258. }
  259. new amount;
  260. new nodeid = GetRouteAtPos(id[playerid],CurrTarget[playerid],amount);
  261. if((CurrTarget[playerid]+1) >= amount)
  262. {
  263. #if defined USE_TIMERS
  264. KillTimer(Timer[playerid]);
  265. #endif
  266. Timer[playerid] = -1;
  267. DeleteArray(id[playerid]);
  268. id[playerid] = -1;
  269. CurrTarget[playerid] = 0;
  270. #if defined USE_ANGULAR
  271. SetVehicleAngularVelocity(vehicleid,0.0,0.0,0.0);
  272. #endif
  273. SetVehicleVelocity(vehicleid,0.0,0.0,0.0);
  274. SendClientMessage(playerid,-1,"Destination reached, have a nice day.");
  275. return 1;
  276. }
  277. GetNodePos(nodeid,pos[1][0],pos[1][1],pos[1][2]);
  278. #if defined USE_SIDES
  279. GetXYRightOfPos(
  280. //atan2VehicleZ(pos[0][0], pos[0][1], pos[1][0],pos[1][1])
  281. atan2( pos[1][0]-pos[0][0],pos[1][1]-pos[0][1])
  282. ,pos[1][0],pos[1][1],sideDist[playerid]);
  283. #endif
  284. if(IsPlayerInRangeOfPoint(playerid,10.0,pos[1][0],pos[1][1],pos[1][2]))
  285. {
  286. CurrTarget[playerid]++;
  287. return 1;
  288. }
  289. PullVehicleIntoDirection(vehicleid,pos[1][0],pos[1][1],pos[1][2],spd[playerid]);
  290. }
  291. else
  292. {
  293. #if defined USE_TIMERS
  294. KillTimer(Timer[playerid]);
  295. #endif
  296. Timer[playerid] = -1;
  297. DeleteArray(id[playerid]);
  298. id[playerid] = -1;
  299. CurrTarget[playerid] = 0;
  300. }
  301. return 1;
  302. }
  303. //--------------------AUTO-PILOT-CORE-FUNCTIONS-------------------------------//
  304. #define DEPRECATE_Z
  305. stock PullVehicleIntoDirection(vehicleid, Float:x, Float:y, Float:z, Float:speed)//Thanks to Miguel for supplying me with this function, I have edited it a bit
  306. {
  307. new
  308. Float:distance,
  309. Float:vehicle_pos[3];
  310.  
  311. GetVehiclePos(vehicleid, vehicle_pos[0], vehicle_pos[1], vehicle_pos[2]);
  312. #if defined USE_ANGULAR
  313.  
  314. #else
  315. SetVehicleZAngle(vehicleid, atan2VehicleZ(vehicle_pos[0], vehicle_pos[1], x, y));
  316. #endif
  317. x -= vehicle_pos[0];
  318. y -= vehicle_pos[1];
  319. z -= vehicle_pos[2];
  320. #if defined DEPRECATE_Z
  321. distance = floatsqroot((x * x) + (y * y));
  322. x = (speed * x) / distance;
  323. y = (speed * y) / distance;
  324. GetVehicleVelocity(vehicleid, vehicle_pos[0], vehicle_pos[0], z);
  325. #else
  326. z+=0.11;
  327. distance = floatsqroot((x * x) + (y * y) + (z * z));
  328. x = (speed * x) / distance;
  329. y = (speed * y) / distance;
  330. z = (speed * z) / distance;
  331. #endif
  332. #if defined USE_ANGULAR
  333. SetVehicleAngularVelocity(vehicleid, 0.0, 0.0, z);
  334. #endif
  335. SetVehicleVelocity(vehicleid, x, y, z);
  336. return 1;
  337. }
  338.  
  339. #if defined USE_ANGULAR
  340.  
  341. #else
  342. forward Float:atan2VehicleZ(Float:Xb,Float:Yb,Float:Xe,Float:Ye);// Dunno how to name it...
  343. stock Float:atan2VehicleZ(Float:Xb,Float:Yb,Float:Xe,Float:Ye)
  344. {
  345. new Float:a = floatabs(360.0 - atan2( Xe-Xb,Ye-Yb));
  346. if(360 > a > 180)return a;
  347. return a-360.0;
  348. }
  349. #endif
  350.  
  351. #if defined USE_SIDES
  352. stock GetXYRightOfPos(Float:a, &Float:x, &Float:y, Float:distance)
  353. {
  354. x += (distance * floatsin(-a-90.0, degrees));
  355. y += (distance * floatcos(-a-90.0, degrees));
  356. }
  357. #endif
  358. //----------------------------------------------------------------------------//
  359.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement