Changed entity ids from int to ushort, inventory sync bugfixes

This commit is contained in:
Regalis
2015-10-21 18:58:36 +03:00
parent 0233579e37
commit 313d16d886
20 changed files with 155 additions and 78 deletions

View File

@@ -534,7 +534,7 @@ namespace Barotrauma
//message.WriteRangedSingle(MathHelper.Clamp(raycastTimer, 0.0f, RaycastInterval), 0.0f, RaycastInterval, 8);
//message.WriteRangedSingle(MathHelper.Clamp(coolDownTimer, 0.0f, attackCoolDown * 2.0f), 0.0f, attackCoolDown * 2.0f, 8);
message.Write(targetEntity==null ? -1 : (targetEntity as Entity).ID);
message.Write(targetEntity==null ? (ushort)0 : (targetEntity as Entity).ID);
}
public override void ReadNetworkData(NetIncomingMessage message)
@@ -548,7 +548,7 @@ namespace Barotrauma
Vector2 targetPosition = Vector2.Zero;
int targetID;
ushort targetID;
try
{
@@ -571,7 +571,7 @@ namespace Barotrauma
//raycastTimer = message.ReadRangedSingle(0.0f, RaycastInterval, 8);
//coolDownTimer = message.ReadRangedSingle(0.0f, attackCoolDown*2.0f, 8);
targetID = message.ReadInt32();
targetID = message.ReadUInt16();
}
catch { return; }
@@ -583,8 +583,7 @@ namespace Barotrauma
//this.raycastTimer = raycastTimer;
//this.coolDownTimer = coolDownTimer;
if (targetID > -1)
targetEntity = Entity.FindEntityByID(targetID) as IDamageable;
if (targetID > 0) targetEntity = Entity.FindEntityByID(targetID) as IDamageable;
}
}

View File

@@ -17,7 +17,7 @@ namespace Barotrauma
public Job Job;
private List<int> pickedItems;
private List<ushort> pickedItems;
private Vector2[] headSpriteRange;
@@ -30,7 +30,7 @@ namespace Barotrauma
public bool StartItemsGiven;
public List<int> PickedItemIDs
public List<ushort> PickedItemIDs
{
get { return pickedItems; }
}
@@ -72,7 +72,7 @@ namespace Barotrauma
headSpriteRange = new Vector2[2];
pickedItems = new List<int>();
pickedItems = new List<ushort>();
//ID = -1;
@@ -218,7 +218,7 @@ namespace Barotrauma
HeadSpriteId = ToolBox.GetAttributeInt(element, "headspriteid", 1);
StartItemsGiven = ToolBox.GetAttributeBool(element, "startitemsgiven", false);
pickedItems = new List<int>();
pickedItems = new List<ushort>();
string pickedItemString = ToolBox.GetAttributeString(element, "items", "");
if (!string.IsNullOrEmpty(pickedItemString))
@@ -226,7 +226,7 @@ namespace Barotrauma
string[] itemIds = pickedItemString.Split(',');
foreach (string s in itemIds)
{
pickedItems.Add(int.Parse(s));
pickedItems.Add((ushort)int.Parse(s));
}
}

View File

@@ -685,11 +685,14 @@ namespace Barotrauma
//limb.body.SetTransform(limb.SimPosition + newMovement * 0.1f, limb.Rotation);
}
correctionMovement = Vector2.Normalize(newMovement) * MathHelper.Clamp(dist*5.0f, 0.1f, 5.0f);
correctionMovement =
Vector2.Lerp(targetMovement, Vector2.Normalize(newMovement) * MathHelper.Clamp(dist * 5.0f, 0.1f, 5.0f), 0.2f);
}
else
{
correctionMovement = Vector2.Normalize(newMovement) * MathHelper.Clamp(dist * 5.0f, 0.1f, 5.0f);
correctionMovement =
Vector2.Lerp(targetMovement, Vector2.Normalize(newMovement) * MathHelper.Clamp(dist * 5.0f, 0.1f, 5.0f), 0.2f);
if (Math.Abs(correctionMovement.Y) < 0.1f) correctionMovement.Y = 0.0f;
}
}

View File

@@ -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);
}
}
}
}

View File

@@ -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)));

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);
}
}
}
}

View File

@@ -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>();

View File

