Example #1
0
        //based on MyGuiScreenHudSpace.DrawOreMarkers
        private IEnumerable <PositionedOreMarker> CreateMarkers()
        {
            MyHudOreMarkers oreMarkers = MyHud.OreMarkers;
            Vector3D        controlledEntityPosition = Vector3D.Zero;

            if (MySession.Static != null && MySession.Static.ControlledEntity != null)
            {
                controlledEntityPosition = ((MyEntity)MySession.Static.ControlledEntity).WorldMatrix.Translation;
            }

            PositionedOreMarker[] nearestOreDeposits =
                new PositionedOreMarker[MyDefinitionManager.Static.VoxelMaterialCount];
            float[] nearestDistancesSquared = new float[nearestOreDeposits.Length];

            for (int i = 0; i < nearestOreDeposits.Length; i++)
            {
                nearestOreDeposits[i]      = null;
                nearestDistancesSquared[i] = float.MaxValue;
            }


            foreach (MyEntityOreDeposit oreMarker in oreMarkers)
            {
                for (int i = 0; i < oreMarker.Materials.Count; i++)
                {
                    MyEntityOreDeposit.Data depositData = oreMarker.Materials[i];

                    var      oreMaterial = depositData.Material;
                    Vector3D oreWorldPosition;
                    var      voxelMap = oreMarker.VoxelMap;
                    MyVoxelCoordSystems.LocalPositionToWorldPosition(
                        (voxelMap.PositionComp.GetPosition() - (Vector3D)voxelMap.StorageMin),
                        ref depositData.AverageLocalPosition, out oreWorldPosition);

                    //ProfilerShort.BeginNextBlock("Distance");
                    Vector3D diff            = (controlledEntityPosition - oreWorldPosition);
                    float    distanceSquared = (float)diff.LengthSquared();

                    float nearestDistanceSquared = nearestDistancesSquared[oreMaterial.Index];
                    if (distanceSquared < nearestDistanceSquared)
                    {
                        MyVoxelMaterialDefinition voxelMaterial =
                            MyDefinitionManager.Static.GetVoxelMaterialDefinition(oreMaterial.Index);
                        nearestOreDeposits[oreMaterial.Index] = new PositionedOreMarker()
                        {
                            Position   = oreWorldPosition,
                            OreDeposit = oreMarker,
                            Name       = oreMarkers.GetOreName(voxelMaterial),
                            Distance   = diff.Length(),
                        };
                        nearestDistancesSquared[oreMaterial.Index] = distanceSquared;
                    }
                }
            }

            return(nearestOreDeposits.Where(nod =>
                                            nod?.OreDeposit?.VoxelMap != null && !nod.OreDeposit.VoxelMap.Closed));
        }
        private void DrawOreMarkers(MyHudOreMarkers oreMarkers)
        {
            ProfilerShort.Begin("MyGuiScreenHud.DrawOreMarkers");

            if (m_nearestOreDeposits == null || m_nearestOreDeposits.Length < MyDefinitionManager.Static.VoxelMaterialCount)
            {
                m_nearestOreDeposits = new OreDepositMarker[MyDefinitionManager.Static.VoxelMaterialCount];
                m_nearestDistanceSquared = new float[m_nearestOreDeposits.Length];
            }

            for (int i = 0; i < m_nearestOreDeposits.Length; i++)
            {
                m_nearestOreDeposits[i] = default(OreDepositMarker);
                m_nearestDistanceSquared[i] = float.MaxValue;
            }

            Vector3D controlledEntityPosition = Vector3D.Zero;
            if (MySession.Static != null && MySession.Static.ControlledEntity != null)
                controlledEntityPosition = (MySession.Static.ControlledEntity as MyEntity).WorldMatrix.Translation;

            foreach (MyEntityOreDeposit oreMarker in oreMarkers)
            {
                for (int i = 0; i < oreMarker.Materials.Count; i++)
                {
                    MyEntityOreDeposit.Data depositData = oreMarker.Materials[i];

                    var oreMaterial = depositData.Material;
                    Vector3D oreWorldPosition;
                    //ProfilerShort.Begin("ComputeWorldPosition");
                    depositData.ComputeWorldPosition(oreMarker.VoxelMap, out oreWorldPosition);

                    //ProfilerShort.BeginNextBlock("Distance");
                    Vector3D diff = (controlledEntityPosition - oreWorldPosition);
                    float distanceSquared = (float)diff.LengthSquared();

                    //ProfilerShort.BeginNextBlock("Use");
                    float nearestDistanceSquared = m_nearestDistanceSquared[oreMaterial.Index];
                    if (distanceSquared < nearestDistanceSquared)
                    {
                        m_nearestOreDeposits[oreMaterial.Index] = MyTuple.Create(oreWorldPosition, oreMarker);
                        m_nearestDistanceSquared[oreMaterial.Index] = distanceSquared;
                    }
                    //ProfilerShort.End();
                }
            }

            for (int i = 0; i < m_nearestOreDeposits.Length; i++)
            {
                var nearestOreDeposit = m_nearestOreDeposits[i];
                if (nearestOreDeposit.Item2 == null ||
                    nearestOreDeposit.Item2.VoxelMap == null ||
                    nearestOreDeposit.Item2.VoxelMap.Closed)
                    continue;

                MyVoxelMaterialDefinition voxelMaterial = MyDefinitionManager.Static.GetVoxelMaterialDefinition((byte)i);
                string oreSubtype = voxelMaterial.MinedOre;

                m_markerRender.AddOre(nearestOreDeposit.Item1, oreSubtype);
            }

            ProfilerShort.End();
        }
        private void DrawOreMarkers(MyHudOreMarkers oreMarkers)
        {
            ProfilerShort.Begin("MyGuiScreenHud.DrawOreMarkers");

            if (m_nearestOreDeposits == null || m_nearestOreDeposits.Length < MyDefinitionManager.Static.VoxelMaterialCount)
            {
                m_nearestOreDeposits = new OreDepositMarker[MyDefinitionManager.Static.VoxelMaterialCount];
                m_nearestDistanceSquared = new float[m_nearestOreDeposits.Length];
            }

            for (int i = 0; i < m_nearestOreDeposits.Length; i++)
            {
                m_nearestOreDeposits[i] = default(OreDepositMarker);
                m_nearestDistanceSquared[i] = float.MaxValue;
            }

            foreach (var oreMarker in oreMarkers)
            {
                bool debugBoxDrawn = false;
                foreach (var depositData in oreMarker.Materials)
                {
                    var oreMaterial = depositData.Material;
                    Vector3D oreWorldPosition;
                    depositData.ComputeWorldPosition(oreMarker.VoxelMap, out oreWorldPosition);

                    var distanceSquared = Vector3.DistanceSquared((Vector3)oreWorldPosition, (Vector3)((MyEntity)MySession.ControlledEntity).WorldMatrix.Translation);
                    float nearestDistanceSquared = m_nearestDistanceSquared[oreMaterial.Index];
                    if (distanceSquared < nearestDistanceSquared)
                    {
                        m_nearestOreDeposits[oreMaterial.Index] = MyTuple.Create(oreWorldPosition, oreMarker);
                        m_nearestDistanceSquared[oreMaterial.Index] = distanceSquared;
                    }

                    if (false && !debugBoxDrawn)
                    {
                        const int shift = MyOreDetectorComponent.CELL_SIZE_IN_VOXELS_BITS + MyOreDetectorComponent.QUERY_LOD;
                        var worldPosition = oreWorldPosition;
                        Vector3I cellCoord;
                        MyVoxelCoordSystems.WorldPositionToVoxelCoord(oreMarker.VoxelMap.PositionLeftBottomCorner, ref worldPosition, out cellCoord);
                        cellCoord >>= shift;
                        worldPosition = cellCoord * MyOreDetectorComponent.CELL_SIZE_IN_METERS + MyVoxelConstants.VOXEL_SIZE_IN_METRES_HALF;
                        worldPosition += oreMarker.VoxelMap.PositionLeftBottomCorner;
                        var bbox = new BoundingBoxD(worldPosition, worldPosition + MyOreDetectorComponent.CELL_SIZE_IN_METERS);

                        VRageRender.MyRenderProxy.DebugDrawAABB(bbox, Vector3.One, 1f, 1f, false);
                        debugBoxDrawn = true;
                    }
                }
            }

            for (int i = 0; i < m_nearestOreDeposits.Length; i++)
            {
                var nearestOreDeposit = m_nearestOreDeposits[i];
                if (nearestOreDeposit.Item2 == null ||
                    nearestOreDeposit.Item2.VoxelMap == null ||
                    nearestOreDeposit.Item2.VoxelMap.Closed)
                    continue;

                MyVoxelMaterialDefinition voxelMaterial = MyDefinitionManager.Static.GetVoxelMaterialDefinition((byte)i);
                string oreSubtype = voxelMaterial.MinedOre;

                var hudParams = new MyHudEntityParams()
                {
                    FlagsEnum = MyHudIndicatorFlagsEnum.SHOW_ALL,
                    Text = new StringBuilder(oreSubtype),
                    OffsetText = true,
                    Icon = MyHudTexturesEnum.HudOre,
                    IconSize = new Vector2(0.02f, 0.02f)
                };

                m_markerRender.DrawLocationMarker(
                    m_oreHudMarkerStyle,
                    nearestOreDeposit.Item1,
                    hudParams,
                    0, 0);
            }

            DrawTexts();

            ProfilerShort.End();
        }