Wiring syncing

This commit is contained in:
Regalis
2017-01-27 20:59:41 +02:00
parent 4589c2bcfb
commit 0570956e24
4 changed files with 161 additions and 28 deletions
@@ -145,7 +145,6 @@ namespace Barotrauma.Items.Components
if (Wires[i] == null)
{
Wires[i] = wire;
UpdateRecipients();
return;
}
}
@@ -154,14 +153,8 @@ namespace Barotrauma.Items.Components
public void AddLink(int index, Wire wire)
{
Wires[index] = wire;
UpdateRecipients();
}
public void UpdateRecipients()
{
}
public void SendSignal(int stepsTaken, string signal, Item sender, float power)
{
for (int i = 0; i < MaxLinked; i++)
@@ -286,8 +279,15 @@ namespace Barotrauma.Items.Components
if (!PlayerInput.LeftButtonHeld())
{
//panel.Item.NewComponentEvent(panel, true, true);
//draggingConnected.Drop(Character);
if (GameMain.Client != null)
{
panel.Item.CreateClientEvent<ConnectionPanel>(panel);
}
else if (GameMain.Server != null)
{
panel.Item.CreateServerEvent<ConnectionPanel>(panel);
}
draggingConnected = null;
}
}
@@ -464,8 +464,7 @@ namespace Barotrauma.Items.Components
public void ConnectLinked()
{
if (wireId == null) return;
for (int i = 0; i < MaxLinked; i++)
{
if (wireId[i] == 0) continue;
@@ -481,10 +480,6 @@ namespace Barotrauma.Items.Components
Wires[i].Connect(this, false, true);
}
}
UpdateRecipients();
//wireId = null;
}
}
@@ -1,12 +1,15 @@
using Microsoft.Xna.Framework;
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Linq;
using Lidgren.Network;
namespace Barotrauma.Items.Components
{
class ConnectionPanel : ItemComponent
class ConnectionPanel : ItemComponent, IServerSerializable, IClientSerializable
{
public static Wire HighlightedWire;
@@ -157,9 +160,110 @@ namespace Barotrauma.Items.Components
}
}
protected override void ShallowRemoveComponentSpecific()
public void ClientWrite(NetBuffer msg, object[] extraData = null)
{
foreach (Connection connection in Connections)
{
Wire[] wires = Array.FindAll(connection.Wires, w => w != null);
msg.WriteRangedInteger(0, Connection.MaxLinked, wires.Length);
for (int i = 0; i < wires.Length; i++)
{
msg.Write(wires[i].Item.ID);
}
}
}
public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c)
{
int[] wireCounts = new int[Connections.Count];
Wire[,] wires = new Wire[Connections.Count, Connection.MaxLinked];
//read wire IDs for each connection
for (int i = 0; i < Connections.Count; i++)
{
wireCounts[i] = msg.ReadRangedInteger(0, Connection.MaxLinked);
for (int j = 0; j < wireCounts[i]; j++)
{
ushort wireId = msg.ReadUInt16();
Item wireItem = MapEntity.FindEntityByID(wireId) as Item;
if (wireItem == null) continue;
Wire wireComponent = wireItem.GetComponent<Wire>();
if (wireComponent != null)
{
wires[i, j] = wireComponent;
}
}
}
item.CreateServerEvent<ConnectionPanel>(this);
//check if the character can access this connectionpanel
//and all the wires they're trying to connect
if (!item.CanClientAccess(c)) return;
foreach (Wire wire in wires)
{
if (wire != null)
{
//wire not found in any of the connections yet (client is trying to connect a new wire)
// -> we need to check if the client has access to it
if (!Connections.Any(connection => connection.Wires.Contains(wire)))
{
if (!wire.Item.CanClientAccess(c)) return;
}
}
}
Networking.GameServer.Log(item.Name + " rewired by " + c.Character.Name, Color.Orange);
//update the connections
for (int i = 0; i < Connections.Count; i++)
{
Connections[i].ClearConnections();
for (int j = 0; j < wireCounts[i]; j++)
{
if (wires[i, j] == null) continue;
Connections[i].Wires[j] = wires[i,j];
wires[i, j].Connect(Connections[i], false);
var otherConnection = Connections[i].Wires[j].OtherConnection(Connections[i]);
Networking.GameServer.Log(
item.Name + " (" + Connections[i].Name + ") -> " +
(otherConnection == null ? "none" : otherConnection.Item.Name + " (" + (otherConnection.Name) + ")"), Color.Orange);
}
}
}
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
{
ClientWrite(msg, extraData);
}
public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime)
{
foreach (Connection connection in Connections)
{
connection.ClearConnections();
int wireCount = msg.ReadRangedInteger(0, Connection.MaxLinked);
for (int i = 0; i < wireCount; i++)
{
ushort wireId = msg.ReadUInt16();
Item wireItem = MapEntity.FindEntityByID(wireId) as Item;
if (wireItem == null) continue;
Wire wireComponent = wireItem.GetComponent<Wire>();
if (wireComponent == null) continue;
connection.Wires[i] = wireComponent;
wireComponent.Connect(connection, false);
}
}
}
}
}
@@ -1,4 +1,6 @@
using Microsoft.Xna.Framework;
using Barotrauma.Networking;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
@@ -9,7 +11,7 @@ using System.Xml.Linq;
namespace Barotrauma.Items.Components
{
class Wire : ItemComponent, IDrawableComponent
class Wire : ItemComponent, IDrawableComponent, IServerSerializable, IClientSerializable
{
class WireSection
{
@@ -120,7 +122,6 @@ namespace Barotrauma.Items.Components
if (connections[i].Wires[n] != this) continue;
connections[i].Wires[n] = null;
connections[i].UpdateRecipients();
}
connections[i] = null;
}
@@ -197,7 +198,14 @@ namespace Barotrauma.Items.Components
if (!loading)
{
//Item.NewComponentEvent(this, true, true);
if (GameMain.Server != null)
{
item.CreateServerEvent(this);
}
else if (GameMain.Client != null)
{
item.CreateClientEvent(this);
}
//the wire is active if only one end has been connected
IsActive = connections[0] == null ^ connections[1] == null;
}
@@ -708,7 +716,7 @@ namespace Barotrauma.Items.Components
base.RemoveComponentSpecific();
}
public void ClientWrite(Lidgren.Network.NetBuffer msg, object[] extraData = null)
public void ClientWrite(NetBuffer msg, object[] extraData = null)
{
msg.Write((byte)Math.Min(nodes.Count, 255));
for (int i = 0; i < Math.Min(nodes.Count, 255); i++)
@@ -718,7 +726,33 @@ namespace Barotrauma.Items.Components
}
}
public void ServerRead(Lidgren.Network.NetIncomingMessage msg, Barotrauma.Networking.Client c)
public void ServerRead(ClientNetObject type, NetIncomingMessage msg, Client c)
{
nodes.Clear();
int nodeCount = msg.ReadByte();
Vector2[] nodePositions = new Vector2[nodeCount];
for (int i = 0; i < nodeCount; i++)
{
nodePositions[i] = new Vector2(msg.ReadFloat(), msg.ReadFloat());
}
if (!item.CanClientAccess(c)) return;
if (nodePositions.Any(n => !MathUtils.IsValid(n))) return;
nodes = nodePositions.ToList();
UpdateSections();
Drawable = nodes.Any();
}
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
{
ClientWrite(msg, extraData);
}
public void ClientRead(ServerNetObject type, NetIncomingMessage msg, float sendingTime)
{
nodes.Clear();
@@ -726,7 +760,7 @@ namespace Barotrauma.Items.Components
for (int i = 0; i < nodeCount; i++)
{
Vector2 newNode = new Vector2(msg.ReadFloat(), msg.ReadFloat());
if (MathUtils.IsValid(newNode)) nodes.Add(newNode);
if (MathUtils.IsValid(newNode)) nodes.Add(newNode);
}
UpdateSections();
+1 -1
View File
@@ -572,7 +572,7 @@ namespace Barotrauma
if (GameMain.Server != null)
{
if (!c.Character.CanAccessItem(item)) continue;
if (!item.CanClientAccess(c)) continue;
}
TryPutItem(item, i, true, false);
}