Guest User

LootTable

a guest
May 20th, 2015
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace NovaEngineFramework.Framework.Probability
  6. {
  7.     public class LootTable
  8.     {
  9.         private readonly List<string> _lootTable;
  10.         private readonly Dictionary<string , uint> _cachedLoot;
  11.         private readonly Random _rngInstance;
  12.         private bool _isRebuildRequired;
  13.  
  14.         public LootTable()
  15.         {
  16.             _rngInstance = new Random();
  17.             _cachedLoot = new Dictionary<string , uint>();
  18.             _lootTable = new List<string>();
  19.         }
  20.  
  21.         public LootTable( Random random )
  22.         {
  23.             this._rngInstance = random;
  24.             _cachedLoot = new Dictionary<string , uint>();
  25.             _lootTable = new List<string>();
  26.         }
  27.  
  28.         public void AddLoot( uint probability , string name )
  29.         {
  30.             if( !_cachedLoot.ContainsKey( name ) )
  31.             {
  32.                 this._cachedLoot.Add( name , probability );
  33.                 _isRebuildRequired = true;
  34.             }
  35.             else
  36.             {
  37.                 throw new Exception( "Item: " + name + " Already Exists!" );
  38.             }
  39.  
  40.         }
  41.  
  42.         public void DeleteLoot( string name )
  43.         {
  44.             if( _cachedLoot.ContainsKey( name ) )
  45.             {
  46.                 this._cachedLoot.Remove( name );
  47.                 _isRebuildRequired = true;
  48.             }
  49.             else
  50.             {
  51.                 throw new Exception( "Item: " + name + " Did not exist!" );
  52.             }
  53.         }
  54.  
  55.         public double CalculateProbability( string name )
  56.         {
  57.             if( _cachedLoot.ContainsKey( name ) )
  58.             {
  59.                 double total = _cachedLoot.Values.Sum( n => ( int )n );
  60.                 double percent = _cachedLoot[ name ] / total;
  61.                 return Math.Round( percent * 100 , 2 );
  62.             }
  63.             throw new Exception( "Item: " + name + " Does not exist." );
  64.         }
  65.  
  66.         public uint CheckRarity( string name )
  67.         {
  68.             if( _cachedLoot.ContainsKey( name ) )
  69.             {
  70.                 return _cachedLoot[ name ];
  71.             }
  72.             throw new Exception( "Item: " + name + " Does not exist." );
  73.         }
  74.  
  75.         public List<string> CheckLoot()
  76.         {
  77.             return this._cachedLoot.Keys.ToList();
  78.         }
  79.  
  80.         public void EditLoot( string name , uint newProbability )
  81.         {
  82.             if( _cachedLoot.ContainsKey( name ) )
  83.             {
  84.                 this._cachedLoot[ name ] = newProbability;
  85.                 _isRebuildRequired = true;
  86.             }
  87.             else
  88.             {
  89.                 throw new Exception( "Item: " + name + " Does not exist." );
  90.             }
  91.         }
  92.  
  93.         public void ClearAllLoot()
  94.         {
  95.             this._cachedLoot.Clear();
  96.             this._isRebuildRequired = true;
  97.         }
  98.  
  99.         private void Build()
  100.         {
  101.             _lootTable.Clear();
  102.             foreach( KeyValuePair<string , uint> pair in _cachedLoot )
  103.             {
  104.                 for( int i = 0; i < pair.Value; i++ )
  105.                 {
  106.                     _lootTable.Add( pair.Key );
  107.                 }
  108.             }
  109.             _isRebuildRequired = false;
  110.         }
  111.  
  112.         public string NextRandomItem()
  113.         {
  114.             if( !_isRebuildRequired )
  115.             {
  116.                 return _lootTable[ _rngInstance.Next( _lootTable.Count ) ];
  117.             }
  118.             this.Build(); // A rebuild is needed, let's go ahead and take care of it!
  119.             return _lootTable[ _rngInstance.Next( _lootTable.Count ) ];
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment