Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Homing Missile Script for GTA IV
- * Copyright (C) 2011 Morten T. Pedersen
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.*/
- **/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GTA;
- namespace LCPD
- {
- public class HomingMissiles : Script
- {
- public HomingMissiles()
- {
- Interval = 25;
- this.Tick += new EventHandler(HomingMissiles_Tick);
- if (Player.Character.Weapons.RocketLauncher.isPresent)
- _lastAmmo = Player.Character.Weapons.RocketLauncher.Ammo;
- BindConsoleCommand("createrocket", Command_CreateRocket);
- }
- private const int rocketHash = 1516578222;
- private const int fireHash = 1443084780;
- private void Command_CreateRocket(ParameterCollection parameters)
- {
- GTA.Object obj = World.CreateObject(new Model(rocketHash), Player.Character.GetOffsetPosition(new Vector3(0, 3, 0)));
- if (!Exists(obj))
- return;
- obj.Heading = Player.Character.Heading;
- GTA.Object fire = World.CreateObject(new Model(fireHash), obj.Position);
- }
- private int _lastAmmo = -1;
- private GTA.Object _rocket;
- private Vehicle _target;
- public int LastAmmo
- {
- get { return _lastAmmo; }
- set { _lastAmmo = value; }
- }
- public GTA.Object Rocket
- {
- get { return _rocket; }
- set { _rocket = value; }
- }
- public Vehicle Target
- {
- get { return _target; }
- private set { _target = value; }
- }
- void HomingMissiles_Tick(object sender, EventArgs e)
- {
- try
- {
- if (Target != null)
- {
- //We have a target. Destroy it!! >:D
- if (!Rocket.Exists() || !Target.Exists())
- {
- Rocket = null;
- Target = null;
- }
- Vector3 rot = Target.Position - ((GTA.Object)Rocket).Position;
- Rocket.Velocity = Vector3.Normalize(rot) * 100;
- }
- if (Player.Character.Weapons.CurrentType != Weapon.Heavy_RocketLauncher)
- return;
- if (Player.Character.Weapons.Current.Ammo < LastAmmo)
- {
- GTA.Object rocket = FindRocket();
- Vehicle target = FindTarget();
- if (Exists(rocket) && Exists(target))
- {
- Rocket = rocket;
- Target = target;
- }
- }
- LastAmmo = Player.Character.Weapons.Current.Ammo;
- }
- catch (Exception)
- {
- }
- }
- private Vehicle FindTarget()
- {
- return Helpers.Math.Choose(World.GetVehicles(Player.Character.Position, 10000));
- }
- private GTA.Object FindRocket()
- {
- List<GTA.Object> qualifiedObjects = new List<GTA.Object>();
- GTA.Object[] objects = World.GetAllObjects();
- foreach (GTA.Object obj in objects)
- {
- if (obj.Model.isVehicle)
- continue;
- if (obj.Position.DistanceTo2D(Player.Character.Position) > 25)
- continue;
- if (obj.isAttachedSomewhere)
- continue;
- qualifiedObjects.Add(obj);
- }
- foreach (GTA.Object obj in qualifiedObjects)
- {
- int hash = obj.Model.Hash;
- if (hash == rocketHash)
- return obj;
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement