Quantcast

Jump to content

» «
Photo

[REL] Teleporter Mod v1.0

1 reply to this topic
Axeman420
  • Axeman420

    Square Civilian

  • Members
  • Joined: 31 Mar 2007

#1

Posted 13 May 2012 - 02:35 AM Edited by Axeman420, 15 May 2012 - 05:59 PM.

This is a simple teleporting mod I've been working on.

It allows you to make as many custom named teleport locations as you want. (In theory, my code works, however, I'm not sure the game won't crash if you make too many, etc,.)

Let me know what you think.

I'm offering it in pure code form, so you can easily edit it, if you know what you're doing, and so that ppl can see how it works, and see that's it doesn't have any malicious code, etc,.

I may properly package it up, if there is any interest in it, if I for some reason don't, you guys have permission to host it, pack it, compile it, bug fix it, etc, just credit me for my work.

Requirements

GTAIV .Net Script Hook v1.7.1.7 BETA

The .Net Script Hook has it's own requirements, be sure to read them, and get anything you need. (See link.)

Usage

Travel to some location that you want to mark, press F1, to open the menu, you'll see the text "Enter Location Name", click that text, it should vanish, then type a name, and hit the "Store Location" button. (You can press the white X in the top right corner of the window to close the menu when you are done..)

Traveling to a location is easy, just open the menu(F1), and double click on the entry that matches the location you stored.

Deleting an entry is possible, open the menu, click on the location you want to delete, and hit the "Delete Location" button.

You can also set a waypoint on your map, and hit the "Teleport: Waypoint" button to fast travel there.

Notes:

All locations are stored in an .xml file that the mod will create for you, if all locations are deleted, the mod should remove the .xml file automatically. (Recreating it as needed..)

Locations will be sorted alphabetically whenever the list get's updated\loaded, etc,.

Installation

Copy the code below, into a file called "Teleporter.cs".

Place that file into your GTAIV directory, in a subfolder called "scripts". (This is where the .exe for the game is located, etc,. You may need to create the "scripts" subfolder.)

Known Issues

It's not 100% accurate at marking locations, so, you fall about 5 feet, and occasionally, through the floors, etc, when teleporting.

-------------------------

CODE

using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using GTA;

#region "Class: TeleportLocation"

/// <summary>
/// A class representing the construct of a teleport location.
/// </summary>
public class TeleportLocation
{
   // A user defined name for this teleport location.
   public string Name = String.Empty;

   // A vector3 representing the player characters current position.
   public Vector3 Position = Vector3.Zero;

   // Constructor
   public TeleportLocation() { }

   // Constructor
   public TeleportLocation(string name, Vector3 position)
   {
       this.Name = name;
       this.Position = position;
   }
}

#endregion

#region "Class: TeleportDialog"

/// <summary>
/// A dialog menu used for teleporting the player character.
/// </summary>
public class TeleportDialog : GTA.Forms.Form
{
   TeleporterInfo Info;
   GTA.Forms.Listbox listBox1;
   GTA.Forms.Textbox textBox1;
   StateObject<int> DoubleClicked;

