Advertisement
Guest User

Untitled

a guest
May 8th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. namespace test_map_generation.voronoi
  3. {
  4.     public struct BiomeInfo
  5.     {
  6.         public char type;
  7.         public int x;
  8.         public int y;
  9.     }
  10.  
  11.     public struct BiomeType
  12.     {
  13.         public char symbol;
  14.     }
  15.  
  16.     public class voronoi_generation
  17.     {
  18.         public int map_size_width;
  19.         public int map_size_height;
  20.         public int starterBiomeCount;
  21.  
  22.         public BiomeType[] biomeTypes;
  23.         public BiomeInfo[] biomeCollection;
  24.  
  25.         public char[,] field;
  26.  
  27.         public voronoi_generation(int width, int height, int numStarterBiomes)
  28.         {
  29.             this.map_size_width = width;
  30.             this.map_size_height = height;
  31.             this.starterBiomeCount = numStarterBiomes;
  32.  
  33.             this.field = new char[this.map_size_width, this.map_size_height];
  34.             this.initBoimeStarters();
  35.         }
  36.  
  37.         public void generateMap()
  38.         {
  39.             for (int i = 0; i < this.map_size_height; i++)
  40.             {
  41.                 for (int j = 0; j < this.map_size_width; j++)
  42.                 {
  43.                     char nearest = '.'; // value here doesn't matter
  44.                     int dist = int.MaxValue;
  45.  
  46.                     // walk over each biomeInfo
  47.                     for (int z = 0; z < this.biomeCollection.Length; z++)
  48.                     {
  49.  
  50.                         // calculate the difference in x and y direction
  51.                         int xdiff = this.biomeCollection[z].x - i;
  52.                         int ydiff = this.biomeCollection[z].y - j;
  53.  
  54.                         // calculate euclidean distance, sqrt is not needed
  55.                         // because we only compare and do not need the real value
  56.                         int cdist = xdiff * xdiff + ydiff * ydiff;
  57.  
  58.                         // is the current distance smaller than the old distance?
  59.                         // if yes, take this biome
  60.                         if (cdist < dist)
  61.                         {
  62.                             nearest = this.biomeCollection[z].type;
  63.                             dist = cdist;
  64.                         }
  65.  
  66.                         // set the field to the nearest biome      
  67.                         this.field[j,i] = nearest;
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.  
  73.         public void printMap()
  74.         {
  75.             for (int i = 0; i < this.map_size_height; i++)
  76.             {
  77.                 String row = " ";
  78.  
  79.                 for (int j = 0; j < this.map_size_width; j++)
  80.                 {
  81.                     row += this.field[j, i].ToString();
  82.                 }
  83.                 Console.WriteLine(row);
  84.             }
  85.         }
  86.  
  87.         private void initBoimeStarters()
  88.         {
  89.             this.initBiomeTypes();
  90.  
  91.             this.biomeCollection = new BiomeInfo[this.starterBiomeCount];
  92.  
  93.             for (int i = 0; i < this.starterBiomeCount; i++)
  94.             {
  95.                 Random rngGen = new Random();
  96.  
  97.                 char biomeTypeSelected = this.biomeTypes[rngGen.Next(this.biomeTypes.Length)].symbol;
  98.                 int positionX = rngGen.Next(this.map_size_width);
  99.                 int positionY = rngGen.Next(this.map_size_height);
  100.  
  101.                 this.biomeCollection[i].type = biomeTypeSelected;
  102.                 this.biomeCollection[i].x = positionX;
  103.                 this.biomeCollection[i].y = positionY;
  104.             }
  105.         }
  106.  
  107.         public void initBiomeTypes()
  108.         {
  109.             this.biomeTypes = new BiomeType[8];
  110.  
  111.             this.biomeTypes[0].symbol = '#';
  112.             this.biomeTypes[1].symbol = 'O';
  113.             this.biomeTypes[2].symbol = '?';
  114.             this.biomeTypes[3].symbol = 'x';
  115.             this.biomeTypes[4].symbol = '@';
  116.             this.biomeTypes[5].symbol = '%';
  117.             this.biomeTypes[6].symbol = '-';
  118.             this.biomeTypes[7].symbol = '+';
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement