public static void PushAnyRequest(IMyConveyorEndpointBlock start, MyInventory srcInventory, long playerId)
        {
            if (srcInventory.Empty())
            {
                return;
            }

            // try all items and stop on first successfull
            foreach (var item in srcInventory.GetItems())
            {
                if (ItemPushRequest(start, srcInventory, playerId, item))
                {
                    return;
                }
            }
        }
예제 #2
0
        private static void StepGrind(ShipyardItem shipyardItem)
        {
            var            random           = new Random();
            float          grindAmount      = Server.Instance.Config.GrinderSpeedMultiplier * PluginSettings.Instance.GrindMultiplier;
            HashSet <long> grindersToRemove = new HashSet <long>();
            //shorten this to grid for convenience
            MyCubeGrid  grid       = shipyardItem.Grid;
            List <Task> blockTasks = new List <Task>();

            if (grid?.Physics == null || grid.Closed)
            {
                return;
            }

            if (grid.BlocksCount < 1)
            {
                return;
            }

            //do a raycast to see if the grinder can see the block we're assigning to it
            foreach (IMyCubeBlock listGrinder in shipyardItem.Tools)
            {
                blockTasks.Add(Task.Run(() =>
                {
                    DisconnectHelper localDisconnect = new DisconnectHelper();
                    MySlimBlock nextBlock;
                    if (!shipyardItem.ProcessBlocks.TryGetValue(listGrinder.EntityId, out nextBlock))
                    {
                        var tryCount = 0;

                        //TODO: optimize the try count instead of picking an arbitrary value
                        //what the hell does that mean?
                        while (tryCount < 30)
                        {
                            if (grid.Physics.LinearVelocity != Vector3D.Zero || grid.Physics.AngularVelocity != Vector3D.Zero)
                            {
                                grid.Stop();
                            }

                            //limit the number of tries so we don't get stuck in a loop forever
                            tryCount++;

                            //pick a random block. we don't really care if two grinders hit the same block, so don't check
                            if (grid.BlocksCount > 30)
                            {
                                nextBlock = grid.CubeBlocks.ElementAt(random.Next(0, grid.BlocksCount - 1));
                            }

                            //if we have less than 30 blocks total, just iterate through them, it's faster than going at random
                            else
                            {
                                nextBlock = grid.CubeBlocks.ElementAt(Math.Min(tryCount, grid.BlocksCount - 1));
                            }
                            if (nextBlock == null)
                            {
                                continue;
                            }

                            if (shipyardItem.ProcessBlocks.ContainsValue(nextBlock))
                            {
                                continue;
                            }

                            //this raycast should give us the grid location of the first block it hits
                            //we don't really care if it hits our random block, just grab whatever the grinder sees first
                            Vector3I?blockResult = grid.RayCastBlocks(listGrinder.GetPosition(),
                                                                      grid.GridIntegerToWorld(nextBlock.Position));
                            if (!blockResult.HasValue)
                            {
                                continue;
                            }

                            //TODO: remove this when my PR is merged
                            //check if removing this block will split the grid
                            if (localDisconnect.TryDisconnect(grid.GetCubeBlock(blockResult.Value)))
                            {
                                //UtilityPlugin.Log.Info( "detected split" );
                                continue;
                            }

                            nextBlock = grid.GetCubeBlock(blockResult.Value);

                            break;
                        }

                        //we weren't able to find a suitable block somehow, so skip this grinder for now
                        if (nextBlock == null)
                        {
                            return;
                        }

                        //we found a block to pair with our grinder, add it to the dictionary and carry on with destruction
                        lock (shipyardItem.ProcessBlocks)
                        {
                            shipyardItem.ProcessBlocks.Add(listGrinder.EntityId, nextBlock);
                        }
                    }
                }));
                if (blockTasks.Count >= 2)
                {
                }
            }
            Task.WaitAll(blockTasks.ToArray());
            blockTasks.Clear();
            var tmpItemList = new List <MyPhysicalInventoryItem>();

            foreach (IMyCubeBlock grinderBlock in shipyardItem.Tools)
            {
                var grinder = (IMyShipGrinder)grinderBlock;

                var         grinderInventory = (MyInventory)grinder.GetInventory(0);
                MySlimBlock block;

                if (!shipyardItem.ProcessBlocks.TryGetValue(grinderBlock.EntityId, out block))
                {
                    continue;
                }

                if (block?.CubeGrid?.Physics == null)
                {
                    continue;
                }

                if (disconnect.TryDisconnect(block))
                {
                    //UtilityPlugin.Log.Info( "detected split at grind" );
                    shipyardItem.ProcessBlocks.Remove(grinderBlock.EntityId);
                    continue;
                }

                Wrapper.GameAction(() =>
                {
                    var damageInfo = new MyDamageInformation(false, grindAmount, MyDamageType.Grind,
                                                             grinder.EntityId);
                    if (block.UseDamageSystem)
                    {
                        MyDamageSystem.Static.RaiseBeforeDamageApplied(block, ref damageInfo);
                    }

                    block.DecreaseMountLevel(damageInfo.Amount, grinderInventory);
                    block.MoveItemsFromConstructionStockpile(grinderInventory);
                    if (block.UseDamageSystem)
                    {
                        MyDamageSystem.Static.RaiseAfterDamageApplied(block, damageInfo);
                    }

                    if (block.IsFullyDismounted)
                    {
                        if (block.FatBlock != null && block.FatBlock.HasInventory)
                        {
                            for (var i = 0; i < block.FatBlock.InventoryCount; ++i)
                            {
                                MyInventory blockInventory = block.FatBlock.GetInventory(i);
                                if (blockInventory == null)
                                {
                                    continue;
                                }

                                if (blockInventory.Empty())
                                {
                                    continue;
                                }

                                tmpItemList.Clear();
                                tmpItemList.AddList(blockInventory.GetItems());

                                foreach (MyPhysicalInventoryItem item in tmpItemList)
                                {
                                    MyInventory.Transfer(blockInventory, grinderInventory, item.ItemId);
                                }
                            }
                        }

                        if (block.UseDamageSystem)
                        {
                            MyDamageSystem.Static.RaiseDestroyed(block, damageInfo);
                        }

                        block.SpawnConstructionStockpile();
                        block.CubeGrid.RazeBlock(block.Min);
                        grindersToRemove.Add(grinderBlock.EntityId);
                    }
                });
                foreach (var tool in shipyardItem.Tools)
                {
                    MySlimBlock targetBlock;
                    Communication.MessageStruct message = new Communication.MessageStruct()
                    {
                        toolId      = tool.EntityId,
                        gridId      = 0,
                        blockPos    = new SerializableVector3I(0, 0, 0),
                        packedColor = 0,
                        pulse       = false
                    };
                    if (!shipyardItem.ProcessBlocks.TryGetValue(tool.EntityId, out targetBlock))
                    {
                        Communication.SendLine(message);
                        continue;
                    }
                    message.gridId      = targetBlock.CubeGrid.EntityId;
                    message.blockPos    = targetBlock.Position;
                    message.packedColor = Color.OrangeRed.PackedValue;
                    Communication.SendLine(message);
                }

                foreach (long removeId in grindersToRemove)
                {
                    shipyardItem.ProcessBlocks.Remove(removeId);
                }
            }
        }
