Advertisement
Guest User

Untitled

a guest
Sep 5th, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.51 KB | None | 0 0
  1. /*
  2.  **************************************************
  3.  * @Script: Surface To Air Missiles (SAMs)        *
  4.  * @Author: Grim_                                 *
  5.  * @License: N/A                                  *
  6.  **************************************************
  7.  ** If you use my script, please give me credits **
  8.  **************************************************
  9.    
  10.     native CreateSAM(bool:create_object, Float:x, Float:y, Float:z, Float:range, Float:damage, Float:speed, delay, teamID=NO_TEAM);
  11.     native DestroySAM(samid);
  12.     native MoveSAM(samid, Float:newx, Float:newy, Float:newz);
  13.     native CreatedSAMs();
  14.     native IsValidSAM(samid);
  15.     native GetSAMRange(samid);
  16.     native GetSAMDamage(samid);
  17.     native GetSAMSpeed(samid);
  18.     native GetSAMDelay(samid);
  19.     native GetSAMTeamID(samid);
  20.     native SetSAMRange(samid, Float:range);
  21.     native SetSAMDamage(samid, Float:damage);
  22.     native SetSAMSpeed(samid, Float:speed);
  23.     native SetSAMDelay(samid, delay);
  24.     native SetSAMTeamID(samid, teamID);
  25.     native FireSAM(samid, targetid, bool:force);
  26.    
  27.     public OnSAMFired(samid, targetid);
  28.     public OnSAMDie(samid, reason);
  29. */
  30.  
  31. #include <a_samp>
  32.  
  33. #define MAX_SAM_SITES           10
  34.  
  35. #define REASON_SAM_TARGET_HIT   1
  36. #define REASON_SAM_TARGET_DIED  2
  37. #define REASON_SAM_DESTROYED    3
  38. #define REASON_SAM_FORCE_FIRE   4
  39.  
  40. enum SamInfo
  41. {
  42.     Float:SamX,
  43.     Float:SamY,
  44.     Float:SamZ,
  45.     Float:SamRange,
  46.     Float:SamDamage,
  47.     Float:SamSpeed,
  48.     SamObject,
  49.     SamIsChasing,
  50.     SamDelay,
  51.     SamMissile,
  52.     bool:SamCreated,
  53.     SamTeamID,
  54.     SamNextTarget
  55. }
  56.  
  57. new SamData[MAX_SAM_SITES][SamInfo];
  58. new Sams;
  59.  
  60.  
  61. /*
  62. FUNCTION: CreateSAM
  63. PARAMATERS:
  64.     bool:create_object - whether or not to create the SAM object
  65.     Float:x - The X position of the SAM
  66.     Float:y - The Y position of the SAM
  67.     Float:z - The Z position of the SAM
  68.     Float:range - The range the SAM will fire at (If the vehicle is in that range)
  69.     damage - The amount of damage the SAM will inflict on the vehicle it hits
  70.     speed - How quickly the SAM missile moves
  71.     delay - The amount of time the SAM must wait to fire another missile
  72.    
  73. USE: Creates a SAM missile at the desired location with the specified stats
  74. RETURNS: The ID of the SAM created (If none, returns -1)
  75. */
  76. stock CreateSAM(bool:create_object, Float:x, Float:y, Float:z, Float:range, Float:damage, Float:speed, delay, teamID=NO_TEAM)
  77. {
  78.     for(new i = 0; i < MAX_SAM_SITES; i++)
  79.     {
  80.         if(SamData[i][SamCreated] == true) continue;
  81.         if(create_object == true)
  82.         {
  83.             SamData[i][SamObject] = CreateObject(3884, x, y, z, 0.00, 0.00, 0.00);
  84.         }
  85.         SamData[i][SamX] = x;
  86.         SamData[i][SamY] = y;
  87.         SamData[i][SamZ] = z;
  88.         SamData[i][SamRange] = range;
  89.         SamData[i][SamDamage] = damage;
  90.         SamData[i][SamSpeed] = speed;
  91.         SamData[i][SamDelay] = delay*1000;
  92.         SamData[i][SamCreated] = true;
  93.         SamData[i][SamTeamID] = teamID;
  94.         Sams++;
  95.         return i;
  96.     }
  97.     return -1;
  98. }
  99.  
  100. /*
  101. FUNCTION: DestroySAM
  102. PARAMATERS:
  103.     samid - The ID of the SAM to destroy
  104.  
  105. USE: Destroys the desired SAM
  106. RETURNS: 1 if successful, 0 if not
  107. */
  108. stock DestroySAM(samid)
  109. {
  110.     if(!IsValidSAM(samid)) return 0;
  111.     DestroyObject(SamData[samid][SamObject]);
  112.     SamData[samid][SamX] = 0.00;
  113.     SamData[samid][SamY] = 0.00;
  114.     SamData[samid][SamZ] = 0.00;
  115.     SamData[samid][SamRange] = 0.00;
  116.     SamData[samid][SamDamage] = 0.00;
  117.     SamData[samid][SamSpeed] = 0.00;
  118.     SamData[samid][SamDelay] = 0;
  119.     SamData[samid][SamCreated] = false;
  120.     if(SamData[samid][SamMissile] != -1)
  121.     {
  122.         new Float:x, Float:y, Float:z;
  123.         GetObjectPos(SamData[samid][SamMissile], x, y, z);
  124.         CreateExplosion(x, y, z, 1, 8);
  125.         DestroyObject(SamData[samid][SamMissile]);
  126.         OnSAMDie(samid, REASON_SAM_DESTROYED);
  127.     }
  128.     SamData[samid][SamMissile] = -1;
  129.     SamData[samid][SamObject] = -1;
  130.     SamData[samid][SamIsChasing] = -1;
  131.     SamData[samid][SamTeamID] = NO_TEAM;
  132.     SamData[samid][SamNextTarget] = -1;
  133.     Sams--;
  134.     return 1;
  135. }
  136.  
  137. /*
  138. FUNCTION: MoveSAM
  139. PARAMATERS:
  140.     samid - The ID of the SAM you want to move
  141.     Float:newx - The new X position of the SAM
  142.     Float:newy - The new Y position of the SAM
  143.     Float:newz - The new Z position of the SAM
  144.    
  145. USE: Moves the desired SAM to the new specified location
  146. RETURNS: 1 if successful, 0 if not
  147. */
  148. stock MoveSAM(samid, Float:newx, Float:newy, Float:newz)
  149. {
  150.     if(!IsValidSAM(samid)) return 0;
  151.     SamData[samid][SamX] = newx;
  152.     SamData[samid][SamY] = newy;
  153.     SamData[samid][SamZ] = newz;
  154.     return 1;
  155. }
  156.  
  157. /*
  158. FUNCTION: GetSAMRange
  159. PARAMATERS:
  160.     samid - The ID of the SAM to get the range from
  161.  
  162. USE: Returns the Range of the specified SAM
  163. RETURNS: Range of the specified SAM, 0 if failure
  164. */
  165. stock Float:GetSAMRange(samid)
  166. {
  167.     if(!IsValidSAM(samid)) return 0.0;
  168.     return SamData[samid][SamRange];
  169. }
  170.  
  171. /*
  172. FUNCTION: SetSAMRange
  173. PARAMATERS:
  174.     samid - The ID of the SAM to change the range of
  175.     Float:range - The new range of the SAM
  176.    
  177. USE: Sets the range of the specified SAM
  178. RETURNS: 1 if successful, 0 if not
  179. */
  180. stock SetSAMRange(samid, Float:range)
  181. {
  182.     if(!IsValidSAM(samid)) return 0;
  183.     SamData[samid][SamRange] = range;
  184.     return 1;
  185. }
  186.  
  187. /*
  188. FUNCTION: GetSAMDamage
  189. PARAMATERS:
  190.     samid - The ID of the SAM to get the damage count from
  191.    
  192. USE: Returns the damage of the specified SAM
  193. RETURNS: Damage Count for the specified SAM, 0 if failure
  194. */
  195. stock Float:GetSAMDamage(samid)
  196. {
  197.     if(!IsValidSAM(samid)) return 0.0;
  198.     return SamData[samid][SamDamage];
  199. }
  200.  
  201. /*
  202. FUNCTION: SetSAMDamage
  203. PARAMATERS:
  204.     samid - The ID of the SAM to set the damage count of
  205.     Float:damage - The new damage of the SAM
  206.    
  207. USE: Sets the damage of the specified SAM
  208. RETURNS: 1 if successful, 0 if not
  209. */
  210. stock SetSAMDamage(samid, Float:damage)
  211. {
  212.     if(!IsValidSAM(samid)) return 0;
  213.     SamData[samid][SamDamage] = damage;
  214.     return 1;
  215. }
  216.  
  217. /*
  218. FUNCTION: GetSAMSpeed
  219. PARAMATERS:
  220.     samid - The ID of the SAM to get the speed from
  221.    
  222. USE: Returns the speed of the specified SAM
  223. RETURNS: Speed of the specified SAM, 0 if failure
  224. */
  225. stock Float:GetSAMSpeed(samid)
  226. {
  227.     if(!IsValidSAM(samid)) return 0.0;
  228.     return SamData[samid][SamSpeed];
  229. }
  230.  
  231. /*
  232. FUNCTION: SetSAMSpeed
  233. PARAMATERS:
  234.     samid - The ID of the SAM to set the speed of
  235.     Float:speed - The new speed of the specified SAM
  236.    
  237. USE: Sets the speed of the specified SAM
  238. RETURNS: 1 if successful, 0 if not
  239. */
  240. stock SetSAMSpeed(samid, Float:speed)
  241. {
  242.     if(!IsValidSAM(samid)) return 0;
  243.     SamData[samid][SamSpeed] = speed;
  244.     return 1;
  245. }
  246.  
  247. /*
  248. FUNCTION: GetSAMDelay
  249. PARAMATERS:
  250.     samid - The ID of the SAM to get the delay from
  251.    
  252. USE: Returns the delay of the specified SAM
  253. RETURNS: Delay of the specified SAM, 0 if failure
  254. */
  255. stock GetSAMDelay(samid)
  256. {
  257.     if(!IsValidSAM(samid)) return 0;
  258.     return SamData[samid][SamDelay];
  259. }
  260.  
  261. /*
  262. FUNCTION: SetSAMDelay
  263. PARAMATERS:
  264.     samid - The ID of the SAM to set the delay of
  265.     delay - The new delay of the specified SAM
  266.    
  267. USE: Sets the delay of the specified SAM
  268. RETURNS: 1 if successful, 0 if not
  269. */
  270. stock SetSAMDelay(samid, delay)
  271. {
  272.     if(!IsValidSAM(samid)) return 0;
  273.     SamData[samid][SamDelay] = delay;
  274.     return 1;
  275. }
  276.  
  277. /*
  278. FUNCTION: GetSAMTeamID
  279. PARAMATERS:
  280.     samid - The ID of the SAM to get the team ID from
  281.  
  282. USE: Returns the team ID of the specified SAM
  283. RETURNS: The team ID of the specified SAM, 0 if failure
  284. */
  285. stock GetSAMTeamID(samid)
  286. {
  287.     if(!IsValidSAM(samid)) return 0;
  288.     return SamData[samid][SamTeamID];
  289. }
  290.  
  291. /*
  292. FUNCTION: SetSAMTeamID
  293. PARAMATERS:
  294.     samid - The ID of the SAM to set the team ID of
  295.     teamID - The new team ID of the specified SAM
  296.    
  297. USE: Sets the team ID of the specified SAM
  298. RETURNS: 1 if successful, 0 if not
  299. */
  300. stock SetSAMTeamID(samid, teamID)
  301. {
  302.     if(!IsValidSAM(samid)) return 0;
  303.     SamData[samid][SamTeamID] = teamID;
  304.     return 1;
  305. }
  306.  
  307. /*
  308. FUNCTION: CreatedSAMs
  309. PARAMATERS: N/A
  310. USE: Returns the number of SAMS in the server
  311. RETURNS: Number of SAMS in the server
  312. */
  313. stock CreatedSAMs() return Sams;
  314.  
  315. /*
  316. FUNCTION: IsValidSAM
  317. PARAMATERS:
  318.     samid - The SAM you want to check is valid
  319.    
  320. USE: Checks whether or not the specified SAM is valid (created)
  321. RETURNS: 1 if the SAM is created, 0 if not
  322. */
  323. stock IsValidSAM(samid)
  324. {
  325.     if(SamData[samid][SamCreated] == true) return 1;
  326.     return 0;
  327. }
  328.  
  329. /*
  330. FUNCTION: FireSAM
  331. PARAMATERS:
  332.     samid - The SAM you want to fire
  333.     targetid - The vehicle ID that you want to fire at
  334.    
  335. USE: Fires a missile directly at targetid
  336. RETURNS: 1 if successful, 0 if not
  337. */
  338. stock FireSAM(samid, targetid, bool:force)
  339. {
  340.     new Float:x, Float:y, Float:z;
  341.     GetVehiclePos(targetid, x, y, z);
  342.     if(x == 0.00 && y == 0.00 && z == 0.00) return 0;
  343.     if(SamData[samid][SamMissile] != -1)
  344.     {
  345.         if(force == false)
  346.         {
  347.             SamData[samid][SamNextTarget] = targetid;
  348.             return 1;
  349.         }
  350.         GetObjectPos(SamData[samid][SamMissile], x, y, z);
  351.         CreateExplosion(x, y, z, 1, 8.0);
  352.         DestroyObject(SamData[samid][SamMissile]);
  353.         SamData[samid][SamIsChasing] = -1;
  354.         OnSAMDie(samid, REASON_SAM_FORCE_FIRE);
  355.     }
  356.     OnSAMFired(samid, targetid);
  357.     SamData[samid][SamMissile] = CreateObject(354, SamData[samid][SamX], SamData[samid][SamY], SamData[samid][SamZ], 0.00, 0.00, 0.00);
  358.     SamData[samid][SamIsChasing] = targetid;
  359.     UpdateSam(targetid, samid);
  360.     return 1;
  361. }
  362.  
  363. /*
  364. CALLBACK: OnSAMFired
  365. ARGUMENTS:
  366.     samid - The SAM that was fired
  367.     targetid - The vehicleid the SAM's missile is chasing
  368.  
  369. CALLED: When A SAM's missile is fired
  370. */
  371. forward OnSAMFired(samid, targetid);
  372. //public OnSAMFired(samid, targetid) return 1;
  373.  
  374. /*
  375. CALLBACK: OnSAMDie
  376. ARGUMENTS:
  377.     samid - The SAM's missile that died
  378.     reason - The reason the missile died
  379.         REASON_SAM_TARGET_HIT - The vehicle the missile was chasing was hit
  380.         REASON_SAM_TARGET_DIED - The vehicle the missile was chasing died
  381.         REASON_SAM_DESTROYED - The SAM site was destroyed
  382.         REASON_SAM_FORCE_FIRE - The SAM site was forced to fire, killing the missile (FireSAM)
  383.        
  384. CALLED: When a SAM's missile dies
  385. */
  386. forward OnSAMDie(samid, reason);
  387. //public OnSAMDie(samid, reason) return 1;
  388.  
  389.  
  390. // ---------------------------------------------------------------------------------------------------
  391. // ---------------------------------------------------------------------------------------------------
  392. // ---------------------------------------------------------------------------------------------------
  393. forward OnSamInit();
  394. public OnSamInit()
  395. {
  396.     SetTimer("CheckSams", 1000, true);
  397.     for(new s = 0; s < MAX_SAM_SITES; s++)
  398.     {
  399.         SamData[s][SamIsChasing] = -1;
  400.         SamData[s][SamObject] = -1;
  401.         SamData[s][SamMissile] = -1;
  402.         SamData[s][SamTeamID] = -1;
  403.         SamData[s][SamNextTarget] = -1;
  404.     }
  405. }
  406.  
  407. forward OnVehicleDeathX(vehicleid, killerid);
  408. public OnVehicleDeathX(vehicleid, killerid)
  409. {
  410.     for(new s = 0; s < MAX_SAM_SITES; s++)
  411.     {
  412.         if(SamData[s][SamIsChasing] == vehicleid)
  413.         {
  414.             new Float:x, Float:y, Float:z;
  415.             GetObjectPos(SamData[s][SamMissile], x, y, z);
  416.             CreateExplosion(x, y, z, 1, 8);
  417.             DestroyObject(SamData[s][SamMissile]);
  418.             OnSAMDie(s, REASON_SAM_TARGET_DIED);
  419.             SetTimerEx("ClearSam", SamData[s][SamDelay], false, "i", s);
  420.         }
  421.     }
  422.     return 1;
  423. }
  424.  
  425. forward CheckSams();
  426. public CheckSams()
  427. {
  428.     for(new i = 0; i < GetMaxPlayers(); i++)
  429.     {
  430.         if(!IsPlayerConnected(i)) continue;
  431.         if(IsPlayerVehicleAirVehicle(i))
  432.         {
  433.             for(new s = 0; s < MAX_SAM_SITES; s++)
  434.             {
  435.                 if(!IsValidSAM(s)) continue;
  436.                 if(SamData[s][SamMissile] != -1) continue;
  437.                 if(GetPlayerTeam(i) == SamData[s][SamTeamID]) continue;
  438.                 if(IsPlayerInRangeOfPoint(i, SamData[s][SamRange], SamData[s][SamX], SamData[s][SamY], SamData[s][SamZ]))
  439.                 {
  440.                     OnSAMFired(s, GetPlayerVehicleID(i));
  441.                     SamData[s][SamMissile] = CreateObject(354, SamData[s][SamX], SamData[s][SamY], SamData[s][SamZ], 0.00, 0.00, 0.00);
  442.                     SamData[s][SamIsChasing] = GetPlayerVehicleID(i);
  443.                     UpdateSam(GetPlayerVehicleID(i), s);
  444.                 }
  445.             }
  446.         }
  447.     }
  448.     return 1;
  449. }
  450.  
  451. forward UpdateSam(chasingid, samid);
  452. public UpdateSam(chasingid, samid)
  453. {
  454.     new Float:x, Float:y, Float:z;
  455.     GetVehiclePos(chasingid, x, y, z);
  456.     if(IsObjectInRangeOfPoint(SamData[samid][SamMissile], 5, x, y, z))
  457.     {
  458.         new Float:vhealth;
  459.         CreateExplosion(x, y, z, 1, 8.0);
  460.         SetVehicleHealth(chasingid, GetVehicleHealth(chasingid, vhealth)-SamData[samid][SamDamage]);
  461.         DestroyObject(SamData[samid][SamMissile]);
  462.         SamData[samid][SamIsChasing] = -1;
  463.         OnSAMDie(samid, REASON_SAM_TARGET_HIT);
  464.         SetTimerEx("ClearSam", SamData[samid][SamDelay], false, "i", samid);
  465.         return 1;
  466.     }
  467.     MoveObject(SamData[samid][SamMissile], x, y, z, SamData[samid][SamSpeed]);
  468.     SetTimerEx("UpdateSam", 100, false, "ii", chasingid, samid);
  469.     return 1;
  470. }
  471.  
  472. forward ClearSam(samid);
  473. public ClearSam(samid)
  474. {
  475.     SamData[samid][SamMissile] = -1;
  476.     if(SamData[samid][SamNextTarget] != -1)
  477.     {
  478.         FireSAM(samid, SamData[samid][SamNextTarget], true);
  479.         SamData[samid][SamNextTarget] = -1;
  480.     }
  481.     return 1;
  482. }
  483.  
  484. IsPlayerVehicleAirVehicle(playerid)
  485. {
  486.     new AirVeh[] = { 592, 577, 511, 512, 593, 520, 553, 476, 519, 460, 513, 548, 425, 417, 487, 488, 497, 563, 447, 469 };
  487.     for(new i = 0; i < sizeof(AirVeh); i++)
  488.     {
  489.         if(GetVehicleModel(GetPlayerVehicleID(playerid)) == AirVeh[i])
  490.         {
  491.             return 1;
  492.         }
  493.     }
  494.     return 0;
  495. }
  496.  
  497. IsObjectInRangeOfPoint(objectid, Float:range, Float:x, Float:y, Float:z)
  498. {
  499.     new Float:oldpos[3];
  500.     new Float:temppos[3];
  501.     GetObjectPos(objectid, oldpos[0], oldpos[1], oldpos[2]);
  502.     temppos[0] = (oldpos[0] -x);
  503.     temppos[1] = (oldpos[1] -y);
  504.     temppos[2] = (oldpos[2] -z);
  505.     if(((temppos[0] < range) && (temppos[0] > -range)) && ((temppos[1] < range) && (temppos[1] > -range)) && ((temppos[2] < range) && (temppos[2] > -range)))
  506.     {
  507.         return true;
  508.     }
  509.     return false;
  510. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement