Advertisement
MulleDK19

GTA IV - Homing Missile Script

Jun 17th, 2011
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. /**
  2.  * Homing Missile Script for GTA IV
  3.  * Copyright (C) 2011 Morten T. Pedersen
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
  17. **/
  18.  
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23.  
  24. using GTA;
  25.  
  26. namespace LCPD
  27. {
  28.     public class HomingMissiles : Script
  29.     {
  30.         public HomingMissiles()
  31.         {
  32.             Interval = 25;
  33.  
  34.             this.Tick += new EventHandler(HomingMissiles_Tick);
  35.             if (Player.Character.Weapons.RocketLauncher.isPresent)
  36.                 _lastAmmo = Player.Character.Weapons.RocketLauncher.Ammo;
  37.             BindConsoleCommand("createrocket", Command_CreateRocket);
  38.         }
  39.  
  40.         private const int rocketHash = 1516578222;
  41.         private const int fireHash = 1443084780;
  42.  
  43.         private void Command_CreateRocket(ParameterCollection parameters)
  44.         {
  45.             GTA.Object obj = World.CreateObject(new Model(rocketHash), Player.Character.GetOffsetPosition(new Vector3(0, 3, 0)));
  46.             if (!Exists(obj))
  47.                 return;
  48.             obj.Heading = Player.Character.Heading;
  49.  
  50.             GTA.Object fire = World.CreateObject(new Model(fireHash), obj.Position);
  51.         }
  52.  
  53.         private int _lastAmmo = -1;
  54.         private GTA.Object _rocket;
  55.         private Vehicle _target;
  56.  
  57.         public int LastAmmo
  58.         {
  59.             get { return _lastAmmo; }
  60.             set { _lastAmmo = value; }
  61.         }
  62.         public GTA.Object Rocket
  63.         {
  64.             get { return _rocket; }
  65.             set { _rocket = value; }
  66.         }
  67.         public Vehicle Target
  68.         {
  69.             get { return _target; }
  70.             private set { _target = value; }
  71.         }
  72.  
  73.         void HomingMissiles_Tick(object sender, EventArgs e)
  74.         {
  75.             try
  76.             {
  77.                 if (Target != null)
  78.                 {
  79.                     //We have a target. Destroy it!! >:D
  80.                     if (!Rocket.Exists() || !Target.Exists())
  81.                     {
  82.                         Rocket = null;
  83.                         Target = null;
  84.                     }
  85.                     Vector3 rot = Target.Position - ((GTA.Object)Rocket).Position;
  86.                     Rocket.Velocity = Vector3.Normalize(rot) * 100;
  87.                 }
  88.  
  89.                 if (Player.Character.Weapons.CurrentType != Weapon.Heavy_RocketLauncher)
  90.                     return;
  91.  
  92.                 if (Player.Character.Weapons.Current.Ammo < LastAmmo)
  93.                 {
  94.                     GTA.Object rocket = FindRocket();
  95.                     Vehicle target = FindTarget();
  96.                     if (Exists(rocket) && Exists(target))
  97.                     {
  98.                         Rocket = rocket;
  99.                         Target = target;
  100.                     }
  101.                 }
  102.                 LastAmmo = Player.Character.Weapons.Current.Ammo;
  103.             }
  104.             catch (Exception)
  105.             {
  106.             }
  107.         }
  108.  
  109.         private Vehicle FindTarget()
  110.         {
  111.             return Helpers.Math.Choose(World.GetVehicles(Player.Character.Position, 10000));
  112.         }
  113.  
  114.         private GTA.Object FindRocket()
  115.         {
  116.             List<GTA.Object> qualifiedObjects = new List<GTA.Object>();
  117.             GTA.Object[] objects = World.GetAllObjects();
  118.             foreach (GTA.Object obj in objects)
  119.             {
  120.                 if (obj.Model.isVehicle)
  121.                     continue;
  122.                 if (obj.Position.DistanceTo2D(Player.Character.Position) > 25)
  123.                     continue;
  124.                 if (obj.isAttachedSomewhere)
  125.                     continue;
  126.                 qualifiedObjects.Add(obj);
  127.             }
  128.  
  129.             foreach (GTA.Object obj in qualifiedObjects)
  130.             {
  131.                 int hash = obj.Model.Hash;
  132.                 if (hash == rocketHash)
  133.                     return obj;
  134.             }
  135.  
  136.             return null;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement