Changed entity ids from int to ushort, inventory sync bugfixes
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Barotrauma.Networking;
|
||||
using Lidgren.Network;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -292,5 +295,71 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
public override bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
{
|
||||
for (int i = 0; i < 5; i++ )
|
||||
{
|
||||
message.Write(items[i]==null ? (ushort)0 : (ushort)items[i].ID);
|
||||
}
|
||||
|
||||
for (int i = 5; i < capacity; i++)
|
||||
{
|
||||
if (items[i] == null) continue;
|
||||
message.Write((ushort)items[i].ID);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
{
|
||||
for (int i = 0; i<5; i++)
|
||||
{
|
||||
ushort itemId = message.ReadUInt16();
|
||||
if (itemId==0)
|
||||
{
|
||||
if (items[i] != null) items[i].Drop(character, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Item item = Entity.FindEntityByID(itemId) as Item;
|
||||
if (item == null) continue;
|
||||
|
||||
TryPutItem(item, i, false);
|
||||
}
|
||||
}
|
||||
|
||||
List<ushort> newItemIDs = new List<ushort>();
|
||||
|
||||
try
|
||||
{
|
||||
while (message.Position <= message.LengthBits - (sizeof(ushort) * 8))
|
||||
{
|
||||
newItemIDs.Add(message.ReadUInt16());
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 5; i < capacity; i++)
|
||||
{
|
||||
if (items[i] == null) continue;
|
||||
if (!newItemIDs.Contains(items[i].ID))
|
||||
{
|
||||
items[i].Drop(null, false);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
foreach (ushort itemId in newItemIDs)
|
||||
{
|
||||
Item item = Entity.FindEntityByID(itemId) as Item;
|
||||
if (item == null) continue;
|
||||
|
||||
TryPutItem(item, LimbSlot.Any, false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (itemIds == null) return;
|
||||
|
||||
for (int i = 0; i < itemIds.Length; i++)
|
||||
for (ushort i = 0; i < itemIds.Length; i++)
|
||||
{
|
||||
Item item = MapEntity.FindEntityByID(itemIds[i]) as Item;
|
||||
if (item == null) continue;
|
||||
@@ -247,17 +247,17 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
string[] itemIdStrings = containedString.Split(',');
|
||||
|
||||
itemIds = new int[itemIdStrings.Length];
|
||||
itemIds = new ushort[itemIdStrings.Length];
|
||||
for (int i = 0; i < itemIdStrings.Length; i++)
|
||||
{
|
||||
int id = -1;
|
||||
if (!int.TryParse(itemIdStrings[i], out id)) continue;
|
||||
ushort id = 0;
|
||||
if (!ushort.TryParse(itemIdStrings[i], out id)) continue;
|
||||
|
||||
itemIds[i] = id;
|
||||
}
|
||||
}
|
||||
|
||||
int[] itemIds;
|
||||
ushort[] itemIds;
|
||||
|
||||
public override XElement Save(XElement parentElement)
|
||||
{
|
||||
@@ -266,7 +266,7 @@ namespace Barotrauma.Items.Components
|
||||
string[] itemIdStrings = new string[inventory.items.Length];
|
||||
for (int i = 0; i < inventory.items.Length; i++)
|
||||
{
|
||||
itemIdStrings[i] = (inventory.items[i]==null) ? "-1" : inventory.items[i].ID.ToString();
|
||||
itemIdStrings[i] = (inventory.items[i]==null) ? "0" : inventory.items[i].ID.ToString();
|
||||
}
|
||||
|
||||
componentElement.Add(new XAttribute("contained", string.Join(",",itemIdStrings)));
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
private List<StatusEffect> effects;
|
||||
|
||||
public readonly int[] wireId;
|
||||
public readonly ushort[] wireId;
|
||||
|
||||
public bool IsPower
|
||||
{
|
||||
@@ -79,7 +79,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
effects = new List<StatusEffect>();
|
||||
|
||||
wireId = new int[MaxLinked];
|
||||
wireId = new ushort[MaxLinked];
|
||||
|
||||
foreach (XElement subElement in element.Elements())
|
||||
{
|
||||
@@ -93,7 +93,9 @@ namespace Barotrauma.Items.Components
|
||||
}
|
||||
if (index == -1) break;
|
||||
|
||||
wireId[index] = ToolBox.GetAttributeInt(subElement, "w", -1);
|
||||
int id = ToolBox.GetAttributeInt(subElement, "w", 0);
|
||||
if (id<0) id = 0;
|
||||
wireId[index] = (ushort)id;
|
||||
|
||||
break;
|
||||
|
||||
@@ -441,7 +443,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
for (int i = 0; i < MaxLinked; i++)
|
||||
{
|
||||
if (wireId[i] == -1) continue;
|
||||
if (wireId[i] == 0) continue;
|
||||
|
||||
Item wireItem = MapEntity.FindEntityByID(wireId[i]) as Item;
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
for (int i = 0; i < wireCount; i++)
|
||||
{
|
||||
int wireId = message.ReadInt32();
|
||||
ushort wireId = message.ReadUInt16();
|
||||
|
||||
Item wireItem = MapEntity.FindEntityByID(wireId) as Item;
|
||||
if (wireItem == null) continue;
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Barotrauma.Networking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
@@ -193,7 +194,7 @@ namespace Barotrauma
|
||||
{
|
||||
if (Owner!=null)
|
||||
{
|
||||
int[] data = { draggingItem.ID, -1 };
|
||||
ushort[] data = { draggingItem.ID, 0 };
|
||||
new NetworkEvent(NetworkEventType.InventoryUpdate, Owner.ID, true, data);
|
||||
}
|
||||
|
||||
@@ -288,25 +289,27 @@ namespace Barotrauma
|
||||
spriteBatch.DrawString(GUI.Font, (int)item.Condition + " %", new Vector2(rect.X + rect.Width / 2, rect.Y + rect.Height / 2), Color.Red);
|
||||
}
|
||||
|
||||
public bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
public virtual bool FillNetworkData(NetworkEventType type, NetOutgoingMessage message, object data)
|
||||
{
|
||||
for (int i = 0; i<capacity; i++)
|
||||
{
|
||||
message.Write((items[i] == null) ? -1 : items[i].ID);
|
||||
if (items[i] == null) continue;
|
||||
message.Write((ushort)items[i].ID);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
public virtual void ReadNetworkData(NetworkEventType type, NetIncomingMessage message)
|
||||
{
|
||||
int[] newItemIDs = new int[capacity];
|
||||
List<ushort> newItemIDs = new List<ushort>();
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i<capacity; i++)
|
||||
|
||||
while (message.Position <= message.LengthBits - (sizeof(ushort) * 8))
|
||||
{
|
||||
newItemIDs[i] = message.ReadInt32();
|
||||
newItemIDs.Add(message.ReadUInt16());
|
||||
}
|
||||
}
|
||||
catch
|
||||
@@ -316,20 +319,20 @@ namespace Barotrauma
|
||||
|
||||
for (int i = 0; i < capacity; i++)
|
||||
{
|
||||
if (newItemIDs[i] == -1)
|
||||
if (items[i] == null) continue;
|
||||
if (!newItemIDs.Contains(items[i].ID))
|
||||
{
|
||||
if (items[i] == null) continue;
|
||||
|
||||
items[i].Drop(null, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
Item item = Entity.FindEntityByID(newItemIDs[i]) as Item;
|
||||
}
|
||||
foreach (ushort itemId in newItemIDs)
|
||||
{
|
||||
Item item = Entity.FindEntityByID(itemId) as Item;
|
||||
if (item == null) continue;
|
||||
|
||||
TryPutItem(item, i, false);
|
||||
TryPutItem(item, item.AllowedSlots, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1101,7 +1101,7 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
Item item = new Item(rect, ip);
|
||||
item.ID = int.Parse(element.Attribute("ID").Value);
|
||||
item.ID = (ushort)int.Parse(element.Attribute("ID").Value);
|
||||
|
||||
item.linkedToID = new List<int>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user