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

457 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Server.Engines.MLQuests.Gumps;
using Server.Engines.MLQuests.Objectives;
using Server.Engines.MLQuests.Rewards;
using Server.Mobiles;
namespace Server.Engines.MLQuests
{
[Flags]
public enum MLQuestInstanceFlags : byte
{
None = 0x00,
ClaimReward = 0x01,
Removed = 0x02,
Failed = 0x04
}
public class MLQuestInstance
{
private MLQuestInstanceFlags m_Flags;
private IQuestGiver m_Quester;
private Timer m_Timer;
public MLQuestInstance(MLQuest quest, IQuestGiver quester, PlayerMobile player)
{
Quest = quest;
m_Quester = quester;
QuesterType = quester?.GetType();
Player = player;
Accepted = DateTime.UtcNow;
m_Flags = MLQuestInstanceFlags.None;
Objectives = new BaseObjectiveInstance[quest.Objectives.Count];
bool timed = quest.Objectives.Aggregate(false, (current, t) => current | t.CreateInstance(this).IsTimed);
Register();
if (timed)
m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5), Slice);
}
public MLQuest Quest{ get; set; }
public IQuestGiver Quester
{
get => m_Quester;
set
{
m_Quester = value;
QuesterType = value?.GetType();
}
}
public Type QuesterType{ get; private set; }
public PlayerMobile Player{ get; set; }
public MLQuestContext PlayerContext => MLQuestSystem.GetOrCreateContext(Player);
public DateTime Accepted{ get; set; }
public bool ClaimReward
{
get => GetFlag(MLQuestInstanceFlags.ClaimReward);
set => SetFlag(MLQuestInstanceFlags.ClaimReward, value);
}
public bool Removed
{
get => GetFlag(MLQuestInstanceFlags.Removed);
set => SetFlag(MLQuestInstanceFlags.Removed, value);
}
public bool Failed
{
get => GetFlag(MLQuestInstanceFlags.Failed);
set => SetFlag(MLQuestInstanceFlags.Failed, value);
}
public BaseObjectiveInstance[] Objectives{ get; set; }
public bool SkipReportBack => TextDefinition.IsNullOrEmpty(Quest.CompletionMessage);
private void Register()
{
Quest?.Instances?.Add(this);
if (Player != null)
PlayerContext.QuestInstances.Add(this);
}
private void Unregister()
{
Quest?.Instances?.Remove(this);
if (Player != null)
PlayerContext.QuestInstances.Remove(this);
Removed = true;
}
public bool AllowsQuestItem(Item item, Type type) =>
Objectives.Any(objective => !objective.Expired && objective.AllowsQuestItem(item, type));
public bool IsCompleted()
{
bool requiresAll = Quest.ObjectiveType == ObjectiveType.All;
foreach (BaseObjectiveInstance obj in Objectives)
{
bool complete = obj.IsCompleted();
if (complete && !requiresAll)
return true;
if (!complete && requiresAll)
return false;
}
return requiresAll;
}
public void CheckComplete()
{
if (IsCompleted())
{
Player.PlaySound(0x5B5); // public sound
foreach (BaseObjectiveInstance obj in Objectives)
obj.OnQuestCompleted();
TextDefinition.SendMessageTo(Player, Quest.CompletionNotice, 0x23);
/*
* Advance to the ClaimReward=true stage if this quest has no
* completion message to show anyway. This suppresses further
* triggers of CheckComplete.
*
* For quests that require collections, this is done later when
* the player double clicks the quester.
*/
if (!Removed && SkipReportBack && !Quest.RequiresCollection
) // An OnQuestCompleted can potentially have removed this instance already
ContinueReportBack(false);
}
}
public void Fail()
{
Failed = true;
}
private void Slice()
{
if (ClaimReward || Removed)
{
StopTimer();
return;
}
bool hasAnyFails = false;
bool hasAnyLeft = false;
foreach (BaseObjectiveInstance obj in Objectives)
if (!obj.Expired)
{
if (obj.IsTimed && obj.EndTime <= DateTime.UtcNow)
{
Player.SendLocalizedMessage(1072258); // You failed to complete an objective in time!
obj.Expired = true;
obj.OnExpire();
hasAnyFails = true;
}
else
{
hasAnyLeft = true;
}
}
if (Quest.ObjectiveType == ObjectiveType.All && hasAnyFails || !hasAnyLeft)
Fail();
if (!hasAnyLeft)
StopTimer();
}
public void SendProgressGump()
{
Player.SendGump(new QuestConversationGump(Quest, Player, Quest.InProgressMessage));
}
public void SendRewardOffer()
{
Quest.GetRewards(this);
}
// TODO: Split next quest stuff from SendRewardGump stuff?
public void SendRewardGump()
{
Type nextQuestType = Quest.NextQuest;
if (nextQuestType != null)
{
ClaimRewards(); // skip reward gump
if (Removed) // rewards were claimed successfully
{
MLQuest nextQuest = MLQuestSystem.FindQuest(nextQuestType);
nextQuest?.SendOffer(m_Quester, Player);
}
}
else
{
Player.SendGump(new QuestRewardGump(this));
}
}
public void SendReportBackGump()
{
if (SkipReportBack)
ContinueReportBack(true); // skip ahead
else
Player.SendGump(new QuestReportBackGump(this));
}
public void ContinueReportBack(bool sendRewardGump)
{
// There is a backpack check here on OSI for the rewards as well (even though it's not needed...)
if (Quest.ObjectiveType == ObjectiveType.All)
{
// TODO: 1115877 - You no longer have the required items to complete this quest.
if (Objectives.Any(objective => !objective.IsCompleted()))
return;
if (Objectives.Any(objective => !objective.OnBeforeClaimReward()))
return;
foreach (BaseObjectiveInstance objective in Objectives)
objective.OnClaimReward();
}
else
{
/* The following behavior is unverified, as OSI (currently) has no collect quest requiring
* only one objective to be completed. It is assumed that only one objective is claimed
* (the first completed one), even when multiple are complete.
*/
bool complete = false;
foreach (BaseObjectiveInstance objective in Objectives)
if (objective.IsCompleted())
{
if (objective.OnBeforeClaimReward())
{
complete = true;
objective.OnClaimReward();
}
break;
}
if (!complete)
return;
}
ClaimReward = true;
if (Quest.HasRestartDelay)
PlayerContext.SetDoneQuest(Quest, DateTime.UtcNow + Quest.GetRestartDelay());
// This is correct for ObjectiveType.Any as well
foreach (BaseObjectiveInstance objective in Objectives)
objective.OnAfterClaimReward();
if (sendRewardGump)
SendRewardOffer();
}
public void ClaimRewards()
{
if (Quest == null || Player?.Deleted != false || !ClaimReward || Removed)
return;
List<Item> rewards = new List<Item>();
foreach (BaseReward reward in Quest.Rewards)
reward.AddRewardItems(Player, rewards);
if (rewards.Count != 0)
{
// On OSI a more naive method of checking is used.
// For containers, only the actual container item counts.
bool canFit = rewards.All(rewardItem => Player.AddToBackpack(rewardItem));
if (!canFit)
{
foreach (Item rewardItem in rewards)
rewardItem.Delete();
Player.SendLocalizedMessage(
1078524); // Your backpack is full. You cannot complete the quest and receive your reward.
return;
}
foreach (Item rewardItem in rewards)
{
string rewardName = rewardItem.Name ?? $"#{rewardItem.LabelNumber}";
if (rewardItem.Stackable)
Player.SendLocalizedMessage(1115917,
$"{rewardItem.Amount}\t{rewardName}"); // You receive a reward: ~1_QUANTITY~ ~2_ITEM~
else
Player.SendLocalizedMessage(1074360, rewardName); // You receive a reward: ~1_REWARD~
}
}
foreach (BaseObjectiveInstance objective in Objectives)
objective.OnRewardClaimed();
Quest.OnRewardClaimed(this);
MLQuestContext context = PlayerContext;
if (Quest.RecordCompletion && !Quest.HasRestartDelay) // Quests with restart delays are logged earlier as per OSI
context.SetDoneQuest(Quest);
if (Quest.IsChainTriggered)
context.ChainOffers.Remove(Quest);
Type nextQuestType = Quest.NextQuest;
if (nextQuestType != null)
{
MLQuest nextQuest = MLQuestSystem.FindQuest(nextQuestType);
if (nextQuest != null && !context.ChainOffers.Contains(nextQuest))
context.ChainOffers.Add(nextQuest);
}
Remove();
}
public void Cancel()
{
Cancel(false);
}
public void Cancel(bool removeChain)
{
Remove();
Player.SendSound(0x5B3); // private sound
foreach (BaseObjectiveInstance obj in Objectives)
obj.OnQuestCancelled();
Quest.OnCancel(this);
if (removeChain)
PlayerContext.ChainOffers.Remove(Quest);
}
public void Remove()
{
Unregister();
StopTimer();
}
private void StopTimer()
{
if (m_Timer == null)
return;
m_Timer.Stop();
m_Timer = null;
}
public void OnQuesterDeleted()
{
foreach (BaseObjectiveInstance obj in Objectives)
obj.OnQuesterDeleted();
Quest.OnQuesterDeleted(this);
}
public void OnPlayerDeath()
{
foreach (BaseObjectiveInstance obj in Objectives)
obj.OnPlayerDeath();
Quest.OnPlayerDeath(this);
}
private bool GetFlag(MLQuestInstanceFlags flag) => (m_Flags & flag) != 0;
private void SetFlag(MLQuestInstanceFlags flag, bool value)
{
if (value)
m_Flags |= flag;
else
m_Flags &= ~flag;
}
public void Serialize(GenericWriter writer)
{
// Version info is written in MLQuestPersistence.Serialize
MLQuestSystem.WriteQuestRef(writer, Quest);
writer.Write(m_Quester?.Deleted != false ? Serial.MinusOne : m_Quester.Serial);
writer.Write(ClaimReward);
writer.Write(Objectives.Length);
foreach (BaseObjectiveInstance objInstance in Objectives)
objInstance.Serialize(writer);
}
public static MLQuestInstance Deserialize(GenericReader reader, int version, PlayerMobile pm)
{
MLQuest quest = MLQuestSystem.ReadQuestRef(reader);
// TODO: Serialize quester TYPE too, the quest giver reference then becomes optional (only for escorts)
IQuestGiver quester = World.FindEntity(reader.ReadUInt()) as IQuestGiver;
bool claimReward = reader.ReadBool();
int objectives = reader.ReadInt();
MLQuestInstance instance;
if (quest != null && quester != null && pm != null)
{
instance = quest.CreateInstance(quester, pm);
instance.ClaimReward = claimReward;
}
else
{
instance = null;
}
for (int i = 0; i < objectives; ++i)
BaseObjectiveInstance.Deserialize(reader, version,
instance != null && i < instance.Objectives.Length ? instance.Objectives[i] : null);
instance?.Slice();
return instance;
}
}
}