Advertisement
Eddlm

Downhill Challenge

Mar 27th, 2016
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.27 KB | None | 0 0
  1. using GTA;
  2. using GTA.Native;
  3. using System;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. using NativeUI;
  7. using GTA.Math;
  8. using System.Drawing;
  9. using System.Xml;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13.  
  14. enum ChallengeStatus { NotStarted,Ready,InProgress}
  15. public class ScriptTest : Script
  16. {
  17.     string ScriptName = "Downhill Challenge";
  18.     string ScriptVer = "1.0";
  19.     Vector3 StartingPoint = new Vector3(494.64f,5531.55f,777.53f);
  20.     Vector3 EndPoint = new Vector3(-426.02f, 4886.73f,191.07f);
  21.     int RestartReference = 0;
  22.     float StartingPointHeading = 131.51f;
  23.     ChallengeStatus ChallengeState = ChallengeStatus.NotStarted;
  24.     Stopwatch Cronometer = new Stopwatch();
  25.     XmlNodeList Leaderboard;
  26.  
  27.     public ScriptTest()
  28.     {      
  29.         Tick += OnTick;
  30.         _menuPool.Add(mainMenu);
  31.         mainMenu.AddItem(UIStartChallenge);
  32.         mainMenu.AddItem(UIEndtChallenge);
  33.         mainMenu.AddItem(UIShowInfo);
  34.         mainMenu.AddItem(UIHideMap);
  35.  
  36.         mainMenu.AddItem(UIForcePerfectConditions);
  37.         foreach (UIMenu menu in _menuPool.ToList())
  38.         {
  39.             menu.RefreshIndex();
  40.             menu.OnItemSelect += OnItemSelect;
  41.  
  42.         }
  43.         UpdateLeaderboard();
  44.         LoadSettings();
  45.     }
  46.     private MenuPool _menuPool = new MenuPool();
  47.     private UIMenu mainMenu = new UIMenu("Downhill Challenge", "");
  48.     private UIMenuItem UIStartChallenge = new UIMenuItem("Start Challenge");
  49.     private UIMenuItem UIEndtChallenge = new UIMenuItem("End Challenge");
  50.     private UIMenuCheckboxItem UIShowInfo = new UIMenuCheckboxItem("Display Vehicle Info", true, "");
  51.     private UIMenuCheckboxItem UIForcePerfectConditions = new UIMenuCheckboxItem("Force perfect weather conditions", true, "");
  52.     private UIMenuCheckboxItem UIHideMap = new UIMenuCheckboxItem("Hide map", true, "");
  53.  
  54.     Blip EndPosBlip;
  55.     float speed =0;
  56.     void UpdateLeaderboard()
  57.     {
  58.         if (File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
  59.         {
  60.             XmlDocument originalXml = new XmlDocument();
  61.             originalXml.Load(@"scripts\\DownhillChallengeLeaderBoard.xml");
  62.             Leaderboard = originalXml.SelectNodes("//Data/*");
  63.             originalXml = null;
  64.         }
  65.  
  66.     }
  67.     public void SaveCar(string carname, string time, string maxspeed)
  68.     {
  69.  
  70.         if (!File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
  71.         {
  72.             File.WriteAllText(@"scripts\\DownhillChallengeLeaderBoard.xml", "<Data></Data>");
  73.         }
  74.  
  75.  
  76.         XmlDocument originalXml = new XmlDocument();
  77.         originalXml.Load(@"scripts\\DownhillChallengeLeaderBoard.xml");
  78.  
  79.         XmlNode changes = originalXml.SelectSingleNode("//Data");
  80.         XmlElement ToRemove = null;
  81.         bool CanAdd = true;
  82.         bool NotifyTime = true;
  83.         foreach (XmlElement node in originalXml.SelectNodes("//Data/*"))
  84.         {
  85.             if (node.GetAttribute("Name") == carname)
  86.                 {
  87.                     NotifyTime = false;
  88.                     TimeSpan oldtime = TimeSpan.Parse(node.GetAttribute("Time"));
  89.                     TimeSpan newtime = TimeSpan.Parse(time);
  90.                     if (newtime< oldtime)
  91.                     {
  92.                         ToRemove = node;
  93.                         UI.Notify("~g~New record!");
  94.                         UI.Notify("Old time: ~y~" + oldtime);
  95.                         UI.Notify("New time: ~g~" + newtime);
  96.                         break;
  97.                     }
  98.                     else
  99.                     {
  100.                         UI.Notify("Time: ~y~" + newtime);
  101.                         UI.Notify("Best time: ~g~" + oldtime);
  102.                         CanAdd = false;
  103.                     }
  104.                 }
  105.             }
  106.             if (ToRemove != null)
  107.             {
  108.                 changes.RemoveChild(ToRemove);
  109.             }
  110.  
  111.             if (NotifyTime) UI.Notify("New time: ~g~" + time);
  112.  
  113.             if (CanAdd)
  114.             {
  115.                 XmlNode Data = originalXml.CreateNode(XmlNodeType.Element, "Car", null);
  116.                 XmlAttribute CarName = originalXml.CreateAttribute("Name");
  117.                 CarName.Value = carname;
  118.  
  119.                 XmlAttribute Time = originalXml.CreateAttribute("Time");
  120.                 Time.Value = time;
  121.  
  122.                 XmlAttribute Maxspeed = originalXml.CreateAttribute("MaxSpeed");
  123.                 Maxspeed.Value = maxspeed;
  124.  
  125.                 Data.Attributes.Append(CarName);
  126.                 Data.Attributes.Append(Time);
  127.                 Data.Attributes.Append(Maxspeed);
  128.  
  129.                 changes.AppendChild(Data);
  130.  
  131.                 originalXml.Save(@"scripts\\DownhillChallengeLeaderBoard.xml");          
  132.         }
  133.     }
  134.  
  135.     public static SizeF res = UIMenu.GetScreenResolutionMantainRatio();
  136.     public static Point safe = UIMenu.GetSafezoneBounds();
  137.     Keys MenuKey = Keys.Add;
  138.  
  139.     void DrawScoreboard()
  140.     {
  141.         int pluszone=0;
  142.         int X = (Convert.ToInt32(res.Width) - safe.X) / 3;
  143.         int Y = (Convert.ToInt32(res.Height) - safe.Y) / 7;
  144.         new UIResRectangle(new Point(X, Y - 32), new Size(X , 30), Color.FromArgb(150, 255, 0, 0)).Draw();
  145.         new UIResText("NAME   -   BEST TIME   -   MAX SPEED", new Point(X + 5, Y - 34), 0.5f,Color.White,GTA.Font.HouseScript,UIResText.Alignment.Left).Draw();
  146.  
  147.         if (File.Exists(@"scripts\\DownhillChallengeLeaderBoard.xml"))
  148.         {
  149.             foreach (XmlElement node in Leaderboard)
  150.             {
  151.                 new UIResRectangle(new Point(X, Y + pluszone), new Size(X, 32), Color.FromArgb(150, 0, 0, 0)).Draw();
  152.                 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();
  153.                 pluszone += 34;
  154.             }
  155.         }
  156.     }
  157.  
  158.     void OnTick(object sender, EventArgs e)
  159.     {
  160.         if (Game.IsKeyPressed(Keys.Tab) && (Game.Player.Character.IsInRangeOf(EndPoint,200f) || Game.Player.Character.IsInRangeOf(StartingPoint,50f))) DrawScoreboard();
  161.         _menuPool.ProcessMenus();
  162.         if (Game.IsKeyPressed(MenuKey) && !_menuPool.IsAnyMenuOpen())
  163.         {
  164.             mainMenu.Visible = !mainMenu.Visible;
  165.         }
  166.         if (ChallengeState == ChallengeStatus.Ready || ChallengeState == ChallengeStatus.InProgress)
  167.         {
  168.             if (UIHideMap.Checked) Function.Call(Hash.HIDE_HUD_AND_RADAR_THIS_FRAME);
  169.  
  170.  
  171.             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);
  172.             Vehicle veh = Game.Player.Character.CurrentVehicle;
  173.             if (CanWeUse(veh))
  174.             {
  175.                 if (veh.IsOnAllWheels && Function.Call<Vector3>(Hash.GET_ENTITY_SPEED_VECTOR,veh,true).X >speed) speed = veh.Speed;
  176.                 string info = "Time: " + (Cronometer.Elapsed).ToString();
  177.                 if (UIShowInfo.Checked) DisplayHelpTextThisFrame(info);
  178.  
  179.                 if (ChallengeState == ChallengeStatus.Ready)
  180.                 {
  181.                     if (Game.IsControlPressed(2, GTA.Control.VehicleAccelerate) && veh.Speed > 1f)
  182.                     {
  183.                         ChallengeState = ChallengeStatus.InProgress;
  184.                         Cronometer.Start();
  185.                     }
  186.                 }
  187.                 if (ChallengeState == ChallengeStatus.InProgress)
  188.                 {
  189.                     //Finish line
  190.                     if (Game.Player.Character.IsInRangeOf(EndPoint, 10f))
  191.                     {
  192.                         //UI.Notify("Car Integrity: ~y~" + Math.Abs((Game.Player.Character.CurrentVehicle.BodyHealth)));
  193.                         SaveCar(veh.FriendlyName, (Cronometer.Elapsed).ToString(),Math.Round(speed*1.6f,1).ToString()+" km/h");
  194.                         ChallengeState = ChallengeStatus.NotStarted;
  195.                         UpdateLeaderboard();
  196.                         if (EndPosBlip != null) EndPosBlip.Remove();
  197.  
  198.                     }
  199.                     if (Game.IsControlPressed(2, GTA.Control.VehicleCinCam))
  200.                     {
  201.                         DisplayHelpTextThisFrame("~y~Restarting...");
  202.                         RestartReference++;
  203.                     }
  204.                     if (RestartReference > 50)
  205.                     {
  206.  
  207.                         StartChallenge();
  208.                         DisplayHelpTextThisFrame("~y~Challenge restarted.");
  209.                     }
  210.                 }
  211.             }
  212.             else
  213.             {
  214.                 EndChallenge();
  215.             }
  216.         }
  217.     }
  218.     void EndChallenge()
  219.     {
  220.         Cronometer.Reset();
  221.         DisplayHelpTextThisFrame("~r~Challenge ended.");
  222.         ChallengeState = ChallengeStatus.NotStarted;
  223.         if(EndPosBlip != null) EndPosBlip.Remove();
  224.         UpdateLeaderboard();
  225.     }
  226.     void StartChallenge()
  227.     {
  228.         Cronometer.Reset();
  229.         speed = 0;
  230.         Vehicle veh = Game.Player.Character.CurrentVehicle;
  231.         if (CanWeUse(veh))
  232.         {
  233.             RestartReference = 0;
  234.             veh.Position = StartingPoint;
  235.             veh.Heading = StartingPointHeading;
  236.             Function.Call(Hash.SET_VEHICLE_FIXED, veh);
  237.             Function.Call(Hash.SET_VEHICLE_DEFORMATION_FIXED, veh);
  238.             ChallengeState = ChallengeStatus.Ready;
  239.             if (EndPosBlip != null) EndPosBlip.Remove();
  240.  
  241.             EndPosBlip = World.CreateBlip(EndPoint);
  242.             EndPosBlip.Color = BlipColor.Yellow;
  243.             EndPosBlip.Sprite = BlipSprite.RaceFinish;
  244.             EndPosBlip.Name = "Finish Line";
  245.  
  246.             if (UIForcePerfectConditions.Checked)
  247.             {
  248.                 World.Weather = Weather.ExtraSunny;
  249.                 Function.Call(Hash.SET_CLOCK_TIME, 12,0,0);
  250.             }
  251.             UpdateLeaderboard();
  252.         }
  253.  
  254.      
  255.     }
  256.  
  257.     public void OnItemSelect(UIMenu sender, UIMenuItem selectedItem, int index)
  258.     {
  259.         if(selectedItem == UIStartChallenge)
  260.         {
  261.             if (CanWeUse(Game.Player.Character.CurrentVehicle))
  262.             {
  263.                 StartChallenge();
  264.                 _menuPool.CloseAllMenus();
  265.             }
  266.             else
  267.             {
  268.                 UI.ShowSubtitle("~r~You are not in a vehicle.");
  269.             }
  270.         }
  271.         if(selectedItem == UIEndtChallenge)
  272.         {
  273.             EndChallenge();
  274.         }
  275.     }
  276.     protected override void Dispose(bool dispose)
  277.     {
  278.         if (EndPosBlip != null) EndPosBlip.Remove();
  279.  
  280.         base.Dispose(dispose);
  281.     }
  282.  
  283.  
  284.  
  285.     /// TOOLS ///
  286.     void LoadSettings()
  287.     {
  288.         if (File.Exists(@"scripts\\DownhillChallengeConfig.ini"))
  289.         {
  290.             ScriptSettings config = ScriptSettings.Load(@"scripts\DownhillChallengeConfig.ini");
  291.  
  292.             string key = config.GetValue<string>("SETTINGS", "MenuKey", "ERROR");
  293.             bool CouldParse = Enum.TryParse(key, out MenuKey);
  294.             if (!CouldParse)
  295.             {
  296.                 WarnPlayer(ScriptName + " " + ScriptVer, "INVALID MENU KEY", "~y~" + ScriptName + " configuration file 'MenuKey' does not exist.");
  297.                 UI.Notify("Edit the file again and make sure that capital letters match (Add instead of add).");
  298.  
  299.                 MenuKey = Keys.Add;
  300.  
  301.             }
  302.         }
  303.         else
  304.         {
  305.             WarnPlayer(ScriptName + " " + ScriptVer, "CONFIG FILE NOT FOUND", "~r~"+ ScriptName + " config not found. Configuration reset to Default.");
  306.         }
  307.     }
  308.  
  309.     void WarnPlayer(string script_name, string title, string message)
  310.     {
  311.         Function.Call(Hash._SET_NOTIFICATION_TEXT_ENTRY, "STRING");
  312.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, message);
  313.         Function.Call(Hash._SET_NOTIFICATION_MESSAGE, "CHAR_SOCIAL_CLUB", "CHAR_SOCIAL_CLUB", true, 0, title, "~b~" + script_name);
  314.     }
  315.  
  316.     bool CanWeUse(Entity entity)
  317.     {
  318.         return entity != null && entity.Exists();
  319.     }
  320.  
  321.     void DisplayHelpTextThisFrame(string text)
  322.     {
  323.         Function.Call(Hash._SET_TEXT_COMPONENT_FORMAT, "STRING");
  324.         Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, text);
  325.         Function.Call(Hash._DISPLAY_HELP_TEXT_FROM_STRING_LABEL, 0, false, false, -1);
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement