ModernUO/Projects/Scripts/Items/Skill Items/Musical Instruments/BaseInstrument.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

552 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using Server.Engines.Craft;
using Server.Mobiles;
using Server.Network;
using Server.Targeting;
namespace Server.Items
{
public delegate void InstrumentPickedCallback(Mobile from, BaseInstrument instrument);
public enum InstrumentQuality
{
Low,
Regular,
Exceptional
}
public abstract class BaseInstrument : Item, ICraftable, ISlayer
{
private static Dictionary<Mobile, BaseInstrument> m_Instruments = new Dictionary<Mobile, BaseInstrument>();
private Mobile m_Crafter;
private DateTime m_LastReplenished;
private InstrumentQuality m_Quality;
private bool m_ReplenishesCharges;
private SlayerName m_Slayer, m_Slayer2;
private int m_UsesRemaining;
public BaseInstrument(int itemID, int wellSound, int badlySound) : base(itemID)
{
SuccessSound = wellSound;
FailureSound = badlySound;
UsesRemaining = Utility.RandomMinMax(InitMinUses, InitMaxUses);
}
public BaseInstrument(Serial serial) : base(serial)
{
}
[CommandProperty(AccessLevel.GameMaster)]
public int SuccessSound{ get; set; }
[CommandProperty(AccessLevel.GameMaster)]
public int FailureSound{ get; set; }
[CommandProperty(AccessLevel.GameMaster)]
public InstrumentQuality Quality
{
get => m_Quality;
set
{
UnscaleUses();
m_Quality = value;
InvalidateProperties();
ScaleUses();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public Mobile Crafter
{
get => m_Crafter;
set
{
m_Crafter = value;
InvalidateProperties();
}
}
public virtual int InitMinUses => 350;
public virtual int InitMaxUses => 450;
public virtual TimeSpan ChargeReplenishRate => TimeSpan.FromMinutes(5.0);
[CommandProperty(AccessLevel.GameMaster)]
public int UsesRemaining
{
get
{
CheckReplenishUses();
return m_UsesRemaining;
}
set
{
m_UsesRemaining = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public DateTime LastReplenished
{
get => m_LastReplenished;
set
{
m_LastReplenished = value;
CheckReplenishUses();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public bool ReplenishesCharges
{
get => m_ReplenishesCharges;
set
{
if (value != m_ReplenishesCharges && value)
m_LastReplenished = DateTime.UtcNow;
m_ReplenishesCharges = value;
}
}
#region ICraftable Members
public int OnCraft(int quality, bool makersMark, Mobile from, CraftSystem craftSystem, Type typeRes, BaseTool tool,
CraftItem craftItem, int resHue)
{
Quality = (InstrumentQuality)quality;
if (makersMark)
Crafter = from;
return quality;
}
#endregion
[CommandProperty(AccessLevel.GameMaster)]
public SlayerName Slayer
{
get => m_Slayer;
set
{
m_Slayer = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public SlayerName Slayer2
{
get => m_Slayer2;
set
{
m_Slayer2 = value;
InvalidateProperties();
}
}
public void CheckReplenishUses(bool invalidate = true)
{
if (!m_ReplenishesCharges || m_UsesRemaining >= InitMaxUses)
return;
if (m_LastReplenished + ChargeReplenishRate < DateTime.UtcNow)
{
TimeSpan timeDifference = DateTime.UtcNow - m_LastReplenished;
m_UsesRemaining = Math.Min(m_UsesRemaining + (int)(timeDifference.Ticks / ChargeReplenishRate.Ticks),
InitMaxUses); //How rude of TimeSpan to not allow timespan division.
m_LastReplenished = DateTime.UtcNow;
if (invalidate)
InvalidateProperties();
}
}
public void ScaleUses()
{
UsesRemaining = UsesRemaining * GetUsesScalar() / 100;
//InvalidateProperties();
}
public void UnscaleUses()
{
UsesRemaining = UsesRemaining * 100 / GetUsesScalar();
}
public int GetUsesScalar() => m_Quality == InstrumentQuality.Exceptional ? 200 : 100;
public void ConsumeUse(Mobile from)
{
// TODO: Confirm what must happen here?
if (UsesRemaining > 1)
{
--UsesRemaining;
}
else
{
from?.SendLocalizedMessage(502079); // The instrument played its last tune.
Delete();
}
}
public static BaseInstrument GetInstrument(Mobile from)
{
if (m_Instruments.TryGetValue(from, out BaseInstrument instrument) && instrument.IsChildOf(from.Backpack))
return instrument;
m_Instruments.Remove(from);
return null;
}
public static int GetBardRange(Mobile bard, SkillName skill) => 8 + (int)(bard.Skills[skill].Value / 15);
public static void PickInstrument(Mobile from, InstrumentPickedCallback callback)
{
BaseInstrument instrument = GetInstrument(from);
if (instrument != null)
{
callback?.Invoke(from, instrument);
}
else
{
from.SendLocalizedMessage(500617); // What instrument shall you play?
from.BeginTarget(1, false, TargetFlags.None, OnPickedInstrument, callback);
}
}
public static void OnPickedInstrument(Mobile from, object targeted, InstrumentPickedCallback callback)
{
if (!(targeted is BaseInstrument instrument))
{
from.SendLocalizedMessage(500619); // That is not a musical instrument.
}
else
{
SetInstrument(from, instrument);
callback?.Invoke(from, instrument);
}
}
public static bool IsMageryCreature(BaseCreature bc) => bc?.AI == AIType.AI_Mage && bc.Skills.Magery.Base > 5.0;
public static bool IsFireBreathingCreature(BaseCreature bc) => bc?.HasBreath == true;
public static bool IsPoisonImmune(BaseCreature bc) => bc?.PoisonImmune != null;
public static int GetPoisonLevel(BaseCreature bc) => (bc?.HitPoison.Level ?? -1) + 1;
public static double GetBaseDifficulty(Mobile targ)
{
/* Difficulty TODO: Add another 100 points for each of the following abilities:
- Radiation or Aura Damage (Heat, Cold etc.)
- Summoning Undead
*/
double val = targ.HitsMax * 1.6 + targ.StamMax + targ.ManaMax;
val += targ.SkillsTotal / 10.0;
if (val > 700)
val = 700 + (int)((val - 700) * (3.0 / 11));
BaseCreature bc = targ as BaseCreature;
if (IsMageryCreature(bc))
val += 100;
if (IsFireBreathingCreature(bc))
val += 100;
if (IsPoisonImmune(bc))
val += 100;
if (targ is VampireBat || targ is VampireBatFamiliar)
val += 100;
val += GetPoisonLevel(bc) * 20;
val /= 10;
if (bc?.IsParagon == true)
val += 40.0;
if (Core.SE && val > 160.0)
val = 160.0;
return val;
}
public double GetDifficultyFor(Mobile targ)
{
double val = GetBaseDifficulty(targ);
if (m_Quality == InstrumentQuality.Exceptional)
val -= 5.0; // 10%
if (m_Slayer != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer);
if (entry != null)
{
if (entry.Slays(targ))
val -= 10.0; // 20%
else if (entry.Group.OppositionSuperSlays(targ))
val += 10.0; // -20%
}
}
if (m_Slayer2 != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer2);
if (entry != null)
{
if (entry.Slays(targ))
val -= 10.0; // 20%
else if (entry.Group.OppositionSuperSlays(targ))
val += 10.0; // -20%
}
}
return val;
}
public static void SetInstrument(Mobile from, BaseInstrument item)
{
m_Instruments[from] = item;
}
public override void GetProperties(ObjectPropertyList list)
{
int oldUses = m_UsesRemaining;
CheckReplenishUses(false);
base.GetProperties(list);
if (m_Crafter != null)
list.Add(1050043, m_Crafter.Name); // crafted by ~1_NAME~
if (m_Quality == InstrumentQuality.Exceptional)
list.Add(1060636); // exceptional
list.Add(1060584, m_UsesRemaining.ToString()); // uses remaining: ~1_val~
if (m_ReplenishesCharges)
list.Add(1070928); // Replenish Charges
if (m_Slayer != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer);
if (entry != null)
list.Add(entry.Title);
}
if (m_Slayer2 != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer2);
if (entry != null)
list.Add(entry.Title);
}
if (m_UsesRemaining != oldUses)
Timer.DelayCall(TimeSpan.Zero, InvalidateProperties);
}
public override void OnSingleClick(Mobile from)
{
List<EquipInfoAttribute> attrs = new List<EquipInfoAttribute>();
if (DisplayLootType)
{
if (LootType == LootType.Blessed)
attrs.Add(new EquipInfoAttribute(1038021)); // blessed
else if (LootType == LootType.Cursed)
attrs.Add(new EquipInfoAttribute(1049643)); // cursed
}
if (m_Quality == InstrumentQuality.Exceptional)
attrs.Add(new EquipInfoAttribute(1018305 - (int)m_Quality));
if (m_ReplenishesCharges)
attrs.Add(new EquipInfoAttribute(1070928)); // Replenish Charges
// TODO: Must this support item identification?
if (m_Slayer != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer);
if (entry != null)
attrs.Add(new EquipInfoAttribute(entry.Title));
}
if (m_Slayer2 != SlayerName.None)
{
SlayerEntry entry = SlayerGroup.GetEntryByName(m_Slayer2);
if (entry != null)
attrs.Add(new EquipInfoAttribute(entry.Title));
}
int number;
if (Name == null)
{
number = LabelNumber;
}
else
{
LabelTo(from, Name);
number = 1041000;
}
if (attrs.Count > 0 || Crafter != null || Name == null)
EquipmentPackets.SendDisplayEquipmentInfo(from.NetState, this, number, m_Crafter, false, attrs);
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write(3); // version
writer.Write(m_ReplenishesCharges);
if (m_ReplenishesCharges)
writer.Write(m_LastReplenished);
writer.Write(m_Crafter);
writer.WriteEncodedInt((int)m_Quality);
writer.WriteEncodedInt((int)m_Slayer);
writer.WriteEncodedInt((int)m_Slayer2);
writer.WriteEncodedInt(UsesRemaining);
writer.WriteEncodedInt(SuccessSound);
writer.WriteEncodedInt(FailureSound);
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
int version = reader.ReadInt();
switch (version)
{
case 3:
{
m_ReplenishesCharges = reader.ReadBool();
if (m_ReplenishesCharges)
m_LastReplenished = reader.ReadDateTime();
goto case 2;
}
case 2:
{
m_Crafter = reader.ReadMobile();
m_Quality = (InstrumentQuality)reader.ReadEncodedInt();
m_Slayer = (SlayerName)reader.ReadEncodedInt();
m_Slayer2 = (SlayerName)reader.ReadEncodedInt();
UsesRemaining = reader.ReadEncodedInt();
SuccessSound = reader.ReadEncodedInt();
FailureSound = reader.ReadEncodedInt();
break;
}
case 1:
{
m_Crafter = reader.ReadMobile();
m_Quality = (InstrumentQuality)reader.ReadEncodedInt();
m_Slayer = (SlayerName)reader.ReadEncodedInt();
UsesRemaining = reader.ReadEncodedInt();
SuccessSound = reader.ReadEncodedInt();
FailureSound = reader.ReadEncodedInt();
break;
}
case 0:
{
SuccessSound = reader.ReadInt();
FailureSound = reader.ReadInt();
UsesRemaining = Utility.RandomMinMax(InitMinUses, InitMaxUses);
break;
}
}
CheckReplenishUses();
}
public override void OnDoubleClick(Mobile from)
{
if (!from.InRange(GetWorldLocation(), 1))
{
from.SendLocalizedMessage(500446); // That is too far away.
}
else if (from.BeginAction<BaseInstrument>())
{
SetInstrument(from, this);
// Delay of 7 second before being able to play another instrument again
new InternalTimer(from).Start();
if (CheckMusicianship(from))
PlayInstrumentWell(from);
else
PlayInstrumentBadly(from);
}
else
{
from.SendLocalizedMessage(500119); // You must wait to perform another action
}
}
public static bool CheckMusicianship(Mobile m)
{
m.CheckSkill(SkillName.Musicianship, 0.0, 120.0);
return m.Skills.Musicianship.Value / 100 > Utility.RandomDouble();
}
public void PlayInstrumentWell(Mobile from)
{
from.PlaySound(SuccessSound);
}
public void PlayInstrumentBadly(Mobile from)
{
from.PlaySound(FailureSound);
}
private class InternalTimer : Timer
{
private Mobile m_From;
public InternalTimer(Mobile from) : base(TimeSpan.FromSeconds(6.0))
{
m_From = from;
Priority = TimerPriority.TwoFiftyMS;
}
protected override void OnTick()
{
m_From.EndAction<BaseInstrument>();
}
}
}
}