Exemple #1
0
        private void AddGridToMap()
        {
            MyCubeGrid grid;

            while (NewGrids.TryDequeue(out grid))
            {
                var allFat = ConcurrentListPool.Get();

                var gridFat = grid.GetFatBlocks();
                for (int i = 0; i < gridFat.Count; i++)
                {
                    allFat.Add(gridFat[i]);
                }
                allFat.ApplyAdditions();

                var gridMap = GridMapPool.Get();

                if (grid.Components.TryGet(out gridMap.Targeting))
                {
                    gridMap.Targeting.AllowScanning = false;
                }
                gridMap.Trash = true;

                gridMap.MyCubeBocks = allFat;
                GridToInfoMap.TryAdd(grid, gridMap);
                grid.OnFatBlockAdded   += ToGridMap;
                grid.OnFatBlockRemoved += FromGridMap;
                grid.OnClose           += RemoveGridFromMap;
                DirtyGridInfos.Add(grid);
            }
        }
Exemple #2
0
        private void UpdateGrids()
        {
            DeferedUpBlockTypeCleanUp();

            DirtyGridsTmp.Clear();
            DirtyGridsTmp.AddRange(DirtyGrids);
            DirtyGrids.Clear();
            for (int i = 0; i < DirtyGridsTmp.Count; i++)
            {
                var grid       = DirtyGridsTmp[i];
                var newTypeMap = BlockTypePool.Get();
                newTypeMap[Offense]    = ConcurrentListPool.Get();
                newTypeMap[Utility]    = ConcurrentListPool.Get();
                newTypeMap[Thrust]     = ConcurrentListPool.Get();
                newTypeMap[Steering]   = ConcurrentListPool.Get();
                newTypeMap[Jumping]    = ConcurrentListPool.Get();
                newTypeMap[Power]      = ConcurrentListPool.Get();
                newTypeMap[Production] = ConcurrentListPool.Get();

                ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > noFatTypeMap;

                FatMap fatMap;
                if (GridToFatMap.TryGetValue(grid, out fatMap))
                {
                    var allFat    = fatMap.MyCubeBocks;
                    var terminals = 0;
                    var tStatus   = fatMap.Targeting == null || fatMap.Targeting.AllowScanning;
                    for (int j = 0; j < allFat.Count; j++)
                    {
                        var fat = allFat[j];
                        if (!(fat is IMyTerminalBlock))
                        {
                            continue;
                        }
                        terminals++;

                        using (fat.Pin()) {
                            if (fat.MarkedForClose)
                            {
                                continue;
                            }
                            if (fat is IMyProductionBlock)
                            {
                                newTypeMap[Production].Add(fat);
                            }
                            else if (fat is IMyPowerProducer)
                            {
                                newTypeMap[Power].Add(fat);
                            }
                            else if (fat is IMyGunBaseUser || fat is IMyWarhead || fat is MyConveyorSorter && WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id.SubtypeId))
                            {
                                if (!tStatus && fat is IMyGunBaseUser && !WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id.SubtypeId))
                                {
                                    tStatus = fatMap.Targeting.AllowScanning = true;
                                }

                                newTypeMap[Offense].Add(fat);
                            }
                            else if (fat is IMyUpgradeModule || fat is IMyRadioAntenna)
                            {
                                newTypeMap[Utility].Add(fat);
                            }
                            else if (fat is MyThrust)
                            {
                                newTypeMap[Thrust].Add(fat);
                            }
                            else if (fat is MyGyro)
                            {
                                newTypeMap[Steering].Add(fat);
                            }
                            else if (fat is MyJumpDrive)
                            {
                                newTypeMap[Jumping].Add(fat);
                            }
                        }
                    }

                    foreach (var type in newTypeMap)
                    {
                        type.Value.ApplyAdditions();
                    }

                    fatMap.MyCubeBocks.ApplyAdditions();

                    fatMap.Trash = terminals == 0;
                    var gridBlocks = grid.BlocksCount;
                    if (gridBlocks > fatMap.MostBlocks)
                    {
                        fatMap.MostBlocks = gridBlocks;
                    }
                    ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > oldTypeMap;
                    if (GridToBlockTypeMap.TryGetValue(grid, out oldTypeMap))
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                        BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                            Collection = oldTypeMap, RequestTick = Tick
                        });
                    }
                    else
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                    }
                }
                else if (GridToBlockTypeMap.TryRemove(grid, out noFatTypeMap))
                {
                    BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                        Collection = noFatTypeMap, RequestTick = Tick
                    });
                }
            }
            DirtyGridsTmp.Clear();
        }
Exemple #3
0
        /*
         * IEnumerable<Vector3I> NearLine(Vector3I start, LineD line)
         * {
         *  MinHeap blocks;
         *  HashSet<Vector3I> seen = new HashSet<Vector3I> {start};
         *  blocks.Add(dist(line, start), start);
         *  while (!blocks.Empty)
         *  {
         *      var next = blocks.RemoveMin();
         *      yield return next;
         *      foreach (var neighbor in Neighbors(next))
         *      {
         *          if (seen.add(neighbor))
         *              blocks.Add(dist(line, neighbor), neighbor);
         *      }
         *  }
         * }
         */

        private void UpdateGrids()
        {
            DeferedUpBlockTypeCleanUp();

            DirtyGridsTmp.Clear();
            DirtyGridsTmp.AddRange(DirtyGridInfos);
            DirtyGridInfos.Clear();
            for (int i = 0; i < DirtyGridsTmp.Count; i++)
            {
                var grid       = DirtyGridsTmp[i];
                var newTypeMap = BlockTypePool.Get();
                newTypeMap[Offense]    = ConcurrentListPool.Get();
                newTypeMap[Utility]    = ConcurrentListPool.Get();
                newTypeMap[Thrust]     = ConcurrentListPool.Get();
                newTypeMap[Steering]   = ConcurrentListPool.Get();
                newTypeMap[Jumping]    = ConcurrentListPool.Get();
                newTypeMap[Power]      = ConcurrentListPool.Get();
                newTypeMap[Production] = ConcurrentListPool.Get();

                ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > noFatTypeMap;

                GridMap gridMap;
                if (GridToInfoMap.TryGetValue(grid, out gridMap))
                {
                    var allFat         = gridMap.MyCubeBocks;
                    var terminals      = 0;
                    var tStatus        = gridMap.Targeting == null || gridMap.Targeting.AllowScanning;
                    var thrusters      = 0;
                    var gyros          = 0;
                    var powerProducers = 0;
                    var warHead        = 0;
                    var working        = 0;
                    for (int j = 0; j < allFat.Count; j++)
                    {
                        var fat = allFat[j];
                        if (!(fat is IMyTerminalBlock))
                        {
                            continue;
                        }
                        terminals++;
                        using (fat.Pin()) {
                            if (fat.MarkedForClose)
                            {
                                continue;
                            }
                            if (fat.IsWorking && ++working == 1)
                            {
                                var oldCube = (gridMap.FakeController.SlimBlock as IMySlimBlock)?.FatBlock as MyCubeBlock;
                                if (oldCube == null || oldCube.MarkedForClose || oldCube.CubeGrid != grid)
                                {
                                    gridMap.FakeController.SlimBlock = fat.SlimBlock;
                                    GridDistributors[grid]           = gridMap;
                                }
                            }

                            var cockpit = fat as MyCockpit;
                            var decoy   = fat as IMyDecoy;
                            var bomb    = fat as IMyWarhead;

                            if (decoy != null)
                            {
                                WeaponDefinition.TargetingDef.BlockTypes type;
                                if (DecoyMap.TryGetValue(fat, out type))
                                {
                                    newTypeMap[type].Add(fat);
                                }
                                else
                                {
                                    newTypeMap[Utility].Add(fat);
                                    DecoyMap[fat] = Utility;
                                }
                                continue;
                            }

                            if (fat is IMyProductionBlock)
                            {
                                newTypeMap[Production].Add(fat);
                            }
                            else if (fat is IMyPowerProducer)
                            {
                                newTypeMap[Power].Add(fat);
                                powerProducers++;
                            }
                            else if (fat is IMyGunBaseUser || bomb != null || fat is MyConveyorSorter && WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id))
                            {
                                if (bomb != null)
                                {
                                    warHead++;
                                }

                                if (!tStatus && fat is IMyGunBaseUser && !WeaponPlatforms.ContainsKey(fat.BlockDefinition.Id))
                                {
                                    tStatus = gridMap.Targeting.AllowScanning = true;
                                }

                                newTypeMap[Offense].Add(fat);
                            }
                            else if (fat is IMyUpgradeModule || fat is IMyRadioAntenna || cockpit != null && cockpit.EnableShipControl || fat is MyRemoteControl || fat is IMyShipGrinder || fat is IMyShipDrill)
                            {
                                newTypeMap[Utility].Add(fat);
                            }
                            else if (fat is MyThrust)
                            {
                                newTypeMap[Thrust].Add(fat);
                                thrusters++;
                            }
                            else if (fat is MyGyro)
                            {
                                newTypeMap[Steering].Add(fat);
                                gyros++;
                            }

                            else if (fat is MyJumpDrive)
                            {
                                newTypeMap[Jumping].Add(fat);
                            }
                        }
                    }

                    foreach (var type in newTypeMap)
                    {
                        type.Value.ApplyAdditions();
                    }

                    GridMap oldMap;
                    if (terminals == 0 && !gridMap.Trash && GridDistributors.TryRemove(grid, out oldMap))
                    {
                        oldMap.FakeController.SlimBlock = null;
                    }

                    gridMap.MyCubeBocks.ApplyAdditions();
                    gridMap.SuspectedDrone = warHead > 0 || powerProducers > 0 && thrusters > 0 && gyros > 0;
                    gridMap.Trash          = terminals == 0;
                    gridMap.Powered        = working > 0;
                    gridMap.PowerCheckTick = Tick;

                    var gridBlocks = grid.BlocksCount;

                    if (gridBlocks > gridMap.MostBlocks)
                    {
                        gridMap.MostBlocks = gridBlocks;
                    }

                    ConcurrentDictionary <WeaponDefinition.TargetingDef.BlockTypes, ConcurrentCachingList <MyCubeBlock> > oldTypeMap;
                    if (GridToBlockTypeMap.TryGetValue(grid, out oldTypeMap))
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                        BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                            Collection = oldTypeMap, RequestTick = Tick
                        });
                    }
                    else
                    {
                        GridToBlockTypeMap[grid] = newTypeMap;
                    }
                }
                else if (GridToBlockTypeMap.TryRemove(grid, out noFatTypeMap))
                {
                    BlockTypeCleanUp.Enqueue(new DeferedTypeCleaning {
                        Collection = noFatTypeMap, RequestTick = Tick
                    });
                }
            }
            DirtyGridsTmp.Clear();
        }