Created
July 4, 2017 20:46
-
-
Save Brian1KB/0ab0e007ba5678a6130d0faf804b841c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MiNET.Utils; | |
using MiNET.Worlds; | |
namespace Network | |
{ | |
public class AnvilProviderFactory | |
{ | |
private static readonly Dictionary<string, AnvilWorldProvider> ProviderCache = new Dictionary<string, AnvilWorldProvider>(); | |
public static AnvilWorldProvider GetLevelProvider(string levelDir, bool readOnly = true, bool cache = true) | |
{ | |
AnvilWorldProvider provider; | |
ProviderCache.TryGetValue(levelDir, out provider); | |
if (provider != null) return readOnly ? provider : (AnvilWorldProvider)provider.Clone(); | |
provider = new AnvilWorldProvider(levelDir); | |
var level = new Level(Network.LevelManager, "cache", provider, new EntityManager(), viewDistance: 20) | |
{ | |
EnableBlockTicking = false, | |
EnableChunkTicking = false | |
}; | |
level.Initialize(); | |
RecalculateLight(level, provider); | |
var processedProvider = (AnvilWorldProvider) provider.Clone(); | |
level.WorldProvider = null; | |
level.Close(); | |
processedProvider.PruneAir(); | |
processedProvider.MakeAirChunksAroundWorldToCompensateForBadRendering(); | |
InsulateChunks(processedProvider); | |
if (cache) ProviderCache.Add(levelDir, processedProvider); | |
return readOnly ? processedProvider : (AnvilWorldProvider) processedProvider.Clone(); | |
} | |
private static void InsulateChunks(AnvilWorldProvider provider, int radius = 3) | |
{ | |
var spawn = new ChunkCoordinates(provider.GetSpawnPoint()); | |
for (var x = -radius; x < radius; x++) | |
{ | |
for (var z = -radius; z < radius; z++) | |
{ | |
ChunkColumn column; | |
var location = new ChunkCoordinates(spawn.X + x, spawn.Z + z); | |
provider._chunkCache.TryGetValue(location, out column); | |
if (column != null) continue; | |
column = new ChunkColumn | |
{ | |
isAllAir = true, | |
x = location.X, | |
z = location.Z | |
}; | |
column.GetBatch(); | |
provider._chunkCache[location] = column; | |
} | |
} | |
} | |
private static void RecalculateLight(Level level, AnvilWorldProvider anvilWorldProvider) | |
{ | |
SkyLightCalculations.Calculate(level); | |
while (anvilWorldProvider.LightSources.Count > 0) | |
{ | |
var block = anvilWorldProvider.LightSources.Dequeue(); | |
block = level.GetBlock(block.Coordinates); | |
BlockLightCalculations.Calculate(level, block); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment