zrrz111

Untitled

Apr 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. void LoadAnchorChunks() {  
  2.     foreach (var anchor in anchors)
  3.     {
  4.         var anchorChunksToLoad = new List<WorldPos>();
  5.         var anchorChunk = ChunkInWorld(new WorldPos(anchor.position));
  6.  
  7.         //TODO switch to not reiterate over insides and do edges only.
  8.  
  9.         for(int i = 0; i <= CHUNK_LOAD_DISTANCE; i++)
  10.         {
  11.             for (var x = -i; x <= i; x++)
  12.             {
  13.                 for (var y = -i; y <= i; y++)
  14.                 {
  15.                     for (var z = -i; z <= i; z++)
  16.                     {
  17.                         //TODO skip these iterations
  18.                         if(x > -i && x < i && y > -i && y < i && z > -i && z < i)
  19.                             continue;
  20.                                
  21.                         var chunkPos = anchorChunk + (new WorldPos(x, y, z) * ChunkInstance.CHUNK_SIZE);
  22.                         if (anchorChunksToLoad.Contains(chunkPos))
  23.                             continue;
  24.  
  25.                         int hashCode = chunkPos.GetHashCode();
  26.                         bool populated = _populatedChunks.Contains(hashCode);
  27.                         if (playerChunksMap.Contains(hashCode) || _chunksReadyToAdd.ContainsKey(hashCode) || populated)
  28.                         {
  29.                             if (populated)
  30.                             {
  31.                                 //Reset chunk time so it will not be unloaded
  32.                                 ChunkInstance chunk;
  33.                                 if (loadedChunksMap.TryGetValue(chunkPos.GetHashCode(), out chunk))
  34.                                     chunk.Time = DateTime.Now;
  35.                                 }
  36.                                 continue;
  37.                             }
  38.  
  39.                             anchorChunksToLoad.Add(chunkPos);
  40.  
  41.                             //Cap player chunk load tasks to 16
  42.                             if (anchorChunksToLoad.Count >= 16)
  43.                             {
  44.                                 x = y = z = i = CHUNK_LOAD_DISTANCE + 1;
  45.                             }
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.  
  52.         for (int i = 0; i < anchorChunksToLoad.Count; i++) {
  53.             playerChunksMap.Add(anchorChunksToLoad[i].GetHashCode());
  54.         }
  55.         playerChunksLists.Add(anchorChunksToLoad);
  56.     }
  57.  
  58.     var merged = ExtensionHelper.ZipMerge(playerChunksLists.ToArray());
  59.     foreach (var chunkPos in merged)
  60.     {
  61.         var cachedChunk = LoadChunk(chunkPos);
  62.  
  63.         int hashCode = chunkPos.GetHashCode();
  64.  
  65.         lock (_chunksReadyToAdd) {
  66.             if (_chunksReadyToAdd.ContainsKey(hashCode))
  67.             {
  68.                 Debug.LogError("_chunksReadyToAdd ALREADY CONTAINS KEY");
  69.             }
  70.             else
  71.             {
  72.                 _chunksReadyToAdd.Add(hashCode, cachedChunk);
  73.             }
  74.         }
  75.         chunksWaitingForNeighbours.Add(chunkPos);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment