void Test()
        {
            var t = new Vector3D(-0.75, 0, 0.5);

            detector.SetValue("RaycastTarget", t);
            if (detector.GetValue <Vector3D>("RaycastTarget") != t)
            {
                throw new Exception("RaycastTarget inequal");
            }
            detector.SetValue("RaycastTarget", detector.GetPosition() + detector.WorldMatrix.Forward);
            var res = detector.GetValue <MyDetectedEntityInfo>("RaycastResult");

            if (res.TimeStamp == 0)
            {
                throw new Exception("RaycastResult invalid");
            }
            string blackList = "Stone,Ice";

            detector.SetValue("OreBlacklist", blackList);
            if (detector.GetValue <string>("OreBlacklist") != blackList)
            {
                throw new Exception("OreBlacklist inequal");
            }
            try
            {
                detector.SetValue("ScanEpoch", 0L);
            }
            catch
            {
                return;
            }
            throw new Exception("shouldnt be able to write ScanEpoch");
        }
Esempio n. 2
0
        /// <summary>
        /// Searches nearby voxels for ores.
        /// </summary>
        /// <param name="position">Position of autopilot block</param>
        /// <param name="oreType">Ore types to search for</param>
        /// <param name="onComplete">Invoked iff an ore is found</param>
        /// <returns>true if an ore is found</returns>
        private bool GetOreLocations(Vector3D position, byte[] oreType, OreSearchComplete onComplete)
        {
            Log.DebugLog("entered GetOreLocations()");

            using (l_getOreLocations.AcquireExclusiveUsing())
            {
                BoundingSphereD detection = new BoundingSphereD(m_oreDetector.GetPosition(), m_maxRange);
                m_nearbyVoxel.Clear();
                MyGamePruningStructure.GetAllVoxelMapsInSphere(ref detection, m_nearbyVoxel);

                IOrderedEnumerable <MyVoxelBase> sorted = m_nearbyVoxel.OrderBy(voxel => Vector3D.DistanceSquared(voxel.GetCentre(), position));

                foreach (IMyVoxelBase nearbyMap in sorted)
                {
                    if (nearbyMap is IMyVoxelMap || nearbyMap is MyPlanet)
                    {
                        VoxelData data;
                        using (l_voxelData.AcquireExclusiveUsing())
                        {
                            if (!m_voxelData.TryGetValue(nearbyMap.EntityId, out data))
                            {
                                data = new VoxelData(m_oreDetector, nearbyMap, m_maxRange);
                                m_voxelData.Add(nearbyMap.EntityId, data);
                            }
                        }
                        if (data.NeedsUpdate)
                        {
                            Log.DebugLog("Data needs to be updated for " + nearbyMap.getBestName());
                            data.Read();
                        }
                        else
                        {
                            Log.DebugLog("Old data OK for " + nearbyMap.getBestName());
                        }

                        IEnumerable <Vector3D> positions;
                        byte foundOre;
                        if (data.GetRandom(oreType, ref position, out foundOre, out positions))
                        {
                            //Log.DebugLog("PositionLeftBottomCorner: " + nearbyMap.PositionLeftBottomCorner + ", worldPosition: " + closest + ", distance: " + Vector3D.Distance(position, closest));
                            string oreName = MyDefinitionManager.Static.GetVoxelMaterialDefinition(foundOre).MinedOre;
                            onComplete(true, nearbyMap, oreName, positions);
                            m_nearbyVoxel.Clear();
                            return(true);
                        }
                        else
                        {
                            Log.DebugLog("No ore found");
                        }
                    }
                }

                m_nearbyVoxel.Clear();
                return(false);
            }
        }
        public void UpdateStatus()
        {
            if (!m_block.Enabled || !m_block.IsFunctional || !Sink.IsPoweredByType(MyResourceDistributorComponent.ElectricityId) || m_tooCloseToOtherDetector)
            {
                m_detectorState = DetectorStates.Disabled;
            }

            else if (m_depositGroupsByEntity.Count == 0)
            {
                m_detectorState = DetectorStates.Enabled;
            }

            if (m_detectorState != m_lastDetectorState || m_tooCloseToOtherDetector != m_tooCloseOld)
            {
                m_tooCloseOld       = m_tooCloseToOtherDetector;
                m_lastDetectorState = m_detectorState;
                MessageHub.SendToPlayerInSyncRange(new MessageOreDetectorStateChange()
                {
                    EntityId = m_block.EntityId,
                    State    = m_lastDetectorState,
                    TooClose = m_tooCloseToOtherDetector
                }, m_block.GetPosition());
            }
        }
