private void GatherEnvItemsInBoxes()
        {
            var work = Interlocked.Exchange(ref m_cubeBlocksPending, m_cubeBlocksToWork);

            m_cubeBlocksToWork = work;

            int itemsVisited  = 0;
            int blocksVisited = 0;

            ProfilerShort.Begin("PlanetEnvironment::GatherItemsInSlimBlocks()");
            foreach (var grid in work.Values)
            {
                for (int blockIndex = 0; blockIndex < grid.Count; ++blockIndex)
                {
                    BoundingBoxD blockAabb = grid[blockIndex];

                    MyGamePruningStructure.GetAllVoxelMapsInBox(ref blockAabb, m_tmpVoxelList);
                    blocksVisited++;

                    for (int i = 0; i < m_tmpVoxelList.Count; ++i)
                    {
                        var p = m_tmpVoxelList[i] as MyPlanet;

                        if (p == null)
                        {
                            continue;
                        }

                        p.Hierarchy.QueryAABB(ref blockAabb, m_tmpEntityList);

                        for (int j = 0; j < m_tmpEntityList.Count; ++j)
                        {
                            var sector = m_tmpEntityList[j] as MyEnvironmentSector;

                            if (sector == null)
                            {
                                return;
                            }

                            var bb = blockAabb;
                            sector.GetItemsInAabb(ref bb, m_itemsToDisable.GetOrAddList(sector));
                            if (sector.DataView != null && sector.DataView.Items != null)
                            {
                                itemsVisited += sector.DataView.Items.Count;
                            }
                        }

                        m_tmpEntityList.Clear();
                    }

                    m_tmpVoxelList.Clear();
                }
            }
            ProfilerShort.End();

            //Debug.Print("Processed {0} blocks with {1} items", blocksVisited, itemsVisited);

            work.Clear();
        }
        void Scan()
        {
            m_lastScan = MySession.Static.GameplayFrameCounter;
            ProfilerShort.Begin("QueryTargets");
            BoundingSphereD bs = new BoundingSphereD(Vector3D.Transform(m_queryLocal.Center, m_grid.WorldMatrix), m_queryLocal.Radius);

            m_targetRoots.Clear();
            m_targetBlocks.Clear();

            ProfilerShort.Begin("MyGamePruningStructure.GetAllTop...");
            MyGamePruningStructure.GetAllTopMostEntitiesInSphere(ref bs, m_targetRoots);
            ProfilerShort.End();

            int targetCount = m_targetRoots.Count;

            m_ownersA.AddList(m_grid.SmallOwners);
            m_ownersA.AddList(m_grid.BigOwners);
            for (int i = 0; i < targetCount; i++)
            {
                var grid = m_targetRoots[i] as MyCubeGrid; //perf: using grid owners to not querry friendly ships for blocks
                if (grid != null)
                {
                    if (grid.Physics != null && !grid.Physics.Enabled)
                    {
                        continue;
                    }

                    ProfilerShort.Begin("Friend checks");
                    bool enemy = false;
                    if (grid.BigOwners.Count == 0 && grid.SmallOwners.Count == 0)
                    {
                        foreach (var owner in m_ownersA)
                        {
                            if (MyIDModule.GetRelation(owner, 0) == VRage.Game.MyRelationsBetweenPlayerAndBlock.Enemies)
                            {
                                enemy = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        m_ownersB.AddList(grid.BigOwners);
                        m_ownersB.AddList(grid.SmallOwners);


                        foreach (var owner in m_ownersA)
                        {
                            foreach (var other in m_ownersB)
                            {
                                if (MyIDModule.GetRelation(owner, other) == VRage.Game.MyRelationsBetweenPlayerAndBlock.Enemies)
                                {
                                    enemy = true;
                                    break;
                                }
                            }
                        }
                        m_ownersB.Clear();
                    }
                    ProfilerShort.End();
                    if (enemy)
                    {
                        ProfilerShort.Begin("grid.Hierarchy.QuerySphere");
                        var list = m_targetBlocks.GetOrAddList(grid);
                        grid.Hierarchy.QuerySphere(ref bs, list);
                        ProfilerShort.End();
                    }
                    else
                    {
                        MyIDModule module;
                        //performance unfriendly case when grid is not clearly neutral or enemy
                        foreach (var block in grid.GetFatBlocks())
                        {
                            var moduleOwner = ((MyEntity)block) as IMyComponentOwner <MyIDModule>;
                            if (!(moduleOwner != null && moduleOwner.GetComponent(out module)))
                            {
                                continue;
                            }

                            var myID = block.OwnerId;
                            foreach (var owner in m_ownersA)
                            {
                                if (MyIDModule.GetRelation(owner, myID) == VRage.Game.MyRelationsBetweenPlayerAndBlock.Enemies)
                                {
                                    enemy = true;
                                    break;
                                }
                            }
                            if (enemy)
                            {
                                break;
                            }
                        }

                        if (enemy)
                        {
                            ProfilerShort.Begin("grid.Hierarchy.QuerySphere");
                            var list = m_targetBlocks.GetOrAddList(grid);
                            grid.Hierarchy.QuerySphere(ref bs, list);
                            ProfilerShort.End();
                        }
                    }
                }
            }
            m_ownersA.Clear();

            ProfilerShort.Begin("Filter small objects");
            for (int i = m_targetRoots.Count - 1; i >= 0; i--)
            {
                var target = m_targetRoots[i];
                if (target is Sandbox.Game.Entities.Debris.MyDebrisBase ||
                    target is MyFloatingObject ||
                    (target.Physics != null && !target.Physics.Enabled) ||
                    target.GetTopMostParent().Physics == null || !target.GetTopMostParent().Physics.Enabled)
                {
                    m_targetRoots.RemoveAtFast(i);
                }
            }
            ProfilerShort.End();
            ProfilerShort.End();
        }
            public override bool HandleInput()
            {
                var handled = base.HandleInput();

                if (handled)
                {
                    return(true);
                }

                if (MyInput.Static.IsKeyPress(MyKeys.OemTilde))
                {
                    var info = m_members.Count > SelectedMember ? m_members[SelectedMember] : null;
                    if (MyInput.Static.IsNewKeyPressed(MyKeys.OemPeriod))
                    {
                        if (m_currentPath.Count > 0 && m_currentPath[m_currentPath.Count - 1] == info)
                        {
                            return(true);
                        }
                        var obj = info.GetValue(m_currentInstance);
                        if (obj != null && !obj.GetType().IsPrimitive&& obj.GetType().GetFields().Length + obj.GetType().GetProperties().Length > 0)
                        {
                            m_currentPath.Add(info);
                        }
                        m_counter = 0;
                        return(true);
                    }
                    else if (MyInput.Static.IsNewKeyPressed(MyKeys.OemComma))
                    {
                        if (m_currentPath.Count > 0)
                        {
                            m_currentPath.RemoveAt(m_currentPath.Count - 1);
                            m_counter = 0;
                        }
                        return(true);
                    }
                    else if (MyInput.Static.IsNewKeyPressed(MyKeys.OemQuestion))
                    {
                        if (m_showWatch)
                        {
                            if (!m_toPlot.Add(SelectedMember))
                            {
                                m_toPlot.Remove(SelectedMember);
                            }
                        }
                        else
                        {
                            MyListDictionary <MemberInfo, MemberInfo> watch = null;
                            if (!m_watch.TryGetValue(m_selectedType, out watch))
                            {
                                watch = m_watch[m_selectedType] = new MyListDictionary <MemberInfo, MemberInfo>();
                            }
                            var lst = watch.GetList(info);
                            if (lst != null)
                            {
                                watch.Remove(info);
                                return(true);
                            }
                            lst = watch.GetOrAddList(info);
                            lst.AddList(m_currentPath);
                            lst.Add(info);
                        }
                        return(true);
                    }
                    else if (MyInput.Static.IsNewKeyPressed(MyKeys.M))
                    {
                        m_showWatch = !m_showWatch;
                    }
                    else if (MyInput.Static.IsNewKeyPressed(MyKeys.OemPlus))
                    {
                        m_scale *= 2;
                    }
                    else if (MyInput.Static.IsNewKeyPressed(MyKeys.OemMinus))
                    {
                        m_scale /= 2;
                    }

                    m_counter += VRage.Input.MyInput.Static.PreviousMouseScrollWheelValue() - VRage.Input.MyInput.Static.MouseScrollWheelValue();
                    m_counter  = (long)MyMath.Clamp(m_counter, 0, m_members.Count / 0.002f);
                }

                return(handled);
            }