Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GTA;
- using GTA.Native;
- using System;
- using System.IO;
- using System.Windows.Forms;
- using NativeUI;
- using GTA.Math;
- using System.Drawing;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- enum ChallengeStatus { NotStarted,Ready,InProgress}
- public class ScriptTest : Script
- {
- string ScriptName = "Downhill Challenge";
- string ScriptVer = "1.0";
- Vector3 StartingPoint = new Vector3(494.64f,5531.55f,777.53f);
- Vector3 EndPoint = new Vector3(-426.02f, 4886.73f,191.07f);
- int RestartReference = 0;
- float StartingPointHeading = 131.51f;
- ChallengeStatus ChallengeState = ChallengeStatus.NotStarted;
- Stopwatch Cronometer = new Stopwatch();
- XmlNodeList Leaderboard;
- public ScriptTest()
- {
- Tick += OnTick;
- _menuPool.Add(mainMenu);
- mainMenu.AddItem(UIStartChallenge);
- mainMenu.AddItem(UIEndtChallenge);
- mainMenu.AddItem(UIShowInfo);
- mainMenu.AddItem(UIHideMap);
- mainMenu.AddItem(UIForcePerfectConditions);
- foreach (UIMenu menu in _menuPool.ToList())
- {
- menu.RefreshIndex();
- menu.OnItemSelect += OnItemSelect;
- }
- UpdateLeaderboard();
- LoadSettings();
- }
- private MenuPool _menuPool = new MenuPool();
- private UIMenu mainMenu = new UIMenu("Downhill Challenge", "");
- private UIMenuItem UIStartChallenge = new UIMenuItem("Start Challenge");
- private UIMenuItem UIEndtChallenge = new UIMenuItem("End Challenge");
- private UIMenuCheckboxItem UIShowInfo = new UIMenuCheckboxItem("Display Vehicle Info", true, "");
- private UIMenuCheckboxItem UIForcePerfectConditions = new UIMenuCheckboxItem("Force perfect weather conditions", true, "");
- private UIMenuCheckboxItem UIHideMap = new UIMenuCheckboxItem("Hide map", true, "");
- Blip EndPosBlip;
- float speed =0;
- void UpdateLeaderboard()
- {
- if (File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
- {
- XmlDocument originalXml = new XmlDocument();
- originalXml.Load(@"scripts\\DownhillChallengeLeaderBoard.xml");
- Leaderboard = originalXml.SelectNodes("//Data/*");
- originalXml = null;
- }
- }
- public void SaveCar(string carname, string time, string maxspeed)
- {
- if (!File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
- {
- File.WriteAllText(@"scripts\\DownhillChallengeLeaderBoard.xml", "<Data></Data>");
- }
- XmlDocument originalXml = new XmlDocument();
- originalXml.Load(@"scripts\\DownhillChallengeLeaderBoard.xml");
- XmlNode changes = originalXml.SelectSingleNode("//Data");
- XmlElement ToRemove = null;
- bool CanAdd = true;
- bool NotifyTime = true;
- foreach (XmlElement node in originalXml.SelectNodes("//Data/*"))
- {
- if (node.GetAttribute("Name") == carname)
- {
- NotifyTime = false;
- TimeSpan oldtime = TimeSpan.Parse(node.GetAttribute("Time"));
- TimeSpan newtime = TimeSpan.Parse(time);
- if (newtime< oldtime)
- {
- ToRemove = node;
- UI.Notify("~g~New record!");
- UI.Notify("Old time: ~y~" + oldtime);
- UI.Notify("New time: ~g~" + newtime);
- break;
- }
- else
- {
- UI.Notify("Time: ~y~" + newtime);
- UI.Notify("Best time: ~g~" + oldtime);
- CanAdd = false;
- }
- }
- }
- if (ToRemove != null)
- {
- changes.RemoveChild(ToRemove);
- }
- if (NotifyTime) UI.Notify("New time: ~g~" + time);
- if (CanAdd)
- {
- XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "Car", null);
- XmlAttribute CarName = originalXml.CreateAttribute("Name");
- CarName.Value = carname;
- XmlAttribute Time = originalXml.CreateAttribute("Time");
- Time.Value = time;
- XmlAttribute Maxspeed = originalXml.CreateAttribute("MaxSpeed");
- Maxspeed.Value = maxspeed;
- Data.Attributes.Append(CarName);
- Data.Attributes.Append(Time);
- Data.Attributes.Append(Maxspeed);
- changes.AppendChild(Data);
- originalXml.Save(@"scripts\\DownhillChallengeLeaderBoard.xml");
- }
- }
- public static SizeF res = UIMenu.GetScreenResolutionMantainRatio();
- public static Point safe = UIMenu.GetSafezoneBounds();
- Keys MenuKey = Keys.Add;
- void DrawScoreboard()
- {
- int pluszone=0;
- int X = (Convert.ToInt32(res.Width) - safe.X) / 3;
- int Y = (Convert.ToInt32(res.Height) - safe.Y) / 7;
- new UIResRectangle(new Point(X, Y - 32), new Size(X , 30), Color.FromArgb(150, 255, 0, 0)).Draw();
- new UIResText("NAME - BEST TIME - MAX SPEED", new Point(X + 5, Y - 34), 0.5f,Color.White,GTA.Font.HouseScript,UIResText.Alignment.Left).Draw();
- if (File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
- {
- foreach (XmlElement node in Leaderboard)
- {
- new UIResRectangle(new Point(X, Y + pluszone), new Size(X, 32), Color.FromArgb(150, 0, 0, 0)).Draw();
- new UIResText(node.GetAttribute("Name") + " - " + node.GetAttribute("Time") + " - " + node.GetAttribute("MaxSpeed"), new Point(X + 5, Y + pluszone-1), 0.5f, Color.White, GTA.Font.HouseScript, UIResText.Alignment.Left).Draw();
- pluszone += 34;
- }
- }
- }
- void OnTick(object sender, EventArgs e)
- {
- if (Game.IsKeyPressed(Keys.Tab) && (Game.Player.Character.IsInRangeOf(EndPoint,200f) || Game.Player.Character.IsInRangeOf(StartingPoint,50f))) DrawScoreboard();
- _menuPool.ProcessMenus();
- if (Game.IsKeyPressed(MenuKey) && !_menuPool.IsAnyMenuOpen())
- {
- mainMenu.Visible = !mainMenu.Visible;
- }
- if (ChallengeState == ChallengeStatus.Ready || ChallengeState == ChallengeStatus.InProgress)
- {
- if (UIHideMap.Checked) Function.Call(Hash.HIDE_HUD_AND_RADAR_THIS_FRAME);
- World.DrawMarker(MarkerType.CheckeredFlagRect, (EndPoint + new Vector3(0f, 0f, 4f)), new Vector3(0f, 0f, 0f), new Vector3(0f, 0f, 0f), new Vector3(3f,3f,3f), Color.White, false, true, 0, false, "", "", false);
- Vehicle veh = Game.Player.Character.CurrentVehicle;
- if (CanWeUse(veh))
- {
- if (veh.IsOnAllWheels && Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR,veh,true).X >speed) speed = veh.Speed;
- string info = "Time: " + (Cronometer.Elapsed).ToString();
- if (UIShowInfo.Checked) DisplayHelpTextThisFrame(info);
- if (ChallengeState == ChallengeStatus.Ready)
- {
- if (Game.IsControlPressed(2, GTA.Control.VehicleAccelerate) && veh.Speed > 1f)
- {
- ChallengeState = ChallengeStatus.InProgress;
- Cronometer.Start();
- }
- }
- if (ChallengeState == ChallengeStatus.InProgress)
- {
- //Finish line
- if (Game.Player.Character.IsInRangeOf(EndPoint, 10f))
- {
- //UI.Notify("Car Integrity: ~y~" + Math.Abs((Game.Player.Character.CurrentVehicle.BodyHealth)));
- SaveCar(veh.FriendlyName, (Cronometer.Elapsed).ToString(),Math.Round(speed*1.6f,1).ToString()+" km/h");
- ChallengeState = ChallengeStatus.NotStarted;
- UpdateLeaderboard();
- if (EndPosBlip != null) EndPosBlip.Remove();
- }
- if (Game.IsControlPressed(2, GTA.Control.VehicleCinCam))
- {
- DisplayHelpTextThisFrame("~y~Restarting...");
- RestartReference++;
- }
- if (RestartReference > 50)
- {
- StartChallenge();
- DisplayHelpTextThisFrame("~y~Challenge restarted.");
- }
- }
- }
- else
- {
- EndChallenge();
- }
- }
- }
- void EndChallenge()
- {
- Cronometer.Reset();
- DisplayHelpTextThisFrame("~r~Challenge ended.");
- ChallengeState = ChallengeStatus.NotStarted;
- if(EndPosBlip != null) EndPosBlip.Remove();
- UpdateLeaderboard();
- }
- void StartChallenge()
- {
- Cronometer.Reset();
- speed = 0;
- Vehicle veh = Game.Player.Character.CurrentVehicle;
- if (CanWeUse(veh))
- {
- RestartReference = 0;
- veh.Position = StartingPoint;
- veh.Heading = StartingPointHeading;
- Function.Call(Hash.SET_VEHICLE_FIXED, veh);
- Function.Call(Hash.SET_VEHICLE_DEFORMATION_FIXED, veh);
- ChallengeState = ChallengeStatus.Ready;
- if (EndPosBlip != null) EndPosBlip.Remove();
- EndPosBlip = World.CreateBlip(EndPoint);
- EndPosBlip.Color = BlipColor.Yellow;
- EndPosBlip.Sprite = BlipSprite.RaceFinish;
- EndPosBlip.Name = "Finish Line";
- if (UIForcePerfectConditions.Checked)
- {
- World.Weather = Weather.ExtraSunny;
- Function.Call(Hash.SET_CLOCK_TIME, 12,0,0);
- }
- UpdateLeaderboard();
- }
- }
- public void OnItemSelect(UIMenu sender, UIMenuItem selectedItem, int index)
- {
- if(selectedItem == UIStartChallenge)
- {
- if (CanWeUse(Game.Player.Character.CurrentVehicle))
- {
- StartChallenge();
- _menuPool.CloseAllMenus();
- }
- else
- {
- UI.ShowSubtitle("~r~You are not in a vehicle.");
- }
- }
- if(selectedItem == UIEndtChallenge)
- {
- EndChallenge();
- }
- }
- protected override void Dispose(bool dispose)
- {
- if (EndPosBlip != null) EndPosBlip.Remove();
- base.Dispose(dispose);
- }
- /// TOOLS ///
- void LoadSettings()
- {
- if (File.Exists(@"scripts\\DownhillChallengeConfig.ini"))
- {
- ScriptSettings config = ScriptSettings.Load(@"scripts\DownhillChallengeConfig.ini");
- string key = config.GetValue<string>("SETTINGS", "MenuKey", "ERROR");
- bool CouldParse = Enum.TryParse(key, out MenuKey);
- if (!CouldParse)
- {
- WarnPlayer(ScriptName + " " + ScriptVer, "INVALID MENU KEY", "~y~" + ScriptName + " configuration file 'MenuKey' does not exist.");
- UI.Notify("Edit the file again and make sure that capital letters match (Add instead of add).");
- MenuKey = Keys.Add;
- }
- }
- else
- {
- WarnPlayer(ScriptName + " " + ScriptVer, "CONFIG FILE NOT FOUND", "~r~"+ ScriptName + " config not found. Configuration reset to Default.");
- }
- }
- void WarnPlayer(string script_name, string title, string message)
- {
- Function.Call(Hash._SET_NOTIFICATION_TEXT_ENTRY, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, message);
- Function.Call(Hash._SET_NOTIFICATION_MESSAGE, "CHAR_SOCIAL_CLUB", "CHAR_SOCIAL_CLUB", true, 0, title, "~b~" + script_name);
- }
- bool CanWeUse(Entity entity)
- {
- return entity != null && entity.Exists();
- }
- void DisplayHelpTextThisFrame(string text)
- {
- Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
- Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
- Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, false, false, -1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement