From 50b59db594b8b046a697e7f28492d699b7765e45 Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 20 Jan 2007 20:39:55 +0000 Subject: [PATCH] refactoring, generics, a bug fix in mana drain, a few todos done --- Scripts/Engines/Craft/Core/CraftSystem.cs | 7 +- Scripts/Engines/Khaldun/PuzzleChest.cs | 53 ++++++-------- Scripts/Mobiles/Vendors/BaseVendor.cs | 19 +++-- Scripts/Mobiles/Vendors/BeverageBuy.cs | 4 +- Scripts/Mobiles/Vendors/GenericBuy.cs | 86 ++++++++++------------- Scripts/Mobiles/Vendors/GenericSell.cs | 24 +++---- Scripts/Mobiles/Vendors/PresetMapBuy.cs | 2 +- Scripts/Multis/HouseFoundation.cs | 11 +-- Scripts/Spells/Fourth/ManaDrain.cs | 8 +-- 9 files changed, 89 insertions(+), 125 deletions(-) diff --git a/Scripts/Engines/Craft/Core/CraftSystem.cs b/Scripts/Engines/Craft/Core/CraftSystem.cs index b5d66966a..b4695ef90 100644 --- a/Scripts/Engines/Craft/Core/CraftSystem.cs +++ b/Scripts/Engines/Craft/Core/CraftSystem.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Server.Items; namespace Server.Engines.Craft @@ -42,7 +42,7 @@ namespace Server.Engines.Craft public virtual CraftECA ECA{ get{ return CraftECA.ChanceMinusSixty; } } - private Hashtable m_ContextTable = new Hashtable(); + private Dictionary m_ContextTable = new Dictionary(); public abstract double GetChanceAtMin( CraftItem item ); @@ -62,7 +62,8 @@ namespace Server.Engines.Craft return null; } - CraftContext c = (CraftContext)m_ContextTable[m]; + CraftContext c = null; + m_ContextTable.TryGetValue( m, out c ); if ( c == null ) m_ContextTable[m] = c = new CraftContext(); diff --git a/Scripts/Engines/Khaldun/PuzzleChest.cs b/Scripts/Engines/Khaldun/PuzzleChest.cs index 433a3b6ae..f5f2b8173 100644 --- a/Scripts/Engines/Khaldun/PuzzleChest.cs +++ b/Scripts/Engines/Khaldun/PuzzleChest.cs @@ -1,5 +1,5 @@ using System; -using System.Collections; +using System.Collections.Generic; using Server; using Server.Gumps; using Server.Network; @@ -50,8 +50,7 @@ namespace Server.Items public PuzzleChestSolution() { - for ( int i = 0; i < m_Cylinders.Length; i++ ) - { + for ( int i = 0; i < m_Cylinders.Length; i++ ) { m_Cylinders[i] = RandomCylinder(); } } @@ -67,8 +66,7 @@ namespace Server.Items public PuzzleChestSolution( PuzzleChestSolution solution ) { - for ( int i = 0; i < m_Cylinders.Length; i++ ) - { + for ( int i = 0; i < m_Cylinders.Length; i++ ) { m_Cylinders[i] = solution.m_Cylinders[i]; } } @@ -183,7 +181,7 @@ namespace Server.Items private PuzzleChestSolution m_Solution; private PuzzleChestCylinder[] m_Hints = new PuzzleChestCylinder[HintsCount]; - private Hashtable m_Guesses = new Hashtable(); + private Dictionary m_Guesses = new Dictionary(); public PuzzleChestSolution Solution { @@ -212,17 +210,15 @@ namespace Server.Items private void InitHints() { - ArrayList list = new ArrayList( Solution.Cylinders.Length - 1 ); + List list = new List( Solution.Cylinders.Length - 1 ); for ( int i = 1; i < Solution.Cylinders.Length; i++ ) - { list.Add( Solution.Cylinders[i] ); - } m_Hints = new PuzzleChestCylinder[HintsCount]; - for ( int i = 0; i < m_Hints.Length; i++ ) - { + + for ( int i = 0; i < m_Hints.Length; i++ ) { int pos = Utility.Random( list.Count ); - m_Hints[i] = (PuzzleChestCylinder)list[pos]; + m_Hints[i] = list[pos]; list.RemoveAt( pos ); } } @@ -256,7 +252,9 @@ namespace Server.Items public PuzzleChestSolutionAndTime GetLastGuess( Mobile m ) { - return (PuzzleChestSolutionAndTime)m_Guesses[m]; + PuzzleChestSolutionAndTime pcst = null; + m_Guesses.TryGetValue( m, out pcst ); + return pcst; } public void SubmitSolution( Mobile m, PuzzleChestSolution solution ) @@ -561,16 +559,14 @@ namespace Server.Items { DropItem( new Gold( 600, 900 ) ); - ArrayList gems = new ArrayList(); + List gems = new List(); for ( int i = 0; i < 9; i++ ) { Item gem = Loot.RandomGem(); Type gemType = gem.GetType(); - foreach ( Item listGem in gems ) - { - if ( listGem.GetType() == gemType ) - { + foreach ( Item listGem in gems ) { + if ( listGem.GetType() == gemType ) { listGem.Amount++; gem.Delete(); break; @@ -582,10 +578,7 @@ namespace Server.Items } foreach ( Item gem in gems ) - { DropItem( gem ); - } - if ( 0.2 > Utility.RandomDouble() ) DropItem( new BagOfReagents( 50 ) ); @@ -676,18 +669,15 @@ namespace Server.Items public void CleanupGuesses() { - ArrayList toDelete = new ArrayList(); + List toDelete = new List(); - foreach ( DictionaryEntry entry in m_Guesses ) - { - if ( DateTime.Now - ((PuzzleChestSolutionAndTime)entry.Value).When > CleanupTime ) - toDelete.Add( entry.Key ); + foreach ( KeyValuePair kvp in m_Guesses ) { + if ( DateTime.Now - kvp.Value.When > CleanupTime ) + toDelete.Add( kvp.Key ); } foreach ( Mobile m in toDelete ) - { m_Guesses.Remove( m ); - } } public PuzzleChest( Serial serial ) : base( serial ) @@ -711,10 +701,9 @@ namespace Server.Items } writer.WriteEncodedInt( (int) m_Guesses.Count ); - foreach ( DictionaryEntry entry in m_Guesses ) - { - writer.Write( (Mobile) entry.Key ); - ((PuzzleChestSolutionAndTime)entry.Value).Serialize( writer ); + foreach ( KeyValuePair kvp in m_Guesses ) { + writer.Write( kvp.Key ); + kvp.Value.Serialize( writer ); } } diff --git a/Scripts/Mobiles/Vendors/BaseVendor.cs b/Scripts/Mobiles/Vendors/BaseVendor.cs index 1048abca8..1c4866dd2 100644 --- a/Scripts/Mobiles/Vendors/BaseVendor.cs +++ b/Scripts/Mobiles/Vendors/BaseVendor.cs @@ -226,7 +226,7 @@ namespace Server.Mobiles GenericBuyInfo buy = m_ArmorBuyInfo[i] as GenericBuyInfo; if ( buy != null ) - buy.DeleteDisplayObject(); + buy.DeleteDisplayEntity(); } SBInfos.Clear(); @@ -560,7 +560,7 @@ namespace Server.Mobiles // NOTE: Only GBI supported; if you use another implementation of IBuyItemInfo, this will crash GenericBuyInfo gbi = (GenericBuyInfo) buyItem; - IEntity disp = gbi.GetDisplayObject() as IEntity; + IEntity disp = gbi.GetDisplayEntity(); list.Add( new BuyItemState( buyItem.Name, cont.Serial, disp == null ? (Serial) 0x7FC0FFEE : disp.Serial, buyItem.Price, buyItem.Amount, buyItem.ItemID, buyItem.Hue ) ); count++; @@ -765,11 +765,10 @@ namespace Server.Mobiles { IBuyItemInfo[] buyInfo = this.GetBuyInfo(); - for ( int i = 0; i < buyInfo.Length; ++i ) - { - GenericBuyInfo gbi = buyInfo[i] as GenericBuyInfo; + for ( int i = 0; i < buyInfo.Length; ++i ) { + GenericBuyInfo gbi = (GenericBuyInfo)buyInfo[i]; - if ( gbi.GetDisplayObject() == obj ) + if ( gbi.GetDisplayEntity() == obj ) return gbi; } @@ -812,7 +811,7 @@ namespace Server.Mobiles bii.Amount -= amount; - object o = bii.GetObject(); + IEntity o = bii.GetEntity(); if ( o is Item ) { @@ -834,7 +833,7 @@ namespace Server.Mobiles for (int i=1;i m_Table; + private List m_Mobiles; public DisplayCache() : base( 0 ) { - m_Table = new Hashtable(); - m_Mobiles = new ArrayList(); + m_Table = new Dictionary(); + m_Mobiles = new List(); } - public object Lookup( Type key ) + public IEntity Lookup( Type key ) { - return m_Table[key]; + IEntity e = null; + m_Table.TryGetValue( key, out e ); + return e; } - public void Store( Type key, object obj, bool cache ) + public void Store( Type key, IEntity obj, bool cache ) { if ( cache ) m_Table[key] = obj; if ( obj is Item ) - AddItem( (Item) obj ); + AddItem( (Item)obj ); else if ( obj is Mobile ) - m_Mobiles.Add( obj ); + m_Mobiles.Add( (Mobile)obj ); } public DisplayCache( Serial serial ) : base( serial ) @@ -57,15 +57,13 @@ namespace Server.Mobiles base.OnAfterDelete(); for ( int i = 0; i < m_Mobiles.Count; ++i ) - ((Mobile)m_Mobiles[i]).Delete(); + m_Mobiles[i].Delete(); m_Mobiles.Clear(); for ( int i = Items.Count - 1; i >= 0; --i ) - { if ( i < Items.Count ) - ((Item)Items[i]).Delete(); - } + Items[i].Delete(); if ( m_Cache == this ) m_Cache = null; @@ -77,7 +75,7 @@ namespace Server.Mobiles writer.Write( (int) 0 ); // version - writer.WriteMobileList( m_Mobiles, true ); + writer.Write( m_Mobiles ); } public override void Deserialize( GenericReader reader ) @@ -86,25 +84,23 @@ namespace Server.Mobiles int version = reader.ReadInt(); - m_Mobiles = reader.ReadMobileList(); + m_Mobiles = reader.ReadStrongMobileList(); for ( int i = 0; i < m_Mobiles.Count; ++i ) - ((Mobile)m_Mobiles[i]).Delete(); + m_Mobiles[i].Delete(); m_Mobiles.Clear(); for ( int i = Items.Count - 1; i >= 0; --i ) - { if ( i < Items.Count ) - ((Item)Items[i]).Delete(); - } + Items[i].Delete(); if ( m_Cache == null ) m_Cache = this; else Delete(); - m_Table = new Hashtable(); + m_Table = new Dictionary(); } } @@ -115,48 +111,44 @@ namespace Server.Mobiles private int m_ItemID; private int m_Hue; private object[] m_Args; - private object m_DisplayObject; + private IEntity m_DisplayEntity; public virtual int ControlSlots{ get{ return 0; } } public virtual bool CanCacheDisplay{ get{ return false; } } //return ( m_Args == null || m_Args.Length == 0 ); } - private bool IsDeleted( object obj ) + private bool IsDeleted( IEntity obj ) { if ( obj is Item ) - return (obj as Item).Deleted; + return ((Item)obj).Deleted; else if ( obj is Mobile ) - return (obj as Mobile).Deleted; + return ((Mobile)obj).Deleted; return false; } - public void DeleteDisplayObject() + public void DeleteDisplayEntity() { - if ( m_DisplayObject is Item ) - (m_DisplayObject as Item).Delete(); - else if ( m_DisplayObject is Mobile ) - (m_DisplayObject as Mobile).Delete(); - - m_DisplayObject = null; + m_DisplayEntity.Delete(); + m_DisplayEntity = null; } - public object GetDisplayObject() + public IEntity GetDisplayEntity() { - if ( m_DisplayObject != null && !IsDeleted( m_DisplayObject ) ) - return m_DisplayObject; + if ( m_DisplayEntity != null && !IsDeleted( m_DisplayEntity ) ) + return m_DisplayEntity; bool canCache = this.CanCacheDisplay; if ( canCache ) - m_DisplayObject = DisplayCache.Cache.Lookup( m_Type ); + m_DisplayEntity = DisplayCache.Cache.Lookup( m_Type ); - if ( m_DisplayObject == null || IsDeleted( m_DisplayObject ) ) - m_DisplayObject = GetObject(); + if ( m_DisplayEntity == null || IsDeleted( m_DisplayEntity ) ) + m_DisplayEntity = GetEntity(); - DisplayCache.Cache.Store( m_Type, m_DisplayObject, canCache ); + DisplayCache.Cache.Store( m_Type, m_DisplayEntity, canCache ); - return m_DisplayObject; + return m_DisplayEntity; } public Type Type @@ -267,12 +259,12 @@ namespace Server.Mobiles } //get a new instance of an object (we just bought it) - public virtual object GetObject() + public virtual IEntity GetEntity() { if ( m_Args == null || m_Args.Length == 0 ) - return Activator.CreateInstance( m_Type ); + return (IEntity)Activator.CreateInstance( m_Type ); - return Activator.CreateInstance( m_Type, m_Args ); + return (IEntity)Activator.CreateInstance( m_Type, m_Args ); //return (Item)Activator.CreateInstance( m_Type ); } diff --git a/Scripts/Mobiles/Vendors/GenericSell.cs b/Scripts/Mobiles/Vendors/GenericSell.cs index 3ffc19d1f..085611769 100644 --- a/Scripts/Mobiles/Vendors/GenericSell.cs +++ b/Scripts/Mobiles/Vendors/GenericSell.cs @@ -1,12 +1,12 @@ using System; -using System.Collections; +using System.Collections.Generic; using Server.Items; namespace Server.Mobiles { public class GenericSellInfo : IShopSellInfo { - private Hashtable m_Table = new Hashtable(); + private Dictionary m_Table = new Dictionary(); private Type[] m_Types; public GenericSellInfo() @@ -21,10 +21,10 @@ namespace Server.Mobiles public int GetSellPriceFor( Item item ) { - int price = (int)m_Table[item.GetType()]; + int price = 0; + m_Table.TryGetValue( item.GetType(), out price ); - if ( item is BaseArmor ) - { + if ( item is BaseArmor ) { BaseArmor armor = (BaseArmor)item; if ( armor.Quality == ArmorQuality.Low ) @@ -39,9 +39,7 @@ namespace Server.Mobiles if ( price < 1 ) price = 1; } - - else if ( item is BaseWeapon ) - { + else if ( item is BaseWeapon ) { BaseWeapon weapon = (BaseWeapon)item; if ( weapon.Quality == WeaponQuality.Low ) @@ -56,8 +54,7 @@ namespace Server.Mobiles if ( price < 1 ) price = 1; } - else if ( item is BaseBeverage ) - { + else if ( item is BaseBeverage ) { int price1 = price, price2 = price; if ( item is Pitcher ) @@ -123,12 +120,7 @@ namespace Server.Mobiles public bool IsInList( Type type ) { - Object o = m_Table[type]; - - if ( o == null ) - return false; - else - return true; + return m_Table.ContainsKey( type ); } } } diff --git a/Scripts/Mobiles/Vendors/PresetMapBuy.cs b/Scripts/Mobiles/Vendors/PresetMapBuy.cs index 1222b05fb..99ad1a050 100644 --- a/Scripts/Mobiles/Vendors/PresetMapBuy.cs +++ b/Scripts/Mobiles/Vendors/PresetMapBuy.cs @@ -15,7 +15,7 @@ namespace Server.Mobiles m_Entry = entry; } - public override object GetObject() + public override IEntity GetEntity() { return new PresetMap( m_Entry ); } diff --git a/Scripts/Multis/HouseFoundation.cs b/Scripts/Multis/HouseFoundation.cs index 1a2694a63..6d57615a3 100644 --- a/Scripts/Multis/HouseFoundation.cs +++ b/Scripts/Multis/HouseFoundation.cs @@ -242,16 +242,7 @@ namespace Server.Multis { int type = (itemID - 0x314) / 16; DoorFacing facing = (DoorFacing)(((itemID - 0x314) / 2) % 8); - - //TODO: Change this to just do a 0x314 + type*16 - switch( type ) - { - case 0: door = new GenericHouseDoor( facing, 0x314, 0xED, 0xF4 ); break; - case 1: door = new GenericHouseDoor( facing, 0x324, 0xED, 0xF4 ); break; - case 2: door = new GenericHouseDoor( facing, 0x334, 0xED, 0xF4 ); break; - case 3: door = new GenericHouseDoor( facing, 0x344, 0xED, 0xF4 ); break; - case 4: door = new GenericHouseDoor( facing, 0x354, 0xED, 0xF4 ); break; - } + door = new GenericHouseDoor( facing, 0x314 + ( type * 16 ), 0xED, 0xF4 ); } else if( itemID >= 0x824 && itemID < 0x834 ) { diff --git a/Scripts/Spells/Fourth/ManaDrain.cs b/Scripts/Spells/Fourth/ManaDrain.cs index 0c538ab02..5e0b45ee8 100644 --- a/Scripts/Spells/Fourth/ManaDrain.cs +++ b/Scripts/Spells/Fourth/ManaDrain.cs @@ -1,7 +1,7 @@ using System; -using System.Collections; -using Server.Targeting; +using System.Collections.Generic; using Server.Network; +using Server.Targeting; namespace Server.Spells.Fourth { @@ -26,7 +26,7 @@ namespace Server.Spells.Fourth Caster.Target = new InternalTarget( this ); } - private Hashtable m_Table = new Hashtable(); + private static Dictionary m_Table = new Dictionary(); private void AosDelay_Callback( object state ) { @@ -72,7 +72,7 @@ namespace Server.Spells.Fourth else if ( toDrain > m.Mana ) toDrain = m.Mana; - if ( m_Table.Contains( m ) ) + if ( m_Table.ContainsKey( m ) ) toDrain = 0; m.FixedParticles( 0x3789, 10, 25, 5032, EffectLayer.Head );