using System;
using Server.Targeting;
using Server.Engines.Craft;
namespace Server.Items
{
public class Coin : Item, ICommodity
{
private CraftResource m_Resource;
[CommandProperty (AccessLevel.GameMaster)]
public CraftResource Resource
{
get {return m_Resource;}
set {m_Resource = value;}
}
int ICommodity.DescriptionNumber {get {return LabelNumber;}}
bool ICommodity.IsDeedable {get {return true;}}
[Constructable]
public Coin () : this (1)
{
}
[Constructable]
public Coin (int amount) : this (amount, CraftResource.MetalCopper)
{
}
[Constructable]
public Coin (CraftResource resource) : this (1, resource)
{
}
[Constructable]
public Coin (int amount, CraftResource resource) : base (0xef0)
{
Stackable = true;
Amount = amount;
Weight = 0.02 * CraftResources.GetWeightModifier (resource);
SalvageValue = CraftResources.GetValueModifier (resource);
Hue = CraftResources.GetHue (resource);
m_Resource = resource;
Alignment = Alignment.Earth;
}
#region Methods
public override void OnDoubleClick (Mobile from)
{
if (!Movable)
return;
if (from.InRange (this.GetWorldLocation (), 2))
{
from.SendMessage ("Select the forge on which to smelt the coins.");
from.Target = new InternalTarget (this);
}
else
{
from.SendMessage ("That is too far away.");
}
}
private class InternalTarget : Target
{
private Coin m_Coin;
public InternalTarget (Coin coin) : base (2, false, TargetFlags.None)
{
m_Coin = coin;
}
private bool IsForge (object obj)
{
if ( obj.GetType ().IsDefined (typeof (ForgeAttribute ), false))
return true;
int itemID = 0;
if (obj is Item)
itemID = ((Item)obj).ItemID;
else if (obj is StaticTarget)
itemID = ((StaticTarget)obj).ItemID & 0x3FFF;
return (itemID == 4017 || (itemID >= 6522 && itemID <= 6569));
}
protected override void OnTarget (Mobile from, object targeted)
{
if (m_Coin.Deleted)
return;
if (!from.InRange (m_Coin.GetWorldLocation (), 2))
{
from.SendMessage ("They are too far away");
return;
}
double smeltingSkill = from.Skills[SkillName.Smelting].Base;
if (IsForge (targeted))
{
double difficulty = CraftResources.GetSmeltingDifficulty (m_Coin.Resource) - 25.0;
int consume_per_make = Math.Max (500, 600 - (int)smeltingSkill);
int make_qty = 1;
double minSkill = difficulty - 25.0;
double maxSkill = difficulty + 25.0;
if (difficulty > 25.0 && difficulty > smeltingSkill)
{
from.SendMessage ("You have no idea how to smelt these coins!");
return;
}
if (m_Coin.Amount < consume_per_make)
{
from.SendMessage ("There are not enough coins to make an ingot.");
return;
}
int consume_qty = consume_per_make;
while (consume_qty + consume_per_make < m_Coin.Amount && make_qty < (int)smeltingSkill / 5)
{
consume_qty += consume_per_make;
make_qty += 1;
}
if (from.CheckTargetSkill (SkillName.Smelting, targeted, minSkill, maxSkill)) // Success
{
Ingot newIngot = new Ingot ();
newIngot.Amount = make_qty;
newIngot.Resource = m_Coin.Resource;
newIngot.Hue = CraftResources.GetHue (newIngot.Resource);
newIngot.Weight = 10.0 * CraftResources.GetWeightModifier (newIngot.Resource);
newIngot.SalvageValue = 15.0 * CraftResources.GetValueModifier (newIngot.Resource);
m_Coin.Consume (consume_qty);
if (m_Coin.Amount <= 0)
m_Coin.Delete ();
newIngot.MoveToWorld (from.Location, from.Map);
from.PlaySound (0x57);
from.SendMessage ("You re-smelt the coins into ingots.");
}
else // Failure
{
int waste = Utility.RandomMinMax (1, 10);
if (m_Coin.Amount > waste)
{
m_Coin.Amount -= waste;
from.SendMessage ("You failed to re-smelt the coins and ruined some.");
}
else
{
m_Coin.Delete ();
from.SendMessage ("You failed to re-smelt the coins and ruined them.");
}
}
}
}
}
public override void AddNameProperty (ObjectPropertyList list)
{
list.Add ("{0} Coins", Amount);
}
public override void GetProperties (ObjectPropertyList list)
{
base.GetProperties (list);
list.Add (CraftResources.GetName (m_Resource));
}
#endregion
public Coin (Serial serial) : base (serial)
{
}
public override void Serialize (GenericWriter writer)
{
base.Serialize (writer);
writer.Write ((int)0); // version
writer.Write ((int)m_Resource);
}
public override void Deserialize (GenericReader reader)
{
base.Deserialize (reader);
int version = reader.ReadInt ();
m_Resource = (CraftResource)reader.ReadInt ();
}
}
}