Created
June 2, 2013 19:19
-
-
Save anonymous/5694573 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections.Generic; | |
using System.IO; | |
using TinkerWorX.AccidentalNoiseLibrary; | |
#region Conditional using statements | |
#if SILVERLIGHT | |
using Microsoft.Xna.Framework; | |
#endif | |
#if SERVER | |
using SlimMath; | |
#endif | |
#endregion Conditional using statements | |
namespace TinkerWorX.Legeria.Shared.TileWorld | |
{ | |
public class ProceduralEarth : Realm | |
{ | |
private const Int32 QUEUE_LIMIT = 50000; | |
private const Int32 TILE_AIR = 1; | |
private static readonly Tile TileAir = new Tile { BackType = TileBackType.None, BaseType = TileBaseType.Air }; | |
private const Int32 TILE_DIRT = 2; | |
private static readonly Tile TileDirt = new Tile { BackType = TileBackType.Dirt, BaseType = TileBaseType.Dirt }; | |
private const Int32 TILE_STONE = 3; | |
private static readonly Tile TileStone = new Tile { BackType = TileBackType.Dirt, BaseType = TileBaseType.Stone }; | |
private const Int32 TILE_COPPER = 4; | |
private static readonly Tile TileCopper = new Tile { BackType = TileBackType.Dirt, BaseType = TileBaseType.Copper }; | |
private const String XML = | |
@"<chain source=""ground_select""> | |
<module type=""constant"" name=""tile_air"" value=""1.00"" /> | |
<module type=""constant"" name=""tile_dirt"" value=""2.00"" /> | |
<module type=""constant"" name=""tile_stone"" value=""3.00"" /> | |
<module type=""constant"" name=""tile_copper"" value=""4.00"" /> | |
<module type=""constant"" name=""tile_silver"" value=""5.00"" /> | |
<module type=""constant"" name=""tile_gold"" value=""6.00"" /> | |
<module type=""constant"" name=""tile_bedrock"" value=""255.00"" /> | |
<module type=""gradient"" name=""contour_gradient"" x0=""0.00"" x1=""0.00"" y0=""0.00"" y1=""1.00"" /> | |
<module type=""fractal"" name=""contour_fractal"" fractal=""fbm"" basis=""gradient"" interpolation=""quintic"" octaves=""3"" frequency=""2.00"" /> | |
<module type=""cache"" name=""contour_fractal_cache"" source=""contour_fractal"" /> | |
<module type=""scale_offset"" name=""contour_scale"" source=""contour_fractal_cache"" scale=""0.50"" offset=""0.00"" /> | |
<module type=""scale_domain"" name=""contour_scale_y"" source=""contour_scale"" ys=""0.00"" /> | |
<module type=""translate_domain"" name=""contour_perturb"" source=""contour_gradient"" ya=""contour_scale_y"" /> | |
<module type=""scale_offset"" name=""contour_overhang_scale"" source=""contour_fractal_cache"" scale=""0.50"" offset=""0.00"" /> | |
<module type=""translate_domain"" name=""contour_overhang_perturb"" source=""contour_perturb"" xa=""contour_overhang_scale"" /> | |
<module type=""gradient"" name=""ground_gradient"" x0=""0.00"" x1=""0.00"" y0=""0.00"" y1=""0.90"" /> | |
<module type=""clamp"" name=""ground_gradient_clamp"" source=""ground_gradient"" low=""0.00"" high=""1.00"" /> | |
<module type=""fractal"" name=""ground_fractal"" fractal=""ridged_multi"" basis=""gradient"" interpolation=""quintic"" octaves=""3"" frequency=""10.00"" /> | |
<module type=""combiner"" name=""ground_gradient_combiner"" combiner_type=""multiply""> | |
<source>ground_fractal</source> | |
<source>ground_gradient_clamp</source> | |
</module> | |
<module type=""select"" name=""ground_1"" source=""ground_gradient_combiner"" low=""tile_dirt"" high=""tile_stone"" threshold=""0.50"" /> | |
<module type=""select"" name=""ground_2"" source=""ground_gradient_combiner"" low=""ground_1"" high=""tile_stone"" threshold=""0.80"" /> | |
<module type=""select"" name=""ground_select"" source=""contour_overhang_perturb"" low=""tile_air"" high=""ground_2"" threshold=""0.50"" /> | |
</chain> | |
"; | |
private readonly ImplicitXmlChain terrain = ImplicitXmlChain.FromString(XML); | |
private readonly Dictionary<HashPoint, Tile> cache = new Dictionary<HashPoint, Tile>(); | |
private readonly Queue<HashPoint> cacheQueue = new Queue<HashPoint>(); | |
private void CacheTile(HashPoint tilePoint, Tile tile) | |
{ | |
while (this.cacheQueue.Count >= QUEUE_LIMIT) | |
{ | |
var oldTileCachePoint = this.cacheQueue.Dequeue(); | |
this.cache.Remove(oldTileCachePoint); | |
} | |
this.cache.Add(tilePoint, tile); | |
this.cacheQueue.Enqueue(tilePoint); | |
} | |
public override Vector2 Gravity | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public override Single Airdrag | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public override Color AmbientLight | |
{ | |
get { return new Color(1.00f, 1.00f, 1.00f, 1.00f); } | |
} | |
public override Color MinimumLight | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public override Vector2 SpawnPoint | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public override IEnumerable<Tile> Tiles | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
private Tile GetTileProcedural(Int32 x, Int32 y) | |
{ | |
switch ((Int32)this.terrain.Get(x / 256.00, y / 256.00)) | |
{ | |
case TILE_AIR: return TileAir; | |
case TILE_DIRT: return TileDirt; | |
case TILE_STONE: return TileStone; | |
case TILE_COPPER: return TileCopper; | |
default: return Tile.Void; | |
} | |
} | |
public override Tile GetTile(Int32 x, Int32 y) | |
{ | |
var tilePoint = new HashPoint(x, y); | |
Tile tile; | |
if (!cache.TryGetValue(tilePoint, out tile)) | |
{ | |
tile = this.GetTileProcedural(x, y); | |
this.CacheTile(tilePoint, tile); | |
} | |
return tile; | |
} | |
public override Boolean SetTile(Int32 x, Int32 y, Tile tile) | |
{ | |
var tileCachePoint = new HashPoint(x, y); | |
Tile oldTile; | |
if (cache.ContainsKey(tileCachePoint)) | |
{ | |
oldTile = cache[tileCachePoint]; | |
cache.Remove(tileCachePoint); | |
} | |
else | |
{ | |
oldTile = this.GetTile(x, y); | |
} | |
this.CacheTile(tileCachePoint, tile); | |
this.OnTileChanged(x, y, oldTile, tile); | |
return false; | |
} | |
public override void SaveTiles(Stream stream) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override void LoadTiles(Stream stream) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment