From 6bbd11630db4c1c90d935e24915fceb854a20390 Mon Sep 17 00:00:00 2001 From: Regalis Date: Sat, 28 Jan 2017 12:40:43 +0200 Subject: [PATCH] Fabricator & deconstructor syncing --- .../Components/Machines/Deconstructor.cs | 50 +++++++++++-- .../Items/Components/Machines/Fabricator.cs | 73 +++++++++++++++++-- 2 files changed, 111 insertions(+), 12 deletions(-) diff --git a/Subsurface/Source/Items/Components/Machines/Deconstructor.cs b/Subsurface/Source/Items/Components/Machines/Deconstructor.cs index 716dfe503..d9c243d73 100644 --- a/Subsurface/Source/Items/Components/Machines/Deconstructor.cs +++ b/Subsurface/Source/Items/Components/Machines/Deconstructor.cs @@ -1,4 +1,6 @@ -using Microsoft.Xna.Framework; +using Barotrauma.Networking; +using Lidgren.Network; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; @@ -8,7 +10,7 @@ using System.Xml.Linq; namespace Barotrauma.Items.Components { - class Deconstructor : Powered + class Deconstructor : Powered, IServerSerializable, IClientSerializable { GUIProgressBar progressBar; GUIButton activateButton; @@ -16,9 +18,7 @@ namespace Barotrauma.Items.Components float progressTimer; ItemContainer container; - - private float lastNetworkUpdate; - + public Deconstructor(Item item, XElement element) : base(item, element) { @@ -100,6 +100,15 @@ namespace Barotrauma.Items.Components SetActive(!IsActive); currPowerConsumption = IsActive ? powerConsumption : 0.0f; + + if (GameMain.Server != null) + { + item.CreateServerEvent(this); + } + else if (GameMain.Client != null) + { + item.CreateClientEvent(this); + } return true; } @@ -129,8 +138,35 @@ namespace Barotrauma.Items.Components activateButton.Text = "Cancel"; } - container.Inventory.Locked = IsActive; - + container.Inventory.Locked = IsActive; + } + + + public void ClientWrite(NetBuffer msg, object[] extraData = null) + { + msg.Write(IsActive); + } + + public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c) + { + bool active = msg.ReadBoolean(); + + item.CreateServerEvent(this); + + if (item.CanClientAccess(c)) + { + SetActive(active); + } + } + + public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null) + { + msg.Write(IsActive); + } + + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) + { + SetActive(msg.ReadBoolean()); } } diff --git a/Subsurface/Source/Items/Components/Machines/Fabricator.cs b/Subsurface/Source/Items/Components/Machines/Fabricator.cs index b14ad084a..a644c4323 100644 --- a/Subsurface/Source/Items/Components/Machines/Fabricator.cs +++ b/Subsurface/Source/Items/Components/Machines/Fabricator.cs @@ -1,4 +1,6 @@ -using Microsoft.Xna.Framework; +using Barotrauma.Networking; +using Lidgren.Network; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; @@ -77,7 +79,7 @@ namespace Barotrauma.Items.Components } } - class Fabricator : Powered + class Fabricator : Powered, IServerSerializable, IClientSerializable { private List fabricableItems; @@ -94,9 +96,7 @@ namespace Barotrauma.Items.Components //used for checking if contained items have changed //(in which case we need to recheck which items can be fabricated) private Item[] prevContainedItems; - - private float lastNetworkUpdate; - + public Fabricator(Item item, XElement element) : base(item, element) { @@ -288,6 +288,15 @@ namespace Barotrauma.Items.Components { CancelFabricating(); } + + if (GameMain.Server != null) + { + item.CreateServerEvent(this); + } + else if (GameMain.Client != null) + { + item.CreateClientEvent(this); + } return true; } @@ -437,6 +446,60 @@ namespace Barotrauma.Items.Components return true; } + + public void ClientWrite(NetBuffer msg, object[] extraData = null) + { + int itemIndex = fabricatedItem == null ? -1 : fabricableItems.IndexOf(fabricatedItem); + msg.WriteRangedInteger(-1, fabricableItems.Count - 1, itemIndex); + } + + public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c) + { + int itemIndex = msg.ReadRangedInteger(-1, fabricableItems.Count - 1); + + item.CreateServerEvent(this); + + if (!item.CanClientAccess(c)) return; + + if (itemIndex == -1) + { + CancelFabricating(); + } + else + { + //if already fabricating the selected item, return + if (fabricatedItem != null && fabricableItems.IndexOf(fabricatedItem) == itemIndex) return; + if (itemIndex < 0 || itemIndex >= fabricableItems.Count) return; + + SelectItem(null, fabricableItems[itemIndex]); + StartFabricating(fabricableItems[itemIndex]); + } + } + + public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null) + { + int itemIndex = fabricatedItem == null ? -1 : fabricableItems.IndexOf(fabricatedItem); + msg.WriteRangedInteger(-1, fabricableItems.Count - 1, itemIndex); + } + + public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime) + { + int itemIndex = msg.ReadRangedInteger(-1, fabricableItems.Count - 1); + + if (itemIndex == -1) + { + CancelFabricating(); + } + else + { + //if already fabricating the selected item, return + if (fabricatedItem != null && fabricableItems.IndexOf(fabricatedItem) == itemIndex) return; + if (itemIndex < 0 || itemIndex >= fabricableItems.Count) return; + + SelectItem(null, fabricableItems[itemIndex]); + StartFabricating(fabricableItems[itemIndex]); + } + } } }