From 74ef5c6ea6030e492005a79e9892dfee618f2c76 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 28 Feb 2018 14:01:38 +0200 Subject: [PATCH] Fixed items attached mid-round by other clients or the host being impossible to interact with and occasionally being attached to an incorrect position. Closes #296 --- .../Items/Components/Holdable/Holdable.cs | 29 ++++++++++++++----- .../Items/Components/Holdable/Pickable.cs | 2 +- .../Source/Items/Components/ItemComponent.cs | 8 +++++ .../Items/Components/Power/PowerTransfer.cs | 14 +++------ .../BarotraumaShared/Source/Items/Item.cs | 5 ++++ 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Holdable.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Holdable.cs index 6ab3087e7..f37c5e456 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Holdable.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Holdable.cs @@ -126,7 +126,15 @@ namespace Barotrauma.Items.Components public override void Drop(Character dropper) { - DropConnectedWires(dropper); + Drop(true, dropper); + } + + private void Drop(bool dropConnectedWires, Character dropper) + { + if (dropConnectedWires) + { + DropConnectedWires(dropper); + } if (attachable) { @@ -147,7 +155,7 @@ namespace Barotrauma.Items.Components picker = dropper; } if (picker.Inventory == null) return; - + item.Submarine = picker.Submarine; if (item.body != null) { @@ -360,6 +368,12 @@ namespace Barotrauma.Items.Components item.body.Dir = -item.body.Dir; } + public override void OnItemLoaded() + { + if (item.Submarine != null && item.Submarine.Loading) return; + OnMapLoaded(); + } + public override void OnMapLoaded() { if (!attachable) return; @@ -384,17 +398,20 @@ namespace Barotrauma.Items.Components public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null) { - if (!attachable) + if (!attachable || body == null) { DebugConsole.ThrowError("Sent an attachment event for an item that's not attachable."); } msg.Write(Attached); + msg.Write(body.SimPosition.X); + msg.Write(body.SimPosition.Y); } public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime) { bool isAttached = msg.ReadBoolean(); + Vector2 simPosition = new Vector2(msg.ReadFloat(), msg.ReadFloat()); if (!attachable) { @@ -404,10 +421,8 @@ namespace Barotrauma.Items.Components if (isAttached) { - if (item.ParentInventory != null) - { - item.ParentInventory.RemoveItem(item); - } + Drop(false, null); + item.SetTransform(simPosition, 0.0f); AttachToWall(); } else diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Pickable.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Pickable.cs index 482aee51f..72319fea4 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Pickable.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/Pickable.cs @@ -208,7 +208,7 @@ namespace Barotrauma.Items.Components if (item.body != null && !item.body.Enabled) { - item.body.ResetDynamics(); + item.body.ResetDynamics(); item.SetTransform(bodyDropPos, 0.0f); item.body.Enabled = true; } diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs index fc8bc3c02..abb75ebf3 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs @@ -575,7 +575,15 @@ namespace Barotrauma.Items.Components } } + /// + /// Called when all items have been loaded. Use to initialize connections between items. + /// public virtual void OnMapLoaded() { } + + /// + /// Called when all the components of the item have been loaded. Use to initialize connections between components and such. + /// + public virtual void OnItemLoaded() { } public static ItemComponent Load(XElement element, Item item, string file, bool errorMessages = true) { diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Power/PowerTransfer.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Power/PowerTransfer.cs index 3bcd01d07..f1f19d80d 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Power/PowerTransfer.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Power/PowerTransfer.cs @@ -23,12 +23,6 @@ namespace Barotrauma.Items.Components { get { - if (powerConnections == null) - { - var connections = Item.Connections; - powerConnections = connections == null ? new List() : connections.FindAll(c => c.IsPower); - - } return powerConnections; } } @@ -338,18 +332,18 @@ namespace Barotrauma.Items.Components connectionDirty[connection] = true; } - public override void OnMapLoaded() + public override void OnItemLoaded() { - var connections = item.Connections; + var connections = Item.Connections; + powerConnections = connections == null ? new List() : connections.FindAll(c => c.IsPower); if (connections == null) { IsActive = false; return; } - SetAllConnectionsDirty(); } - + public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power) { base.ReceiveSignal(stepsTaken, signal, connection, source, sender, power); diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index 77f91b451..b475fb2fe 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -461,6 +461,11 @@ namespace Barotrauma InsertToList(); ItemList.Add(this); + + foreach (ItemComponent ic in components) + { + ic.OnItemLoaded(); + } } public override MapEntity Clone()