ModernUO/Projects/Scripts/Engines/MLQuests/Objectives/EscortObjective.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

260 lines
7.1 KiB
C#

using System;
using System.Linq;
using Server.Gumps;
using Server.Misc;
using Server.Mobiles;
namespace Server.Engines.MLQuests.Objectives
{
public class EscortObjective : BaseObjective
{
public EscortObjective(QuestArea destination = null) => Destination = destination;
public QuestArea Destination{ get; set; }
public override bool CanOffer(IQuestGiver quester, PlayerMobile pm, bool message)
{
if (quester is BaseCreature creature && creature.Controlled ||
quester is BaseEscortable escortable && escortable.IsBeingDeleted)
return false;
MLQuestContext context = MLQuestSystem.GetContext(pm);
if (context != null)
if (context.QuestInstances.Any(instance => instance.Quest.IsEscort))
{
if (message)
MLQuestSystem.Tell(quester, pm, 500896); // I see you already have an escort.
return false;
}
DateTime nextEscort = pm.LastEscortTime + BaseEscortable.EscortDelay;
if (nextEscort > DateTime.UtcNow)
{
if (message)
{
int minutes = (int)Math.Ceiling((nextEscort - DateTime.UtcNow).TotalMinutes);
if (minutes == 1)
MLQuestSystem.Tell(quester, pm, "You must rest 1 minute before we set out on this journey.");
else
MLQuestSystem.Tell(quester, pm, 1071195,
minutes.ToString()); // You must rest ~1_minsleft~ minutes before we set out on this journey.
}
return false;
}
return true;
}
public override void WriteToGump(Gump g, ref int y)
{
g.AddHtmlLocalized(98, y, 312, 16, 1072206, 0x15F90); // Escort to
if (Destination.Name.Number > 0)
g.AddHtmlLocalized(173, y, 312, 20, Destination.Name.Number, 0xFFFFFF);
else if (Destination.Name.String != null)
g.AddLabel(173, y, 0x481, Destination.Name.String);
y += 16;
}
public override BaseObjectiveInstance CreateInstance(MLQuestInstance instance)
{
if (instance == null || Destination == null)
return null;
return new EscortObjectiveInstance(this, instance);
}
}
public class EscortObjectiveInstance : BaseObjectiveInstance
{
private BaseCreature m_Escort;
private DateTime m_LastSeenEscorter;
private EscortObjective m_Objective;
private Timer m_Timer;
public EscortObjectiveInstance(EscortObjective objective, MLQuestInstance instance)
: base(instance, objective)
{
m_Objective = objective;
HasCompleted = false;
m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), CheckDestination);
m_LastSeenEscorter = DateTime.UtcNow;
m_Escort = instance.Quester as BaseCreature;
if (MLQuestSystem.Debug && m_Escort == null && instance.Quester != null)
Console.WriteLine("Warning: EscortObjective is not supported for type '{0}'",
instance.Quester.GetType().Name);
}
public bool HasCompleted{ get; set; }
public override DataType ExtraDataType => DataType.EscortObjective;
public override bool IsCompleted() => HasCompleted;
private void CheckDestination()
{
if (m_Escort == null || HasCompleted) // Completed by deserialization
{
StopTimer();
return;
}
MLQuestInstance instance = Instance;
PlayerMobile pm = instance.Player;
if (instance.Removed)
{
Abandon();
}
else if (m_Objective.Destination.Contains(m_Escort))
{
m_Escort.Say(1042809,
pm.Name); // We have arrived! I thank thee, ~1_PLAYER_NAME~! I have no further need of thy services. Here is thy pay.
if (pm.Young || m_Escort.Region.IsPartOf("Haven Island"))
Titles.AwardFame(pm, 10, true);
else
VirtueHelper.AwardVirtue(pm, VirtueName.Compassion,
m_Escort is BaseEscortable escortable && escortable.IsPrisoner ? 400 : 200);
EndFollow(m_Escort);
StopTimer();
HasCompleted = true;
CheckComplete();
// Auto claim reward
MLQuestSystem.OnDoubleClick(m_Escort, pm);
}
else if (pm.Map != m_Escort.Map || !pm.InRange(m_Escort, 30)) // TODO: verify range
{
if (m_LastSeenEscorter + BaseEscortable.AbandonDelay <= DateTime.UtcNow)
Abandon();
}
else
{
m_LastSeenEscorter = DateTime.UtcNow;
}
}
private void StopTimer()
{
if (m_Timer != null)
{
m_Timer.Stop();
m_Timer = null;
}
}
public static void BeginFollow(BaseCreature quester, PlayerMobile pm)
{
quester.ControlSlots = 0;
quester.SetControlMaster(pm);
quester.ActiveSpeed = 0.1;
quester.PassiveSpeed = 0.2;
quester.ControlOrder = OrderType.Follow;
quester.ControlTarget = pm;
quester.CantWalk = false;
quester.CurrentSpeed = 0.1;
}
public static void EndFollow(BaseCreature quester)
{
quester.ActiveSpeed = 0.2;
quester.PassiveSpeed = 1.0;
quester.ControlOrder = OrderType.None;
quester.ControlTarget = null;
quester.CurrentSpeed = 1.0;
quester.SetControlMaster(null);
(quester as BaseEscortable)?.BeginDelete();
}
public override void OnQuestAccepted()
{
MLQuestInstance instance = Instance;
PlayerMobile pm = instance.Player;
pm.LastEscortTime = DateTime.UtcNow;
if (m_Escort != null)
BeginFollow(m_Escort, pm);
}
public void Abandon()
{
StopTimer();
MLQuestInstance instance = Instance;
PlayerMobile pm = instance.Player;
if (m_Escort?.Deleted == false)
{
if (!pm.Alive)
m_Escort.Say(500901); // Ack! My escort has come to haunt me!
else
m_Escort.Say(500902); // My escort seems to have abandoned me!
EndFollow(m_Escort);
}
// Note: this sound is sent twice on OSI (once here and once in Cancel())
//m_Player.SendSound( 0x5B3 ); // private sound
pm.SendLocalizedMessage(1071194); // You have failed your escort quest...
if (!instance.Removed)
instance.Cancel();
}
public override void OnQuesterDeleted()
{
if (IsCompleted() || Instance.Removed)
return;
Abandon();
}
public override void OnPlayerDeath()
{
// Note: OSI also cancels it when the quest is already complete
if ( /*IsCompleted() ||*/ Instance.Removed)
return;
Instance.Cancel();
}
public override void OnExpire()
{
Abandon();
}
public override void WriteToGump(Gump g, ref int y)
{
m_Objective.WriteToGump(g, ref y);
base.WriteToGump(g, ref y);
// No extra instance stuff printed for this objective
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write(HasCompleted);
}
}
}