@@ -7,15 +7,16 @@ namespace Barotrauma
{
class Entity
{
private static Dictionary<int, Entity> dictionary = new Dictionary<int, Entity>();
private static Dictionary<ushort, Entity> dictionary = new Dictionary<ushort, Entity>();
private int id;
private ushort id;
protected AITarget aiTarget;
//protected float soundRange;
//protected float sightRange;
public int ID
public ushort ID
{
get { return id; }
set
@@ -73,7 +74,7 @@ namespace Barotrauma
/// <summary>
/// Find an entity based on the ID
/// </summary>
public static Entity FindEntityByID(int ID)
public static Entity FindEntityByID(ushort ID)
{
Entity matchingEntity;
dictionary.TryGetValue(ID, out matchingEntity);

View File

@@ -607,7 +607,7 @@ namespace Barotrauma
int.Parse(element.Attribute("height").Value));
Gap g = new Gap(rect);
g.ID = int.Parse(element.Attribute("ID").Value);
g.ID = (ushort)int.Parse(element.Attribute("ID").Value);
g.linkedToID = new List<int>();
//int i = 0;

View File

@@ -468,7 +468,7 @@ namespace Barotrauma
h.volume = ToolBox.GetAttributeFloat(element, "pressure", 0.0f);
h.ID = int.Parse(element.Attribute("ID").Value);
h.ID = (ushort)int.Parse(element.Attribute("ID").Value);
}
public override bool FillNetworkData(Networking.NetworkEventType type, Lidgren.Network.NetOutgoingMessage message, object data)

View File

@@ -515,7 +515,7 @@ namespace Barotrauma
e.linkedTo.Clear();
foreach (int i in e.linkedToID)
foreach (ushort i in e.linkedToID)
{
MapEntity linked = FindEntityByID(i) as MapEntity;

View File

@@ -609,7 +609,7 @@ namespace Barotrauma
if (ep.Name == name)
{
s = new Structure(rect, (StructurePrefab)ep);
s.ID = int.Parse(element.Attribute("ID").Value);
s.ID = (ushort)int.Parse(element.Attribute("ID").Value);
break;
}
}

View File

@@ -149,7 +149,7 @@ namespace Barotrauma
}
base.Remove();
ID = -5;
ID = ushort.MaxValue;
}
//drawing ----------------------------------------------------
@@ -635,7 +635,7 @@ namespace Barotrauma
GameMain.LightManager.OnMapLoaded();
ID = int.MaxValue-10;
ID = ushort.MaxValue-10;
loaded = this;
}

View File

@@ -312,7 +312,7 @@ namespace Barotrauma
WayPoint w = new WayPoint(rect);
w.ID = int.Parse(element.Attribute("ID").Value);
w.ID = (ushort)int.Parse(element.Attribute("ID").Value);
w.spawnType = (SpawnType)Enum.Parse(typeof(SpawnType),
ToolBox.GetAttributeString(element, "spawn", "None"));

View File

