This repository has been archived on 2024-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
TrueCraft/TrueCraft.Core/TerrainGen/BiomeRepository.cs
Drew DeVault 362c852f51 Many improvements to server stability+performance
Sorry for the vague commit message. There were a lot of changes. Here's
a list of most of them:

- Lighting updates are timeboxed each frame
- The next environment frame is queued sooner if the current one took
  longer (as soon as immediately)
- Issues with the physics engines and mobs using it were (mostly) fixed,
  mobs no longer freak out and get stuck on physics objects
- Mob AI/pathfinding is done more intelligently
- The player can no longer spawn in an ocean or a desert biome
- Some deadlocks in Region were fixed (more remain to be fixed)

The current performance bottlenecks are lighting (still) and propegating
initial chunk loads to blocks that need to schedule things (such as
grass blocks). I think the main culprit in the latter case is grass
blocks and water blocks. The former can be improved by adding a block
cache to World, but that'll take some custom work. This step is just
gonna be slow no matter what, we might have to split it across several
frames but it's never going to be great.

There still seems to be a deadlock somewhere in all of this mess, in the
world code. I'll find it later.
2017-05-22 19:51:23 -04:00

109 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.API.World;
using TrueCraft.Core.TerrainGen.Biomes;
using TrueCraft.Core.TerrainGen.Noise;
using System.Reflection;
namespace TrueCraft.Core.TerrainGen
{
public class BiomeRepository : IBiomeRepository
{
private readonly IBiomeProvider[] BiomeProviders = new IBiomeProvider[0x100];
public BiomeRepository()
{
DiscoverBiomes();
}
internal void DiscoverBiomes()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
foreach (var type in assembly.GetTypes().Where(t => typeof(IBiomeProvider).IsAssignableFrom(t) && !t.IsAbstract))
{
var instance = (IBiomeProvider)Activator.CreateInstance(type);
RegisterBiomeProvider(instance);
}
}
catch
{
// There are some bugs with loading mscorlib during a unit test like this
}
}
}
public void RegisterBiomeProvider(IBiomeProvider provider)
{
BiomeProviders[provider.ID] = provider;
}
public IBiomeProvider GetBiome(byte id)
{
return BiomeProviders[id];
}
public IBiomeProvider GetBiome(double temperature, double rainfall, bool spawn)
{
List<IBiomeProvider> temperatureResults = new List<IBiomeProvider>();
foreach (var biome in BiomeProviders)
{
if (biome != null && biome.Temperature.Equals(temperature))
{
temperatureResults.Add(biome);
}
}
if (temperatureResults.Count.Equals(0))
{
IBiomeProvider provider = null;
float temperatureDifference = 100.0f;
foreach (var biome in BiomeProviders)
{
if (biome != null)
{
var Difference = Math.Abs(temperature - biome.Temperature);
if (provider == null || Difference < temperatureDifference)
{
provider = biome;
temperatureDifference = (float)Difference;
}
}
}
temperatureResults.Add(provider);
}
foreach (var biome in BiomeProviders)
{
if (biome != null
&& biome.Rainfall.Equals(rainfall)
&& temperatureResults.Contains(biome)
&& (!spawn || biome.Spawn))
{
return biome;
}
}
IBiomeProvider biomeProvider = null;
float rainfallDifference = 100.0f;
foreach (var biome in BiomeProviders)
{
if (biome != null)
{
var difference = Math.Abs(temperature - biome.Temperature);
if ((biomeProvider == null || difference < rainfallDifference)
&& (!spawn || biome.Spawn))
{
biomeProvider = biome;
rainfallDifference = (float)difference;
}
}
}
return biomeProvider ?? new PlainsBiome();
}
}
}