Example #1
0
        private bool PlayerIdentityLoop(Dictionary <long, Dictionary <string, int> > BlocksAndOwnerForLimits, int blocksToBuild)
        {
            foreach (KeyValuePair <long, Dictionary <string, int> > Player in BlocksAndOwnerForLimits)
            {
                Dictionary <string, int> PlayerBuiltBlocks = Player.Value;
                MyIdentity myIdentity = MySession.Static.Players.TryGetIdentity(Player.Key);
                if (myIdentity != null)
                {
                    MyBlockLimits blockLimits = myIdentity.BlockLimits;
                    if (MySession.Static.BlockLimitsEnabled == MyBlockLimitsEnabledEnum.PER_FACTION && MySession.Static.Factions.GetPlayerFaction(myIdentity.IdentityId) == null)
                    {
                        Chat.Respond("ServerLimits are set PerFaction. You are not in a faction! Contact an Admin!");
                        return(false);
                    }

                    if (blockLimits != null)
                    {
                        if (MySession.Static.MaxBlocksPerPlayer != 0 && blockLimits.BlocksBuilt + blocksToBuild > blockLimits.MaxBlocks)
                        {
                            Chat.Respond("Cannot load grid! You would be over your Max Blocks!");
                            return(false);
                        }

                        //Double check to see if the list is null
                        if (PlayerBuiltBlocks != null)
                        {
                            foreach (KeyValuePair <string, short> ServerBlockLimits in MySession.Static.BlockTypeLimits)
                            {
                                if (PlayerBuiltBlocks.ContainsKey(ServerBlockLimits.Key))
                                {
                                    int TotalNumberOfBlocks = PlayerBuiltBlocks[ServerBlockLimits.Key];

                                    if (blockLimits.BlockTypeBuilt.TryGetValue(ServerBlockLimits.Key, out MyBlockLimits.MyTypeLimitData LimitData))
                                    {
                                        //Grab their existing block count for the block limit
                                        TotalNumberOfBlocks += LimitData.BlocksBuilt;
                                    }

                                    //Compare to see if they would be over!
                                    short ServerLimit = MySession.Static.GetBlockTypeLimit(ServerBlockLimits.Key);
                                    if (TotalNumberOfBlocks > ServerLimit)
                                    {
                                        Chat.Respond("Player " + myIdentity.DisplayName + " would be over their " + ServerBlockLimits.Key + " limits! " + TotalNumberOfBlocks + "/" + ServerLimit);
                                        //Player would be over their block type limits
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(true);
        }
Example #2
0
        private bool CheckGridLimits(GridStamp Grid)
        {
            //Backwards compatibale
            if (Config.OnLoadTransfer)
            {
                return(true);
            }



            if (Grid.ShipPCU.Count == 0)
            {
                MyBlockLimits blockLimits = Identity.BlockLimits;

                MyBlockLimits a = MySession.Static.GlobalBlockLimits;

                if (a.PCU <= 0)
                {
                    //PCU Limits on server is 0
                    //Skip PCU Checks
                    //Log.Debug("PCU Server limits is 0!");
                    return(true);
                }

                //Main.Debug("PCU Limit from Server:"+a.PCU);
                //Main.Debug("PCU Limit from Player: " + blockLimits.PCU);
                //Main.Debug("PCU Built from Player: " + blockLimits.PCUBuilt);

                int CurrentPcu = blockLimits.PCUBuilt;
                //Hangar.Debug("Current PCU: " + CurrentPcu);

                int MaxPcu = blockLimits.PCU + CurrentPcu;

                int pcu = MaxPcu - CurrentPcu;
                //Main.Debug("MaxPcu: " + pcu);
                //Hangar.Debug("Grid PCU: " + Grid.GridPCU);


                //Hangar.Debug("Current player PCU:" + CurrentPcu);

                //Find the difference
                if (MaxPcu - CurrentPcu <= Grid.GridPCU)
                {
                    int Need = Grid.GridPCU - (MaxPcu - CurrentPcu);
                    Chat.Respond("PCU limit reached! You need an additional " + Need + " pcu to perform this action!");
                    return(false);
                }

                return(true);
            }


            foreach (KeyValuePair <long, int> Player in Grid.ShipPCU)
            {
                MyIdentity Identity = MySession.Static.Players.TryGetIdentity(Player.Key);
                if (Identity == null)
                {
                    continue;
                }


                MyBlockLimits blockLimits = Identity.BlockLimits;
                MyBlockLimits a           = MySession.Static.GlobalBlockLimits;

                if (a.PCU <= 0)
                {
                    //PCU Limits on server is 0
                    //Skip PCU Checks
                    //Hangar.Debug("PCU Server limits is 0!");
                    continue;
                }

                //Main.Debug("PCU Limit from Server:"+a.PCU);
                //Main.Debug("PCU Limit from Player: " + blockLimits.PCU);
                //Main.Debug("PCU Built from Player: " + blockLimits.PCUBuilt);

                int CurrentPcu = blockLimits.PCUBuilt;
                //Hangar.Debug("Current PCU: " + CurrentPcu);

                int MaxPcu = blockLimits.PCU + CurrentPcu;

                int pcu = MaxPcu - CurrentPcu;
                //Main.Debug("MaxPcu: " + pcu);
                //Hangar.Debug("Grid PCU: " + Grid.GridPCU);


                //Hangar.Debug("Current player PCU:" + CurrentPcu);

                //Find the difference
                if (MaxPcu - CurrentPcu <= Player.Value)
                {
                    int Need = Player.Value - (MaxPcu - CurrentPcu);
                    Chat.Respond("PCU limit reached! " + Identity.DisplayName + " needs an additional " + Need + " PCU to load this grid!");
                    return(false);
                }
            }

            return(true);
        }
Example #3
0
        public override void Update()
        {
            //NEED TO PULL THIS INFO FROM WPF LIST
            string NPCName = "Space Pirates";

            //Run first checks!
            // Grap space pirate entities id (im assuming its all the same for each world?)
            if (!RunOnce)
            {
                PirateEntityID = GetActiveNPCSIds(NPCName);
                RunOnce        = true;
            }


            if (PluginTickCounter >= MaxTickCounter && Config.PluginEnabled)
            {
                //Get active NPC Grids and store to wacthlist
                Parallel.ForEach(MyCubeGridGroups.Static.Mechanical.Groups, group =>
                {
                    foreach (MyGroups <MyCubeGrid, MyGridMechanicalGroupData> .Node groupNodes in group.Nodes)
                    {
                        MyCubeGrid cubeGrid = groupNodes.NodeData;

                        Tracker TGrid         = new Tracker();
                        TGrid.GridEntityID    = cubeGrid.EntityId;
                        TGrid.GridDisplayName = cubeGrid.DisplayName;
                        TGrid.Grid            = cubeGrid;


                        if (cubeGrid == null || cubeGrid.Physics == null)
                        {
                            continue;
                        }

                        if (TrackedGrids.Any(item => item.GridEntityID == TGrid.GridEntityID)) //Checkwatchlist
                        {
                            continue;
                        }



                        //Grab all blocks built by space pirates.
                        HashSet <MySlimBlock> Blocks       = cubeGrid.FindBlocksBuiltByID(PirateEntityID);
                        HashSet <MySlimBlock> NobodyBlocks = cubeGrid.FindBlocksBuiltByID(0); //Entity ID is ownedby Nobody


                        //Combine these hash sets
                        Blocks.UnionWith(NobodyBlocks);



                        if (Blocks.Count != 0 && cubeGrid.BlocksCount >= Config.BlockThreshold) //Checks to see if the grid is greater than 3 blocks. Debris Limit
                        {
                            //Blocks build by space pirates means the grid still has pcu of space pirate therefore must still be an npc grid.
                            if (cubeGrid.EntityId != null) //Check to make sure its not null and not in the list
                            {
                                //Alert logs and add it to the collection of tracked grids
                                Log.Info("Grid: " + cubeGrid.DisplayName + " Is being tracked with EntityID of (" + cubeGrid.EntityId + ")");

                                TrackedGrids.Add(TGrid);
                            }
                        }

                        /*
                         * Log.Info(cubeGrid.DisplayName + " Has large owners:");
                         * foreach (long BigOwnerID in cubeGrid.BigOwners)
                         * {
                         *  Log.Info(BigOwnerID);
                         *  //Log.Info(BigOwnerID);
                         *  if(BigOwnerID == PirateEntityID)
                         *  {
                         *
                         *      //Add this cubegrid to tracked entities
                         *      //Log.Info(cubeGrid.EntityId + "  :  " + TrackedGrids.Count);
                         *
                         *      if (cubeGrid.EntityId != null && !TrackedGrids.Contains(cubeGrid.EntityId))
                         *      {
                         *          Log.Info("Grid: " + cubeGrid.DisplayName + " Is being tracked with EntityID of ("+cubeGrid.EntityId+")");
                         *          TrackedGrids.Add(cubeGrid.EntityId);
                         *      }
                         *  }
                         * }
                         *
                         *
                         * Log.Info(cubeGrid.DisplayName + " Has small owners:");
                         * foreach (long SmallOwnerID in cubeGrid.SmallOwners)
                         * {
                         *  Log.Info(SmallOwnerID);
                         *  //cubeGrid.owner
                         * }
                         */
                    }
                });
                PluginTickCounter = 0;

                //Check owner/grid changes to see if player captured npc grids
                Parallel.ForEach(TrackedGrids, grid =>
                {
                    IMyCubeGrid cubeGrid = null;

                    try
                    {
                        foreach (IMyCubeGrid gridentity in MyEntities.GetEntities().OfType <IMyCubeGrid>().Where(g => g.EntityId == grid.GridEntityID))
                        {
                            //Get Cubegrid so we can transfer all blocks built by!
                            cubeGrid = gridentity;
                        }

                        if (cubeGrid == null)
                        {
                            Log.Info("Entity was removed from the game. Removing from watchlist!");
                            TrackedGrids.Remove(grid);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Warn("Entity cannot be converted to IMyCubeGrid! Removing entity from tracked list!");
                        TrackedGrids.Remove(grid);
                        Log.Warn(e);
                        return;
                    }



                    var gts = MyAPIGateway.TerminalActionsHelper.GetTerminalSystemForGrid(cubeGrid);
                    List <Sandbox.ModAPI.Ingame.IMyShipController> blockList = new List <Sandbox.ModAPI.Ingame.IMyShipController>();
                    gts.GetBlocksOfType <Sandbox.ModAPI.Ingame.IMyShipController>(blockList);
                    //Message to alert players when they start taking the grid?
                    //Example: This grid requires your to take all control blocks


                    //Undoo this comment to debug tracked grids!
                    //Log.Info("Checking grid changes on: " + cubeGrid.DisplayName);

                    if (blockList.Count == 0)
                    {
                        //Log.Info("Grid doesnt have any control blocks!");
                        return;
                    }


                    bool AllControlersCaptured = true;
                    long PlayerWhoCapturedNPC  = 0;
                    foreach (IMyShipController controler in blockList)
                    {
                        if (controler.OwnerId == PirateEntityID)
                        {
                            AllControlersCaptured = false;
                            continue;
                        }

                        //Basically grab last owner and transfer it all to him. (I dont really care about other faction mates right now. They can transfer between them)
                        PlayerWhoCapturedNPC = controler.OwnerId;
                    }


                    //Transfer all blocks to last block owner

                    if (AllControlersCaptured && PlayerWhoCapturedNPC != 0)
                    {
                        Log.Info("Player " + PlayerWhoCapturedNPC + " captured all the control blocks!");
                        MyCubeGrid Cubegrid = grid.Grid;



                        if (Cubegrid != null)
                        {
                            Log.Info("Transfering " + Cubegrid.DisplayName + " to new player!");


                            MyIdentity NewPlayer      = MySession.Static.Players.TryGetIdentity(PlayerWhoCapturedNPC);
                            MyBlockLimits blockLimits = NewPlayer.BlockLimits;

                            ulong UserSteamID = MyModAPIHelper.MyMultiplayer.Static.Players.TryGetSteamId(PlayerWhoCapturedNPC);



                            //Transfer Blocks!
                            List <long> authors = new List <long>();
                            foreach (MySlimBlock block in Cubegrid.GetBlocks())
                            {
                                if (block.BuiltBy == 0 || block.BuiltBy == PirateEntityID)
                                {
                                    block.TransferAuthorshipClient(PlayerWhoCapturedNPC);
                                    block.AddAuthorship();

                                    if (!authors.Contains(block.BuiltBy))
                                    {
                                        authors.Add(block.BuiltBy);
                                    }
                                }
                            }

                            foreach (long author in authors)
                            {
                                MyMultiplayer.RaiseEvent(Cubegrid, x => new Action <long, long>(x.TransferBlocksBuiltByID), author, PlayerWhoCapturedNPC, new EndpointId());
                            }



                            int CurrentPcu = blockLimits.PCUBuilt;
                            int MaxPcu     = blockLimits.PCU + CurrentPcu;



                            var sb = new StringBuilder();
                            sb.AppendLine("");
                            sb.AppendLine("Current PCU: " + CurrentPcu + "/" + MaxPcu);

                            if (CurrentPcu / MaxPcu >= 1.0)
                            {
                                /*
                                 * If they dont have room, Add list to secondary watch list on removing grid
                                 * This watch list will be saved to file so if server crashes we can still track grid and remove it when it re-starts
                                 *
                                 *
                                 *
                                 *
                                 *
                                 */

                                sb.AppendLine("Your are over your PCU Limits! This grid will be deleted on player log-off or server restart!");
                                sb.AppendLine("If you manage to get under your PCU limits before these times, the grid wont be deleted.");


                                Tracker TGrid         = new Tracker();
                                TGrid.GridEntityID    = grid.GridEntityID;
                                TGrid.PlayerEntityID  = NewPlayer;
                                TGrid.GridDisplayName = Cubegrid.DisplayName;
                                TGrid.PlayerName      = NewPlayer.DisplayName;
                                TGrid.Grid            = Cubegrid;



                                DeleteList.Add(TGrid);

                                Debug("Ship was added to delete watcher");
                            }

                            //Log.Info(NewPlayer.DisplayName);

                            //Will this work with entity id?
                            ModCommunication.SendMessageTo(new DialogMessage("NPC-Takeover", $"All control blocks captured!", "Transfering authorship on " + Cubegrid.DisplayName + " to you! " + sb), UserSteamID);

                            //ModCommunication.SendMessageTo(new TorchChatMessage("NPC-Takeover", $"All control blocks captured!", "Transfering authorship on " + Cubegrid.DisplayName + " to you!"), UserSteamID);

                            //MyAPIGateway.Utilities.SendMessage(76561198045096439, "PrivateTxt", "Target is Space Pirate");

                            //Cubegrid.TransferBlocksBuiltByID(PirateEntityID, PlayerWhoCapturedNPC);
                            //MyCubeGridGroups.Static.UpdateDynamicState(Cubegrid);
                            Debug("Removed grid from watchlist");
                            TrackedGrids.Remove(grid);
                            return;
                        }
                    }
                });


                //Check deleted grids list if player is offline or under pcu limit
                Parallel.ForEach(DeleteList, grid =>
                {
                    IMyCubeGrid cubeGrid = null;

                    try
                    {
                        foreach (IMyCubeGrid gridentity in MyEntities.GetEntities().OfType <IMyCubeGrid>().Where(g => g.EntityId == grid.GridEntityID))
                        {
                            //Get Cubegrid so we can transfer all blocks built by!
                            cubeGrid = gridentity;
                        }

                        if (cubeGrid == null)
                        {
                            Log.Info("Entity was removed from the game. Removing from watchlist!");
                            DeleteList.Remove(grid);
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Warn("Entity cannot be converted to IMyCubeGrid! Removing entity from tracked list!");
                        DeleteList.Remove(grid);
                        Log.Warn(e);
                        return;
                    }


                    //Get player offline
                    bool PlayerIsOffline = true;
                    foreach (MyPlayer Player in MySession.Static.Players.GetOnlinePlayers())
                    {
                        //Log.Info("Player " + Player.DisplayName + " is online!");
                        if (Player.Identity.IdentityId == grid.PlayerEntityID.IdentityId)
                        {
                            //Player is online
                            PlayerIsOffline = false;
                        }
                    }

                    if (PlayerIsOffline)
                    {
                        //Debuggin if players report their grid got deleted
                        Log.Info("Grid owner: " + grid.PlayerName + " is offline. Deleting grid " + grid.GridDisplayName + " (" + grid.GridEntityID + ")");
                        //Remove entitiy from game and delete from list
                        MyEntities.Close(cubeGrid as MyEntity);
                        DeleteList.Remove(grid);
                    }


                    bool GridFitsWithinPlayerlimits = false;
                    try
                    {
                        MyIdentity NewPlayer      = MySession.Static.Players.TryGetIdentity(grid.PlayerEntityID.IdentityId);
                        MyBlockLimits blockLimits = NewPlayer.BlockLimits;

                        int CurrentPcu = blockLimits.PCUBuilt;
                        int MaxPcu     = blockLimits.PCU + CurrentPcu;

                        //Get Current PCU of grid?
                        int GridPCU = grid.Grid.BlocksPCU;

                        if (MaxPcu - CurrentPcu >= GridPCU)
                        {
                            //Grid fits withing player PCU! Remove from watchlist!
                            Log.Info("Grid " + grid.GridDisplayName + " has been removed from watchlist. Player cleared PCU");
                            //Maybe alert player?
                            DeleteList.Remove(grid);
                        }
                    }
                    catch (Exception e)
                    {
                        //Failure to get player entitiy or Cubegrid class
                        Log.Warn(e);
                    }
                });

                //SaveList to file
            }

            PluginTickCounter++;
        }