Esempio n. 4
0
            /// <summary>
            /// Start the reads if it is not already running.
            /// </summary>
            /// <param name="onFinished">Invoked when reads finish, not invoked if already running.</param>
            /// <returns>True if started, false if already running.</returns>
            public void Read()
            {
                Profiler.StartProfileBlock();
                m_throwOutVoxelData = Globals.ElapsedTime + Static.LifeSpan_VoxelData;

                NeedsUpdate = false;
                Vector3D m_oreDetectorPosition = m_oreDetector.GetPosition();
                Vector3D worldMin = m_oreDetectorPosition - m_maxRange;
                Vector3D worldMax = m_oreDetectorPosition + m_maxRange;

                float rangeSquared = m_maxRange * m_maxRange;

                Vector3I odPosVoxelStorage, m_localMin, m_localMax;

                MyVoxelCoordSystems.WorldPositionToVoxelCoord(m_voxel.PositionLeftBottomCorner, ref m_oreDetectorPosition, out odPosVoxelStorage);
                MyVoxelCoordSystems.WorldPositionToVoxelCoord(m_voxel.PositionLeftBottomCorner, ref worldMin, out m_localMin);
                MyVoxelCoordSystems.WorldPositionToVoxelCoord(m_voxel.PositionLeftBottomCorner, ref worldMax, out m_localMax);

                MyVoxelBase vox = m_voxel as MyVoxelBase;

                if (m_voxel == null || m_voxel.Storage == null)
                {
                    Profiler.EndProfileBlock();
                    return;
                }

                m_localMin          = Vector3I.Clamp(m_localMin, vox.StorageMin, vox.StorageMax);
                m_localMax          = Vector3I.Clamp(m_localMax, vox.StorageMin, vox.StorageMax);
                m_localMin        >>= QUERY_LOD;
                m_localMax        >>= QUERY_LOD;
                odPosVoxelStorage >>= QUERY_LOD;
                Log.DebugLog("minLocal: " + m_localMin + ", maxLocal: " + m_localMax + ", odPosVoxelStorage: " + odPosVoxelStorage);

                Vector3I size = m_localMax - m_localMin;

                Log.DebugLog("number of coords in box: " + (size.X + 1) * (size.Y + 1) * (size.Z + 1));
                //ulong processed = 0;
                foreach (List <Vector3I> locations in m_materialLocations2.Values)
                {
                    locations.Clear();
                }

                Vector3I vector = new Vector3I();

                for (vector.X = m_localMin.X; vector.X < m_localMax.X; vector.X += QUERY_STEP)
                {
                    for (vector.Y = m_localMin.Y; vector.Y < m_localMax.Y; vector.Y += QUERY_STEP)
                    {
                        for (vector.Z = m_localMin.Z; vector.Z < m_localMax.Z; vector.Z += QUERY_STEP)
                        {
                            if (vector.DistanceSquared(odPosVoxelStorage) <= rangeSquared)
                            {
                                m_voxel.Storage.ReadRange(m_storage, MyStorageDataTypeFlags.ContentAndMaterial, QUERY_LOD, vector, vector + QUERY_MAX);

                                Vector3I index  = Vector3I.Zero;
                                Vector3I size3D = m_storage.Size3D;
                                for (index.X = 0; index.X < size3D.X; index.X++)
                                {
                                    for (index.Y = 0; index.Y < size3D.Y; index.Y++)
                                    {
                                        for (index.Z = 0; index.Z < size3D.Z; index.Z++)
                                        {
                                            int linear = m_storage.ComputeLinear(ref index);
                                            if (m_storage.Content(linear) > MyVoxelConstants.VOXEL_ISO_LEVEL)
                                            {
                                                byte mat = m_storage.Material(linear);
                                                if (Static.RareMaterials[mat])
                                                {
                                                    //Log.DebugLog("mat: " + mat + ", content: " + m_storage.Content(linear) + ", vector: " + vector + ", position: " + vector + index
                                                    //	+ ", name: " + MyDefinitionManager.Static.GetVoxelMaterialDefinition(mat).MinedOre, "Read()");
                                                    //m_materialLocations[vector + index] = mat;

                                                    List <Vector3I> locations;
                                                    if (!m_materialLocations2.TryGetValue(mat, out locations))
                                                    {
                                                        locations = new List <Vector3I>(1000);
                                                        m_materialLocations2.Add(mat, locations);
                                                    }
                                                    locations.Add(vector + index);

                                                    //processed++;
                                                    goto Finished_Deposit;
                                                }
                                            }
                                        }
                                    }
                                }

                                Finished_Deposit :;
                                //processed++;
                            }
                        }
                    }
                }

                //Log.DebugLog("read " + processed + ", chunks" + ", number of mats: " + m_materialLocations.Count, Logger.severity.DEBUG);
                Profiler.EndProfileBlock();
            }