ModernUO/Projects/Scripts/Items/Misc/Key.cs
Kamron Batman 179cb50557 Updates Packets & Randomizer (#43)
* Fixes a bug with the main loop. Removes unnecessary optimizations for RDRand.

* WIP - Rewriting packets.

* Cleanup and updates README

* Converts more packets

* Fixes header

* Converts Effects packets.

* Forgot the send command

* Adds the start of containers packets.

* Adds message packets with caching

* Extends the send methods

* Starts to add Acquire methods

* Converted the packets

* Cleanup

* Cleanup

* Adds more packets

* Adds more packets

* Fixes clearing arrays from pool. Adds Mobile Incoming

* Converts more packets.

* Moves more packets

* Moves more packets

* Test compression

* Merges

* Migrating to an idiomatic syntax that also supports compression, and proxying.

* Optimize the stack alloc. Changes attributes

* Converted container packets

* Visual Studio doesn't auto save files. I am still getting used to subpar IDEs.

* Adds static packet caching. Will profile later. Converts more packets

* Fixes packets and changes how compression is configured

* WIP - Adds basics for Gumps. Not finished though.

* Fix file

* Fixes formatting

* Changed SpanWriter to be more idiomatic.

* Fixes Span vs RawSpan and missing stackallocs

* Converts over some more gumps

* WIP - Deletes 32bit support.

* Drops RDRand32 support.

* Fixes gump compilation

* Converting gump components

* Removes old huffman compression function

* Revert signature for backwards compatibility

* WIP - Converting more gump components

* Creates ArraySet for the strings. Updates AppendTo to reference that.

* Converts the maining gump components

* Cleans up gump components

* Cleans up directives

* Cleans up ArraySet and moves it. Adds null-coalescing-assignment

* Cleanup, Target Packets, and C# 8 changes.

* Removed OPL Packet

* World packets

* Fixing packet uses

* Cleans up code. Fixes packet uses in various places.

* More cleanup for packets

* Code cleanup

* Converts more packet uses and cleans up more code

* More code cleanup

* Finishes fixing the packets in Item

* Updates secure trade packets

* Updates core and gets it to compile.

* Moved packets to scripts. Fixed account handler use of packets

* Code cleanup

* Updates chat packets

* Code cleanuo

* Adds party packets, but need to implement them.

* Party packets WIP

* Finishes party packets

* Rearrange movement namespaces and classes

* Finishes plant packets

* Code Formatting

* Fixes moving effects

* Code cleanup, eliminates equipinfo

* Adds more packets. Fixes bugs with various packets.

* Finishes mahjon packets

* Fixes mahjong packet

* Code cleanup

* Finishes mahjong packets

* Adds Map packets

* Add multifacet maps and charts

* Cleans up some packets with UTF8

* Optimizes packets

* Cleans up more packets. Moves the MessageHelper

* Removes assistant support. Removes extended protocol. Incorporates MapUO packets as normal packets.

* Updates protocol extensions packet receiver

* Fixes a few bugs. Fixes a few more packets.

* Code cleanup and fixing more packets

* Fixes packet effects

* Cleaned up more code

* Buff Icon cleanup

* Removed unused constructors

* Code Cleanup. Adds BoatHS Packets

* Moves house files. Updates house foundation packets.

* Deployment cleanup

* Fixes:

* Code cleanup

* More code cleanup

* More code cleanup

* Cleaned up BaseHouse

* Enforces styling

* Converts foreach to linq where possible.

* Dont need that

* Goals/Readme updates

* Removes 32bit support at the highest level. Turns on HRT by default.

* Code cleanup. Fixes extended features packet.

* Fixes various bugs

* Code cleanup

* Code cleanup

* Code cleanup. Fixes gump X/Y assignment.

* Code cleanup using |= operator

* More code cleanup

* Cleanup

* Fixes spacing issues. Thanks Visual Studio. You suck.

* Compiler error

* Fixes NPE from RunUO 2.7

* Renames ScriptCompiler to AssemblyHandler. Fixes packets. Updates README

* Fixes more packets. Stupid trailing nulls.

* Fixes various bugs.

* Fixes for gumps

* Fixes more gump stuff. Going to split it out later since it is getting insane

* Recoded the gump writing

* WIP

* *Added output path of scripts project to dev branch
*Activated debugging in code
2019-11-11 12:40:16 +01:00

371 lines
8.7 KiB
C#

using Server.Network;
using Server.Targeting;
namespace Server.Items
{
public enum KeyType
{
Copper = 0x100E,
Gold = 0x100F,
Iron = 0x1010,
Rusty = 0x1013
}
public interface ILockable
{
bool Locked{ get; set; }
uint KeyValue{ get; set; }
}
public class Key : Item
{
private string m_Description;
private uint m_KeyVal;
[Constructible]
public Key(uint val = 0) : this(KeyType.Iron, val)
{
}
public Key(KeyType type, uint val = 0, Item link = null) : base((int)type)
{
Weight = 1.0;
MaxRange = 3;
m_KeyVal = val;
Link = link;
}
public Key(Serial serial) : base(serial)
{
}
[CommandProperty(AccessLevel.GameMaster)]
public string Description
{
get => m_Description;
set
{
m_Description = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public int MaxRange{ get; set; }
[CommandProperty(AccessLevel.GameMaster)]
public uint KeyValue
{
get => m_KeyVal;
set
{
m_KeyVal = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public Item Link{ get; set; }
public static uint RandomValue() => (uint)(0xFFFFFFFE * Utility.RandomDouble()) + 1;
public static void RemoveKeys(Mobile m, uint keyValue)
{
if (keyValue == 0)
return;
RemoveKeys(m.Backpack, keyValue);
RemoveKeys(m.BankBox, keyValue);
}
public static void RemoveKeys(Container cont, uint keyValue)
{
if (cont == null || keyValue == 0)
return;
Item[] items = cont.FindItemsByType(new[] { typeof(Key), typeof(KeyRing) });
foreach (Item item in items)
if (item is Key key)
{
if (key.KeyValue == keyValue)
key.Delete();
}
else
{
KeyRing keyRing = (KeyRing)item;
keyRing.RemoveKeys(keyValue);
}
}
public static bool ContainsKey(Container cont, uint keyValue)
{
if (cont == null)
return false;
Item[] items = cont.FindItemsByType(new[] { typeof(Key), typeof(KeyRing) });
foreach (Item item in items)
if (item is Key key)
{
if (key.KeyValue == keyValue)
return true;
}
else
{
KeyRing keyRing = (KeyRing)item;
if (keyRing.ContainsKey(keyValue))
return true;
}
return false;
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write(2); // version
writer.Write(MaxRange);
writer.Write(Link);
writer.Write(m_Description);
writer.Write(m_KeyVal);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
switch (version)
{
case 2:
{
MaxRange = reader.ReadInt();
goto case 1;
}
case 1:
{
Link = reader.ReadItem();
goto case 0;
}
case 0:
{
if (version < 2 || MaxRange == 0)
MaxRange = 3;
m_Description = reader.ReadString();
m_KeyVal = reader.ReadUInt();
break;
}
}
}
public override void OnDoubleClick(Mobile from)
{
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(501661); // That key is unreachable.
return;
}
Target t;
int number;
if (m_KeyVal != 0)
{
number = 501662; // What shall I use this key on?
t = new UnlockTarget(this);
}
else
{
number = 501663; // This key is a key blank. Which key would you like to make a copy of?
t = new CopyTarget(this);
}
from.SendLocalizedMessage(number);
from.Target = t;
}
public override void GetProperties(ObjectPropertyList list)
{
base.GetProperties(list);
string desc = m_KeyVal == 0 ? "(blank)" : m_Description?.Trim() ?? "";
if (desc != "")
list.Add(desc);
}
public override void OnSingleClick(Mobile from)
{
base.OnSingleClick(from);
string desc = m_KeyVal == 0 ? "(blank)" : m_Description?.Trim() ?? "";
if (desc.Length > 0)
Packets.SendUnicodeMessage(from.NetState, Serial, ItemID, MessageType.Regular, 0x3B2, 3, "ENU", "", desc);
}
public bool UseOn(Mobile from, ILockable o)
{
if (o.KeyValue == KeyValue)
{
if (o is BaseDoor door && !door.UseLocks())
return false;
o.Locked = !o.Locked;
if (o is LockableContainer cont1)
if (cont1.LockLevel == -255)
cont1.LockLevel = cont1.RequiredSkill - 10;
if (o is Item item)
{
if (o.Locked)
item.SendLocalizedMessageTo(from, 1048000); // You lock it.
else
item.SendLocalizedMessageTo(from, 1048001); // You unlock it.
if (item is LockableContainer cont && cont.TrapType != TrapType.None && cont.TrapOnLockpick)
{
if (o.Locked)
cont.SendLocalizedMessageTo(from, 501673); // You re-enable the trap.
else
cont.SendLocalizedMessageTo(from,
501672); // You disable the trap temporarily. Lock it again to re-enable it.
}
}
return true;
}
return false;
}
private class RenamePrompt : Prompt
{
private Key m_Key;
public RenamePrompt(Key key) => m_Key = key;
public override void OnResponse(Mobile from, string text)
{
if (m_Key.Deleted || !m_Key.IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(501661); // That key is unreachable.
return;
}
m_Key.Description = Utility.FixHtml(text);
}
}
private class UnlockTarget : Target
{
private Key m_Key;
public UnlockTarget(Key key) : base(key.MaxRange, false, TargetFlags.None)
{
m_Key = key;
CheckLOS = false;
}
protected override void OnTarget(Mobile from, object targeted)
{
if (m_Key.Deleted || !m_Key.IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(501661); // That key is unreachable.
return;
}
int number;
if (targeted == m_Key)
{
number = 501665; // Enter a description for this key.
from.Prompt = new RenamePrompt(m_Key);
}
else if (targeted is ILockable lockable)
{
if (m_Key.UseOn(from, lockable))
number = -1;
else
number = 501668; // This key doesn't seem to unlock that.
}
else
{
number = 501666; // You can't unlock that!
}
if (number != -1) from.SendLocalizedMessage(number);
}
}
private class CopyTarget : Target
{
private Key m_Key;
public CopyTarget(Key key) : base(3, false, TargetFlags.None) => m_Key = key;
protected override void OnTarget(Mobile from, object targeted)
{
if (m_Key.Deleted || !m_Key.IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(501661); // That key is unreachable.
return;
}
int number;
if (targeted is Key k)
{
if (k.m_KeyVal == 0)
{
number = 501675; // This key is also blank.
}
else if (from.CheckTargetSkill(SkillName.Tinkering, k, 0, 75.0))
{
number = 501676; // You make a copy of the key.
m_Key.Description = k.Description;
m_Key.KeyValue = k.KeyValue;
m_Key.Link = k.Link;
m_Key.MaxRange = k.MaxRange;
}
else if (Utility.RandomDouble() <= 0.1) // 10% chance to destroy the key
{
from.SendLocalizedMessage(501677); // You fail to make a copy of the key.
number = 501678; // The key was destroyed in the attempt.
m_Key.Delete();
}
else
{
number = 501677; // You fail to make a copy of the key.
}
}
else
{
number = 501688; // Not a key.
}
from.SendLocalizedMessage(number);
}
}
}
}