Beispiel #1
0
 public static bool IsOwner(MyCubeGrid grid, long id)
 {
     if (grid == null || id == 0)
     {
         return(false);
     }
     return(GridCache.GetOwners(grid).Contains(id) || GridCache.GetBuilders(grid).Contains(id));
 }
Beispiel #2
0
        public static void DecreaseCount(MyCubeBlockDefinition def, long playerId, int amount = 1, long gridId = 0)
        {
            if (!BlockLimiterConfig.Instance.EnableLimits)
            {
                return;
            }
            var faction = MySession.Static.Factions.GetPlayerFaction(playerId);

            foreach (var limit in Limits)
            {
                if (!limit.IsMatch(def))
                {
                    continue;
                }

                var foundGrid = GridCache.TryGetGridById(gridId, out var grid);

                if (foundGrid && !limit.IsGridType(grid))
                {
                    limit.FoundEntities.Remove(gridId);
                    continue;
                }

                if (limit.IgnoreNpcs)
                {
                    if (MySession.Static.Players.IdentityIsNpc(playerId))
                    {
                        limit.FoundEntities.Remove(playerId);
                        continue;
                    }
                    if (foundGrid && MySession.Static.Players.IdentityIsNpc(GridCache.GetOwners(grid).FirstOrDefault()))
                    {
                        continue;
                    }
                }

                if (limit.LimitPlayers && playerId > 0)
                {
                    limit.FoundEntities.AddOrUpdate(playerId, 0, (l, i) => Math.Max(0, i - amount));
                }
                if (limit.LimitGrids && gridId > 0)
                {
                    limit.FoundEntities.AddOrUpdate(gridId, 0, (l, i) => Math.Max(0, i - amount));
                }
                if (limit.LimitFaction && faction != null)
                {
                    limit.FoundEntities.AddOrUpdate(faction.FactionId, 0, (l, i) => Math.Max(0, i - amount));
                }
                limit.ClearEmptyEntities();
            }
        }
Beispiel #3
0
        public static bool IsExcepted(object target, LimitItem limit = null)
        {
            if (target == null)
            {
                return(false);
            }

            HashSet <string> allExceptions = new HashSet <string>();

            if (limit != null)
            {
                allExceptions = new HashSet <string>(limit.Exceptions);
            }
            allExceptions.UnionWith(BlockLimiterConfig.Instance.GeneralException);

            if (allExceptions.Count == 0)
            {
                return(false);
            }

            MyIdentity     identity      = null;
            MyFaction      faction       = null;
            long           identityId    = 0;
            ulong          playerSteamId = 0;
            string         displayName   = "";
            HashSet <long> gridOwners    = new HashSet <long>();

            switch (target)
            {
            case HashSet <long> owners:
                gridOwners.UnionWith(owners);
                break;

            case ulong steamId:
                if (steamId == 0)
                {
                    return(false);
                }
                playerSteamId = steamId;
                identityId    = GetPlayerIdFromSteamId(steamId);
                identity      = MySession.Static.Players.TryGetIdentity(identityId);
                displayName   = identity.DisplayName;
                faction       = MySession.Static.Factions.GetPlayerFaction(identityId);
                break;

            case string name:
                if (allExceptions.Contains(name))
                {
                    return(true);
                }
                if (TryGetPlayerByNameOrId(name, out identity))
                {
                    identityId    = identity.IdentityId;
                    faction       = MySession.Static.Factions.GetPlayerFaction(identityId);
                    displayName   = identity.DisplayName;
                    playerSteamId = GetSteamIdFromPlayerId(identityId);
                }
                break;

            case long id:
                if (id == 0)
                {
                    return(false);
                }
                identityId = id;
                identity   = MySession.Static.Players.TryGetIdentity(id);
                if (identity != null)
                {
                    faction       = MySession.Static.Factions.GetPlayerFaction(id);
                    displayName   = identity.DisplayName;
                    playerSteamId = GetSteamIdFromPlayerId(id);
                }
                else
                {
                    faction = (MyFaction)MySession.Static.Factions.TryGetFactionById(id);
                }
                if (MyEntities.TryGetEntityById(id, out var entity))
                {
                    if (allExceptions.Contains(entity.DisplayName))
                    {
                        return(true);
                    }
                }

                if (GridCache.TryGetGridById(id, out var foundGrid))
                {
                    gridOwners.UnionWith(GridCache.GetOwners(foundGrid));
                    if (allExceptions.Contains(foundGrid.DisplayName))
                    {
                        return(true);
                    }
                }
                break;

            case MyFaction targetFaction:
                if (allExceptions.Contains(targetFaction.Tag) ||
                    allExceptions.Contains(targetFaction.FactionId.ToString()))
                {
                    return(true);
                }
                break;

            case MyPlayer player:
                playerSteamId = player.Character.ControlSteamId;
                if (playerSteamId == 0)
                {
                    return(false);
                }
                if (allExceptions.Contains(playerSteamId.ToString()))
                {
                    return(true);
                }
                identityId = GetPlayerIdFromSteamId(playerSteamId);
                if (identityId > 0)
                {
                    if (allExceptions.Contains(identityId.ToString()))
                    {
                        return(true);
                    }
                    identity    = MySession.Static.Players.TryGetIdentity(identityId);
                    displayName = identity.DisplayName;
                }
                break;

            case MyCubeGrid grid:
            {
                if (allExceptions.Contains(grid.DisplayName) || allExceptions.Contains(grid.EntityId.ToString()))
                {
                    return(true);
                }
                var owners = GridCache.GetOwners(grid);
                if (owners.Count == 0)
                {
                    break;
                }
                gridOwners.UnionWith(owners);
                break;
            }
            }

            foreach (var owner in gridOwners)
            {
                if (owner == 0)
                {
                    continue;
                }
                if (allExceptions.Contains(owner.ToString()))
                {
                    return(true);
                }
                identity      = MySession.Static.Players.TryGetIdentity(owner);
                playerSteamId = GetSteamIdFromPlayerId(owner);
                if (playerSteamId > 0 && allExceptions.Contains(playerSteamId.ToString()))
                {
                    return(true);
                }
                if (identity != null)
                {
                    if (allExceptions.Contains(identity.DisplayName))
                    {
                        return(true);
                    }
                }
                faction = MySession.Static.Factions.GetPlayerFaction(owner);
                if (faction != null && (allExceptions.Contains(faction.Tag) ||
                                        allExceptions.Contains(faction.FactionId.ToString())))
                {
                    return(true);
                }
            }

            if (playerSteamId > 0 && allExceptions.Contains(playerSteamId.ToString()))
            {
                return(true);
            }
            if (identityId > 0 && allExceptions.Contains(identityId.ToString()))
            {
                return(true);
            }
            if (identity != null && allExceptions.Contains(identity.DisplayName))
            {
                return(true);
            }
            if (faction != null && (allExceptions.Contains(faction.Tag) || allExceptions.Contains(faction.FactionId.ToString())))
            {
                return(true);
            }
            if (!string.IsNullOrEmpty(displayName) && allExceptions.Contains(displayName))
            {
                return(true);
            }
            return(false);
        }