@@ -347,7 +347,7 @@ namespace Barotrauma.Networking
}
else if (gameStarted)
{
new NetworkEvent(myCharacter.ID, true);
myCharacter.SendNetworkEvent(true);
}
}
@@ -525,7 +525,7 @@ namespace Barotrauma.Networking
List<Character> crew = new List<Character>();
int count = inc.ReadInt32();
int count = inc.ReadByte();
for (int n = 0; n < count; n++)
{
int id = inc.ReadInt32();
@@ -631,7 +631,7 @@ namespace Barotrauma.Networking
msg.Write((byte)PacketTypes.CharacterInfo);
msg.Write(characterInfo.Name);
msg.Write(characterInfo.Gender == Gender.Male);
msg.Write(characterInfo.HeadSpriteId);
msg.Write((byte)characterInfo.HeadSpriteId);
var jobPreferences = GameMain.NetLobbyScreen.JobPreferences;
int count = Math.Min(jobPreferences.Count, 3);
@@ -647,10 +647,10 @@ namespace Barotrauma.Networking
private Character ReadCharacterData(NetIncomingMessage inc, bool isMyCharacter)
{
string newName = inc.ReadString();
int ID = inc.ReadInt32();
ushort ID = inc.ReadUInt16();
bool isFemale = inc.ReadBoolean();
int headSpriteID = inc.ReadInt32();
int headSpriteID = inc.ReadByte();
Vector2 position = new Vector2(inc.ReadFloat(), inc.ReadFloat());
@@ -719,7 +719,7 @@ namespace Barotrauma.Networking
{
case 0:
msg.Write((byte)PacketTypes.NetworkEvent);
msg.Write((byte)NetworkEventType.UpdateEntity);
msg.Write((byte)NetworkEventType.EntityUpdate);
msg.Write(Rand.Int(MapEntity.mapEntityList.Count));
break;
case 1:

View File

@@ -297,7 +297,7 @@ namespace Barotrauma.Networking
{
if (gameStarted)
{
if (myCharacter != null) new NetworkEvent(myCharacter.ID, true);
if (myCharacter != null) myCharacter.SendNetworkEvent(true);
foreach (Character c in Character.CharacterList)
{
@@ -305,7 +305,7 @@ namespace Barotrauma.Networking
if (c.SimPosition == Vector2.Zero || c.SimPosition.Length() < 100.0f)
{
new NetworkEvent(c.ID, false);
c.SendNetworkEvent(false);
}
}
}
@@ -678,8 +678,6 @@ namespace Barotrauma.Networking
}
else
{
if (server.ConnectionsCount>0)
{
server.SendMessage(message, recipientConnections, NetDeliveryMethod.Unreliable, 0);
@@ -777,7 +775,7 @@ namespace Barotrauma.Networking
//msg.Write(GameMain.NetLobbyScreen.GameDuration.TotalMinutes);
msg.Write((myCharacter == null) ? connectedClients.Count : connectedClients.Count + 1);
msg.Write((myCharacter == null) ? (byte)connectedClients.Count : (byte)(connectedClients.Count + 1));
foreach (Client client in connectedClients)
{
msg.Write(client.ID);
@@ -1036,7 +1034,7 @@ namespace Barotrauma.Networking
{
name = message.ReadString();
gender = message.ReadBoolean() ? Gender.Male : Gender.Female;
headSpriteId = message.ReadInt32();
headSpriteId = message.ReadByte();
}
catch
{
@@ -1072,7 +1070,7 @@ namespace Barotrauma.Networking
message.Write(character.ID);
message.Write(character.Info.Gender == Gender.Female);
message.Write(character.Info.HeadSpriteId);
message.Write((byte)character.Info.HeadSpriteId);
message.Write(character.SimPosition.X);
message.Write(character.SimPosition.Y);

View File

@@ -1,11 +1,12 @@
using System.Collections.Generic;
using Lidgren.Network;
using System;
namespace Barotrauma.Networking
{
enum NetworkEventType
{
UpdateEntity = 0,
EntityUpdate = 0,
KillCharacter = 1,
UpdateComponent = 2,
DropItem = 3,
@@ -13,17 +14,19 @@ namespace Barotrauma.Networking
PickItem = 5,
UpdateProperty = 6,
WallDamage = 7,
SelectCharacter = 8
SelectCharacter = 8,
EntityUpdateLarge = 9
}
class NetworkEvent
{
public static List<NetworkEvent> events = new List<NetworkEvent>();
private static bool[] isImportant = { false, true, false, true, true, true, true, true, true };
private static bool[] overridePrevious = { true, false, true, false, false, false, true, true, true };
private static bool[] isImportant = { false, true, false, true, true, true, true, true, true, false };
private static bool[] overridePrevious = { true, false, true, false, false, false, true, true, true, true };
private int id;
private ushort id;
private NetworkEventType eventType;
@@ -33,7 +36,7 @@ namespace Barotrauma.Networking
//private NetOutgoingMessage message;
public int ID
public ushort ID
{
get { return id; }
}
@@ -53,12 +56,12 @@ namespace Barotrauma.Networking
get { return eventType; }
}
public NetworkEvent(int id, bool isClient)
: this(NetworkEventType.UpdateEntity, id, isClient)
public NetworkEvent(ushort id, bool isClient)
: this(NetworkEventType.EntityUpdate, id, isClient)
{
}
public NetworkEvent(NetworkEventType type, int id, bool isClient, object data = null)
public NetworkEvent(NetworkEventType type, ushort id, bool isClient, object data = null)
{
if (isClient)
{
@@ -109,12 +112,12 @@ namespace Barotrauma.Networking
public static bool ReadData(NetIncomingMessage message)
{
NetworkEventType eventType;
int id;
ushort id;
try
{
eventType = (NetworkEventType)message.ReadByte();
id = message.ReadInt32();
id = message.ReadUInt16();
}
catch
{
@@ -129,16 +132,15 @@ namespace Barotrauma.Networking
return false;
}
//System.Diagnostics.Debug.WriteLine("Networkevent entity: "+e.ToString());
//System.Diagnostics.Debug.WriteLine("new message: " + eventType +" - "+e);
try
{
e.ReadNetworkData(eventType, message);
}
catch
catch (Exception exception)
{
DebugConsole.ThrowError("Received invalid network message");
#if DEBUG
DebugConsole.ThrowError("Received invalid network message", exception);
#endif
return false;
}

Binary file not shown.