diff --git a/CalPlayer/CalamityPlayer.cs b/CalPlayer/CalamityPlayer.cs index bcac3459bf..ac9c310e0d 100644 --- a/CalPlayer/CalamityPlayer.cs +++ b/CalPlayer/CalamityPlayer.cs @@ -1148,6 +1148,31 @@ namespace CalamityMod.CalPlayer public bool AbleToSelectExoMech = false; #endregion Draedon Summoning + #region Mouse Controls Syncing + public bool mouseRight = false; + private bool oldMouseRight = false; + public Vector2 mouseWorld; + private Vector2 oldMouseWorld; + + /// + /// Set this to true if you need to recieve updates on right clicks from players and sync them in mp. + /// Automatically resets itself after sending an update + /// <\summary> + public bool rightClickListener = false; + /// + /// Set this to true if you need to recieve updates on the position of the player's mouse and sync them in mp. + /// Automatically resets itself after sending an update + /// <\summary> + public bool mouseWorldListener = false; + /// + /// Set this to true if you need to recieve updates on the rotation of the mouse to the player. This sends updates less frequently than the more tight tolerance mouseWorldListener + /// Automatically resets itself after sending an update + /// <\summary> + public bool mouseRotationListener = false; + + public bool syncMouseControls = false; + #endregion + #endregion #region Saving And Loading @@ -2127,6 +2152,8 @@ namespace CalamityMod.CalPlayer AbleToSelectExoMech = false; + mouseRight = false; + CalamityPlayerMiscEffects.EnchantHeldItemEffects(player, player.Calamity(), player.ActiveItem()); } #endregion @@ -3867,10 +3894,10 @@ namespace CalamityMod.CalPlayer player.AddBuff(ModContent.BuffType(), 60, true); } } - #endregion + #endregion - #region PreUpdate - public override void PreUpdate() + #region PreUpdate + public override void PreUpdate() { tailFrameUp++; if (tailFrameUp == 8) @@ -3906,7 +3933,30 @@ namespace CalamityMod.CalPlayer GameShaders.Armor.GetSecondaryShader(player.dye[i].dye, player)?.UseColor(CalamityPlayerDrawEffects.GetCurrentMoonlightDyeColor()); } } - } + //Syncing mouse controls + if (Main.myPlayer == player.whoAmI) + { + mouseRight = PlayerInput.Triggers.Current.MouseRight; + if (rightClickListener && mouseRight != oldMouseRight) + { + oldMouseRight = mouseRight; + syncMouseControls = true; + rightClickListener = false; + } + if (mouseWorldListener && Vector2.Distance(mouseWorld, oldMouseWorld) > 10f) + { + oldMouseWorld = mouseWorld; + syncMouseControls = true; + mouseWorldListener = false; + } + if (mouseRotationListener && Math.Abs((mouseWorld - player.MountedCenter).ToRotation() - (oldMouseWorld - player.MountedCenter).ToRotation()) > 0.15f) + { + oldMouseWorld = mouseWorld; + syncMouseControls = true; + mouseRotationListener = false; + } + } + } #endregion #region PreUpdateBuffs diff --git a/CalPlayer/CalamityPlayerMiscEffects.cs b/CalPlayer/CalamityPlayerMiscEffects.cs index f49483f406..7427318ac1 100644 --- a/CalPlayer/CalamityPlayerMiscEffects.cs +++ b/CalPlayer/CalamityPlayerMiscEffects.cs @@ -135,7 +135,7 @@ namespace CalamityMod.CalPlayer // Update the gem tech armor set. modPlayer.GemTechState.Update(); - // Regularly sync player stats during multiplayer + // Regularly sync player stats & mouse control info during multiplayer if (player.whoAmI == Main.myPlayer && Main.netMode == NetmodeID.MultiplayerClient) { modPlayer.packetTimer++; @@ -144,8 +144,15 @@ namespace CalamityMod.CalPlayer modPlayer.packetTimer = 0; modPlayer.StandardSync(); } + + if (modPlayer.syncMouseControls) + { + modPlayer.syncMouseControls = false; + modPlayer.MouseControlsSync(); + } } + // After everything else, if Daawnlight Spirit Origin is equipped, set ranged crit to the base 4%. // Store all the crit so it can be used in damage calculations. if (modPlayer.spiritOrigin) diff --git a/CalPlayer/CalamityPlayerNetcode.cs b/CalPlayer/CalamityPlayerNetcode.cs index 278d7a0729..a8b64631f2 100644 --- a/CalPlayer/CalamityPlayerNetcode.cs +++ b/CalPlayer/CalamityPlayerNetcode.cs @@ -35,6 +35,12 @@ namespace CalamityMod.CalPlayer SyncLevel(false, 3); SyncLevel(false, 4); } + + internal void MouseControlsSync() + { + SyncRightClick(false); + SyncMousePosition(false); + } #endregion #region Creating and Sending Packets @@ -198,6 +204,24 @@ namespace CalamityMod.CalPlayer packet.Write(reforgeTierSafety); player.SendPacket(packet, server); } + + public void SyncRightClick(bool server) + { + ModPacket packet = mod.GetPacket(256); + packet.Write((byte)CalamityModMessageType.RightClickSync); + packet.Write(player.whoAmI); + packet.Write(mouseRight); + player.SendPacket(packet, server); + } + public void SyncMousePosition(bool server) + { + ModPacket packet = mod.GetPacket(256); + packet.Write((byte)CalamityModMessageType.MousePositionSync); + packet.Write(player.whoAmI); + packet.WriteVector2(mouseWorld); + player.SendPacket(packet, server); + } + #endregion #region Reading and Handling Packets @@ -323,6 +347,19 @@ namespace CalamityMod.CalPlayer if (Main.netMode == NetmodeID.Server) SyncDodgeCooldown(true); } + + internal void HandleRightClick(BinaryReader reader) + { + mouseRight = reader.ReadBoolean(); + if (Main.netMode == NetmodeID.Server) + SyncRightClick(true); + } + internal void HandleMousePosition(BinaryReader reader) + { + mouseWorld = reader.ReadVector2(); + if (Main.netMode == NetmodeID.Server) + SyncMousePosition(true); + } #endregion } } diff --git a/CalamityNetcode.cs b/CalamityNetcode.cs index 64f1b013b6..6183c30f20 100644 --- a/CalamityNetcode.cs +++ b/CalamityNetcode.cs @@ -280,11 +280,24 @@ namespace CalamityMod Main.player[reader.ReadInt32()].Calamity().HandleReforgeTierSafety(reader); break; - // - // Default case: with no idea how long the packet is, we can't safely read data. - // Throw an exception now instead of allowing the network stream to corrupt. - // - default: + // + // Mouse control syncs + // + + case CalamityModMessageType.RightClickSync: + Main.player[reader.ReadInt32()].Calamity().HandleRightClick(reader); + break; + case CalamityModMessageType.MousePositionSync: + Main.player[reader.ReadInt32()].Calamity().HandleMousePosition(reader); + break; + + + + // + // Default case: with no idea how long the packet is, we can't safely read data. + // Throw an exception now instead of allowing the network stream to corrupt. + // + default: CalamityMod.Instance.Logger.Error($"Failed to parse Calamity packet: No Calamity packet exists with ID {msgType}."); throw new Exception("Failed to parse Calamity packet: Invalid Calamity packet ID."); } @@ -399,6 +412,10 @@ namespace CalamityMod // Reforge syncs ItemTypeLastReforgedSync, // TODO -- there has to be a better way to do this, but I don't know what it is - ReforgeTierSafetySync + ReforgeTierSafetySync, + + // Mouse Controls syncs + RightClickSync, + MousePositionSync } } diff --git a/Items/Weapons/Ranged/RCCAnihilator.cs b/Items/Weapons/Ranged/RCCAnihilator.cs index 4f988875f5..5a33d2f5bf 100644 --- a/Items/Weapons/Ranged/RCCAnihilator.cs +++ b/Items/Weapons/Ranged/RCCAnihilator.cs @@ -38,7 +38,9 @@ namespace CalamityMod.Items.Weapons.Ranged item.knockBack = 4.25f; item.value = CalamityGlobalItem.Rarity10BuyPrice; item.rare = ItemRarityID.Red; - item.UseSound = SoundID.Item5; + item.noUseGraphic = true; + item.UseSound = SoundID.Item20; + item.channel = true; item.autoReuse = true; item.shoot = ProjectileID.PurificationPowder; item.shootSpeed = 15f; @@ -55,29 +57,13 @@ namespace CalamityMod.Items.Weapons.Ranged return clone; } - public override void NetSend(BinaryWriter writer) - { - writer.Write(RCChannel); - } - - public override void NetRecieve(BinaryReader reader) - { - RCChannel = reader.ReadBoolean(); - } - public override string Texture => "CalamityMod/Projectiles/Ranged/RCCHoldout"; //What. Huh. HUH? you got a problem with my file organization in my proof of concept HUH???? HUH???? public override bool AltFunctionUse(Player player) { return true; } - public override bool CanUseItem(Player player) - { - item.noUseGraphic = true; - item.UseSound = SoundID.Item20; - item.channel = true; - return (player.ownedProjectileCounts[ModContent.ProjectileType()] <= 0 && player.ownedProjectileCounts[ModContent.ProjectileType()] <= 0); - } + public override bool CanUseItem(Player player) =>(player.ownedProjectileCounts[ModContent.ProjectileType()] <= 0 && player.ownedProjectileCounts[ModContent.ProjectileType()] <= 0); public override bool Shoot(Player player, ref Vector2 position, ref float speedX, ref float speedY, ref int type, ref int damage, ref float knockBack) { @@ -89,8 +75,7 @@ namespace CalamityMod.Items.Weapons.Ranged Vector2 shootVelocity = new Vector2(speedX, speedY); Vector2 shootDirection = shootVelocity.SafeNormalize(Vector2.UnitX * player.direction); // Charge-up. Done via a holdout projectile. - Projectile.NewProjectile(position, shootDirection, ModContent.ProjectileType(), damage, knockBack, player.whoAmI); - RCChannel = true; + Projectile.NewProjectile(position, shootDirection, ModContent.ProjectileType(), damage, knockBack, player.whoAmI); } return false; } diff --git a/Projectiles/Ranged/RCCHoldout.cs b/Projectiles/Ranged/RCCHoldout.cs index f3c807929c..90ad4ffb6f 100644 --- a/Projectiles/Ranged/RCCHoldout.cs +++ b/Projectiles/Ranged/RCCHoldout.cs @@ -7,6 +7,7 @@ using Terraria.Graphics.Shaders; using Microsoft.Xna.Framework.Graphics; using CalamityMod.Items.Weapons.Ranged; using static Terraria.ModLoader.ModContent; +using CalamityMod.CalPlayer; namespace CalamityMod.Projectiles.Ranged { @@ -45,11 +46,18 @@ namespace CalamityMod.Projectiles.Ranged public override void AI() { + + CalamityPlayer controlsSync = Owner.GetModPlayer(); + if (Main.myPlayer == projectile.owner) + { + controlsSync.rightClickListener = true; + } + Vector2 armPosition = Owner.RotatedRelativePoint(Owner.MountedCenter, true); Vector2 tipPosition = armPosition + projectile.velocity * projectile.width * 0.5f; - // If the player releases left click, shoot out the arrows - if (!OwnerCanShoot) + // If the player releases left click, shoot out the arrows (also if its not the very first frame to avoid it killing itself before it sets the RCChannel variable to true + if (!OwnerCanShoot && FramesToLoadBolt != 0f) { if (LoadedBolts <= 0f) //If theres no arrows to shoot @@ -68,6 +76,7 @@ namespace CalamityMod.Projectiles.Ranged // Frame 1 effects: Initialize the shoot speed if (FramesToLoadBolt == 0f) { + (Owner.HeldItem.modItem as RCCAnihilator).RCChannel = true; FramesToLoadBolt = Owner.ActiveItem().useAnimation; } @@ -102,7 +111,7 @@ namespace CalamityMod.Projectiles.Ranged UpdateProjectileHeldVariables(armPosition); ManipulatePlayerVariables(); - if (!Main.mouseRight && Main.myPlayer == projectile.owner) + if (!controlsSync.mouseRight) (Owner.HeldItem.modItem as RCCAnihilator).RCChannel = false; }