Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void LoadAnchorChunks() {
- foreach (var anchor in anchors)
- {
- var anchorChunksToLoad = new List<WorldPos>();
- var anchorChunk = ChunkInWorld(new WorldPos(anchor.position));
- //TODO switch to not reiterate over insides and do edges only.
- for(int i = 0; i <= CHUNK_LOAD_DISTANCE; i++)
- {
- for (var x = -i; x <= i; x++)
- {
- for (var y = -i; y <= i; y++)
- {
- for (var z = -i; z <= i; z++)
- {
- //TODO skip these iterations
- if(x > -i && x < i && y > -i && y < i && z > -i && z < i)
- continue;
- var chunkPos = anchorChunk + (new WorldPos(x, y, z) * ChunkInstance.CHUNK_SIZE);
- if (anchorChunksToLoad.Contains(chunkPos))
- continue;
- int hashCode = chunkPos.GetHashCode();
- bool populated = _populatedChunks.Contains(hashCode);
- if (playerChunksMap.Contains(hashCode) || _chunksReadyToAdd.ContainsKey(hashCode) || populated)
- {
- if (populated)
- {
- //Reset chunk time so it will not be unloaded
- ChunkInstance chunk;
- if (loadedChunksMap.TryGetValue(chunkPos.GetHashCode(), out chunk))
- chunk.Time = DateTime.Now;
- }
- continue;
- }
- anchorChunksToLoad.Add(chunkPos);
- //Cap player chunk load tasks to 16
- if (anchorChunksToLoad.Count >= 16)
- {
- x = y = z = i = CHUNK_LOAD_DISTANCE + 1;
- }
- }
- }
- }
- }
- }
- for (int i = 0; i < anchorChunksToLoad.Count; i++) {
- playerChunksMap.Add(anchorChunksToLoad[i].GetHashCode());
- }
- playerChunksLists.Add(anchorChunksToLoad);
- }
- var merged = ExtensionHelper.ZipMerge(playerChunksLists.ToArray());
- foreach (var chunkPos in merged)
- {
- var cachedChunk = LoadChunk(chunkPos);
- int hashCode = chunkPos.GetHashCode();
- lock (_chunksReadyToAdd) {
- if (_chunksReadyToAdd.ContainsKey(hashCode))
- {
- Debug.LogError("_chunksReadyToAdd ALREADY CONTAINS KEY");
- }
- else
- {
- _chunksReadyToAdd.Add(hashCode, cachedChunk);
- }
- }
- chunksWaitingForNeighbours.Add(chunkPos);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment