APPROVED: Powerscroll combine bag
Posted: Sat Sep 20, 2008 6:59 pm
Uhm, do people really care about self repair? Legendary repair deeds are a dime a dozen.
Actually, in retrospect, I'm probably not using this approach. Seeing as all Powerscrolls are a single item type, there is no easy way to distinguish between them. It is easier to create a container that checks what Powerscrolls it contains, and acts accordingly to that.Lord_Gaav wrote:I have taken it upon myself to look at the powerscroll combining bag. I'm currently experimenting with something similar to a blacksmith hammer, only for powerscrolls. In this way, the power scrolls will behave like a kind of resource like wood and ingots.
Code: Select all
using System;
using System.Collections;
using Server.Multis;
using Server.Mobiles;
using Server.Network;
namespace Server.Items
{
public class PowerscrollBag : BaseContainer
{
#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;
}
public PowerscrollBag(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();
}
#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;
}
else
{
// Item is a PowerScroll
if (item.ItemID == 0x14F0)
{
// Recast Item
PowerScroll s = (PowerScroll)item;
// If scroll value is withing normal values
if (s.Value >= 105 && s.Value <= 115)
{
from.PublicOverheadMessage(MessageType.System, 0x22, true, "Powerscroll added, triggering check");
// Start counting
this.countPowerScrolls(s.Skill, s.Value);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
public override bool OnDragDrop(Mobile from, Item dropped)
{
return this.OnDragDropInto(from, dropped, new Point3D());
}
/**
* Count all PowerScrolls of a given type.
*/
private void countPowerScrolls(SkillName skill, double power)
{
// Loop through all Items in this Bag
int count = 0;
foreach (Item i in this.Items)
{
// If this item is a PowerScroll
if (i.ItemID == 0x14F0)
{
// Recast Item
PowerScroll s = (PowerScroll) i;
// If this PowerScroll is the same as the one that was dropped
if (s.Skill == skill && s.Value == power)
{
// Add one
count++;
}
}
}
Console.WriteLine("Counted: " + count.ToString() + "x " + skill.ToString() + " scrolls (" + power.ToString() + ")");
// Check if transformation threshold has been reached
this.transformPowerScrolls(skill, power, count);
}
/**
* 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
*/
private void transformPowerScrolls(SkillName skill, double power, int count)
{
// We don't care about doubles
switch (Convert.ToInt16(Math.Ceiling(power)))
{
case 105:
if (count >= 7)
{
Console.WriteLine("Sufficient scrolls for transform");
this.AddItem(new PowerScroll(skill, 110));
this.removePowerScrolls(skill, power);
}
else
{
Console.WriteLine("Insufficient scrolls for transform");
}
break;
case 110:
if (count >= 7)
{
Console.WriteLine("Sufficient scrolls for transform");
this.AddItem(new PowerScroll(skill, 115));
this.removePowerScrolls(skill, power);
}
else
{
Console.WriteLine("Insufficient scrolls for transform");
}
break;
case 115:
if (count >= 5)
{
Console.WriteLine("Sufficient scrolls for transform");
this.AddItem(new PowerScroll(skill, 120));
this.removePowerScrolls(skill, power);
}
else
{
Console.WriteLine("Insufficient scrolls for transform");
}
break;
default:
Console.WriteLine("What manner of PowerScroll is this!?!?!");
break;
}
}
/**
* Removes all PowerScrolls of a given type
*/
private void removePowerScrolls(SkillName skill, double power)
{
for (int i = (this.Items.Count - 1); i >= 0; i--)
{
Console.WriteLine("Checking item " + i.ToString());
Item item = (Item)this.Items[i];
if (item.ItemID == 0x14F0)
{
PowerScroll s = (PowerScroll)item;
if (s.Skill == skill && s.Value == power)
{
item.Delete();
}
}
}
}
}
}
+Colibri wrote:I think this new cooperation system might just work
Ok looks good, there's a few things that need to be fixed:
1. Since it's a bag, i'd simply extend the Bag class and remove all that DefaultGumpId/Sound, Bounds.
2. There are many items that use itemid 0x14F0 (bank checks, misc deeds), so the correct way to check for the correct type would be:
if ( item is PowerScroll )
3. This is optional, but i suggest that you dont use single chars for variable names. So, "PowerScroll scroll = .." and "Item item". Also RunUO is coded so that private&public methods are all capitalized. But if you leave the private ones starting with a lowercase that's fine too (in a way even more correct).
4. Comment out all of the Console.WriteLine. (a server with 30 people already makes a lot of console chatter)
5. Checking the power of scrolls, i'd say you should define actual values, not the value range.
6. Instead of Math.Ceil i'd rather use Math.Round... Sometimes these doubles get freaky and a ceiling of 115.0000000001 can be turned into 116.