From b30acba14a3ac4cd7ffc608791d91eea4e01fbbe Mon Sep 17 00:00:00 2001 From: Big E Date: Fri, 26 Dec 2025 03:37:35 -0500 Subject: [PATCH] + Added proper multiplayer support to Dialogue Display System --- CalamityNetcode.cs | 4 +- Packets/StartDialogueDisplayPacket.cs | 125 ++++++++++++++++++++++++ UI/DialogueDisplay/DialogueDisplayUI.cs | 97 ++++++++++++++++-- 3 files changed, 217 insertions(+), 9 deletions(-) create mode 100644 Packets/StartDialogueDisplayPacket.cs diff --git a/CalamityNetcode.cs b/CalamityNetcode.cs index bfffa1013f..33c5e2f016 100644 --- a/CalamityNetcode.cs +++ b/CalamityNetcode.cs @@ -209,6 +209,8 @@ namespace CalamityMod WantToRefundReforges, // Player Draw Effect Parameters - SyncPlayerDrawParameter + SyncPlayerDrawParameter, + + DialogueDisplayStart, } } diff --git a/Packets/StartDialogueDisplayPacket.cs b/Packets/StartDialogueDisplayPacket.cs new file mode 100644 index 0000000000..0cc1812a65 --- /dev/null +++ b/Packets/StartDialogueDisplayPacket.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CalamityMod.Items.Weapons.Ranged; +using CalamityMod.Systems; +using CalamityMod.UI.DialogueDisplay.DisplayEffects; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Terraria; +using Terraria.ID; +using Terraria.ModLoader.IO; +using Terraria.Net; +using static CalamityMod.UI.DialogueDisplay.DialogueDisplaySystem; + +namespace CalamityMod.Packets +{ + internal sealed class StartDialogueDisplayPacket : CalamityPacket + { + public static StartDialogueDisplayPacket Instance { get; private set; } + + public override byte MessageType => (byte)CalamityModMessageType.DialogueDisplayStart; + + public static void Send(string name, bool progressDialogue, Vector2 position, int index, int uptime, DisplayEffectID effect, float wrapWidth, int toClient = -1, int ignoreClient = -1) + { + // Only Server should send Reponse to Clients + if (!Main.dedServ) + return; + + var packet = Instance.CreateBasePacket(); + + packet.Write(name); + packet.WriteFlags(progressDialogue, false); + packet.WritePackedVector2(position); + packet.Write(index); + packet.Write(uptime); + packet.Write((byte)effect); + packet.Write(wrapWidth); + + packet.Send(toClient, ignoreClient); + } + + public enum EntityType + { + NPC, + Player, + Projectile + } + + public static void Send(string name, bool progressDialogue, EntityType type, int entity, int index, int uptime, DisplayEffectID effect, float wrapWidth, int toClient = -1, int ignoreClient = -1) + { + // Only Server should send Reponse to Clients + if (!Main.dedServ) + return; + + var packet = Instance.CreateBasePacket(); + + packet.Write(name); + packet.WriteFlags(progressDialogue, true); + packet.WriteFlags(type == EntityType.NPC, type == EntityType.Player); + packet.Write(entity); + packet.Write(index); + packet.Write(uptime); + packet.Write((byte)effect); + packet.Write(wrapWidth); + + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(in BinaryReader packet, int sender) + { + // Only receive info as clients + if (Main.netMode != NetmodeID.MultiplayerClient) + { + packet.ReadString(); + packet.ReadFlags(out _, out bool n); + + if (n) + { + packet.ReadFlags(out _, out _); + packet.ReadInt32(); + } + else + packet.ReadPackedVector2(); + + packet.ReadInt32(); + packet.ReadInt32(); + packet.ReadByte(); + packet.ReadSingle(); + return; + } + + string name = packet.ReadString(); + packet.ReadFlags(out bool progressDialogue, out bool hasEntity); + + int entity = -1; + Vector2 pos = Vector2.zeroVector; + bool isNpc = false; + bool isPlayer = false; + if (hasEntity) + { + packet.ReadFlags(out isNpc, out isPlayer); + entity = packet.ReadInt32(); + } + else + pos = packet.ReadPackedVector2(); + + int index = packet.ReadInt32(); + int uptime = packet.ReadInt32(); + byte effect = packet.ReadByte(); + float wrapWidth = packet.ReadSingle(); + + DisplayEffect de = GetEffect((DisplayEffectID)effect); + + if (hasEntity) + StartDialogueOnClient(name, isNpc ? Main.npc[entity] : isPlayer ? Main.player[entity] : Main.projectile.FirstOrDefault(p => p.identity == entity), index, uptime, progressDialogue, de, wrapWidth); + else + StartDialogueOnClient(name, pos, index, uptime, progressDialogue, de, wrapWidth); + + } + } + +} diff --git a/UI/DialogueDisplay/DialogueDisplayUI.cs b/UI/DialogueDisplay/DialogueDisplayUI.cs index 4bf1265e07..18eb13319e 100644 --- a/UI/DialogueDisplay/DialogueDisplayUI.cs +++ b/UI/DialogueDisplay/DialogueDisplayUI.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using System.Net.Sockets; using System.Text.Json; using System.Text.Json.Serialization; +using CalamityMod.Packets; using CalamityMod.UI.DialogueDisplay.DialogueEvents; using CalamityMod.UI.DialogueDisplay.DisplayEffects; using CalamityMod.UI.DialogueDisplay.TextEffects; @@ -22,6 +24,7 @@ using Terraria.ModLoader; using Terraria.UI; using Terraria.UI.Chat; using static System.Runtime.InteropServices.JavaScript.JSType; +using static CalamityMod.Packets.StartDialogueDisplayPacket; using static ReLogic.Graphics.DynamicSpriteFont; namespace CalamityMod.UI.DialogueDisplay @@ -63,8 +66,13 @@ namespace CalamityMod.UI.DialogueDisplay { if (dialog.entity.active) dialog.ui.Position = dialog.entity.Center; - else + else if (ui.DisplayEffects.DespawnWithAttachedNPC) dialog.ui.ClosingDialogue = true; + else + { + dialog.ui.Position = dialog.entity.Center; + dialog.entity = null; + } } if (dialog.upTime != -1) @@ -911,6 +919,43 @@ namespace CalamityMod.UI.DialogueDisplay internal static UserInterface UI; + public enum DisplayEffectID + { + Invalid = -1, + None, + AlwaysOnScreen, + BossText, + Built, + WhisperingPearls + } + + public static DisplayEffectID GetID(object obj) + { + if (obj is not DisplayEffect) + return DisplayEffectID.Invalid; + + return obj switch + { + AlwaysOnScreen => DisplayEffectID.AlwaysOnScreen, + BossText => DisplayEffectID.BossText, + BuiltEffect => DisplayEffectID.Built, + WhisperingPearlEffects => DisplayEffectID.WhisperingPearls, + _ => DisplayEffectID.None + }; + } + + public static DisplayEffect GetEffect(DisplayEffectID id) + { + return id switch + { + DisplayEffectID.AlwaysOnScreen => new AlwaysOnScreen(), + DisplayEffectID.BossText => new BossText(), + DisplayEffectID.Built => new BuiltEffect(), + DisplayEffectID.WhisperingPearls => new WhisperingPearlEffects(), + _ => new DisplayEffect() + }; + } + public override void Load() { if (!Main.dedServ) @@ -1009,8 +1054,26 @@ namespace CalamityMod.UI.DialogueDisplay /// /// The name of the dialogue's localization key /// The position of the text in the world - public static int StartDialogue(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1) + public static int StartDialogue(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1, int toClient = -1, int ignoreClient = -1) { + if (Main.dedServ) + { + StartDialogueDisplayPacket.Send(name, progressDialogue, startPosition, startIndex, Uptime, GetID(effects), wrapWidth, toClient, ignoreClient); + return -1; + } + else if (Main.netMode == NetmodeID.SinglePlayer) + { + return StartDialogueOnClient(name, startPosition, startIndex, Uptime, progressDialogue, effects, wrapWidth); + } + + return -1; + } + + public static int StartDialogueOnClient(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1) + { + if (Main.dedServ) + return -1; + UI ??= new(); State ??= new(); effects ??= new DisplayEffect(); @@ -1048,10 +1111,28 @@ namespace CalamityMod.UI.DialogueDisplay /// The name of the dialogue's localization key /// The entity this dialogue will appear with /// The entity this dialogue will appear with - public static int StartDialogue(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1) + public static int StartDialogue(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1, int toClient = -1, int ignoreClient = -1) { - UI ??= new(); - State ??= new(); + if (Main.dedServ) + { + StartDialogueDisplayPacket.Send(name, progressDialogue, entity is NPC ? EntityType.NPC : entity is Player ? EntityType.Player : EntityType.Projectile, entity is Projectile p ? p.identity : entity.whoAmI, startIndex, Uptime, GetID(effects), wrapWidth, toClient, ignoreClient); + return -1; + } + else if (Main.netMode == NetmodeID.SinglePlayer) + { + return StartDialogueOnClient(name, entity, startIndex, Uptime, progressDialogue, effects, wrapWidth); + } + + return -1; + } + + public static int StartDialogueOnClient(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1) + { + if (Main.dedServ) + return -1; + + UI ??= new (); + State ??= new (); effects ??= new DisplayEffect(); DialogueTextData textData = Deserialize(name); @@ -1066,9 +1147,9 @@ namespace CalamityMod.UI.DialogueDisplay }; int slot; - for (slot = 0; slot <= DialogueDisplayUI.Dialogues.Count; slot++) - if (!DialogueDisplayUI.Dialogues.ContainsKey(slot)) - break; + for (slot = 0; slot <= DialogueDisplayUI.Dialogues.Count; slot++) + if (!DialogueDisplayUI.Dialogues.ContainsKey(slot)) + break; DialogueDisplayUI.Dialogues.Add(slot, (name, display, textData, entity, Uptime));