using System;
using Server;
using Server.Targeting;
using Server.Items;
namespace CustomBridge
{
public class CustomBridgeDeed : Item
{
[Constructable]
public CustomBridgeDeed() : base(0x14F0)
{
Name = "Custom Bridge Deed";
Hue = 1153;
Weight = 1.0;
}
public CustomBridgeDeed(Serial serial) : base(serial)
{
}
public override void OnDoubleClick(Mobile from)
{
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
}
else
{
from.SendMessage("Where do you want to start the bridge?");
from.Target = new CustomBridgeTarget(this);
}
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write((int)0);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
}
private class CustomBridgeTarget : Target
{
private CustomBridgeDeed m_Deed;
public CustomBridgeTarget(CustomBridgeDeed deed) : base(3, false, TargetFlags.None)
{
m_Deed = deed;
}
protected override void OnTarget(Mobile from, object targeted)
{
if (targeted is IPoint3D)
{
Point3D p = new Point3D((IPoint3D)targeted);
if (!Map.CanFit(p.X, p.Y, p.Z, 3, false, false))
{
from.SendMessage("You cannot build a bridge here.");
}
else
{
from.SendMessage("Which direction do you want the bridge to face?");
from.Target = new CustomBridgeDirectionTarget(m_Deed, p);
}
}
else
{
from.SendMessage("Invalid target.");
}
}
}
private class CustomBridgeDirectionTarget : Target
{
private CustomBridgeDeed m_Deed;
private Point3D m_Start;
public CustomBridgeDirectionTarget(CustomBridgeDeed deed, Point3D start) : base(-1, true, TargetFlags.None)
{
m_Deed = deed;
m_Start = start;
}
protected override void OnTarget(Mobile from, object targeted)
{
if (targeted is StaticTarget)
{
int itemID = ((StaticTarget)targeted).ItemID;
if (itemID == 0x0C1D || itemID == 0x0C1E || itemID == 0x0C1F)
{
Direction dir = ((StaticTarget)targeted).Facing;
int[][] bridge = new int[][] {
new int[] { 0x23BA, 0x4319, 0x23BA },
new int[] { 0x23BA, 0x23BA, 0x23BA },
new int[] { 0x2A0, 0x2A0, 0x2A0 }
};
Point3D[] tiles = new Point3D[3];
tiles[0] = m_Start;
if (dir == Direction
{
tiles[1] = new Point3D(m_Start.X - 1, m_Start.Y, m_Start.Z);
tiles[2] = new Point3D(m_Start.X - 2, m_Start.Y, m_Start.Z);
}
else if (dir == Direction.South)
{
tiles[1] = new Point3D(m_Start.X, m_Start.Y + 1, m_Start.Z);
tiles[2] = new Point3D(m_Start.X, m_Start.Y + 2, m_Start.Z);
}
else if (dir == Direction.East)
{
tiles[1] = new Point3D(m_Start.X + 1, m_Start.Y, m_Start.Z);
tiles[2] = new Point3D(m_Start.X + 2, m_Start.Y, m_Start.Z);
}
else if (dir == Direction.West)
{
tiles[1] = new Point3D(m_Start.X - 1, m_Start.Y, m_Start.Z);
tiles[2] = new Point3D(m_Start.X - 2, m_Start.Y, m_Start.Z);
}
else if (dir == Direction.North)
{
tiles[1] = new Point3D(m_Start.X, m_Start.Y - 1, m_Start.Z);
tiles[2] = new Point3D(m_Start.X, m_Start.Y - 2, m_Start.Z);
}
CustomBridge bridgeItem = new CustomBridge(bridge, from.Map, tiles[0], tiles[1], tiles[2], dir);
if (from.PlaceInBackpack(bridgeItem))
{
m_Deed.Delete();
from.SendMessage("You have created a custom bridge!");
}
else
{
from.SendMessage("You do not have enough room in your backpack for the bridge.");
bridgeItem.Delete();
}
}
else
{
from.SendMessage("You must target a directional carpet to determine the bridge direction.");
}
}
else
{
from.SendMessage("Invalid target.");
}
}
}
}
public class CustomBridge : BaseAddon
{
private int[][] m_Bridge;
private Point3D m_StartTile;
private Point3D m_MiddleTile;
private Point3D m_EndTile;
private Direction m_Direction;
[Constructable]
public CustomBridge(int[][] bridge, Map map, Point3D startTile, Point3D middleTile, Point3D endTile, Direction dir) : base()
{
m_Bridge = bridge;
m_StartTile = startTile;
m_MiddleTile = middleTile;
m_EndTile = endTile;
m_Direction = dir;
for (int i = 0; i < bridge.Length; i++)
{
for (int j = 0; j < bridge[i].Length; j++)
{
AddComponent(new CustomBridgeComponent(bridge[i][j]), i - 1, j - 1, 0);
}
}
Movable = false;
Map = map;
}
public CustomBridge(Serial serial) : base(serial)
{
}
public override void OnDoubleClick(Mobile from)
{
if (from.AccessLevel >= AccessLevel.GameMaster)
{
from.SendGump(new PropertiesGump(from, this));
}
else
{
from.SendMessage("Double-click the bridge to turn it back into a deed.");
}
}
public override void OnDoubleClickDead(Mobile from)
{
from.SendLocalizedMessage(1048146
}
public override void OnComponentUsed(AddonComponent c, Mobile from)
{
if (from.AccessLevel >= AccessLevel.GameMaster)
{
from.SendGump(new PropertiesGump(from, c));
}
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write((int)0);
writer.Write(m_Bridge.Length);
for (int i = 0; i < m_Bridge.Length; i++)
{
writer.Write(m_Bridge[i].Length);
for (int j = 0; j < m_Bridge[i].Length; j++)
{
writer.Write(m_Bridge[i][j]);
}
}
writer.Write(m_StartTile);
writer.Write(m_MiddleTile);
writer.Write(m_EndTile);
writer.Write((int)m_Direction);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
int length = reader.ReadInt();
m_Bridge = new int[length][];
for (int i = 0; i < length; i++)
{
int innerLength = reader.ReadInt();
m_Bridge[i] = new int[innerLength];
for (int j = 0; j < innerLength; j++)
{
m_Bridge[i][j] = reader.ReadInt();
}
}
m_StartTile = reader.ReadPoint3D();
m_MiddleTile = reader.ReadPoint3D();
m_EndTile = reader.ReadPoint3D();
m_Direction = (Direction)reader.ReadInt();
}
}
public class CustomBridgeComponent : AddonComponent
{
private int m_ItemID;
public override int LabelNumber { get { return 1041413; } } // a bridge section
[Constructable]
public CustomBridgeComponent(int itemID) : base()
{
m_ItemID = itemID;
Movable = false;
}
public CustomBridgeComponent(Serial serial) : base(serial)
{
}
public override void OnDoubleClick(Mobile from)
{
if (from.AccessLevel >= AccessLevel.GameMaster)
{
from.SendGump(new PropertiesGump(from, this));
}
else
{
from.SendMessage("Double-click the bridge to turn it back into a deed.");
}
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write((int)0);
writer.Write(m_ItemID);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
m_ItemID = reader.ReadInt();
}
public override void OnPropertiesChanged()
{
ItemID = m_ItemID;
}
}
}