   // Constructor
   public TeleportDialog(TeleporterInfo info)
   {
       // Information about the teleporter.
       this.Info = info;

       // Control Layout Padding
       int Padding = 8;

       // Main Form
       this.Transparency = 65;
       this.Text = "Teleporter";

       // ListBox - Display stored locations
       listBox1 = new GTA.Forms.Listbox();
       listBox1.Location = new Point(20, 20);
       listBox1.Size = new Size(this.Width - 40, 256);
       listBox1.Border = true;
       listBox1.Click += new MouseEventHandler(listBox1_Click);
       listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
       this.Controls.Add(listBox1);

       // TextBox - User defined name
       textBox1 = new GTA.Forms.Textbox();
       textBox1.Size = new System.Drawing.Size(256, 32);
       textBox1.Location = new Point(this.Width / 2 - textBox1.Size.Width / 2, listBox1.Location.Y + listBox1.Size.Height + Padding);
       textBox1.Text = "Enter Location Name";
       textBox1.Click += new MouseEventHandler(textBox1_Click);
       this.Controls.Add(textBox1);

       // Button - Store a location
       GTA.Forms.Button button1 = new GTA.Forms.Button();
       button1.Text = "Store Location";
       button1.Size = new System.Drawing.Size(110, 32);
       button1.Location = new Point(this.Width / 2 - button1.Size.Width / 2, textBox1.Location.Y + textBox1.Size.Height + Padding);
       button1.Click += new MouseEventHandler(button1_Click);
       this.Controls.Add(button1);

       // Button - Delete a stored location
       GTA.Forms.Button button2 = new GTA.Forms.Button();
       button2.Text = "Delete Location";
       button2.Size = new System.Drawing.Size(110, 32);
       button2.Location = new Point(this.Width / 2 - button2.Size.Width / 2, button1.Location.Y + button1.Size.Height + Padding);
       button2.Click += new MouseEventHandler(button2_Click);
       this.Controls.Add(button2);

       // Button - Teleport to user defined waypoint (Defined via the map screen.)
       GTA.Forms.Button button3 = new GTA.Forms.Button();
       button3.Text = "Teleport: Waypoint";
       button3.Size = new System.Drawing.Size(130, 32);
       button3.Location = new Point(this.Width / 2 - button3.Size.Width / 2, button2.Location.Y + button2.Size.Height + Padding);
       button3.Click += new MouseEventHandler(button3_Click);
       this.Controls.Add(button3);

       // Double Click
       DoubleClicked = new StateObject<int>(listBox1.SelectedIndex);

       // Syncs the ListBox with the stored location container
       if (Info.Teleporter.Locations.Count != 0)
           UpdateListBox();

       // Form Closed Event - Fires when the form closes
       this.Closed += new EventHandler(TeleportDialog_Closed);
   }

   void listBox1_KeyDown(object sender, KeyEventArgs e)
   {
       if (e.Key == System.Windows.Forms.Keys.Down)
       {
           if (listBox1.SelectedIndex < listBox1.Items.Count - 1)
           {
               listBox1.SelectedIndex++;
           }
       }
       else if (e.Key == System.Windows.Forms.Keys.Up)
       {
           if (listBox1.SelectedIndex >= 1)
               listBox1.SelectedIndex--;
       }

       if (e.Key == System.Windows.Forms.Keys.Enter)
       {
           if (listBox1.SelectedIndex != -1)
           {
               TeleportLocation location = Info.Teleporter.Locations[listBox1.SelectedIndex];
               Info.Player.TeleportTo(location.Position);
           }
       }
   }

   // Enter Textbox - Clears existing text
   void textBox1_Click(object sender, MouseEventArgs e)
   {
       GTA.Forms.Textbox tb = sender as GTA.Forms.Textbox;
       tb.Text = String.Empty;
   }

   // Select Location
   void listBox1_Click(object sender, MouseEventArgs e)
   {
       if (listBox1.SelectedIndex != -1)
       {
           DoubleClicked.Update(listBox1.SelectedIndex);

           if (DoubleClicked.Current == DoubleClicked.Previous)
           {
               TeleportLocation location = Info.Teleporter.Locations[listBox1.SelectedIndex];
               Info.Player.TeleportTo(location.Position);
               DoubleClicked.Update(-1);
           }
       }
   }

   // Store Location
   void button1_Click(object sender, MouseEventArgs e)
   {
       // Error: No name specified error.
       if (String.IsNullOrEmpty(textBox1.Text))
       {
           Game.DisplayText("ERROR: You must name the teleport location!");
           return;
       }

       // Add current position to the stored locations container.
       Info.Teleporter.Locations.Add(new TeleportLocation(textBox1.Text, Info.Player.Character.Position));

       // Update ListBox
       UpdateListBox();
   }

   // Delete Location
   void button2_Click(object sender, MouseEventArgs e)
   {
       if (listBox1.SelectedIndex != -1)
       {
           Info.Teleporter.Locations.RemoveAt(listBox1.SelectedIndex);
           UpdateListBox();
       }
   }

