private void SpawnedGridsSuccessful(HashSet <MyCubeGrid> Grids)
        {
            GridUtilities.BiggestGrid(Grids, out MyCubeGrid BiggestGrid);

            if (BiggestGrid != null && IdentityID != 0)
            {
                CharacterUtilities.SendGps(BiggestGrid.PositionComp.GetPosition(), BiggestGrid.DisplayName, IdentityID);
            }
        }
        private bool CheckDistanceToLoadPoint(Vector3D LoadPoint)
        {
            if (!Config.RequireLoadRadius)
            {
                return(true);
            }


            double Distance = Vector3D.Distance(PlayerPosition, LoadPoint);

            if (Distance < Config.LoadRadius)
            {
                return(true);
            }

            CharacterUtilities.SendGps(LoadPoint, "Load Point", IdentityID);
            Chat.Respond("Cannot load! You are " + Math.Round(Distance, 0) + "m away from the load point! Check your GPS points!");
            return(false);
        }
        private bool CheckEnemyDistance(LoadType LoadingAtSavePoint, Vector3D Position = new Vector3D())
        {
            if (LoadingAtSavePoint == LoadType.ForceLoadMearPlayer)
            {
                Position = PlayerPosition;
            }

            MyFaction PlayersFaction = MySession.Static.Factions.GetPlayerFaction(IdentityID);
            bool      EnemyFoundFlag = false;

            if (Config.DistanceCheck > 0)
            {
                //Check enemy location! If under limit return!
                foreach (MyCharacter OnlinePlayer in MyEntities.GetEntities().OfType <MyCharacter>())
                {
                    if (OnlinePlayer == null || OnlinePlayer.MarkedForClose)
                    {
                        continue;
                    }

                    long PlayerID = OnlinePlayer.GetPlayerIdentityId();
                    if (PlayerID == 0L)
                    {
                        continue;
                    }
                    if (PlayerID == IdentityID)
                    {
                        continue;
                    }



                    MyFaction TargetPlayerFaction = MySession.Static.Factions.GetPlayerFaction(PlayerID);
                    if (PlayersFaction != null && TargetPlayerFaction != null)
                    {
                        if (PlayersFaction.FactionId == TargetPlayerFaction.FactionId)
                        {
                            continue;
                        }

                        //Neutrals count as allies not friends for some reason
                        MyRelationsBetweenFactions Relation = MySession.Static.Factions.GetRelationBetweenFactions(PlayersFaction.FactionId, TargetPlayerFaction.FactionId).Item1;
                        if (Relation == MyRelationsBetweenFactions.Neutral || Relation == MyRelationsBetweenFactions.Friends)
                        {
                            continue;
                        }
                    }

                    if (Vector3D.Distance(Position, OnlinePlayer.PositionComp.GetPosition()) == 0)
                    {
                        continue;
                    }

                    if (Vector3D.Distance(Position, OnlinePlayer.PositionComp.GetPosition()) <= Config.DistanceCheck)
                    {
                        Chat?.Respond("Unable to load grid! Enemy within " + Config.DistanceCheck + "m!");
                        CharacterUtilities.SendGps(Position, "Failed Hangar Load! (Enemy nearby)", IdentityID);
                        EnemyFoundFlag = true;
                        break;
                    }
                }
            }


            if (Config.GridDistanceCheck > 0 && Config.GridCheckMinBlock > 0 && EnemyFoundFlag == false)
            {
                BoundingSphereD SpawnSphere = new BoundingSphereD(Position, Config.GridDistanceCheck);

                List <MyEntity> entities = new List <MyEntity>();
                MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref SpawnSphere, entities);



                //This is looping through all grids in the specified range. If we find an enemy, we need to break and return/deny spawning
                foreach (MyCubeGrid Grid in entities.OfType <MyCubeGrid>())
                {
                    if (Grid == null || Grid.MarkedForClose)
                    {
                        continue;
                    }

                    if (Grid.BigOwners.Count <= 0 || Grid.CubeBlocks.Count < Config.GridCheckMinBlock)
                    {
                        continue;
                    }

                    if (Grid.BigOwners.Contains(IdentityID))
                    {
                        continue;
                    }



                    //if the player isnt big owner, we need to scan for faction mates
                    bool FoundAlly = true;
                    foreach (long Owner in Grid.BigOwners)
                    {
                        MyFaction TargetPlayerFaction = MySession.Static.Factions.GetPlayerFaction(Owner);
                        if (PlayersFaction != null && TargetPlayerFaction != null)
                        {
                            if (PlayersFaction.FactionId == TargetPlayerFaction.FactionId)
                            {
                                continue;
                            }

                            MyRelationsBetweenFactions Relation = MySession.Static.Factions.GetRelationBetweenFactions(PlayersFaction.FactionId, TargetPlayerFaction.FactionId).Item1;
                            if (Relation == MyRelationsBetweenFactions.Enemies)
                            {
                                FoundAlly = false;
                                break;
                            }
                        }
                        else
                        {
                            FoundAlly = false;
                            break;
                        }
                    }


                    if (!FoundAlly)
                    {
                        //Stop loop
                        Chat?.Respond("Unable to load grid! Enemy within " + Config.GridDistanceCheck + "m!");
                        CharacterUtilities.SendGps(Position, "Failed Hangar Load! (Enemy nearby)", IdentityID);
                        EnemyFoundFlag = true;
                        break;
                    }
                }
            }
            return(!EnemyFoundFlag);
        }
        private bool CheckZoneRestrictions(bool IsSave)
        {
            if (Config.ZoneRestrictions.Count != 0)
            {
                //Get save point
                int    ClosestPoint = -1;
                double Distance     = -1;

                for (int i = 0; i < Config.ZoneRestrictions.Count(); i++)
                {
                    Vector3D ZoneCenter = new Vector3D(Config.ZoneRestrictions[i].X, Config.ZoneRestrictions[i].Y, Config.ZoneRestrictions[i].Z);

                    double PlayerDistance = Vector3D.Distance(ZoneCenter, PlayerPosition);

                    if (PlayerDistance <= Config.ZoneRestrictions[i].Radius)
                    {
                        //if player is within range

                        if (IsSave && !Config.ZoneRestrictions[i].AllowSaving)
                        {
                            Chat?.Respond("You are not permitted to save grids in this zone");
                            return(false);
                        }

                        if (!IsSave && !Config.ZoneRestrictions[i].AllowLoading)
                        {
                            Chat?.Respond("You are not permitted to load grids in this zone");
                            return(false);
                        }
                        return(true);
                    }



                    if (IsSave && Config.ZoneRestrictions[i].AllowSaving)
                    {
                        if (ClosestPoint == -1 || PlayerDistance <= Distance)
                        {
                            ClosestPoint = i;
                            Distance     = PlayerDistance;
                        }
                    }


                    if (!IsSave && Config.ZoneRestrictions[i].AllowLoading)
                    {
                        if (ClosestPoint == -1 || PlayerDistance <= Distance)
                        {
                            ClosestPoint = i;
                            Distance     = PlayerDistance;
                        }
                    }
                }
                Vector3D ClosestZone = new Vector3D();
                try
                {
                    ClosestZone = new Vector3D(Config.ZoneRestrictions[ClosestPoint].X, Config.ZoneRestrictions[ClosestPoint].Y, Config.ZoneRestrictions[ClosestPoint].Z);
                }
                catch (Exception e)
                {
                    Chat?.Respond("No areas found!");
                    //Log.Warn(e, "No suitable zones found! (Possible Error)");
                    return(false);
                }



                if (IsSave)
                {
                    CharacterUtilities.SendGps(ClosestZone, Config.ZoneRestrictions[ClosestPoint].Name + " (within " + Config.ZoneRestrictions[ClosestPoint].Radius + "m)", IdentityID);
                    Chat?.Respond("Nearest save area has been added to your HUD");
                    return(false);
                }
                else
                {
                    CharacterUtilities.SendGps(ClosestZone, Config.ZoneRestrictions[ClosestPoint].Name + " (within " + Config.ZoneRestrictions[ClosestPoint].Radius + "m)", IdentityID);
                    //Chat chat = new Chat(Context);
                    Chat?.Respond("Nearest load area has been added to your HUD");
                    return(false);
                }
            }
            return(true);
        }