Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Sandbox.Common;
- using Sandbox.Common.ObjectBuilders;
- using Sandbox.Common.ObjectBuilders.Definitions;
- using Sandbox.Definitions;
- using Sandbox.Game;
- using Sandbox.Game.Entities;
- using Sandbox.Game.EntityComponents;
- using Sandbox.ModAPI.Ingame;
- using Sandbox.ModAPI.Interfaces;
- using SpaceEngineers.Game.ModAPI;
- using VRage.Game;
- using VRage.Game.Components;
- using VRage.Game.Entity;
- using VRage.Game.ModAPI;
- using VRage.ModAPI;
- using VRage.ObjectBuilders;
- using VRage.Utils;
- using VRageMath;
- namespace Tilleen{
- [MySessionComponentDescriptor(MyUpdateOrder.BeforeSimulation)]
- public class GPSDistanceHelper : MySessionComponentBase{
- int scriptRun = 0;
- int scriptRunTrigger = 180;
- bool scriptSetup = false;
- string [] cobaltTags = {"Cobalt", "Co"};
- string [] goldTags = {"Gold", "Au"};
- string [] ironTags = {"Iron", "Fe"};
- string [] iceTags = {"Ice", "H2O"};
- string [] magnesiumTags = {"Magnesium", "Mg"};
- string [] nickelTags = {"Nickel", "Ni"};
- string [] platinumTags = {"Platinum", "Pt"};
- string [] siliconTags = {"Silicon", "Si"};
- string [] silverTags = {"Silver", "Ag"};
- string [] uraniumTags = {"Uranium", "U"};
- // string [][] oreTags = {cobaltTags, goldTags, ironTags, iceTags, magnesiumTags, nickelTags, platinumTags, siliconTags, silverTags, uraniumTags};
- char[] separators = new char [] {'[',']'};
- char[] whitespace = new char [] {' ','!','@','#','$','%','^','&','*','(',')','-','=','_','+','~',',','.'};
- public class GDHIMyGPS : IEquatable<GDHIMyGPS>, IComparable<GDHIMyGPS> {
- public GDHIMyGPS() {}
- public IMyGps gpsLoc_ {get; set; }
- public double distance_ {get; set; }
- public override bool Equals(object obj)
- {
- if (obj == null) return false;
- GDHIMyGPS objAsGDHIMyGPS = obj as GDHIMyGPS;
- if (objAsGDHIMyGPS == null) return false;
- else return Equals(objAsGDHIMyGPS);
- }
- public bool Equals(GDHIMyGPS obj)
- {
- if (obj == null) return false;
- else return this.distance_.Equals(obj.distance_);
- }
- public int CompareTo(GDHIMyGPS compareGDHIMyGPS)
- {
- if (compareGDHIMyGPS == null)
- return 1;
- else
- return this.distance_.CompareTo(compareGDHIMyGPS.distance_);
- }
- }
- public override void UpdateBeforeSimulation(){
- if(scriptSetup == false){
- MyAPIGateway.Utilities.MessageEntered += ChatCommand;
- scriptSetup = true;
- }
- scriptRun++;
- if(scriptRun <= scriptRunTrigger){
- return;
- }
- scriptRun = 0;
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- //MyVisualScriptLogicProvider.SendChatMessage("GDH: Update Distances", "Msg", player.IdentityId, "Blue");
- if(player == null){
- return;
- }
- var guiScreen = MyAPIGateway.Gui.GetCurrentScreen;
- if(player.IsBot == true || guiScreen == MyTerminalPageEnum.Gps){
- return;
- }
- List<IMyGps> playerGPS = new List<IMyGps>();
- MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
- foreach(var gps in playerGPS){
- double distance = MeasureDistance(player.GetPosition(), gps.Coords);
- string suffix = "m]:";
- if(distance >= 1000){
- suffix = "km]:";
- distance = Math.Round(distance / 1000, 2);
- }
- string description = gps.Description;
- string distanceString = "["+distance.ToString() + suffix;
- if (description.Length == 0)
- {
- gps.Description = distanceString;
- } else
- {
- int openindex = description.IndexOf("[",0);
- int closeindex = description.IndexOf("]",0);
- if ((openindex >= 0) && (closeindex > openindex+2))
- {
- description = description.Remove(openindex, (2 + closeindex - openindex));
- gps.Description = description.Insert(openindex, distanceString);
- } else
- {
- gps.Description = distanceString + gps.Description;
- }
- }
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
- }
- }
- void ChatCommand(string messageText, ref bool sendToOthers){
- if(messageText.StartsWith("/GDH"))
- {
- if(messageText.StartsWith("/GDHHelp"))
- {
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- MyVisualScriptLogicProvider.SendChatMessage("GDH: GPS Distance Helper - Help", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHHelp", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHNearest {ore|tag} [count] [Hide]", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHFurthest {ore|tag} [count] [Hide]", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHWithin {distance} [Hide]", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHOutsideof {distance} [Hide]", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHHideAllGPS", "Msg", player.IdentityId, "Blue");
- MyVisualScriptLogicProvider.SendChatMessage("GDH: /GDHShowAllGPS", "Msg", player.IdentityId, "Blue");
- }
- if((messageText.StartsWith("/GDHNearest")) || (messageText.StartsWith("/Nearest")) ||
- (messageText.StartsWith("/GDHFurthest")) || (messageText.StartsWith("/Furthest"))
- ){
- //GDHIMyGPS test = new GDHIMyGPS{gpsLoc_ = NULL, distance_ = 0};
- bool furthest = false, hideAllFirst = false;
- int gpsCount = 1;
- if (messageText.Contains("Furthest"))
- {
- furthest = true;
- }
- string [] msgSplit = messageText.Split(' ');
- if(msgSplit.Length < 2){
- return;
- }
- if(msgSplit.Length >= 3){
- for (int i=2;i<msgSplit.Length;i++)
- {
- if (msgSplit[i] == "Hide")
- {
- hideAllFirst = true;
- } else {
- if (msgSplit[i].Length > 0)
- int.TryParse(msgSplit[i],out gpsCount);
- }
- }
- }
- gpsCount = Math.Min(gpsCount,10); // Force gpsCount to be 10 or less.
- gpsCount = Math.Max(gpsCount,1); // Force gpsCount to be 1 or more.
- string [] oreArray = {msgSplit[1]};
- if(msgSplit[1] == "Ag"){
- oreArray = silverTags;
- }
- if(msgSplit[1] == "Au"){
- oreArray = goldTags;
- }
- if(msgSplit[1] == "Co"){
- oreArray = cobaltTags;
- }
- if(msgSplit[1] == "Fe"){
- oreArray = ironTags;
- }
- if(msgSplit[1] == "Ice"){
- oreArray = iceTags;
- }
- if(msgSplit[1] == "Mg"){
- oreArray = magnesiumTags;
- }
- if(msgSplit[1] == "Ni"){
- oreArray = nickelTags;
- }
- if(msgSplit[1] == "Pt"){
- oreArray = platinumTags;
- }
- if(msgSplit[1] == "Si"){
- oreArray = siliconTags;
- }
- if(msgSplit[1] == "U"){
- oreArray = uraniumTags;
- }
- sendToOthers = false;
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- List<IMyGps> playerGPS = new List<IMyGps>();
- MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
- // MyVisualScriptLogicProvider.SendChatMessage("GDH: Count = "+gpsCount.ToString(), "Msg", player.IdentityId, "Blue");
- // MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + msgSplit[1], "Msg", player.IdentityId, "Blue");
- IMyGps foundGPS = null;
- List<GDHIMyGPS> foundGPSs = new List<GDHIMyGPS>();
- // double [] foundDistances = new double[gpsCount];
- int numberFound = 0;
- double compareDistance = 0;
- foreach(var gps in playerGPS){
- bool foundTag = false;
- string [] splitName = gps.Name.Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
- foreach(var nameSegment in splitName)
- {
- foreach(var tag in oreArray){
- // MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + tag + " in " + nameSegment, "Msg", player.IdentityId, "Blue");
- if(nameSegment.Equals(tag,StringComparison.CurrentCultureIgnoreCase)){
- // MyVisualScriptLogicProvider.SendChatMessage("GDH: Found.", "Msg", player.IdentityId, "Blue");
- double distance = 0;
- double multiplier = 1;
- string [] distStrings = gps.Description.Split(separators,2,StringSplitOptions.RemoveEmptyEntries);
- string distanceString = "";
- if(distStrings[0].Contains("km")){
- distanceString = distStrings[0].Replace("km", "");
- multiplier = 1000;
- }
- else
- if(distStrings[0].Contains("m")){
- distanceString = distStrings[0].Replace("m", "");
- }
- double.TryParse(distanceString, out distance);
- distance = distance * multiplier;
- foundGPSs.Add(new GDHIMyGPS{gpsLoc_ = gps, distance_ = distance});
- foundTag = true;
- numberFound++;
- break;
- }
- }
- }
- if(foundTag == false){
- continue;
- }
- /*
- if(distance == 0){
- continue;
- }
- if(foundGPS == null){
- foundGPS = gps;
- compareDistance = distance;
- // foundGPSs.add(gps,distance);
- // foundDistances[0] = distance;
- }else{
- if (furthest)
- {
- if(distance > compareDistance){
- foundGPS = gps;
- compareDistance = distance;
- for (int i = gpsCount-1; i > 0; i--)
- {
- foundGPSs[i] = foundGPSs[i-1];
- foundDistances[i] = foundDistances[i-1];
- }
- foundGPSs[0] = gps;
- foundDistances[0] = distance;
- }
- }
- else
- if (distance < compareDistance){
- foundGPS = gps;
- compareDistance = distance;
- for (int i = gpsCount-1; i > 0; i--)
- {
- foundGPSs[i] = foundGPSs[i-1];
- foundDistances[i] = foundDistances[i-1];
- }
- foundGPSs[0] = gps;
- foundDistances[0] = distance;
- }
- } */
- }
- /* if(foundGPS != null){
- if (hideAllFirst)
- HideAllGPS(ref sendToOthers);
- if (gpsCount == 1)
- {
- foundGPS.ShowOnHud = true;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, foundGPS);
- MyVisualScriptLogicProvider.SendChatMessage("GDH: "+foundGPS.Name + " enabled at distance of " + compareDistance.ToString() + "m", "Msg", player.IdentityId, "Blue");
- }
- else*/
- if (foundGPSs.Count > 0)
- {
- if (furthest)
- foundGPSs.Sort(delegate(GDHIMyGPS x, GDHIMyGPS y)
- {
- return y.CompareTo(x);
- });
- else
- foundGPSs.Sort();
- if (hideAllFirst)
- HideAllGPS(ref sendToOthers);
- MyVisualScriptLogicProvider.SendChatMessage("GDH: List", "Msg", player.IdentityId, "Blue");
- int count = 0;
- foreach (var fgps in foundGPSs)
- {
- fgps.gpsLoc_.ShowOnHud = true;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, fgps.gpsLoc_);
- MyVisualScriptLogicProvider.SendChatMessage("GDH: "+fgps.gpsLoc_.Name + " enabled at distance of " + fgps.distance_.ToString() + "m", "Msg", player.IdentityId, "Blue");
- if (++count >= gpsCount)
- break; // Only display the requested number.
- }
- }
- else
- MyVisualScriptLogicProvider.SendChatMessage("GDH: None found.", "Msg", player.IdentityId, "Blue");
- }
- if((messageText.StartsWith("/GDHWithin")) || (messageText.StartsWith("/DisplayGPSWithin")) ||
- (messageText.StartsWith("/GDHOutsideof")) || (messageText.StartsWith("/DisplayGPSOutsideof"))){
- bool outsideof = false, hideAllFirst = false;
- string sideof = "within";
- if (messageText.Contains("Outsideof"))
- {
- outsideof = true;
- sideof = "outside of";
- }
- string [] msgSplit = messageText.Split(' ');
- if(msgSplit.Length < 2){
- return;
- }
- if (msgSplit[2] == "Hide")
- hideAllFirst = true;
- string reqDistance = msgSplit[1];
- double multiplier = 1;
- if(msgSplit[1].Contains("km")){
- reqDistance = msgSplit[1].Replace("km", "");
- multiplier = 1000;
- }
- else
- if(msgSplit[1].Contains("m")){
- reqDistance = msgSplit[1].Replace("m", "");
- }
- double requestedDistance = 0;
- double.TryParse(reqDistance, out requestedDistance);
- requestedDistance = requestedDistance * multiplier;
- if(requestedDistance == 0){
- return;
- }
- sendToOthers = false;
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- List<IMyGps> playerGPS = new List<IMyGps>();
- MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
- if (hideAllFirst)
- HideAllGPS(ref sendToOthers);
- foreach(var gps in playerGPS){
- string [] distStrings = gps.Description.Split(separators,2,StringSplitOptions.RemoveEmptyEntries);
- string distanceString = "";
- multiplier = 1;
- if(distStrings[0].Contains("km")){
- distanceString = distStrings[0].Replace("km", "");
- multiplier = 1000;
- }
- else
- if(distStrings[0].Contains("m")){
- distanceString = distStrings[0].Replace("m", "");
- }
- double distance = 0;
- double.TryParse(distanceString, out distance);
- distance = distance * multiplier;
- // MyVisualScriptLogicProvider.SendChatMessage("GDH: Searching for " + requestedDistance + ", at " + distance + " or " + distanceString +" from " + distStrings[0], "Msg", player.IdentityId, "Blue");
- if(distance == 0){
- continue;
- }
- if (outsideof)
- {
- if(distance > requestedDistance){
- gps.ShowOnHud = true;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
- }
- } else
- if(distance < requestedDistance){
- gps.ShowOnHud = true;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
- }
- }
- MyVisualScriptLogicProvider.SendChatMessage("GDH: Activated all signals "+sideof+" " + requestedDistance.ToString() + "m Of Player", "Msg", player.IdentityId, "Blue");
- }
- if((messageText.StartsWith("/GDHHideAllGPS")) || (messageText.StartsWith("/HideAllGPS"))){
- HideAllGPS(ref sendToOthers);
- }
- if((messageText.StartsWith("/GDHShowAllGPS"))||(messageText.StartsWith("/ShowAllGPS"))){
- sendToOthers = false;
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- List<IMyGps> playerGPS = new List<IMyGps>();
- MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
- foreach(var gps in playerGPS){
- gps.ShowOnHud = true;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
- }
- MyVisualScriptLogicProvider.SendChatMessage("GDH: All GPS Signals Now Visible", "Msg", player.IdentityId, "Blue");
- }
- }
- }
- void HideAllGPS(ref bool sendToOthers){
- sendToOthers = false;
- var player = MyAPIGateway.Session.LocalHumanPlayer;
- List<IMyGps> playerGPS = new List<IMyGps>();
- MyAPIGateway.Session.GPS.GetGpsList(player.IdentityId, playerGPS);
- foreach(var gps in playerGPS){
- gps.ShowOnHud = false;
- MyAPIGateway.Session.GPS.ModifyGps(player.IdentityId, gps);
- }
- MyVisualScriptLogicProvider.SendChatMessage("GDH: All GPS Signals Now Hidden", "Msg", player.IdentityId, "Blue");
- }
- double MeasureDistance(Vector3D coordsStart, Vector3D coordsEnd){
- double distance = Math.Round( Vector3D.Distance( coordsStart, coordsEnd ), 2 );
- return distance;
- }
- protected override void UnloadData(){
- MyAPIGateway.Utilities.MessageEntered -= ChatCommand;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment