Guest User

Untitled

a guest
May 4th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.72 KB | None | 0 0
  1. #region Using Statements
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Microsoft.Xna.Framework;
  7. using System.Threading;
  8. using Shared;
  9. #endregion
  10.  
  11. namespace Craft
  12. {
  13.     public class SimpleTerrain
  14.     {
  15.         #region Fields
  16.         public static Random r = new Random(SEED);
  17.         public static int SEED = 1984;
  18.         public const byte SIZEX = BlockFunctions.SIZEX;
  19.         public const byte SIZEY = BlockFunctions.SIZEY;
  20.         public const byte SIZEZ = BlockFunctions.SIZEZ;
  21.  
  22.         public const int WATERLEVEL = 48;
  23.         public const int SNOWLEVEL = 95;
  24.         public const int MINIMUMGROUNDHEIGHT = 32;
  25.         #endregion
  26.  
  27.         Region region;
  28.  
  29.         public Region Region
  30.         {
  31.             get { return region; }
  32.             set { region = value; }
  33.         }
  34.         public SimpleTerrain() { }
  35.         public SimpleTerrain(Region region)
  36.         {
  37.             this.region = region;
  38.             ThreadPool.QueueUserWorkItem(GenerateTerrain);
  39.         }
  40.         /// <summary>
  41.         /// Generate terrain from code
  42.         /// </summary>
  43.         public void GenerateTerrain()
  44.         {
  45.             int tX = ((int)region.Position.X / (BlockFunctions.SCALE * 16)) + SEED;
  46.             int tZ = ((int)region.Position.Z / (BlockFunctions.SCALE * 16));
  47.             //Console.WriteLine("Thread Started");
  48.             float temperature = PerlinSimplexNoise.noise(tX * 0.1f, tZ * 0.1f);
  49.             region.BlockTypes = new BlockType[SIZEX, SIZEY, SIZEZ];
  50.             for (byte x = 0; x < SIZEX; x++)
  51.             {
  52.                 int worldX = ((int)region.Position.X / BlockFunctions.SCALE) + x + SEED;
  53.                 for (byte z = 0; z < SIZEZ; z++)
  54.                 {
  55.                     int worldZ = ((int)region.Position.Z / BlockFunctions.SCALE) + z;
  56.                     //Console.WriteLine((worldX - SEED)  + " " + worldZ);
  57.                     GenerateTerrain(x, z, worldX, worldZ, tX, tZ);
  58.                 }
  59.             }
  60.             region.RegionState = Region.RegionStates.Partial;
  61.             Region.loading = false;
  62.             Region.loadTime = TimeSpan.Zero;
  63.         }
  64.         /// <summary>
  65.         /// Check if region is available on disk. Else generate new terrain from code.
  66.         /// </summary>
  67.         /// <param name="o"></param>
  68.         public void GenerateTerrain(object o)
  69.         {
  70.             //bool opened = Integrity.OpenRegion(region);
  71.             //if (opened)
  72.             //{
  73.             //    GenerateBlocks();
  74.             //    BlockFunctions.CreateFaces(region);
  75.             //    region.BlockRenderer.UpdateVertexBuffers();
  76.  
  77.             //    region.RegionState = Region.RegionStates.Loaded;
  78.             //}
  79.             //else
  80.             //{
  81.             GenerateTerrain();
  82.             //}
  83.         }
  84.         /// <summary>
  85.         /// Multiplayer
  86.         /// </summary>
  87.         /// <param name="o"></param>
  88.         public void GenerateBlocks(object o)
  89.         {
  90.             //GenerateBlocks();
  91.             BlockFunctions.CreateFaces(region);
  92.  
  93.             region.RegionState = Region.RegionStates.Loaded;
  94.         }
  95.         protected virtual void GenerateTerrain(byte x, byte z, int worldX, int worldZ, float tempX, float tempZ)
  96.         {
  97.             int minimumGroundDepth = 32;
  98.  
  99.             float temp1 = PerlinSimplexNoise.noise(worldX * 0.01f, worldZ * 0.01f) * 0.5f;
  100.             float temp2 = PerlinSimplexNoise.noise(worldX * 0.0005f, worldZ * 0.0005f) * 0.25f;
  101.             float temp3 = PerlinSimplexNoise.noise(worldX * 0.005f, worldZ * 0.005f) * 0.12f;
  102.             float temp4 = PerlinSimplexNoise.noise(worldX * 0.01f, worldZ * 0.01f) * 0.09f;
  103.             float temp5 = PerlinSimplexNoise.noise(worldX * 0.03f, worldZ * 0.03f) * temp4;
  104.  
  105.             float humidity1 = PerlinSimplexNoise.noise(tempX * 0.01f, tempZ * 0.01f) * 0.5f;
  106.             float humidity2 = PerlinSimplexNoise.noise(tempX * 0.0005f, tempZ * 0.0005f) * 0.25f;
  107.             float humidity3 = PerlinSimplexNoise.noise(tempX * 0.005f, tempZ * 0.005f) * 0.12f;
  108.             float humidity4 = PerlinSimplexNoise.noise(tempX * 0.01f, tempZ * 0.01f) * 0.09f;
  109.             float humidity5 = PerlinSimplexNoise.noise(worldX * 0.03f, worldZ * 0.03f) * humidity4;
  110.  
  111.             float temp = temp1 + temp2 + temp3 + temp4 + temp5;
  112.             float humidity = humidity1 + humidity2 + humidity3 + humidity4 + humidity5;
  113.             //Console.WriteLine(temp * humidity);
  114.             Biome.Biomes biome = Biome.GetBiomeType(humidity, temp);
  115.  
  116.             float octave1 = PerlinSimplexNoise.noise(worldX * 0.0001f, worldZ * 0.0001f) * 0.5f;
  117.             float octave2 = PerlinSimplexNoise.noise(worldX * 0.0005f, worldZ * 0.0005f) * 0.25f;
  118.             float octave3 = PerlinSimplexNoise.noise(worldX * 0.005f, worldZ * 0.005f) * 0.12f;
  119.             float octave4 = PerlinSimplexNoise.noise(worldX * 0.01f, worldZ * 0.01f) * 0.25f;
  120.             float octave5 = PerlinSimplexNoise.noise(worldX * 0.03f, worldZ * 0.03f) * octave4;
  121.             float lowerGroundHeight = octave1 + octave2 + octave3 + octave4 + octave5;
  122.  
  123.             bool sunlit = true;
  124.  
  125.             BlockType blockType2 = BlockType.Grass;
  126.             BlockType blockType = BlockType.Air;
  127.  
  128.             switch (biome)
  129.             {
  130.                 case Biome.Biomes.Plains:
  131.                     blockType2 = BlockType.Sand;
  132.                     lowerGroundHeight -= octave5;
  133.                     break;
  134.                 case Biome.Biomes.Tundra:
  135.                     blockType2 = BlockType.Snow;
  136.                     break;
  137.                 case Biome.Biomes.Desert:
  138.                     blockType2 = BlockType.Sand;
  139.                     break;
  140.                 case Biome.Biomes.Forest:
  141.                     blockType2 = BlockType.Grass;
  142.                     break;
  143.                 case Biome.Biomes.Savanna:
  144.                     blockType2 = BlockType.Sand;
  145.                     break;
  146.                 case Biome.Biomes.SeasonalForest:
  147.                     blockType2 = BlockType.Grass;
  148.                     break;
  149.                 case Biome.Biomes.Shrubland:
  150.                     blockType2 = BlockType.Grass;
  151.                     break;
  152.                 case Biome.Biomes.RainForest:
  153.                     blockType2 = BlockType.Grass;
  154.                     break;
  155.                 case Biome.Biomes.Taiga:
  156.                     blockType2 = BlockType.Snow;
  157.                     break;
  158.  
  159.             }
  160.  
  161.             lowerGroundHeight *= minimumGroundDepth;
  162.             for (int y = SIZEY - 1; y >= 0; y--)
  163.             {
  164.                 if (y <= lowerGroundHeight)
  165.                 {
  166.                     if (sunlit)
  167.                     {
  168.                         blockType = blockType2;
  169.                         sunlit = false;
  170.                         //if (r.Next(700) == 1)
  171.                         {
  172.                             //Trees.Tree(region, x, (byte)y, z);
  173.                             //BuildTree(x, (byte)y, z);
  174.                         }
  175.                     }
  176.                     else
  177.                     {
  178.                         blockType = BlockType.Dirt;
  179.                     }
  180.                 }
  181.                 region.BlockTypes[x, y, z] = blockType;
  182.             }
  183.         }
  184.         #region BuildTree
  185.         public virtual void BuildTree(byte tx, byte ty, byte tz)
  186.         {
  187.             // Trunk
  188.             byte height = (byte)(4 + (byte)r.Next(3));
  189.             if ((ty + height) < SIZEY)
  190.             {
  191.                 for (byte y = ty; y < ty + height; y++)
  192.                 {
  193.                     region.BlockTypes[tx, y, tz] = BlockType.Wood;
  194.                 }
  195.             }
  196.  
  197.             // Foliage
  198.             int radius = 3 + r.Next(2);
  199.             int ny = ty + height;
  200.             for (int i = 0; i < 80 + r.Next(2); i++)
  201.             {
  202.                 int lx = tx + r.Next(radius) - r.Next(radius);
  203.                 int ly = ny + r.Next(radius) - r.Next(radius);
  204.                 int lz = tz + r.Next(radius) - r.Next(radius);
  205.                 unchecked //TODO foliage out of bound => new chunk.blockat or needs a chunk.setat
  206.                 {
  207.                     if (WithinMapBounds(lx, ly, lz))
  208.                     {
  209.                         if (region.BlockTypes[lx, ly, lz] == BlockType.Air)
  210.                         {
  211.                             region.BlockTypes[lx, ly, lz] = BlockType.Leaves; //leaves
  212.                         }
  213.                     }
  214.                 }
  215.             }
  216.  
  217.         }
  218.         #endregion
  219.  
  220.         #region MakeTreeTrunk
  221.         private void MakeTreeTrunk(byte tx, byte ty, byte tz, int height)
  222.         {
  223.             for (byte y = ty; y < ty + height; y++)
  224.             {
  225.                 region.BlockTypes[tx, y, tz] = BlockType.Gravel;
  226.             }
  227.         }
  228.         #endregion
  229.         #region MakeTreeFoliage
  230.         private void MakeTreeFoliage(int tx, int ty, int tz, int height)
  231.         {
  232.             int start = ty + height - 4;
  233.             int end = ty + height + 3;
  234.  
  235.             int rad;
  236.             int radiusEnd = 2;
  237.             int radiusMiddle = radiusEnd + 1;
  238.  
  239.             for (int y = start; y < end; y++)
  240.             {
  241.                 if ((y > start) && (y < end - 1))
  242.                 {
  243.                     rad = radiusMiddle;
  244.                 }
  245.                 else
  246.                 {
  247.                     rad = radiusEnd;
  248.                 }
  249.  
  250.                 for (int xoff = -rad; xoff < rad + 1; xoff++)
  251.                 {
  252.                     for (int zoff = -rad; zoff < rad + 1; zoff++)
  253.                     {
  254.                         if (WithinMapBounds((byte)(tx + xoff), (byte)y, (byte)(tz + zoff)) == false)
  255.                         {
  256.                             region.BlockTypes[(byte)(tx + xoff), (byte)y, (byte)(tz + zoff)] = BlockType.Grass; //leaves
  257.                         }
  258.                     }
  259.                 }
  260.             }
  261.         }
  262.         #endregion
  263.         public static bool WithinMapBounds(int x, int y, int z)
  264.         {
  265.             if (x < 0 || y < 0 || z < 0)
  266.                 return false;
  267.             if (x > SIZEX - 1 || y > SIZEY - 1 || z > SIZEZ - 1)
  268.                 return false;
  269.             return true;
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment