Exemplo n.º 1
0
        internal bool GridHasPower(MyCubeGrid grid, GridMap map = null)
        {
            bool state = false;

            if (map != null || GridDistributors.TryGetValue(grid, out map))
            {
                var slim = map.FakeController.SlimBlock as IMySlimBlock;
                var cube = slim?.FatBlock as MyCubeBlock;

                if (cube != null && cube.CubeGrid == grid)
                {
                    var dist = map.FakeController.GridResourceDistributor;
                    state = dist?.ResourceState != MyResourceStateEnum.NoPower;
                }

                map.PowerCheckTick = Tick;
                map.Powered        = state;
            }
            return(state);
        }
Exemplo n.º 2
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();
        }