diff --git a/Subsurface/Barotrauma.csproj b/Subsurface/Barotrauma.csproj index e01deafe2..9909da4cb 100644 --- a/Subsurface/Barotrauma.csproj +++ b/Subsurface/Barotrauma.csproj @@ -151,6 +151,7 @@ + diff --git a/Subsurface/Source/Characters/Character.cs b/Subsurface/Source/Characters/Character.cs index d206ef057..0ae815d10 100644 --- a/Subsurface/Source/Characters/Character.cs +++ b/Subsurface/Source/Characters/Character.cs @@ -15,12 +15,18 @@ using System.Xml.Linq; namespace Barotrauma { - class Character : Entity, IDamageable, IPropertyObject + class Character : Entity, IDamageable, IPropertyObject, IClientSerializable, IServerSerializable { public static List CharacterList = new List(); public static bool DisableControls; + private ushort netStateID; + public ushort NetStateID + { + get { return netStateID; } + } + //the Character that the player is currently controlling private static Character controlled; @@ -1567,5 +1573,25 @@ namespace Barotrauma if (AnimController!=null) AnimController.Remove(); } + public virtual void ClientWrite(NetOutgoingMessage msg) + { + //TODO: write inputs + } + public virtual void ServerRead(NetIncomingMessage msg) + { + //TODO: read inputs + } + + public virtual void ServerWrite(NetOutgoingMessage msg) + { + //TODO: write position, health, etc + } + + public virtual void ClientRead(NetIncomingMessage msg) + { + //TODO: read positions health, etc + } + + } } diff --git a/Subsurface/Source/Items/Components/ItemComponent.cs b/Subsurface/Source/Items/Components/ItemComponent.cs index 92855dbd1..412e5ebc8 100644 --- a/Subsurface/Source/Items/Components/ItemComponent.cs +++ b/Subsurface/Source/Items/Components/ItemComponent.cs @@ -43,7 +43,7 @@ namespace Barotrauma.Items.Components /// /// The base class for components holding the different functionalities of the item /// - class ItemComponent : IPropertyObject + class ItemComponent : IPropertyObject, IClientSerializable, IServerSerializable { protected Item item; @@ -74,6 +74,12 @@ namespace Barotrauma.Items.Components private string msg; + protected ushort netStateID; + public ushort NetStateID + { + get { return netStateID; } + } + [HasDefaultValue(0.0f, false)] public float PickingTime { @@ -577,22 +583,6 @@ namespace Barotrauma.Items.Components return (average+100.0f)/2.0f; } - //public bool CheckFailure(Character Character) - //{ - // foreach (Skill skill in requiredSkills) - // { - // int characterLevel = Character.GetSkillLevel(skill.Name); - // if (characterLevel > skill.Level) continue; - - // item.ApplyStatusEffects(ActionType.OnFailure, 1.0f, Character); - // //Item.ApplyStatusEffects(); - // return true; - - // } - - // return false; - //} - public bool HasRequiredContainedItems(bool addMessage) { List requiredContained = requiredItems.FindAll(ri=> ri.Type == RelatedItem.RelationType.Contained); @@ -668,6 +658,12 @@ namespace Barotrauma.Items.Components } } + public virtual void ClientWrite(NetOutgoingMessage msg) { } + public virtual void ServerRead(NetIncomingMessage msg) { } + + public virtual void ServerWrite(NetOutgoingMessage msg) { } + public virtual void ClientRead(NetIncomingMessage msg) { } + public virtual XElement Save(XElement parentElement) { XElement componentElement = new XElement(name); @@ -681,26 +677,6 @@ namespace Barotrauma.Items.Components ObjectProperty.SaveProperties(this, componentElement); - //var saveProperties = ObjectProperty.GetProperties(this); - //foreach (var property in saveProperties) - //{ - // object value = property.GetValue(); - // if (value == null) continue; - - // bool dontSave = false; - // foreach (var ini in property.Attributes.OfType()) - // { - // if (ini.defaultValue != value) continue; - - // dontSave = true; - // break; - // } - - // if (dontSave) continue; - - // componentElement.Add(new XAttribute(property.Name.ToLower(), value)); - //} - parentElement.Add(componentElement); return componentElement; } diff --git a/Subsurface/Source/Items/Item.cs b/Subsurface/Source/Items/Item.cs index 84adfe5a9..80361ee0b 100644 --- a/Subsurface/Source/Items/Item.cs +++ b/Subsurface/Source/Items/Item.cs @@ -26,7 +26,7 @@ namespace Barotrauma OnImpact } - class Item : MapEntity, IDamageable, IPropertyObject + class Item : MapEntity, IDamageable, IPropertyObject, IServerSerializable { public static List ItemList = new List(); private ItemPrefab prefab; @@ -55,6 +55,12 @@ namespace Barotrauma //a dictionary containing lists of the status effects in all the components of the item private Dictionary> statusEffectLists; + private ushort netStateID; + public ushort NetStateID + { + get { return netStateID; } + } + public readonly Dictionary properties; public Dictionary ObjectProperties { @@ -1383,12 +1389,6 @@ namespace Barotrauma public void Drop(Character dropper = null) { - //if (dropper == Character.Controlled) - // new NetworkEvent(NetworkEventType.DropItem, ID, true); - - - //if (dropper != null) GameServer.Log(dropper.Name + " dropped " + Name, Color.Orange); - foreach (ItemComponent ic in components) ic.Drop(dropper); if (Container != null) Container.RemoveContained(this); @@ -1514,6 +1514,9 @@ namespace Barotrauma return element; } + + public void ServerWrite(NetOutgoingMessage msg) { } + public void ClientRead(NetIncomingMessage msg) { } public static void Load(XElement element, Submarine submarine) { diff --git a/Subsurface/Source/Map/Structure.cs b/Subsurface/Source/Map/Structure.cs index 4d5e2dc5e..41f461cfe 100644 --- a/Subsurface/Source/Map/Structure.cs +++ b/Subsurface/Source/Map/Structure.cs @@ -23,7 +23,7 @@ namespace Barotrauma public int GapIndex; - public float lastSentDamage; + //public float lastSentDamage; public bool isHighLighted; public ConvexHull hull; @@ -41,7 +41,7 @@ namespace Barotrauma } } - class Structure : MapEntity, IDamageable + class Structure : MapEntity, IDamageable, IServerSerializable { public static int wallSectionSize = 100; public static List WallList = new List(); @@ -58,7 +58,11 @@ namespace Barotrauma bool isHorizontal; - public float lastUpdate; + private ushort netStateID; + public ushort NetStateID + { + get { return netStateID; } + } public override Sprite Sprite { @@ -570,10 +574,10 @@ namespace Barotrauma if (!MathUtils.IsValid(damage)) return; - if (damage != sections[sectionIndex].damage && Math.Abs(sections[sectionIndex].lastSentDamage - damage)>5.0f) - { - //sections[sectionIndex].lastSentDamage = damage; - } + //if (damage != sections[sectionIndex].damage && Math.Abs(sections[sectionIndex].lastSentDamage - damage) > 5.0f) + //{ + // sections[sectionIndex].lastSentDamage = damage; + //} if (damage < prefab.MaxHealth*0.5f) { @@ -685,6 +689,25 @@ namespace Barotrauma return newBody; } + public void ServerWrite(NetOutgoingMessage msg) + { + for (int i = 0; i < sections.Length; i++) + { + msg.WriteRangedSingle(sections[i].damage / Health, 0.0f, 1.0f, 8); + + //sections[i].lastSentDamage = sections[i].damage; + } + } + + public void ClientRead(NetIncomingMessage msg) + { + for (int i = 0; i < sections.Count(); i++) + { + float damage = msg.ReadRangedSingle(0.0f, 1.0f, 8) * Health; + + SetDamage(i, damage); + } + } public override XElement Save(XElement parentElement) { diff --git a/Subsurface/Source/Networking/INetSerializable.cs b/Subsurface/Source/Networking/INetSerializable.cs new file mode 100644 index 000000000..cba257b4f --- /dev/null +++ b/Subsurface/Source/Networking/INetSerializable.cs @@ -0,0 +1,26 @@ +using Lidgren.Network; + +namespace Barotrauma.Networking +{ + /// + /// Interface for entities that the clients can send information of to the server + /// + interface IClientSerializable + { + ushort NetStateID { get; } + + void ClientWrite(NetOutgoingMessage msg); + void ServerRead(NetIncomingMessage msg); + } + + /// + /// Interface for entities that the server can send information of to the clients + /// + interface IServerSerializable + { + ushort NetStateID { get; } + + void ServerWrite(NetOutgoingMessage msg); + void ClientRead(NetIncomingMessage msg); + } +}