Guest User

Untitled

a guest
Jan 9th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.21 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Sandbox.Common;
  7. using Sandbox.Common.ObjectBuilders;
  8. using Sandbox.Common.ObjectBuilders.Definitions;
  9. using Sandbox.Definitions;
  10. using Sandbox.Game;
  11. using Sandbox.Game.Entities;
  12. using Sandbox.Game.EntityComponents;
  13. using Sandbox.ModAPI.Ingame;
  14. using Sandbox.ModAPI.Interfaces;
  15. using SpaceEngineers.Game.ModAPI;
  16. using VRage.Game;
  17. using VRage.Game.Components;
  18. using VRage.Game.Entity;
  19. using VRage.Game.ModAPI;
  20. using VRage.ModAPI;
  21. using VRage.ObjectBuilders;
  22. using VRage.Utils;
  23. using VRageMath;
  24.  
  25. namespace Tilleen{
  26.    
  27.     [MySessionComponentDescriptor(MyUpdateOrder.BeforeSimulation)]
  28.        
  29.    
  30.        
  31.     public class GPSDistanceHelper : MySessionComponentBase{
  32.  
  33.         int scriptRun = 0;
  34.         int scriptRunTrigger = 180;
  35.         bool scriptSetup = false;
  36.        
  37.         string [] cobaltTags = {"Cobalt", "Co"};
  38.         string [] goldTags = {"Gold", "Au"};
  39.         string [] ironTags = {"Iron", "Fe"};
  40.         string [] iceTags = {"Ice", "H2O"};
  41.         string [] magnesiumTags = {"Magnesium", "Mg"};
  42.         string [] nickelTags = {"Nickel", "Ni"};
  43.         string [] platinumTags = {"Platinum", "Pt"};
  44.         string [] siliconTags = {"Silicon", "Si"};
  45.         string [] silverTags = {"Silver", "Ag"};
  46.         string [] uraniumTags = {"Uranium", "U"};
  47. //      string [][] oreTags = {cobaltTags, goldTags, ironTags, iceTags, magnesiumTags, nickelTags, platinumTags, siliconTags, silverTags, uraniumTags};
  48.         char[] separators = new char [] {'[',']'};
  49.         char[] whitespace = new char [] {' ','!','@','#','$','%','^','&','*','(',')','-','=','_','+','~',',','.'};
  50.        
  51.         public class GDHIMyGPS : IEquatable<GDHIMyGPS>, IComparable<GDHIMyGPS> {
  52.             public GDHIMyGPS() {}
  53.             public IMyGps gpsLoc_ {get; set; }
  54.             public double distance_ {get; set; }
  55.             public override bool Equals(object obj)
  56.             {
  57.                 if (obj == null) return false;
  58.                 GDHIMyGPS objAsGDHIMyGPS = obj as GDHIMyGPS;
  59.                 if (objAsGDHIMyGPS == null) return false;
  60.                 else return Equals(objAsGDHIMyGPS);
  61.             }
  62.             public bool Equals(GDHIMyGPS obj)
  63.             {
  64.                 if (obj == null) return false;
  65.                 else return this.distance_.Equals(obj.distance_);
  66.             }
  67.             public int CompareTo(GDHIMyGPS compareGDHIMyGPS)
  68.             {
  69.                 if (compareGDHIMyGPS == null)
  70.                     return 1;
  71.                 else
  72.                     return this.distance_.CompareTo(compareGDHIMyGPS.distance_);
  73.             }
  74.         }
  75.        
  76.         public override void UpdateBeforeSimulation(){
  77.            
  78.             if(scriptSetup == false){
  79.                
  80.                 MyAPIGateway.Utilities.MessageEntered += ChatCommand;
  81.                 scriptSetup = true;
  82.                
  83.             }
  84.            
  85.             scriptRun++;
  86.            
  87.             if(scriptRun <= scriptRunTrigger){
  88.                
  89.                 return;
  90.                
  91.             }
  92.            
  93.             scriptRun = 0;
  94.                    
  95.             var player = MyAPIGateway.Session.LocalHumanPlayer;
  96.             //MyVisualScriptLogicProvider.SendChatMessage("GDH: Update Distances", "Msg", player.IdentityId, "Blue");
  97.  
  98.             if(player == null){
  99.                
  100.                 return;
  101.                
  102.             }
  103.            
  104.             var guiScreen = MyAPIGateway.Gui.GetCurrentScreen;
  105.            
  106.             if(player.IsBot == true || guiScreen == MyTerminalPageEnum.Gps){
  107.                
  108.                 return;
  109.                
  110.             }
  111.            
  112.             List<IMyGps> playerGPS = new List<IMyGps>();
  113.             MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
  114.            
  115.             foreach(var gps in playerGPS){
  116.                
  117.                 double distance = MeasureDistance(player.GetPosition(), gps.Coords);
  118.                 string suffix = "m]:";
  119.                 if(distance >= 1000){
  120.                     suffix = "km]:";
  121.                     distance = Math.Round(distance / 1000, 2);
  122.                 }
  123.                 string description = gps.Description;
  124.                 string distanceString = "["+distance.ToString() + suffix;
  125.                 if (description.Length == 0)
  126.                 {
  127.                     gps.Description = distanceString;
  128.                 } else
  129.                 {
  130.                     int openindex = description.IndexOf("[",0);
  131.                     int closeindex = description.IndexOf("]",0);
  132.                     if ((openindex >= 0) && (closeindex > openindex+2))
  133.                     {
  134.                         description = description.Remove(openindex, (2 + closeindex - openindex));
  135.                         gps.Description = description.Insert(openindex, distanceString);
  136.                     } else
  137.                     {
  138.                         gps.Description = distanceString + gps.Description;
  139.                     }
  140.                 }
  141.                 MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
  142.                
  143.             }
  144.                                    
  145.         }
  146.        
  147.         void ChatCommand(string messageText, ref bool sendToOthers){
  148.             if(messageText.StartsWith("/GDH"))
  149.             {
  150.                 if(messageText.StartsWith("/GDHHelp"))
  151.                 {
  152.                     var player = MyAPIGateway.Session.LocalHumanPlayer;
  153.                     MyVisualScriptLogicProvider.SendChatMessage("GDH: GPS Distance Helper - Help", "Msg", player.IdentityId, "Blue");
  154.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHHelp", "Msg", player.IdentityId, "Blue");
  155.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHNearest {ore|tag} [count] [Hide]", "Msg", player.IdentityId, "Blue");
  156.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHFurthest {ore|tag} [count] [Hide]", "Msg", player.IdentityId, "Blue");
  157.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHWithin {distance} [Hide]", "Msg", player.IdentityId, "Blue");
  158.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHOutsideof {distance} [Hide]", "Msg", player.IdentityId, "Blue");
  159.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHHideAllGPS", "Msg", player.IdentityId, "Blue");
  160.                     MyVisualScriptLogicProvider.SendChatMessage("GDH:   /GDHShowAllGPS", "Msg", player.IdentityId, "Blue");
  161.                    
  162.                 }
  163.                 if((messageText.StartsWith("/GDHNearest")) || (messageText.StartsWith("/Nearest")) ||
  164.                     (messageText.StartsWith("/GDHFurthest")) || (messageText.StartsWith("/Furthest"))
  165.                     ){
  166.                     //GDHIMyGPS test = new GDHIMyGPS{gpsLoc_ = NULL, distance_ = 0};
  167.                     bool furthest = false, hideAllFirst = false;
  168.                     int gpsCount = 1;
  169.                     if (messageText.Contains("Furthest"))
  170.                     {
  171.                         furthest = true;
  172.                     }
  173.                    
  174.                     string [] msgSplit = messageText.Split(' ');
  175.                    
  176.                     if(msgSplit.Length < 2){
  177.                        
  178.                         return;
  179.                     }
  180.                    
  181.                     if(msgSplit.Length >= 3){
  182.                         for (int i=2;i<msgSplit.Length;i++)
  183.                         {
  184.                             if (msgSplit[i] == "Hide")
  185.                             {
  186.                                 hideAllFirst = true;
  187.                             } else {
  188.                                 if (msgSplit[i].Length > 0)
  189.                                     int.TryParse(msgSplit[i],out gpsCount);
  190.                             }
  191.                         }
  192.                     }
  193.                     gpsCount = Math.Min(gpsCount,10);                   // Force gpsCount to be 10 or less.
  194.                     gpsCount = Math.Max(gpsCount,1);                    // Force gpsCount to be 1 or more.
  195.                    
  196.                     string [] oreArray = {msgSplit[1]};
  197.                    
  198.                     if(msgSplit[1] == "Ag"){
  199.                        
  200.                         oreArray = silverTags; 
  201.                     }
  202.                    
  203.                     if(msgSplit[1] == "Au"){
  204.                        
  205.                         oreArray = goldTags;
  206.                     }
  207.                    
  208.                     if(msgSplit[1] == "Co"){
  209.                        
  210.                         oreArray = cobaltTags;
  211.                     }
  212.                    
  213.                     if(msgSplit[1] == "Fe"){
  214.                        
  215.                         oreArray = ironTags;
  216.                     }
  217.                    
  218.                     if(msgSplit[1] == "Ice"){
  219.                        
  220.                         oreArray = iceTags;
  221.                     }
  222.                    
  223.                     if(msgSplit[1] == "Mg"){
  224.                        
  225.                         oreArray = magnesiumTags;
  226.                     }
  227.                    
  228.                     if(msgSplit[1] == "Ni"){
  229.                        
  230.                         oreArray = nickelTags;
  231.                     }
  232.                    
  233.                     if(msgSplit[1] == "Pt"){
  234.                        
  235.                         oreArray = platinumTags;
  236.                     }
  237.                    
  238.                     if(msgSplit[1] == "Si"){
  239.                        
  240.                         oreArray = siliconTags;
  241.                     }
  242.                    
  243.                     if(msgSplit[1] == "U"){
  244.                        
  245.                         oreArray = uraniumTags;
  246.                     }
  247.                    
  248.                     sendToOthers = false;
  249.                     var player = MyAPIGateway.Session.LocalHumanPlayer;
  250.                     List<IMyGps> playerGPS = new List<IMyGps>();
  251.                     MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
  252. //                  MyVisualScriptLogicProvider.SendChatMessage("GDH:   Count = "+gpsCount.ToString(), "Msg", player.IdentityId, "Blue");
  253. //                  MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + msgSplit[1], "Msg", player.IdentityId, "Blue");
  254.                    
  255.                     IMyGps foundGPS = null;
  256.                     List<GDHIMyGPS> foundGPSs = new List<GDHIMyGPS>();
  257. //                  double [] foundDistances = new double[gpsCount];
  258.                     int numberFound = 0;
  259.                    
  260.                     double compareDistance = 0;
  261.                     foreach(var gps in playerGPS){
  262.                        
  263.                         bool foundTag = false;
  264.                         string [] splitName = gps.Name.Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
  265.                         foreach(var nameSegment in splitName)
  266.                         {
  267.                             foreach(var tag in oreArray){
  268. //                              MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + tag + " in " + nameSegment, "Msg", player.IdentityId, "Blue");
  269.                                
  270.                                 if(nameSegment.Equals(tag,StringComparison.CurrentCultureIgnoreCase)){ 
  271. //                                  MyVisualScriptLogicProvider.SendChatMessage("GDH: Found.", "Msg", player.IdentityId, "Blue");
  272.                                     double distance = 0;
  273.                                     double multiplier = 1;
  274.                                     string [] distStrings = gps.Description.Split(separators,2,StringSplitOptions.RemoveEmptyEntries);
  275.                                     string distanceString = "";
  276.                                     if(distStrings[0].Contains("km")){
  277.                                        
  278.                                         distanceString = distStrings[0].Replace("km", "");
  279.                                         multiplier = 1000;                     
  280.                                     }
  281.                                     else
  282.                                     if(distStrings[0].Contains("m")){
  283.                                        
  284.                                         distanceString = distStrings[0].Replace("m", "");                      
  285.                                     }
  286.                                    
  287.                                     double.TryParse(distanceString, out distance);
  288.                                    
  289.                                     distance = distance * multiplier;
  290.                                     foundGPSs.Add(new GDHIMyGPS{gpsLoc_ = gps, distance_ = distance});
  291.                                     foundTag = true;
  292.                                     numberFound++;
  293.                                     break;                         
  294.                                 }
  295.                             }
  296.                         }
  297.                        
  298.                         if(foundTag == false){                     
  299.                             continue;                      
  300.                         }
  301.                        
  302. /*                     
  303.                         if(distance == 0){
  304.                            
  305.                             continue;                      
  306.                         }
  307.  
  308.                         if(foundGPS == null){
  309.                            
  310.                             foundGPS = gps;
  311.                             compareDistance = distance;
  312. //                          foundGPSs.add(gps,distance);
  313. //                          foundDistances[0] = distance;
  314.                            
  315.                         }else{
  316.                            
  317.                             if (furthest)
  318.                             {
  319.                                 if(distance > compareDistance){
  320.                                    
  321.                                     foundGPS = gps;
  322.                                     compareDistance = distance;
  323.                                     for (int i = gpsCount-1; i > 0; i--)
  324.                                     {
  325.                                         foundGPSs[i] = foundGPSs[i-1];
  326.                                         foundDistances[i] = foundDistances[i-1];
  327.                                     }
  328.                                     foundGPSs[0] = gps;
  329.                                     foundDistances[0] = distance;
  330.                                 }
  331.                             }
  332.                             else
  333.                             if (distance < compareDistance){
  334.                                
  335.                                 foundGPS = gps;
  336.                                 compareDistance = distance;                        
  337.                                 for (int i = gpsCount-1; i > 0; i--)
  338.                                 {
  339.                                     foundGPSs[i] = foundGPSs[i-1];
  340.                                     foundDistances[i] = foundDistances[i-1];
  341.                                 }
  342.                                 foundGPSs[0] = gps;
  343.                                 foundDistances[0] = distance;
  344.                             }
  345.                         }                   */
  346.                     }
  347.                    
  348. /*                  if(foundGPS != null){
  349.                        
  350.                         if (hideAllFirst)
  351.                             HideAllGPS(ref sendToOthers);
  352.                        
  353.                         if (gpsCount == 1)
  354.                         {
  355.                             foundGPS.ShowOnHud = true;
  356.                             MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, foundGPS);
  357.                             MyVisualScriptLogicProvider.SendChatMessage("GDH: "+foundGPS.Name + " enabled at distance of " + compareDistance.ToString() + "m", "Msg", player.IdentityId, "Blue");                  
  358.                         }
  359.                         else*/
  360.                     if (foundGPSs.Count > 0)
  361.                     {
  362.                         if (furthest)
  363.                             foundGPSs.Sort(delegate(GDHIMyGPS x, GDHIMyGPS y)
  364.                             {
  365.                                 return y.CompareTo(x);
  366.                             });
  367.                         else
  368.                             foundGPSs.Sort();
  369.                         if (hideAllFirst)
  370.                             HideAllGPS(ref sendToOthers);
  371.                         MyVisualScriptLogicProvider.SendChatMessage("GDH: List", "Msg", player.IdentityId, "Blue");
  372.                         int count = 0;
  373.                         foreach (var fgps in foundGPSs)
  374.                         {
  375.                             fgps.gpsLoc_.ShowOnHud = true;
  376.                             MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, fgps.gpsLoc_);
  377.                             MyVisualScriptLogicProvider.SendChatMessage("GDH: "+fgps.gpsLoc_.Name + " enabled at distance of " + fgps.distance_.ToString() + "m", "Msg", player.IdentityId, "Blue");
  378.                             if (++count >= gpsCount)
  379.                                 break;                      // Only display the requested number.
  380.                         }
  381.                     }
  382.                    
  383.                     else
  384.                         MyVisualScriptLogicProvider.SendChatMessage("GDH: None found.", "Msg", player.IdentityId, "Blue");                 
  385.                 }
  386.                
  387.                 if((messageText.StartsWith("/GDHWithin")) || (messageText.StartsWith("/DisplayGPSWithin")) ||
  388.                   (messageText.StartsWith("/GDHOutsideof")) || (messageText.StartsWith("/DisplayGPSOutsideof"))){
  389.                     bool outsideof = false, hideAllFirst = false;
  390.                     string sideof = "within";
  391.                     if (messageText.Contains("Outsideof"))
  392.                     {
  393.                         outsideof = true;
  394.                         sideof = "outside of";
  395.                     }
  396.                     string [] msgSplit = messageText.Split(' ');
  397.                    
  398.                     if(msgSplit.Length < 2){
  399.                        
  400.                         return;                
  401.                     }
  402.                     if (msgSplit[2] == "Hide")
  403.                         hideAllFirst = true;
  404.                     string reqDistance = msgSplit[1];
  405.                     double multiplier = 1;
  406.                    
  407.                     if(msgSplit[1].Contains("km")){
  408.                        
  409.                         reqDistance = msgSplit[1].Replace("km", "");
  410.                         multiplier = 1000;                     
  411.                     }
  412.                     else
  413.                     if(msgSplit[1].Contains("m")){
  414.                        
  415.                         reqDistance = msgSplit[1].Replace("m", "");                    
  416.                     }
  417.                     double requestedDistance = 0;
  418.                     double.TryParse(reqDistance, out requestedDistance);
  419.                     requestedDistance = requestedDistance * multiplier;
  420.                    
  421.                     if(requestedDistance == 0){
  422.                        
  423.                         return;                
  424.                     }
  425.                    
  426.                     sendToOthers = false;
  427.                     var player = MyAPIGateway.Session.LocalHumanPlayer;
  428.                     List<IMyGps> playerGPS = new List<IMyGps>();
  429.                     MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
  430.                     if (hideAllFirst)
  431.                         HideAllGPS(ref sendToOthers);
  432.                    
  433.                     foreach(var gps in playerGPS){
  434.                        
  435.                         string [] distStrings = gps.Description.Split(separators,2,StringSplitOptions.RemoveEmptyEntries);
  436.                         string distanceString = "";
  437.                         multiplier = 1;
  438.                         if(distStrings[0].Contains("km")){
  439.                            
  440.                             distanceString = distStrings[0].Replace("km", "");
  441.                             multiplier = 1000;                     
  442.                         }
  443.                         else
  444.                         if(distStrings[0].Contains("m")){
  445.                            
  446.                             distanceString = distStrings[0].Replace("m", "");                      
  447.                         }
  448.                        
  449.                         double distance = 0;
  450.                         double.TryParse(distanceString, out distance);
  451.                        
  452.                         distance = distance * multiplier;
  453. //                      MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + requestedDistance + ", at " + distance + " or " + distanceString +" from " + distStrings[0], "Msg", player.IdentityId, "Blue");
  454.                        
  455.                         if(distance == 0){
  456.                            
  457.                             continue;                      
  458.                         }
  459.                         if (outsideof)
  460.                         {
  461.                             if(distance > requestedDistance){
  462.                                
  463.                                 gps.ShowOnHud = true;
  464.                                 MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);                    
  465.                             }                                          
  466.                         } else
  467.                         if(distance < requestedDistance){
  468.                            
  469.                             gps.ShowOnHud = true;
  470.                             MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);                    
  471.                         }                  
  472.                     }
  473.                    
  474.                     MyVisualScriptLogicProvider.SendChatMessage("GDH: Activated all signals "+sideof+" " + requestedDistance.ToString() + "m Of Player", "Msg", player.IdentityId, "Blue");            
  475.                 }
  476.                
  477.                 if((messageText.StartsWith("/GDHHideAllGPS")) || (messageText.StartsWith("/HideAllGPS"))){
  478.                     HideAllGPS(ref sendToOthers);
  479.                 }
  480.                
  481.                 if((messageText.StartsWith("/GDHShowAllGPS"))||(messageText.StartsWith("/ShowAllGPS"))){
  482.                    
  483.                     sendToOthers = false;
  484.                     var player = MyAPIGateway.Session.LocalHumanPlayer;
  485.                     List<IMyGps> playerGPS = new List<IMyGps>();
  486.                     MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
  487.                    
  488.                     foreach(var gps in playerGPS){
  489.                        
  490.                         gps.ShowOnHud = true;
  491.                         MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);                
  492.                     }
  493.                    
  494.                     MyVisualScriptLogicProvider.SendChatMessage("GDH: All GPS Signals Now Visible", "Msg", player.IdentityId, "Blue");
  495.                                    
  496.                 }
  497.             }      
  498.         }
  499.        
  500.         void HideAllGPS(ref bool sendToOthers){
  501.             sendToOthers = false;
  502.             var player = MyAPIGateway.Session.LocalHumanPlayer;
  503.             List<IMyGps> playerGPS = new List<IMyGps>();
  504.             MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
  505.            
  506.             foreach(var gps in playerGPS){
  507.                
  508.                 gps.ShowOnHud = false;
  509.                 MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);                
  510.             }              
  511.             MyVisualScriptLogicProvider.SendChatMessage("GDH: All GPS Signals Now Hidden", "Msg", player.IdentityId, "Blue");                              
  512.            
  513.         }
  514.        
  515.         double MeasureDistance(Vector3D coordsStart, Vector3D coordsEnd){
  516.                    
  517.             double distance = Math.Round( Vector3D.Distance( coordsStart, coordsEnd ), 2 );
  518.             return distance;           
  519.         }
  520.        
  521.         protected override void UnloadData(){
  522.  
  523.             MyAPIGateway.Utilities.MessageEntered -= ChatCommand;
  524.            
  525.         }
  526.     }
  527. }
Advertisement
Add Comment
Please, Sign In to add comment