ItemInventories don't have own ID's anymore but rely on owner ID, relaying reliablemessages through server
This commit is contained in:
@@ -95,8 +95,8 @@ namespace Barotrauma
|
||||
message.Write(LargeUpdateTimer <= 0);
|
||||
|
||||
message.Write(AnimController.TargetDir == Direction.Right);
|
||||
message.Write(AnimController.TargetMovement.X);
|
||||
message.Write(AnimController.TargetMovement.Y);
|
||||
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.X, -10.0f, 10.0f), -10.0f, 10.0f, 16);
|
||||
message.WriteRangedSingle(MathHelper.Clamp(AnimController.TargetMovement.Y, -10.0f, 10.0f), -10.0f, 10.0f, 16);
|
||||
|
||||
if (LargeUpdateTimer <= 0)
|
||||
{
|
||||
@@ -165,8 +165,8 @@ namespace Barotrauma
|
||||
try
|
||||
{
|
||||
targetDir = message.ReadBoolean();
|
||||
targetMovement.X = message.ReadFloat();
|
||||
targetMovement.Y = message.ReadFloat();
|
||||
targetMovement.X = message.ReadRangedSingle(-10.0f, 10.0f, 8);
|
||||
targetMovement.Y = message.ReadRangedSingle(-10.0f, 10.0f, 8);
|
||||
|
||||
}
|
||||
catch
|
||||
|
||||
@@ -1100,6 +1100,11 @@ namespace Barotrauma
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (type == NetworkEventType.InventoryUpdate)
|
||||
{
|
||||
if (inventory == null) return false;
|
||||
return inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
|
||||
}
|
||||
|
||||
var hasInputs =
|
||||
(GetInputState(InputType.Left) ||
|
||||
@@ -1229,6 +1234,12 @@ namespace Barotrauma
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (type == NetworkEventType.InventoryUpdate)
|
||||
{
|
||||
if (inventory == null) return;
|
||||
inventory.ReadNetworkData(NetworkEventType.InventoryUpdate, message);
|
||||
return;
|
||||
}
|
||||
|
||||
bool actionKeyState = false;
|
||||
bool secondaryKeyState = false;
|
||||
@@ -1393,8 +1404,6 @@ namespace Barotrauma
|
||||
|
||||
if (GameMain.Client!=null && GameMain.Client.Character == this) GameMain.Client.Character = null;
|
||||
|
||||
if (inventory != null) inventory.Remove();
|
||||
|
||||
if (aiTarget != null)
|
||||
aiTarget.Remove();
|
||||
|
||||
|
||||
@@ -25,15 +25,14 @@ namespace Barotrauma
|
||||
private Vector2[] slotPositions;
|
||||
|
||||
public CharacterInventory(int capacity, Character character)
|
||||
: base(capacity)
|
||||
: base(character, capacity)
|
||||
{
|
||||
this.character = character;
|
||||
|
||||
if (icons == null) icons = TextureLoader.FromFile("Content/UI/inventoryIcons.png");
|
||||
|
||||
slotPositions = new Vector2[limbSlots.Length];
|
||||
|
||||
|
||||
|
||||
int rectWidth = 40, rectHeight = 40;
|
||||
int spacing = 10;
|
||||
for (int i = 0; i < slotPositions.Length; i++)
|
||||
@@ -123,16 +122,19 @@ namespace Barotrauma
|
||||
if (allowedSlots.HasFlag(limbSlots[i]) && items[i]!=null) return false;
|
||||
}
|
||||
|
||||
bool placed = false;
|
||||
for (int i = 0; i < capacity; i++)
|
||||
{
|
||||
if (allowedSlots.HasFlag(limbSlots[i]) && items[i] == null)
|
||||
{
|
||||
PutItem(item, i, createNetworkEvent);
|
||||
PutItem(item, i, createNetworkEvent, !placed);
|
||||
item.Equip(character);
|
||||
return true;
|
||||
placed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (placed) return true;
|
||||
|
||||
if (allowedSlots.HasFlag(LimbSlot.BothHands)) TryPutItem(item, 3, createNetworkEvent);
|
||||
|
||||
return false;
|
||||
@@ -158,7 +160,7 @@ namespace Barotrauma
|
||||
Inventory otherInventory = items[i].inventory;
|
||||
if (otherInventory!=null)
|
||||
{
|
||||
new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.ID, true);
|
||||
new Networking.NetworkEvent(Networking.NetworkEventType.InventoryUpdate, otherInventory.Owner.ID, true);
|
||||
}
|
||||
|
||||
combined = true;
|
||||
|
||||
@@ -89,7 +89,7 @@ namespace Barotrauma.Items.Components
|
||||
public ItemContainer(Item item, XElement element)
|
||||
: base (item, element)
|
||||
{
|
||||
inventory = new ItemInventory(this, capacity, hudPos, slotsPerRow);
|
||||
inventory = new ItemInventory(item, this, capacity, hudPos, slotsPerRow);
|
||||
containableItems = new List<RelatedItem>();
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
|
||||
@@ -8,11 +8,13 @@ using System;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class Inventory : Entity
|
||||
class Inventory
|
||||
{
|
||||
public static Item draggingItem;
|
||||
public static Item doubleClickedItem;
|
||||
|
||||
public readonly Entity Owner;
|
||||
|
||||
private int slotsPerRow;
|
||||
|
||||
public int SlotsPerRow
|
||||
@@ -39,10 +41,12 @@ namespace Barotrauma
|
||||
|
||||
public Item[] items;
|
||||
|
||||
public Inventory(int capacity, Vector2? centerPos = null, int slotsPerRow=5)
|
||||
public Inventory(Entity owner, int capacity, Vector2? centerPos = null, int slotsPerRow=5)
|
||||
{
|
||||
this.capacity = capacity;
|
||||
|
||||
this.Owner = owner;
|
||||
|
||||
this.slotsPerRow = slotsPerRow;
|
||||
|
||||
items = new Item[capacity];
|
||||
@@ -95,6 +99,7 @@ namespace Barotrauma
|
||||
|
||||
public virtual bool TryPutItem(Item item, int i, bool createNetworkEvent = true)
|
||||
{
|
||||
if (Owner == null) return false;
|
||||
if (CanBePut(item,i))
|
||||
{
|
||||
PutItem(item, i, createNetworkEvent);
|
||||
@@ -108,6 +113,8 @@ namespace Barotrauma
|
||||
|
||||
protected void PutItem(Item item, int i, bool createNetworkEvent, bool removeItem = true)
|
||||
{
|
||||
if (Owner == null) return;
|
||||
|
||||
if (item.inventory != null && removeItem)
|
||||
{
|
||||
item.Drop();
|
||||
@@ -121,7 +128,7 @@ namespace Barotrauma
|
||||
item.body.Enabled = false;
|
||||
}
|
||||
|
||||
if (createNetworkEvent) new NetworkEvent(NetworkEventType.InventoryUpdate, ID, true);
|
||||
if (createNetworkEvent) new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true);
|
||||
}
|
||||
|
||||
public void RemoveItem(Item item)
|
||||
@@ -136,7 +143,6 @@ namespace Barotrauma
|
||||
|
||||
protected virtual void DropItem(Item item)
|
||||
{
|
||||
|
||||
item.Drop(null, false);
|
||||
return;
|
||||
}
|
||||
@@ -185,8 +191,11 @@ namespace Barotrauma
|
||||
}
|
||||
else
|
||||
{
|
||||
int[] data = { draggingItem.ID, -1 };
|
||||
new NetworkEvent(NetworkEventType.InventoryUpdate, ID, true, data);
|
||||
if (Owner!=null)
|
||||
{
|
||||
int[] data = { draggingItem.ID, -1 };
|
||||
new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true, data);
|
||||
}
|
||||
|
||||
DropItem(draggingItem);
|
||||
}
|
||||
@@ -279,7 +288,7 @@ namespace Barotrauma
|
||||
spriteBatch.DrawString(GUI.Font, (int)item.Condition + " %", new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), Color.Red);
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
public bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
{
|
||||
for (int i = 0; i<capacity; i++)
|
||||
{
|
||||
@@ -289,7 +298,7 @@ namespace Barotrauma
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
public void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
{
|
||||
int[] newItemIDs = new int[capacity];
|
||||
|
||||
@@ -315,7 +324,7 @@ namespace Barotrauma
|
||||
continue;
|
||||
}
|
||||
|
||||
Item item = FindEntityByID(newItemIDs[i]) as Item;
|
||||
Item item = Entity.FindEntityByID(newItemIDs[i]) as Item;
|
||||
if (item == null) continue;
|
||||
|
||||
TryPutItem(item, i, false);
|
||||
|
||||
@@ -1169,6 +1169,10 @@ namespace Barotrauma
|
||||
message.Write(body.SimPosition.Y);
|
||||
}
|
||||
break;
|
||||
case NetworkEventType.InventoryUpdate:
|
||||
var itemContainer = GetComponent<ItemContainer>();
|
||||
if (itemContainer == null || itemContainer.inventory == null) return false;
|
||||
return itemContainer.inventory.FillNetworkData(NetworkEventType.InventoryUpdate, message, data);
|
||||
case NetworkEventType.UpdateComponent:
|
||||
|
||||
int componentIndex = (int)data;
|
||||
@@ -1233,6 +1237,11 @@ namespace Barotrauma
|
||||
SetTransform(newSimPos, body.Rotation);
|
||||
Drop(null, false);
|
||||
break;
|
||||
case NetworkEventType.InventoryUpdate:
|
||||
var itemContainer = GetComponent<ItemContainer>();
|
||||
if (itemContainer == null || itemContainer.inventory == null) return;
|
||||
itemContainer.inventory.ReadNetworkData(NetworkEventType.DropItem, message);
|
||||
break;
|
||||
case NetworkEventType.UpdateComponent:
|
||||
int componentIndex = message.ReadByte();
|
||||
if (componentIndex < 0 || componentIndex > components.Count - 1) return;
|
||||
|
||||
@@ -7,8 +7,8 @@ namespace Barotrauma
|
||||
{
|
||||
ItemContainer container;
|
||||
|
||||
public ItemInventory(ItemContainer container, int capacity, Vector2? centerPos = null, int slotsPerRow = 5)
|
||||
: base(capacity, centerPos, slotsPerRow)
|
||||
public ItemInventory(Item owner, ItemContainer container, int capacity, Vector2? centerPos = null, int slotsPerRow = 5)
|
||||
: base(owner, capacity, centerPos, slotsPerRow)
|
||||
{
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@@ -649,7 +649,6 @@ namespace Barotrauma.Networking
|
||||
string newName = inc.ReadString();
|
||||
int ID = inc.ReadInt32();
|
||||
bool isFemale = inc.ReadBoolean();
|
||||
int inventoryID = inc.ReadInt32();
|
||||
|
||||
int headSpriteID = inc.ReadInt32();
|
||||
|
||||
@@ -683,7 +682,6 @@ namespace Barotrauma.Networking
|
||||
new Character(ch, closestWaypoint, !isMyCharacter);
|
||||
|
||||
character.ID = ID;
|
||||
character.Inventory.ID = inventoryID;
|
||||
|
||||
character.GiveJobItems(closestWaypoint);
|
||||
|
||||
|
||||
@@ -282,10 +282,10 @@ namespace Barotrauma.Networking
|
||||
{
|
||||
ReadMessage(inc);
|
||||
}
|
||||
catch
|
||||
catch (Exception e)
|
||||
{
|
||||
#if DEBUG
|
||||
DebugConsole.ThrowError("Failed to read incoming message");
|
||||
DebugConsole.ThrowError("Failed to read incoming message", e);
|
||||
#endif
|
||||
|
||||
continue;
|
||||
@@ -440,10 +440,13 @@ namespace Barotrauma.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
bool isReliable = false;
|
||||
if (packetType == (byte)PacketTypes.ReliableMessage)
|
||||
{
|
||||
if (!dataSender.ReliableChannel.CheckMessage(inc)) return;
|
||||
packetType = inc.ReadByte();
|
||||
|
||||
isReliable = true;
|
||||
}
|
||||
|
||||
switch (packetType)
|
||||
@@ -452,21 +455,40 @@ namespace Barotrauma.Networking
|
||||
if (!gameStarted) break;
|
||||
if (!NetworkEvent.ReadData(inc)) break;
|
||||
|
||||
outmsg = server.CreateMessage();
|
||||
outmsg.Write(inc);
|
||||
|
||||
List<NetConnection> recipients = new List<NetConnection>();
|
||||
|
||||
foreach (Client client in connectedClients)
|
||||
{
|
||||
if (client.Connection == inc.SenderConnection) continue;
|
||||
if (!client.inGame) continue;
|
||||
|
||||
recipients.Add(client.Connection);
|
||||
}
|
||||
|
||||
List<Client> recipients = connectedClients.FindAll(c => c.Connection != inc.SenderConnection && c.inGame);
|
||||
if (recipients.Count == 0) break;
|
||||
server.SendMessage(outmsg, recipients, inc.DeliveryMethod, 0);
|
||||
|
||||
//foreach (Client client in connectedClients)
|
||||
//{
|
||||
// if (client.Connection == inc.SenderConnection) continue;
|
||||
// if (!client.inGame) continue;
|
||||
|
||||
// recipients.Add(client.Connection);
|
||||
//}
|
||||
|
||||
if (isReliable)
|
||||
{
|
||||
Debug.WriteLine("receiver reliable networkevent");
|
||||
foreach (Client c in recipients)
|
||||
{
|
||||
var reliableMessage = c.ReliableChannel.CreateMessage();
|
||||
inc.Position = 8+16;
|
||||
byte[] messageBytes = inc.ReadBytes(inc.LengthBytes-3);
|
||||
reliableMessage.InnerMessage.Write(messageBytes);
|
||||
|
||||
c.ReliableChannel.SendMessage(reliableMessage, c.Connection);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
outmsg = server.CreateMessage();
|
||||
outmsg.Write(inc);
|
||||
|
||||
List<NetConnection> recipientConnections = new List<NetConnection>();
|
||||
foreach (Client c in recipients) recipientConnections.Add(c.Connection);
|
||||
|
||||
server.SendMessage(outmsg, recipientConnections, inc.DeliveryMethod, 0);
|
||||
}
|
||||
|
||||
break;
|
||||
case (byte)PacketTypes.Chatmessage:
|
||||
@@ -628,36 +650,35 @@ namespace Barotrauma.Networking
|
||||
|
||||
if (recipients.Count == 0) return;
|
||||
|
||||
|
||||
|
||||
foreach (NetworkEvent networkEvent in NetworkEvent.events)
|
||||
{
|
||||
Entity e = Entity.FindEntityByID(networkEvent.ID);
|
||||
if (e == null) continue;
|
||||
NetOutgoingMessage message = server.CreateMessage();
|
||||
message.Write((byte)PacketTypes.NetworkEvent);
|
||||
//if (!networkEvent.IsClient) continue;
|
||||
|
||||
if (!networkEvent.FillData(message))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//Entity e = Entity.FindEntityByID(networkEvent.ID);
|
||||
//if (e == null) continue;
|
||||
if (networkEvent.IsImportant)
|
||||
{
|
||||
foreach (Client c in recipients)
|
||||
{
|
||||
ReliableMessage reliableMessage = c.ReliableChannel.CreateMessage();
|
||||
reliableMessage.InnerMessage.Write((byte)PacketTypes.NetworkEvent);
|
||||
|
||||
if (!networkEvent.FillData(reliableMessage.InnerMessage))
|
||||
{
|
||||
break;
|
||||
}
|
||||
message.Position = 0;
|
||||
reliableMessage.InnerMessage.Write(message.ReadBytes(message.LengthBytes));
|
||||
|
||||
c.ReliableChannel.SendMessage(reliableMessage, c.Connection);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
NetOutgoingMessage message = server.CreateMessage();
|
||||
message.Write((byte)PacketTypes.NetworkEvent);
|
||||
//if (!networkEvent.IsClient) continue;
|
||||
|
||||
if (!networkEvent.FillData(message))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (server.ConnectionsCount>0)
|
||||
{
|
||||
@@ -1050,7 +1071,6 @@ namespace Barotrauma.Networking
|
||||
message.Write(name);
|
||||
message.Write(character.ID);
|
||||
message.Write(character.Info.Gender == Gender.Female);
|
||||
message.Write(character.Inventory.ID);
|
||||
|
||||
message.Write(character.Info.HeadSpriteId);
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ namespace Barotrauma.Networking
|
||||
return false;
|
||||
}
|
||||
|
||||
System.Diagnostics.Debug.WriteLine("Networkevent entity: "+e.ToString());
|
||||
//System.Diagnostics.Debug.WriteLine("Networkevent entity: "+e.ToString());
|
||||
|
||||
//System.Diagnostics.Debug.WriteLine("new message: " + eventType +" - "+e);
|
||||
try
|
||||
|
||||
@@ -79,16 +79,14 @@ namespace Barotrauma.Networking.ReliableMessages
|
||||
|
||||
public ReliableMessage CreateMessage()
|
||||
{
|
||||
if (messageCount == ushort.MaxValue) messageCount = 0;
|
||||
messageCount++;
|
||||
ushort messageID = (messageCount==ushort.MaxValue) ? (ushort)0 : (ushort)(messageCount + 1);
|
||||
|
||||
NetOutgoingMessage message = sender.CreateMessage();
|
||||
|
||||
var reliableMessage = new ReliableMessage(message, messageCount);
|
||||
messageBuffer.Add(reliableMessage.ID, reliableMessage);
|
||||
var reliableMessage = new ReliableMessage(message, messageID);
|
||||
|
||||
message.Write((byte)PacketTypes.ReliableMessage);
|
||||
message.Write(messageCount);
|
||||
message.Write(messageID);
|
||||
|
||||
int bufferSize=100;
|
||||
if (messageBuffer.Count>bufferSize)
|
||||
@@ -117,10 +115,7 @@ namespace Barotrauma.Networking.ReliableMessages
|
||||
messageBuffer.Remove(i);
|
||||
if (i == ushort.MaxValue) break;
|
||||
Debug.WriteLine("removing message " + i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return reliableMessage;
|
||||
@@ -133,6 +128,11 @@ namespace Barotrauma.Networking.ReliableMessages
|
||||
ackInterval = 0.0f;
|
||||
ackTimer = connection.AverageRoundtripTime;
|
||||
|
||||
messageBuffer.Add(message.ID, message);
|
||||
|
||||
if (messageCount == ushort.MaxValue) messageCount = 0;
|
||||
messageCount++;
|
||||
|
||||
message.SaveInnerMessage();
|
||||
|
||||
sender.SendMessage(message.InnerMessage, connection, NetDeliveryMethod.Unreliable, 0);
|
||||
@@ -172,7 +172,7 @@ namespace Barotrauma.Networking.ReliableMessages
|
||||
|
||||
if (ackTimer > 0.0f) return;
|
||||
|
||||
Debug.WriteLine("Sending ack message: "+messageCount);
|
||||
//Debug.WriteLine("Sending ack message: "+messageCount);
|
||||
|
||||
NetOutgoingMessage message = sender.CreateMessage();
|
||||
message.Write((byte)PacketTypes.Ack);
|
||||
@@ -337,7 +337,7 @@ namespace Barotrauma.Networking.ReliableMessages
|
||||
//id matches, all good
|
||||
if (messageId == lastMessageID)
|
||||
{
|
||||
Debug.WriteLine("Received ack message: " + messageId + ", all good");
|
||||
//Debug.WriteLine("Received ack message: " + messageId + ", all good");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user