View Single Post
Old 07-11-2003, 10:30 AM   #2 (permalink)
php-junkie
Guest
 
Posts: n/a
Default

In this example of class inheritance we are going to add a player accessible pack to the Nightmare just like what the Beetle has. This time we're going to use the Nightmare as the parent class.
Code:
using System;
using Server;
using Server.Items;
using Server.Mobiles;

namespace Server.Mobiles
{
	public class PackMare : Nightmare
	{
		[Constructable]
		public PackMare()
		{
		}
	}
}
As you can see I named my new class PackMare and used Nightmare as the parent class. Next I'm going to add my serialize and deserialize methods.
Code:
using System;
using Server;
using Server.Items;
using Server.Mobiles;

namespace Server.Mobiles
{
	public class PackMare : Nightmare
	{
		[Constructable]
		public PackMare()
		{
			Container pack = Backpack;

			if ( pack != null )
				pack.Delete();

			pack = new StrongBackpack();
			pack.Movable = false;

			AddItem( pack );
		}

		public PackMare( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}
	}
}
Ok this next part we're going to copy and past some lines from \Scripts\Mobiles\Animals\Mounts\Beetle.cs. Lets start by copying lines 48 - 56 and past them to line 13 in our new script. This next part we have to pay careful attention to because these lines we just added deletes the loot added by the parent (Nightmare) class, so now we have to re-add it's loot to the new pack. Open \Scripts\Mobiles\Animals\Mounts\Nightmare.cs and copy lines 76 - 85 and past them to line 23 in our new script.

Here is what you should have thus far.
Code:
using System;
using Server;
using Server.Items;
using Server.Mobiles;

namespace Server.Mobiles
{
	public class PackMare : Nightmare
	{
		[Constructable]
		public PackMare()
		{
			Container pack = Backpack;

			if ( pack != null )
				pack.Delete();

			pack = new StrongBackpack();
			pack.Movable = false;

			AddItem( pack );

			PackGem();
			PackGold( 250, 350 );
			PackItem( new SulfurousAsh( Utility.RandomMinMax( 3, 5 ) ) );
			PackScroll( 1, 5 );
			PackPotion();

			PackNecroScroll( 1 ); // Blood Oath
			PackNecroScroll( 10 ); // Strangle
			PackNecroScroll( 5 ); // Horrific Beast
			PackNecroScroll( 13 ); // Vengeful Spirit
		}

		public PackMare( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}
	}
}
One more thing left to do. Lets copy lines 90 - 139 from Beetle.cs and past them to line 39 in our new script. Here's what you should have when your finished.
Code:
using System;
using Server;
using Server.Items;
using Server.Mobiles;

namespace Server.Mobiles
{
	public class PackMare : Nightmare
	{
		[Constructable]
		public PackMare()
		{
			Container pack = Backpack;

			if ( pack != null )
				pack.Delete();

			pack = new StrongBackpack();
			pack.Movable = false;

			AddItem( pack );

			PackGem();
			PackGold( 250, 350 );
			PackItem( new SulfurousAsh( Utility.RandomMinMax( 3, 5 ) ) );
			PackScroll( 1, 5 );
			PackPotion();

			PackNecroScroll( 1 ); // Blood Oath
			PackNecroScroll( 10 ); // Strangle
			PackNecroScroll( 5 ); // Horrific Beast
			PackNecroScroll( 13 ); // Vengeful Spirit
		}

		public PackMare( Serial serial ) : base( serial )
		{
		}

		#region Pack Animal Methods
		public override bool OnBeforeDeath()
		{
			if ( !base.OnBeforeDeath() )
				return false;

			PackAnimal.CombineBackpacks( this );

			return true;
		}

		public override bool IsSnoop( Mobile from )
		{
			if ( PackAnimal.CheckAccess( this, from ) )
				return false;

			return base.IsSnoop( from );
		}

		public override bool OnDragDrop( Mobile from, Item item )
		{
			if ( CheckFeed( from, item ) )
				return true;

			if ( PackAnimal.CheckAccess( this, from ) )
			{
				AddToBackpack( item );
				return true;
			}

			return base.OnDragDrop( from, item );
		}

		public override bool CheckNonlocalDrop( Mobile from, Item item, Item target )
		{
			return PackAnimal.CheckAccess( this, from );
		}

		public override bool CheckNonlocalLift( Mobile from, Item item )
		{
			return PackAnimal.CheckAccess( this, from );
		}

		public override void GetContextMenuEntries( Mobile from, System.Collections.ArrayList list )
		{
			base.GetContextMenuEntries( from, list );

			PackAnimal.GetContextMenuEntries( this, from, list );
		}
		#endregion

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}
	}
}
You just made a new kind of Nightmare with a player accessible pack without editing the Nightmare.cs file. Now when the dev's release a new beta there isn't anything you need to modify. Simply copy the script to the new beta release and start the server. Also, remember you can do this with anything in the RunUO script package.

Last edited by daat99; 01-12-2006 at 05:57 PM.
  Reply With Quote