ModernUO/Projects/Scripts/Skills/Tracking.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

378 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Server.Gumps;
using Server.Network;
using Server.Spells;
using Server.Spells.Necromancy;
namespace Server.SkillHandlers
{
public class Tracking
{
private static Dictionary<Mobile, TrackingInfo> m_Table = new Dictionary<Mobile, TrackingInfo>();
public static void Initialize()
{
SkillInfo.Table[(int)SkillName.Tracking].Callback = OnUse;
}
public static TimeSpan OnUse(Mobile m)
{
m.SendLocalizedMessage(1011350); // What do you wish to track?
m.CloseGump<TrackWhatGump>();
m.CloseGump<TrackWhoGump>();
m.SendGump(new TrackWhatGump(m));
return TimeSpan.FromSeconds(10.0); // 10 second delay before beign able to re-use a skill
}
public static void AddInfo(Mobile tracker, Mobile target)
{
TrackingInfo info = new TrackingInfo(tracker, target);
m_Table[tracker] = info;
}
public static double GetStalkingBonus(Mobile tracker, Mobile target)
{
if (!m_Table.TryGetValue(tracker, out TrackingInfo info) || info.m_Target != target || info.m_Map != target.Map)
return 0.0;
int xDelta = info.m_Location.X - target.X;
int yDelta = info.m_Location.Y - target.Y;
double bonus = Math.Sqrt(xDelta * xDelta + yDelta * yDelta);
m_Table.Remove(tracker); //Reset as of Pub 40, counting it as bug for Core.SE.
return Core.ML ? Math.Min(bonus, 10 + tracker.Skills.Tracking.Value / 10) : bonus;
}
public static void ClearTrackingInfo(Mobile tracker)
{
m_Table.Remove(tracker);
}
public class TrackingInfo
{
public Point2D m_Location;
public Map m_Map;
public Mobile m_Target;
public Mobile m_Tracker;
public TrackingInfo(Mobile tracker, Mobile target)
{
m_Tracker = tracker;
m_Target = target;
m_Location = new Point2D(target.X, target.Y);
m_Map = target.Map;
}
}
}
public class TrackWhatGump : Gump
{
private Mobile m_From;
private bool m_Success;
public TrackWhatGump(Mobile from) : base(20, 30)
{
m_From = from;
m_Success = from.CheckSkill(SkillName.Tracking, 0.0, 21.1);
AddPage(0);
AddBackground(0, 0, 440, 135, 5054);
AddBackground(10, 10, 420, 75, 2620);
AddBackground(10, 85, 420, 25, 3000);
AddItem(20, 20, 9682);
AddButton(20, 110, 4005, 4007, 1);
AddHtmlLocalized(20, 90, 100, 20, 1018087); // Animals
AddItem(120, 20, 9607);
AddButton(120, 110, 4005, 4007, 2);
AddHtmlLocalized(120, 90, 100, 20, 1018088); // Monsters
AddItem(220, 20, 8454);
AddButton(220, 110, 4005, 4007, 3);
AddHtmlLocalized(220, 90, 100, 20, 1018089); // Human NPCs
AddItem(320, 20, 8455);
AddButton(320, 110, 4005, 4007, 4);
AddHtmlLocalized(320, 90, 100, 20, 1018090); // Players
}
public override void OnResponse(NetState sender, RelayInfo info)
{
if (info.ButtonID >= 1 && info.ButtonID <= 4)
TrackWhoGump.DisplayTo(m_Success, m_From, info.ButtonID - 1);
}
}
public delegate bool TrackTypeDelegate(Mobile m);
public class TrackWhoGump : Gump
{
private static TrackTypeDelegate[] m_Delegates =
{
IsAnimal,
IsMonster,
IsHumanNPC,
IsPlayer
};
private Mobile m_From;
private List<Mobile> m_List;
private int m_Range;
private TrackWhoGump(Mobile from, List<Mobile> list, int range) : base(20, 30)
{
m_From = from;
m_List = list;
m_Range = range;
AddPage(0);
AddBackground(0, 0, 440, 155, 5054);
AddBackground(10, 10, 420, 75, 2620);
AddBackground(10, 85, 420, 45, 3000);
if (list.Count > 4)
{
AddBackground(0, 155, 440, 155, 5054);
AddBackground(10, 165, 420, 75, 2620);
AddBackground(10, 240, 420, 45, 3000);
if (list.Count > 8)
{
AddBackground(0, 310, 440, 155, 5054);
AddBackground(10, 320, 420, 75, 2620);
AddBackground(10, 395, 420, 45, 3000);
}
}
for (int i = 0; i < list.Count && i < 12; ++i)
{
Mobile m = list[i];
AddItem(20 + i % 4 * 100, 20 + i / 4 * 155, ShrinkTable.Lookup(m));
AddButton(20 + i % 4 * 100, 130 + i / 4 * 155, 4005, 4007, i + 1);
if (m.Name != null)
AddHtml(20 + i % 4 * 100, 90 + i / 4 * 155, 90, 40, m.Name);
}
}
public static void DisplayTo(bool success, Mobile from, int type)
{
if (!success)
{
from.SendLocalizedMessage(1018092); // You see no evidence of those in the area.
return;
}
Map map = from.Map;
if (map == null)
return;
TrackTypeDelegate check = m_Delegates[type];
from.CheckSkill(SkillName.Tracking, 21.1, 100.0); // Passive gain
int range = 10 + (int)(from.Skills.Tracking.Value / 10);
List<Mobile> list = from.GetMobilesInRange(range)
.Where(m => m != from && (!Core.AOS || m.Alive) && (!m.Hidden || m.AccessLevel == AccessLevel.Player || from.AccessLevel > m.AccessLevel) && check(m) && CheckDifficulty(from, m))
.ToList();
if (list.Count > 0)
{
list.Sort(new InternalSorter(from));
from.SendGump(new TrackWhoGump(from, list, range));
from.SendLocalizedMessage(1018093); // Select the one you would like to track.
}
else
{
if (type == 0)
from.SendLocalizedMessage(502991); // You see no evidence of animals in the area.
else if (type == 1)
from.SendLocalizedMessage(502993); // You see no evidence of creatures in the area.
else
from.SendLocalizedMessage(502995); // You see no evidence of people in the area.
}
}
// Tracking players uses tracking and detect hidden vs. hiding and stealth
private static bool CheckDifficulty(Mobile from, Mobile m)
{
if (!Core.AOS || !m.Player)
return true;
int tracking = from.Skills.Tracking.Fixed;
int detectHidden = from.Skills.DetectHidden.Fixed;
if (Core.ML && m.Race == Race.Elf)
tracking /= 2; //The 'Guide' says that it requires twice as Much tracking SKILL to track an elf. Not the total difficulty to track.
int hiding = m.Skills.Hiding.Fixed;
int stealth = m.Skills.Stealth.Fixed;
int divisor = hiding + stealth;
// Necromancy forms affect tracking difficulty
if (TransformationSpellHelper.UnderTransformation(m, typeof(HorrificBeastSpell)))
divisor -= 200;
else if (TransformationSpellHelper.UnderTransformation(m, typeof(VampiricEmbraceSpell)) && divisor < 500)
divisor = 500;
else if (TransformationSpellHelper.UnderTransformation(m, typeof(WraithFormSpell)) && divisor <= 2000)
divisor += 200;
int chance;
if (divisor > 0)
{
if (Core.SE)
chance = 50 * (tracking * 2 + detectHidden) / divisor;
else
chance = 50 * (tracking + detectHidden + 10 * Utility.RandomMinMax(1, 20)) / divisor;
}
else
{
chance = 100;
}
return chance > Utility.Random(100);
}
private static bool IsAnimal(Mobile m) => !m.Player && m.Body.IsAnimal;
private static bool IsMonster(Mobile m) => !m.Player && m.Body.IsMonster;
private static bool IsHumanNPC(Mobile m) => !m.Player && m.Body.IsHuman;
private static bool IsPlayer(Mobile m) => m.Player;
public override void OnResponse(NetState sender, RelayInfo info)
{
int index = info.ButtonID - 1;
if (index >= 0 && index < m_List.Count && index < 12)
{
Mobile m = m_List[index];
m_From.QuestArrow = new TrackArrow(m_From, m, m_Range * 2);
if (Core.SE)
Tracking.AddInfo(m_From, m);
}
}
private class InternalSorter : IComparer<Mobile>
{
private Mobile m_From;
public InternalSorter(Mobile from) => m_From = from;
public int Compare(Mobile x, Mobile y)
{
if (x == null && y == null)
return 0;
if (x == null)
return -1;
if (y == null)
return 1;
return m_From.GetDistanceToSqrt(x).CompareTo(m_From.GetDistanceToSqrt(y));
}
}
}
public class TrackArrow : QuestArrow
{
private Mobile m_From;
private Timer m_Timer;
public TrackArrow(Mobile from, Mobile target, int range) : base(from, target)
{
m_From = from;
m_Timer = new TrackTimer(from, target, range, this);
m_Timer.Start();
}
public override void OnClick(bool rightClick)
{
if (rightClick)
{
Tracking.ClearTrackingInfo(m_From);
m_From = null;
Stop();
}
}
public override void OnStop()
{
m_Timer.Stop();
if (m_From != null)
{
Tracking.ClearTrackingInfo(m_From);
m_From.SendLocalizedMessage(503177); // You have lost your quarry.
}
}
}
public class TrackTimer : Timer
{
private QuestArrow m_Arrow;
private Mobile m_From, m_Target;
private int m_LastX, m_LastY;
private int m_Range;
public TrackTimer(Mobile from, Mobile target, int range, QuestArrow arrow) : base(TimeSpan.FromSeconds(0.25),
TimeSpan.FromSeconds(2.5))
{
m_From = from;
m_Target = target;
m_Range = range;
m_Arrow = arrow;
}
protected override void OnTick()
{
if (!m_Arrow.Running)
{
Stop();
return;
}
if (m_From.NetState == null || m_From.Deleted || m_Target.Deleted || m_From.Map != m_Target.Map ||
!m_From.InRange(m_Target, m_Range) || m_Target.Hidden && m_Target.AccessLevel > m_From.AccessLevel)
{
m_Arrow.Stop();
Stop();
return;
}
if (m_LastX != m_Target.X || m_LastY != m_Target.Y)
{
m_LastX = m_Target.X;
m_LastY = m_Target.Y;
m_Arrow.Update();
}
}
}
}