Phoenix84

voxel scanning

Nov 15th, 2016 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.28 KB | None | 0 0
  1. // A chunk of this is from tyrsis
  2. Dictionary<Vector3D, MyVoxelMaterialDefinition> tempOreList = new Dictionary<Vector3D, MyVoxelMaterialDefinition>(80000);
  3. private void ScanVoxelMapForMaterials(IMyVoxelBase voxelMap, BoundingSphereD sphere, ref Dictionary<Vector3D, MiningInformation> oreList, bool quickScan = false)
  4. {
  5.     long stupidCounterToFindDumbGameBug = 0;
  6.     try
  7.     {
  8.         Logger.Instance.LogMessageOnGameThread("Scanning for ore deposits");
  9.         Vector3I min, max;
  10.         Vector3I numCells;
  11.         var worldMin = sphere.Center - sphere.Radius;
  12.         var worldMax = sphere.Center + sphere.Radius;
  13.         MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref worldMin, out min);
  14.         MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelMap.PositionLeftBottomCorner, ref worldMax, out max);
  15.         stupidCounterToFindDumbGameBug = 1;
  16.         min += (voxelMap as MyVoxelBase).StorageMin;
  17.         max += (voxelMap as MyVoxelBase).StorageMin + 1;
  18.         stupidCounterToFindDumbGameBug = 2;
  19.         var storageSize = voxelMap.Storage.Size - 1;
  20.         Vector3I.Clamp(ref min, ref Vector3I.Zero, ref storageSize, out min);
  21.         Vector3I.Clamp(ref max, ref Vector3I.Zero, ref storageSize, out max);
  22.         numCells = new Vector3I((max.X - min.X) / CELL_SIZE, (max.Y - min.Y) / CELL_SIZE, (max.Z - min.Z) / CELL_SIZE);
  23.         stupidCounterToFindDumbGameBug = 3;
  24.  
  25.         var storage = voxelMap.Storage as VRage.ModAPI.IMyStorage;
  26.         if (storage == null)
  27.             return;
  28.         stupidCounterToFindDumbGameBug = 4;
  29.  
  30.         var cache = Cache;
  31.         cache.Clear(MyStorageDataTypeEnum.Content, 0);
  32.         cache.Clear(MyStorageDataTypeEnum.Material, 0);
  33.         var processedOre = new HashSet<string>();
  34.         stupidCounterToFindDumbGameBug = 5;
  35.  
  36.         for (var itCells = new Vector3I_RangeIterator(ref Vector3I.Zero, ref numCells); itCells.IsValid(); itCells.MoveNext())
  37.         {
  38.             Vector3I cellMinCorner, cellMaxCorner;
  39.             GetCellCorners(ref min, ref max, ref itCells, out cellMinCorner, out cellMaxCorner);
  40.             stupidCounterToFindDumbGameBug = 6;
  41.  
  42.             // Since game can close the stupid Storage on us, check if it's still good.
  43.             if (voxelMap.Storage == null)
  44.             {
  45.                 Logger.Instance.LogMessageOnGameThread("Storage is null");
  46.                 return;                                     // Abort early, we'll be back
  47.             }
  48.  
  49.             var cacheMin = cellMinCorner - 1;
  50.             var cacheMax = cellMaxCorner + 1;
  51.             try
  52.             {
  53.                 voxelMap.Storage.ClampVoxel(ref cacheMin);
  54.                 voxelMap.Storage.ClampVoxel(ref cacheMax);
  55.                 cache.Resize(cacheMin, cacheMax);
  56.                 voxelMap.Storage.ReadRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, 0, cacheMin, cacheMax);
  57.             }
  58.             catch
  59.             {
  60.                 // STUPID STORAGE IS CLOSED!!!!
  61.                 // STUPID GAME BUG!!!!
  62.                 // WAIT 5 SECONDS
  63.                 var waittime = DateTime.Now.AddSeconds(5);
  64.                 while (DateTime.Now < waittime)
  65.                 {
  66.                     if (voxelMap.Storage != null)
  67.                         break;
  68.                 }
  69.  
  70.                 voxelMap.Storage.ClampVoxel(ref cacheMin);
  71.                 voxelMap.Storage.ClampVoxel(ref cacheMax);
  72.                 cache.Resize(cacheMin, cacheMax);
  73.                 voxelMap.Storage.ReadRange(cache, MyStorageDataTypeFlags.ContentAndMaterial, 0, cacheMin, cacheMax);
  74.             }
  75.  
  76.             for (var it = new Vector3I_RangeIterator(ref cellMinCorner, ref cellMaxCorner); it.IsValid(); it.MoveNext())
  77.             {
  78.                 //Logger.Instance.LogMessage("Scanning: " + it.Current.ToString());
  79.                 var relPos = it.Current - cacheMin; // Position of voxel in local space
  80.                 var original = cache.Content(ref relPos); // Content at this position
  81.                 var material = cache.Material(ref relPos); // Material at this position
  82.                 //Logger.Instance.LogMessageOnGameThread("ScanVoxelMapForMaterials 5");
  83.  
  84.                 if (original <= MyVoxelConstants.VOXEL_ISO_LEVEL)
  85.                     continue;
  86.  
  87.                 Vector3D vpos;
  88.  
  89.                 MyVoxelCoordSystems.VoxelCoordToWorldPosition(voxelMap.PositionLeftBottomCorner, ref it.Current, out vpos);
  90.  
  91.                 if (tempOreList.ContainsKey(vpos))
  92.                     continue;
  93.  
  94.                 //var volume = sphere.GetVolume(ref vpos);
  95.                 //if (volume == 0f) // Shape and voxel do not intersect at this position, so continue
  96.                 //    continue;
  97.  
  98.                 // Pull information about voxel required for later processing
  99.                 var voxelMat = MyDefinitionManager.Static.GetVoxelMaterialDefinition(material);
  100.                 if (voxelMat != null && voxelMat.MinedOre != "Stone" && !(voxelMap is MyPlanet && voxelMat.MinedOre == "Ice") &&
  101.                     (!quickScan || (quickScan && ((it.Current.X % 2 == 0 && it.Current.Y % 2 == 0 && it.Current.Z % 2 == 0) ||
  102.                     !processedOre.Contains(voxelMat.MinedOre)))))
  103.                 //if (voxelMat.IsRare)
  104.                 {
  105.                     //Logger.Instance.LogDebug("Found material: " + voxelMat.MinedOre + "; content: " + cache.Content(ref relPos));
  106.                     tempOreList.Add(vpos, voxelMat);
  107.                     processedOre.Add(voxelMat.MinedOre);
  108.                 }
  109.             }
  110.         }
  111.  
  112.         //Logger.Instance.LogDebugOnGameThread("ScanVoxelMapForMaterials: AcquireExclusiveUsing");
  113.         //using (m_voxelInformationLock.AcquireExclusiveUsing())
  114.         //{
  115.  
  116.         foreach (var ore in tempOreList)
  117.             if (!oreList.ContainsKey(ore.Key))
  118.                 oreList.Add(ore.Key, new MiningInformation() { Location = ore.Key, Material = ore.Value, Voxel = voxelMap });
  119.         //}
  120.         //Logger.Instance.LogDebugOnGameThread("ScanVoxelMapForMaterials: ReleaseExclusiveUsing");
  121.         tempOreList.Clear();
  122.     }
  123.     catch
  124.     {
  125.         Logger.Instance.LogMessageOnGameThread("stupidCounterToFindDumbGameBug: " + stupidCounterToFindDumbGameBug);
  126.         throw;
  127.     }
  128.     Logger.Instance.LogMessageOnGameThread(string.Format("Total voxels with ore: {0}", oreList.Count));
  129. }
Advertisement
Add Comment
Please, Sign In to add comment