From d4b0d7539fb1a2d1db205835600c8c6e20c6c79b Mon Sep 17 00:00:00 2001 From: cinder Date: Tue, 21 Jan 2025 18:15:15 -0600 Subject: [PATCH] Misc cleanup in AgentManagerMovement --- LibreMetaverse/AgentManagerMovement.cs | 133 +++++++++++++------------ 1 file changed, 68 insertions(+), 65 deletions(-) diff --git a/LibreMetaverse/AgentManagerMovement.cs b/LibreMetaverse/AgentManagerMovement.cs index afcb19fa..23d429cd 100644 --- a/LibreMetaverse/AgentManagerMovement.cs +++ b/LibreMetaverse/AgentManagerMovement.cs @@ -1,5 +1,6 @@ /* * Copyright (c) 2006-2016, openmetaverse.co + * Copyright (c) 2025, Sjofn LLC. * All rights reserved. * * - Redistribution and use in source and binary forms, with or without @@ -445,7 +446,7 @@ namespace OpenMetaverse #endregion Change tracking private bool alwaysRun; - private GridClient Client; + private readonly GridClient Client; private int duplicateCount; private AgentState lastState; /// Timer for sending AgentUpdate packets @@ -464,11 +465,8 @@ namespace OpenMetaverse private void CleanupTimer() { - if (updateTimer != null) - { - updateTimer.Dispose(); - updateTimer = null; - } + updateTimer?.Dispose(); + updateTimer = null; } private void Network_OnDisconnected(object sender, DisconnectedEventArgs e) @@ -483,7 +481,7 @@ namespace OpenMetaverse CleanupTimer(); if (Client.Settings.SEND_AGENT_UPDATES_REGULARLY) { - updateTimer = new Timer(new TimerCallback(UpdateTimer_Elapsed), null, updateInterval, updateInterval); + updateTimer = new Timer(UpdateTimer_Elapsed, null, updateInterval, updateInterval); } } } @@ -523,48 +521,38 @@ namespace OpenMetaverse /// /// Region coordinates to turn toward /// whether to send update or not + /// Returns if TurnToward operation was successful public bool TurnToward(Vector3 target, bool sendUpdate) { - if (Client.Settings.SEND_AGENT_UPDATES) - { - Quaternion parentRot = Quaternion.Identity; - - if (Client.Self.SittingOn > 0) - { - if (!Client.Network.CurrentSim.ObjectsPrimitives.TryGetValue(Client.Self.SittingOn, out var parent)) - { - Logger.Log("Attempted TurnToward but parent prim is not in dictionary", Helpers.LogLevel.Warning, Client); - return false; - } - - parentRot = parent.Rotation; - } - - Quaternion between = Vector3.RotationBetween(Vector3.UnitX, Vector3.Normalize(target - Client.Self.SimPosition)); - Quaternion rot = between * (Quaternion.Identity / parentRot); - - BodyRotation = rot; - HeadRotation = rot; - Camera.LookAt(Client.Self.SimPosition, target); - - if (sendUpdate) SendUpdate(); - - return true; - } - else + if (!Client.Settings.SEND_AGENT_UPDATES) { Logger.Log("Attempted TurnToward but agent updates are disabled", Helpers.LogLevel.Warning, Client); return false; } - } - /// - /// Send new AgentUpdate packet to update our current camera - /// position and rotation - /// - public void SendUpdate() - { - SendUpdate(false, Client.Network.CurrentSim); + Quaternion parentRot = Quaternion.Identity; + + if (Client.Self.SittingOn > 0) + { + if (!Client.Network.CurrentSim.ObjectsPrimitives.TryGetValue(Client.Self.SittingOn, out var parent)) + { + Logger.Log("Attempted TurnToward but parent prim is not in dictionary", Helpers.LogLevel.Warning, Client); + return false; + } + + parentRot = parent.Rotation; + } + + Quaternion between = Vector3.RotationBetween(Vector3.UnitX, Vector3.Normalize(target - Client.Self.SimPosition)); + Quaternion rot = between * (Quaternion.Identity / parentRot); + + BodyRotation = rot; + HeadRotation = rot; + Camera.LookAt(Client.Self.SimPosition, target); + + if (sendUpdate) { SendUpdate(); } + + return true; } /// @@ -573,7 +561,7 @@ namespace OpenMetaverse /// /// Whether to require server acknowledgement /// of this packet - public void SendUpdate(bool reliable) + public void SendUpdate(bool reliable = false) { SendUpdate(reliable, Client.Network.CurrentSim); } @@ -589,8 +577,10 @@ namespace OpenMetaverse { // Since version 1.40.4 of the Linden simulator, sending this update // causes corruption of the agent position in the simulator - if (simulator != null && (!simulator.AgentMovementComplete)) + if (simulator != null && !simulator.AgentMovementComplete) + { return; + } Vector3 origin = Camera.Position; Vector3 xAxis = Camera.LeftAxis; @@ -646,7 +636,8 @@ namespace OpenMetaverse Client.Network.SendPacket(update, simulator); - if (AutoResetControls) { + if (AutoResetControls) + { ResetControlFlags(); } } @@ -677,20 +668,24 @@ namespace OpenMetaverse if (Client.Network.CurrentSim != null && (!Client.Network.CurrentSim.HandshakeComplete)) return; - AgentUpdatePacket update = new AgentUpdatePacket(); - - update.AgentData.AgentID = Client.Self.AgentID; - update.AgentData.SessionID = Client.Self.SessionID; - update.AgentData.BodyRotation = bodyRotation; - update.AgentData.HeadRotation = headRotation; - update.AgentData.CameraCenter = position; - update.AgentData.CameraAtAxis = forwardAxis; - update.AgentData.CameraLeftAxis = leftAxis; - update.AgentData.CameraUpAxis = upAxis; - update.AgentData.Far = farClip; - update.AgentData.ControlFlags = (uint)controlFlags; - update.AgentData.Flags = (byte)flags; - update.AgentData.State = (byte)state; + AgentUpdatePacket update = new AgentUpdatePacket + { + AgentData = + { + AgentID = Client.Self.AgentID, + SessionID = Client.Self.SessionID, + BodyRotation = bodyRotation, + HeadRotation = headRotation, + CameraCenter = position, + CameraAtAxis = forwardAxis, + CameraLeftAxis = leftAxis, + CameraUpAxis = upAxis, + Far = farClip, + ControlFlags = (uint)controlFlags, + Flags = (byte)flags, + State = (byte)state + } + }; update.Header.Reliable = reliable; @@ -726,12 +721,20 @@ namespace OpenMetaverse /// Angle in radians public void SetFOVVerticalAngle(float angle) { - OpenMetaverse.Packets.AgentFOVPacket msg = new OpenMetaverse.Packets.AgentFOVPacket(); - msg.AgentData.AgentID = Client.Self.AgentID; - msg.AgentData.SessionID = Client.Self.SessionID; - msg.AgentData.CircuitCode = Client.Network.CircuitCode; - msg.FOVBlock.GenCounter = 0; - msg.FOVBlock.VerticalAngle = angle; + OpenMetaverse.Packets.AgentFOVPacket msg = new OpenMetaverse.Packets.AgentFOVPacket + { + AgentData = + { + AgentID = Client.Self.AgentID, + SessionID = Client.Self.SessionID, + CircuitCode = Client.Network.CircuitCode + }, + FOVBlock = + { + GenCounter = 0, + VerticalAngle = angle + } + }; Client.Network.SendPacket(msg); }