예제 #3
0
        public static void PushAnyRequest(IMyConveyorEndpointBlock start, MyInventory srcInventory, long playerId)
        {
            if (srcInventory.Empty())
                return;

            var itemArray = srcInventory.GetItems().ToArray();
            foreach (var item in itemArray)
            {
                ItemPushRequest(start, srcInventory, playerId, item);
            }
        }
예제 #4
0
        public static void PushAnyRequest(IMyConveyorEndpointBlock start, MyInventory srcInventory, long playerId)
        {
            if (srcInventory.Empty())
                return;

            // try all items and stop on first successfull
            foreach (var item in srcInventory.GetItems())
            {
                if (ItemPushRequest(start, srcInventory, playerId, item))
                    return;
            }
        }
        private void StepGrind(ShipyardItem shipyardItem)
        {
            var targetsToRedraw = new HashSet <BlockTarget>();
            //we need to multiply this by MyShipGrinderConstants.GRINDER_AMOUNT_PER_SECOND / 2... which evaluates to 1
            float grindAmount = MyAPIGateway.Session.GrinderSpeedMultiplier * shipyardItem.Settings.GrindMultiplier;

            if (shipyardItem.TargetBlocks.Count == 0)
            {
                var targetblock = Profiler.Start(FullName, nameof(StepGrind), "Populate target blocks");
                var blocks      = new List <IMySlimBlock>();
                Logging.Instance.WriteLine(shipyardItem.YardGrids.Count.ToString());
                foreach (IMyCubeGrid yardGrid in shipyardItem.YardGrids.Where(g => g.Physics != null)) //don't process projections
                {
                    var tmpBlocks = new List <IMySlimBlock>();
                    yardGrid.GetBlocks(tmpBlocks);
                    blocks.AddRange(tmpBlocks);
                }

                foreach (IMySlimBlock tmpBlock in blocks)
                {
                    shipyardItem.TargetBlocks.Add(new BlockTarget(tmpBlock, shipyardItem));
                }
                targetblock.End();
            }
            List <BlockTarget> allBlocks = shipyardItem.TargetBlocks.ToList();

            if (allBlocks.Count == 0)
            {
                return;
            }

            if (!shipyardItem.ProxDict.Any())
            {
                //sort blocks by distance to each tool
                foreach (IMyCubeBlock tool in shipyardItem.Tools)
                {
                    var targetSortBlock            = Profiler.Start(FullName, nameof(StepGrind), "Sort Targets");
                    List <BlockTarget> sortTargets = allBlocks.ToList();
                    sortTargets.Sort((a, b) => a.ToolDist[tool.EntityId].CompareTo(b.ToolDist[tool.EntityId]));
                    shipyardItem.ProxDict[tool.EntityId] = sortTargets;
                    targetSortBlock.End();
                }
            }

            var targetFindBlock = Profiler.Start(FullName, nameof(StepGrind), "Find Targets");

            foreach (IMyCubeBlock tool in shipyardItem.Tools)
            {
                if (tool.Closed || tool.MarkedForClose)
                {
                    //this is bad
                    shipyardItem.Disable();
                    return;
                }

                BlockTarget[] blockArray = shipyardItem.BlocksToProcess[tool.EntityId];

                //find the next target for each grinder, if it needs one
                for (int i = 0; i < shipyardItem.Settings.BeamCount; i++)
                {
                    var toRemove = new HashSet <BlockTarget>();
                    if (blockArray[i] != null)
                    {
                        continue;
                    }

                    BlockTarget nextTarget = null;

                    for (int b = 0; b < shipyardItem.ProxDict[tool.EntityId].Count; b++)
                    {
                        nextTarget = shipyardItem.ProxDict[tool.EntityId][b];

                        if (nextTarget.CubeGrid.Closed || nextTarget.CubeGrid.MarkedForClose)
                        {
                            continue;
                        }

                        //one grinder per block, please
                        bool found = false;
                        foreach (KeyValuePair <long, BlockTarget[]> entry in shipyardItem.BlocksToProcess)
                        {
                            foreach (BlockTarget target in entry.Value)
                            {
                                if (target == null)
                                {
                                    continue;
                                }

                                if (target == nextTarget)
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }

                        if (found)
                        {
                            toRemove.Add(nextTarget);
                            continue;
                        }

                        targetsToRedraw.Add(nextTarget);
                        break;
                    }

                    foreach (BlockTarget removeTarget in toRemove)
                    {
                        shipyardItem.ProxDict[tool.EntityId].Remove(removeTarget);
                        shipyardItem.TargetBlocks.Remove(removeTarget);
                    }

                    //we found a block to pair with our grinder, add it to the dictionary and carry on with destruction
                    if (nextTarget != null)
                    {
                        shipyardItem.BlocksToProcess[tool.EntityId][i] = nextTarget;
                    }
                }
            }
            targetFindBlock.End();
            shipyardItem.UpdatePowerUse();
            var grindActionBlock = Profiler.Start(FullName, nameof(StepGrind), "Grind action");
            var removeTargets    = new List <BlockTarget>();

            //do the grinding
            Utilities.InvokeBlocking(() =>
            {
                foreach (IMyCubeBlock tool in shipyardItem.Tools)
                {
                    for (int b = 0; b < shipyardItem.BlocksToProcess[tool.EntityId].Length; b++)
                    {
                        BlockTarget target = shipyardItem.BlocksToProcess[tool.EntityId][b];
                        if (target == null)
                        {
                            continue;
                        }

                        if (target.CubeGrid.Closed || target.CubeGrid.MarkedForClose)
                        {
                            Logging.Instance.WriteDebug("Error in grind action: Target closed");
                            removeTargets.Add(target);
                            return;
                        }

                        if (targetsToRedraw.Contains(target))
                        {
                            var toolLine = new Communication.ToolLineStruct
                            {
                                ToolId       = tool.EntityId,
                                GridId       = target.CubeGrid.EntityId,
                                BlockPos     = target.GridPosition,
                                PackedColor  = Color.OrangeRed.PackedValue,
                                Pulse        = false,
                                EmitterIndex = (byte)b
                            };

                            Communication.SendLine(toolLine, shipyardItem.ShipyardBox.Center);
                        }

                        /*
                         * Grinding laser "efficiency" is a float between 0-1 where:
                         *   0.0 =>   0% of components recovered
                         *   1.0 => 100% of components recovered
                         *
                         * Efficiency decays exponentially as distance to the target (length of the "laser") increases
                         *     0m => 1.0000
                         *    10m => 0.9995
                         *    25m => 0.9969
                         *    50m => 0.9875
                         *   100m => 0.9500
                         *   150m => 0.8875
                         *   250m => 0.6875
                         *   400m => 0.2000
                         *    inf => 0.1000
                         * We impose a minimum efficiency of 0.1 (10%), which happens at distances > ~450m
                         */
                        double efficiency = 1 - (target.ToolDist[tool.EntityId] / 200000);
                        if (!shipyardItem.StaticYard)
                        {
                            efficiency /= 2;
                        }
                        if (efficiency < 0.1)
                        {
                            efficiency = 0.1;
                        }
                        //Logging.Instance.WriteDebug(String.Format("Grinder[{0}]block[{1}] distance=[{2:F2}m] efficiency=[{3:F5}]", tool.DisplayNameText, b, Math.Sqrt(target.ToolDist[tool.EntityId]), efficiency));

                        if (!shipyardItem.YardGrids.Contains(target.CubeGrid))
                        {
                            //we missed this grid or its split at some point, so add it to the list and register the split event
                            shipyardItem.YardGrids.Add(target.CubeGrid);
                            ((MyCubeGrid)target.CubeGrid).OnGridSplit += shipyardItem.OnGridSplit;
                        }
                        MyInventory grinderInventory = ((MyEntity)tool).GetInventory();

                        if (!target.Block.IsFullyDismounted)
                        {
                            var decreaseBlock = Profiler.Start(FullName, nameof(StepGrind), "DecreaseMountLevel");
                            target.Block.DecreaseMountLevel(grindAmount, grinderInventory);
                            decreaseBlock.End();

                            var inventoryBlock = Profiler.Start(FullName, nameof(StepGrind), "Grind Inventory");
                            // First move everything into _tmpInventory
                            target.Block.MoveItemsFromConstructionStockpile(_tmpInventory);

                            // Then move items into grinder inventory, factoring in our efficiency ratio
                            foreach (MyPhysicalInventoryItem item in _tmpInventory.GetItems())
                            {
                                //Logging.Instance.WriteDebug(String.Format("Grinder[{0}]block[{1}] Item[{2}] grind_amt[{3:F2}] collect_amt[{4:F2}]", tool.DisplayNameText, b, item.Content.SubtypeName, item.Amount, (double)item.Amount*efficiency));
                                grinderInventory.Add(item, (int)Math.Round((double)item.Amount * efficiency));
                            }

                            // Then clear out everything left in _tmpInventory
                            _tmpInventory.Clear();
                            inventoryBlock.End();
                        }

                        // This isn't an <else> clause because target.Block may have become FullyDismounted above,
                        // in which case we need to run both code blocks
                        if (target.Block.IsFullyDismounted)
                        {
                            var dismountBlock = Profiler.Start(FullName, nameof(StepGrind), "FullyDismounted");
                            var tmpItemList   = new List <MyPhysicalInventoryItem>();
                            var blockEntity   = target.Block.FatBlock as MyEntity;
                            if (blockEntity != null && blockEntity.HasInventory)
                            {
                                var dismountInventory = Profiler.Start(FullName, nameof(StepGrind), "DismountInventory");
                                for (int i = 0; i < blockEntity.InventoryCount; ++i)
                                {
                                    MyInventory blockInventory = blockEntity.GetInventory(i);
                                    if (blockInventory == null)
                                    {
                                        continue;
                                    }

                                    if (blockInventory.Empty())
                                    {
                                        continue;
                                    }

                                    tmpItemList.Clear();
                                    tmpItemList.AddRange(blockInventory.GetItems());

                                    foreach (MyPhysicalInventoryItem item in tmpItemList)
                                    {
                                        //Logging.Instance.WriteDebug(String.Format("Grinder[{0}]block[{1}] Item[{2}] inventory[{3:F2}] collected[{4:F2}]", tool.DisplayNameText, b, item.Content.SubtypeName, item.Amount, (double)item.Amount * efficiency));
                                        blockInventory.Remove(item, item.Amount);
                                        grinderInventory.Add(item, (int)Math.Round((double)item.Amount * efficiency));
                                    }
                                }
                                dismountInventory.End();
                            }
                            target.Block.SpawnConstructionStockpile();
                            target.CubeGrid.RazeBlock(target.GridPosition);
                            removeTargets.Add(target);
                            shipyardItem.TargetBlocks.Remove(target);
                            dismountBlock.End();
                        }
                    }
                }
            });

            foreach (KeyValuePair <long, List <BlockTarget> > entry in shipyardItem.ProxDict)
            {
                foreach (BlockTarget removeBlock in removeTargets)
                {
                    entry.Value.Remove(removeBlock);
                }
            }

            foreach (BlockTarget removeBlock in removeTargets)
            {
                shipyardItem.TargetBlocks.Remove(removeBlock);
            }

            //shipyardItem.ActiveTargets = 0;
            //clear lines for any destroyed blocks and update our target count
            foreach (KeyValuePair <long, BlockTarget[]> entry in shipyardItem.BlocksToProcess)
            {
                for (int i = 0; i < entry.Value.Length; i++)
                {
                    BlockTarget removeBlock = entry.Value[i];

                    if (removeTargets.Contains(removeBlock))
                    {
                        Communication.ClearLine(entry.Key, i);
                        entry.Value[i] = null;
                    }

                    //if (!removeTargets.Contains(removeBlock) && removeBlock != null)
                    //{
                    //    shipyardItem.ActiveTargets++;
                    //}
                }
            }

            grindActionBlock.End();
        }