diff --git a/Subsurface/Source/Characters/AI/EnemyAIController.cs b/Subsurface/Source/Characters/AI/EnemyAIController.cs index cdf6666bc..1e75e725e 100644 --- a/Subsurface/Source/Characters/AI/EnemyAIController.cs +++ b/Subsurface/Source/Characters/AI/EnemyAIController.cs @@ -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; } } diff --git a/Subsurface/Source/Characters/CharacterInfo.cs b/Subsurface/Source/Characters/CharacterInfo.cs index e44b25d2a..4ed7af120 100644 --- a/Subsurface/Source/Characters/CharacterInfo.cs +++ b/Subsurface/Source/Characters/CharacterInfo.cs @@ -17,7 +17,7 @@ namespace Barotrauma public Job Job; - private List pickedItems; + private List pickedItems; private Vector2[] headSpriteRange; @@ -30,7 +30,7 @@ namespace Barotrauma public bool StartItemsGiven; - public List PickedItemIDs + public List PickedItemIDs { get { return pickedItems; } } @@ -72,7 +72,7 @@ namespace Barotrauma headSpriteRange = new Vector2[2]; - pickedItems = new List(); + pickedItems = new List(); //ID = -1; @@ -218,7 +218,7 @@ namespace Barotrauma HeadSpriteId = ToolBox.GetAttributeInt(element, "headspriteid", 1); StartItemsGiven = ToolBox.GetAttributeBool(element, "startitemsgiven", false); - pickedItems = new List(); + pickedItems = new List(); 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)); } } diff --git a/Subsurface/Source/Characters/Ragdoll.cs b/Subsurface/Source/Characters/Ragdoll.cs index fe78963fc..d36eb7595 100644 --- a/Subsurface/Source/Characters/Ragdoll.cs +++ b/Subsurface/Source/Characters/Ragdoll.cs @@ -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; } } diff --git a/Subsurface/Source/Items/CharacterInventory.cs b/Subsurface/Source/Items/CharacterInventory.cs index e17b11c50..c1b470c64 100644 --- a/Subsurface/Source/Items/CharacterInventory.cs +++ b/Subsurface/Source/Items/CharacterInventory.cs @@ -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 newItemIDs = new List(); + + 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); + } + } + } } diff --git a/Subsurface/Source/Items/Components/ItemContainer.cs b/Subsurface/Source/Items/Components/ItemContainer.cs index 0bb746595..e8ccd3022 100644 --- a/Subsurface/Source/Items/Components/ItemContainer.cs +++ b/Subsurface/Source/Items/Components/ItemContainer.cs @@ -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))); diff --git a/Subsurface/Source/Items/Components/Signal/Connection.cs b/Subsurface/Source/Items/Components/Signal/Connection.cs index 191defac1..16d91ed27 100644 --- a/Subsurface/Source/Items/Components/Signal/Connection.cs +++ b/Subsurface/Source/Items/Components/Signal/Connection.cs @@ -28,7 +28,7 @@ namespace Barotrauma.Items.Components private List effects; - public readonly int[] wireId; + public readonly ushort[] wireId; public bool IsPower { @@ -79,7 +79,7 @@ namespace Barotrauma.Items.Components effects = new List(); - 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; diff --git a/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs b/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs index 26174d8fe..478e2681d 100644 --- a/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs +++ b/Subsurface/Source/Items/Components/Signal/ConnectionPanel.cs @@ -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; diff --git a/Subsurface/Source/Items/Inventory.cs b/Subsurface/Source/Items/Inventory.cs index 4f34771ac..558fa1456 100644 --- a/Subsurface/Source/Items/Inventory.cs +++ b/Subsurface/Source/Items/Inventory.cs @@ -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 newItemIDs = new List(); try { - for (int i = 0; i(); diff --git a/Subsurface/Source/Map/Entity.cs b/Subsurface/Source/Map/Entity.cs index 835ccdcec..1449ad68e 100644 --- a/Subsurface/Source/Map/Entity.cs +++ b/Subsurface/Source/Map/Entity.cs @@ -7,15 +7,16 @@ namespace Barotrauma { class Entity { - private static Dictionary dictionary = new Dictionary(); + private static Dictionary dictionary = new Dictionary(); - 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 /// /// Find an entity based on the ID /// - public static Entity FindEntityByID(int ID) + public static Entity FindEntityByID(ushort ID) { Entity matchingEntity; dictionary.TryGetValue(ID, out matchingEntity); diff --git a/Subsurface/Source/Map/Gap.cs b/Subsurface/Source/Map/Gap.cs index e7490e2b3..24408afe3 100644 --- a/Subsurface/Source/Map/Gap.cs +++ b/Subsurface/Source/Map/Gap.cs @@ -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 i = 0; diff --git a/Subsurface/Source/Map/Hull.cs b/Subsurface/Source/Map/Hull.cs index b75ef5671..97244e295 100644 --- a/Subsurface/Source/Map/Hull.cs +++ b/Subsurface/Source/Map/Hull.cs @@ -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) diff --git a/Subsurface/Source/Map/MapEntity.cs b/Subsurface/Source/Map/MapEntity.cs index 1cc9cbc74..4722963ba 100644 --- a/Subsurface/Source/Map/MapEntity.cs +++ b/Subsurface/Source/Map/MapEntity.cs @@ -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; diff --git a/Subsurface/Source/Map/Structure.cs b/Subsurface/Source/Map/Structure.cs index d8858e61b..c5b16eb73 100644 --- a/Subsurface/Source/Map/Structure.cs +++ b/Subsurface/Source/Map/Structure.cs @@ -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; } } diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index b52386284..1ef28c468 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -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; } diff --git a/Subsurface/Source/Map/WayPoint.cs b/Subsurface/Source/Map/WayPoint.cs index 4f1b8ae7e..c63a00e2a 100644 --- a/Subsurface/Source/Map/WayPoint.cs +++ b/Subsurface/Source/Map/WayPoint.cs @@ -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")); diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 69131d260..d919ab677 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -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 crew = new List(); - 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: diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index cf492b216..16ec29e2d 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -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); diff --git a/Subsurface/Source/Networking/NetworkEvent.cs b/Subsurface/Source/Networking/NetworkEvent.cs index d511149a2..b2b3c2f94 100644 --- a/Subsurface/Source/Networking/NetworkEvent.cs +++ b/Subsurface/Source/Networking/NetworkEvent.cs @@ -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 events = new List(); - 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; } diff --git a/Subsurface_Solution.v12.suo b/Subsurface_Solution.v12.suo index 20d8381f5..2f3da2e21 100644 Binary files a/Subsurface_Solution.v12.suo and b/Subsurface_Solution.v12.suo differ