APPROVED: Powerscroll combine bag

LordGaav
Adept Scribe
Posts: 49
Joined: Fri Sep 12, 2008 4:09 pm

Re: APPROVED: Powerscroll combine bag

Post by LordGaav »

+Colibri wrote:Well just putting it in... i'm not sure how much you tested this but server crash here we come :lol:
I'll rewrite it so that it works well.
Have a little faith in me~....

Also, please share the updated version.
User avatar
+Colibri
Administrator
Posts: 3958
Joined: Sat Feb 25, 2006 4:08 pm
Location: static void Main

Re: APPROVED: Powerscroll combine bag

Post by +Colibri »

Well in your case, what would happen, is that after a world restart, if there's already a powerscroll in that bag, the currentAcceptedSkills would be null, and IndexOf would give an exception.

Also, well i know this was my idea, it could be possible to get 125 of all kinds of skills... was supposed to be just the fighting ones (for those that elixiers exist) so i just disabled the 120 dropping in the bag.

Here's what i got now and i dont think i'll be editing it any further:

Code: Select all

	using System;
	using System.Collections;
	using Server.Multis;
	using Server.Mobiles;
	using Server.Network;

	namespace Server.Items
	{
		public class PowerScrollBag : BaseContainer
		{
			private double currentPower = 0;
			private int currentAcceptedSkills = 1;

			private static SkillName[][] SkillTypes = new SkillName[][]{ 
				new SkillName[] {
					SkillName.Anatomy,
					SkillName.AnimalLore,
					SkillName.ArmsLore,
					SkillName.Parry,
					SkillName.Peacemaking,
					SkillName.Discordance,
					SkillName.EvalInt,
					SkillName.Healing,
					SkillName.Hiding,
					SkillName.Provocation,
					SkillName.Magery,
					SkillName.MagicResist,
					SkillName.Tactics,
					SkillName.Musicianship,
					SkillName.Poisoning,
					SkillName.Archery,
					SkillName.SpiritSpeak,
					SkillName.Stealing,
					SkillName.AnimalTaming,
					SkillName.Veterinary,
					SkillName.Swords,
					SkillName.Macing,
					SkillName.Fencing,
					SkillName.Wrestling,
					SkillName.Lumberjacking,
					SkillName.Meditation,
					SkillName.Stealth,
					SkillName.RemoveTrap,
					SkillName.Necromancy,
					SkillName.Focus,
					SkillName.Chivalry,
					SkillName.Bushido,
					SkillName.Ninjitsu
				},
				
				new SkillName[] 
				{
					SkillName.Alchemy,
					SkillName.Blacksmith,
					SkillName.Fletching,
					SkillName.Carpentry,
					SkillName.Cartography,
					SkillName.Cooking,
					SkillName.Fishing,
					SkillName.Inscribe,
					SkillName.Tailoring,
					SkillName.Tinkering,
					SkillName.Mining,
					SkillName.Lockpicking
				}
			};


			#region default stuff
			public override int DefaultGumpID { get { return 0x3D; } }
			public override int DefaultDropSound { get { return 0x48; } }

			public override Rectangle2D Bounds
			{
				get { return new Rectangle2D(29, 34, 108, 94); }
			}

			[Constructable]
			public PowerScrollBag() : base(0xE76)
			{
				Weight = 2.0;
				Hue = 86;
				Name = "PowerScroll bag";
			}

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

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

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

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

				int version = reader.ReadInt();
				
				currentPower = reader.ReadDouble();
				currentAcceptedSkills = reader.ReadInt();
				
			}
			#endregion

			public override bool OnDragDropInto(Mobile from, Item item, Point3D p)
			{
				// Check if this action passes BaseContainer's check
				if (!base.OnDragDropInto(from, item, p))
				return false;

				// Recast Item
				PowerScroll scroll = (PowerScroll)item;
				if (scroll == null)
				return false;

				int power = (int)Math.Round(scroll.Value);

				// If scroll value is withing normal values
				if (!(power == 105 || power == 110 || power == 115 ))//|| power == 120))
				return false;

				// Start counting
				return this.countPowerScrolls(from, scroll.Skill, scroll.Value);
			}

			public override bool OnDragDrop(Mobile from, Item dropped)
			{
				return this.OnDragDropInto(from, dropped, new Point3D());
			}

			/**
			* Count all PowerScrolls of a given type.
			*/
			private bool countPowerScrolls(Mobile from, SkillName skill, double power)
			{
				// Loop through all Items in this Bag
				int count = 0;

				count = this.Items.Count;

				if (count == 0 || count == 1)
				{
					this.currentPower = power;
					
					currentAcceptedSkills = -1;
					
					if ( Array.IndexOf(SkillTypes[0], skill) > -1 )
						currentAcceptedSkills = 0;
						
					if ( Array.IndexOf(SkillTypes[1], skill) > -1 )
						currentAcceptedSkills = 1;
					
					if ( currentAcceptedSkills == -1 )
					{
						Say("That's a very strange scroll and cannot be transformed using this bag.");
						return false;
					}
					
					Say("The bag now accepts all "+(currentAcceptedSkills==1?"":"non-")+"crafting scrolls with power " + power);
				} 
				else if (this.currentPower != power )
				{
					Say("You can currently drop only scrolls with the power " + power+" in this bag.");
					return false;
				}
				else if ( Array.IndexOf(SkillTypes[currentAcceptedSkills], skill) == -1) 
				{
					Say("You can currently drop only "+(currentAcceptedSkills==1?"":"non-")+"crafting power scrolls in this bag.");
					return false;					
					
				}

				// Check if transformation threshold has been reached
				this.transformPowerScrolls(from, skill, power, count);
				return true;
			}

			/**
			* Transforms lesser PowerScrolls into greater ones, if the threshold has been reached.
			*
			* Transformation thresholds:
			* 105 -> 110 PS: 7x
			* 110 -> 115 PS: 7x
			* 115 -> 120 PS: 5x
			* 120 -> 125 PS: 5x
			*/
			private void transformPowerScrolls(Mobile from, SkillName skill, double power, int count)
			{
				int numRequired;
				// We don't care about doubles
				switch ((int)(Math.Round(power)))
				{
				case 105:
				case 110:
					numRequired = 7;
					break;
					
				case 115:
				case 120:
					numRequired = 5;
					break;
				default:
					numRequired = 7;
					break;
				}
				
				
				if (count >= numRequired)
				{
					from.SendMessage("Upgrading power scrolls with the power of " + power);
					

							
					if (countSameSkills(skill, power, count))
					{
						this.DropItem(new PowerScroll(skill, ((int)power) + 5));                           
						this.removePowerScrolls(from, skill, power, numRequired);
					}
					else
					{
						this.DropItem(this.randomScroll(SkillTypes[currentAcceptedSkills], ((int)power + 5)));
						this.removePowerScrollsByGroup(from, SkillTypes[currentAcceptedSkills], power, numRequired);
					}
					
					currentPower += 5;
				}
				else
				{
					Say("Add " + (numRequired - count) + " more "+(currentAcceptedSkills==1?"":"non-")+"crafting scrolls with power of " + this.currentPower + ".");
				}
			
			}

			/**
			* Removes all PowerScrolls of a given group
			*/
			private void removePowerScrollsByGroup(Mobile from, SkillName[] skills, double power, int number)
			{
				int count = 0;

				for (int i = (this.Items.Count - 1); i >= 0; i--)
				{
					Item item = (Item)this.Items[i];
					if (!(item is PowerScroll))
					continue;

					PowerScroll scroll = (PowerScroll)item;
					if ((Array.IndexOf(skills, scroll.Skill) > -1) && scroll.Value == power && count < number)
					{
						count++;
						item.Delete();
					}
				}


				Effects.SendLocationParticles(EffectItem.Create(from.Location, from.Map, EffectItem.DefaultDuration), 0, 0, 0, 0, 0, 5060, 0);
				Effects.PlaySound(from.Location, from.Map, 0x243);

				Effects.SendMovingParticles(new Entity(Serial.Zero, new Point3D(from.X - 6, from.Y - 6, from.Z + 15), from.Map), from, 0x36D4, 7, 0, false, true, 0x497, 0, 9502, 1, 0, (EffectLayer)255, 0x100);
				Effects.SendMovingParticles(new Entity(Serial.Zero, new Point3D(from.X - 4, from.Y - 6, from.Z + 15), from.Map), from, 0x36D4, 7, 0, false, true, 0x497, 0, 9502, 1, 0, (EffectLayer)255, 0x100);
				Effects.SendMovingParticles(new Entity(Serial.Zero, new Point3D(from.X - 6, from.Y - 4, from.Z + 15), from.Map), from, 0x36D4, 7, 0, false, true, 0x497, 0, 9502, 1, 0, (EffectLayer)255, 0x100);

				Effects.SendTargetParticles(from, 0x375A, 35, 90, 0x00, 0x00, 9502, (EffectLayer)255, 0x100);
			}
			
			/**
			* Removes all PowerScrolls of a given type
			*/
			private void removePowerScrolls(Mobile from, SkillName skill, double power, int number)
			{
				this.removePowerScrollsByGroup(from, new SkillName[] { skill }, power, number);
			}

			/**
			* Checks if all the scrolls are of the given skill and power, and if there are enough available.
			* Returns true if conditions are met, false if not
			*/
			private bool countSameSkills(SkillName skill, double power, int needed)
			{
				int count = 0;
				foreach (Item i in this.Items)
				{
					// If this item is a PowerScroll
					if (i is PowerScroll)
					{
						// Recast Item
						PowerScroll scroll = (PowerScroll)i;
						// If this PowerScroll is the same as the one that was dropped
						if (scroll.Skill == skill && scroll.Value == power)
						{
							// Add one
							count++;
						}

						if (count >= needed)
						{
							return true;
						}
					}
				}

				return false;
			}

			/**
			* Generate a random scroll with the given power, and picking its skill from the given list.
			*/
			private PowerScroll randomScroll(SkillName[] skills, double power)
			{
				return new PowerScroll(SkillTypes[currentAcceptedSkills][Utility.Random(skills.Length)], power);
			}

			/**
			* Make the bag say the string.
			*/
			public void Say(string args)
			{
				PublicOverheadMessage(MessageType.Regular, 0x3B2, false, args);
			}

		}
	}
+Colibri, Administrator of UO Excelsior Shard

Don't know what the purpose of your life is? Well then make something up! ;)
(Old Colibrian proverb)
LordGaav
Adept Scribe
Posts: 49
Joined: Fri Sep 12, 2008 4:09 pm

Re: APPROVED: Powerscroll combine bag

Post by LordGaav »

I have added a wiki page.
Locked