Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A chunk of this is from tyrsis
- Dictionary<Vector3D, MyVoxelMaterialDefinition> tempOreList = new Dictionary<Vector3D, MyVoxelMaterialDefinition>(80000);
- private void ScanVoxelMapForMaterials(IMyVoxelBase voxelMap, BoundingSphereD sphere, ref Dictionary<Vector3D, MiningInformation> oreList, bool quickScan = false)
- {
- long stupidCounterToFindDumbGameBug = 0;
- try
- {
- Logger.Instance.LogMessageOnGameThread("Scanning for ore deposits");
- Vector3I min, max;
- Vector3I numCells;
- var worldMin = sphere.Center - sphere.Radius;
- var worldMax = sphere.Center + sphere.Radius;
- MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref worldMin, out min);
- MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref worldMax, out max);
- stupidCounterToFindDumbGameBug = 1;
- min += (voxelMap as MyVoxelBase).StorageMin;
- max += (voxelMap as MyVoxelBase).StorageMin + 1;
- stupidCounterToFindDumbGameBug = 2;
- var storageSize = voxelMap.Storage.Size - 1;
- Vector3I.Clamp(ref min, ref Vector3I.Zero, ref storageSize, out min);
- Vector3I.Clamp(ref max, ref Vector3I.Zero, ref storageSize, out max);
- numCells = new Vector3I((max.X - min.X) / CELL_SIZE, (max.Y - min.Y) / CELL_SIZE, (max.Z - min.Z) / CELL_SIZE);
- stupidCounterToFindDumbGameBug = 3;
- var storage = voxelMap.Storage as VRage.ModAPI.IMyStorage;
- if (storage == null)
- return;
- stupidCounterToFindDumbGameBug = 4;
- var cache = Cache;
- cache.Clear(MyStorageDataTypeEnum.Content, 0);
- cache.Clear(MyStorageDataTypeEnum.Material, 0);
- var processedOre = new HashSet<string>();
- stupidCounterToFindDumbGameBug = 5;
- for (var itCells = new Vector3I_RangeIterator(ref Vector3I.Zero, ref numCells); itCells.IsValid(); itCells.MoveNext())
- {
- Vector3I cellMinCorner, cellMaxCorner;
- GetCellCorners(ref min, ref max, ref itCells, out cellMinCorner, out cellMaxCorner);
- stupidCounterToFindDumbGameBug = 6;
- // Since game can close the stupid Storage on us, check if it's still good.
- if (voxelMap.Storage == null)
- {
- Logger.Instance.LogMessageOnGameThread("Storage is null");
- return; // Abort early, we'll be back
- }
- var cacheMin = cellMinCorner - 1;
- var cacheMax = cellMaxCorner + 1;
- try
- {
- voxelMap.Storage.ClampVoxel(ref cacheMin);
- voxelMap.Storage.ClampVoxel(ref cacheMax);
- cache.Resize(cacheMin, cacheMax);
- voxelMap.Storage.ReadRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, 0, cacheMin, cacheMax);
- }
- catch
- {
- // STUPID STORAGE IS CLOSED!!!!
- // STUPID GAME BUG!!!!
- // WAIT 5 SECONDS
- var waittime = DateTime.Now.AddSeconds(5);
- while (DateTime.Now < waittime)
- {
- if (voxelMap.Storage != null)
- break;
- }
- voxelMap.Storage.ClampVoxel(ref cacheMin);
- voxelMap.Storage.ClampVoxel(ref cacheMax);
- cache.Resize(cacheMin, cacheMax);
- voxelMap.Storage.ReadRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, 0, cacheMin, cacheMax);
- }
- for (var it = new Vector3I_RangeIterator(ref cellMinCorner, ref cellMaxCorner); it.IsValid(); it.MoveNext())
- {
- //Logger.Instance.LogMessage("Scanning: " + it.Current.ToString());
- var relPos = it.Current - cacheMin; // Position of voxel in local space
- var original = cache.Content(ref relPos); // Content at this position
- var material = cache.Material(ref relPos); // Material at this position
- //Logger.Instance.LogMessageOnGameThread("ScanVoxelMapForMaterials 5");
- if (original <= MyVoxelConstants.VOXEL_ISO_LEVEL)
- continue;
- Vector3D vpos;
- MyVoxelCoordSystems.VoxelCoordToWorldPosition(voxelMap.PositionLeftBottomCorner, ref it.Current, out vpos);
- if (tempOreList.ContainsKey(vpos))
- continue;
- //var volume = sphere.GetVolume(ref vpos);
- //if (volume == 0f) // Shape and voxel do not intersect at this position, so continue
- // continue;
- // Pull information about voxel required for later processing
- var voxelMat = MyDefinitionManager.Static.GetVoxelMaterialDefinition(material);
- if (voxelMat != null && voxelMat.MinedOre != "Stone" && !(voxelMap is MyPlanet && voxelMat.MinedOre == "Ice") &&
- (!quickScan || (quickScan && ((it.Current.X % 2 == 0 && it.Current.Y % 2 == 0 && it.Current.Z % 2 == 0) ||
- !processedOre.Contains(voxelMat.MinedOre)))))
- //if (voxelMat.IsRare)
- {
- //Logger.Instance.LogDebug("Found material: " + voxelMat.MinedOre + "; content: " + cache.Content(ref relPos));
- tempOreList.Add(vpos, voxelMat);
- processedOre.Add(voxelMat.MinedOre);
- }
- }
- }
- //Logger.Instance.LogDebugOnGameThread("ScanVoxelMapForMaterials: AcquireExclusiveUsing");
- //using (m_voxelInformationLock.AcquireExclusiveUsing())
- //{
- foreach (var ore in tempOreList)
- if (!oreList.ContainsKey(ore.Key))
- oreList.Add(ore.Key, new MiningInformation() { Location = ore.Key, Material = ore.Value, Voxel = voxelMap });
- //}
- //Logger.Instance.LogDebugOnGameThread("ScanVoxelMapForMaterials: ReleaseExclusiveUsing");
- tempOreList.Clear();
- }
- catch
- {
- Logger.Instance.LogMessageOnGameThread("stupidCounterToFindDumbGameBug: " + stupidCounterToFindDumbGameBug);
- throw;
- }
- Logger.Instance.LogMessageOnGameThread(string.Format("Total voxels with ore: {0}", oreList.Count));
- }
Advertisement
Add Comment
Please, Sign In to add comment