From 19b3120d603bd70ae801bddd7456320bee9c3a0c Mon Sep 17 00:00:00 2001 From: Dreitone Date: Thu, 11 Jun 2026 16:55:04 -0500 Subject: [PATCH] gift fix --- Items/Tools/TheGift.cs | 103 ++++++++++++------ NPCs/CalamityGlobalTownNPC.cs | 18 +++ Packets/Entities/RequestApplyTheGiftPacket.cs | 39 +++++++ Packets/Entities/TheGiftEffectsPacket.cs | 36 ++++++ 4 files changed, 161 insertions(+), 35 deletions(-) create mode 100644 Packets/Entities/RequestApplyTheGiftPacket.cs create mode 100644 Packets/Entities/TheGiftEffectsPacket.cs diff --git a/Items/Tools/TheGift.cs b/Items/Tools/TheGift.cs index 3e8f1f0324..a5fbc6b26e 100644 --- a/Items/Tools/TheGift.cs +++ b/Items/Tools/TheGift.cs @@ -1,10 +1,12 @@ using CalamityMod.Items.Placeables.Furniture; using CalamityMod.NPCs; +using CalamityMod.Packets.Entities; using CalamityMod.Particles; using Microsoft.Xna.Framework; using Terraria; using Terraria.Audio; using Terraria.GameContent; +using Terraria.DataStructures; using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; @@ -15,6 +17,65 @@ namespace CalamityMod.Items.Tools { public new string LocalizationCategory => "Items.Tools"; public static readonly SoundStyle BadBuzzer = new SoundStyle("CalamityMod/Sounds/Custom/BadGiftBuzzer") with { Volume = 0.85f }; + + public static bool CanApplyToNPC(NPC npc) + { + return npc.isLikeATownNPC && !NPCID.Sets.NoTownNPCHappiness[npc.type] && !NPCID.Sets.IsTownPet[npc.type] && !npc.GetGlobalNPC().TheGiftStatus.HasValue; + } + + public static void ApplyGiftEffects(NPC npc, bool positive) + { + Color color = positive ? Color.Green : Color.Red; + SoundEngine.PlaySound(positive ? SoundID.ResearchComplete : BadBuzzer, npc.Center); + + // nothing ever happens.... + for (int i = 0; i < 6; i++) + { + Vector2 spawnPos = npc.Center + Main.rand.NextVector2Circular(30f, 30f); + HealingPlus sparkle = new(spawnPos, 0.5f, (spawnPos - npc.Center).SafeNormalize(Vector2.Zero) * 0.65f, color, color * 0.2f, 25) + { + Rotation = positive ? 0f : MathHelper.PiOver4 + }; + GeneralParticleHandler.SpawnParticle(sparkle); + } + + for (int j = 0; j < 4; j++) + { + Vector2 spawnPos = npc.Center + Main.rand.NextVector2Circular(50f, 50f); + Vector2 arrowVelocity = -Vector2.UnitY * 0.6f * positive.ToDirectionInt(); + StatChangeArrow arrow = new(spawnPos, arrowVelocity, -MathHelper.PiOver2 * positive.ToDirectionInt(), color, color * 0.1f, 0.7f, 40); + GeneralParticleHandler.SpawnParticle(arrow); + } + } + + public static bool TryApplyGift(Item item, NPC npc, bool? forcedOutcome = null) + { + if (!item.active || item.type != ModContent.ItemType() || Main.remixWorld || !CanApplyToNPC(npc)) + return false; + + bool positive = forcedOutcome ?? Main.rand.NextBool(); + var townNPC = npc.GetGlobalNPC(); + townNPC.TheGiftStatus = positive; + townNPC.TheGiftReset = 0.0; + ApplyGiftEffects(npc, positive); + + item.stack--; + if (item.stack <= 0) + { + item.active = false; + item.type = ItemID.None; + } + + if (Main.netMode == NetmodeID.Server) + { + npc.netUpdate = true; + NetMessage.SendData(MessageID.SyncItem, -1, -1, null, item.whoAmI); + TheGiftEffectsPacket.Send(npc, positive); + } + + return true; + } + public override void SetDefaults() { Item.width = 30; @@ -39,49 +100,21 @@ namespace CalamityMod.Items.Tools { // Controls actually applying the effect to an NPC // None of this should work in Remix because Remix disables happiness - if (Item.noGrabDelay <= 0 && !Main.remixWorld && Item.playerIndexTheItemIsReservedFor == Main.myPlayer) + bool localGiftCollisionCheck = Main.netMode == NetmodeID.SinglePlayer || Item.playerIndexTheItemIsReservedFor == Main.myPlayer; + if (Item.noGrabDelay <= 0 && !Main.remixWorld && localGiftCollisionCheck) { foreach (NPC n in Main.ActiveNPCs) { - // Don't run this code for NPCs that aren't affected by happiness - // Also don't run this if The Gift has already been used on this NPC; they all will hold grudges against you :) - if (!n.isLikeATownNPC || NPCID.Sets.NoTownNPCHappiness[n.type] || NPCID.Sets.IsTownPet[n.type] || n.GetGlobalNPC().TheGiftStatus.HasValue) + if (!CanApplyToNPC(n)) continue; if (Item.Hitbox.Intersects(n.Hitbox)) { - bool positive = Main.rand.NextBool(); - Color c = positive ? Color.Green : Color.Red; - SoundEngine.PlaySound(positive ? SoundID.ResearchComplete : BadBuzzer, n.Center); - n.GetGlobalNPC().TheGiftStatus = positive; - n.GetGlobalNPC().TheGiftReset = 0.0; + if (Main.netMode == NetmodeID.SinglePlayer) + TryApplyGift(Item, n); + else if (Main.netMode == NetmodeID.MultiplayerClient) + RequestApplyTheGiftPacket.Send(Item, n); - // Visual effect to demonstrate that something happened - for (int i = 0; i < 6; i++) - { - Vector2 spawnPos = n.Center + Main.rand.NextVector2Circular(30f, 30f); - HealingPlus s = new(spawnPos, 0.5f, (spawnPos - n.Center).SafeNormalize(Vector2.Zero) * 0.65f, c, c * 0.2f, 25) - { - Rotation = positive ? 0f : MathHelper.PiOver4 - }; - GeneralParticleHandler.SpawnParticle(s); - } - for (int j = 0; j < 4; j++) - { - Vector2 spawnPos = n.Center + Main.rand.NextVector2Circular(50f, 50f); - Vector2 arrVel = -Vector2.UnitY * 0.6f * positive.ToDirectionInt(); - StatChangeArrow arr = new(spawnPos, arrVel, -MathHelper.PiOver2 * positive.ToDirectionInt(), c, c * 0.1f, 0.7f, 40); - GeneralParticleHandler.SpawnParticle(arr); - } - - Item.stack--; - if (Item.stack <= 0) - { - Item.active = false; - Item.type = ItemID.None; - } - if (Main.netMode != NetmodeID.SinglePlayer) - NetMessage.SendData(MessageID.SyncItem, -1, -1, null, Item.whoAmI); // Make sure it can't apply to multiple NPCs at once break; } diff --git a/NPCs/CalamityGlobalTownNPC.cs b/NPCs/CalamityGlobalTownNPC.cs index 2dd74d7cb7..9000618921 100644 --- a/NPCs/CalamityGlobalTownNPC.cs +++ b/NPCs/CalamityGlobalTownNPC.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using CalamityMod.Events; using CalamityMod.Items.Accessories; using CalamityMod.Items.Accessories.Vanity; @@ -32,6 +33,7 @@ using Terraria.GameContent; using Terraria.ID; using Terraria.Localization; using Terraria.ModLoader; +using Terraria.ModLoader.IO; using Terraria.WorldBuilding; using static Terraria.ModLoader.ModContent; @@ -106,6 +108,22 @@ namespace CalamityMod.NPCs return myClone; } + // TODO: [The Gift] Move out of this godforsaken class + public override void SendExtraAI(NPC npc, BitWriter bitWriter, BinaryWriter binaryWriter) + { + bitWriter.WriteBit(TheGiftStatus.HasValue); + if (TheGiftStatus.HasValue) + bitWriter.WriteBit(TheGiftStatus.Value); + + binaryWriter.Write(TheGiftReset); + } + + public override void ReceiveExtraAI(NPC npc, BitReader bitReader, BinaryReader binaryReader) + { + TheGiftStatus = bitReader.ReadBit() ? bitReader.ReadBit() : null; + TheGiftReset = binaryReader.ReadDouble(); + } + #region Town NPC Patreon Name Sets private static readonly string[] AnglerNames = [ diff --git a/Packets/Entities/RequestApplyTheGiftPacket.cs b/Packets/Entities/RequestApplyTheGiftPacket.cs new file mode 100644 index 0000000000..d71f7654ae --- /dev/null +++ b/Packets/Entities/RequestApplyTheGiftPacket.cs @@ -0,0 +1,39 @@ +using System.IO; +using CalamityMod.Items.Tools; +using Terraria; +using Terraria.ID; +using Terraria.ModLoader; + +namespace CalamityMod.Packets.Entities +{ + internal sealed class RequestApplyTheGiftPacket : CalamityPacket + { + public static RequestApplyTheGiftPacket Instance { get; private set; } + + public static void Send(Item item, NPC npc, int toClient = -1, int ignoreClient = -1) + { + if (item is null || npc is null) + return; + + var packet = Instance.CreateBasePacket(); + packet.Write((short)item.whoAmI); + packet.WriteWhoAmI(npc); + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(BinaryReader packet, int sender) + { + int itemWhoAmI = packet.ReadInt16(); + NPC npc = packet.ReadNPC(); + + if (!Main.dedServ || npc is null || itemWhoAmI < 0 || itemWhoAmI >= Main.maxItems) + return; + + Item item = Main.item[itemWhoAmI]; + if (!item.active || item.type != ModContent.ItemType()) + return; + + TheGift.TryApplyGift(item, npc); + } + } +} diff --git a/Packets/Entities/TheGiftEffectsPacket.cs b/Packets/Entities/TheGiftEffectsPacket.cs new file mode 100644 index 0000000000..eb6b641e48 --- /dev/null +++ b/Packets/Entities/TheGiftEffectsPacket.cs @@ -0,0 +1,36 @@ +using System.IO; +using CalamityMod.Items.Tools; +using Terraria; + +namespace CalamityMod.Packets.Entities +{ + internal sealed class TheGiftEffectsPacket : CalamityPacket + { + public static TheGiftEffectsPacket Instance { get; private set; } + + public static void Send(NPC npc, bool positive, int toClient = -1, int ignoreClient = -1) + { + if (npc is null) + return; + + var packet = Instance.CreateBasePacket(); + packet.WriteWhoAmI(npc); + packet.Write(positive); + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(BinaryReader packet, int sender) + { + NPC npc = packet.ReadNPC(); + bool positive = packet.ReadBoolean(); + + if (npc is null) + return; + + TheGift.ApplyGiftEffects(npc, positive); + + if (Main.dedServ) + Send(npc, positive, ignoreClient: sender); + } + } +}