   // Fast Travel: Waypoint
   void button3_Click(object sender, MouseEventArgs e)
   {
       Blip b = Game.GetWaypoint();

       if (b != null)
       {
           Info.Player.TeleportTo(b.Position);
           this.Close();
       }
   }

   // Refresh ListBox Contents
   void UpdateListBox()
   {
       // Clear the ListBox.
       listBox1.Items.Clear();

       // Sort the stored locations. (Alphabetically)
       Info.Teleporter.Locations.Sort(delegate(TeleportLocation t1, TeleportLocation t2)
       {
           return t1.Name.CompareTo(t2.Name);
       });

       // Add the locations to the ListBox.
       foreach (TeleportLocation location in Info.Teleporter.Locations)
           listBox1.Items.Add(location, location.Name);
   }

   // Form Closed
   void TeleportDialog_Closed(object sender, EventArgs e)
   {
       Info.Teleporter.Serialize(); // Save stored locations to file.
   }
}

#endregion

#region "Class: Teleporter"

/// <summary>
/// The main script for this teleportation mod.
/// </summary>
public class Teleporter : Script
{
   // A container for storing user chosen locations.
   public List<TeleportLocation> Locations = new List<TeleportLocation>();

   // Constructor
   public Teleporter()
   {
       // KeyUp Event
       this.KeyUp += new KeyEventHandler(Teleporter_KeyUp);

       // Load stored locations from .xml
       Deserialize();
   }

   // KeyUp Event
   void Teleporter_KeyUp(object sender, KeyEventArgs e)
   {
       // F1
       if (e.Key == System.Windows.Forms.Keys.F1)
       {
           TeleportDialog td = new TeleportDialog(TeleporterInfo.Create(Player, this));
           td.Show();
       }
   }

   // Serialize
   public void Serialize()
   {
       if (Locations != null && Locations.Count != 0)
       {
           XmlSerializer xs = new XmlSerializer(typeof(List<TeleportLocation>));
           TextWriter textWriter = new StreamWriter("scripts/Teleporter.xml");
           xs.Serialize(textWriter, Locations);
           textWriter.Close();
       }
       else if (Locations.Count == 0)
       {
           File.Delete("scripts/Teleporter.xml");
       }
   }

   // Deserialize
   public void Deserialize()
   {
       if (!File.Exists("scripts/Teleporter.xml")) return;

       XmlSerializer deserializer = new XmlSerializer(typeof(List<TeleportLocation>));
       TextReader textReader = new StreamReader("scripts/Teleporter.xml");
       Locations = (List<TeleportLocation>)deserializer.Deserialize(textReader);
       textReader.Close();
   }
}

#endregion

#region "Class: TeleporterInfo"

/// <summary>
/// Teleporter info...
/// </summary>
public class TeleporterInfo
{
   // A reference to the player character.
   public Player Player;

   // A reference to the teleporter.
   public Teleporter Teleporter;

   // Constructor
   public TeleporterInfo(Player player, Teleporter teleporter)
   {
       this.Player = player;
       this.Teleporter = teleporter;
   }

   // Create
   public static TeleporterInfo Create(Player p, Teleporter t)
   {
       return new TeleporterInfo(p, t);
   }
}

#endregion

#region "Class: StateObject"

public class StateObject<T>
{
   public T Current;
   public T Previous;

   public StateObject(T state)
   {
       Current = state;
       Previous = Current;
   }

   public void Update(T state)
   {
       Previous = Current;
       Current = state;
   }
}

#endregion


Code Updates

- Fixed: "Unable to delete saved locations bug.".

- Added: Janky double click support.

- Added: Keyboard navigation - (Up\Down\Enter, when inside the ListBox.)

- Updated: Minor code cleanup.

- Added: Comments\Regions\Etc

paulicabos
  • paulicabos

    Player Hater

  • Members
  • Joined: 27 Jul 2012

#2

Posted 04 August 2012 - 11:45 AM

Can you give me the project source ? : D

I'm trying to make a program that teleports to some XYZ cords but i cant figure out how.. please help me




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users