Beispiel #4
0
        public static bool CanMerge(MyCubeGrid grid1, MyCubeGrid grid2, out List <string> blocks, out int count, out string limitName)
        {
            limitName = null;
            blocks    = new List <string>();
            count     = 0;
            if (grid1 == null || grid2 == null)
            {
                return(true);
            }

            if (Utilities.IsExcepted(GridCache.GetOwners(grid1)) ||
                Utilities.IsExcepted(GridCache.GetOwners(grid2)))
            {
                return(true);
            }

            var blocksHash = new HashSet <MySlimBlock>(grid1.CubeBlocks);

            blocksHash.UnionWith(grid2.CubeBlocks);

            if (blocksHash.Count == 0)
            {
                return(true);
            }

            blocks.Add("All blocks - Size Violation");
            var gridSize = grid1.CubeBlocks.Count + grid2.CubeBlocks.Count;
            var gridType = grid1.GridSizeEnum;
            var isStatic = grid1.IsStatic || grid2.IsStatic;

            if (BlockLimiterConfig.Instance.MaxBlockSizeShips > 0 && !isStatic && gridSize >= BlockLimiterConfig.Instance.MaxBlockSizeShips)
            {
                count     = Math.Abs(gridSize - BlockLimiterConfig.Instance.MaxBlockSizeShips);
                limitName = "MaxBlockSizeShips";
                return(false);
            }

            if (BlockLimiterConfig.Instance.MaxBlockSizeStations > 0 && isStatic && gridSize >= BlockLimiterConfig.Instance.MaxBlockSizeStations)
            {
                count     = Math.Abs(gridSize - BlockLimiterConfig.Instance.MaxBlockSizeStations);
                limitName = "MaxBlockSizeStations";

                return(false);
            }

            if (BlockLimiterConfig.Instance.MaxBlocksLargeGrid > 0 && gridType == MyCubeSize.Large && gridSize >= BlockLimiterConfig.Instance.MaxBlocksLargeGrid)
            {
                count     = Math.Abs(gridSize - BlockLimiterConfig.Instance.MaxBlocksLargeGrid);
                limitName = "MaxBlocksLargeGrid";
                return(false);
            }

            if (BlockLimiterConfig.Instance.MaxBlocksSmallGrid > 0 && gridType == MyCubeSize.Small && gridSize >= BlockLimiterConfig.Instance.MaxBlocksSmallGrid)
            {
                count     = Math.Abs(gridSize - BlockLimiterConfig.Instance.MaxBlocksSmallGrid);
                limitName = "MaxBlocksSmallGrid";

                return(false);
            }

            blocks.Clear();
            count = 0;
            foreach (var limit in BlockLimiterConfig.Instance.AllLimits)
            {
                limitName = limit.Name;
                if (!limit.LimitGrids)
                {
                    continue;
                }

                if (Utilities.IsExcepted(grid1) || Utilities.IsExcepted(grid2))
                {
                    continue;
                }

                var matchingBlocks = new List <MySlimBlock>(blocksHash.Where(x => limit.IsMatch(x.BlockDefinition)));

                if (matchingBlocks.Count <= limit.Limit)
                {
                    continue;
                }
                count = Math.Abs(matchingBlocks.Count - limit.Limit);
                blocks.Add(matchingBlocks[0].BlockDefinition.ToString().Substring(16));

                return(false);
            }

            return(true);
        }